1//===-- Assembler.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
10/// Defines classes to assemble functions composed of a single basic block of
11/// MCInsts.
12///
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_TOOLS_LLVM_EXEGESIS_ASSEMBLER_H
16#define LLVM_TOOLS_LLVM_EXEGESIS_ASSEMBLER_H
17
18#include <memory>
19
20#include "BenchmarkCode.h"
21#include "Error.h"
22#include "llvm/ADT/ArrayRef.h"
23#include "llvm/ADT/BitVector.h"
24#include "llvm/CodeGen/MachineFunction.h"
25#include "llvm/CodeGen/MachineModuleInfo.h"
26#include "llvm/ExecutionEngine/Orc/LLJIT.h"
27#include "llvm/IR/LLVMContext.h"
28#include "llvm/IR/Module.h"
29#include "llvm/MC/MCInst.h"
30#include "llvm/Object/Binary.h"
31#include "llvm/Object/ObjectFile.h"
32#include "llvm/Support/Compiler.h"
33#include "llvm/Support/raw_ostream.h"
34#include "llvm/Target/TargetMachine.h"
35
36namespace llvm {
37namespace exegesis {
38
39class ExegesisTarget;
40
41// Gather the set of reserved registers (depends on function's calling
42// convention and target machine).
43BitVector getFunctionReservedRegs(const TargetMachine &TM);
44
45// Helper to fill in a basic block.
46class BasicBlockFiller {
47public:
48 BasicBlockFiller(MachineFunction &MF, MachineBasicBlock *MBB,
49 const MCInstrInfo *MCII);
50
51 void addInstruction(const MCInst &Inst, const DebugLoc &DL = DebugLoc());
52 void addInstructions(ArrayRef<MCInst> Insts, const DebugLoc &DL = DebugLoc());
53
54 void addReturn(const ExegesisTarget &ET, bool SubprocessCleanup,
55 const DebugLoc &DL = DebugLoc());
56
57 MachineFunction &MF;
58 MachineBasicBlock *const MBB;
59 const MCInstrInfo *const MCII;
60};
61
62// Helper to fill in a function.
63class FunctionFiller {
64public:
65 FunctionFiller(MachineFunction &MF, std::vector<MCRegister> RegistersSetUp);
66
67 // Adds a basic block to the function.
68 BasicBlockFiller addBasicBlock();
69
70 // Returns the function entry point.
71 BasicBlockFiller getEntry() { return Entry; }
72
73 MachineFunction &MF;
74 const MCInstrInfo *const MCII;
75
76 // Returns the set of registers in the snippet setup code.
77 ArrayRef<MCRegister> getRegistersSetUp() const;
78
79private:
80 BasicBlockFiller Entry;
81 // The set of registers that are set up in the basic block.
82 std::vector<MCRegister> RegistersSetUp;
83};
84
85// A callback that fills a function.
86using FillFunction = std::function<void(FunctionFiller &)>;
87
88// Creates a temporary `void foo(char*)` function containing the provided
89// Instructions. Runs a set of llvm Passes to provide correct prologue and
90// epilogue. Once the MachineFunction is ready, it is assembled for TM to
91// AsmStream, the temporary function is eventually discarded.
92Error assembleToStream(const ExegesisTarget &ET,
93 std::unique_ptr<TargetMachine> TM,
94 ArrayRef<MCRegister> LiveIns, const FillFunction &Fill,
95 raw_pwrite_stream &AsmStreamm, const BenchmarkKey &Key,
96 bool GenerateMemoryInstructions);
97
98// Creates an ObjectFile in the format understood by the host.
99// Note: the resulting object keeps a copy of Buffer so it can be discarded once
100// this function returns.
101object::OwningBinary<object::ObjectFile> getObjectFromBuffer(StringRef Buffer);
102
103// Loads the content of Filename as on ObjectFile and returns it.
104object::OwningBinary<object::ObjectFile> getObjectFromFile(StringRef Filename);
105
106// Consumes an ObjectFile containing a `void foo(char*)` function and make it
107// executable.
108class ExecutableFunction {
109public:
110 static Expected<ExecutableFunction>
111 create(std::unique_ptr<TargetMachine> TM,
112 object::OwningBinary<object::ObjectFile> &&ObjectFileHolder);
113
114 // Retrieves the function as an array of bytes.
115 StringRef getFunctionBytes() const { return FunctionBytes; }
116
117 // Executes the function.
118 void operator()(char *Memory) const
119 LLVM_NO_SANITIZE("cfi-icall") /* Incompatible with JIT */ {
120 ((void (*)(char *))(uintptr_t)FunctionBytes.data())(Memory);
121 }
122
123 StringRef FunctionBytes;
124
125private:
126 ExecutableFunction(std::unique_ptr<LLVMContext> Ctx,
127 std::unique_ptr<orc::LLJIT> EJIT, StringRef FunctionBytes);
128
129 std::unique_ptr<LLVMContext> Context;
130 std::unique_ptr<orc::LLJIT> ExecJIT;
131};
132
133// Copies benchmark function's bytes from benchmark object.
134Error getBenchmarkFunctionBytes(const StringRef InputData,
135 std::vector<uint8_t> &Bytes);
136
137// Creates a void(int8*) MachineFunction.
138MachineFunction &createVoidVoidPtrMachineFunction(StringRef FunctionID,
139 Module *Module,
140 MachineModuleInfo *MMI);
141
142} // namespace exegesis
143} // namespace llvm
144
145#endif // LLVM_TOOLS_LLVM_EXEGESIS_ASSEMBLER_H
146