18bcb0991SDimitry Andric //===---------- speculation.cpp - Utilities for Speculation ----------===// 28bcb0991SDimitry Andric // 38bcb0991SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 48bcb0991SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 58bcb0991SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 68bcb0991SDimitry Andric // 78bcb0991SDimitry Andric //===----------------------------------------------------------------------===// 88bcb0991SDimitry Andric 98bcb0991SDimitry Andric #include "llvm/ExecutionEngine/Orc/Speculation.h" 108bcb0991SDimitry Andric #include "llvm/IR/BasicBlock.h" 118bcb0991SDimitry Andric #include "llvm/IR/Function.h" 128bcb0991SDimitry Andric #include "llvm/IR/IRBuilder.h" 138bcb0991SDimitry Andric #include "llvm/IR/Instruction.h" 148bcb0991SDimitry Andric #include "llvm/IR/Instructions.h" 158bcb0991SDimitry Andric #include "llvm/IR/LLVMContext.h" 168bcb0991SDimitry Andric #include "llvm/IR/Module.h" 178bcb0991SDimitry Andric #include "llvm/IR/Type.h" 188bcb0991SDimitry Andric #include "llvm/IR/Verifier.h" 198bcb0991SDimitry Andric 208bcb0991SDimitry Andric namespace llvm { 218bcb0991SDimitry Andric 228bcb0991SDimitry Andric namespace orc { 238bcb0991SDimitry Andric 248bcb0991SDimitry Andric // ImplSymbolMap methods 258bcb0991SDimitry Andric void ImplSymbolMap::trackImpls(SymbolAliasMap ImplMaps, JITDylib *SrcJD) { 268bcb0991SDimitry Andric assert(SrcJD && "Tracking on Null Source .impl dylib"); 278bcb0991SDimitry Andric std::lock_guard<std::mutex> Lockit(ConcurrentAccess); 288bcb0991SDimitry Andric for (auto &I : ImplMaps) { 298bcb0991SDimitry Andric auto It = Maps.insert({I.first, {I.second.Aliasee, SrcJD}}); 308bcb0991SDimitry Andric // check rationale when independent dylibs have same symbol name? 318bcb0991SDimitry Andric assert(It.second && "ImplSymbols are already tracked for this Symbol?"); 328bcb0991SDimitry Andric (void)(It); 338bcb0991SDimitry Andric } 348bcb0991SDimitry Andric } 358bcb0991SDimitry Andric 368bcb0991SDimitry Andric // Trigger Speculative Compiles. 378bcb0991SDimitry Andric void Speculator::speculateForEntryPoint(Speculator *Ptr, uint64_t StubId) { 388bcb0991SDimitry Andric assert(Ptr && " Null Address Received in orc_speculate_for "); 3906c3fb27SDimitry Andric Ptr->speculateFor(ExecutorAddr(StubId)); 408bcb0991SDimitry Andric } 418bcb0991SDimitry Andric 428bcb0991SDimitry Andric Error Speculator::addSpeculationRuntime(JITDylib &JD, 438bcb0991SDimitry Andric MangleAndInterner &Mangle) { 4406c3fb27SDimitry Andric ExecutorSymbolDef ThisPtr(ExecutorAddr::fromPtr(this), 458bcb0991SDimitry Andric JITSymbolFlags::Exported); 4606c3fb27SDimitry Andric ExecutorSymbolDef SpeculateForEntryPtr( 4706c3fb27SDimitry Andric ExecutorAddr::fromPtr(&speculateForEntryPoint), JITSymbolFlags::Exported); 488bcb0991SDimitry Andric return JD.define(absoluteSymbols({ 498bcb0991SDimitry Andric {Mangle("__orc_speculator"), ThisPtr}, // Data Symbol 508bcb0991SDimitry Andric {Mangle("__orc_speculate_for"), SpeculateForEntryPtr} // Callable Symbol 518bcb0991SDimitry Andric })); 528bcb0991SDimitry Andric } 538bcb0991SDimitry Andric 548bcb0991SDimitry Andric // If two modules, share the same LLVMContext, different threads must 558bcb0991SDimitry Andric // not access them concurrently without locking the associated LLVMContext 568bcb0991SDimitry Andric // this implementation follows this contract. 57e8d8bef9SDimitry Andric void IRSpeculationLayer::emit(std::unique_ptr<MaterializationResponsibility> R, 588bcb0991SDimitry Andric ThreadSafeModule TSM) { 598bcb0991SDimitry Andric 608bcb0991SDimitry Andric assert(TSM && "Speculation Layer received Null Module ?"); 618bcb0991SDimitry Andric assert(TSM.getContext().getContext() != nullptr && 628bcb0991SDimitry Andric "Module with null LLVMContext?"); 638bcb0991SDimitry Andric 648bcb0991SDimitry Andric // Instrumentation of runtime calls, lock the Module 658bcb0991SDimitry Andric TSM.withModuleDo([this, &R](Module &M) { 668bcb0991SDimitry Andric auto &MContext = M.getContext(); 678bcb0991SDimitry Andric auto SpeculatorVTy = StructType::create(MContext, "Class.Speculator"); 688bcb0991SDimitry Andric auto RuntimeCallTy = FunctionType::get( 698bcb0991SDimitry Andric Type::getVoidTy(MContext), 70*5f757f3fSDimitry Andric {PointerType::getUnqual(MContext), Type::getInt64Ty(MContext)}, false); 718bcb0991SDimitry Andric auto RuntimeCall = 728bcb0991SDimitry Andric Function::Create(RuntimeCallTy, Function::LinkageTypes::ExternalLinkage, 738bcb0991SDimitry Andric "__orc_speculate_for", &M); 748bcb0991SDimitry Andric auto SpeclAddr = new GlobalVariable( 758bcb0991SDimitry Andric M, SpeculatorVTy, false, GlobalValue::LinkageTypes::ExternalLinkage, 768bcb0991SDimitry Andric nullptr, "__orc_speculator"); 778bcb0991SDimitry Andric 788bcb0991SDimitry Andric IRBuilder<> Mutator(MContext); 798bcb0991SDimitry Andric 808bcb0991SDimitry Andric // QueryAnalysis allowed to transform the IR source, one such example is 818bcb0991SDimitry Andric // Simplify CFG helps the static branch prediction heuristics! 828bcb0991SDimitry Andric for (auto &Fn : M.getFunctionList()) { 838bcb0991SDimitry Andric if (!Fn.isDeclaration()) { 848bcb0991SDimitry Andric 858bcb0991SDimitry Andric auto IRNames = QueryAnalysis(Fn); 868bcb0991SDimitry Andric // Instrument and register if Query has result 8781ad6265SDimitry Andric if (IRNames) { 888bcb0991SDimitry Andric 898bcb0991SDimitry Andric // Emit globals for each function. 908bcb0991SDimitry Andric auto LoadValueTy = Type::getInt8Ty(MContext); 918bcb0991SDimitry Andric auto SpeculatorGuard = new GlobalVariable( 928bcb0991SDimitry Andric M, LoadValueTy, false, GlobalValue::LinkageTypes::InternalLinkage, 938bcb0991SDimitry Andric ConstantInt::get(LoadValueTy, 0), 948bcb0991SDimitry Andric "__orc_speculate.guard.for." + Fn.getName()); 955ffd83dbSDimitry Andric SpeculatorGuard->setAlignment(Align(1)); 968bcb0991SDimitry Andric SpeculatorGuard->setUnnamedAddr(GlobalValue::UnnamedAddr::Local); 978bcb0991SDimitry Andric 988bcb0991SDimitry Andric BasicBlock &ProgramEntry = Fn.getEntryBlock(); 998bcb0991SDimitry Andric // Create BasicBlocks before the program's entry basicblock 1008bcb0991SDimitry Andric BasicBlock *SpeculateBlock = BasicBlock::Create( 1018bcb0991SDimitry Andric MContext, "__orc_speculate.block", &Fn, &ProgramEntry); 1028bcb0991SDimitry Andric BasicBlock *SpeculateDecisionBlock = BasicBlock::Create( 1038bcb0991SDimitry Andric MContext, "__orc_speculate.decision.block", &Fn, SpeculateBlock); 1048bcb0991SDimitry Andric 1058bcb0991SDimitry Andric assert(SpeculateDecisionBlock == &Fn.getEntryBlock() && 1068bcb0991SDimitry Andric "SpeculateDecisionBlock not updated?"); 1078bcb0991SDimitry Andric Mutator.SetInsertPoint(SpeculateDecisionBlock); 1088bcb0991SDimitry Andric 1098bcb0991SDimitry Andric auto LoadGuard = 1108bcb0991SDimitry Andric Mutator.CreateLoad(LoadValueTy, SpeculatorGuard, "guard.value"); 1118bcb0991SDimitry Andric // if just loaded value equal to 0,return true. 1128bcb0991SDimitry Andric auto CanSpeculate = 1138bcb0991SDimitry Andric Mutator.CreateICmpEQ(LoadGuard, ConstantInt::get(LoadValueTy, 0), 1148bcb0991SDimitry Andric "compare.to.speculate"); 1158bcb0991SDimitry Andric Mutator.CreateCondBr(CanSpeculate, SpeculateBlock, &ProgramEntry); 1168bcb0991SDimitry Andric 1178bcb0991SDimitry Andric Mutator.SetInsertPoint(SpeculateBlock); 1188bcb0991SDimitry Andric auto ImplAddrToUint = 1198bcb0991SDimitry Andric Mutator.CreatePtrToInt(&Fn, Type::getInt64Ty(MContext)); 1208bcb0991SDimitry Andric Mutator.CreateCall(RuntimeCallTy, RuntimeCall, 1218bcb0991SDimitry Andric {SpeclAddr, ImplAddrToUint}); 1228bcb0991SDimitry Andric Mutator.CreateStore(ConstantInt::get(LoadValueTy, 1), 1238bcb0991SDimitry Andric SpeculatorGuard); 1248bcb0991SDimitry Andric Mutator.CreateBr(&ProgramEntry); 1258bcb0991SDimitry Andric 1268bcb0991SDimitry Andric assert(Mutator.GetInsertBlock()->getParent() == &Fn && 1278bcb0991SDimitry Andric "IR builder association mismatch?"); 12881ad6265SDimitry Andric S.registerSymbols(internToJITSymbols(*IRNames), 129e8d8bef9SDimitry Andric &R->getTargetJITDylib()); 1308bcb0991SDimitry Andric } 1318bcb0991SDimitry Andric } 1328bcb0991SDimitry Andric } 1338bcb0991SDimitry Andric }); 1348bcb0991SDimitry Andric 1358bcb0991SDimitry Andric assert(!TSM.withModuleDo([](const Module &M) { return verifyModule(M); }) && 1368bcb0991SDimitry Andric "Speculation Instrumentation breaks IR?"); 1378bcb0991SDimitry Andric 1388bcb0991SDimitry Andric NextLayer.emit(std::move(R), std::move(TSM)); 1398bcb0991SDimitry Andric } 1408bcb0991SDimitry Andric 1418bcb0991SDimitry Andric } // namespace orc 1428bcb0991SDimitry Andric } // namespace llvm 143