Skip to content

Commit 43d59d3

Browse files
Add unittest; Make SetVector small; Rebase to fix CI
1 parent af7adca commit 43d59d3

File tree

5 files changed

+97
-16
lines changed

5 files changed

+97
-16
lines changed

llvm/include/llvm/CodeGen/GlobalISel/InstructionSelect.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
namespace llvm {
2222

2323
class InstructionSelector;
24+
class GISelKnownBits;
2425
class BlockFrequencyInfo;
2526
class ProfileSummaryInfo;
2627

@@ -54,17 +55,20 @@ class InstructionSelect : public MachineFunctionPass {
5455
InstructionSelect();
5556

5657
bool runOnMachineFunction(MachineFunction &MF) override;
58+
bool selectMachineFunction(MachineFunction &MF);
59+
void setInstructionSelector(InstructionSelector *NewISel) { ISel = NewISel; }
5760

5861
protected:
5962
class MIIteratorMaintainer;
6063

6164
InstructionSelector *ISel = nullptr;
65+
GISelKnownBits *KB = nullptr;
6266
BlockFrequencyInfo *BFI = nullptr;
6367
ProfileSummaryInfo *PSI = nullptr;
6468

6569
CodeGenOptLevel OptLevel = CodeGenOptLevel::None;
6670

67-
bool select(MachineInstr &MI);
71+
bool selectInstr(MachineInstr &MI);
6872
};
6973
} // End namespace llvm.
7074

llvm/include/llvm/CodeGen/GlobalISel/InstructionSelector.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,9 @@ class InstructionSelector : public GIMatchTableExecutor {
3232
/// !isPreISelGenericOpcode(I.getOpcode())
3333
virtual bool select(MachineInstr &I) = 0;
3434

35-
void setTargetPassConfig(const TargetPassConfig *T) { TPC = T; }
36-
37-
void setRemarkEmitter(MachineOptimizationRemarkEmitter *M) { MORE = M; }
38-
39-
protected:
35+
// FIXME: Eliminate dependency on TargetPassConfig for NewPM transition
4036
const TargetPassConfig *TPC = nullptr;
37+
4138
MachineOptimizationRemarkEmitter *MORE = nullptr;
4239
};
4340
} // namespace llvm

