1 //===-- Assembler.cpp -------------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
11 #include "SnippetRepetitor.h"
13 #include "llvm/CodeGen/GlobalISel/CallLowering.h"
14 #include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h"
15 #include "llvm/CodeGen/MachineInstrBuilder.h"
16 #include "llvm/CodeGen/MachineModuleInfo.h"
17 #include "llvm/CodeGen/MachineRegisterInfo.h"
18 #include "llvm/CodeGen/TargetInstrInfo.h"
19 #include "llvm/CodeGen/TargetPassConfig.h"
20 #include "llvm/CodeGen/TargetSubtargetInfo.h"
21 #include "llvm/ExecutionEngine/SectionMemoryManager.h"
22 #include "llvm/IR/LegacyPassManager.h"
23 #include "llvm/MC/MCInstrInfo.h"
24 #include "llvm/Support/Alignment.h"
25 #include "llvm/Support/MemoryBuffer.h"
30 static constexpr const char ModuleID
[] = "ExegesisInfoTest";
31 static constexpr const char FunctionID
[] = "foo";
32 static const Align
kFunctionAlignment(4096);
34 // Fills the given basic block with register setup code, and returns true if
35 // all registers could be setup correctly.
36 static bool generateSnippetSetupCode(
37 const ExegesisTarget
&ET
, const MCSubtargetInfo
*const MSI
,
38 ArrayRef
<RegisterValue
> RegisterInitialValues
, BasicBlockFiller
&BBF
) {
39 bool IsSnippetSetupComplete
= true;
40 for (const RegisterValue
&RV
: RegisterInitialValues
) {
41 // Load a constant in the register.
42 const auto SetRegisterCode
= ET
.setRegTo(*MSI
, RV
.Register
, RV
.Value
);
43 if (SetRegisterCode
.empty())
44 IsSnippetSetupComplete
= false;
45 BBF
.addInstructions(SetRegisterCode
);
47 return IsSnippetSetupComplete
;
50 // Small utility function to add named passes.
51 static bool addPass(PassManagerBase
&PM
, StringRef PassName
,
52 TargetPassConfig
&TPC
) {
53 const PassRegistry
*PR
= PassRegistry::getPassRegistry();
54 const PassInfo
*PI
= PR
->getPassInfo(PassName
);
56 errs() << " run-pass " << PassName
<< " is not registered.\n";
60 if (!PI
->getNormalCtor()) {
61 errs() << " cannot create pass: " << PI
->getPassName() << "\n";
64 Pass
*P
= PI
->getNormalCtor()();
65 std::string Banner
= std::string("After ") + std::string(P
->getPassName());
67 TPC
.printAndVerify(Banner
);
72 MachineFunction
&createVoidVoidPtrMachineFunction(StringRef FunctionID
,
74 MachineModuleInfo
*MMI
) {
75 Type
*const ReturnType
= Type::getInt32Ty(Module
->getContext());
76 Type
*const MemParamType
= PointerType::get(
77 Type::getInt8Ty(Module
->getContext()), 0 /*default address space*/);
78 FunctionType
*FunctionType
=
79 FunctionType::get(ReturnType
, {MemParamType
}, false);
80 Function
*const F
= Function::Create(
81 FunctionType
, GlobalValue::InternalLinkage
, FunctionID
, Module
);
82 // Making sure we can create a MachineFunction out of this Function even if it
84 F
->setIsMaterializable(true);
85 return MMI
->getOrCreateMachineFunction(*F
);
88 BasicBlockFiller::BasicBlockFiller(MachineFunction
&MF
, MachineBasicBlock
*MBB
,
89 const MCInstrInfo
*MCII
)
90 : MF(MF
), MBB(MBB
), MCII(MCII
) {}
92 void BasicBlockFiller::addInstruction(const MCInst
&Inst
, const DebugLoc
&DL
) {
93 const unsigned Opcode
= Inst
.getOpcode();
94 const MCInstrDesc
&MCID
= MCII
->get(Opcode
);
95 MachineInstrBuilder Builder
= BuildMI(MBB
, DL
, MCID
);
96 for (unsigned OpIndex
= 0, E
= Inst
.getNumOperands(); OpIndex
< E
;
98 const MCOperand
&Op
= Inst
.getOperand(OpIndex
);
100 const bool IsDef
= OpIndex
< MCID
.getNumDefs();
102 const MCOperandInfo
&OpInfo
= MCID
.operands().begin()[OpIndex
];
103 if (IsDef
&& !OpInfo
.isOptionalDef())
104 Flags
|= RegState::Define
;
105 Builder
.addReg(Op
.getReg(), Flags
);
106 } else if (Op
.isImm()) {
107 Builder
.addImm(Op
.getImm());
108 } else if (!Op
.isValid()) {
109 llvm_unreachable("Operand is not set");
111 llvm_unreachable("Not yet implemented");
116 void BasicBlockFiller::addInstructions(ArrayRef
<MCInst
> Insts
,
117 const DebugLoc
&DL
) {
118 for (const MCInst
&Inst
: Insts
)
119 addInstruction(Inst
, DL
);
122 void BasicBlockFiller::addReturn(const DebugLoc
&DL
) {
123 // Insert the return code.
124 const TargetInstrInfo
*TII
= MF
.getSubtarget().getInstrInfo();
125 if (TII
->getReturnOpcode() < TII
->getNumOpcodes()) {
126 BuildMI(MBB
, DL
, TII
->get(TII
->getReturnOpcode()));
128 MachineIRBuilder
MIB(MF
);
130 MF
.getSubtarget().getCallLowering()->lowerReturn(MIB
, nullptr, {});
134 FunctionFiller::FunctionFiller(MachineFunction
&MF
,
135 std::vector
<unsigned> RegistersSetUp
)
136 : MF(MF
), MCII(MF
.getTarget().getMCInstrInfo()), Entry(addBasicBlock()),
137 RegistersSetUp(std::move(RegistersSetUp
)) {}
139 BasicBlockFiller
FunctionFiller::addBasicBlock() {
140 MachineBasicBlock
*MBB
= MF
.CreateMachineBasicBlock();
142 return BasicBlockFiller(MF
, MBB
, MCII
);
145 ArrayRef
<unsigned> FunctionFiller::getRegistersSetUp() const {
146 return RegistersSetUp
;
149 static std::unique_ptr
<Module
>
150 createModule(const std::unique_ptr
<LLVMContext
> &Context
, const DataLayout DL
) {
151 auto Mod
= std::make_unique
<Module
>(ModuleID
, *Context
);
152 Mod
->setDataLayout(DL
);
156 BitVector
getFunctionReservedRegs(const TargetMachine
&TM
) {
157 std::unique_ptr
<LLVMContext
> Context
= std::make_unique
<LLVMContext
>();
158 std::unique_ptr
<Module
> Module
= createModule(Context
, TM
.createDataLayout());
159 // TODO: This only works for targets implementing LLVMTargetMachine.
160 const LLVMTargetMachine
&LLVMTM
= static_cast<const LLVMTargetMachine
&>(TM
);
161 std::unique_ptr
<MachineModuleInfoWrapperPass
> MMIWP
=
162 std::make_unique
<MachineModuleInfoWrapperPass
>(&LLVMTM
);
163 MachineFunction
&MF
= createVoidVoidPtrMachineFunction(
164 FunctionID
, Module
.get(), &MMIWP
.get()->getMMI());
165 // Saving reserved registers for client.
166 return MF
.getSubtarget().getRegisterInfo()->getReservedRegs(MF
);
169 void assembleToStream(const ExegesisTarget
&ET
,
170 std::unique_ptr
<LLVMTargetMachine
> TM
,
171 ArrayRef
<unsigned> LiveIns
,
172 ArrayRef
<RegisterValue
> RegisterInitialValues
,
173 const FillFunction
&Fill
, raw_pwrite_stream
&AsmStream
) {
174 auto Context
= std::make_unique
<LLVMContext
>();
175 std::unique_ptr
<Module
> Module
=
176 createModule(Context
, TM
->createDataLayout());
177 auto MMIWP
= std::make_unique
<MachineModuleInfoWrapperPass
>(TM
.get());
178 MachineFunction
&MF
= createVoidVoidPtrMachineFunction(
179 FunctionID
, Module
.get(), &MMIWP
.get()->getMMI());
180 MF
.ensureAlignment(kFunctionAlignment
);
182 // We need to instruct the passes that we're done with SSA and virtual
184 auto &Properties
= MF
.getProperties();
185 Properties
.set(MachineFunctionProperties::Property::NoVRegs
);
186 Properties
.reset(MachineFunctionProperties::Property::IsSSA
);
187 Properties
.set(MachineFunctionProperties::Property::NoPHIs
);
189 for (const unsigned Reg
: LiveIns
)
190 MF
.getRegInfo().addLiveIn(Reg
);
192 std::vector
<unsigned> RegistersSetUp
;
193 for (const auto &InitValue
: RegisterInitialValues
) {
194 RegistersSetUp
.push_back(InitValue
.Register
);
196 FunctionFiller
Sink(MF
, std::move(RegistersSetUp
));
197 auto Entry
= Sink
.getEntry();
198 for (const unsigned Reg
: LiveIns
)
199 Entry
.MBB
->addLiveIn(Reg
);
201 const bool IsSnippetSetupComplete
= generateSnippetSetupCode(
202 ET
, TM
->getMCSubtargetInfo(), RegisterInitialValues
, Entry
);
204 // If the snippet setup is not complete, we disable liveliness tracking. This
205 // means that we won't know what values are in the registers.
206 if (!IsSnippetSetupComplete
)
207 Properties
.reset(MachineFunctionProperties::Property::TracksLiveness
);
211 // prologue/epilogue pass needs the reserved registers to be frozen, this
212 // is usually done by the SelectionDAGISel pass.
213 MF
.getRegInfo().freezeReservedRegs(MF
);
215 // We create the pass manager, run the passes to populate AsmBuffer.
216 MCContext
&MCContext
= MMIWP
->getMMI().getContext();
217 legacy::PassManager PM
;
219 TargetLibraryInfoImpl
TLII(Triple(Module
->getTargetTriple()));
220 PM
.add(new TargetLibraryInfoWrapperPass(TLII
));
222 TargetPassConfig
*TPC
= TM
->createPassConfig(PM
);
224 PM
.add(MMIWP
.release());
225 TPC
->printAndVerify("MachineFunctionGenerator::assemble");
226 // Add target-specific passes.
227 ET
.addTargetSpecificPasses(PM
);
228 TPC
->printAndVerify("After ExegesisTarget::addTargetSpecificPasses");
229 // Adding the following passes:
230 // - postrapseudos: expands pseudo return instructions used on some targets.
231 // - machineverifier: checks that the MachineFunction is well formed.
232 // - prologepilog: saves and restore callee saved registers.
233 for (const char *PassName
:
234 {"postrapseudos", "machineverifier", "prologepilog"})
235 if (addPass(PM
, PassName
, *TPC
))
236 report_fatal_error("Unable to add a mandatory pass");
237 TPC
->setInitialized();
239 // AsmPrinter is responsible for generating the assembly into AsmBuffer.
240 if (TM
->addAsmPrinter(PM
, AsmStream
, nullptr, TargetMachine::CGFT_ObjectFile
,
242 report_fatal_error("Cannot add AsmPrinter passes");
244 PM
.run(*Module
); // Run all the passes
247 object::OwningBinary
<object::ObjectFile
>
248 getObjectFromBuffer(StringRef InputData
) {
249 // Storing the generated assembly into a MemoryBuffer that owns the memory.
250 std::unique_ptr
<MemoryBuffer
> Buffer
=
251 MemoryBuffer::getMemBufferCopy(InputData
);
252 // Create the ObjectFile from the MemoryBuffer.
253 std::unique_ptr
<object::ObjectFile
> Obj
=
254 cantFail(object::ObjectFile::createObjectFile(Buffer
->getMemBufferRef()));
255 // Returning both the MemoryBuffer and the ObjectFile.
256 return object::OwningBinary
<object::ObjectFile
>(std::move(Obj
),
260 object::OwningBinary
<object::ObjectFile
> getObjectFromFile(StringRef Filename
) {
261 return cantFail(object::ObjectFile::createObjectFile(Filename
));
266 // Implementation of this class relies on the fact that a single object with a
267 // single function will be loaded into memory.
268 class TrackingSectionMemoryManager
: public SectionMemoryManager
{
270 explicit TrackingSectionMemoryManager(uintptr_t *CodeSize
)
271 : CodeSize(CodeSize
) {}
273 uint8_t *allocateCodeSection(uintptr_t Size
, unsigned Alignment
,
275 StringRef SectionName
) override
{
277 return SectionMemoryManager::allocateCodeSection(Size
, Alignment
, SectionID
,
282 uintptr_t *const CodeSize
= nullptr;
287 ExecutableFunction::ExecutableFunction(
288 std::unique_ptr
<LLVMTargetMachine
> TM
,
289 object::OwningBinary
<object::ObjectFile
> &&ObjectFileHolder
)
290 : Context(std::make_unique
<LLVMContext
>()) {
291 assert(ObjectFileHolder
.getBinary() && "cannot create object file");
292 // Initializing the execution engine.
293 // We need to use the JIT EngineKind to be able to add an object file.
295 uintptr_t CodeSize
= 0;
298 EngineBuilder(createModule(Context
, TM
->createDataLayout()))
300 .setMCPU(TM
->getTargetCPU())
301 .setEngineKind(EngineKind::JIT
)
302 .setMCJITMemoryManager(
303 std::make_unique
<TrackingSectionMemoryManager
>(&CodeSize
))
304 .create(TM
.release()));
306 report_fatal_error(Error
);
307 // Adding the generated object file containing the assembled function.
308 // The ExecutionEngine makes sure the object file is copied into an
310 ExecEngine
->addObjectFile(std::move(ObjectFileHolder
));
311 // Fetching function bytes.
312 const uint64_t FunctionAddress
= ExecEngine
->getFunctionAddress(FunctionID
);
313 assert(isAligned(kFunctionAlignment
, FunctionAddress
) &&
314 "function is not properly aligned");
316 StringRef(reinterpret_cast<const char *>(FunctionAddress
), CodeSize
);
319 } // namespace exegesis