[llvm] [SPIR-V] Implement SPV_KHR_float_controls2 (PR #146941)
Nathan Gauër via llvm-commits
llvm-commits at lists.llvm.org
Mon Aug 25 08:53:56 PDT 2025
================
@@ -606,6 +664,73 @@ void SPIRVAsmPrinter::outputAnnotations(const Module &M) {
}
}
+void SPIRVAsmPrinter::outputFPFastMathDefaultInfo() {
+ // Collect the SPIRVTypes that are OpTypeFloat.
+ std::vector<const MachineInstr *> SPIRVFloatTypes;
+ for (const MachineInstr *MI : MAI->getMSInstrs(SPIRV::MB_TypeConstVars)) {
+ // Skip if the instruction is not OpTypeFloat.
+ if (MI->getOpcode() != SPIRV::OpTypeFloat)
+ continue;
+
+ // Collect the SPIRV type.
+ SPIRVFloatTypes.push_back(MI);
+ }
+
+ for (const auto &[Func, FPFastMathDefaultInfoVec] :
+ MAI->FPFastMathDefaultInfoMap) {
+ if (FPFastMathDefaultInfoVec.empty())
+ continue;
+
+ for (const MachineInstr *MI : SPIRVFloatTypes) {
+ unsigned OpTypeFloatSize = MI->getOperand(1).getImm();
+ unsigned Index = computeFPFastMathDefaultInfoVecIndex(OpTypeFloatSize);
+ assert(Index < FPFastMathDefaultInfoVec.size() &&
+ "Index out of bounds for FPFastMathDefaultInfoVec");
+ const auto &FPFastMathDefaultInfo = FPFastMathDefaultInfoVec[Index];
+ assert(FPFastMathDefaultInfo.Ty &&
+ "Expected target type for FPFastMathDefaultInfo");
+ assert(FPFastMathDefaultInfo.Ty->getScalarSizeInBits() ==
+ OpTypeFloatSize &&
+ "Mismatched float type size");
+ MCInst Inst;
+ Inst.setOpcode(SPIRV::OpExecutionMode);
+ MCRegister FuncReg = MAI->getFuncReg(Func);
+ assert(FuncReg.isValid());
+ Inst.addOperand(MCOperand::createReg(FuncReg));
+ Inst.addOperand(
+ MCOperand::createImm(SPIRV::ExecutionMode::FPFastMathDefault));
+ MCRegister TypeReg =
+ MAI->getRegisterAlias(MI->getMF(), MI->getOperand(0).getReg());
+ Inst.addOperand(MCOperand::createReg(TypeReg));
+ unsigned Flags = FPFastMathDefaultInfo.FastMathFlags;
+ if (FPFastMathDefaultInfo.ContractionOff &&
+ (Flags & SPIRV::FPFastMathMode::AllowContract) &&
+ FPFastMathDefaultInfo.FPFastMathDefault)
----------------
Keenuts wrote:
Might be missing something.
`ContractionOff + FastMath = illegal` from the spec point of view. But as you mention when using `float_controls_2`, `ContractionOff` is not emitted. Instead the `FastMathDefault` mask is used. So If I get it, even if the spec states it's illegal, here we are fine since in the end we won't emit the `ContractionOff` decoration. We are just converting the old decorations to the new bitmask format.
If that's the case, seems the condition should then be independent of the result of `FPFastMathDefaultInfo.FPFastMathDefault` but be more `if we have both NoContraction and AllowContract bit is set, something is wrong`, independently from the `FPFastMathDefault`
And we might want to assert that if any bit is set in the FPFastMathDefaultInfo, `FPFastMathDefault` should be true no?
https://github.com/llvm/llvm-project/pull/146941
More information about the llvm-commits
mailing list