llvm/lib/CodeGen/GlobalISel/InstructionSelect.cpp

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,9 @@ INITIALIZE_PASS_END(InstructionSelect, DEBUG_TYPE,
7575
class InstructionSelect::MIIteratorMaintainer
7676
: public MachineFunction::Delegate {
7777
#ifndef NDEBUG
78-
SetVector<const MachineInstr *> CreatedInstrs;
78+
SetVector<const MachineInstr *, SmallVector<const MachineInstr *, 8>,
79+
DenseSet<const MachineInstr *>, 8>
80+
CreatedInstrs;
7981
#endif
8082
public:
8183
MachineBasicBlock::reverse_iterator MII;
@@ -138,31 +140,36 @@ bool InstructionSelect::runOnMachineFunction(MachineFunction &MF) {
138140
MachineFunctionProperties::Property::FailedISel))
139141
return false;
140142

141-
LLVM_DEBUG(dbgs() << "Selecting function: " << MF.getName() << '\n');
142-
143-
const TargetPassConfig &TPC = getAnalysis<TargetPassConfig>();
144143
ISel = MF.getSubtarget().getInstructionSelector();
145-
ISel->setTargetPassConfig(&TPC);
144+
ISel->TPC = &getAnalysis<TargetPassConfig>();
146145

146+
// FIXME: Properly override OptLevel in TargetMachine. See OptLevelChanger
147147
CodeGenOptLevel OldOptLevel = OptLevel;
148148
auto RestoreOptLevel = make_scope_exit([=]() { OptLevel = OldOptLevel; });
149149
OptLevel = MF.getFunction().hasOptNone() ? CodeGenOptLevel::None
150150
: MF.getTarget().getOptLevel();
151151

152-
GISelKnownBits *KB = &getAnalysis<GISelKnownBitsAnalysis>().get(MF);
152+
KB = &getAnalysis<GISelKnownBitsAnalysis>().get(MF);
153153
if (OptLevel != CodeGenOptLevel::None) {
154154
PSI = &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI();
155155
if (PSI && PSI->hasProfileSummary())
156156
BFI = &getAnalysis<LazyBlockFrequencyInfoPass>().getBFI();
157157
}
158158

159-
CodeGenCoverage CoverageInfo;
159+
return selectMachineFunction(MF);
160+
}
161+
162+
bool InstructionSelect::selectMachineFunction(MachineFunction &MF) {
163+
LLVM_DEBUG(dbgs() << "Selecting function: " << MF.getName() << '\n');
160164
assert(ISel && "Cannot work without InstructionSelector");
165+
166+
const TargetPassConfig &TPC = *ISel->TPC;
167+
CodeGenCoverage CoverageInfo;
161168
ISel->setupMF(MF, KB, &CoverageInfo, PSI, BFI);
162169

163170
// An optimization remark emitter. Used to report failures.
164171
MachineOptimizationRemarkEmitter MORE(MF, /*MBFI=*/nullptr);
165-
ISel->setRemarkEmitter(&MORE);
172+
ISel->MORE = &MORE;
166173

167174
// FIXME: There are many other MF/MFI fields we need to initialize.
168175

@@ -206,7 +213,7 @@ bool InstructionSelect::runOnMachineFunction(MachineFunction &MF) {
206213
++MIIMaintainer.MII;
207214

208215
LLVM_DEBUG(dbgs() << "\nSelect: " << MI);
209-
if (!select(MI)) {
216+
if (!selectInstr(MI)) {
210217
LLVM_DEBUG(dbgs() << "Selection failed!\n";
211218
MIIMaintainer.reportFullyCreatedInstrs());
212219
reportGISelFailure(MF, TPC, MORE, "gisel-select", "cannot select",
@@ -341,7 +348,7 @@ bool InstructionSelect::runOnMachineFunction(MachineFunction &MF) {
341348
return true;
342349
}
343350

344-
bool InstructionSelect::select(MachineInstr &MI) {
351+
bool InstructionSelect::selectInstr(MachineInstr &MI) {
345352
MachineRegisterInfo &MRI = ISel->MF->getRegInfo();
346353

347354
// We could have folded this instruction away already, making it dead.

llvm/unittests/CodeGen/GlobalISel/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@ add_llvm_unittest(GlobalISelTests
2828
GISelUtilsTest.cpp
2929
GISelAliasTest.cpp
3030
CallLowering.cpp
31+
InstructionSelectTest.cpp
3132
)
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#include "GISelMITest.h"
2+
#include "llvm/CodeGen/GlobalISel/InstructionSelect.h"
3+
#include "llvm/CodeGen/GlobalISel/InstructionSelector.h"
4+
#include "llvm/CodeGen/TargetPassConfig.h"
5+
#include "llvm/IR/LegacyPassManager.h"
6+
7+
class EraseMockInstructionSelector : public InstructionSelector {
8+
public:
9+
int NumSelected = 0;
10+
SmallVector<MachineInstr *> MIs;
11+
12+
bool select(MachineInstr &MI) override {
13+
++NumSelected;
14+
switch (NumSelected) {
15+
case 1:
16+
EXPECT_EQ(&MI, MIs[8]);
17+
// Erase previous instructions
18+
MIs[7]->eraseFromParent();
19+
MIs[6]->eraseFromParent();
20+
// Don't erase this MI before step 3 to prevent DCE
21+
return true;
22+
case 2:
23+
EXPECT_EQ(&MI, MIs[5]);
24+
// Erase previous instructions reversed
25+
MIs[3]->eraseFromParent();
26+
MIs[4]->eraseFromParent();
27+
MI.eraseFromParent();
28+
return true;
29+
case 3:
30+
EXPECT_EQ(&MI, MIs[2]);
31+
MIs[8]->eraseFromParent();
32+
// Erase first instructions
33+
MIs[0]->eraseFromParent();
34+
MIs[1]->eraseFromParent();
35+
MI.eraseFromParent();
36+
return true;
37+
default:
38+
ADD_FAILURE();
39+
return false;
40+
}
41+
}
42+
43+
void setupGeneratedPerFunctionState(MachineFunction &MF) override {}
44+
};
45+
46+
TEST_F(AArch64GISelMITest, TestInstructionSelectErase) {
47+
StringRef MIRString = R"(
48+
$x0 = COPY %2(s64)
49+
$x0 = COPY %2(s64)
50+
$x0 = COPY %2(s64)
51+
$x0 = COPY %2(s64)
52+
$x0 = COPY %2(s64)
53+
$x0 = COPY %2(s64)
54+
)";
55+
setUp(MIRString);
56+
if (!TM)
57+
GTEST_SKIP();
58+
59+
legacy::PassManager PM;
60+
std::unique_ptr<TargetPassConfig> TPC(TM->createPassConfig(PM));
61+
62+
EraseMockInstructionSelector ISel;
63+
ISel.TPC = TPC.get();
64+
for (auto &MI : *EntryMBB) {
65+
ISel.MIs.push_back(&MI);
66+
}
67+
68+
InstructionSelect ISelPass;
69+
ISelPass.setInstructionSelector(&ISel);
70+
ISelPass.selectMachineFunction(*MF);
71+
EXPECT_EQ(ISel.NumSelected, 3);
72+
}

0 commit comments

Comments
 (0)