@@ -79,6 +79,35 @@ TypeToAspectsMapTy getTypesThatUseAspectsFromMetadata(const Module &M) {
7979 return Result;
8080}
8181
82+ using AspectValueToNameMapTy = SmallMapVector<StringRef, int , 32 >;
83+
84+ // / Retrieves from metadata (sycl_aspects) the mapping between SYCL aspect names
85+ // / and their integral values.
86+ AspectValueToNameMapTy getAspectsFromMetadata (const Module &M) {
87+ const NamedMDNode *Node = M.getNamedMetadata (" sycl_aspects" );
88+ AspectValueToNameMapTy Result;
89+ if (!Node)
90+ return Result;
91+
92+ for (const auto OperandIt : Node->operands ()) {
93+ const MDNode &N = *OperandIt;
94+ assert (N.getNumOperands () == 2 &&
95+ " Each operand of sycl_aspects must be a pair." );
96+
97+ // The aspect's name is the first operand.
98+ const auto *AspectName = cast<MDString>(N.getOperand (0 ));
99+
100+ // The aspect's integral value is the second operand.
101+ const auto *AspectCAM = cast<ConstantAsMetadata>(N.getOperand (1 ));
102+ const Constant *AspectC = AspectCAM->getValue ();
103+
104+ Result[AspectName->getString ()] =
105+ cast<ConstantInt>(AspectC)->getSExtValue ();
106+ }
107+
108+ return Result;
109+ }
110+
82111using TypesEdgesTy =
83112 std::unordered_map<const Type *, std::vector<const Type *>>;
84113
@@ -107,6 +136,7 @@ void propagateAspectsThroughTypes(const TypesEdgesTy &Edges, const Type *Start,
107136// / another type TT, which in turn uses the aspect A.
108137// / @TypesWithAspects argument consist of known types with aspects
109138// / from metadata information.
139+ // / @AspectValues argument consist of known aspect values and their names.
110140// /
111141// / The algorithm is the following:
112142// / 1) Make a list of all structure types from module @M. The list also
@@ -121,18 +151,17 @@ void propagateAspectsThroughTypes(const TypesEdgesTy &Edges, const Type *Start,
121151// / Time complexity: O((V + E) * T) where T is the number of input types
122152// / containing aspects.
123153void propagateAspectsToOtherTypesInModule (
124- const Module &M, TypeToAspectsMapTy &TypesWithAspects) {
154+ const Module &M, TypeToAspectsMapTy &TypesWithAspects,
155+ AspectValueToNameMapTy &AspectValues) {
125156 std::unordered_set<const Type *> TypesToProcess;
126157 const Type *DoubleTy = Type::getDoubleTy (M.getContext ());
127158
128- // 6 is taken from sycl/include/CL/sycl/aspects.hpp
129- // Note: that magic number must strictly correspond to the one assigned to
130- // 'fp64' value of 'aspect' enum.
131- // FIXME: we should develop some kind of mechanism which will allow us to
132- // avoid hardcoding this number here and having a build dependency between
133- // the compiler and the runtime. See intel/llvm#5892
134- static constexpr int AspectFP64 = 6 ;
135- TypesWithAspects[DoubleTy].insert (AspectFP64);
159+ // Find the value of the fp64 aspect from the aspect values map and register
160+ // it as a special-case type with aspect for double.
161+ auto FP64AspectIt = AspectValues.find (" fp64" );
162+ assert (FP64AspectIt != AspectValues.end () &&
163+ " fp64 aspect was not found in the aspect values." );
164+ TypesWithAspects[DoubleTy].insert (FP64AspectIt->second );
136165
137166 TypesToProcess.insert (DoubleTy);
138167 for (const Type *T : M.getIdentifiedStructTypes ())
@@ -339,7 +368,19 @@ buildFunctionsToAspectsMap(Module &M, TypeToAspectsMapTy &TypesWithAspects) {
339368PreservedAnalyses
340369SYCLPropagateAspectsUsagePass::run (Module &M, ModuleAnalysisManager &MAM) {
341370 TypeToAspectsMapTy TypesWithAspects = getTypesThatUseAspectsFromMetadata (M);
342- propagateAspectsToOtherTypesInModule (M, TypesWithAspects);
371+ AspectValueToNameMapTy AspectValues = getAspectsFromMetadata (M);
372+
373+ // If there is no metadata for aspect values the source code must not have
374+ // included the SYCL headers. In that case there should also not be any types
375+ // that use aspects, so we can skip this pass.
376+ if (AspectValues.empty ()) {
377+ assert (TypesWithAspects.empty () &&
378+ " sycl_aspects metadata is missing but "
379+ " sycl_types_that_use_aspects is present." );
380+ return PreservedAnalyses::all ();
381+ }
382+
383+ propagateAspectsToOtherTypesInModule (M, TypesWithAspects, AspectValues);
343384
344385 FunctionToAspectsMapTy FunctionToAspects =
345386 buildFunctionsToAspectsMap (M, TypesWithAspects);
0 commit comments