Skip to content

Commit d6fdfe0

Browse files
authored
CodeGen: Record tied virtual register operands in finalizeBundle (llvm#166209)
This is in preparation of a future AMDGPU change where we are going to create bundles before register allocation and want to rely on the TwoAddressInstructionPass handling those bundles correctly. v2: - simplify the virtual register check and the test
1 parent cfca229 commit d6fdfe0

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

llvm/lib/CodeGen/MachineInstrBundle.cpp

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ void llvm::finalizeBundle(MachineBasicBlock &MBB,
136136
SmallSetVector<Register, 8> ExternUses;
137137
SmallSet<Register, 8> KilledUseSet;
138138
SmallSet<Register, 8> UndefUseSet;
139+
SmallVector<std::pair<Register, Register>> TiedOperands;
139140
for (auto MII = FirstMI; MII != LastMI; ++MII) {
140141
// Debug instructions have no effects to track.
141142
if (MII->isDebugInstr())
@@ -161,6 +162,15 @@ void llvm::finalizeBundle(MachineBasicBlock &MBB,
161162
// External def is now killed.
162163
KilledUseSet.insert(Reg);
163164
}
165+
if (MO.isTied() && Reg.isVirtual()) {
166+
// Record tied operand constraints that involve virtual registers so
167+
// that bundles that are formed pre-register allocation reflect the
168+
// relevant constraints.
169+
unsigned TiedIdx = MII->findTiedOperandIdx(MO.getOperandNo());
170+
MachineOperand &TiedMO = MII->getOperand(TiedIdx);
171+
Register DefReg = TiedMO.getReg();
172+
TiedOperands.emplace_back(DefReg, Reg);
173+
}
164174
}
165175
}
166176

@@ -203,7 +213,17 @@ void llvm::finalizeBundle(MachineBasicBlock &MBB,
203213
bool isKill = KilledUseSet.contains(Reg);
204214
bool isUndef = UndefUseSet.contains(Reg);
205215
MIB.addReg(Reg, getKillRegState(isKill) | getUndefRegState(isUndef) |
206-
getImplRegState(true));
216+
getImplRegState(true));
217+
}
218+
219+
for (auto [DefReg, UseReg] : TiedOperands) {
220+
unsigned DefIdx =
221+
std::distance(LocalDefs.begin(), llvm::find(LocalDefs, DefReg));
222+
unsigned UseIdx =
223+
std::distance(ExternUses.begin(), llvm::find(ExternUses, UseReg));
224+
assert(DefIdx < LocalDefs.size());
225+
assert(UseIdx < ExternUses.size());
226+
MIB->tieOperands(DefIdx, LocalDefs.size() + UseIdx);
207227
}
208228
}
209229

llvm/test/CodeGen/AMDGPU/finalizebundle.mir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ name: test_tied
4040
body: |
4141
bb.0:
4242
; CHECK-LABEL: name: test_tied
43-
; CHECK: BUNDLE implicit-def %0, implicit-def %2, implicit %1:vgpr_32, implicit $mode, implicit $exec {
43+
; CHECK: BUNDLE implicit-def %0, implicit-def %2, implicit %1:vgpr_32(tied-def 1), implicit $mode, implicit $exec {
4444
; CHECK-NEXT: [[COPY:%[0-9]+]]:vgpr_32 = COPY %1:vgpr_32
4545
; CHECK-NEXT: [[V_FMAC_F16_e32_:%[0-9]+]]:vgpr_32 = V_FMAC_F16_e32 internal [[COPY]], internal [[COPY]], %1:vgpr_32, implicit $mode, implicit $exec
4646
; CHECK-NEXT: }

0 commit comments

Comments
 (0)