1 //===---- IndirectionUtils.cpp - Utilities for call indirection in Orc ----===//
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 "llvm/ExecutionEngine/Orc/IndirectionUtils.h"
11 #include "llvm/ADT/STLExtras.h"
12 #include "llvm/ADT/Triple.h"
13 #include "llvm/ExecutionEngine/Orc/OrcABISupport.h"
14 #include "llvm/IR/CallSite.h"
15 #include "llvm/IR/IRBuilder.h"
16 #include "llvm/Support/Format.h"
17 #include "llvm/Transforms/Utils/Cloning.h"
18 #include <sstream>
19
20 using namespace llvm;
21 using namespace llvm::orc;
22
23 namespace {
24
25 class CompileCallbackMaterializationUnit : public orc::MaterializationUnit {
26 public:
27 using CompileFunction = JITCompileCallbackManager::CompileFunction;
28
CompileCallbackMaterializationUnit(SymbolStringPtr Name,CompileFunction Compile)29 CompileCallbackMaterializationUnit(SymbolStringPtr Name,
30 CompileFunction Compile)
31 : MaterializationUnit(SymbolFlagsMap({{Name, JITSymbolFlags::Exported}})),
32 Name(std::move(Name)), Compile(std::move(Compile)) {}
33
34 private:
materialize(MaterializationResponsibility R)35 void materialize(MaterializationResponsibility R) {
36 SymbolMap Result;
37 Result[Name] = JITEvaluatedSymbol(Compile(), JITSymbolFlags::Exported);
38 R.resolve(Result);
39 R.finalize();
40 }
41
discard(const VSO & V,SymbolStringPtr Name)42 void discard(const VSO &V, SymbolStringPtr Name) {
43 llvm_unreachable("Discard should never occur on a LMU?");
44 }
45
46 SymbolStringPtr Name;
47 CompileFunction Compile;
48 };
49
50 } // namespace
51
52 namespace llvm {
53 namespace orc {
54
anchor()55 void JITCompileCallbackManager::anchor() {}
anchor()56 void IndirectStubsManager::anchor() {}
57
58 Expected<JITTargetAddress>
getCompileCallback(CompileFunction Compile)59 JITCompileCallbackManager::getCompileCallback(CompileFunction Compile) {
60 if (auto TrampolineAddr = getAvailableTrampolineAddr()) {
61 auto CallbackName = ES.getSymbolStringPool().intern(
62 std::string("cc") + std::to_string(++NextCallbackId));
63
64 std::lock_guard<std::mutex> Lock(CCMgrMutex);
65 AddrToSymbol[*TrampolineAddr] = CallbackName;
66 cantFail(CallbacksVSO.define(
67 llvm::make_unique<CompileCallbackMaterializationUnit>(
68 std::move(CallbackName), std::move(Compile))));
69 return *TrampolineAddr;
70 } else
71 return TrampolineAddr.takeError();
72 }
73
executeCompileCallback(JITTargetAddress TrampolineAddr)74 JITTargetAddress JITCompileCallbackManager::executeCompileCallback(
75 JITTargetAddress TrampolineAddr) {
76 SymbolStringPtr Name;
77
78 {
79 std::unique_lock<std::mutex> Lock(CCMgrMutex);
80 auto I = AddrToSymbol.find(TrampolineAddr);
81
82 // If this address is not associated with a compile callback then report an
83 // error to the execution session and return ErrorHandlerAddress to the
84 // callee.
85 if (I == AddrToSymbol.end()) {
86 Lock.unlock();
87 std::string ErrMsg;
88 {
89 raw_string_ostream ErrMsgStream(ErrMsg);
90 ErrMsgStream << "No compile callback for trampoline at "
91 << format("0x%016x", TrampolineAddr);
92 }
93 ES.reportError(
94 make_error<StringError>(std::move(ErrMsg), inconvertibleErrorCode()));
95 return ErrorHandlerAddress;
96 } else
97 Name = I->second;
98 }
99
100 if (auto Sym = lookup({&CallbacksVSO}, Name))
101 return Sym->getAddress();
102 else {
103 // If anything goes wrong materializing Sym then report it to the session
104 // and return the ErrorHandlerAddress;
105 ES.reportError(Sym.takeError());
106 return ErrorHandlerAddress;
107 }
108 }
109
110 std::unique_ptr<JITCompileCallbackManager>
createLocalCompileCallbackManager(const Triple & T,ExecutionSession & ES,JITTargetAddress ErrorHandlerAddress)111 createLocalCompileCallbackManager(const Triple &T, ExecutionSession &ES,
112 JITTargetAddress ErrorHandlerAddress) {
113 switch (T.getArch()) {
114 default: return nullptr;
115
116 case Triple::aarch64: {
117 typedef orc::LocalJITCompileCallbackManager<orc::OrcAArch64> CCMgrT;
118 return llvm::make_unique<CCMgrT>(ES, ErrorHandlerAddress);
119 }
120
121 case Triple::x86: {
122 typedef orc::LocalJITCompileCallbackManager<orc::OrcI386> CCMgrT;
123 return llvm::make_unique<CCMgrT>(ES, ErrorHandlerAddress);
124 }
125
126 case Triple::x86_64: {
127 if ( T.getOS() == Triple::OSType::Win32 ) {
128 typedef orc::LocalJITCompileCallbackManager<orc::OrcX86_64_Win32> CCMgrT;
129 return llvm::make_unique<CCMgrT>(ES, ErrorHandlerAddress);
130 } else {
131 typedef orc::LocalJITCompileCallbackManager<orc::OrcX86_64_SysV> CCMgrT;
132 return llvm::make_unique<CCMgrT>(ES, ErrorHandlerAddress);
133 }
134 }
135
136 }
137 }
138
139 std::function<std::unique_ptr<IndirectStubsManager>()>
createLocalIndirectStubsManagerBuilder(const Triple & T)140 createLocalIndirectStubsManagerBuilder(const Triple &T) {
141 switch (T.getArch()) {
142 default:
143 return [](){
144 return llvm::make_unique<
145 orc::LocalIndirectStubsManager<orc::OrcGenericABI>>();
146 };
147
148 case Triple::aarch64:
149 return [](){
150 return llvm::make_unique<
151 orc::LocalIndirectStubsManager<orc::OrcAArch64>>();
152 };
153
154 case Triple::x86:
155 return [](){
156 return llvm::make_unique<
157 orc::LocalIndirectStubsManager<orc::OrcI386>>();
158 };
159
160 case Triple::x86_64:
161 if (T.getOS() == Triple::OSType::Win32) {
162 return [](){
163 return llvm::make_unique<
164 orc::LocalIndirectStubsManager<orc::OrcX86_64_Win32>>();
165 };
166 } else {
167 return [](){
168 return llvm::make_unique<
169 orc::LocalIndirectStubsManager<orc::OrcX86_64_SysV>>();
170 };
171 }
172
173 }
174 }
175
createIRTypedAddress(FunctionType & FT,JITTargetAddress Addr)176 Constant* createIRTypedAddress(FunctionType &FT, JITTargetAddress Addr) {
177 Constant *AddrIntVal =
178 ConstantInt::get(Type::getInt64Ty(FT.getContext()), Addr);
179 Constant *AddrPtrVal =
180 ConstantExpr::getCast(Instruction::IntToPtr, AddrIntVal,
181 PointerType::get(&FT, 0));
182 return AddrPtrVal;
183 }
184
createImplPointer(PointerType & PT,Module & M,const Twine & Name,Constant * Initializer)185 GlobalVariable* createImplPointer(PointerType &PT, Module &M,
186 const Twine &Name, Constant *Initializer) {
187 auto IP = new GlobalVariable(M, &PT, false, GlobalValue::ExternalLinkage,
188 Initializer, Name, nullptr,
189 GlobalValue::NotThreadLocal, 0, true);
190 IP->setVisibility(GlobalValue::HiddenVisibility);
191 return IP;
192 }
193
makeStub(Function & F,Value & ImplPointer)194 void makeStub(Function &F, Value &ImplPointer) {
195 assert(F.isDeclaration() && "Can't turn a definition into a stub.");
196 assert(F.getParent() && "Function isn't in a module.");
197 Module &M = *F.getParent();
198 BasicBlock *EntryBlock = BasicBlock::Create(M.getContext(), "entry", &F);
199 IRBuilder<> Builder(EntryBlock);
200 LoadInst *ImplAddr = Builder.CreateLoad(&ImplPointer);
201 std::vector<Value*> CallArgs;
202 for (auto &A : F.args())
203 CallArgs.push_back(&A);
204 CallInst *Call = Builder.CreateCall(ImplAddr, CallArgs);
205 Call->setTailCall();
206 Call->setAttributes(F.getAttributes());
207 if (F.getReturnType()->isVoidTy())
208 Builder.CreateRetVoid();
209 else
210 Builder.CreateRet(Call);
211 }
212
213 // Utility class for renaming global values and functions during partitioning.
214 class GlobalRenamer {
215 public:
216
needsRenaming(const Value & New)217 static bool needsRenaming(const Value &New) {
218 return !New.hasName() || New.getName().startswith("\01L");
219 }
220
getRename(const Value & Orig)221 const std::string& getRename(const Value &Orig) {
222 // See if we have a name for this global.
223 {
224 auto I = Names.find(&Orig);
225 if (I != Names.end())
226 return I->second;
227 }
228
229 // Nope. Create a new one.
230 // FIXME: Use a more robust uniquing scheme. (This may blow up if the user
231 // writes a "__orc_anon[[:digit:]]* method).
232 unsigned ID = Names.size();
233 std::ostringstream NameStream;
234 NameStream << "__orc_anon" << ID++;
235 auto I = Names.insert(std::make_pair(&Orig, NameStream.str()));
236 return I.first->second;
237 }
238 private:
239 DenseMap<const Value*, std::string> Names;
240 };
241
raiseVisibilityOnValue(GlobalValue & V,GlobalRenamer & R)242 static void raiseVisibilityOnValue(GlobalValue &V, GlobalRenamer &R) {
243 if (V.hasLocalLinkage()) {
244 if (R.needsRenaming(V))
245 V.setName(R.getRename(V));
246 V.setLinkage(GlobalValue::ExternalLinkage);
247 V.setVisibility(GlobalValue::HiddenVisibility);
248 }
249 V.setUnnamedAddr(GlobalValue::UnnamedAddr::None);
250 assert(!R.needsRenaming(V) && "Invalid global name.");
251 }
252
makeAllSymbolsExternallyAccessible(Module & M)253 void makeAllSymbolsExternallyAccessible(Module &M) {
254 GlobalRenamer Renamer;
255
256 for (auto &F : M)
257 raiseVisibilityOnValue(F, Renamer);
258
259 for (auto &GV : M.globals())
260 raiseVisibilityOnValue(GV, Renamer);
261
262 for (auto &A : M.aliases())
263 raiseVisibilityOnValue(A, Renamer);
264 }
265
cloneFunctionDecl(Module & Dst,const Function & F,ValueToValueMapTy * VMap)266 Function* cloneFunctionDecl(Module &Dst, const Function &F,
267 ValueToValueMapTy *VMap) {
268 Function *NewF =
269 Function::Create(cast<FunctionType>(F.getValueType()),
270 F.getLinkage(), F.getName(), &Dst);
271 NewF->copyAttributesFrom(&F);
272
273 if (VMap) {
274 (*VMap)[&F] = NewF;
275 auto NewArgI = NewF->arg_begin();
276 for (auto ArgI = F.arg_begin(), ArgE = F.arg_end(); ArgI != ArgE;
277 ++ArgI, ++NewArgI)
278 (*VMap)[&*ArgI] = &*NewArgI;
279 }
280
281 return NewF;
282 }
283
moveFunctionBody(Function & OrigF,ValueToValueMapTy & VMap,ValueMaterializer * Materializer,Function * NewF)284 void moveFunctionBody(Function &OrigF, ValueToValueMapTy &VMap,
285 ValueMaterializer *Materializer,
286 Function *NewF) {
287 assert(!OrigF.isDeclaration() && "Nothing to move");
288 if (!NewF)
289 NewF = cast<Function>(VMap[&OrigF]);
290 else
291 assert(VMap[&OrigF] == NewF && "Incorrect function mapping in VMap.");
292 assert(NewF && "Function mapping missing from VMap.");
293 assert(NewF->getParent() != OrigF.getParent() &&
294 "moveFunctionBody should only be used to move bodies between "
295 "modules.");
296
297 SmallVector<ReturnInst *, 8> Returns; // Ignore returns cloned.
298 CloneFunctionInto(NewF, &OrigF, VMap, /*ModuleLevelChanges=*/true, Returns,
299 "", nullptr, nullptr, Materializer);
300 OrigF.deleteBody();
301 }
302
cloneGlobalVariableDecl(Module & Dst,const GlobalVariable & GV,ValueToValueMapTy * VMap)303 GlobalVariable* cloneGlobalVariableDecl(Module &Dst, const GlobalVariable &GV,
304 ValueToValueMapTy *VMap) {
305 GlobalVariable *NewGV = new GlobalVariable(
306 Dst, GV.getValueType(), GV.isConstant(),
307 GV.getLinkage(), nullptr, GV.getName(), nullptr,
308 GV.getThreadLocalMode(), GV.getType()->getAddressSpace());
309 NewGV->copyAttributesFrom(&GV);
310 if (VMap)
311 (*VMap)[&GV] = NewGV;
312 return NewGV;
313 }
314
moveGlobalVariableInitializer(GlobalVariable & OrigGV,ValueToValueMapTy & VMap,ValueMaterializer * Materializer,GlobalVariable * NewGV)315 void moveGlobalVariableInitializer(GlobalVariable &OrigGV,
316 ValueToValueMapTy &VMap,
317 ValueMaterializer *Materializer,
318 GlobalVariable *NewGV) {
319 assert(OrigGV.hasInitializer() && "Nothing to move");
320 if (!NewGV)
321 NewGV = cast<GlobalVariable>(VMap[&OrigGV]);
322 else
323 assert(VMap[&OrigGV] == NewGV &&
324 "Incorrect global variable mapping in VMap.");
325 assert(NewGV->getParent() != OrigGV.getParent() &&
326 "moveGlobalVariableInitializer should only be used to move "
327 "initializers between modules");
328
329 NewGV->setInitializer(MapValue(OrigGV.getInitializer(), VMap, RF_None,
330 nullptr, Materializer));
331 }
332
cloneGlobalAliasDecl(Module & Dst,const GlobalAlias & OrigA,ValueToValueMapTy & VMap)333 GlobalAlias* cloneGlobalAliasDecl(Module &Dst, const GlobalAlias &OrigA,
334 ValueToValueMapTy &VMap) {
335 assert(OrigA.getAliasee() && "Original alias doesn't have an aliasee?");
336 auto *NewA = GlobalAlias::create(OrigA.getValueType(),
337 OrigA.getType()->getPointerAddressSpace(),
338 OrigA.getLinkage(), OrigA.getName(), &Dst);
339 NewA->copyAttributesFrom(&OrigA);
340 VMap[&OrigA] = NewA;
341 return NewA;
342 }
343
cloneModuleFlagsMetadata(Module & Dst,const Module & Src,ValueToValueMapTy & VMap)344 void cloneModuleFlagsMetadata(Module &Dst, const Module &Src,
345 ValueToValueMapTy &VMap) {
346 auto *MFs = Src.getModuleFlagsMetadata();
347 if (!MFs)
348 return;
349 for (auto *MF : MFs->operands())
350 Dst.addModuleFlag(MapMetadata(MF, VMap));
351 }
352
353 } // End namespace orc.
354 } // End namespace llvm.
355