Skip to content
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
cbe7a07
Initial support for SPV_KHR_float_controls2.
maarquitos14 Jun 25, 2025
4c325ac
Fix test failures.
maarquitos14 Jun 30, 2025
a664820
Deprecations only apply when the extension is enabled.
maarquitos14 Jul 3, 2025
0648840
Add extension specific tests.
maarquitos14 Jul 3, 2025
34c0628
Further work to apply deprecations only when extension is enabled.
maarquitos14 Jul 3, 2025
1d87705
Remove leftover comment.
maarquitos14 Jul 3, 2025
de07e83
Yet more work to apply deprecations only when extension is enabled.
maarquitos14 Jul 3, 2025
3edf82a
Renamings and bugfixes.
maarquitos14 Jul 3, 2025
c0ef106
Renamings, bugfixes and undo undesired changes.
maarquitos14 Jul 3, 2025
97720d5
Undo undesired changes.
maarquitos14 Jul 3, 2025
3792464
Merge remote-tracking branch 'origin/main' into maronas/float-controls2
maarquitos14 Jul 4, 2025
eccd27a
Fix after-merge bugs.
maarquitos14 Jul 4, 2025
868c6a4
Fix clang-format issues.
maarquitos14 Jul 4, 2025
21c17d1
Replace undef with poison for placeholders.
maarquitos14 Jul 4, 2025
cfdc210
Add SPIR-V Instructions Mapped to LLVM Metadata section.
maarquitos14 Jul 7, 2025
9b8b43d
Address code review.
maarquitos14 Jul 7, 2025
a353295
Address code review feedback.
maarquitos14 Jul 16, 2025
f1403d6
Fix documentation build.
maarquitos14 Jul 17, 2025
37a3abe
Fix another error in documentation build.
maarquitos14 Jul 17, 2025
d76db3e
Ignore spirv.Decorations metadata related to FP flags.
maarquitos14 Aug 1, 2025
00548e5
Merge remote-tracking branch 'origin/main' into maronas/float-controls2
maarquitos14 Aug 1, 2025
f2aefbf
Address code review feedback.
maarquitos14 Aug 8, 2025
6a77fcd
Merge remote-tracking branch 'origin/main' into maronas/float-controls2
maarquitos14 Aug 8, 2025
848611a
Fix clang-format.
maarquitos14 Aug 8, 2025
527311d
Merge remote-tracking branch 'origin/main' into maronas/float-controls2
maarquitos14 Aug 26, 2025
bde0292
Address reviewer feedback.
maarquitos14 Aug 26, 2025
2295de4
Merge branch 'main' into maronas/float-controls2
maarquitos14 Aug 26, 2025
6420f43
Use constant instruction for flags.
maarquitos14 Sep 2, 2025
0bf9ca6
Enable spirv-val and update tests accordingly.
maarquitos14 Sep 2, 2025
5f26101
Merge remote-tracking branch 'origin/main' into maronas/float-controls2
maarquitos14 Sep 2, 2025
368793f
Merge branch 'maronas/float-controls2-conflict' into maronas/float-co…
maarquitos14 Sep 2, 2025
cb1065e
Rename new function to better represent what it does.
maarquitos14 Sep 2, 2025
d337ef2
Merge branch 'main' into maronas/float-controls2
maarquitos14 Sep 2, 2025
c19d296
Fix clang-format issues.
maarquitos14 Sep 2, 2025
c9d226c
Merge branch 'maronas/float-controls2-conflict' into maronas/float-co…
maarquitos14 Sep 2, 2025
b5ce97a
Remove unused variable.
maarquitos14 Sep 2, 2025
0cd1923
Merge remote-tracking branch 'origin/main' into maronas/float-controls2
maarquitos14 Sep 2, 2025
c973c1a
Fix test failure.
maarquitos14 Sep 2, 2025
f684280
Apply code review suggestions.
maarquitos14 Sep 11, 2025
931431b
Merge remote-tracking branch 'origin/main' into maronas/float-controls2
maarquitos14 Sep 11, 2025
f2af0d4
Fix clang-format issue.
maarquitos14 Sep 11, 2025
910cd69
Merge branch 'main' into maronas/float-controls2
MrSidims Sep 29, 2025
b487250
Merge branch 'main' into maronas/float-controls2
maarquitos14 Sep 29, 2025
9768733
Merge remote-tracking branch 'origin/main' into HEAD
maarquitos14 Sep 30, 2025
fe49f8b
Address test failures caused by main branch merge.
maarquitos14 Sep 30, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 81 additions & 22 deletions llvm/lib/Target/SPIRV/SPIRVAsmPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -574,23 +574,56 @@ void SPIRVAsmPrinter::outputExecutionMode(const Module &M) {
// 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
// to match the effect of ContractionOff. As of now, there are 3 FP
// types: fp16, fp32 and fp64.

// We only end up here because there is no "spirv.ExecutionMode"
// metadata, so that means no FPFastMathDefault. Therefore, we only
// need to make sure AllowContract is set to 0, as the rest of flags.
// We still need to emit the OpExecutionMode instruction, otherwise
// it's up to the client API to define the flags. Therefore, we need
// to find the constant with 0 value.

// Collect the SPIRVTypes for fp16, fp32, and fp64 and the constant of
// type int32 with 0 value to represent the FP Fast Math Mode.
std::vector<const MachineInstr *> SPIRVFloatTypes;
const MachineInstr *ConstZero = nullptr;
for (const MachineInstr *MI :
MAI->getMSInstrs(SPIRV::MB_TypeConstVars)) {
// Skip if the instruction is not OpTypeFloat.
if (MI->getOpcode() != SPIRV::OpTypeFloat)
// Skip if the instruction is not OpTypeFloat or OpConstant.
unsigned OpCode = MI->getOpcode();
if (OpCode != SPIRV::OpTypeFloat && OpCode != SPIRV::OpConstantNull)
continue;

// Skip if the target type is not fp16, fp32, fp64.
const unsigned OpTypeFloatSize = MI->getOperand(1).getImm();
if (OpTypeFloatSize != 16 && OpTypeFloatSize != 32 &&
OpTypeFloatSize != 64) {
continue;
// Collect the SPIRV type if it's a float.
if (OpCode == SPIRV::OpTypeFloat) {
// Skip if the target type is not fp16, fp32, fp64.
const unsigned OpTypeFloatSize = MI->getOperand(1).getImm();
if (OpTypeFloatSize != 16 && OpTypeFloatSize != 32 &&
OpTypeFloatSize != 64) {
continue;
}
SPIRVFloatTypes.push_back(MI);
} else {
// Check if the constant is int32, if not skip it.
const MachineRegisterInfo &MRI = MI->getMF()->getRegInfo();
MachineInstr *TypeMI = MRI.getVRegDef(MI->getOperand(1).getReg());
if (!TypeMI || TypeMI->getOperand(1).getImm() != 32)
continue;

ConstZero = MI;
}
}

// 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 3 FP
// types: fp16, fp32 and fp64.
for (const MachineInstr *MI : SPIRVFloatTypes) {
MCInst Inst;
Inst.setOpcode(SPIRV::OpExecutionMode);
Inst.setOpcode(SPIRV::OpExecutionModeId);
Inst.addOperand(MCOperand::createReg(FReg));
unsigned EM =
static_cast<unsigned>(SPIRV::ExecutionMode::FPFastMathDefault);
Expand All @@ -599,12 +632,10 @@ void SPIRVAsmPrinter::outputExecutionMode(const Module &M) {
MCRegister TypeReg =
MAI->getRegisterAlias(MF, MI->getOperand(0).getReg());
Inst.addOperand(MCOperand::createReg(TypeReg));
// We only end up here because there is no "spirv.ExecutionMode"
// metadata, so that means no FPFastMathDefault. Therefore, we only
// need to make sure AllowContract is set to 0, as the rest of flags.
// We still need to emit the OpExecutionMode instruction, otherwise
// it's up to the client API to define the flags.
Inst.addOperand(MCOperand::createImm(SPIRV::FPFastMathMode::None));
assert(ConstZero && "There should be a constant zero.");
MCRegister ConstReg = MAI->getRegisterAlias(
ConstZero->getMF(), ConstZero->getOperand(0).getReg());
Inst.addOperand(MCOperand::createReg(ConstReg));
outputMCInst(Inst);
}
} else {
Expand Down Expand Up @@ -665,15 +696,34 @@ void SPIRVAsmPrinter::outputAnnotations(const Module &M) {
}

void SPIRVAsmPrinter::outputFPFastMathDefaultInfo() {
// Collect the SPIRVTypes that are OpTypeFloat.
// Collect the SPIRVTypes that are OpTypeFloat and the constants of type
// int32, that might be used as FP Fast Math Mode.
std::vector<const MachineInstr *> SPIRVFloatTypes;
// Hashtable to associate immediate values with the constant holding them.
std::unordered_map<unsigned, const MachineInstr *> ConstMap;
for (const MachineInstr *MI : MAI->getMSInstrs(SPIRV::MB_TypeConstVars)) {
// Skip if the instruction is not OpTypeFloat.
if (MI->getOpcode() != SPIRV::OpTypeFloat)
// Skip if the instruction is not OpTypeFloat or OpConstant.
unsigned OpCode = MI->getOpcode();
if (OpCode != SPIRV::OpTypeFloat && OpCode != SPIRV::OpConstantI &&
OpCode != SPIRV::OpConstantNull)
continue;

// Collect the SPIRV type.
SPIRVFloatTypes.push_back(MI);
// Collect the SPIRV type if it's a float.
if (OpCode == SPIRV::OpTypeFloat) {
SPIRVFloatTypes.push_back(MI);
} else {
// Check if the constant is int32, if not skip it.
const MachineRegisterInfo &MRI = MI->getMF()->getRegInfo();
MachineInstr *TypeMI = MRI.getVRegDef(MI->getOperand(1).getReg());
if (!TypeMI || TypeMI->getOpcode() != SPIRV::OpTypeInt ||
TypeMI->getOperand(1).getImm() != 32)
continue;

if (OpCode == SPIRV::OpConstantI)
ConstMap[MI->getOperand(2).getImm()] = MI;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ints could be signed, so here seems like you could end up with a -1 being added as a max_uint or something like that no?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed the key of the map from unsigned to int.

else
ConstMap[0] = MI;
}
}

for (const auto &[Func, FPFastMathDefaultInfoVec] :
Expand All @@ -693,7 +743,7 @@ void SPIRVAsmPrinter::outputFPFastMathDefaultInfo() {
OpTypeFloatSize &&
"Mismatched float type size");
MCInst Inst;
Inst.setOpcode(SPIRV::OpExecutionMode);
Inst.setOpcode(SPIRV::OpExecutionModeId);
MCRegister FuncReg = MAI->getFuncReg(Func);
assert(FuncReg.isValid());
Inst.addOperand(MCOperand::createReg(FuncReg));
Expand Down Expand Up @@ -724,7 +774,16 @@ void SPIRVAsmPrinter::outputFPFastMathDefaultInfo() {
!FPFastMathDefaultInfo.SignedZeroInfNanPreserve &&
!FPFastMathDefaultInfo.FPFastMathDefault)
continue;
Inst.addOperand(MCOperand::createImm(Flags));

// Retrieve the constant instruction for the immediate value.
auto It = ConstMap.find(Flags);
if (It == ConstMap.end())
report_fatal_error("Expected constant instruction for FP Fast Math "
"Mode operand of FPFastMathDefault execution mode.");
const MachineInstr *ConstMI = It->second;
MCRegister ConstReg = MAI->getRegisterAlias(
ConstMI->getMF(), ConstMI->getOperand(0).getReg());
Inst.addOperand(MCOperand::createReg(ConstReg));
outputMCInst(Inst);
}
}
Expand Down
Loading