Skip to content

Commit b691977

Browse files
committed
[Review] set FPFastMathDefault equal to 0 for every entry-point
1 parent 394d9f2 commit b691977

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

lib/SPIRV/SPIRVWriter.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6583,10 +6583,45 @@ bool LLVMToSPIRVBase::transExecutionMode() {
65836583
}
65846584

65856585
transFPContract();
6586+
transFPFastMathDefault();
65866587

65876588
return true;
65886589
}
65896590

6591+
void LLVMToSPIRVBase::transFPFastMathDefault() {
6592+
if (!BM->hasCapability(CapabilityFloatControls2))
6593+
return;
6594+
6595+
SmallVector<SPIRVType *> AllFPTypes;
6596+
for (auto [_, SPVTy] : TypeMap) {
6597+
if (!SPVTy->isTypeFloat())
6598+
continue;
6599+
AllFPTypes.push_back(SPVTy);
6600+
}
6601+
6602+
// We encode an fp-operaiton with no FPFastMathMode flags set as an
6603+
// fp-operation with all the flags set to 0. Instead of setting the flag for
6604+
// every individual operation, we set it once, for the entry-point.
6605+
SPIRVConstant *AllFlagsZero = BM->getLiteralAsConstant(0);
6606+
for (Function &F : *M) {
6607+
SPIRVValue *TranslatedF = getTranslatedValue(&F);
6608+
if (!TranslatedF)
6609+
continue;
6610+
6611+
SPIRVFunction *BF = static_cast<SPIRVFunction *>(TranslatedF);
6612+
bool IsKernelEntryPoint =
6613+
BM->isEntryPoint(spv::ExecutionModelKernel, BF->getId());
6614+
if (!IsKernelEntryPoint)
6615+
continue;
6616+
6617+
for (SPIRVType *FPTy : AllFPTypes) {
6618+
BF->addExecutionMode(
6619+
new SPIRVExecutionModeId(BF, spv::ExecutionModeFPFastMathDefault,
6620+
FPTy->getId(), AllFlagsZero->getId()));
6621+
}
6622+
}
6623+
}
6624+
65906625
void LLVMToSPIRVBase::transFPContract() {
65916626
// SPV_KHR_float_controls2 deprecates ContractionOff.
65926627
if (BM->hasCapability(CapabilityFloatControls2))

lib/SPIRV/SPIRVWriter.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ class LLVMToSPIRVBase : protected BuiltinCallHelper {
148148
bool translate();
149149
bool transExecutionMode();
150150
void transFPContract();
151+
void transFPFastMathDefault();
151152
SPIRVValue *transConstant(Value *V);
152153
/// Translate a reference to a constant in a constant expression. This may
153154
/// involve inserting extra bitcasts to correct type issues.

lib/SPIRV/libSPIRV/SPIRVEntry.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,15 @@ class SPIRVExecutionMode : public SPIRVAnnotation {
668668
WordLiterals.push_back(Z);
669669
updateModuleVersion();
670670
}
671+
// Complete constructor for FPFastMathDefault
672+
SPIRVExecutionMode(Op OC, SPIRVEntry *TheTarget,
673+
SPIRVExecutionModeKind TheExecMode, SPIRVWord X,
674+
SPIRVWord Y)
675+
: SPIRVAnnotation(OC, TheTarget, 5), ExecMode(TheExecMode) {
676+
WordLiterals.push_back(X);
677+
WordLiterals.push_back(Y);
678+
updateModuleVersion();
679+
}
671680
// Complete constructor for VecTypeHint, SubgroupSize, SubgroupsPerWorkgroup
672681
SPIRVExecutionMode(Op OC, SPIRVEntry *TheTarget,
673682
SPIRVExecutionModeKind TheExecMode, SPIRVWord Code)
@@ -728,6 +737,13 @@ class SPIRVExecutionModeId : public SPIRVExecutionMode {
728737
: SPIRVExecutionMode(OpExecutionModeId, TheTarget, TheExecMode, X, Y, Z) {
729738
updateModuleVersion();
730739
}
740+
// Complete constructor for FPFastMathDefault
741+
SPIRVExecutionModeId(SPIRVEntry *TheTarget,
742+
SPIRVExecutionModeKind TheExecMode, SPIRVWord X,
743+
SPIRVWord Y)
744+
: SPIRVExecutionMode(OpExecutionModeId, TheTarget, TheExecMode, X, Y) {
745+
updateModuleVersion();
746+
}
731747
// Complete constructor for SubgroupsPerWorkgroupId
732748
SPIRVExecutionModeId(SPIRVEntry *TheTarget,
733749
SPIRVExecutionModeKind TheExecMode, SPIRVWord Code)

0 commit comments

Comments
 (0)