| 1 | //===---------- speculation.cpp - Utilities for Speculation ----------===// |
| 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 | #include "llvm/ExecutionEngine/Orc/Speculation.h" |
| 10 | |
| 11 | #include "llvm/ExecutionEngine/Orc/AbsoluteSymbols.h" |
| 12 | #include "llvm/IR/BasicBlock.h" |
| 13 | #include "llvm/IR/Function.h" |
| 14 | #include "llvm/IR/IRBuilder.h" |
| 15 | #include "llvm/IR/Instruction.h" |
| 16 | #include "llvm/IR/Instructions.h" |
| 17 | #include "llvm/IR/LLVMContext.h" |
| 18 | #include "llvm/IR/Module.h" |
| 19 | #include "llvm/IR/Type.h" |
| 20 | #include "llvm/IR/Verifier.h" |
| 21 | |
| 22 | namespace llvm { |
| 23 | |
| 24 | namespace orc { |
| 25 | |
| 26 | // ImplSymbolMap methods |
| 27 | void ImplSymbolMap::trackImpls(SymbolAliasMap ImplMaps, JITDylib *SrcJD) { |
| 28 | assert(SrcJD && "Tracking on Null Source .impl dylib" ); |
| 29 | std::lock_guard<std::mutex> Lockit(ConcurrentAccess); |
| 30 | for (auto &I : ImplMaps) { |
| 31 | auto It = Maps.insert(KV: {I.first, {I.second.Aliasee, SrcJD}}); |
| 32 | // check rationale when independent dylibs have same symbol name? |
| 33 | assert(It.second && "ImplSymbols are already tracked for this Symbol?" ); |
| 34 | (void)(It); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | // Trigger Speculative Compiles. |
| 39 | void Speculator::speculateForEntryPoint(Speculator *Ptr, uint64_t StubId) { |
| 40 | assert(Ptr && " Null Address Received in orc_speculate_for " ); |
| 41 | Ptr->speculateFor(StubAddr: ExecutorAddr(StubId)); |
| 42 | } |
| 43 | |
| 44 | Error Speculator::addSpeculationRuntime(JITDylib &JD, |
| 45 | MangleAndInterner &Mangle) { |
| 46 | ExecutorSymbolDef ThisPtr(ExecutorAddr::fromPtr(Ptr: this), |
| 47 | JITSymbolFlags::Exported); |
| 48 | ExecutorSymbolDef SpeculateForEntryPtr( |
| 49 | ExecutorAddr::fromPtr(Ptr: &speculateForEntryPoint), JITSymbolFlags::Exported); |
| 50 | return JD.define(MU: absoluteSymbols(Symbols: { |
| 51 | {Mangle("__orc_speculator" ), ThisPtr}, // Data Symbol |
| 52 | {Mangle("__orc_speculate_for" ), SpeculateForEntryPtr} // Callable Symbol |
| 53 | })); |
| 54 | } |
| 55 | |
| 56 | // If two modules, share the same LLVMContext, different threads must |
| 57 | // not access them concurrently without locking the associated LLVMContext |
| 58 | // this implementation follows this contract. |
| 59 | void IRSpeculationLayer::emit(std::unique_ptr<MaterializationResponsibility> R, |
| 60 | ThreadSafeModule TSM) { |
| 61 | |
| 62 | assert(TSM && "Speculation Layer received Null Module ?" ); |
| 63 | |
| 64 | // Instrumentation of runtime calls, lock the Module |
| 65 | TSM.withModuleDo(F: [this, &R](Module &M) { |
| 66 | auto &MContext = M.getContext(); |
| 67 | auto SpeculatorVTy = StructType::create(Context&: MContext, Name: "Class.Speculator" ); |
| 68 | auto RuntimeCallTy = FunctionType::get( |
| 69 | Result: Type::getVoidTy(C&: MContext), |
| 70 | Params: {PointerType::getUnqual(C&: MContext), Type::getInt64Ty(C&: MContext)}, isVarArg: false); |
| 71 | auto RuntimeCall = |
| 72 | Function::Create(Ty: RuntimeCallTy, Linkage: Function::LinkageTypes::ExternalLinkage, |
| 73 | N: "__orc_speculate_for" , M: &M); |
| 74 | auto SpeclAddr = new GlobalVariable( |
| 75 | M, SpeculatorVTy, false, GlobalValue::LinkageTypes::ExternalLinkage, |
| 76 | nullptr, "__orc_speculator" ); |
| 77 | |
| 78 | IRBuilder<> Mutator(MContext); |
| 79 | |
| 80 | // QueryAnalysis allowed to transform the IR source, one such example is |
| 81 | // Simplify CFG helps the static branch prediction heuristics! |
| 82 | for (auto &Fn : M.getFunctionList()) { |
| 83 | if (!Fn.isDeclaration()) { |
| 84 | |
| 85 | auto IRNames = QueryAnalysis(Fn); |
| 86 | // Instrument and register if Query has result |
| 87 | if (IRNames) { |
| 88 | |
| 89 | // Emit globals for each function. |
| 90 | auto LoadValueTy = Type::getInt8Ty(C&: MContext); |
| 91 | auto SpeculatorGuard = new GlobalVariable( |
| 92 | M, LoadValueTy, false, GlobalValue::LinkageTypes::InternalLinkage, |
| 93 | ConstantInt::get(Ty: LoadValueTy, V: 0), |
| 94 | "__orc_speculate.guard.for." + Fn.getName()); |
| 95 | SpeculatorGuard->setAlignment(Align(1)); |
| 96 | SpeculatorGuard->setUnnamedAddr(GlobalValue::UnnamedAddr::Local); |
| 97 | |
| 98 | BasicBlock &ProgramEntry = Fn.getEntryBlock(); |
| 99 | // Create BasicBlocks before the program's entry basicblock |
| 100 | BasicBlock *SpeculateBlock = BasicBlock::Create( |
| 101 | Context&: MContext, Name: "__orc_speculate.block" , Parent: &Fn, InsertBefore: &ProgramEntry); |
| 102 | BasicBlock *SpeculateDecisionBlock = BasicBlock::Create( |
| 103 | Context&: MContext, Name: "__orc_speculate.decision.block" , Parent: &Fn, InsertBefore: SpeculateBlock); |
| 104 | |
| 105 | assert(SpeculateDecisionBlock == &Fn.getEntryBlock() && |
| 106 | "SpeculateDecisionBlock not updated?" ); |
| 107 | Mutator.SetInsertPoint(SpeculateDecisionBlock); |
| 108 | |
| 109 | auto LoadGuard = |
| 110 | Mutator.CreateLoad(Ty: LoadValueTy, Ptr: SpeculatorGuard, Name: "guard.value" ); |
| 111 | // if just loaded value equal to 0,return true. |
| 112 | auto CanSpeculate = |
| 113 | Mutator.CreateICmpEQ(LHS: LoadGuard, RHS: ConstantInt::get(Ty: LoadValueTy, V: 0), |
| 114 | Name: "compare.to.speculate" ); |
| 115 | Mutator.CreateCondBr(Cond: CanSpeculate, True: SpeculateBlock, False: &ProgramEntry); |
| 116 | |
| 117 | Mutator.SetInsertPoint(SpeculateBlock); |
| 118 | auto ImplAddrToUint = |
| 119 | Mutator.CreatePtrToInt(V: &Fn, DestTy: Type::getInt64Ty(C&: MContext)); |
| 120 | Mutator.CreateCall(FTy: RuntimeCallTy, Callee: RuntimeCall, |
| 121 | Args: {SpeclAddr, ImplAddrToUint}); |
| 122 | Mutator.CreateStore(Val: ConstantInt::get(Ty: LoadValueTy, V: 1), |
| 123 | Ptr: SpeculatorGuard); |
| 124 | Mutator.CreateBr(Dest: &ProgramEntry); |
| 125 | |
| 126 | assert(Mutator.GetInsertBlock()->getParent() == &Fn && |
| 127 | "IR builder association mismatch?" ); |
| 128 | S.registerSymbols(Candidates: internToJITSymbols(IRNames: *IRNames), |
| 129 | JD: &R->getTargetJITDylib()); |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | }); |
| 134 | |
| 135 | assert(!TSM.withModuleDo([](const Module &M) { return verifyModule(M); }) && |
| 136 | "Speculation Instrumentation breaks IR?" ); |
| 137 | |
| 138 | NextLayer.emit(R: std::move(R), TSM: std::move(TSM)); |
| 139 | } |
| 140 | |
| 141 | } // namespace orc |
| 142 | } // namespace llvm |
| 143 | |