Skip to content

Commit bb3f5e1

Browse files
authored
Overhaul the TargetMachine and LLVMTargetMachine Classes (#111234)
Following discussions in #110443, and the following earlier discussions in https://lists.llvm.org/pipermail/llvm-dev/2017-October/117907.html, https://reviews.llvm.org/D38482, https://reviews.llvm.org/D38489, this PR attempts to overhaul the `TargetMachine` and `LLVMTargetMachine` interface classes. More specifically: 1. Makes `TargetMachine` the only class implemented under `TargetMachine.h` in the `Target` library. 2. `TargetMachine` contains target-specific interface functions that relate to IR/CodeGen/MC constructs, whereas before (at least on paper) it was supposed to have only IR/MC constructs. Any Target that doesn't want to use the independent code generator simply does not implement them, and returns either `false` or `nullptr`. 3. Renames `LLVMTargetMachine` to `CodeGenCommonTMImpl`. This renaming aims to make the purpose of `LLVMTargetMachine` clearer. Its interface was moved under the CodeGen library, to further emphasis its usage in Targets that use CodeGen directly. 4. Makes `TargetMachine` the only interface used across LLVM and its projects. With these changes, `CodeGenCommonTMImpl` is simply a set of shared function implementations of `TargetMachine`, and CodeGen users don't need to static cast to `LLVMTargetMachine` every time they need a CodeGen-specific feature of the `TargetMachine`. 5. More importantly, does not change any requirements regarding library linking. cc @arsenm @aeubanks
1 parent 5d16fbc commit bb3f5e1

File tree

122 files changed

+507
-485
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

122 files changed

+507
-485
lines changed

llvm/docs/WritingAnLLVMBackend.rst

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ To make your target actually do something, you need to implement a subclass of
152152
``lib/Target/DummyTargetMachine.cpp``, but any file in the ``lib/Target``
153153
directory will be built and should work. To use LLVM's target independent code
154154
generator, you should do what all current machine backends do: create a
155-
subclass of ``LLVMTargetMachine``. (To create a target from scratch, create a
155+
subclass of ``CodeGenTargetMachineImpl``. (To create a target from scratch, create a
156156
subclass of ``TargetMachine``.)
157157

158158
To get LLVM to actually build and link your target, you need to run ``cmake``
@@ -165,15 +165,15 @@ located in the main ``CMakeLists.txt``.
165165
Target Machine
166166
==============
167167

168-
``LLVMTargetMachine`` is designed as a base class for targets implemented with
169-
the LLVM target-independent code generator. The ``LLVMTargetMachine`` class
168+
``CodeGenTargetMachineImpl`` is designed as a base class for targets implemented with
169+
the LLVM target-independent code generator. The ``CodeGenTargetMachineImpl`` class
170170
should be specialized by a concrete target class that implements the various
171-
virtual methods. ``LLVMTargetMachine`` is defined as a subclass of
172-
``TargetMachine`` in ``include/llvm/Target/TargetMachine.h``. The
173-
``TargetMachine`` class implementation (``TargetMachine.cpp``) also processes
174-
numerous command-line options.
171+
virtual methods. ``CodeGenTargetMachineImpl`` is defined as a subclass of
172+
``TargetMachine`` in ``include/llvm/CodeGen/CodeGenTargetMachineImpl.h``. The
173+
``TargetMachine`` class implementation (``include/llvm/Target/TargetMachine.cpp``)
174+
also processes numerous command-line options.
175175

176-
To create a concrete target-specific subclass of ``LLVMTargetMachine``, start
176+
To create a concrete target-specific subclass of ``CodeGenTargetMachineImpl``, start
177177
by copying an existing ``TargetMachine`` class and header. You should name the
178178
files that you create to reflect your specific target. For instance, for the
179179
SPARC target, name the files ``SparcTargetMachine.h`` and
@@ -197,7 +197,7 @@ simply return a class member.
197197

198198
class Module;
199199

200-
class SparcTargetMachine : public LLVMTargetMachine {
200+
class SparcTargetMachine : public CodeGenTargetMachineImpl {
201201
const DataLayout DataLayout; // Calculates type size & alignment
202202
SparcSubtarget Subtarget;
203203
SparcInstrInfo InstrInfo;
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
//===-- CodeGenTargetMachineImpl.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 describes the CodeGenTargetMachineImpl class, which
10+
/// implements a set of functionality used by \c TargetMachine classes in
11+
/// LLVM that make use of the target-independent code generator.
12+
//===----------------------------------------------------------------------===//
13+
#ifndef LLVM_CODEGEN_CODEGENTARGETMACHINEIMPL_H
14+
#define LLVM_CODEGEN_CODEGENTARGETMACHINEIMPL_H
15+
#include "llvm/Target/TargetMachine.h"
16+
17+
namespace llvm {
18+
19+
/// \brief implements a set of functionality in the \c TargetMachine class
20+
/// for targets that make use of the independent code generator (CodeGen)
21+
/// library. Must not be used directly in code unless to inherit its
22+
/// implementation.
23+
class CodeGenTargetMachineImpl : public TargetMachine {
24+
protected: // Can only create subclasses.
25+
CodeGenTargetMachineImpl(const Target &T, StringRef DataLayoutString,
26+
const Triple &TT, StringRef CPU, StringRef FS,
27+
const TargetOptions &Options, Reloc::Model RM,
28+
CodeModel::Model CM, CodeGenOptLevel OL);
29+
30+
void initAsmInfo();
31+
32+
/// Reset internal state.
33+
virtual void reset() {};
34+
35+
public:
36+
/// Get a TargetTransformInfo implementation for the target.
37+
///
38+
/// The TTI returned uses the common code generator to answer queries about
39+
/// the IR.
40+
TargetTransformInfo getTargetTransformInfo(const Function &F) const override;
41+
42+
/// Create a pass configuration object to be used by addPassToEmitX methods
43+
/// for generating a pipeline of CodeGen passes.
44+
virtual TargetPassConfig *createPassConfig(PassManagerBase &PM) override;
45+
46+
/// Add passes to the specified pass manager to get the specified file
47+
/// emitted. Typically this will involve several steps of code generation.
48+
/// \p MMIWP is an optional parameter that, if set to non-nullptr,
49+
/// will be used to set the MachineModuloInfo for this PM.
50+
bool
51+
addPassesToEmitFile(PassManagerBase &PM, raw_pwrite_stream &Out,
52+
raw_pwrite_stream *DwoOut, CodeGenFileType FileType,
53+
bool DisableVerify = true,
54+
MachineModuleInfoWrapperPass *MMIWP = nullptr) override;
55+
56+
/// Add passes to the specified pass manager to get machine code emitted with
57+
/// the MCJIT. This method returns true if machine code is not supported. It
58+
/// fills the MCContext Ctx pointer which can be used to build custom
59+
/// MCStreamer.
60+
bool addPassesToEmitMC(PassManagerBase &PM, MCContext *&Ctx,
61+
raw_pwrite_stream &Out,
62+
bool DisableVerify = true) override;
63+
64+
/// Adds an AsmPrinter pass to the pipeline that prints assembly or
65+
/// machine code from the MI representation.
66+
bool addAsmPrinter(PassManagerBase &PM, raw_pwrite_stream &Out,
67+
raw_pwrite_stream *DwoOut, CodeGenFileType FileType,
68+
MCContext &Context) override;
69+
70+
Expected<std::unique_ptr<MCStreamer>>
71+
createMCStreamer(raw_pwrite_stream &Out, raw_pwrite_stream *DwoOut,
72+
CodeGenFileType FileType, MCContext &Ctx) override;
73+
};
74+
75+
/// Helper method for getting the code model, returning Default if
76+
/// CM does not have a value. The tiny and kernel models will produce
77+
/// an error, so targets that support them or require more complex codemodel
78+
/// selection logic should implement and call their own getEffectiveCodeModel.
79+
inline CodeModel::Model
80+
getEffectiveCodeModel(std::optional<CodeModel::Model> CM,
81+
CodeModel::Model Default) {
82+
if (CM) {
83+
// By default, targets do not support the tiny and kernel models.
84+
if (*CM == CodeModel::Tiny)
85+
report_fatal_error("Target does not support the tiny CodeModel", false);
86+
if (*CM == CodeModel::Kernel)
87+
report_fatal_error("Target does not support the kernel CodeModel", false);
88+
return *CM;
89+
}
90+
return Default;
91+
}
92+
93+
} // namespace llvm
94+
95+
#endif

llvm/include/llvm/CodeGen/MachineFunction.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class DILocation;
5454
class Function;
5555
class GISelChangeObserver;
5656
class GlobalValue;
57-
class LLVMTargetMachine;
57+
class TargetMachine;
5858
class MachineConstantPool;
5959
class MachineFrameInfo;
6060
class MachineFunction;
@@ -256,7 +256,7 @@ struct LandingPadInfo {
256256

257257
class LLVM_ABI MachineFunction {
258258
Function &F;
259-
const LLVMTargetMachine &Target;
259+
const TargetMachine &Target;
260260
const TargetSubtargetInfo *STI;
261261
MCContext &Ctx;
262262

@@ -633,7 +633,7 @@ class LLVM_ABI MachineFunction {
633633
/// for instructions that have a stack spill fused into them.
634634
const static unsigned int DebugOperandMemNumber;
635635

636-
MachineFunction(Function &F, const LLVMTargetMachine &Target,
636+
MachineFunction(Function &F, const TargetMachine &Target,
637637
const TargetSubtargetInfo &STI, MCContext &Ctx,
638638
unsigned FunctionNum);
639639
MachineFunction(const MachineFunction &) = delete;
@@ -706,7 +706,7 @@ class LLVM_ABI MachineFunction {
706706
void assignBeginEndSections();
707707

708708
/// getTarget - Return the target machine this machine code is compiled with
709-
const LLVMTargetMachine &getTarget() const { return Target; }
709+
const TargetMachine &getTarget() const { return Target; }
710710

711711
/// getSubtarget - Return the subtarget for which this machine code is being
712712
/// compiled.

llvm/include/llvm/CodeGen/MachineFunctionAnalysis.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
namespace llvm {
1919

2020
class MachineFunction;
21-
class LLVMTargetMachine;
21+
class TargetMachine;
2222

2323
/// This analysis create MachineFunction for given Function.
2424
/// To release the MachineFunction, users should invalidate it explicitly.
@@ -28,7 +28,7 @@ class MachineFunctionAnalysis
2828

2929
static AnalysisKey Key;
3030

31-
const LLVMTargetMachine *TM;
31+
const TargetMachine *TM;
3232

3333
public:
3434
class Result {
@@ -41,7 +41,7 @@ class MachineFunctionAnalysis
4141
FunctionAnalysisManager::Invalidator &);
4242
};
4343

44-
MachineFunctionAnalysis(const LLVMTargetMachine *TM) : TM(TM){};
44+
MachineFunctionAnalysis(const TargetMachine *TM) : TM(TM) {};
4545
Result run(Function &F, FunctionAnalysisManager &FAM);
4646
};
4747

llvm/include/llvm/CodeGen/MachineModuleInfo.h

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
namespace llvm {
4444

4545
class Function;
46-
class LLVMTargetMachine;
46+
class TargetMachine;
4747
class MachineFunction;
4848
class Module;
4949

@@ -83,7 +83,7 @@ class MachineModuleInfo {
8383
friend class MachineModuleInfoWrapperPass;
8484
friend class MachineModuleAnalysis;
8585

86-
const LLVMTargetMachine &TM;
86+
const TargetMachine &TM;
8787

8888
/// This is the MCContext used for the entire code generator.
8989
MCContext Context;
@@ -109,10 +109,9 @@ class MachineModuleInfo {
109109
MachineModuleInfo &operator=(MachineModuleInfo &&MMII) = delete;
110110

111111
public:
112-
explicit MachineModuleInfo(const LLVMTargetMachine *TM = nullptr);
112+
explicit MachineModuleInfo(const TargetMachine *TM = nullptr);
113113

114-
explicit MachineModuleInfo(const LLVMTargetMachine *TM,
115-
MCContext *ExtContext);
114+
explicit MachineModuleInfo(const TargetMachine *TM, MCContext *ExtContext);
116115

117116
MachineModuleInfo(MachineModuleInfo &&MMII);
118117

@@ -121,7 +120,7 @@ class MachineModuleInfo {
121120
void initialize();
122121
void finalize();
123122

124-
const LLVMTargetMachine &getTarget() const { return TM; }
123+
const TargetMachine &getTarget() const { return TM; }
125124

126125
const MCContext &getContext() const {
127126
return ExternalContext ? *ExternalContext : Context;
@@ -173,9 +172,9 @@ class MachineModuleInfoWrapperPass : public ImmutablePass {
173172

174173
public:
175174
static char ID; // Pass identification, replacement for typeid
176-
explicit MachineModuleInfoWrapperPass(const LLVMTargetMachine *TM = nullptr);
175+
explicit MachineModuleInfoWrapperPass(const TargetMachine *TM = nullptr);
177176

178-
explicit MachineModuleInfoWrapperPass(const LLVMTargetMachine *TM,
177+
explicit MachineModuleInfoWrapperPass(const TargetMachine *TM,
179178
MCContext *ExtContext);
180179

181180
// Initialization and Finalization

llvm/include/llvm/CodeGen/RegisterUsageInfo.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
namespace llvm {
3030

3131
class Function;
32-
class LLVMTargetMachine;
32+
class TargetMachine;
3333

3434
class PhysicalRegisterUsageInfo : public ImmutablePass {
3535
public:
@@ -41,7 +41,7 @@ class PhysicalRegisterUsageInfo : public ImmutablePass {
4141
}
4242

4343
/// Set TargetMachine which is used to print analysis.
44-
void setTargetMachine(const LLVMTargetMachine &TM);
44+
void setTargetMachine(const TargetMachine &TM);
4545

4646
bool doInitialization(Module &M) override;
4747

@@ -63,7 +63,7 @@ class PhysicalRegisterUsageInfo : public ImmutablePass {
6363
/// and 1 means content of register will be preserved around function call.
6464
DenseMap<const Function *, std::vector<uint32_t>> RegMasks;
6565

66-
const LLVMTargetMachine *TM = nullptr;
66+
const TargetMachine *TM = nullptr;
6767
};
6868

6969
} // end namespace llvm

llvm/include/llvm/CodeGen/ScheduleDAG.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ namespace llvm {
3232

3333
template <class GraphType> struct GraphTraits;
3434
template<class Graph> class GraphWriter;
35-
class LLVMTargetMachine;
35+
class TargetMachine;
3636
class MachineFunction;
3737
class MachineRegisterInfo;
3838
class MCInstrDesc;
@@ -571,7 +571,7 @@ class TargetRegisterInfo;
571571

572572
class ScheduleDAG {
573573
public:
574-
const LLVMTargetMachine &TM; ///< Target processor
574+
const TargetMachine &TM; ///< Target processor
575575
const TargetInstrInfo *TII; ///< Target instruction information
576576
const TargetRegisterInfo *TRI; ///< Target processor register info
577577
MachineFunction &MF; ///< Machine function

llvm/include/llvm/CodeGen/TargetPassConfig.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
namespace llvm {
2323

24-
class LLVMTargetMachine;
24+
class TargetMachine;
2525
struct MachineSchedContext;
2626
class PassConfigImpl;
2727
class ScheduleDAGInstrs;
@@ -120,7 +120,7 @@ class TargetPassConfig : public ImmutablePass {
120120
void setStartStopPasses();
121121

122122
protected:
123-
LLVMTargetMachine *TM;
123+
TargetMachine *TM;
124124
PassConfigImpl *Impl = nullptr; // Internal data structures
125125
bool Initialized = false; // Flagged after all passes are configured.
126126

@@ -148,7 +148,7 @@ class TargetPassConfig : public ImmutablePass {
148148
bool addCoreISelPasses();
149149

150150
public:
151-
TargetPassConfig(LLVMTargetMachine &TM, PassManagerBase &pm);
151+
TargetPassConfig(TargetMachine &TM, PassManagerBase &PM);
152152
// Dummy constructor.
153153
TargetPassConfig();
154154

@@ -413,7 +413,7 @@ class TargetPassConfig : public ImmutablePass {
413413
virtual void addFastRegAlloc();
414414

415415
/// addOptimizedRegAlloc - Add passes related to register allocation.
416-
/// LLVMTargetMachine provides standard regalloc passes for most targets.
416+
/// CodeGenCommonTMImpl provides standard regalloc passes for most targets.
417417
virtual void addOptimizedRegAlloc();
418418

419419
/// addPreRewrite - Add passes to the optimized register allocation pipeline
@@ -497,7 +497,7 @@ class TargetPassConfig : public ImmutablePass {
497497
};
498498

499499
void registerCodeGenCallback(PassInstrumentationCallbacks &PIC,
500-
LLVMTargetMachine &);
500+
TargetMachine &);
501501

502502
} // end namespace llvm
503503

llvm/include/llvm/Passes/CodeGenPassBuilder.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ template <typename DerivedT, typename TargetMachineT> class CodeGenPassBuilder {
443443
Error addFastRegAlloc(AddMachinePass &) const;
444444

445445
/// addOptimizedRegAlloc - Add passes related to register allocation.
446-
/// LLVMTargetMachine provides standard regalloc passes for most targets.
446+
/// CodeGenCommonTMImpl provides standard regalloc passes for most targets.
447447
void addOptimizedRegAlloc(AddMachinePass &) const;
448448

449449
/// Add passes that optimize machine instructions after register allocation.

0 commit comments

Comments
 (0)