@@ -42,6 +42,7 @@ constexpr uint32_t SPIRV_PIPELINE_ENABLE_DECOR = 5919;
4242enum class DecorValueTy {
4343 uint32,
4444 boolean,
45+ string,
4546 none,
4647};
4748
@@ -78,6 +79,27 @@ MDNode *buildSpirvDecorMetadata(LLVMContext &Ctx, uint32_t OpCode,
7879 return MDNode::get (Ctx, MD );
7980}
8081
82+ // / Builds a metadata node for a SPIR-V decoration (decoration code
83+ // / is \c uint32_t integer and value is a string).
84+ // /
85+ // / @param Ctx [in] the LLVM Context.
86+ // / @param OpCode [in] the SPIR-V OpCode code.
87+ // / @param Value [in] the SPIR-V decoration value.
88+ // /
89+ // / @returns a pointer to the metadata node created for the required decoration
90+ // / and its value.
91+ MDNode *buildSpirvDecorMetadata (LLVMContext &Ctx, uint32_t OpCode,
92+ StringRef Value) {
93+ auto *Ty = Type::getInt32Ty (Ctx);
94+ SmallVector<Metadata *, 2 > MD ;
95+ MD .push_back (ConstantAsMetadata::get (
96+ Constant::getIntegerValue (Ty, APInt (32 , OpCode))));
97+ MD .push_back (
98+ ConstantAsMetadata::get (ConstantDataArray::getString (Ctx, Value,
99+ /* AddNull=*/ true )));
100+ return MDNode::get (Ctx, MD );
101+ }
102+
81103// / Builds a metadata node for a SPIR-V decoration (both decoration code
82104// / and value are \c uint32_t integers, and the secondary extra operand is a
83105// / string).
@@ -143,6 +165,8 @@ MDNode *attributeToDecorateMetadata(LLVMContext &Ctx, const Attribute &Attr) {
143165 getAttributeAsInteger<uint32_t >(Attr));
144166 case DecorValueTy::boolean:
145167 return buildSpirvDecorMetadata (Ctx, DecorCode, hasProperty (Attr));
168+ case DecorValueTy::string:
169+ return buildSpirvDecorMetadata (Ctx, DecorCode, Attr.getValueAsString ());
146170 default :
147171 llvm_unreachable (" Unhandled decorator type." );
148172 }
@@ -251,29 +275,31 @@ parseSYCLPropertiesString(Module &M, IntrinsicInst *IntrInst) {
251275 SmallVector<std::pair<std::optional<StringRef>, std::optional<StringRef>>, 8 >
252276 result;
253277
254- if (const auto *Cast =
255- dyn_cast<BitCastOperator>(IntrInst->getArgOperand (4 ))) {
256- if (const auto *AnnotValsGV =
257- dyn_cast<GlobalVariable>(Cast->getOperand (0 ))) {
258- if (const auto *AnnotValsAggr =
259- dyn_cast<ConstantAggregate>(AnnotValsGV->getInitializer ())) {
260- assert (
261- (AnnotValsAggr->getNumOperands () & 1 ) == 0 &&
262- " sycl-properties annotation must have an even number of annotation "
263- " values." );
264-
265- // Iterate over the pairs of property meta-names and meta-values.
266- for (size_t I = 0 ; I < AnnotValsAggr->getNumOperands (); I += 2 ) {
267- std::optional<StringRef> PropMetaName =
268- getGlobalVariableString (AnnotValsAggr->getOperand (I));
269- std::optional<StringRef> PropMetaValue =
270- getGlobalVariableString (AnnotValsAggr->getOperand (I + 1 ));
271-
272- assert (PropMetaName &&
273- " Unexpected format for property name in annotation." );
274-
275- result.push_back (std::make_pair (PropMetaName, PropMetaValue));
276- }
278+ auto AnnotValsIntrOpd = IntrInst->getArgOperand (4 );
279+ const GlobalVariable *AnnotValsGV = nullptr ;
280+ if (AnnotValsIntrOpd->getType ()->isOpaquePointerTy ())
281+ AnnotValsGV = dyn_cast<GlobalVariable>(AnnotValsIntrOpd);
282+ else if (const auto *Cast = dyn_cast<BitCastOperator>(AnnotValsIntrOpd))
283+ AnnotValsGV = dyn_cast<GlobalVariable>(Cast->getOperand (0 ));
284+ if (AnnotValsGV) {
285+ if (const auto *AnnotValsAggr =
286+ dyn_cast<ConstantAggregate>(AnnotValsGV->getInitializer ())) {
287+ assert (
288+ (AnnotValsAggr->getNumOperands () & 1 ) == 0 &&
289+ " sycl-properties annotation must have an even number of annotation "
290+ " values." );
291+
292+ // Iterate over the pairs of property meta-names and meta-values.
293+ for (size_t I = 0 ; I < AnnotValsAggr->getNumOperands (); I += 2 ) {
294+ std::optional<StringRef> PropMetaName =
295+ getGlobalVariableString (AnnotValsAggr->getOperand (I));
296+ std::optional<StringRef> PropMetaValue =
297+ getGlobalVariableString (AnnotValsAggr->getOperand (I + 1 ));
298+
299+ assert (PropMetaName &&
300+ " Unexpected format for property name in annotation." );
301+
302+ result.push_back (std::make_pair (PropMetaName, PropMetaValue));
277303 }
278304 }
279305 }
@@ -510,9 +536,10 @@ bool CompileTimePropertiesPass::transformSYCLPropertiesAnnotation(
510536 // Get the global variable with the annotation string.
511537 const GlobalVariable *AnnotStrArgGV = nullptr ;
512538 const Value *IntrAnnotStringArg = IntrInst->getArgOperand (1 );
513- if (auto *GEP = dyn_cast<GEPOperator>(IntrAnnotStringArg))
514- if (auto *C = dyn_cast<Constant>(GEP ->getOperand (0 )))
515- AnnotStrArgGV = dyn_cast<GlobalVariable>(C);
539+ if (IntrAnnotStringArg->getType ()->isOpaquePointerTy ())
540+ AnnotStrArgGV = dyn_cast<GlobalVariable>(IntrAnnotStringArg);
541+ else if (auto *GEP = dyn_cast<GEPOperator>(IntrAnnotStringArg))
542+ AnnotStrArgGV = dyn_cast<GlobalVariable>(GEP ->getOperand (0 ));
516543 if (!AnnotStrArgGV)
517544 return false ;
518545
0 commit comments