Skip to content

Commit fae3612

Browse files
committed
[Experimental] Create a BURS-style instruction selector
Experiment to create a BURS-style instruction selector for LLVM. For the algorithm to construct the tables and how to use them, see [Proebsting 1995, BURS Automata Generation](https://dl.acm.org/doi/pdf/10.1145/203095.203098) For the extension of tree parsing to DAGs, see [Ertl 1999; Optimal Code Selection in DAGs](https://dl.acm.org/doi/pdf/10.1145/292540.292562) For the integration of constraints, see [Thier, Ertl, Krall 2018; Fast and Flexible Instruction Selection with Constraints](https://publik.tuwien.ac.at/files/publik_277344.pdf) For the hard-coded output values, see [Fraser, Henry 1991; Hard-coding Bottom-up Code Generation Tables to Save Time and Space](http://tfeng.me/papers/fh91hard.pdf)
1 parent 4af1051 commit fae3612

10 files changed

+2273
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
//== llvm/CodeGen/GlobalISel/BURSInstructionSelect.h -------------*- C++ -*-==//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
/// \file This file describes the interface of the MachineFunctionPass
9+
/// responsible for selecting (possibly generic) machine instructions to
10+
/// target-specific instructions.
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef LLVM_CODEGEN_GLOBALISEL_BURSINSTRUCTIONSELECT_H
14+
#define LLVM_CODEGEN_GLOBALISEL_BURSINSTRUCTIONSELECT_H
15+
16+
#include "llvm/ADT/StringRef.h"
17+
#include "llvm/CodeGen/MachineFunction.h"
18+
#include "llvm/CodeGen/MachineFunctionPass.h"
19+
#include "llvm/Support/CodeGen.h"
20+
21+
namespace llvm {
22+
23+
class BlockFrequencyInfo;
24+
class ProfileSummaryInfo;
25+
26+
/// This pass is responsible for selecting generic machine instructions to
27+
/// target-specific instructions. It relies on the InstructionSelector provided
28+
/// by the target.
29+
/// Selection is done by examining blocks in post-order, and instructions are
30+
/// first labelled in normal order, and then reduced in reverse order.
31+
///
32+
/// \post for all inst in MF: not isPreISelGenericOpcode(inst.opcode)
33+
class BURSInstructionSelect : public MachineFunctionPass {
34+
public:
35+
static char ID;
36+
StringRef getPassName() const override { return "BURSInstructionSelect"; }
37+
38+
void getAnalysisUsage(AnalysisUsage &AU) const override;
39+
40+
MachineFunctionProperties getRequiredProperties() const override {
41+
return MachineFunctionProperties()
42+
.set(MachineFunctionProperties::Property::IsSSA)
43+
.set(MachineFunctionProperties::Property::Legalized)
44+
.set(MachineFunctionProperties::Property::RegBankSelected);
45+
}
46+
47+
MachineFunctionProperties getSetProperties() const override {
48+
return MachineFunctionProperties().set(
49+
MachineFunctionProperties::Property::Selected);
50+
}
51+
52+
BURSInstructionSelect(CodeGenOptLevel OL);
53+
BURSInstructionSelect();
54+
55+
bool runOnMachineFunction(MachineFunction &MF) override;
56+
57+
protected:
58+
BlockFrequencyInfo *BFI = nullptr;
59+
ProfileSummaryInfo *PSI = nullptr;
60+
61+
CodeGenOptLevel OptLevel = CodeGenOptLevel::None;
62+
};
63+
} // End namespace llvm.
64+
65+
#endif
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
//===- llvm/CodeGen/GlobalISel/BURSInstructionSelector.h --------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
/// \file This file declares the API for the BURS instruction selector.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef LLVM_CODEGEN_GLOBALISEL_BURSINSTRUCTIONSELECTOR_H
14+
#define LLVM_CODEGEN_GLOBALISEL_BURSINSTRUCTIONSELECTOR_H
15+
16+
#include <llvm/ADT/DenseMap.h>
17+
#include <cstdint>
18+
19+
namespace llvm {
20+
class BlockFrequencyInfo;
21+
class CodeGenCoverage;
22+
class GISelKnownBits;
23+
class MachineBasicBlock;
24+
class MachineFunction;
25+
class MachineInstr;
26+
class MachineOptimizationRemarkEmitter;
27+
class ProfileSummaryInfo;
28+
class TargetPassConfig;
29+
30+
class BURSInstructionSelector {
31+
// The mapping of MIs to a state.
32+
DenseMap<MachineInstr *, uint32_t> States;
33+
34+
public:
35+
virtual ~BURSInstructionSelector();
36+
37+
// The tablegen-erated functions.
38+
virtual void label(MachineInstr &I) = 0;
39+
virtual bool reduce(MachineInstr &I) = 0;
40+
41+
protected:
42+
/// A lowering phase that runs before any selection attempts.
43+
/// Returns true if the instruction was modified.
44+
virtual bool preISelLower(MachineInstr &I) { return false; }
45+
46+
/// An early selection function that runs before the reduce() call.
47+
/// Returns true if the instruction was selected.
48+
virtual bool earlySelect(MachineInstr &I) { return false; }
49+
50+
/// Select the (possibly generic) instruction \p I to only use target-specific
51+
/// opcodes. It is OK to insert multiple instructions, but they cannot be
52+
/// generic pre-isel instructions.
53+
///
54+
/// \returns whether selection succeeded.
55+
/// \pre I.getParent() && I.getParent()->getParent()
56+
/// \post
57+
/// if returns true:
58+
/// for I in all mutated/inserted instructions:
59+
/// !isPreISelGenericOpcode(I.getOpcode())
60+
virtual bool select(MachineInstr &I) = 0;
61+
62+
public:
63+
void setTargetPassConfig(const TargetPassConfig *T) { TPC = T; }
64+
65+
void setRemarkEmitter(MachineOptimizationRemarkEmitter *M) { MORE = M; }
66+
67+
protected:
68+
CodeGenCoverage *CoverageInfo = nullptr;
69+
GISelKnownBits *KB = nullptr;
70+
MachineFunction *MF = nullptr;
71+
ProfileSummaryInfo *PSI = nullptr;
72+
BlockFrequencyInfo *BFI = nullptr;
73+
// For some predicates, we need to track the current MBB.
74+
MachineBasicBlock *CurMBB = nullptr;
75+
76+
public:
77+
virtual void setupGeneratedPerFunctionState(MachineFunction &MF) = 0;
78+
79+
/// Setup per-MF executor state.
80+
virtual void setupMF(MachineFunction &MF, GISelKnownBits *KB,
81+
CodeGenCoverage *CoverageInfo = nullptr,
82+
ProfileSummaryInfo *PSI = nullptr,
83+
BlockFrequencyInfo *BFI = nullptr) {
84+
this->CoverageInfo = CoverageInfo;
85+
this->KB = KB;
86+
this->MF = &MF;
87+
this->PSI = PSI;
88+
this->BFI = BFI;
89+
this->CurMBB = nullptr;
90+
setupGeneratedPerFunctionState(MF);
91+
}
92+
93+
void setCurMBB(MachineBasicBlock *CurMBB) {
94+
this->CurMBB = CurMBB;
95+
// Clear the recorded states for a new MBB.
96+
// Cannot give a good default, since MBB.size() is in O(N).
97+
States.clear();
98+
}
99+
100+
protected:
101+
const TargetPassConfig *TPC = nullptr;
102+
MachineOptimizationRemarkEmitter *MORE = nullptr;
103+
};
104+
} // namespace llvm
105+
106+
#endif

llvm/include/llvm/CodeGen/TargetSubtargetInfo.h

+5
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
namespace llvm {
2929

3030
class APInt;
31+
class BURSInstructionSelector;
3132
class MachineFunction;
3233
class ScheduleDAGMutation;
3334
class CallLowering;
@@ -115,6 +116,10 @@ class TargetSubtargetInfo : public MCSubtargetInfo {
115116
return nullptr;
116117
}
117118

119+
virtual BURSInstructionSelector *getBURSInstructionSelector() const {
120+
return nullptr;
121+
}
122+
118123
/// Target can subclass this hook to select a different DAG scheduler.
119124
virtual RegisterScheduler::FunctionPassCtor
120125
getDAGScheduler(CodeGenOptLevel) const {

llvm/include/llvm/InitializePasses.h

+1
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ void initializeIndirectBrExpandLegacyPassPass(PassRegistry &);
137137
void initializeInferAddressSpacesPass(PassRegistry &);
138138
void initializeInstSimplifyLegacyPassPass(PassRegistry &);
139139
void initializeInstructionCombiningPassPass(PassRegistry &);
140+
void initializeBURSInstructionSelectPass(PassRegistry&);
140141
void initializeInstructionSelectPass(PassRegistry &);
141142
void initializeInterleavedAccessPass(PassRegistry &);
142143
void initializeInterleavedLoadCombinePass(PassRegistry &);

0 commit comments

Comments
 (0)