Skip to content

Commit 1485d0e

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 25b6a15 commit 1485d0e

File tree

5 files changed

+70
-46
lines changed

5 files changed

+70
-46
lines changed

llvm/lib/Target/AMDGPU/AMDGPURegBankLegalize.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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: 47 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -32,28 +32,45 @@ using namespace AMDGPU;
3232
RegBankLegalizeHelper::RegBankLegalizeHelper(
3333
MachineIRBuilder &B, const MachineUniformityInfo &MUI,
3434
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()),
35+
: MF(B.getMF()), ST(MF.getSubtarget<GCNSubtarget>()), B(B),
36+
MRI(*B.getMRI()), MUI(MUI), RBI(RBI), MORE(MF, nullptr),
37+
RBLRules(RBLRules), IsWave32(ST.isWave32()),
3738
SgprRB(&RBI.getRegBank(AMDGPU::SGPRRegBankID)),
3839
VgprRB(&RBI.getRegBank(AMDGPU::VGPRRegBankID)),
3940
VccRB(&RBI.getRegBank(AMDGPU::VCCRegBankID)) {}
4041

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

4560
SmallSet<Register, 4> WaterfallSgprs;
4661
unsigned OpIdx = 0;
47-
if (Mapping.DstOpMapping.size() > 0) {
62+
if (Mapping->DstOpMapping.size() > 0) {
4863
B.setInsertPt(*MI.getParent(), std::next(MI.getIterator()));
49-
applyMappingDst(MI, OpIdx, Mapping.DstOpMapping);
64+
if (!applyMappingDst(MI, OpIdx, Mapping->DstOpMapping))
65+
return false;
5066
}
51-
if (Mapping.SrcOpMapping.size() > 0) {
67+
if (Mapping->SrcOpMapping.size() > 0) {
5268
B.setInstr(MI);
53-
applyMappingSrc(MI, OpIdx, Mapping.SrcOpMapping, WaterfallSgprs);
69+
applyMappingSrc(MI, OpIdx, Mapping->SrcOpMapping, WaterfallSgprs);
5470
}
5571

56-
lower(MI, Mapping, WaterfallSgprs);
72+
lower(MI, *Mapping, WaterfallSgprs);
73+
return true;
5774
}
5875

5976
bool RegBankLegalizeHelper::executeInWaterfallLoop(
@@ -1055,7 +1072,7 @@ RegBankLegalizeHelper::getRegBankFromID(RegBankLLTMappingApplyID ID) {
10551072
}
10561073
}
10571074

1058-
void RegBankLegalizeHelper::applyMappingDst(
1075+
bool RegBankLegalizeHelper::applyMappingDst(
10591076
MachineInstr &MI, unsigned &OpIdx,
10601077
const SmallVectorImpl<RegBankLLTMappingApplyID> &MethodIDs) {
10611078
// Defs start from operand 0
@@ -1180,13 +1197,17 @@ void RegBankLegalizeHelper::applyMappingDst(
11801197
break;
11811198
}
11821199
case InvalidMapping: {
1183-
LLVM_DEBUG(dbgs() << "Instruction with Invalid mapping: "; MI.dump(););
1184-
llvm_unreachable("missing fast rule for MI");
1200+
reportGISelFailure(
1201+
MF, MORE, "amdgpu-regbanklegalize",
1202+
"AMDGPU RegBankLegalize: missing fast rule ('Div' or 'Uni') for", MI);
1203+
return false;
11851204
}
11861205
default:
11871206
llvm_unreachable("ID not supported");
11881207
}
11891208
}
1209+
1210+
return true;
11901211
}
11911212

11921213
void RegBankLegalizeHelper::applyMappingSrc(
@@ -1348,7 +1369,7 @@ void RegBankLegalizeHelper::applyMappingSrc(
13481369
}
13491370
}
13501371

