Lang Hames | 53ccf88 | 2015-02-22 01:45:31 +0000 | [diff] [blame] | 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 | |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame^] | 10 | #include "llvm/ExecutionEngine/Orc/IndirectionUtils.h" |
Benjamin Kramer | 0a446fd | 2015-03-01 21:28:53 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/STLExtras.h" |
Lang Hames | 93de2a1 | 2015-01-23 21:25:00 +0000 | [diff] [blame] | 12 | #include "llvm/ADT/Triple.h" |
Lang Hames | 105518f | 2016-05-26 17:20:35 +0000 | [diff] [blame] | 13 | #include "llvm/ExecutionEngine/Orc/OrcABISupport.h" |
Lang Hames | 93de2a1 | 2015-01-23 21:25:00 +0000 | [diff] [blame] | 14 | #include "llvm/IR/CallSite.h" |
| 15 | #include "llvm/IR/IRBuilder.h" |
Lang Hames | cd68eba | 2015-05-05 17:37:18 +0000 | [diff] [blame] | 16 | #include "llvm/Transforms/Utils/Cloning.h" |
Lang Hames | c6de458 | 2015-04-12 20:05:51 +0000 | [diff] [blame] | 17 | #include <sstream> |
Lang Hames | 93de2a1 | 2015-01-23 21:25:00 +0000 | [diff] [blame] | 18 | |
Lang Hames | 93de2a1 | 2015-01-23 21:25:00 +0000 | [diff] [blame] | 19 | namespace llvm { |
Lang Hames | e738061 | 2015-02-21 20:44:36 +0000 | [diff] [blame] | 20 | namespace orc { |
Lang Hames | 93de2a1 | 2015-01-23 21:25:00 +0000 | [diff] [blame] | 21 | |
Lang Hames | f0f4b4c | 2015-12-04 02:15:39 +0000 | [diff] [blame] | 22 | void JITCompileCallbackManager::anchor() {} |
Lang Hames | ea39de8 | 2015-12-06 19:44:45 +0000 | [diff] [blame] | 23 | void IndirectStubsManager::anchor() {} |
Lang Hames | 98c2ac1 | 2015-10-19 17:43:51 +0000 | [diff] [blame] | 24 | |
Lang Hames | 105518f | 2016-05-26 17:20:35 +0000 | [diff] [blame] | 25 | std::unique_ptr<JITCompileCallbackManager> |
Benjamin Kramer | c321e53 | 2016-06-08 19:09:22 +0000 | [diff] [blame] | 26 | createLocalCompileCallbackManager(const Triple &T, |
Lang Hames | ad4a911 | 2016-08-01 20:49:11 +0000 | [diff] [blame] | 27 | JITTargetAddress ErrorHandlerAddress) { |
Lang Hames | 105518f | 2016-05-26 17:20:35 +0000 | [diff] [blame] | 28 | switch (T.getArch()) { |
| 29 | default: return nullptr; |
| 30 | |
| 31 | case Triple::x86: { |
| 32 | typedef orc::LocalJITCompileCallbackManager<orc::OrcI386> CCMgrT; |
| 33 | return llvm::make_unique<CCMgrT>(ErrorHandlerAddress); |
| 34 | } |
| 35 | |
| 36 | case Triple::x86_64: { |
| 37 | if ( T.getOS() == Triple::OSType::Win32 ) { |
| 38 | typedef orc::LocalJITCompileCallbackManager<orc::OrcX86_64_Win32> CCMgrT; |
| 39 | return llvm::make_unique<CCMgrT>(ErrorHandlerAddress); |
| 40 | } else { |
| 41 | typedef orc::LocalJITCompileCallbackManager<orc::OrcX86_64_SysV> CCMgrT; |
| 42 | return llvm::make_unique<CCMgrT>(ErrorHandlerAddress); |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | std::function<std::unique_ptr<IndirectStubsManager>()> |
Benjamin Kramer | c321e53 | 2016-06-08 19:09:22 +0000 | [diff] [blame] | 49 | createLocalIndirectStubsManagerBuilder(const Triple &T) { |
Lang Hames | 105518f | 2016-05-26 17:20:35 +0000 | [diff] [blame] | 50 | switch (T.getArch()) { |
| 51 | default: return nullptr; |
| 52 | |
| 53 | case Triple::x86: |
| 54 | return [](){ |
| 55 | return llvm::make_unique< |
| 56 | orc::LocalIndirectStubsManager<orc::OrcI386>>(); |
| 57 | }; |
| 58 | |
| 59 | case Triple::x86_64: |
| 60 | if (T.getOS() == Triple::OSType::Win32) { |
| 61 | return [](){ |
| 62 | return llvm::make_unique< |
| 63 | orc::LocalIndirectStubsManager<orc::OrcX86_64_Win32>>(); |
| 64 | }; |
| 65 | } else { |
| 66 | return [](){ |
| 67 | return llvm::make_unique< |
| 68 | orc::LocalIndirectStubsManager<orc::OrcX86_64_SysV>>(); |
| 69 | }; |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
Lang Hames | ad4a911 | 2016-08-01 20:49:11 +0000 | [diff] [blame] | 74 | Constant* createIRTypedAddress(FunctionType &FT, JITTargetAddress Addr) { |
Lang Hames | eb9bdb5 | 2015-04-11 00:23:49 +0000 | [diff] [blame] | 75 | Constant *AddrIntVal = |
| 76 | ConstantInt::get(Type::getInt64Ty(FT.getContext()), Addr); |
| 77 | Constant *AddrPtrVal = |
| 78 | ConstantExpr::getCast(Instruction::IntToPtr, AddrIntVal, |
| 79 | PointerType::get(&FT, 0)); |
| 80 | return AddrPtrVal; |
| 81 | } |
| 82 | |
| 83 | GlobalVariable* createImplPointer(PointerType &PT, Module &M, |
| 84 | const Twine &Name, Constant *Initializer) { |
Lang Hames | cd68eba | 2015-05-05 17:37:18 +0000 | [diff] [blame] | 85 | auto IP = new GlobalVariable(M, &PT, false, GlobalValue::ExternalLinkage, |
| 86 | Initializer, Name, nullptr, |
| 87 | GlobalValue::NotThreadLocal, 0, true); |
| 88 | IP->setVisibility(GlobalValue::HiddenVisibility); |
| 89 | return IP; |
Lang Hames | 93de2a1 | 2015-01-23 21:25:00 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Lang Hames | 98c2ac1 | 2015-10-19 17:43:51 +0000 | [diff] [blame] | 92 | void makeStub(Function &F, Value &ImplPointer) { |
Lang Hames | 2754714 | 2015-02-17 01:18:38 +0000 | [diff] [blame] | 93 | assert(F.isDeclaration() && "Can't turn a definition into a stub."); |
| 94 | assert(F.getParent() && "Function isn't in a module."); |
| 95 | Module &M = *F.getParent(); |
| 96 | BasicBlock *EntryBlock = BasicBlock::Create(M.getContext(), "entry", &F); |
| 97 | IRBuilder<> Builder(EntryBlock); |
| 98 | LoadInst *ImplAddr = Builder.CreateLoad(&ImplPointer); |
| 99 | std::vector<Value*> CallArgs; |
| 100 | for (auto &A : F.args()) |
| 101 | CallArgs.push_back(&A); |
| 102 | CallInst *Call = Builder.CreateCall(ImplAddr, CallArgs); |
| 103 | Call->setTailCall(); |
Lang Hames | dc4260d | 2015-04-20 20:41:45 +0000 | [diff] [blame] | 104 | Call->setAttributes(F.getAttributes()); |
Lang Hames | cd68eba | 2015-05-05 17:37:18 +0000 | [diff] [blame] | 105 | if (F.getReturnType()->isVoidTy()) |
| 106 | Builder.CreateRetVoid(); |
| 107 | else |
| 108 | Builder.CreateRet(Call); |
Lang Hames | 93de2a1 | 2015-01-23 21:25:00 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Lang Hames | c6de458 | 2015-04-12 20:05:51 +0000 | [diff] [blame] | 111 | // Utility class for renaming global values and functions during partitioning. |
| 112 | class GlobalRenamer { |
| 113 | public: |
| 114 | |
| 115 | static bool needsRenaming(const Value &New) { |
Alexander Kornienko | db73c2f | 2015-11-05 21:18:09 +0000 | [diff] [blame] | 116 | return !New.hasName() || New.getName().startswith("\01L"); |
Lang Hames | c6de458 | 2015-04-12 20:05:51 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | const std::string& getRename(const Value &Orig) { |
| 120 | // See if we have a name for this global. |
| 121 | { |
| 122 | auto I = Names.find(&Orig); |
| 123 | if (I != Names.end()) |
| 124 | return I->second; |
| 125 | } |
| 126 | |
| 127 | // Nope. Create a new one. |
| 128 | // FIXME: Use a more robust uniquing scheme. (This may blow up if the user |
| 129 | // writes a "__orc_anon[[:digit:]]* method). |
| 130 | unsigned ID = Names.size(); |
| 131 | std::ostringstream NameStream; |
| 132 | NameStream << "__orc_anon" << ID++; |
| 133 | auto I = Names.insert(std::make_pair(&Orig, NameStream.str())); |
| 134 | return I.first->second; |
| 135 | } |
| 136 | private: |
| 137 | DenseMap<const Value*, std::string> Names; |
| 138 | }; |
| 139 | |
Lang Hames | cd68eba | 2015-05-05 17:37:18 +0000 | [diff] [blame] | 140 | static void raiseVisibilityOnValue(GlobalValue &V, GlobalRenamer &R) { |
| 141 | if (V.hasLocalLinkage()) { |
| 142 | if (R.needsRenaming(V)) |
| 143 | V.setName(R.getRename(V)); |
| 144 | V.setLinkage(GlobalValue::ExternalLinkage); |
| 145 | V.setVisibility(GlobalValue::HiddenVisibility); |
Lang Hames | ac31a1f | 2015-05-04 23:30:01 +0000 | [diff] [blame] | 146 | } |
Peter Collingbourne | 96efdd6 | 2016-06-14 21:01:22 +0000 | [diff] [blame] | 147 | V.setUnnamedAddr(GlobalValue::UnnamedAddr::None); |
Lang Hames | cd68eba | 2015-05-05 17:37:18 +0000 | [diff] [blame] | 148 | assert(!R.needsRenaming(V) && "Invalid global name."); |
Lang Hames | a68970d | 2015-05-04 22:03:10 +0000 | [diff] [blame] | 149 | } |
Lang Hames | 2754714 | 2015-02-17 01:18:38 +0000 | [diff] [blame] | 150 | |
Lang Hames | cd68eba | 2015-05-05 17:37:18 +0000 | [diff] [blame] | 151 | void makeAllSymbolsExternallyAccessible(Module &M) { |
| 152 | GlobalRenamer Renamer; |
Lang Hames | 93de2a1 | 2015-01-23 21:25:00 +0000 | [diff] [blame] | 153 | |
Lang Hames | cd68eba | 2015-05-05 17:37:18 +0000 | [diff] [blame] | 154 | for (auto &F : M) |
| 155 | raiseVisibilityOnValue(F, Renamer); |
Lang Hames | ac31a1f | 2015-05-04 23:30:01 +0000 | [diff] [blame] | 156 | |
Lang Hames | cd68eba | 2015-05-05 17:37:18 +0000 | [diff] [blame] | 157 | for (auto &GV : M.globals()) |
| 158 | raiseVisibilityOnValue(GV, Renamer); |
Lang Hames | 98c2ac1 | 2015-10-19 17:43:51 +0000 | [diff] [blame] | 159 | |
| 160 | for (auto &A : M.aliases()) |
| 161 | raiseVisibilityOnValue(A, Renamer); |
Lang Hames | cd68eba | 2015-05-05 17:37:18 +0000 | [diff] [blame] | 162 | } |
Lang Hames | ac31a1f | 2015-05-04 23:30:01 +0000 | [diff] [blame] | 163 | |
Lang Hames | cd68eba | 2015-05-05 17:37:18 +0000 | [diff] [blame] | 164 | Function* cloneFunctionDecl(Module &Dst, const Function &F, |
| 165 | ValueToValueMapTy *VMap) { |
| 166 | assert(F.getParent() != &Dst && "Can't copy decl over existing function."); |
| 167 | Function *NewF = |
Manuel Jacob | 5f6eaac | 2016-01-16 20:30:46 +0000 | [diff] [blame] | 168 | Function::Create(cast<FunctionType>(F.getValueType()), |
Lang Hames | cd68eba | 2015-05-05 17:37:18 +0000 | [diff] [blame] | 169 | F.getLinkage(), F.getName(), &Dst); |
| 170 | NewF->copyAttributesFrom(&F); |
Lang Hames | ac31a1f | 2015-05-04 23:30:01 +0000 | [diff] [blame] | 171 | |
Lang Hames | cd68eba | 2015-05-05 17:37:18 +0000 | [diff] [blame] | 172 | if (VMap) { |
| 173 | (*VMap)[&F] = NewF; |
| 174 | auto NewArgI = NewF->arg_begin(); |
| 175 | for (auto ArgI = F.arg_begin(), ArgE = F.arg_end(); ArgI != ArgE; |
| 176 | ++ArgI, ++NewArgI) |
Duncan P. N. Exon Smith | 1275bff | 2015-10-13 18:10:59 +0000 | [diff] [blame] | 177 | (*VMap)[&*ArgI] = &*NewArgI; |
Lang Hames | 93de2a1 | 2015-01-23 21:25:00 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Lang Hames | cd68eba | 2015-05-05 17:37:18 +0000 | [diff] [blame] | 180 | return NewF; |
| 181 | } |
Lang Hames | 2754714 | 2015-02-17 01:18:38 +0000 | [diff] [blame] | 182 | |
Lang Hames | cd68eba | 2015-05-05 17:37:18 +0000 | [diff] [blame] | 183 | void moveFunctionBody(Function &OrigF, ValueToValueMapTy &VMap, |
| 184 | ValueMaterializer *Materializer, |
| 185 | Function *NewF) { |
| 186 | assert(!OrigF.isDeclaration() && "Nothing to move"); |
| 187 | if (!NewF) |
| 188 | NewF = cast<Function>(VMap[&OrigF]); |
| 189 | else |
| 190 | assert(VMap[&OrigF] == NewF && "Incorrect function mapping in VMap."); |
| 191 | assert(NewF && "Function mapping missing from VMap."); |
| 192 | assert(NewF->getParent() != OrigF.getParent() && |
| 193 | "moveFunctionBody should only be used to move bodies between " |
| 194 | "modules."); |
Lang Hames | 2754714 | 2015-02-17 01:18:38 +0000 | [diff] [blame] | 195 | |
Lang Hames | cd68eba | 2015-05-05 17:37:18 +0000 | [diff] [blame] | 196 | SmallVector<ReturnInst *, 8> Returns; // Ignore returns cloned. |
| 197 | CloneFunctionInto(NewF, &OrigF, VMap, /*ModuleLevelChanges=*/true, Returns, |
| 198 | "", nullptr, nullptr, Materializer); |
| 199 | OrigF.deleteBody(); |
| 200 | } |
Lang Hames | 2754714 | 2015-02-17 01:18:38 +0000 | [diff] [blame] | 201 | |
Lang Hames | cd68eba | 2015-05-05 17:37:18 +0000 | [diff] [blame] | 202 | GlobalVariable* cloneGlobalVariableDecl(Module &Dst, const GlobalVariable &GV, |
| 203 | ValueToValueMapTy *VMap) { |
| 204 | assert(GV.getParent() != &Dst && "Can't copy decl over existing global var."); |
| 205 | GlobalVariable *NewGV = new GlobalVariable( |
Manuel Jacob | 5f6eaac | 2016-01-16 20:30:46 +0000 | [diff] [blame] | 206 | Dst, GV.getValueType(), GV.isConstant(), |
Lang Hames | cd68eba | 2015-05-05 17:37:18 +0000 | [diff] [blame] | 207 | GV.getLinkage(), nullptr, GV.getName(), nullptr, |
| 208 | GV.getThreadLocalMode(), GV.getType()->getAddressSpace()); |
| 209 | NewGV->copyAttributesFrom(&GV); |
| 210 | if (VMap) |
| 211 | (*VMap)[&GV] = NewGV; |
| 212 | return NewGV; |
| 213 | } |
Lang Hames | 2754714 | 2015-02-17 01:18:38 +0000 | [diff] [blame] | 214 | |
Lang Hames | cd68eba | 2015-05-05 17:37:18 +0000 | [diff] [blame] | 215 | void moveGlobalVariableInitializer(GlobalVariable &OrigGV, |
| 216 | ValueToValueMapTy &VMap, |
| 217 | ValueMaterializer *Materializer, |
| 218 | GlobalVariable *NewGV) { |
| 219 | assert(OrigGV.hasInitializer() && "Nothing to move"); |
| 220 | if (!NewGV) |
| 221 | NewGV = cast<GlobalVariable>(VMap[&OrigGV]); |
| 222 | else |
| 223 | assert(VMap[&OrigGV] == NewGV && |
| 224 | "Incorrect global variable mapping in VMap."); |
| 225 | assert(NewGV->getParent() != OrigGV.getParent() && |
| 226 | "moveGlobalVariable should only be used to move initializers between " |
| 227 | "modules"); |
| 228 | |
| 229 | NewGV->setInitializer(MapValue(OrigGV.getInitializer(), VMap, RF_None, |
| 230 | nullptr, Materializer)); |
Lang Hames | 93de2a1 | 2015-01-23 21:25:00 +0000 | [diff] [blame] | 231 | } |
| 232 | |
Lang Hames | 98c2ac1 | 2015-10-19 17:43:51 +0000 | [diff] [blame] | 233 | GlobalAlias* cloneGlobalAliasDecl(Module &Dst, const GlobalAlias &OrigA, |
| 234 | ValueToValueMapTy &VMap) { |
Lang Hames | 44780ac | 2015-10-06 22:55:05 +0000 | [diff] [blame] | 235 | assert(OrigA.getAliasee() && "Original alias doesn't have an aliasee?"); |
| 236 | auto *NewA = GlobalAlias::create(OrigA.getValueType(), |
| 237 | OrigA.getType()->getPointerAddressSpace(), |
| 238 | OrigA.getLinkage(), OrigA.getName(), &Dst); |
| 239 | NewA->copyAttributesFrom(&OrigA); |
| 240 | VMap[&OrigA] = NewA; |
Lang Hames | 44780ac | 2015-10-06 22:55:05 +0000 | [diff] [blame] | 241 | return NewA; |
| 242 | } |
| 243 | |
Lang Hames | 38c7927 | 2016-09-04 17:53:30 +0000 | [diff] [blame] | 244 | void cloneModuleFlagsMetadata(Module &Dst, const Module &Src, |
| 245 | ValueToValueMapTy &VMap) { |
| 246 | auto *MFs = Src.getModuleFlagsMetadata(); |
| 247 | if (!MFs) |
| 248 | return; |
| 249 | for (auto *MF : MFs->operands()) |
| 250 | Dst.addModuleFlag(MapMetadata(MF, VMap)); |
| 251 | } |
| 252 | |
Lang Hames | e738061 | 2015-02-21 20:44:36 +0000 | [diff] [blame] | 253 | } // End namespace orc. |
| 254 | } // End namespace llvm. |