Path: blob/main/contrib/llvm-project/llvm/lib/ExecutionEngine/Orc/Speculation.cpp
35266 views
//===---------- speculation.cpp - Utilities for Speculation ----------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "llvm/ExecutionEngine/Orc/Speculation.h"9#include "llvm/IR/BasicBlock.h"10#include "llvm/IR/Function.h"11#include "llvm/IR/IRBuilder.h"12#include "llvm/IR/Instruction.h"13#include "llvm/IR/Instructions.h"14#include "llvm/IR/LLVMContext.h"15#include "llvm/IR/Module.h"16#include "llvm/IR/Type.h"17#include "llvm/IR/Verifier.h"1819namespace llvm {2021namespace orc {2223// ImplSymbolMap methods24void ImplSymbolMap::trackImpls(SymbolAliasMap ImplMaps, JITDylib *SrcJD) {25assert(SrcJD && "Tracking on Null Source .impl dylib");26std::lock_guard<std::mutex> Lockit(ConcurrentAccess);27for (auto &I : ImplMaps) {28auto It = Maps.insert({I.first, {I.second.Aliasee, SrcJD}});29// check rationale when independent dylibs have same symbol name?30assert(It.second && "ImplSymbols are already tracked for this Symbol?");31(void)(It);32}33}3435// Trigger Speculative Compiles.36void Speculator::speculateForEntryPoint(Speculator *Ptr, uint64_t StubId) {37assert(Ptr && " Null Address Received in orc_speculate_for ");38Ptr->speculateFor(ExecutorAddr(StubId));39}4041Error Speculator::addSpeculationRuntime(JITDylib &JD,42MangleAndInterner &Mangle) {43ExecutorSymbolDef ThisPtr(ExecutorAddr::fromPtr(this),44JITSymbolFlags::Exported);45ExecutorSymbolDef SpeculateForEntryPtr(46ExecutorAddr::fromPtr(&speculateForEntryPoint), JITSymbolFlags::Exported);47return JD.define(absoluteSymbols({48{Mangle("__orc_speculator"), ThisPtr}, // Data Symbol49{Mangle("__orc_speculate_for"), SpeculateForEntryPtr} // Callable Symbol50}));51}5253// If two modules, share the same LLVMContext, different threads must54// not access them concurrently without locking the associated LLVMContext55// this implementation follows this contract.56void IRSpeculationLayer::emit(std::unique_ptr<MaterializationResponsibility> R,57ThreadSafeModule TSM) {5859assert(TSM && "Speculation Layer received Null Module ?");60assert(TSM.getContext().getContext() != nullptr &&61"Module with null LLVMContext?");6263// Instrumentation of runtime calls, lock the Module64TSM.withModuleDo([this, &R](Module &M) {65auto &MContext = M.getContext();66auto SpeculatorVTy = StructType::create(MContext, "Class.Speculator");67auto RuntimeCallTy = FunctionType::get(68Type::getVoidTy(MContext),69{PointerType::getUnqual(MContext), Type::getInt64Ty(MContext)}, false);70auto RuntimeCall =71Function::Create(RuntimeCallTy, Function::LinkageTypes::ExternalLinkage,72"__orc_speculate_for", &M);73auto SpeclAddr = new GlobalVariable(74M, SpeculatorVTy, false, GlobalValue::LinkageTypes::ExternalLinkage,75nullptr, "__orc_speculator");7677IRBuilder<> Mutator(MContext);7879// QueryAnalysis allowed to transform the IR source, one such example is80// Simplify CFG helps the static branch prediction heuristics!81for (auto &Fn : M.getFunctionList()) {82if (!Fn.isDeclaration()) {8384auto IRNames = QueryAnalysis(Fn);85// Instrument and register if Query has result86if (IRNames) {8788// Emit globals for each function.89auto LoadValueTy = Type::getInt8Ty(MContext);90auto SpeculatorGuard = new GlobalVariable(91M, LoadValueTy, false, GlobalValue::LinkageTypes::InternalLinkage,92ConstantInt::get(LoadValueTy, 0),93"__orc_speculate.guard.for." + Fn.getName());94SpeculatorGuard->setAlignment(Align(1));95SpeculatorGuard->setUnnamedAddr(GlobalValue::UnnamedAddr::Local);9697BasicBlock &ProgramEntry = Fn.getEntryBlock();98// Create BasicBlocks before the program's entry basicblock99BasicBlock *SpeculateBlock = BasicBlock::Create(100MContext, "__orc_speculate.block", &Fn, &ProgramEntry);101BasicBlock *SpeculateDecisionBlock = BasicBlock::Create(102MContext, "__orc_speculate.decision.block", &Fn, SpeculateBlock);103104assert(SpeculateDecisionBlock == &Fn.getEntryBlock() &&105"SpeculateDecisionBlock not updated?");106Mutator.SetInsertPoint(SpeculateDecisionBlock);107108auto LoadGuard =109Mutator.CreateLoad(LoadValueTy, SpeculatorGuard, "guard.value");110// if just loaded value equal to 0,return true.111auto CanSpeculate =112Mutator.CreateICmpEQ(LoadGuard, ConstantInt::get(LoadValueTy, 0),113"compare.to.speculate");114Mutator.CreateCondBr(CanSpeculate, SpeculateBlock, &ProgramEntry);115116Mutator.SetInsertPoint(SpeculateBlock);117auto ImplAddrToUint =118Mutator.CreatePtrToInt(&Fn, Type::getInt64Ty(MContext));119Mutator.CreateCall(RuntimeCallTy, RuntimeCall,120{SpeclAddr, ImplAddrToUint});121Mutator.CreateStore(ConstantInt::get(LoadValueTy, 1),122SpeculatorGuard);123Mutator.CreateBr(&ProgramEntry);124125assert(Mutator.GetInsertBlock()->getParent() == &Fn &&126"IR builder association mismatch?");127S.registerSymbols(internToJITSymbols(*IRNames),128&R->getTargetJITDylib());129}130}131}132});133134assert(!TSM.withModuleDo([](const Module &M) { return verifyModule(M); }) &&135"Speculation Instrumentation breaks IR?");136137NextLayer.emit(std::move(R), std::move(TSM));138}139140} // namespace orc141} // namespace llvm142143144