blob: c7ba57228ab711eaaeb5e14b6d94ded3a525de53 [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- CompileUtils.h - Utilities for compiling IR in the JIT ---*- C++ -*-===//
2//
Andrew Walbran16937d02019-10-22 13:54:20 +01003// 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
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01006//
7//===----------------------------------------------------------------------===//
8//
9// Contains utilities for compiling IR to object files.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_EXECUTIONENGINE_ORC_COMPILEUTILS_H
14#define LLVM_EXECUTIONENGINE_ORC_COMPILEUTILS_H
15
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020016#include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
Andrew Scull0372a572018-11-16 15:47:06 +000017#include "llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h"
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020018#include "llvm/ExecutionEngine/Orc/Layer.h"
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010019#include <memory>
20
21namespace llvm {
22
23class MCContext;
Andrew Walbran3d2c1972020-04-07 12:24:26 +010024class MemoryBuffer;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010025class Module;
Andrew Walbran3d2c1972020-04-07 12:24:26 +010026class ObjectCache;
27class TargetMachine;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010028
29namespace orc {
30
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020031IRSymbolMapper::ManglingOptions
32irManglingOptionsFromTargetOptions(const TargetOptions &Opts);
33
Andrew Scullcdfcccc2018-10-05 20:58:37 +010034/// Simple compile functor: Takes a single IR module and returns an ObjectFile.
35/// This compiler supports a single compilation thread and LLVMContext only.
Andrew Walbran16937d02019-10-22 13:54:20 +010036/// For multithreaded compilation, use ConcurrentIRCompiler below.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020037class SimpleCompiler : public IRCompileLayer::IRCompiler {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010038public:
39 using CompileResult = std::unique_ptr<MemoryBuffer>;
40
Andrew Scullcdfcccc2018-10-05 20:58:37 +010041 /// Construct a simple compile functor with the given target.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010042 SimpleCompiler(TargetMachine &TM, ObjectCache *ObjCache = nullptr)
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020043 : IRCompiler(irManglingOptionsFromTargetOptions(TM.Options)), TM(TM),
44 ObjCache(ObjCache) {}
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010045
Andrew Scullcdfcccc2018-10-05 20:58:37 +010046 /// Set an ObjectCache to query before compiling.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010047 void setObjectCache(ObjectCache *NewCache) { ObjCache = NewCache; }
48
Andrew Scullcdfcccc2018-10-05 20:58:37 +010049 /// Compile a Module to an ObjectFile.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020050 Expected<CompileResult> operator()(Module &M) override;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010051
52private:
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020053 IRSymbolMapper::ManglingOptions
54 manglingOptionsForTargetMachine(const TargetMachine &TM);
55
Andrew Walbran3d2c1972020-04-07 12:24:26 +010056 CompileResult tryToLoadFromObjectCache(const Module &M);
57 void notifyObjectCompiled(const Module &M, const MemoryBuffer &ObjBuffer);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010058
59 TargetMachine &TM;
60 ObjectCache *ObjCache = nullptr;
61};
62
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020063/// A SimpleCompiler that owns its TargetMachine.
64///
65/// This convenient for clients who don't want to own their TargetMachines,
66/// e.g. LLJIT.
67class TMOwningSimpleCompiler : public SimpleCompiler {
68public:
69 TMOwningSimpleCompiler(std::unique_ptr<TargetMachine> TM,
70 ObjectCache *ObjCache = nullptr)
71 : SimpleCompiler(*TM, ObjCache), TM(std::move(TM)) {}
72
73private:
74 // FIXME: shared because std::functions (and consequently
75 // IRCompileLayer::CompileFunction) are not moveable.
76 std::shared_ptr<llvm::TargetMachine> TM;
77};
78
Andrew Scullcdfcccc2018-10-05 20:58:37 +010079/// A thread-safe version of SimpleCompiler.
80///
81/// This class creates a new TargetMachine and SimpleCompiler instance for each
82/// compile.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020083class ConcurrentIRCompiler : public IRCompileLayer::IRCompiler {
Andrew Scullcdfcccc2018-10-05 20:58:37 +010084public:
Andrew Walbran16937d02019-10-22 13:54:20 +010085 ConcurrentIRCompiler(JITTargetMachineBuilder JTMB,
Andrew Walbran3d2c1972020-04-07 12:24:26 +010086 ObjectCache *ObjCache = nullptr);
Andrew Scullcdfcccc2018-10-05 20:58:37 +010087
88 void setObjectCache(ObjectCache *ObjCache) { this->ObjCache = ObjCache; }
89
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020090 Expected<std::unique_ptr<MemoryBuffer>> operator()(Module &M) override;
Andrew Scullcdfcccc2018-10-05 20:58:37 +010091
92private:
93 JITTargetMachineBuilder JTMB;
94 ObjectCache *ObjCache = nullptr;
95};
96
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010097} // end namespace orc
98
99} // end namespace llvm
100
101#endif // LLVM_EXECUTIONENGINE_ORC_COMPILEUTILS_H