Skip to content

Commit da70a67

Browse files
[WIP]AMDGPU/GlobalISel: Gracefully report regbanklegalize errors
1 parent 51dd3ec commit da70a67

File tree

5 files changed

+63
-36
lines changed

5 files changed

+63
-36
lines changed

llvm/lib/Target/AMDGPU/AMDGPURegBankLegalize.cpp

Lines changed: 3 additions & 2 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

@@ -466,7 +466,8 @@ bool AMDGPURegBankLegalize::runOnMachineFunction(MachineFunction &MF) {
466466
// S1 rules are in RegBankLegalizeRules.
467467
}
468468

469-
RBLHelper.findRuleAndApplyMapping(*MI);
469+
if (!RBLHelper.findRuleAndApplyMapping(*MI))
470+
return false;
470471
}
471472

472473
// Sgpr S1 clean up combines:

llvm/lib/Target/AMDGPU/AMDGPURegBankLegalizeHelper.cpp

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "MCTargetDesc/AMDGPUMCTargetDesc.h"
2121
#include "llvm/CodeGen/GlobalISel/GenericMachineInstrs.h"
2222
#include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h"
23+
#include "llvm/CodeGen/MachineFunction.h"
2324
#include "llvm/CodeGen/MachineInstr.h"
2425
#include "llvm/CodeGen/MachineUniformityAnalysis.h"
2526
#include "llvm/IR/IntrinsicsAMDGPU.h"
@@ -31,29 +32,47 @@ using namespace AMDGPU;
3132

3233
RegBankLegalizeHelper::RegBankLegalizeHelper(
3334
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()),
35+
const RegisterBankInfo &RBI, const TargetPassConfig &TPC,
36+
const RegBankLegalizeRules &RBLRules)
37+
: MF(B.getMF()), ST(MF.getSubtarget<GCNSubtarget>()), B(B),
38+
MRI(*B.getMRI()), MUI(MUI), RBI(RBI), TPC(TPC), MORE(MF, nullptr),
39+
RBLRules(RBLRules), IsWave32(ST.isWave32()),
3740
SgprRB(&RBI.getRegBank(AMDGPU::SGPRRegBankID)),
3841
VgprRB(&RBI.getRegBank(AMDGPU::VGPRRegBankID)),
3942
VccRB(&RBI.getRegBank(AMDGPU::VCCRegBankID)) {}
4043

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

4562
SmallSet<Register, 4> WaterfallSgprs;
4663
unsigned OpIdx = 0;
47-
if (Mapping.DstOpMapping.size() > 0) {
64+
if (Mapping->DstOpMapping.size() > 0) {
4865
B.setInsertPt(*MI.getParent(), std::next(MI.getIterator()));
49-
applyMappingDst(MI, OpIdx, Mapping.DstOpMapping);
66+
if (!applyMappingDst(MI, OpIdx, Mapping->DstOpMapping))
67+
return false;
5068
}
51-
if (Mapping.SrcOpMapping.size() > 0) {
69+
if (Mapping->SrcOpMapping.size() > 0) {
5270
B.setInstr(MI);
53-
applyMappingSrc(MI, OpIdx, Mapping.SrcOpMapping, WaterfallSgprs);
71+
applyMappingSrc(MI, OpIdx, Mapping->SrcOpMapping, WaterfallSgprs);
5472
}
5573

56-
lower(MI, Mapping, WaterfallSgprs);
74+
lower(MI, *Mapping, WaterfallSgprs);
75+
return true;
5776
}
5877

5978
bool RegBankLegalizeHelper::executeInWaterfallLoop(
@@ -1055,7 +1074,7 @@ RegBankLegalizeHelper::getRegBankFromID(RegBankLLTMappingApplyID ID) {
10551074
}
10561075
}
10571076

1058-
void RegBankLegalizeHelper::applyMappingDst(
1077+
bool RegBankLegalizeHelper::applyMappingDst(
10591078
MachineInstr &MI, unsigned &OpIdx,
10601079
const SmallVectorImpl<RegBankLLTMappingApplyID> &MethodIDs) {
10611080
// Defs start from operand 0
@@ -1180,13 +1199,18 @@ void RegBankLegalizeHelper::applyMappingDst(
11801199
break;
11811200
}
11821201
case InvalidMapping: {
1183-
LLVM_DEBUG(dbgs() << "Instruction with Invalid mapping: "; MI.dump(););
1184-
llvm_unreachable("missing fast rule for MI");
1202+
reportGISelFailure(
1203+
MF, TPC, MORE, "amdgpu-regbanklegalize",
1204+
"AMDGPU RegBankLegalize: missing fast rule ('Div' or 'Uni') for ",
1205+
MI);
1206+
return false;
11851207
}
11861208
default:
11871209
llvm_unreachable("ID not supported");
11881210
}
11891211
}
1212+
1213+
return true;
11901214
}
11911215

11921216
void RegBankLegalizeHelper::applyMappingSrc(

llvm/lib/Target/AMDGPU/AMDGPURegBankLegalizeHelper.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@
1212
#include "AMDGPURegBankLegalizeRules.h"
1313
#include "llvm/ADT/SmallSet.h"
1414
#include "llvm/CodeGen/GlobalISel/GenericMachineInstrs.h"
15+
#include "llvm/CodeGen/MachineFunction.h"
16+
#include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h"
1517
#include "llvm/CodeGen/MachineRegisterInfo.h"
18+
#include "llvm/CodeGen/TargetPassConfig.h"
1619

1720
namespace llvm {
1821

@@ -27,11 +30,14 @@ namespace AMDGPU {
2730
// to replace instruction. In other case InstApplyMethod will create new
2831
// instruction(s).
2932
class RegBankLegalizeHelper {
33+
MachineFunction &MF;
3034
const GCNSubtarget &ST;
3135
MachineIRBuilder &B;
3236
MachineRegisterInfo &MRI;
3337
const MachineUniformityInfo &MUI;
3438
const RegisterBankInfo &RBI;
39+
const TargetPassConfig &TPC;
40+
MachineOptimizationRemarkEmitter MORE;
3541
const RegBankLegalizeRules &RBLRules;
3642
const bool IsWave32;
3743
const RegisterBank *SgprRB;
@@ -79,9 +85,10 @@ class RegBankLegalizeHelper {
7985
public:
8086
RegBankLegalizeHelper(MachineIRBuilder &B, const MachineUniformityInfo &MUI,
8187
const RegisterBankInfo &RBI,
88+
const TargetPassConfig &TPC,
8289
const RegBankLegalizeRules &RBLRules);
8390

84-
void findRuleAndApplyMapping(MachineInstr &MI);
91+
bool findRuleAndApplyMapping(MachineInstr &MI);
8592

8693
// Manual apply helpers.
8794
void applyMappingPHI(MachineInstr &MI);
@@ -97,7 +104,7 @@ class RegBankLegalizeHelper {
97104

98105
const RegisterBank *getRegBankFromID(RegBankLLTMappingApplyID ID);
99106

100-
void
107+
bool
101108
applyMappingDst(MachineInstr &MI, unsigned &OpIdx,
102109
const SmallVectorImpl<RegBankLLTMappingApplyID> &MethodIDs);
103110

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)