Skip to content

Commit 67d5ba9

Browse files
[SPIR-V] Add support for SPV_KHR_float_controls (llvm#83418)
This PR is to add explicit support for SPV_KHR_float_controls (https://github.com/KhronosGroup/SPIRV-Registry/blob/main/extensions/KHR/SPV_KHR_float_controls.asciidoc). This extension is included into SPIR-V after version 1.4, but in case of lower versions it is to be included explicitly and OpExtension must be present in the module with `OpExtension "SPV_KHR_float_controls"`. This PR fixes this issue and fixes the test case test/CodeGen/SPIRV/exec_mode_float_control_khr.ll to account for a version lower than 1.4.
1 parent ecc3bda commit 67d5ba9

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

llvm/lib/Target/SPIRV/SPIRVModuleAnalysis.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,6 +1143,8 @@ static void collectReqs(const Module &M, SPIRV::ModuleAnalysisInfo &MAI,
11431143
// Collect requirements for OpExecutionMode instructions.
11441144
auto Node = M.getNamedMetadata("spirv.ExecutionMode");
11451145
if (Node) {
1146+
// SPV_KHR_float_controls is not available until v1.4
1147+
bool RequireFloatControls = false, VerLower14 = !ST.isAtLeastSPIRVVer(14);
11461148
for (unsigned i = 0; i < Node->getNumOperands(); i++) {
11471149
MDNode *MDN = cast<MDNode>(Node->getOperand(i));
11481150
const MDOperand &MDOp = MDN->getOperand(1);
@@ -1152,9 +1154,22 @@ static void collectReqs(const Module &M, SPIRV::ModuleAnalysisInfo &MAI,
11521154
auto EM = Const->getZExtValue();
11531155
MAI.Reqs.getAndAddRequirements(
11541156
SPIRV::OperandCategory::ExecutionModeOperand, EM, ST);
1157+
// add SPV_KHR_float_controls if the version is too low
1158+
switch (EM) {
1159+
case SPIRV::ExecutionMode::DenormPreserve:
1160+
case SPIRV::ExecutionMode::DenormFlushToZero:
1161+
case SPIRV::ExecutionMode::SignedZeroInfNanPreserve:
1162+
case SPIRV::ExecutionMode::RoundingModeRTE:
1163+
case SPIRV::ExecutionMode::RoundingModeRTZ:
1164+
RequireFloatControls = VerLower14;
1165+
break;
1166+
}
11551167
}
11561168
}
11571169
}
1170+
if (RequireFloatControls &&
1171+
ST.canUseExtension(SPIRV::Extension::SPV_KHR_float_controls))
1172+
MAI.Reqs.addExtension(SPIRV::Extension::SPV_KHR_float_controls);
11581173
}
11591174
for (auto FI = M.begin(), E = M.end(); FI != E; ++FI) {
11601175
const Function &F = *FI;

llvm/lib/Target/SPIRV/SPIRVSubtarget.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ cl::list<SPIRV::Extension::Extension> Extensions(
6868
"SPV_KHR_no_integer_wrap_decoration",
6969
"Adds decorations to indicate that a given instruction does "
7070
"not cause integer wrapping."),
71+
clEnumValN(
72+
SPIRV::Extension::SPV_KHR_float_controls, "SPV_KHR_float_controls",
73+
"Provides new execution modes to control floating-point "
74+
"computations by overriding an implementation’s default behavior "
75+
"for rounding modes, denormals, signed zero, and infinities."),
7176
clEnumValN(SPIRV::Extension::SPV_KHR_expect_assume,
7277
"SPV_KHR_expect_assume",
7378
"Provides additional information to a compiler, similar to "

llvm/test/CodeGen/SPIRV/exec_mode_float_control_khr.ll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
; RUN: llc -O0 -mtriple=spirv32-unknown-unknown %s -o - | FileCheck %s --check-prefixes=SPV
2+
; RUN: llc -O0 -mtriple=spirv32-unknown-unknown %s --mattr=+spirv1.3 --spirv-extensions=SPV_KHR_float_controls -o - | FileCheck %s --check-prefixes=SPVEXT
23

34
define dso_local dllexport spir_kernel void @k_float_controls_0(i32 %ibuf, i32 %obuf) local_unnamed_addr {
45
entry:
@@ -29,6 +30,7 @@ entry:
2930
!spirv.ExecutionMode = !{!15, !16, !17, !18, !19, !20, !21, !22, !23, !24, !25, !26, !27, !28, !29}
3031

3132
; SPV-NOT: OpExtension "SPV_KHR_float_controls"
33+
; SPVEXT: OpExtension "SPV_KHR_float_controls"
3234

3335
; SPV-DAG: OpEntryPoint {{.*}} %[[#KERNEL0:]] "k_float_controls_0"
3436
; SPV-DAG: OpEntryPoint {{.*}} %[[#KERNEL1:]] "k_float_controls_1"

0 commit comments

Comments
 (0)