@@ -160,7 +160,7 @@ static void addNamedMetadataStringSet(LLVMContext *Context, Module *M,
160
160
NamedMD->addOperand (MDNode::get (*Context, ValueVec));
161
161
}
162
162
163
- static void addOCLKernelArgumentMetadata (
163
+ static void addKernelArgumentMetadata (
164
164
LLVMContext *Context, const std::string &MDName, SPIRVFunction *BF,
165
165
llvm::Function *Fn,
166
166
std::function<Metadata *(SPIRVFunctionParameter *)> ForeachFnArg) {
@@ -3553,13 +3553,83 @@ void SPIRVToLLVM::transGlobalAnnotations() {
3553
3553
}
3554
3554
}
3555
3555
3556
+ static llvm::MDNode *
3557
+ transDecorationsToMetadataList (llvm::LLVMContext *Context,
3558
+ std::vector<SPIRVDecorate const *> Decorates) {
3559
+ SmallVector<Metadata *, 4 > MDs;
3560
+ MDs.reserve (Decorates.size ());
3561
+ for (const auto *Deco : Decorates) {
3562
+ std::vector<Metadata *> OPs;
3563
+ auto *KindMD = ConstantAsMetadata::get (
3564
+ ConstantInt::get (Type::getInt32Ty (*Context), Deco->getDecorateKind ()));
3565
+ OPs.push_back (KindMD);
3566
+ switch (static_cast <size_t >(Deco->getDecorateKind ())) {
3567
+ case DecorationLinkageAttributes: {
3568
+ const auto *const LinkAttrDeco =
3569
+ static_cast <const SPIRVDecorateLinkageAttr *>(Deco);
3570
+ auto *const LinkNameMD =
3571
+ MDString::get (*Context, LinkAttrDeco->getLinkageName ());
3572
+ auto *const LinkTypeMD = ConstantAsMetadata::get (ConstantInt::get (
3573
+ Type::getInt32Ty (*Context), LinkAttrDeco->getLinkageType ()));
3574
+ OPs.push_back (LinkNameMD);
3575
+ OPs.push_back (LinkTypeMD);
3576
+ break ;
3577
+ }
3578
+ case DecorationMergeINTEL: {
3579
+ const auto MergeAttrLits = Deco->getVecLiteral ();
3580
+ std::string FirstString = getString (MergeAttrLits);
3581
+ std::string SecondString =
3582
+ getString (MergeAttrLits.cbegin () + getVec (FirstString).size (),
3583
+ MergeAttrLits.cend ());
3584
+ OPs.push_back (MDString::get (*Context, FirstString));
3585
+ OPs.push_back (MDString::get (*Context, SecondString));
3586
+ break ;
3587
+ }
3588
+ case DecorationMemoryINTEL:
3589
+ case DecorationUserSemantic: {
3590
+ auto *const StrMD =
3591
+ MDString::get (*Context, getString (Deco->getVecLiteral ()));
3592
+ OPs.push_back (StrMD);
3593
+ break ;
3594
+ }
3595
+ default : {
3596
+ for (const SPIRVWord Lit : Deco->getVecLiteral ()) {
3597
+ auto *const LitMD = ConstantAsMetadata::get (
3598
+ ConstantInt::get (Type::getInt32Ty (*Context), Lit));
3599
+ OPs.push_back (LitMD);
3600
+ }
3601
+ break ;
3602
+ }
3603
+ }
3604
+ MDs.push_back (MDNode::get (*Context, OPs));
3605
+ }
3606
+ return MDNode::get (*Context, MDs);
3607
+ }
3608
+
3609
+ void SPIRVToLLVM::transVarDecorationsToMetadata (SPIRVValue *BV, Value *V) {
3610
+ if (!BV->isVariable ())
3611
+ return ;
3612
+
3613
+ if (auto *GV = dyn_cast<GlobalVariable>(V)) {
3614
+ std::vector<SPIRVDecorate const *> Decorates = BV->getDecorations ();
3615
+ if (!Decorates.empty ()) {
3616
+ MDNode *MDList = transDecorationsToMetadataList (Context, Decorates);
3617
+ GV->setMetadata (SPIRV_MD_DECORATIONS, MDList);
3618
+ }
3619
+ }
3620
+ }
3621
+
3556
3622
bool SPIRVToLLVM::transDecoration (SPIRVValue *BV, Value *V) {
3557
3623
if (!transAlign (BV, V))
3558
3624
return false ;
3559
3625
3560
3626
transIntelFPGADecorations (BV, V);
3561
3627
transMemAliasingINTELDecorations (BV, V);
3562
3628
3629
+ // Decoration metadata is only enabled in SPIR-V friendly mode
3630
+ if (BM->getDesiredBIsRepresentation () == BIsRepresentation::SPIRVFriendlyIR)
3631
+ transVarDecorationsToMetadata (BV, V);
3632
+
3563
3633
DbgTran->transDbgInfo (BV, V);
3564
3634
return true ;
3565
3635
}
@@ -3691,6 +3761,23 @@ static bool transKernelArgTypeMedataFromString(LLVMContext *Ctx,
3691
3761
return true ;
3692
3762
}
3693
3763
3764
+ void SPIRVToLLVM::transFunctionDecorationsToMetadata (SPIRVFunction *BF,
3765
+ Function *F) {
3766
+ size_t TotalParameterDecorations = 0 ;
3767
+ BF->foreachArgument ([&](SPIRVFunctionParameter *Arg) {
3768
+ TotalParameterDecorations += Arg->getNumDecorations ();
3769
+ });
3770
+ if (TotalParameterDecorations == 0 )
3771
+ return ;
3772
+
3773
+ // Generate metadata for spirv.ParameterDecorations
3774
+ addKernelArgumentMetadata (Context, SPIRV_MD_PARAMETER_DECORATIONS, BF, F,
3775
+ [=](SPIRVFunctionParameter *Arg) {
3776
+ return transDecorationsToMetadataList (
3777
+ Context, Arg->getDecorations ());
3778
+ });
3779
+ }
3780
+
3694
3781
bool SPIRVToLLVM::transMetadata () {
3695
3782
SmallVector<Function *, 2 > CtorKernels;
3696
3783
for (unsigned I = 0 , E = BM->getNumFunctions (); I != E; ++I) {
@@ -3702,6 +3789,10 @@ bool SPIRVToLLVM::transMetadata() {
3702
3789
transVectorComputeMetadata (BF);
3703
3790
transFPGAFunctionMetadata (BF, F);
3704
3791
3792
+ // Decoration metadata is only enabled in SPIR-V friendly mode
3793
+ if (BM->getDesiredBIsRepresentation () == BIsRepresentation::SPIRVFriendlyIR)
3794
+ transFunctionDecorationsToMetadata (BF, F);
3795
+
3705
3796
if (BF->hasDecorate (internal::DecorationCallableFunctionINTEL))
3706
3797
F->addFnAttr (kVCMetadata ::VCCallable);
3707
3798
if (isKernel (BF) &&
@@ -3806,7 +3897,7 @@ bool SPIRVToLLVM::transOCLMetadata(SPIRVFunction *BF) {
3806
3897
return true ;
3807
3898
3808
3899
// Generate metadata for kernel_arg_addr_space
3809
- addOCLKernelArgumentMetadata (
3900
+ addKernelArgumentMetadata (
3810
3901
Context, SPIR_MD_KERNEL_ARG_ADDR_SPACE, BF, F,
3811
3902
[=](SPIRVFunctionParameter *Arg) {
3812
3903
SPIRVType *ArgTy = Arg->getType ();
@@ -3819,31 +3910,31 @@ bool SPIRVToLLVM::transOCLMetadata(SPIRVFunction *BF) {
3819
3910
ConstantInt::get (Type::getInt32Ty (*Context), AS));
3820
3911
});
3821
3912
// Generate metadata for kernel_arg_access_qual
3822
- addOCLKernelArgumentMetadata (Context, SPIR_MD_KERNEL_ARG_ACCESS_QUAL, BF, F,
3823
- [=](SPIRVFunctionParameter *Arg) {
3824
- std::string Qual;
3825
- auto T = Arg->getType ();
3826
- if (T->isTypeOCLImage ()) {
3827
- auto ST = static_cast <SPIRVTypeImage *>(T);
3828
- Qual = transOCLImageTypeAccessQualifier (ST);
3829
- } else if (T->isTypePipe ()) {
3830
- auto PT = static_cast <SPIRVTypePipe *>(T);
3831
- Qual = transOCLPipeTypeAccessQualifier (PT);
3832
- } else
3833
- Qual = " none" ;
3834
- return MDString::get (*Context, Qual);
3835
- });
3913
+ addKernelArgumentMetadata (Context, SPIR_MD_KERNEL_ARG_ACCESS_QUAL, BF, F,
3914
+ [=](SPIRVFunctionParameter *Arg) {
3915
+ std::string Qual;
3916
+ auto * T = Arg->getType ();
3917
+ if (T->isTypeOCLImage ()) {
3918
+ auto * ST = static_cast <SPIRVTypeImage *>(T);
3919
+ Qual = transOCLImageTypeAccessQualifier (ST);
3920
+ } else if (T->isTypePipe ()) {
3921
+ auto * PT = static_cast <SPIRVTypePipe *>(T);
3922
+ Qual = transOCLPipeTypeAccessQualifier (PT);
3923
+ } else
3924
+ Qual = " none" ;
3925
+ return MDString::get (*Context, Qual);
3926
+ });
3836
3927
// Generate metadata for kernel_arg_type
3837
3928
if (!transKernelArgTypeMedataFromString (Context, BM, F,
3838
3929
SPIR_MD_KERNEL_ARG_TYPE))
3839
- addOCLKernelArgumentMetadata (Context, SPIR_MD_KERNEL_ARG_TYPE, BF, F,
3840
- [=](SPIRVFunctionParameter *Arg) {
3841
- return transOCLKernelArgTypeName (Arg);
3842
- });
3930
+ addKernelArgumentMetadata (Context, SPIR_MD_KERNEL_ARG_TYPE, BF, F,
3931
+ [=](SPIRVFunctionParameter *Arg) {
3932
+ return transOCLKernelArgTypeName (Arg);
3933
+ });
3843
3934
// Generate metadata for kernel_arg_type_qual
3844
3935
if (!transKernelArgTypeMedataFromString (Context, BM, F,
3845
3936
SPIR_MD_KERNEL_ARG_TYPE_QUAL))
3846
- addOCLKernelArgumentMetadata (
3937
+ addKernelArgumentMetadata (
3847
3938
Context, SPIR_MD_KERNEL_ARG_TYPE_QUAL, BF, F,
3848
3939
[=](SPIRVFunctionParameter *Arg) {
3849
3940
std::string Qual;
@@ -3861,17 +3952,16 @@ bool SPIRVToLLVM::transOCLMetadata(SPIRVFunction *BF) {
3861
3952
return MDString::get (*Context, Qual);
3862
3953
});
3863
3954
// Generate metadata for kernel_arg_base_type
3864
- addOCLKernelArgumentMetadata (Context, SPIR_MD_KERNEL_ARG_BASE_TYPE, BF, F,
3865
- [=](SPIRVFunctionParameter *Arg) {
3866
- return transOCLKernelArgTypeName (Arg);
3867
- });
3955
+ addKernelArgumentMetadata (Context, SPIR_MD_KERNEL_ARG_BASE_TYPE, BF, F,
3956
+ [=](SPIRVFunctionParameter *Arg) {
3957
+ return transOCLKernelArgTypeName (Arg);
3958
+ });
3868
3959
// Generate metadata for kernel_arg_name
3869
3960
if (BM->isGenArgNameMDEnabled ()) {
3870
- addOCLKernelArgumentMetadata (Context, SPIR_MD_KERNEL_ARG_NAME, BF, F,
3871
- [=](SPIRVFunctionParameter *Arg) {
3872
- return MDString::get (*Context,
3873
- Arg->getName ());
3874
- });
3961
+ addKernelArgumentMetadata (Context, SPIR_MD_KERNEL_ARG_NAME, BF, F,
3962
+ [=](SPIRVFunctionParameter *Arg) {
3963
+ return MDString::get (*Context, Arg->getName ());
3964
+ });
3875
3965
}
3876
3966
// Generate metadata for kernel_arg_buffer_location
3877
3967
addBufferLocationMetadata (Context, BF, F, [=](SPIRVFunctionParameter *Arg) {
0 commit comments