Skip to content

Commit 36ad912

Browse files
nhaehnlevinay-deshmukh
authored andcommitted
CodeGen: Handle bundled instructions in two-address-instructions pass (llvm#166212)
If the instruction with tied operands is a BUNDLE instruction and we handle it by replacing an operand, then we need to update the corresponding internal operands as well. Otherwise, the resulting MIR is invalid. The test case is degenerate in the sense that the bundle only contains a single instruction, but it is sufficient to exercise this issue.
1 parent 48adab7 commit 36ad912

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

llvm/lib/CodeGen/TwoAddressInstructionPass.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1665,6 +1665,17 @@ void TwoAddressInstructionImpl::processTiedPairs(MachineInstr *MI,
16651665
// by SubRegB is compatible with RegA with no subregister. So regardless of
16661666
// whether the dest oper writes a subreg, the source oper should not.
16671667
MO.setSubReg(0);
1668+
1669+
// Update uses of RegB to uses of RegA inside the bundle.
1670+
if (MI->isBundle()) {
1671+
for (MachineOperand &MO : mi_bundle_ops(*MI)) {
1672+
if (MO.isReg() && MO.getReg() == RegB) {
1673+
assert(MO.getSubReg() == 0 && SubRegB == 0 &&
1674+
"tied subregister uses in bundled instructions not supported");
1675+
MO.setReg(RegA);
1676+
}
1677+
}
1678+
}
16681679
}
16691680

16701681
if (AllUsesCopied) {
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 6
2+
# RUN: llc -mtriple=amdgcn -mcpu=gfx1200 %s --passes=two-address-instruction -verify-each -o - | FileCheck --check-prefixes=GCN %s
3+
4+
# Exercise very basic handling of BUNDLE'd instructions by the two-address-instruction pass.
5+
6+
# This test is an example where it is best to keep the two-address instruction
7+
# and resolve the tie with a COPY that is expected to be coalesced.
8+
---
9+
name: test_fmac_bundle
10+
body: |
11+
bb.0:
12+
13+
; GCN-LABEL: name: test_fmac_bundle
14+
; GCN: [[COPY:%[0-9]+]]:vgpr_32 = COPY $vgpr0
15+
; GCN-NEXT: [[COPY1:%[0-9]+]]:vgpr_32 = COPY $vgpr1
16+
; GCN-NEXT: [[V_ADD_U32_e64_:%[0-9]+]]:vgpr_32 = V_ADD_U32_e64 [[COPY]], [[COPY1]], 0, implicit $exec
17+
; GCN-NEXT: [[DEF:%[0-9]+]]:vgpr_32 = IMPLICIT_DEF
18+
; GCN-NEXT: [[DEF1:%[0-9]+]]:vgpr_32 = IMPLICIT_DEF
19+
; GCN-NEXT: [[COPY2:%[0-9]+]]:vgpr_32 = COPY [[V_ADD_U32_e64_]]
20+
; GCN-NEXT: BUNDLE implicit-def [[COPY2]], implicit [[DEF]], implicit [[DEF1]], implicit [[COPY2]](tied-def 0), implicit $mode, implicit $exec {
21+
; GCN-NEXT: [[COPY2:%[0-9]+]]:vgpr_32 = V_FMAC_F32_e32 killed [[DEF]], killed [[DEF1]], killed [[COPY2]], implicit $mode, implicit $exec
22+
; GCN-NEXT: }
23+
%10:vgpr_32 = COPY $vgpr0
24+
%11:vgpr_32 = COPY $vgpr1
25+
%2:vgpr_32 = V_ADD_U32_e64 %10, %11, 0, implicit $exec
26+
%0:vgpr_32 = IMPLICIT_DEF
27+
%1:vgpr_32 = IMPLICIT_DEF
28+
BUNDLE implicit-def %3:vgpr_32, implicit %0, implicit %1, implicit killed %2(tied-def 0), implicit $mode, implicit $exec {
29+
%3:vgpr_32 = V_FMAC_F32_e32 killed %0, killed %1, killed %2, implicit $mode, implicit $exec
30+
}
31+
32+
...
33+
34+
# This test is an example where conversion to three-address form would be beneficial.
35+
---
36+
name: test_fmac_reuse_bundle
37+
body: |
38+
bb.0:
39+
40+
; GCN-LABEL: name: test_fmac_reuse_bundle
41+
; GCN: [[COPY:%[0-9]+]]:vgpr_32 = COPY $vgpr0
42+
; GCN-NEXT: [[DEF:%[0-9]+]]:vgpr_32 = IMPLICIT_DEF
43+
; GCN-NEXT: [[DEF1:%[0-9]+]]:vgpr_32 = IMPLICIT_DEF
44+
; GCN-NEXT: [[COPY1:%[0-9]+]]:vgpr_32 = COPY [[COPY]]
45+
; GCN-NEXT: BUNDLE implicit-def [[COPY1]], implicit [[DEF]], implicit [[DEF1]], implicit [[COPY1]](tied-def 0), implicit $mode, implicit $exec {
46+
; GCN-NEXT: [[COPY1:%[0-9]+]]:vgpr_32 = V_FMAC_F32_e32 killed [[DEF]], killed [[DEF1]], killed [[COPY1]], implicit $mode, implicit $exec
47+
; GCN-NEXT: }
48+
; GCN-NEXT: [[V_ADD_U32_e64_:%[0-9]+]]:vgpr_32 = V_ADD_U32_e64 [[COPY1]], [[COPY]], 0, implicit $exec
49+
%2:vgpr_32 = COPY $vgpr0
50+
%0:vgpr_32 = IMPLICIT_DEF
51+
%1:vgpr_32 = IMPLICIT_DEF
52+
BUNDLE implicit-def %3:vgpr_32, implicit %0, implicit %1, implicit %2(tied-def 0), implicit $mode, implicit $exec {
53+
%3:vgpr_32 = V_FMAC_F32_e32 killed %0, killed %1, killed %2, implicit $mode, implicit $exec
54+
}
55+
%4:vgpr_32 = V_ADD_U32_e64 %3, %2, 0, implicit $exec
56+
57+
...

0 commit comments

Comments
 (0)