[llvm] [SPIR-V] Implement SPV_KHR_float_controls2 (PR #146941)
Marcos Maronas via llvm-commits
llvm-commits at lists.llvm.org
Wed Aug 6 07:32:13 PDT 2025
================
@@ -551,12 +568,53 @@ void SPIRVAsmPrinter::outputExecutionMode(const Module &M) {
}
if (ST->isKernel() && !M.getNamedMetadata("spirv.ExecutionMode") &&
!M.getNamedMetadata("opencl.enable.FP_CONTRACT")) {
- MCInst Inst;
- Inst.setOpcode(SPIRV::OpExecutionMode);
- Inst.addOperand(MCOperand::createReg(FReg));
- unsigned EM = static_cast<unsigned>(SPIRV::ExecutionMode::ContractionOff);
- Inst.addOperand(MCOperand::createImm(EM));
- outputMCInst(Inst);
+ if (ST->canUseExtension(SPIRV::Extension::SPV_KHR_float_controls2)) {
+ // When SPV_KHR_float_controls2 is enabled, ContractionOff is
+ // deprecated. We need to use FPFastMathDefault with the appropriate
+ // flags instead. Since FPFastMathDefault takes a target type, we need
+ // to emit it for each floating-point type that exists in the module
+ // to match the effect of ContractionOff. As of now, there are 4 FP
+ // types: fp16, fp32 and fp64.
+ for (const MachineInstr *MI :
+ MAI->getMSInstrs(SPIRV::MB_TypeConstVars)) {
+ // Skip if the instruction is not OpTypeFloat.
+ if (MI->getOpcode() != SPIRV::OpTypeFloat)
----------------
maarquitos14 wrote:
Actually, this is the wording in the spec:
```
FPFastMathDefault
Set the default fast math flags for instructions not themselves decorated with FPFastMathMode. This only affects instructions operating on or resulting in a type that is Target Type or an OpTypeMatrix or OpTypeVector derived from it. **Target Type must be a scalar, floating-point type**. Fast-Math Mode must be the <id> of a [constant instruction](https://github.khronos.org/SPIRV-Registry/extensions/KHR/SPV_KHR_float_controls2.html#ConstantInstruction) of 32-bit integer type containing a valid [FP Fast Math Mode](https://github.khronos.org/SPIRV-Registry/extensions/KHR/SPV_KHR_float_controls2.html#FP_Fast_Math_Mode) bitmask. Fast-Math Mode must not be a specialization-constant instruction. May be applied at most once per Target Type to any execution mode.
```
So I think we should not. However, I will double-check if `handleMIFlagDecoration` is working properly for vector types.
https://github.com/llvm/llvm-project/pull/146941
More information about the llvm-commits
mailing list