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