LLVM  12.0.0git
IndirectionUtils.cpp
Go to the documentation of this file.
1 //===---- IndirectionUtils.cpp - Utilities for call indirection in Orc ----===//
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 
10 #include "llvm/ADT/STLExtras.h"
11 #include "llvm/ADT/Triple.h"
13 #include "llvm/IR/IRBuilder.h"
14 #include "llvm/Support/Format.h"
16 #include <sstream>
17 
18 using namespace llvm;
19 using namespace llvm::orc;
20 
21 namespace {
22 
23 class CompileCallbackMaterializationUnit : public orc::MaterializationUnit {
24 public:
25  using CompileFunction = JITCompileCallbackManager::CompileFunction;
26 
27  CompileCallbackMaterializationUnit(SymbolStringPtr Name,
28  CompileFunction Compile)
30  nullptr),
31  Name(std::move(Name)), Compile(std::move(Compile)) {}
32 
33  StringRef getName() const override { return "<Compile Callbacks>"; }
34 
35 private:
36  void materialize(std::unique_ptr<MaterializationResponsibility> R) override {
39  // No dependencies, so these calls cannot fail.
40  cantFail(R->notifyResolved(Result));
41  cantFail(R->notifyEmitted());
42  }
43 
44  void discard(const JITDylib &JD, const SymbolStringPtr &Name) override {
45  llvm_unreachable("Discard should never occur on a LMU?");
46  }
47 
49  CompileFunction Compile;
50 };
51 
52 } // namespace
53 
54 namespace llvm {
55 namespace orc {
56 
58 void IndirectStubsManager::anchor() {}
59 
62  if (auto TrampolineAddr = TP->getTrampoline()) {
63  auto CallbackName =
64  ES.intern(std::string("cc") + std::to_string(++NextCallbackId));
65 
66  std::lock_guard<std::mutex> Lock(CCMgrMutex);
67  AddrToSymbol[*TrampolineAddr] = CallbackName;
68  cantFail(
69  CallbacksJD.define(std::make_unique<CompileCallbackMaterializationUnit>(
70  std::move(CallbackName), std::move(Compile))));
71  return *TrampolineAddr;
72  } else
73  return TrampolineAddr.takeError();
74 }
75 
77  JITTargetAddress TrampolineAddr) {
79 
80  {
81  std::unique_lock<std::mutex> Lock(CCMgrMutex);
82  auto I = AddrToSymbol.find(TrampolineAddr);
83 
84  // If this address is not associated with a compile callback then report an
85  // error to the execution session and return ErrorHandlerAddress to the
86  // callee.
87  if (I == AddrToSymbol.end()) {
88  Lock.unlock();
89  std::string ErrMsg;
90  {
91  raw_string_ostream ErrMsgStream(ErrMsg);
92  ErrMsgStream << "No compile callback for trampoline at "
93  << format("0x%016" PRIx64, TrampolineAddr);
94  }
95  ES.reportError(
96  make_error<StringError>(std::move(ErrMsg), inconvertibleErrorCode()));
97  return ErrorHandlerAddress;
98  } else
99  Name = I->second;
100  }
101 
102  if (auto Sym =
103  ES.lookup(makeJITDylibSearchOrder(
105  Name))
106  return Sym->getAddress();
107  else {
108  llvm::dbgs() << "Didn't find callback.\n";
109  // If anything goes wrong materializing Sym then report it to the session
110  // and return the ErrorHandlerAddress;
111  ES.reportError(Sym.takeError());
112  return ErrorHandlerAddress;
113  }
114 }
115 
118  JITTargetAddress ErrorHandlerAddress) {
119  switch (T.getArch()) {
120  default:
121  return make_error<StringError>(
122  std::string("No callback manager available for ") + T.str(),
124  case Triple::aarch64:
125  case Triple::aarch64_32: {
127  return CCMgrT::Create(ES, ErrorHandlerAddress);
128  }
129 
130  case Triple::x86: {
132  return CCMgrT::Create(ES, ErrorHandlerAddress);
133  }
134 
135  case Triple::mips: {
137  return CCMgrT::Create(ES, ErrorHandlerAddress);
138  }
139  case Triple::mipsel: {
141  return CCMgrT::Create(ES, ErrorHandlerAddress);
142  }
143 
144  case Triple::mips64:
145  case Triple::mips64el: {
147  return CCMgrT::Create(ES, ErrorHandlerAddress);
148  }
149 
150  case Triple::x86_64: {
151  if (T.getOS() == Triple::OSType::Win32) {
153  return CCMgrT::Create(ES, ErrorHandlerAddress);
154  } else {
156  return CCMgrT::Create(ES, ErrorHandlerAddress);
157  }
158  }
159 
160  }
161 }
162 
163 std::function<std::unique_ptr<IndirectStubsManager>()>
165  switch (T.getArch()) {
166  default:
167  return [](){
168  return std::make_unique<
170  };
171 
172  case Triple::aarch64:
173  case Triple::aarch64_32:
174  return [](){
175  return std::make_unique<
177  };
178 
179  case Triple::x86:
180  return [](){
181  return std::make_unique<
183  };
184 
185  case Triple::mips:
186  return [](){
187  return std::make_unique<
189  };
190 
191  case Triple::mipsel:
192  return [](){
193  return std::make_unique<
195  };
196 
197  case Triple::mips64:
198  case Triple::mips64el:
199  return [](){
200  return std::make_unique<
202  };
203 
204  case Triple::x86_64:
205  if (T.getOS() == Triple::OSType::Win32) {
206  return [](){
207  return std::make_unique<
209  };
210  } else {
211  return [](){
212  return std::make_unique<
214  };
215  }
216 
217  }
218 }
219 
221  Constant *AddrIntVal =
223  Constant *AddrPtrVal =
224  ConstantExpr::getCast(Instruction::IntToPtr, AddrIntVal,
225  PointerType::get(&FT, 0));
226  return AddrPtrVal;
227 }
228 
230  const Twine &Name, Constant *Initializer) {
231  auto IP = new GlobalVariable(M, &PT, false, GlobalValue::ExternalLinkage,
232  Initializer, Name, nullptr,
233  GlobalValue::NotThreadLocal, 0, true);
234  IP->setVisibility(GlobalValue::HiddenVisibility);
235  return IP;
236 }
237 
238 void makeStub(Function &F, Value &ImplPointer) {
239  assert(F.isDeclaration() && "Can't turn a definition into a stub.");
240  assert(F.getParent() && "Function isn't in a module.");
241  Module &M = *F.getParent();
242  BasicBlock *EntryBlock = BasicBlock::Create(M.getContext(), "entry", &F);
243  IRBuilder<> Builder(EntryBlock);
244  LoadInst *ImplAddr = Builder.CreateLoad(F.getType(), &ImplPointer);
245  std::vector<Value*> CallArgs;
246  for (auto &A : F.args())
247  CallArgs.push_back(&A);
248  CallInst *Call = Builder.CreateCall(F.getFunctionType(), ImplAddr, CallArgs);
249  Call->setTailCall();
250  Call->setAttributes(F.getAttributes());
251  if (F.getReturnType()->isVoidTy())
252  Builder.CreateRetVoid();
253  else
254  Builder.CreateRet(Call);
255 }
256 
257 std::vector<GlobalValue *> SymbolLinkagePromoter::operator()(Module &M) {
258  std::vector<GlobalValue *> PromotedGlobals;
259 
260  for (auto &GV : M.global_values()) {
261  bool Promoted = true;
262 
263  // Rename if necessary.
264  if (!GV.hasName())
265  GV.setName("__orc_anon." + Twine(NextId++));
266  else if (GV.getName().startswith("\01L"))
267  GV.setName("__" + GV.getName().substr(1) + "." + Twine(NextId++));
268  else if (GV.hasLocalLinkage())
269  GV.setName("__orc_lcl." + GV.getName() + "." + Twine(NextId++));
270  else
271  Promoted = false;
272 
273  if (GV.hasLocalLinkage()) {
274  GV.setLinkage(GlobalValue::ExternalLinkage);
275  GV.setVisibility(GlobalValue::HiddenVisibility);
276  Promoted = true;
277  }
278  GV.setUnnamedAddr(GlobalValue::UnnamedAddr::None);
279 
280  if (Promoted)
281  PromotedGlobals.push_back(&GV);
282  }
283 
284  return PromotedGlobals;
285 }
286 
288  ValueToValueMapTy *VMap) {
289  Function *NewF =
290  Function::Create(cast<FunctionType>(F.getValueType()),
291  F.getLinkage(), F.getName(), &Dst);
292  NewF->copyAttributesFrom(&F);
293 
294  if (VMap) {
295  (*VMap)[&F] = NewF;
296  auto NewArgI = NewF->arg_begin();
297  for (auto ArgI = F.arg_begin(), ArgE = F.arg_end(); ArgI != ArgE;
298  ++ArgI, ++NewArgI)
299  (*VMap)[&*ArgI] = &*NewArgI;
300  }
301 
302  return NewF;
303 }
304 
306  ValueMaterializer *Materializer,
307  Function *NewF) {
308  assert(!OrigF.isDeclaration() && "Nothing to move");
309  if (!NewF)
310  NewF = cast<Function>(VMap[&OrigF]);
311  else
312  assert(VMap[&OrigF] == NewF && "Incorrect function mapping in VMap.");
313  assert(NewF && "Function mapping missing from VMap.");
314  assert(NewF->getParent() != OrigF.getParent() &&
315  "moveFunctionBody should only be used to move bodies between "
316  "modules.");
317 
318  SmallVector<ReturnInst *, 8> Returns; // Ignore returns cloned.
319  CloneFunctionInto(NewF, &OrigF, VMap, /*ModuleLevelChanges=*/true, Returns,
320  "", nullptr, nullptr, Materializer);
321  OrigF.deleteBody();
322 }
323 
325  ValueToValueMapTy *VMap) {
326  GlobalVariable *NewGV = new GlobalVariable(
327  Dst, GV.getValueType(), GV.isConstant(),
328  GV.getLinkage(), nullptr, GV.getName(), nullptr,
330  NewGV->copyAttributesFrom(&GV);
331  if (VMap)
332  (*VMap)[&GV] = NewGV;
333  return NewGV;
334 }
335 
337  ValueToValueMapTy &VMap,
338  ValueMaterializer *Materializer,
339  GlobalVariable *NewGV) {
340  assert(OrigGV.hasInitializer() && "Nothing to move");
341  if (!NewGV)
342  NewGV = cast<GlobalVariable>(VMap[&OrigGV]);
343  else
344  assert(VMap[&OrigGV] == NewGV &&
345  "Incorrect global variable mapping in VMap.");
346  assert(NewGV->getParent() != OrigGV.getParent() &&
347  "moveGlobalVariableInitializer should only be used to move "
348  "initializers between modules");
349 
350  NewGV->setInitializer(MapValue(OrigGV.getInitializer(), VMap, RF_None,
351  nullptr, Materializer));
352 }
353 
355  ValueToValueMapTy &VMap) {
356  assert(OrigA.getAliasee() && "Original alias doesn't have an aliasee?");
357  auto *NewA = GlobalAlias::create(OrigA.getValueType(),
358  OrigA.getType()->getPointerAddressSpace(),
359  OrigA.getLinkage(), OrigA.getName(), &Dst);
360  NewA->copyAttributesFrom(&OrigA);
361  VMap[&OrigA] = NewA;
362  return NewA;
363 }
364 
365 void cloneModuleFlagsMetadata(Module &Dst, const Module &Src,
366  ValueToValueMapTy &VMap) {
367  auto *MFs = Src.getModuleFlagsMetadata();
368  if (!MFs)
369  return;
370  for (auto *MF : MFs->operands())
371  Dst.addModuleFlag(MapMetadata(MF, VMap));
372 }
373 
374 } // End namespace orc.
375 } // End namespace llvm.
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
Definition: Error.h:708
ThreadLocalMode getThreadLocalMode() const
Definition: GlobalValue.h:252
IndirectStubsManager implementation for the host architecture, e.g.
This class represents lattice values for constants.
Definition: AllocatorList.h:23
std::function< std::unique_ptr< IndirectStubsManager >)> createLocalIndirectStubsManagerBuilder(const Triple &T)
Create a local indriect stubs manager builder.
GlobalAlias * cloneGlobalAliasDecl(Module &Dst, const GlobalAlias &OrigA, ValueToValueMapTy &VMap)
Clone a global alias declaration into a new module.
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
void cloneModuleFlagsMetadata(Module &Dst, const Module &Src, ValueToValueMapTy &VMap)
Clone module flags metadata into the destination module.
This class represents a function call, abstracting a target machine's calling convention.
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Definition: Format.h:124
static PointerType * get(Type *ElementType, unsigned AddressSpace)
This constructs a pointer to an object of the specified type in a numbered address space.
Definition: Type.cpp:669
static sys::Mutex Lock
Externally visible function.
Definition: GlobalValue.h:48
F(f)
unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
Definition: DerivedTypes.h:711
An instruction for reading from memory.
Definition: Instructions.h:174
static IntegerType * getInt64Ty(LLVMContext &C)
Definition: Type.cpp:198
Manage compile callbacks for in-process JITs.
Metadata * MapMetadata(const Metadata *MD, ValueToValueMapTy &VM, RemapFlags Flags=RF_None, ValueMapTypeRemapper *TypeMapper=nullptr, ValueMaterializer *Materializer=nullptr)
Lookup or compute a mapping for a piece of metadata.
Definition: ValueMapper.h:228
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
Definition: Type.h:128
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:80
void setInitializer(Constant *InitVal)
setInitializer - Sets the initializer for this global variable, removing any existing initializer if ...
Definition: Globals.cpp:389
GlobalVariable * createImplPointer(PointerType &PT, Module &M, const Twine &Name, Constant *Initializer)
Create a function pointer with the given type, name, and initializer in the given Module.
bool isConstant() const
If the value is a global constant, its value is immutable throughout the runtime execution of the pro...
void CloneFunctionInto(Function *NewFunc, const Function *OldFunc, ValueToValueMapTy &VMap, bool ModuleLevelChanges, SmallVectorImpl< ReturnInst * > &Returns, const char *NameSuffix="", ClonedCodeInfo *CodeInfo=nullptr, ValueMapTypeRemapper *TypeMapper=nullptr, ValueMaterializer *Materializer=nullptr)
Clone OldFunc into NewFunc, transforming the old arguments into references to VMap values.
uint64_t Addr
static StringRef getName(Value *V)
void moveGlobalVariableInitializer(GlobalVariable &OrigGV, ValueToValueMapTy &VMap, ValueMaterializer *Materializer=nullptr, GlobalVariable *NewGV=nullptr)
Move global variable GV from its parent module to cloned global declaration in a different module.
Tagged union holding either a T or a Error.
Definition: APFloat.h:42
void makeStub(Function &F, Value &ImplPointer)
Turn a function declaration into a stub function that makes an indirect call using the given function...
std::vector< GlobalValue * > operator()(Module &M)
Promote symbols in the given module.
Class to represent function types.
Definition: DerivedTypes.h:102
uint64_t JITTargetAddress
Represents an address in the target process's address space.
Definition: JITSymbol.h:42
JITDylibSearchOrder makeJITDylibSearchOrder(ArrayRef< JITDylib * > JDs, JITDylibLookupFlags Flags=JITDylibLookupFlags::MatchExportedSymbolsOnly)
Convenience function for creating a search order from an ArrayRef of JITDylib*, all with the same fla...
Definition: Core.h:155
LinkageTypes getLinkage() const
Definition: GlobalValue.h:461
GlobalVariable * cloneGlobalVariableDecl(Module &Dst, const GlobalVariable &GV, ValueToValueMapTy *VMap=nullptr)
Clone a global variable declaration into a new module.
Class to represent pointers.
Definition: DerivedTypes.h:655
Pointer to a pooled string representing a symbol name.
This is a class that can be implemented by clients to materialize Values on demand.
Definition: ValueMapper.h:50
void getModuleFlagsMetadata(SmallVectorImpl< ModuleFlagEntry > &Flags) const
Returns the module flags in the provided vector.
Definition: Module.cpp:303
static Function * Create(FunctionType *Ty, LinkageTypes Linkage, unsigned AddrSpace, const Twine &N="", Module *M=nullptr)
Definition: Function.h:137
LLVM Basic Block Representation.
Definition: BasicBlock.h:58
This is an important base class in LLVM.
Definition: Constant.h:41
void copyAttributesFrom(const Function *Src)
copyAttributesFrom - copy all additional attributes (those not needed to create a Function) from the ...
Definition: Function.cpp:621
void addModuleFlag(ModFlagBehavior Behavior, StringRef Key, Metadata *Val)
Add a module-level flag to the module-level flags metadata.
Definition: Module.cpp:348
unsigned getAddressSpace() const
Return the address space of the Pointer type.
Definition: DerivedTypes.h:683
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
Definition: BasicBlock.h:100
arg_iterator arg_begin()
Definition: Function.h:762
assume Assume Builder
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Value * MapValue(const Value *V, ValueToValueMapTy &VM, RemapFlags Flags=RF_None, ValueMapTypeRemapper *TypeMapper=nullptr, ValueMaterializer *Materializer=nullptr)
Look up or compute a value in the value map.
Definition: ValueMapper.h:206
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:45
Constant * createIRTypedAddress(FunctionType &FT, JITTargetAddress Addr)
Build a function pointer of FunctionType with the given constant address.
std::function< JITTargetAddress()> CompileFunction
A MaterializationUnit represents a set of symbol definitions that can be materialized as a group,...
Definition: Core.h:635
See the file comment.
Definition: ValueMap.h:85
Function * cloneFunctionDecl(Module &Dst, const Function &F, ValueToValueMapTy *VMap=nullptr)
Clone a function declaration into a new module.
Expected< std::unique_ptr< JITCompileCallbackManager > > createLocalCompileCallbackManager(const Triple &T, ExecutionSession &ES, JITTargetAddress ErrorHandlerAddress)
Create a local compile callback manager.
void copyAttributesFrom(const GlobalValue *Src)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1116
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
Definition: Constants.cpp:867
Expected< JITTargetAddress > getCompileCallback(CompileFunction Compile)
Reserve a compile callback.
An ExecutionSession represents a running JIT program.
Definition: Core.h:1221
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
Represents a symbol that has been evaluated to an address already.
Definition: JITSymbol.h:229
static Constant * getCast(unsigned ops, Constant *C, Type *Ty, bool OnlyIfReduced=false)
Convenience function for getting a Cast operation.
Definition: Constants.cpp:1928
JITTargetAddress executeCompileCallback(JITTargetAddress TrampolineAddr)
Execute the callback for the given trampoline id.
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:295
void copyAttributesFrom(const GlobalVariable *Src)
copyAttributesFrom - copy all additional attributes (those not needed to create a GlobalVariable) fro...
Definition: Globals.cpp:412
#define I(x, y, z)
Definition: MD5.cpp:59
Type * getValueType() const
Definition: GlobalValue.h:273
const std::string to_string(const T &Value)
Definition: ScopedPrinter.h:61
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition: Globals.cpp:228
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:607
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:572
LLVM Value Representation.
Definition: Value.h:75
bool hasInitializer() const
Definitions have initializers, declarations don't.
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1556
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:57
void moveFunctionBody(Function &OrigF, ValueToValueMapTy &VMap, ValueMaterializer *Materializer=nullptr, Function *NewF=nullptr)
Move the body of function 'F' to a cloned function declaration in a different module (See related clo...
static GlobalAlias * create(Type *Ty, unsigned AddressSpace, LinkageTypes Linkage, const Twine &Name, Constant *Aliasee, Module *Parent)
If a parent module is specified, the alias is automatically inserted into the end of the specified mo...
Definition: Globals.cpp:485
PointerType * getType() const
Global values are always pointers.
Definition: GlobalValue.h:271
A symbol table that supports asynchoronous symbol queries.
Definition: Core.h:894
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition: Error.cpp:77
const Constant * getAliasee() const
Definition: GlobalAlias.h:73