@@ -306,6 +306,31 @@ static bool recursiveType(const StructType *ST, const Type *Ty) {
306
306
return Run (Ty);
307
307
}
308
308
309
+ // Add decoration if needed
310
+ void addFPBuiltinDecoration (SPIRVModule *BM, Instruction *Inst,
311
+ SPIRVInstruction *I) {
312
+ if (!BM->isAllowedToUseExtension (ExtensionID::SPV_INTEL_fp_max_error))
313
+ return ;
314
+ auto *II = dyn_cast_or_null<IntrinsicInst>(Inst);
315
+ if (II && II->getCalledFunction ()->getName ().starts_with (" llvm.fpbuiltin" )) {
316
+ // Add a new decoration for llvm.builtin intrinsics, if needed
317
+ if (II->getAttributes ().hasFnAttr (" fpbuiltin-max-error" )) {
318
+ double F = 0.0 ;
319
+ II->getAttributes ()
320
+ .getFnAttr (" fpbuiltin-max-error" )
321
+ .getValueAsString ()
322
+ .getAsDouble (F);
323
+ I->addDecorate (DecorationFPMaxErrorDecorationINTEL,
324
+ convertFloatToSPIRVWord (F));
325
+ }
326
+ } else if (auto *MD = Inst->getMetadata (" fpmath" )) {
327
+ auto *MDVal = mdconst::dyn_extract<ConstantFP>(MD->getOperand (0 ));
328
+ double ValAsDouble = MDVal->getValue ().convertToFloat ();
329
+ I->addDecorate (DecorationFPMaxErrorDecorationINTEL,
330
+ convertFloatToSPIRVWord (ValAsDouble));
331
+ }
332
+ }
333
+
309
334
SPIRVType *LLVMToSPIRVBase::transType (Type *T) {
310
335
LLVMToSPIRVTypeMap::iterator Loc = TypeMap.find (T);
311
336
if (Loc != TypeMap.end ())
@@ -2894,6 +2919,8 @@ bool LLVMToSPIRVBase::transDecoration(Value *V, SPIRVValue *BV) {
2894
2919
transMemAliasingINTELDecorations (Inst, BV);
2895
2920
if (auto *IDecoMD = Inst->getMetadata (SPIRV_MD_DECORATIONS))
2896
2921
transMetadataDecorations (IDecoMD, BV);
2922
+ if (BV->isInst ())
2923
+ addFPBuiltinDecoration (BM, Inst, static_cast <SPIRVInstruction *>(BV));
2897
2924
}
2898
2925
2899
2926
if (auto *CI = dyn_cast<CallInst>(V)) {
@@ -3690,26 +3717,6 @@ bool LLVMToSPIRVBase::isKnownIntrinsic(Intrinsic::ID Id) {
3690
3717
}
3691
3718
}
3692
3719
3693
- // Add decoration if needed
3694
- SPIRVInstruction *addFPBuiltinDecoration (SPIRVModule *BM, IntrinsicInst *II,
3695
- SPIRVInstruction *I) {
3696
- const bool AllowFPMaxError =
3697
- BM->isAllowedToUseExtension (ExtensionID::SPV_INTEL_fp_max_error);
3698
- assert (II->getCalledFunction ()->getName ().starts_with (" llvm.fpbuiltin" ));
3699
- // Add a new decoration for llvm.builtin intrinsics, if needed
3700
- if (AllowFPMaxError)
3701
- if (II->getAttributes ().hasFnAttr (" fpbuiltin-max-error" )) {
3702
- double F = 0.0 ;
3703
- II->getAttributes ()
3704
- .getFnAttr (" fpbuiltin-max-error" )
3705
- .getValueAsString ()
3706
- .getAsDouble (F);
3707
- I->addDecorate (DecorationFPMaxErrorDecorationINTEL,
3708
- convertFloatToSPIRVWord (F));
3709
- }
3710
- return I;
3711
- }
3712
-
3713
3720
// Performs mapping of LLVM IR rounding mode to SPIR-V rounding mode
3714
3721
// Value *V is metadata <rounding mode> argument of
3715
3722
// llvm.experimental.constrained.* intrinsics
@@ -4822,10 +4829,9 @@ SPIRVValue *LLVMToSPIRVBase::transFPBuiltinIntrinsicInst(IntrinsicInst *II,
4822
4829
.Case (" fdiv" , OpFDiv)
4823
4830
.Case (" frem" , OpFRem)
4824
4831
.Default (OpUndef);
4825
- auto *BI = BM->addBinaryInst (BinOp, transType (II->getType ()),
4826
- transValue (II->getArgOperand (0 ), BB),
4827
- transValue (II->getArgOperand (1 ), BB), BB);
4828
- return addFPBuiltinDecoration (BM, II, BI);
4832
+ return BM->addBinaryInst (BinOp, transType (II->getType ()),
4833
+ transValue (II->getArgOperand (0 ), BB),
4834
+ transValue (II->getArgOperand (1 ), BB), BB);
4829
4835
}
4830
4836
case FPBuiltinType::EXT_1OPS: {
4831
4837
if (!checkTypeForSPIRVExtendedInstLowering (II, BM))
@@ -4859,9 +4865,8 @@ SPIRVValue *LLVMToSPIRVBase::transFPBuiltinIntrinsicInst(IntrinsicInst *II,
4859
4865
.Case (" erfc" , OpenCLLIB::Erfc)
4860
4866
.Default (SPIRVWORD_MAX);
4861
4867
assert (ExtOp != SPIRVWORD_MAX);
4862
- auto *BI = BM->addExtInst (STy, BM->getExtInstSetId (SPIRVEIS_OpenCL), ExtOp,
4863
- Ops, BB);
4864
- return addFPBuiltinDecoration (BM, II, BI);
4868
+ return BM->addExtInst (STy, BM->getExtInstSetId (SPIRVEIS_OpenCL), ExtOp, Ops,
4869
+ BB);
4865
4870
}
4866
4871
case FPBuiltinType::EXT_2OPS: {
4867
4872
if (!checkTypeForSPIRVExtendedInstLowering (II, BM))
@@ -4876,9 +4881,8 @@ SPIRVValue *LLVMToSPIRVBase::transFPBuiltinIntrinsicInst(IntrinsicInst *II,
4876
4881
.Case (" ldexp" , OpenCLLIB::Ldexp)
4877
4882
.Default (SPIRVWORD_MAX);
4878
4883
assert (ExtOp != SPIRVWORD_MAX);
4879
- auto *BI = BM->addExtInst (STy, BM->getExtInstSetId (SPIRVEIS_OpenCL), ExtOp,
4880
- Ops, BB);
4881
- return addFPBuiltinDecoration (BM, II, BI);
4884
+ return BM->addExtInst (STy, BM->getExtInstSetId (SPIRVEIS_OpenCL), ExtOp, Ops,
4885
+ BB);
4882
4886
}
4883
4887
case FPBuiltinType::EXT_3OPS: {
4884
4888
if (!checkTypeForSPIRVExtendedInstLowering (II, BM))
@@ -4891,9 +4895,8 @@ SPIRVValue *LLVMToSPIRVBase::transFPBuiltinIntrinsicInst(IntrinsicInst *II,
4891
4895
.Case (" sincos" , OpenCLLIB::Sincos)
4892
4896
.Default (SPIRVWORD_MAX);
4893
4897
assert (ExtOp != SPIRVWORD_MAX);
4894
- auto *BI = BM->addExtInst (STy, BM->getExtInstSetId (SPIRVEIS_OpenCL), ExtOp,
4895
- Ops, BB);
4896
- return addFPBuiltinDecoration (BM, II, BI);
4898
+ return BM->addExtInst (STy, BM->getExtInstSetId (SPIRVEIS_OpenCL), ExtOp, Ops,
4899
+ BB);
4897
4900
}
4898
4901
default :
4899
4902
return nullptr ;
0 commit comments