@@ -302,6 +302,31 @@ static bool recursiveType(const StructType *ST, const Type *Ty) {
302
302
return Run (Ty);
303
303
}
304
304
305
+ // Add decoration if needed
306
+ void addFPBuiltinDecoration (SPIRVModule *BM, Instruction *Inst,
307
+ SPIRVInstruction *I) {
308
+ if (!BM->isAllowedToUseExtension (ExtensionID::SPV_INTEL_fp_max_error))
309
+ return ;
310
+ auto *II = dyn_cast_or_null<IntrinsicInst>(Inst);
311
+ if (II && II->getCalledFunction ()->getName ().starts_with (" llvm.fpbuiltin" )) {
312
+ // Add a new decoration for llvm.builtin intrinsics, if needed
313
+ if (II->getAttributes ().hasFnAttr (" fpbuiltin-max-error" )) {
314
+ double F = 0.0 ;
315
+ II->getAttributes ()
316
+ .getFnAttr (" fpbuiltin-max-error" )
317
+ .getValueAsString ()
318
+ .getAsDouble (F);
319
+ I->addDecorate (DecorationFPMaxErrorDecorationINTEL,
320
+ convertFloatToSPIRVWord (F));
321
+ }
322
+ } else if (auto *MD = Inst->getMetadata (" fpmath" )) {
323
+ auto *MDVal = mdconst::dyn_extract<ConstantFP>(MD->getOperand (0 ));
324
+ double ValAsDouble = MDVal->getValue ().convertToFloat ();
325
+ I->addDecorate (DecorationFPMaxErrorDecorationINTEL,
326
+ convertFloatToSPIRVWord (ValAsDouble));
327
+ }
328
+ }
329
+
305
330
SPIRVType *LLVMToSPIRVBase::transType (Type *T) {
306
331
LLVMToSPIRVTypeMap::iterator Loc = TypeMap.find (T);
307
332
if (Loc != TypeMap.end ())
@@ -2830,6 +2855,8 @@ bool LLVMToSPIRVBase::transDecoration(Value *V, SPIRVValue *BV) {
2830
2855
transMemAliasingINTELDecorations (Inst, BV);
2831
2856
if (auto *IDecoMD = Inst->getMetadata (SPIRV_MD_DECORATIONS))
2832
2857
transMetadataDecorations (IDecoMD, BV);
2858
+ if (BV->isInst ())
2859
+ addFPBuiltinDecoration (BM, Inst, static_cast <SPIRVInstruction *>(BV));
2833
2860
}
2834
2861
2835
2862
if (auto *CI = dyn_cast<CallInst>(V)) {
@@ -3626,26 +3653,6 @@ bool LLVMToSPIRVBase::isKnownIntrinsic(Intrinsic::ID Id) {
3626
3653
}
3627
3654
}
3628
3655
3629
- // Add decoration if needed
3630
- SPIRVInstruction *addFPBuiltinDecoration (SPIRVModule *BM, IntrinsicInst *II,
3631
- SPIRVInstruction *I) {
3632
- const bool AllowFPMaxError =
3633
- BM->isAllowedToUseExtension (ExtensionID::SPV_INTEL_fp_max_error);
3634
- assert (II->getCalledFunction ()->getName ().starts_with (" llvm.fpbuiltin" ));
3635
- // Add a new decoration for llvm.builtin intrinsics, if needed
3636
- if (AllowFPMaxError)
3637
- if (II->getAttributes ().hasFnAttr (" fpbuiltin-max-error" )) {
3638
- double F = 0.0 ;
3639
- II->getAttributes ()
3640
- .getFnAttr (" fpbuiltin-max-error" )
3641
- .getValueAsString ()
3642
- .getAsDouble (F);
3643
- I->addDecorate (DecorationFPMaxErrorDecorationINTEL,
3644
- convertFloatToSPIRVWord (F));
3645
- }
3646
- return I;
3647
- }
3648
-
3649
3656
// Performs mapping of LLVM IR rounding mode to SPIR-V rounding mode
3650
3657
// Value *V is metadata <rounding mode> argument of
3651
3658
// llvm.experimental.constrained.* intrinsics
@@ -4758,10 +4765,9 @@ SPIRVValue *LLVMToSPIRVBase::transFPBuiltinIntrinsicInst(IntrinsicInst *II,
4758
4765
.Case (" fdiv" , OpFDiv)
4759
4766
.Case (" frem" , OpFRem)
4760
4767
.Default (OpUndef);
4761
- auto *BI = BM->addBinaryInst (BinOp, transType (II->getType ()),
4762
- transValue (II->getArgOperand (0 ), BB),
4763
- transValue (II->getArgOperand (1 ), BB), BB);
4764
- return addFPBuiltinDecoration (BM, II, BI);
4768
+ return BM->addBinaryInst (BinOp, transType (II->getType ()),
4769
+ transValue (II->getArgOperand (0 ), BB),
4770
+ transValue (II->getArgOperand (1 ), BB), BB);
4765
4771
}
4766
4772
case FPBuiltinType::EXT_1OPS: {
4767
4773
if (!checkTypeForSPIRVExtendedInstLowering (II, BM))
@@ -4795,9 +4801,8 @@ SPIRVValue *LLVMToSPIRVBase::transFPBuiltinIntrinsicInst(IntrinsicInst *II,
4795
4801
.Case (" erfc" , OpenCLLIB::Erfc)
4796
4802
.Default (SPIRVWORD_MAX);
4797
4803
assert (ExtOp != SPIRVWORD_MAX);
4798
- auto *BI = BM->addExtInst (STy, BM->getExtInstSetId (SPIRVEIS_OpenCL), ExtOp,
4799
- Ops, BB);
4800
- return addFPBuiltinDecoration (BM, II, BI);
4804
+ return BM->addExtInst (STy, BM->getExtInstSetId (SPIRVEIS_OpenCL), ExtOp, Ops,
4805
+ BB);
4801
4806
}
4802
4807
case FPBuiltinType::EXT_2OPS: {
4803
4808
if (!checkTypeForSPIRVExtendedInstLowering (II, BM))
@@ -4812,9 +4817,8 @@ SPIRVValue *LLVMToSPIRVBase::transFPBuiltinIntrinsicInst(IntrinsicInst *II,
4812
4817
.Case (" ldexp" , OpenCLLIB::Ldexp)
4813
4818
.Default (SPIRVWORD_MAX);
4814
4819
assert (ExtOp != SPIRVWORD_MAX);
4815
- auto *BI = BM->addExtInst (STy, BM->getExtInstSetId (SPIRVEIS_OpenCL), ExtOp,
4816
- Ops, BB);
4817
- return addFPBuiltinDecoration (BM, II, BI);
4820
+ return BM->addExtInst (STy, BM->getExtInstSetId (SPIRVEIS_OpenCL), ExtOp, Ops,
4821
+ BB);
4818
4822
}
4819
4823
case FPBuiltinType::EXT_3OPS: {
4820
4824
if (!checkTypeForSPIRVExtendedInstLowering (II, BM))
@@ -4827,9 +4831,8 @@ SPIRVValue *LLVMToSPIRVBase::transFPBuiltinIntrinsicInst(IntrinsicInst *II,
4827
4831
.Case (" sincos" , OpenCLLIB::Sincos)
4828
4832
.Default (SPIRVWORD_MAX);
4829
4833
assert (ExtOp != SPIRVWORD_MAX);
4830
- auto *BI = BM->addExtInst (STy, BM->getExtInstSetId (SPIRVEIS_OpenCL), ExtOp,
4831
- Ops, BB);
4832
- return addFPBuiltinDecoration (BM, II, BI);
4834
+ return BM->addExtInst (STy, BM->getExtInstSetId (SPIRVEIS_OpenCL), ExtOp, Ops,
4835
+ BB);
4833
4836
}
4834
4837
default :
4835
4838
return nullptr ;
0 commit comments