|
| 1 | +//===----- SYCLDeviceRequirements.cpp - collect data for used aspects ----=-==// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#include "llvm/SYCLLowerIR/SYCLDeviceRequirements.h" |
| 10 | + |
| 11 | +#include "llvm/ADT/SmallString.h" |
| 12 | +#include "llvm/ADT/StringRef.h" |
| 13 | +#include "llvm/IR/Module.h" |
| 14 | +#include "llvm/SYCLLowerIR/ModuleSplitter.h" |
| 15 | +#include "llvm/Support/PropertySetIO.h" |
| 16 | + |
| 17 | +#include <set> |
| 18 | +#include <vector> |
| 19 | + |
| 20 | +using namespace llvm; |
| 21 | + |
| 22 | +static int64_t ExtractSignedIntegerFromMDNodeOperand(const MDNode *N, |
| 23 | + unsigned OpNo) { |
| 24 | + Constant *C = cast<ConstantAsMetadata>(N->getOperand(OpNo).get())->getValue(); |
| 25 | + return C->getUniqueInteger().getSExtValue(); |
| 26 | +} |
| 27 | +static uint64_t ExtractUnsignedIntegerFromMDNodeOperand(const MDNode *N, |
| 28 | + unsigned OpNo) { |
| 29 | + Constant *C = cast<ConstantAsMetadata>(N->getOperand(OpNo).get())->getValue(); |
| 30 | + return C->getUniqueInteger().getZExtValue(); |
| 31 | +} |
| 32 | +static llvm::StringRef ExtractStringFromMDNodeOperand(const MDNode *N, |
| 33 | + unsigned OpNo) { |
| 34 | + MDString *S = cast<llvm::MDString>(N->getOperand(OpNo).get()); |
| 35 | + return S->getString(); |
| 36 | +} |
| 37 | + |
| 38 | +SYCLDeviceRequirements |
| 39 | +llvm::computeDeviceRequirements(const module_split::ModuleDesc &MD) { |
| 40 | + SYCLDeviceRequirements Reqs; |
| 41 | + // Process all functions in the module |
| 42 | + for (const Function &F : MD.getModule()) { |
| 43 | + if (auto *MDN = F.getMetadata("sycl_used_aspects")) { |
| 44 | + for (size_t I = 0, E = MDN->getNumOperands(); I < E; ++I) { |
| 45 | + auto Val = ExtractSignedIntegerFromMDNodeOperand(MDN, I); |
| 46 | + // Don't put internal aspects (with negative integer value) into the |
| 47 | + // requirements, they are used only for device image splitting. |
| 48 | + if (Val >= 0) |
| 49 | + Reqs.Aspects.insert(Val); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + if (auto *MDN = F.getMetadata("sycl_fixed_targets")) { |
| 54 | + for (size_t I = 0, E = MDN->getNumOperands(); I < E; ++I) { |
| 55 | + auto Val = ExtractUnsignedIntegerFromMDNodeOperand(MDN, I); |
| 56 | + Reqs.FixedTarget.insert(Val); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + if (auto *MDN = F.getMetadata("reqd_work_group_size")) { |
| 61 | + llvm::SmallVector<uint64_t, 3> NewReqdWorkGroupSize; |
| 62 | + for (size_t I = 0, E = MDN->getNumOperands(); I < E; ++I) |
| 63 | + NewReqdWorkGroupSize.push_back( |
| 64 | + ExtractUnsignedIntegerFromMDNodeOperand(MDN, I)); |
| 65 | + if (!Reqs.ReqdWorkGroupSize.has_value()) |
| 66 | + Reqs.ReqdWorkGroupSize = NewReqdWorkGroupSize; |
| 67 | + } |
| 68 | + |
| 69 | + if (auto *MDN = F.getMetadata("sycl_joint_matrix")) { |
| 70 | + auto Val = ExtractStringFromMDNodeOperand(MDN, 0); |
| 71 | + if (!Val.empty()) |
| 72 | + Reqs.JointMatrix = Val; |
| 73 | + } |
| 74 | + |
| 75 | + if (auto *MDN = F.getMetadata("sycl_joint_matrix_mad")) { |
| 76 | + auto Val = ExtractStringFromMDNodeOperand(MDN, 0); |
| 77 | + if (!Val.empty()) |
| 78 | + Reqs.JointMatrixMad = Val; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + // Process just the entry points in the module |
| 83 | + for (const Function *F : MD.entries()) { |
| 84 | + if (auto *MDN = F->getMetadata("intel_reqd_sub_group_size")) { |
| 85 | + // There should only be at most one function with |
| 86 | + // intel_reqd_sub_group_size metadata when considering the entry |
| 87 | + // points of a module, but not necessarily when considering all the |
| 88 | + // functions of a module: an entry point with a |
| 89 | + // intel_reqd_sub_group_size can call an ESIMD function through |
| 90 | + // invoke_esimd, and that function has intel_reqd_sub_group_size=1, |
| 91 | + // which is valid. |
| 92 | + assert( |
| 93 | + MDN->getNumOperands() == 1 && |
| 94 | + "intel_reqd_sub_group_size metadata expects exactly one argument!"); |
| 95 | + auto MDValue = ExtractUnsignedIntegerFromMDNodeOperand(MDN, 0); |
| 96 | + if (!Reqs.SubGroupSize) |
| 97 | + Reqs.SubGroupSize = MDValue; |
| 98 | + else |
| 99 | + assert(*Reqs.SubGroupSize == static_cast<uint32_t>(MDValue)); |
| 100 | + } |
| 101 | + } |
| 102 | + return Reqs; |
| 103 | +} |
| 104 | + |
| 105 | +std::map<StringRef, util::PropertyValue> SYCLDeviceRequirements::asMap() const { |
| 106 | + std::map<StringRef, util::PropertyValue> Requirements; |
| 107 | + |
| 108 | + // For all properties except for "aspects", we'll only add the |
| 109 | + // value to the map if the corresponding value from |
| 110 | + // SYCLDeviceRequirements has a value/is non-empty. |
| 111 | + Requirements["aspects"] = |
| 112 | + std::vector<uint32_t>(Aspects.begin(), Aspects.end()); |
| 113 | + |
| 114 | + if (!FixedTarget.empty()) |
| 115 | + Requirements["fixed_target"] = |
| 116 | + std::vector<uint32_t>(FixedTarget.begin(), FixedTarget.end()); |
| 117 | + |
| 118 | + // TODO: Before intel/llvm#10620, the reqd_work_group_size attribute |
| 119 | + // stores its values as uint32_t, but this needed to be expanded to |
| 120 | + // uint64_t. However, this change did not happen in ABI-breaking |
| 121 | + // window, so we attach the required work-group size as the |
| 122 | + // reqd_work_group_size_uint64_t attribute. At the next ABI-breaking |
| 123 | + // window, this can be changed back to reqd_work_group_size. |
| 124 | + if (ReqdWorkGroupSize.has_value()) |
| 125 | + Requirements["reqd_work_group_size_uint64_t"] = *ReqdWorkGroupSize; |
| 126 | + |
| 127 | + if (JointMatrix.has_value()) |
| 128 | + Requirements["joint_matrix"] = *JointMatrix; |
| 129 | + |
| 130 | + if (JointMatrixMad.has_value()) |
| 131 | + Requirements["joint_matrix_mad"] = *JointMatrixMad; |
| 132 | + |
| 133 | + if (SubGroupSize.has_value()) |
| 134 | + Requirements["reqd_sub_group_size"] = *SubGroupSize; |
| 135 | + |
| 136 | + return Requirements; |
| 137 | +} |
0 commit comments