• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- Assembler.cpp -------------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "Assembler.h"
11 
12 #include "Target.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/MemoryBuffer.h"
25 
26 namespace exegesis {
27 
28 static constexpr const char ModuleID[] = "ExegesisInfoTest";
29 static constexpr const char FunctionID[] = "foo";
30 
31 static std::vector<llvm::MCInst>
generateSnippetSetupCode(const llvm::ArrayRef<unsigned> RegsToDef,const ExegesisTarget & ET,const llvm::LLVMTargetMachine & TM,bool & IsComplete)32 generateSnippetSetupCode(const llvm::ArrayRef<unsigned> RegsToDef,
33                          const ExegesisTarget &ET,
34                          const llvm::LLVMTargetMachine &TM, bool &IsComplete) {
35   IsComplete = true;
36   std::vector<llvm::MCInst> Result;
37   for (const unsigned Reg : RegsToDef) {
38     // Load a constant in the register.
39     const auto Code = ET.setRegToConstant(*TM.getMCSubtargetInfo(), Reg);
40     if (Code.empty())
41       IsComplete = false;
42     Result.insert(Result.end(), Code.begin(), Code.end());
43   }
44   return Result;
45 }
46 
47 // Small utility function to add named passes.
addPass(llvm::PassManagerBase & PM,llvm::StringRef PassName,llvm::TargetPassConfig & TPC)48 static bool addPass(llvm::PassManagerBase &PM, llvm::StringRef PassName,
49                     llvm::TargetPassConfig &TPC) {
50   const llvm::PassRegistry *PR = llvm::PassRegistry::getPassRegistry();
51   const llvm::PassInfo *PI = PR->getPassInfo(PassName);
52   if (!PI) {
53     llvm::errs() << " run-pass " << PassName << " is not registered.\n";
54     return true;
55   }
56 
57   if (!PI->getNormalCtor()) {
58     llvm::errs() << " cannot create pass: " << PI->getPassName() << "\n";
59     return true;
60   }
61   llvm::Pass *P = PI->getNormalCtor()();
62   std::string Banner = std::string("After ") + std::string(P->getPassName());
63   PM.add(P);
64   TPC.printAndVerify(Banner);
65 
66   return false;
67 }
68 
69 // Creates a void MachineFunction with no argument.
70 static llvm::MachineFunction &
createVoidVoidMachineFunction(llvm::StringRef FunctionID,llvm::Module * Module,llvm::MachineModuleInfo * MMI)71 createVoidVoidMachineFunction(llvm::StringRef FunctionID, llvm::Module *Module,
72                               llvm::MachineModuleInfo *MMI) {
73   llvm::Type *const ReturnType = llvm::Type::getInt32Ty(Module->getContext());
74   llvm::FunctionType *FunctionType = llvm::FunctionType::get(ReturnType, false);
75   llvm::Function *const F = llvm::Function::Create(
76       FunctionType, llvm::GlobalValue::InternalLinkage, FunctionID, Module);
77   // Making sure we can create a MachineFunction out of this Function even if it
78   // contains no IR.
79   F->setIsMaterializable(true);
80   return MMI->getOrCreateMachineFunction(*F);
81 }
82 
fillMachineFunction(llvm::MachineFunction & MF,llvm::ArrayRef<llvm::MCInst> Instructions)83 static void fillMachineFunction(llvm::MachineFunction &MF,
84                                 llvm::ArrayRef<llvm::MCInst> Instructions) {
85   llvm::MachineBasicBlock *MBB = MF.CreateMachineBasicBlock();
86   MF.push_back(MBB);
87   const llvm::MCInstrInfo *MCII = MF.getTarget().getMCInstrInfo();
88   llvm::DebugLoc DL;
89   for (const llvm::MCInst &Inst : Instructions) {
90     const unsigned Opcode = Inst.getOpcode();
91     const llvm::MCInstrDesc &MCID = MCII->get(Opcode);
92     llvm::MachineInstrBuilder Builder = llvm::BuildMI(MBB, DL, MCID);
93     for (unsigned OpIndex = 0, E = Inst.getNumOperands(); OpIndex < E;
94          ++OpIndex) {
95       const llvm::MCOperand &Op = Inst.getOperand(OpIndex);
96       if (Op.isReg()) {
97         const bool IsDef = OpIndex < MCID.getNumDefs();
98         unsigned Flags = 0;
99         const llvm::MCOperandInfo &OpInfo = MCID.operands().begin()[OpIndex];
100         if (IsDef && !OpInfo.isOptionalDef())
101           Flags |= llvm::RegState::Define;
102         Builder.addReg(Op.getReg(), Flags);
103       } else if (Op.isImm()) {
104         Builder.addImm(Op.getImm());
105       } else {
106         llvm_unreachable("Not yet implemented");
107       }
108     }
109   }
110   // Insert the return code.
111   const llvm::TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
112   if (TII->getReturnOpcode() < TII->getNumOpcodes()) {
113     llvm::BuildMI(MBB, DL, TII->get(TII->getReturnOpcode()));
114   } else {
115     llvm::MachineIRBuilder MIB(MF);
116     MIB.setMBB(*MBB);
117     MF.getSubtarget().getCallLowering()->lowerReturn(MIB, nullptr, 0);
118   }
119 }
120 
121 static std::unique_ptr<llvm::Module>
createModule(const std::unique_ptr<llvm::LLVMContext> & Context,const llvm::DataLayout DL)122 createModule(const std::unique_ptr<llvm::LLVMContext> &Context,
123              const llvm::DataLayout DL) {
124   auto Module = llvm::make_unique<llvm::Module>(ModuleID, *Context);
125   Module->setDataLayout(DL);
126   return Module;
127 }
128 
getFunctionReservedRegs(const llvm::TargetMachine & TM)129 llvm::BitVector getFunctionReservedRegs(const llvm::TargetMachine &TM) {
130   std::unique_ptr<llvm::LLVMContext> Context =
131       llvm::make_unique<llvm::LLVMContext>();
132   std::unique_ptr<llvm::Module> Module =
133       createModule(Context, TM.createDataLayout());
134   std::unique_ptr<llvm::MachineModuleInfo> MMI =
135       llvm::make_unique<llvm::MachineModuleInfo>(&TM);
136   llvm::MachineFunction &MF =
137       createVoidVoidMachineFunction(FunctionID, Module.get(), MMI.get());
138   // Saving reserved registers for client.
139   return MF.getSubtarget().getRegisterInfo()->getReservedRegs(MF);
140 }
141 
assembleToStream(const ExegesisTarget & ET,std::unique_ptr<llvm::LLVMTargetMachine> TM,llvm::ArrayRef<unsigned> RegsToDef,llvm::ArrayRef<llvm::MCInst> Instructions,llvm::raw_pwrite_stream & AsmStream)142 void assembleToStream(const ExegesisTarget &ET,
143                       std::unique_ptr<llvm::LLVMTargetMachine> TM,
144                       llvm::ArrayRef<unsigned> RegsToDef,
145                       llvm::ArrayRef<llvm::MCInst> Instructions,
146                       llvm::raw_pwrite_stream &AsmStream) {
147   std::unique_ptr<llvm::LLVMContext> Context =
148       llvm::make_unique<llvm::LLVMContext>();
149   std::unique_ptr<llvm::Module> Module =
150       createModule(Context, TM->createDataLayout());
151   std::unique_ptr<llvm::MachineModuleInfo> MMI =
152       llvm::make_unique<llvm::MachineModuleInfo>(TM.get());
153   llvm::MachineFunction &MF =
154       createVoidVoidMachineFunction(FunctionID, Module.get(), MMI.get());
155 
156   // We need to instruct the passes that we're done with SSA and virtual
157   // registers.
158   auto &Properties = MF.getProperties();
159   Properties.set(llvm::MachineFunctionProperties::Property::NoVRegs);
160   Properties.reset(llvm::MachineFunctionProperties::Property::IsSSA);
161   bool IsSnippetSetupComplete = false;
162   std::vector<llvm::MCInst> SnippetWithSetup =
163       generateSnippetSetupCode(RegsToDef, ET, *TM, IsSnippetSetupComplete);
164   if (!SnippetWithSetup.empty()) {
165     SnippetWithSetup.insert(SnippetWithSetup.end(), Instructions.begin(),
166                             Instructions.end());
167     Instructions = SnippetWithSetup;
168   }
169   // If the snippet setup is not complete, we disable liveliness tracking. This
170   // means that we won't know what values are in the registers.
171   if (!IsSnippetSetupComplete)
172     Properties.reset(llvm::MachineFunctionProperties::Property::TracksLiveness);
173 
174   // prologue/epilogue pass needs the reserved registers to be frozen, this
175   // is usually done by the SelectionDAGISel pass.
176   MF.getRegInfo().freezeReservedRegs(MF);
177 
178   // Fill the MachineFunction from the instructions.
179   fillMachineFunction(MF, Instructions);
180 
181   // We create the pass manager, run the passes to populate AsmBuffer.
182   llvm::MCContext &MCContext = MMI->getContext();
183   llvm::legacy::PassManager PM;
184 
185   llvm::TargetLibraryInfoImpl TLII(llvm::Triple(Module->getTargetTriple()));
186   PM.add(new llvm::TargetLibraryInfoWrapperPass(TLII));
187 
188   llvm::TargetPassConfig *TPC = TM->createPassConfig(PM);
189   PM.add(TPC);
190   PM.add(MMI.release());
191   TPC->printAndVerify("MachineFunctionGenerator::assemble");
192   // Add target-specific passes.
193   ET.addTargetSpecificPasses(PM);
194   TPC->printAndVerify("After ExegesisTarget::addTargetSpecificPasses");
195   // Adding the following passes:
196   // - machineverifier: checks that the MachineFunction is well formed.
197   // - prologepilog: saves and restore callee saved registers.
198   for (const char *PassName : {"machineverifier", "prologepilog"})
199     if (addPass(PM, PassName, *TPC))
200       llvm::report_fatal_error("Unable to add a mandatory pass");
201   TPC->setInitialized();
202 
203   // AsmPrinter is responsible for generating the assembly into AsmBuffer.
204   if (TM->addAsmPrinter(PM, AsmStream, nullptr,
205                         llvm::TargetMachine::CGFT_ObjectFile, MCContext))
206     llvm::report_fatal_error("Cannot add AsmPrinter passes");
207 
208   PM.run(*Module); // Run all the passes
209 }
210 
211 llvm::object::OwningBinary<llvm::object::ObjectFile>
getObjectFromBuffer(llvm::StringRef InputData)212 getObjectFromBuffer(llvm::StringRef InputData) {
213   // Storing the generated assembly into a MemoryBuffer that owns the memory.
214   std::unique_ptr<llvm::MemoryBuffer> Buffer =
215       llvm::MemoryBuffer::getMemBufferCopy(InputData);
216   // Create the ObjectFile from the MemoryBuffer.
217   std::unique_ptr<llvm::object::ObjectFile> Obj = llvm::cantFail(
218       llvm::object::ObjectFile::createObjectFile(Buffer->getMemBufferRef()));
219   // Returning both the MemoryBuffer and the ObjectFile.
220   return llvm::object::OwningBinary<llvm::object::ObjectFile>(
221       std::move(Obj), std::move(Buffer));
222 }
223 
224 llvm::object::OwningBinary<llvm::object::ObjectFile>
getObjectFromFile(llvm::StringRef Filename)225 getObjectFromFile(llvm::StringRef Filename) {
226   return llvm::cantFail(llvm::object::ObjectFile::createObjectFile(Filename));
227 }
228 
229 namespace {
230 
231 // Implementation of this class relies on the fact that a single object with a
232 // single function will be loaded into memory.
233 class TrackingSectionMemoryManager : public llvm::SectionMemoryManager {
234 public:
TrackingSectionMemoryManager(uintptr_t * CodeSize)235   explicit TrackingSectionMemoryManager(uintptr_t *CodeSize)
236       : CodeSize(CodeSize) {}
237 
allocateCodeSection(uintptr_t Size,unsigned Alignment,unsigned SectionID,llvm::StringRef SectionName)238   uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
239                                unsigned SectionID,
240                                llvm::StringRef SectionName) override {
241     *CodeSize = Size;
242     return llvm::SectionMemoryManager::allocateCodeSection(
243         Size, Alignment, SectionID, SectionName);
244   }
245 
246 private:
247   uintptr_t *const CodeSize = nullptr;
248 };
249 
250 } // namespace
251 
ExecutableFunction(std::unique_ptr<llvm::LLVMTargetMachine> TM,llvm::object::OwningBinary<llvm::object::ObjectFile> && ObjectFileHolder)252 ExecutableFunction::ExecutableFunction(
253     std::unique_ptr<llvm::LLVMTargetMachine> TM,
254     llvm::object::OwningBinary<llvm::object::ObjectFile> &&ObjectFileHolder)
255     : Context(llvm::make_unique<llvm::LLVMContext>()) {
256   assert(ObjectFileHolder.getBinary() && "cannot create object file");
257   // Initializing the execution engine.
258   // We need to use the JIT EngineKind to be able to add an object file.
259   LLVMLinkInMCJIT();
260   uintptr_t CodeSize = 0;
261   std::string Error;
262   ExecEngine.reset(
263       llvm::EngineBuilder(createModule(Context, TM->createDataLayout()))
264           .setErrorStr(&Error)
265           .setMCPU(TM->getTargetCPU())
266           .setEngineKind(llvm::EngineKind::JIT)
267           .setMCJITMemoryManager(
268               llvm::make_unique<TrackingSectionMemoryManager>(&CodeSize))
269           .create(TM.release()));
270   if (!ExecEngine)
271     llvm::report_fatal_error(Error);
272   // Adding the generated object file containing the assembled function.
273   // The ExecutionEngine makes sure the object file is copied into an
274   // executable page.
275   ExecEngine->addObjectFile(std::move(ObjectFileHolder));
276   // Fetching function bytes.
277   FunctionBytes =
278       llvm::StringRef(reinterpret_cast<const char *>(
279                           ExecEngine->getFunctionAddress(FunctionID)),
280                       CodeSize);
281 }
282 
283 } // namespace exegesis
284