Skip to content

Commit e41df5c

Browse files
authored
[SPIR-V] Fix OpDecorate emission after vreg def. (#114426)
In SPIR-V, OpDecorate instructions are allowed to forward-declare a virtual register. But while we are at the MIR level, we must comply with stricter rules, meaning OpDecorate should be emited after, not before the reg definition. (In some cases, we defined those just before, switching to just after). Related to #110652 --------- Signed-off-by: Nathan Gauër <[email protected]>
1 parent 17d8ed7 commit e41df5c

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -990,13 +990,13 @@ bool SPIRVInstructionSelector::selectMemOperation(Register ResVReg,
990990
Register VarReg = MRI->createGenericVirtualRegister(LLT::scalar(64));
991991
GR.add(GV, GR.CurMF, VarReg);
992992

993-
buildOpDecorate(VarReg, I, TII, SPIRV::Decoration::Constant, {});
994993
BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(SPIRV::OpVariable))
995994
.addDef(VarReg)
996995
.addUse(GR.getSPIRVTypeID(VarTy))
997996
.addImm(SPIRV::StorageClass::UniformConstant)
998997
.addUse(Const)
999998
.constrainAllUses(TII, TRI, RBI);
999+
buildOpDecorate(VarReg, I, TII, SPIRV::Decoration::Constant, {});
10001000
SPIRVType *SourceTy = GR.getOrCreateSPIRVPointerType(
10011001
ValTy, I, TII, SPIRV::StorageClass::UniformConstant);
10021002
SrcReg = MRI->createGenericVirtualRegister(LLT::scalar(64));

llvm/lib/Target/SPIRV/SPIRVPreLegalizer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -829,7 +829,7 @@ static void insertSpirvDecorations(MachineFunction &MF, MachineIRBuilder MIB) {
829829
for (MachineInstr &MI : MBB) {
830830
if (!isSpvIntrinsic(MI, Intrinsic::spv_assign_decoration))
831831
continue;
832-
MIB.setInsertPt(*MI.getParent(), MI);
832+
MIB.setInsertPt(*MI.getParent(), MI.getNextNode());
833833
buildOpSpirvDecorations(MI.getOperand(1).getReg(), MIB,
834834
MI.getOperand(2).getMetadata());
835835
ToErase.push_back(&MI);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
; RUN: %if spirv-tools %{ llc -O0 -verify-machineinstrs -mtriple=spirv64-unknown-unknown %s -o - -filetype=obj | spirv-val %}
2+
; This test checks the OpDecorate MIR is generated after the associated
3+
; vreg definition in the case of an array size declared through this lowering.
4+
5+
define spir_func i32 @foo() {
6+
entry:
7+
%var = alloca i64
8+
br label %block
9+
10+
block:
11+
call void @llvm.memset.p0.i64(ptr align 8 %var, i8 0, i64 24, i1 false)
12+
ret i32 0
13+
}
14+
15+
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg)

0 commit comments

Comments
 (0)