//===--- LLJITWithThinLTOSummaries.cpp - Module summaries as LLJIT input --===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // // In this example we will use a module summary index file produced for ThinLTO // to (A) find the module that defines the main entry point and (B) find all // extra modules that we need. We will do this in five steps: // // (1) Read the index file and parse the module summary index. // (2) Find the path of the module that defines "main". // (3) Parse the main module and create a matching LLJIT. // (4) Add all modules to the LLJIT that are covered by the index. // (5) Look up and run the JIT'd function. // // The index file name must be passed in as command line argument. Please find // this test for instructions on creating the index file: // // llvm/test/Examples/OrcV2Examples/lljit-with-thinlto-summaries.test // // If you use "build" as the build directory, you can run the test from the root // of the monorepo like this: // // > build/bin/llvm-lit -a \ // llvm/test/Examples/OrcV2Examples/lljit-with-thinlto-summaries.test // //===----------------------------------------------------------------------===// #include "llvm/ADT/STLExtras.h" #include "llvm/ADT/StringRef.h" #include "llvm/Bitcode/BitcodeReader.h" #include "llvm/ExecutionEngine/Orc/Core.h" #include "llvm/ExecutionEngine/Orc/ExecutionUtils.h" #include "llvm/ExecutionEngine/Orc/LLJIT.h" #include "llvm/ExecutionEngine/Orc/ThreadSafeModule.h" #include "llvm/ExecutionEngine/Orc/TargetProcess/TargetExecutionUtils.h" #include "llvm/IR/GlobalValue.h" #include "llvm/IR/LLVMContext.h" #include "llvm/IR/ModuleSummaryIndex.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/Error.h" #include "llvm/Support/InitLLVM.h" #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/TargetSelect.h" #include "llvm/Support/raw_ostream.h" #include <string> #include <system_error> #include <vector> usingnamespacellvm; usingnamespacellvm::orc; // Path of the module summary index file. cl::opt<std::string> IndexFile{ … }; // Describe a fail state that is caused by the given ModuleSummaryIndex // providing multiple definitions of the given global value name. It will dump // name and GUID for the global value and list the paths of the modules covered // by the index. class DuplicateDefinitionInSummary : public ErrorInfo<DuplicateDefinitionInSummary> { … }; // Describe a fail state where the given global value name was not found in the // given ModuleSummaryIndex. It will dump name and GUID for the global value and // list the paths of the modules covered by the index. class DefinitionNotFoundInSummary : public ErrorInfo<DefinitionNotFoundInSummary> { … }; char DuplicateDefinitionInSummary::ID = …; char DefinitionNotFoundInSummary::ID = …; // Lookup the a function in the ModuleSummaryIndex and return the path of the // module that defines it. Paths in the ModuleSummaryIndex are relative to the // build directory of the covered modules. Expected<StringRef> getMainModulePath(StringRef FunctionName, ModuleSummaryIndex &Index) { … } // Parse the bitcode module from the given path into a ThreadSafeModule. Expected<ThreadSafeModule> loadModule(StringRef Path, orc::ThreadSafeContext TSCtx) { … } int main(int Argc, char *Argv[]) { … }