Skip to content

Commit eb80a51

Browse files
author
Aditya Nandakumar
committed
[GlobalISel]: Fix bug where we can report GISelFailure on erased instructions
The original instruction might get legalized and erased and expanded into intermediate instructions and the intermediate instructions might fail legalization. This end up in reporting GISelFailure on the erased instruction. Instead report GISelFailure on the intermediate instruction which failed legalization. Reviewed by: ab llvm-svn: 299802
1 parent 492db48 commit eb80a51

File tree

4 files changed

+38
-41
lines changed

4 files changed

+38
-41
lines changed

llvm/include/llvm/CodeGen/GlobalISel/LegalizerHelper.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,6 @@ class LegalizerHelper {
5757
/// registers as \p MI.
5858
LegalizeResult legalizeInstrStep(MachineInstr &MI);
5959

60-
LegalizeResult legalizeInstr(MachineInstr &MI);
61-
6260
/// Legalize an instruction by emiting a runtime library call instead.
6361
LegalizeResult libcall(MachineInstr &MI);
6462

@@ -85,6 +83,10 @@ class LegalizerHelper {
8583
LegalizeResult moreElementsVector(MachineInstr &MI, unsigned TypeIdx,
8684
LLT WideTy);
8785

86+
/// Expose MIRBuilder so clients can set their own RecordInsertInstruction
87+
/// functions
88+
MachineIRBuilder MIRBuilder;
89+
8890
private:
8991

9092
/// Helper function to split a wide generic register into bitwise blocks with
@@ -93,7 +95,6 @@ class LegalizerHelper {
9395
void extractParts(unsigned Reg, LLT Ty, int NumParts,
9496
SmallVectorImpl<unsigned> &Ops);
9597

96-
MachineIRBuilder MIRBuilder;
9798
MachineRegisterInfo &MRI;
9899
const LegalizerInfo &LI;
99100
};

llvm/lib/CodeGen/GlobalISel/Legalizer.cpp

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -171,21 +171,34 @@ bool Legalizer::runOnMachineFunction(MachineFunction &MF) {
171171
// and are assumed to be legal.
172172
if (!isPreISelGenericOpcode(MI->getOpcode()))
173173
continue;
174-
175-
auto Res = Helper.legalizeInstr(*MI);
176-
177-
// Error out if we couldn't legalize this instruction. We may want to fall
178-
// back to DAG ISel instead in the future.
179-
if (Res == LegalizerHelper::UnableToLegalize) {
180-
reportGISelFailure(MF, TPC, MORE, "gisel-legalize",
181-
"unable to legalize instruction", *MI);
182-
return false;
183-
}
184-
185-
Changed |= Res == LegalizerHelper::Legalized;
174+
SmallVector<MachineInstr *, 4> WorkList;
175+
Helper.MIRBuilder.recordInsertions(
176+
[&](MachineInstr *MI) { WorkList.push_back(MI); });
177+
WorkList.push_back(&*MI);
178+
179+
LegalizerHelper::LegalizeResult Res;
180+
unsigned Idx = 0;
181+
do {
182+
Res = Helper.legalizeInstrStep(*WorkList[Idx]);
183+
// Error out if we couldn't legalize this instruction. We may want to
184+
// fall
185+
// back to DAG ISel instead in the future.
186+
if (Res == LegalizerHelper::UnableToLegalize) {
187+
Helper.MIRBuilder.stopRecordingInsertions();
188+
if (Res == LegalizerHelper::UnableToLegalize) {
189+
reportGISelFailure(MF, TPC, MORE, "gisel-legalize",
190+
"unable to legalize instruction",
191+
*WorkList[Idx]);
192+
return false;
193+
}
194+
}
195+
Changed |= Res == LegalizerHelper::Legalized;
196+
++Idx;
197+
} while (Idx < WorkList.size());
198+
199+
Helper.MIRBuilder.stopRecordingInsertions();
186200
}
187201

188-
189202
MachineRegisterInfo &MRI = MF.getRegInfo();
190203
const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
191204
for (auto &MBB : MF) {

llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -57,31 +57,6 @@ LegalizerHelper::legalizeInstrStep(MachineInstr &MI) {
5757
}
5858
}
5959

60-
LegalizerHelper::LegalizeResult
61-
LegalizerHelper::legalizeInstr(MachineInstr &MI) {
62-
SmallVector<MachineInstr *, 4> WorkList;
63-
MIRBuilder.recordInsertions(
64-
[&](MachineInstr *MI) { WorkList.push_back(MI); });
65-
WorkList.push_back(&MI);
66-
67-
bool Changed = false;
68-
LegalizeResult Res;
69-
unsigned Idx = 0;
70-
do {
71-
Res = legalizeInstrStep(*WorkList[Idx]);
72-
if (Res == UnableToLegalize) {
73-
MIRBuilder.stopRecordingInsertions();
74-
return UnableToLegalize;
75-
}
76-
Changed |= Res == Legalized;
77-
++Idx;
78-
} while (Idx < WorkList.size());
79-
80-
MIRBuilder.stopRecordingInsertions();
81-
82-
return Changed ? Legalized : AlreadyLegal;
83-
}
84-
8560
void LegalizerHelper::extractParts(unsigned Reg, LLT Ty, int NumParts,
8661
SmallVectorImpl<unsigned> &VRegs) {
8762
for (int i = 0; i < NumParts; ++i)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
;RUN: llc -mtriple=aarch64-unknown-unknown -o - -global-isel -global-isel-abort=2 %s 2>&1 | FileCheck %s
2+
; CHECK: fallback
3+
; CHECK-LABEL: foo
4+
define i16 @foo(half* %p) {
5+
%tmp0 = load half, half* %p
6+
%tmp1 = fptoui half %tmp0 to i16
7+
ret i16 %tmp1
8+
}

0 commit comments

Comments
 (0)