Skip to content

Commit 96e473a

Browse files
authored
[RFC][GlobalISel] Use Builders in MatchTable (llvm#65955)
The MatchTableExecutor did not use the MachineIRBuilder but instead created instructions ad-hoc. Making it use a Builder has the benefit that any observer added by a combine is now notified when instructions are created by MIR patterns. Another benefit is that it allows me to improve how constants are created in apply MIR patterns. `MachineIRBuilder::buildConstant` automatically handles splats for us, this means that we may change `addCImm` to use that and handle vector cases automatically.
1 parent 94d0a3c commit 96e473a

File tree

6 files changed

+45
-33
lines changed

6 files changed

+45
-33
lines changed

llvm/include/llvm/CodeGen/GlobalISel/GIMatchTableExecutor.h

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class APInt;
4040
class APFloat;
4141
class GISelKnownBits;
4242
class MachineInstr;
43+
class MachineIRBuilder;
4344
class MachineInstrBuilder;
4445
class MachineFunction;
4546
class MachineOperand;
@@ -555,15 +556,15 @@ class GIMatchTableExecutor {
555556
/// and false otherwise.
556557
template <class TgtExecutor, class PredicateBitset, class ComplexMatcherMemFn,
557558
class CustomRendererFn>
558-
bool executeMatchTable(
559-
TgtExecutor &Exec, NewMIVector &OutMIs, MatcherState &State,
560-
const ExecInfoTy<PredicateBitset, ComplexMatcherMemFn, CustomRendererFn>
561-
&ISelInfo,
562-
const int64_t *MatchTable, const TargetInstrInfo &TII,
563-
MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI,
564-
const RegisterBankInfo &RBI, const PredicateBitset &AvailableFeatures,
565-
CodeGenCoverage *CoverageInfo,
566-
GISelChangeObserver *Observer = nullptr) const;
559+
bool executeMatchTable(TgtExecutor &Exec, MatcherState &State,
560+
const ExecInfoTy<PredicateBitset, ComplexMatcherMemFn,
561+
CustomRendererFn> &ExecInfo,
562+
MachineIRBuilder &Builder, const int64_t *MatchTable,
563+
const TargetInstrInfo &TII, MachineRegisterInfo &MRI,
564+
const TargetRegisterInfo &TRI,
565+
const RegisterBankInfo &RBI,
566+
const PredicateBitset &AvailableFeatures,
567+
CodeGenCoverage *CoverageInfo) const;
567568

568569
virtual const int64_t *getMatchTable() const {
569570
llvm_unreachable("Should have been overridden by tablegen if used");

llvm/include/llvm/CodeGen/GlobalISel/GIMatchTableExecutorImpl.h

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "llvm/ADT/SmallVector.h"
1919
#include "llvm/CodeGen/GlobalISel/GIMatchTableExecutor.h"
2020
#include "llvm/CodeGen/GlobalISel/GISelChangeObserver.h"
21+
#include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h"
2122
#include "llvm/CodeGen/GlobalISel/Utils.h"
2223
#include "llvm/CodeGen/MachineInstrBuilder.h"
2324
#include "llvm/CodeGen/MachineOperand.h"
@@ -42,17 +43,20 @@ namespace llvm {
4243
template <class TgtExecutor, class PredicateBitset, class ComplexMatcherMemFn,
4344
class CustomRendererFn>
4445
bool GIMatchTableExecutor::executeMatchTable(
45-
TgtExecutor &Exec, NewMIVector &OutMIs, MatcherState &State,
46+
TgtExecutor &Exec, MatcherState &State,
4647
const ExecInfoTy<PredicateBitset, ComplexMatcherMemFn, CustomRendererFn>
4748
&ExecInfo,
48-
const int64_t *MatchTable, const TargetInstrInfo &TII,
49-
MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI,
50-
const RegisterBankInfo &RBI, const PredicateBitset &AvailableFeatures,
51-
CodeGenCoverage *CoverageInfo, GISelChangeObserver *Observer) const {
49+
MachineIRBuilder &Builder, const int64_t *MatchTable,
50+
const TargetInstrInfo &TII, MachineRegisterInfo &MRI,
51+
const TargetRegisterInfo &TRI, const RegisterBankInfo &RBI,
52+
const PredicateBitset &AvailableFeatures,
53+
CodeGenCoverage *CoverageInfo) const {
5254

5355
uint64_t CurrentIdx = 0;
5456
SmallVector<uint64_t, 4> OnFailResumeAt;
57+
NewMIVector OutMIs;
5558

59+
GISelChangeObserver *Observer = Builder.getObserver();
5660
// Bypass the flag check on the instruction, and only look at the MCInstrDesc.
5761
bool NoFPException = !State.MIs[0]->getDesc().mayRaiseFPException();
5862

@@ -71,14 +75,18 @@ bool GIMatchTableExecutor::executeMatchTable(
7175
return RejectAndResume;
7276
};
7377

74-
auto propagateFlags = [=](NewMIVector &OutMIs) {
78+
const auto propagateFlags = [&]() {
7579
for (auto MIB : OutMIs) {
7680
// Set the NoFPExcept flag when no original matched instruction could
7781
// raise an FP exception, but the new instruction potentially might.
7882
uint16_t MIBFlags = Flags;
7983
if (NoFPException && MIB->mayRaiseFPException())
8084
MIBFlags |= MachineInstr::NoFPExcept;
85+
if (Observer)
86+
Observer->changingInstr(*MIB);
8187
MIB.setMIFlags(MIBFlags);
88+
if (Observer)
89+
Observer->changedInstr(*MIB);
8290
}
8391

8492
return true;
@@ -898,9 +906,13 @@ bool GIMatchTableExecutor::executeMatchTable(
898906
if (NewInsnID >= OutMIs.size())
899907
OutMIs.resize(NewInsnID + 1);
900908

901-
OutMIs[NewInsnID] = MachineInstrBuilder(*State.MIs[OldInsnID]->getMF(),
902-
State.MIs[OldInsnID]);
909+
MachineInstr *OldMI = State.MIs[OldInsnID];
910+
if (Observer)
911+
Observer->changingInstr(*OldMI);
912+
OutMIs[NewInsnID] = MachineInstrBuilder(*OldMI->getMF(), OldMI);
903913
OutMIs[NewInsnID]->setDesc(TII.get(NewOpcode));
914+
if (Observer)
915+
Observer->changedInstr(*OldMI);
904916
DEBUG_WITH_TYPE(TgtExecutor::getName(),
905917
dbgs() << CurrentIdx << ": GIR_MutateOpcode(OutMIs["
906918
<< NewInsnID << "], MIs[" << OldInsnID << "], "
@@ -914,8 +926,7 @@ bool GIMatchTableExecutor::executeMatchTable(
914926
if (NewInsnID >= OutMIs.size())
915927
OutMIs.resize(NewInsnID + 1);
916928

917-
OutMIs[NewInsnID] = BuildMI(*State.MIs[0]->getParent(), State.MIs[0],
918-
MIMetadata(*State.MIs[0]), TII.get(Opcode));
929+
OutMIs[NewInsnID] = Builder.buildInstr(Opcode);
919930
DEBUG_WITH_TYPE(TgtExecutor::getName(),
920931
dbgs() << CurrentIdx << ": GIR_BuildMI(OutMIs["
921932
<< NewInsnID << "], " << Opcode << ")\n");
@@ -1239,6 +1250,10 @@ bool GIMatchTableExecutor::executeMatchTable(
12391250
DEBUG_WITH_TYPE(TgtExecutor::getName(),
12401251
dbgs() << CurrentIdx << ": GIR_EraseFromParent(MIs["
12411252
<< InsnID << "])\n");
1253+
// If we're erasing the insertion point, ensure we don't leave a dangling
1254+
// pointer in the builder.
1255+
if (Builder.getInsertPt() == MI)
1256+
Builder.setInsertPt(*MI->getParent(), ++MI->getIterator());
12421257
if (Observer)
12431258
Observer->erasingInstr(*MI);
12441259
MI->eraseFromParent();
@@ -1309,11 +1324,7 @@ bool GIMatchTableExecutor::executeMatchTable(
13091324
case GIR_Done:
13101325
DEBUG_WITH_TYPE(TgtExecutor::getName(),
13111326
dbgs() << CurrentIdx << ": GIR_Done\n");
1312-
if (Observer) {
1313-
for (MachineInstr *MI : OutMIs)
1314-
Observer->createdInstr(*MI);
1315-
}
1316-
propagateFlags(OutMIs);
1327+
propagateFlags();
13171328
return true;
13181329
default:
13191330
llvm_unreachable("Unexpected command");

llvm/test/TableGen/GlobalISelCombinerEmitter/match-table.td

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,12 @@ def MyCombiner: GICombiner<"GenMyCombiner", [
9393
// CHECK: bool GenMyCombiner::tryCombineAll(MachineInstr &I) const {
9494
// CHECK-NEXT: const TargetSubtargetInfo &ST = MF.getSubtarget();
9595
// CHECK-NEXT: const PredicateBitset AvailableFeatures = getAvailableFeatures();
96-
// CHECK-NEXT: NewMIVector OutMIs;
96+
// CHECK-NEXT: B.setInstrAndDebugLoc(I);
9797
// CHECK-NEXT: State.MIs.clear();
9898
// CHECK-NEXT: State.MIs.push_back(&I);
9999
// CHECK-NEXT: MatchInfos = MatchInfosTy();
100100
// CHECK-EMPTY:
101-
// CHECK-NEXT: if (executeMatchTable(*this, OutMIs, State, ExecInfo, getMatchTable(), *ST.getInstrInfo(), MRI, *MRI.getTargetRegisterInfo(), *ST.getRegBankInfo(), AvailableFeatures, /*CoverageInfo*/ nullptr, &Observer))
101+
// CHECK-NEXT: if (executeMatchTable(*this, State, ExecInfo, B, getMatchTable(), *ST.getInstrInfo(), MRI, *MRI.getTargetRegisterInfo(), *ST.getRegBankInfo(), AvailableFeatures, /*CoverageInfo*/ nullptr))
102102
// CHECK-NEXT: return true;
103103
// CHECK-NEXT: }
104104
// CHECK-EMPTY:

llvm/test/TableGen/GlobalISelEmitter.td

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,11 +216,11 @@ def HasC : Predicate<"Subtarget->hasC()"> { let RecomputePerFunction = 1; }
216216

217217
// CHECK: bool MyTargetInstructionSelector::selectImpl(MachineInstr &I, CodeGenCoverage &CoverageInfo) const {
218218
// CHECK-NEXT: const PredicateBitset AvailableFeatures = getAvailableFeatures();
219-
// CHECK-NEXT: NewMIVector OutMIs;
219+
// CHECK-NEXT: MachineIRBuilder B(I);
220220
// CHECK-NEXT: State.MIs.clear();
221221
// CHECK-NEXT: State.MIs.push_back(&I);
222222

223-
// CHECK: if (executeMatchTable(*this, OutMIs, State, ExecInfo, getMatchTable(), TII, MF->getRegInfo(), TRI, RBI, AvailableFeatures, &CoverageInfo)) {
223+
// CHECK: if (executeMatchTable(*this, State, ExecInfo, B, getMatchTable(), TII, MF->getRegInfo(), TRI, RBI, AvailableFeatures, &CoverageInfo)) {
224224
// CHECK-NEXT: return true;
225225
// CHECK-NEXT: }
226226

llvm/utils/TableGen/GlobalISelCombinerEmitter.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3465,15 +3465,15 @@ void GICombinerEmitter::emitAdditionalImpl(raw_ostream &OS) {
34653465
<< " const TargetSubtargetInfo &ST = MF.getSubtarget();\n"
34663466
<< " const PredicateBitset AvailableFeatures = "
34673467
"getAvailableFeatures();\n"
3468-
<< " NewMIVector OutMIs;\n"
3468+
<< " B.setInstrAndDebugLoc(I);\n"
34693469
<< " State.MIs.clear();\n"
34703470
<< " State.MIs.push_back(&I);\n"
34713471
<< " " << MatchDataInfo::StructName << " = "
34723472
<< MatchDataInfo::StructTypeName << "();\n\n"
3473-
<< " if (executeMatchTable(*this, OutMIs, State, ExecInfo"
3473+
<< " if (executeMatchTable(*this, State, ExecInfo, B"
34743474
<< ", getMatchTable(), *ST.getInstrInfo(), MRI, "
34753475
"*MRI.getTargetRegisterInfo(), *ST.getRegBankInfo(), AvailableFeatures"
3476-
<< ", /*CoverageInfo*/ nullptr, &Observer)) {\n"
3476+
<< ", /*CoverageInfo*/ nullptr)) {\n"
34773477
<< " return true;\n"
34783478
<< " }\n\n"
34793479
<< " return false;\n"

llvm/utils/TableGen/GlobalISelEmitter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2267,10 +2267,10 @@ void GlobalISelEmitter::emitAdditionalImpl(raw_ostream &OS) {
22672267
"&CoverageInfo) const {\n"
22682268
<< " const PredicateBitset AvailableFeatures = "
22692269
"getAvailableFeatures();\n"
2270-
<< " NewMIVector OutMIs;\n"
2270+
<< " MachineIRBuilder B(I);\n"
22712271
<< " State.MIs.clear();\n"
22722272
<< " State.MIs.push_back(&I);\n\n"
2273-
<< " if (executeMatchTable(*this, OutMIs, State, ExecInfo"
2273+
<< " if (executeMatchTable(*this, State, ExecInfo, B"
22742274
<< ", getMatchTable(), TII, MF->getRegInfo(), TRI, RBI, AvailableFeatures"
22752275
<< ", &CoverageInfo)) {\n"
22762276
<< " return true;\n"

0 commit comments

Comments
 (0)