Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:52

0001 //===- CompileUtils.h - Utilities for compiling IR in the JIT ---*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // Contains utilities for compiling IR to object files.
0010 //
0011 //===----------------------------------------------------------------------===//
0012 
0013 #ifndef LLVM_EXECUTIONENGINE_ORC_COMPILEUTILS_H
0014 #define LLVM_EXECUTIONENGINE_ORC_COMPILEUTILS_H
0015 
0016 #include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
0017 #include "llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h"
0018 #include "llvm/ExecutionEngine/Orc/Layer.h"
0019 #include <memory>
0020 
0021 namespace llvm {
0022 
0023 class MemoryBuffer;
0024 class Module;
0025 class ObjectCache;
0026 class TargetMachine;
0027 
0028 namespace orc {
0029 
0030 IRSymbolMapper::ManglingOptions
0031 irManglingOptionsFromTargetOptions(const TargetOptions &Opts);
0032 
0033 /// Simple compile functor: Takes a single IR module and returns an ObjectFile.
0034 /// This compiler supports a single compilation thread and LLVMContext only.
0035 /// For multithreaded compilation, use ConcurrentIRCompiler below.
0036 class SimpleCompiler : public IRCompileLayer::IRCompiler {
0037 public:
0038   using CompileResult = std::unique_ptr<MemoryBuffer>;
0039 
0040   /// Construct a simple compile functor with the given target.
0041   SimpleCompiler(TargetMachine &TM, ObjectCache *ObjCache = nullptr)
0042       : IRCompiler(irManglingOptionsFromTargetOptions(TM.Options)), TM(TM),
0043         ObjCache(ObjCache) {}
0044 
0045   /// Set an ObjectCache to query before compiling.
0046   void setObjectCache(ObjectCache *NewCache) { ObjCache = NewCache; }
0047 
0048   /// Compile a Module to an ObjectFile.
0049   Expected<CompileResult> operator()(Module &M) override;
0050 
0051 private:
0052   IRSymbolMapper::ManglingOptions
0053   manglingOptionsForTargetMachine(const TargetMachine &TM);
0054 
0055   CompileResult tryToLoadFromObjectCache(const Module &M);
0056   void notifyObjectCompiled(const Module &M, const MemoryBuffer &ObjBuffer);
0057 
0058   TargetMachine &TM;
0059   ObjectCache *ObjCache = nullptr;
0060 };
0061 
0062 /// A SimpleCompiler that owns its TargetMachine.
0063 ///
0064 /// This is convenient for clients who don't want to own their TargetMachines,
0065 /// e.g. LLJIT.
0066 class TMOwningSimpleCompiler : public SimpleCompiler {
0067 public:
0068   TMOwningSimpleCompiler(std::unique_ptr<TargetMachine> TM,
0069                          ObjectCache *ObjCache = nullptr)
0070       : SimpleCompiler(*TM, ObjCache), TM(std::move(TM)) {}
0071 
0072 private:
0073   // FIXME: shared because std::functions (and consequently
0074   // IRCompileLayer::CompileFunction) are not moveable.
0075   std::shared_ptr<llvm::TargetMachine> TM;
0076 };
0077 
0078 /// A thread-safe version of SimpleCompiler.
0079 ///
0080 /// This class creates a new TargetMachine and SimpleCompiler instance for each
0081 /// compile.
0082 class ConcurrentIRCompiler : public IRCompileLayer::IRCompiler {
0083 public:
0084   ConcurrentIRCompiler(JITTargetMachineBuilder JTMB,
0085                        ObjectCache *ObjCache = nullptr);
0086 
0087   void setObjectCache(ObjectCache *ObjCache) { this->ObjCache = ObjCache; }
0088 
0089   Expected<std::unique_ptr<MemoryBuffer>> operator()(Module &M) override;
0090 
0091 private:
0092   JITTargetMachineBuilder JTMB;
0093   ObjectCache *ObjCache = nullptr;
0094 };
0095 
0096 } // end namespace orc
0097 
0098 } // end namespace llvm
0099 
0100 #endif // LLVM_EXECUTIONENGINE_ORC_COMPILEUTILS_H