Skip to content

Commit 6277189

Browse files
committed
[MacroFusion][RISCV] Allocate same register for second instruction of fusible pair
We add a MI flag to indicate the constraint and set this flag to true for the second instruction of fusible pairs in pre-regalloc macrofusion. Then, we add register allocation hints for it. During regalloc, the allocator will choose the same register according to the hint. This is a PoC currently.
1 parent 99a8ffe commit 6277189

File tree

4 files changed

+50
-0
lines changed

4 files changed

+50
-0
lines changed

llvm/include/llvm/CodeGen/MachineInstr.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ class MachineInstr
115115
// this instruction.
116116
Unpredictable = 1 << 16, // Instruction with unpredictable condition.
117117
NoConvergent = 1 << 17, // Call does not require convergence guarantees.
118+
Fusible = 1 << 18, // Instruction is the second of a fusible pair.
118119
};
119120

120121
private:
@@ -1031,6 +1032,9 @@ class MachineInstr
10311032
return hasProperty(MCID::Convergent, Type);
10321033
}
10331034

1035+
/// Return true if this instruction is fusible.
1036+
bool isFusible() const { return getFlag(Fusible); }
1037+
10341038
/// Returns true if the specified instruction has a delay slot
10351039
/// which must be filled by the code generator.
10361040
bool hasDelaySlot(QueryType Type = AnyInBundle) const {

llvm/lib/CodeGen/MacroFusion.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,12 @@ bool llvm::fuseInstructionPair(ScheduleDAGInstrs &DAG, SUnit &FirstSU,
128128
}
129129
}
130130

131+
// Mark the second instruction of fusible pair as MachineInstr::Fusible if
132+
// this mutation is running in pre-ra scheduler.
133+
if (!DAG.MF.getProperties().hasProperty(
134+
MachineFunctionProperties::Property::NoVRegs))
135+
SecondSU.getInstr()->setFlag(MachineInstr::Fusible);
136+
131137
++NumFused;
132138
return true;
133139
}

llvm/lib/Target/RISCV/RISCVRegisterInfo.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -838,6 +838,9 @@ bool RISCVRegisterInfo::getRegAllocationHints(
838838
tryAddHint(MO, MI.getOperand(0), NeedGPRC);
839839
}
840840
}
841+
if (MI.isFusible())
842+
if (OpIdx == 1 || (OpIdx == 2 && MI.isCommutable()))
843+
tryAddHint(MO, MI.getOperand(0), false);
841844
}
842845

843846
for (MCPhysReg OrderReg : Order)

llvm/test/CodeGen/RISCV/pr76779.ll

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
2+
;RUN: llc < %s -mtriple=riscv64 -mattr=+f -target-abi=lp64f \
3+
;RUN: | FileCheck %s --check-prefix=NOFUSION
4+
;RUN: llc < %s -mtriple=riscv64 -mattr=+f,+lui-addi-fusion \
5+
;RUN: -target-abi=lp64f | FileCheck %s --check-prefix=FUSION
6+
;RUN: llc < %s -mtriple=riscv64 -mattr=+f,+lui-addi-fusion,+use-postra-scheduler \
7+
;RUN: -target-abi=lp64f | FileCheck %s --check-prefixes=FUSION-POSTRA
8+
9+
define void @foo(i32 noundef signext %0, i32 noundef signext %1) {
10+
; NOFUSION-LABEL: foo:
11+
; NOFUSION: # %bb.0:
12+
; NOFUSION-NEXT: lui a0, 3014
13+
; NOFUSION-NEXT: addiw a2, a0, 334
14+
; NOFUSION-NEXT: mv a0, a1
15+
; NOFUSION-NEXT: mv a1, a2
16+
; NOFUSION-NEXT: tail bar
17+
;
18+
; FUSION-LABEL: foo:
19+
; FUSION: # %bb.0:
20+
; FUSION-NEXT: lui a2, 3014
21+
; FUSION-NEXT: addiw a2, a2, 334
22+
; FUSION-NEXT: mv a0, a1
23+
; FUSION-NEXT: mv a1, a2
24+
; FUSION-NEXT: tail bar
25+
;
26+
; FUSION-POSTRA-LABEL: foo:
27+
; FUSION-POSTRA: # %bb.0:
28+
; FUSION-POSTRA-NEXT: lui a2, 3014
29+
; FUSION-POSTRA-NEXT: addiw a2, a2, 334
30+
; FUSION-POSTRA-NEXT: mv a0, a1
31+
; FUSION-POSTRA-NEXT: mv a1, a2
32+
; FUSION-POSTRA-NEXT: tail bar
33+
tail call void @bar(i32 noundef signext %1, i32 noundef signext 12345678)
34+
ret void
35+
}
36+
37+
declare void @bar(i32 noundef signext, i32 noundef signext)

0 commit comments

Comments
 (0)