Skip to content

Commit ce5257a

Browse files
AMDGPU/GlobalISel: Report RegBankLegalize errors using reportGISelFailure
Use standard GlobalISel error reporting with reportGISelFailure and pass returning false instead of llvm_unreachable. Also enables -global-isel-abort=0 or 2 for -global-isel -new-reg-bank-select. Note: new-reg-bank-select with abort 0 or 2 runs LCSSA, while "intended use" without abort or with abort 1 does not run LCSSA.
1 parent 135ddf1 commit ce5257a

File tree

5 files changed

+77
-48
lines changed

5 files changed

+77
-48
lines changed

llvm/lib/Target/AMDGPU/AMDGPURegBankLegalize.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ bool AMDGPURegBankLegalize::runOnMachineFunction(MachineFunction &MF) {
418418
const RegBankLegalizeRules &RBLRules = getRules(ST, MRI);
419419

420420
// Logic that does legalization based on IDs assigned to Opcode.
421-
RegBankLegalizeHelper RBLHelper(B, MUI, RBI, RBLRules);
421+
RegBankLegalizeHelper RBLHelper(B, MUI, RBI, TPC, RBLRules);
422422

423423
SmallVector<MachineInstr *> AllInst;
424424

@@ -435,7 +435,8 @@ bool AMDGPURegBankLegalize::runOnMachineFunction(MachineFunction &MF) {
435435
unsigned Opc = MI->getOpcode();
436436
// Insert point for use operands needs some calculation.
437437
if (Opc == AMDGPU::G_PHI) {
438-
RBLHelper.applyMappingPHI(*MI);
438+
if (!RBLHelper.applyMappingPHI(*MI))
439+
return false;
439440
continue;
440441
}
441442

@@ -466,7 +467,8 @@ bool AMDGPURegBankLegalize::runOnMachineFunction(MachineFunction &MF) {
466467
// S1 rules are in RegBankLegalizeRules.
467468
}
468469

469-
RBLHelper.findRuleAndApplyMapping(*MI);
470+
if (!RBLHelper.findRuleAndApplyMapping(*MI))
471+
return false;
470472
}
471473

472474
// Sgpr S1 clean up combines:

llvm/lib/Target/AMDGPU/AMDGPURegBankLegalizeHelper.cpp

Lines changed: 50 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -31,29 +31,47 @@ using namespace AMDGPU;
3131

3232
RegBankLegalizeHelper::RegBankLegalizeHelper(
3333
MachineIRBuilder &B, const MachineUniformityInfo &MUI,
34-
const RegisterBankInfo &RBI, const RegBankLegalizeRules &RBLRules)
35-
: ST(B.getMF().getSubtarget<GCNSubtarget>()), B(B), MRI(*B.getMRI()),
36-
MUI(MUI), RBI(RBI), RBLRules(RBLRules), IsWave32(ST.isWave32()),
34+
const RegisterBankInfo &RBI, const TargetPassConfig &TPC,
35+
const RegBankLegalizeRules &RBLRules)
36+
: MF(B.getMF()), ST(MF.getSubtarget<GCNSubtarget>()), B(B),
37+
MRI(*B.getMRI()), MUI(MUI), RBI(RBI), TPC(TPC), MORE(MF, nullptr),
38+
RBLRules(RBLRules), IsWave32(ST.isWave32()),
3739
SgprRB(&RBI.getRegBank(AMDGPU::SGPRRegBankID)),
3840
VgprRB(&RBI.getRegBank(AMDGPU::VGPRRegBankID)),
3941
VccRB(&RBI.getRegBank(AMDGPU::VCCRegBankID)) {}
4042

