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