1351-
void RegBankLegalizeHelper::applyMappingPHI(MachineInstr &MI) {
1372+
bool RegBankLegalizeHelper::applyMappingPHI(MachineInstr &MI) {
13521373
Register Dst = MI.getOperand(0).getReg();
13531374
LLT Ty = MRI.getType(Dst);
13541375

@@ -1371,28 +1392,31 @@ void RegBankLegalizeHelper::applyMappingPHI(MachineInstr &MI) {
13711392
MI.getOperand(i).setReg(NewUse.getReg(0));
13721393
}
13731394

1374-
return;
1395+
return true;
13751396
}
13761397

1377-
// ALL divergent i1 phis should be already lowered and inst-selected into PHI
1378-
// with sgpr reg class and S1 LLT.
1398+
// ALL divergent i1 phis should have been lowered and inst-selected into PHI
1399+
// with sgpr reg class and S1 LLT in AMDGPUGlobalISelDivergenceLowering pass.
13791400
// Note: this includes divergent phis that don't require lowering.
13801401
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");
1402+
reportGISelFailure(MF, MORE, "amdgpu-regbanklegalize",
1403+
"AMDGPU RegBankLegalize: Can't lower divergent S1 G_PHI",
1404+
MI);
1405+
return false;
13841406
}
13851407

13861408
// We accept all types that can fit in some register class.
13871409
// Uniform G_PHIs have all sgpr registers.
13881410
// Divergent G_PHIs have vgpr dst but inputs can be sgpr or vgpr.
13891411
if (Ty == LLT::scalar(32) || Ty == LLT::pointer(1, 64) ||
13901412
Ty == LLT::pointer(4, 64)) {
1391-
return;
1413+
return true;
13921414
}
13931415

1394-
LLVM_DEBUG(dbgs() << "G_PHI not handled: "; MI.dump(););
1395-
llvm_unreachable("type not supported");
1416+
reportGISelFailure(MF, MORE, "amdgpu-regbanklegalize",
1417+
"AMDGPU RegBankLegalize: type not supported for G_PHI",
1418+
MI);
1419+
return false;
13961420
}
13971421

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

llvm/lib/Target/AMDGPU/AMDGPURegBankLegalizeHelper.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
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"
1617

1718
namespace llvm {
@@ -27,11 +28,13 @@ namespace AMDGPU {
2728
// to replace instruction. In other case InstApplyMethod will create new
2829
// instruction(s).
2930
class RegBankLegalizeHelper {
31+
MachineFunction &MF;
3032
const GCNSubtarget &ST;
3133
MachineIRBuilder &B;
3234
MachineRegisterInfo &MRI;
3335
const MachineUniformityInfo &MUI;
3436
const RegisterBankInfo &RBI;
37+
MachineOptimizationRemarkEmitter MORE;
3538
const RegBankLegalizeRules &RBLRules;
3639
const bool IsWave32;
3740
const RegisterBank *SgprRB;
@@ -81,10 +84,10 @@ class RegBankLegalizeHelper {
8184
const RegisterBankInfo &RBI,
8285
const RegBankLegalizeRules &RBLRules);
8386

84-
void findRuleAndApplyMapping(MachineInstr &MI);
87+
bool findRuleAndApplyMapping(MachineInstr &MI);
8588

8689
// Manual apply helpers.
87-
void applyMappingPHI(MachineInstr &MI);
90+
bool applyMappingPHI(MachineInstr &MI);
8891
void applyMappingTrivial(MachineInstr &MI);
8992

9093
private:
@@ -97,7 +100,7 @@ class RegBankLegalizeHelper {
97100

98101
const RegisterBank *getRegBankFromID(RegBankLLTMappingApplyID ID);
99102

100-
void
103+
bool
101104
applyMappingDst(MachineInstr &MI, unsigned &OpIdx,
102105
const SmallVectorImpl<RegBankLLTMappingApplyID> &MethodIDs);
103106

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)