41-
void RegBankLegalizeHelper::findRuleAndApplyMapping(MachineInstr &MI) {
42-
const SetOfRulesForOpcode &RuleSet = RBLRules.getRulesForOpc(MI);
43-
const RegBankLLTMapping &Mapping = RuleSet.findMappingForMI(MI, MRI, MUI);
43+
bool RegBankLegalizeHelper::findRuleAndApplyMapping(MachineInstr &MI) {
44+
const SetOfRulesForOpcode *RuleSet = RBLRules.getRulesForOpc(MI);
45+
if (!RuleSet) {
46+
reportGISelFailure(MF, TPC, MORE, "amdgpu-regbanklegalize",
47+
"No AMDGPU RegBankLegalize rules defined for opcode",
48+
MI);
49+
return false;
50+
}
51+
52+
const RegBankLLTMapping *Mapping = RuleSet->findMappingForMI(MI, MRI, MUI);
53+
if (!Mapping) {
54+
reportGISelFailure(MF, TPC, MORE, "amdgpu-regbanklegalize",
55+
"AMDGPU RegBankLegalize: none of the rules defined with "
56+
"'Any' for MI's opcode matched MI",
57+
MI);
58+
return false;
59+
}
4460

4561
SmallSet<Register, 4> WaterfallSgprs;
4662
unsigned OpIdx = 0;
47-
if (Mapping.DstOpMapping.size() > 0) {
63+
if (Mapping->DstOpMapping.size() > 0) {
4864
B.setInsertPt(*MI.getParent(), std::next(MI.getIterator()));
49-
applyMappingDst(MI, OpIdx, Mapping.DstOpMapping);
65+
if (!applyMappingDst(MI, OpIdx, Mapping->DstOpMapping))
66+
return false;
5067
}
51-
if (Mapping.SrcOpMapping.size() > 0) {
68+
if (Mapping->SrcOpMapping.size() > 0) {
5269
B.setInstr(MI);
53-
applyMappingSrc(MI, OpIdx, Mapping.SrcOpMapping, WaterfallSgprs);
70+
applyMappingSrc(MI, OpIdx, Mapping->SrcOpMapping, WaterfallSgprs);
5471
}
5572

56-
lower(MI, Mapping, WaterfallSgprs);
73+
lower(MI, *Mapping, WaterfallSgprs);
74+
return true;
5775
}
5876

5977
bool RegBankLegalizeHelper::executeInWaterfallLoop(
@@ -1055,7 +1073,7 @@ RegBankLegalizeHelper::getRegBankFromID(RegBankLLTMappingApplyID ID) {
10551073
}
10561074
}
10571075

1058-
void RegBankLegalizeHelper::applyMappingDst(
1076+
bool RegBankLegalizeHelper::applyMappingDst(
10591077
MachineInstr &MI, unsigned &OpIdx,
10601078
const SmallVectorImpl<RegBankLLTMappingApplyID> &MethodIDs) {
10611079
// Defs start from operand 0
@@ -1180,13 +1198,18 @@ void RegBankLegalizeHelper::applyMappingDst(
11801198
break;
11811199
}
11821200
case InvalidMapping: {
1183-
LLVM_DEBUG(dbgs() << "Instruction with Invalid mapping: "; MI.dump(););
1184-
llvm_unreachable("missing fast rule for MI");
1201+
reportGISelFailure(
1202+
MF, TPC, MORE, "amdgpu-regbanklegalize",
1203+
"AMDGPU RegBankLegalize: missing fast rule ('Div' or 'Uni') for",
1204+
MI);
1205+
return false;
11851206
}
11861207
default:
11871208
llvm_unreachable("ID not supported");
11881209
}
11891210
}
1211+
1212+
return true;
11901213
}
11911214

11921215
void RegBankLegalizeHelper::applyMappingSrc(
@@ -1348,7 +1371,7 @@ void RegBankLegalizeHelper::applyMappingSrc(
13481371
}
13491372
}
13501373

1351-
void RegBankLegalizeHelper::applyMappingPHI(MachineInstr &MI) {
1374+
bool RegBankLegalizeHelper::applyMappingPHI(MachineInstr &MI) {
13521375
Register Dst = MI.getOperand(0).getReg();
13531376
LLT Ty = MRI.getType(Dst);
13541377

@@ -1371,28 +1394,31 @@ void RegBankLegalizeHelper::applyMappingPHI(MachineInstr &MI) {
13711394
MI.getOperand(i).setReg(NewUse.getReg(0));
13721395
}
13731396

1374-
return;
1397+
return true;
13751398
}
13761399

1377-
// ALL divergent i1 phis should be already lowered and inst-selected into PHI
1378-
// with sgpr reg class and S1 LLT.
1400+
// ALL divergent i1 phis should have been lowered and inst-selected into PHI
1401+
// with sgpr reg class and S1 LLT in AMDGPUGlobalISelDivergenceLowering pass.
13791402
// Note: this includes divergent phis that don't require lowering.
13801403
if (Ty == LLT::scalar(1) && MUI.isDivergent(Dst)) {
1381-
LLVM_DEBUG(dbgs() << "Divergent S1 G_PHI: "; MI.dump(););
1382-
llvm_unreachable("Make sure to run AMDGPUGlobalISelDivergenceLowering "
1383-
"before RegBankLegalize to lower lane mask(vcc) phis");
1404+
reportGISelFailure(MF, TPC, MORE, "amdgpu-regbanklegalize",
1405+
"AMDGPU RegBankLegalize: Can't lower divergent S1 G_PHI",
1406+
MI);
1407+
return false;
13841408
}
13851409

13861410
// We accept all types that can fit in some register class.
13871411
// Uniform G_PHIs have all sgpr registers.
13881412
// Divergent G_PHIs have vgpr dst but inputs can be sgpr or vgpr.
13891413
if (Ty == LLT::scalar(32) || Ty == LLT::pointer(1, 64) ||
13901414
Ty == LLT::pointer(4, 64)) {
1391-
return;
1415+
return true;
13921416
}
13931417

1394-
LLVM_DEBUG(dbgs() << "G_PHI not handled: "; MI.dump(););
1395-
llvm_unreachable("type not supported");
1418+
reportGISelFailure(MF, TPC, MORE, "amdgpu-regbanklegalize",
1419+
"AMDGPU RegBankLegalize: type not supported for G_PHI",
1420+
MI);
1421+
return false;
13961422
}
13971423

13981424
[[maybe_unused]] static bool verifyRegBankOnOperands(MachineInstr &MI,

llvm/lib/Target/AMDGPU/AMDGPURegBankLegalizeHelper.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
#include "AMDGPURegBankLegalizeRules.h"
1313
#include "llvm/ADT/SmallSet.h"
1414
#include "llvm/CodeGen/GlobalISel/GenericMachineInstrs.h"
15+
#include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h"
1516
#include "llvm/CodeGen/MachineRegisterInfo.h"
17+
#include "llvm/CodeGen/TargetPassConfig.h"
1618

1719
namespace llvm {
1820

@@ -27,11 +29,14 @@ namespace AMDGPU {
2729
// to replace instruction. In other case InstApplyMethod will create new
2830
// instruction(s).
2931
class RegBankLegalizeHelper {
32+
MachineFunction &MF;
3033
const GCNSubtarget &ST;
3134
MachineIRBuilder &B;
3235
MachineRegisterInfo &MRI;
3336
const MachineUniformityInfo &MUI;
3437
const RegisterBankInfo &RBI;
38+
const TargetPassConfig &TPC;
39+
MachineOptimizationRemarkEmitter MORE;
3540
const RegBankLegalizeRules &RBLRules;
3641
const bool IsWave32;
3742
const RegisterBank *SgprRB;
@@ -79,12 +84,13 @@ class RegBankLegalizeHelper {
7984
public:
8085
RegBankLegalizeHelper(MachineIRBuilder &B, const MachineUniformityInfo &MUI,
8186
const RegisterBankInfo &RBI,
87+
const TargetPassConfig &TPC,
8288
const RegBankLegalizeRules &RBLRules);
8389

84-
void findRuleAndApplyMapping(MachineInstr &MI);
90+
bool findRuleAndApplyMapping(MachineInstr &MI);
8591

8692
// Manual apply helpers.
87-
void applyMappingPHI(MachineInstr &MI);
93+
bool applyMappingPHI(MachineInstr &MI);
8894
void applyMappingTrivial(MachineInstr &MI);
8995

9096
private:
@@ -97,7 +103,7 @@ class RegBankLegalizeHelper {
97103

98104
const RegisterBank *getRegBankFromID(RegBankLLTMappingApplyID ID);
99105

100-
void
106+
bool
101107
applyMappingDst(MachineInstr &MI, unsigned &OpIdx,
102108
const SmallVectorImpl<RegBankLLTMappingApplyID> &MethodIDs);
103109

llvm/lib/Target/AMDGPU/AMDGPURegBankLegalizeRules.cpp

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ UniformityLLTOpPredicateID LLTToBId(LLT Ty) {
243243
return _;
244244
}
245245

246-
const RegBankLLTMapping &
246+
const RegBankLLTMapping *
247247
SetOfRulesForOpcode::findMappingForMI(const MachineInstr &MI,
248248
const MachineRegisterInfo &MRI,
249249
const MachineUniformityInfo &MUI) const {
@@ -260,17 +260,16 @@ SetOfRulesForOpcode::findMappingForMI(const MachineInstr &MI,
260260
Slot = getFastPredicateSlot(LLTToId(MRI.getType(Reg)));
261261

262262
if (Slot != -1)
263-
return MUI.isUniform(Reg) ? Uni[Slot] : Div[Slot];
263+
return MUI.isUniform(Reg) ? &Uni[Slot] : &Div[Slot];
264264
}
265265

266266
// Slow search for more complex rules.
267267
for (const RegBankLegalizeRule &Rule : Rules) {
268268
if (Rule.Predicate.match(MI, MUI, MRI))
269-
return Rule.OperandMapping;
269+
return &Rule.OperandMapping;
270270
}
271271

272-
LLVM_DEBUG(dbgs() << "MI: "; MI.dump(););
273-
llvm_unreachable("None of the rules defined for MI's opcode matched MI");
272+
return nullptr;
274273
}
275274

276275
void SetOfRulesForOpcode::addRule(RegBankLegalizeRule Rule) {
@@ -353,27 +352,23 @@ RegBankLegalizeRules::addRulesForIOpcs(std::initializer_list<unsigned> OpcList,
353352
return RuleSetInitializer(OpcList, IRulesAlias, IRules, FastTypes);
354353
}
355354

356-
const SetOfRulesForOpcode &
355+
const SetOfRulesForOpcode *
357356
RegBankLegalizeRules::getRulesForOpc(MachineInstr &MI) const {
358357
unsigned Opc = MI.getOpcode();
359358
if (Opc == AMDGPU::G_INTRINSIC || Opc == AMDGPU::G_INTRINSIC_CONVERGENT ||
360359
Opc == AMDGPU::G_INTRINSIC_W_SIDE_EFFECTS ||
361360
Opc == AMDGPU::G_INTRINSIC_CONVERGENT_W_SIDE_EFFECTS) {
362361
unsigned IntrID = cast<GIntrinsic>(MI).getIntrinsicID();
363362
auto IRAIt = IRulesAlias.find(IntrID);
364-
if (IRAIt == IRulesAlias.end()) {
365-
LLVM_DEBUG(dbgs() << "MI: "; MI.dump(););
366-
llvm_unreachable("No rules defined for intrinsic opcode");
367-
}
368-
return IRules.at(IRAIt->second);
363+
if (IRAIt == IRulesAlias.end())
364+
return nullptr;
365+
return &IRules.at(IRAIt->second);
369366
}
370367

371368
auto GRAIt = GRulesAlias.find(Opc);
372-
if (GRAIt == GRulesAlias.end()) {
373-
LLVM_DEBUG(dbgs() << "MI: "; MI.dump(););
374-
llvm_unreachable("No rules defined for generic opcode");
375-
}
376-
return GRules.at(GRAIt->second);
369+
if (GRAIt == GRulesAlias.end())
370+
return nullptr;
371+
return &GRules.at(GRAIt->second);
377372
}
378373

379374
// Syntactic sugar wrapper for predicate lambda that enables '&&', '||' and '!'.

llvm/lib/Target/AMDGPU/AMDGPURegBankLegalizeRules.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ class SetOfRulesForOpcode {
287287
SetOfRulesForOpcode();
288288
SetOfRulesForOpcode(FastRulesTypes FastTypes);
289289

290-
const RegBankLLTMapping &
290+
const RegBankLLTMapping *
291291
findMappingForMI(const MachineInstr &MI, const MachineRegisterInfo &MRI,
292292
const MachineUniformityInfo &MUI) const;
293293

@@ -385,7 +385,7 @@ class RegBankLegalizeRules {
385385
MRI = &_MRI;
386386
};
387387

388-
const SetOfRulesForOpcode &getRulesForOpc(MachineInstr &MI) const;
388+
const SetOfRulesForOpcode *getRulesForOpc(MachineInstr &MI) const;
389389
};
390390

391391
} // end namespace AMDGPU

0 commit comments

Comments
 (0)