blob: 158592544eabe77aed020790c13300e13bc69766 [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
Andrew Scull0372a572018-11-16 15:47:06 +000016#include "llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h"
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010017#include <memory>
18
19namespace llvm {
20
Andrew Walbran3d2c1972020-04-07 12:24:26 +010021class JITTargetMachineBuilder;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010022class MCContext;
Andrew Walbran3d2c1972020-04-07 12:24:26 +010023class MemoryBuffer;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010024class Module;
Andrew Walbran3d2c1972020-04-07 12:24:26 +010025class ObjectCache;
26class TargetMachine;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010027
28namespace orc {
29
Andrew Scullcdfcccc2018-10-05 20:58:37 +010030/// Simple compile functor: Takes a single IR module and returns an ObjectFile.
31/// This compiler supports a single compilation thread and LLVMContext only.
Andrew Walbran16937d02019-10-22 13:54:20 +010032/// For multithreaded compilation, use ConcurrentIRCompiler below.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010033class SimpleCompiler {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010034public:
35 using CompileResult = std::unique_ptr<MemoryBuffer>;
36
Andrew Scullcdfcccc2018-10-05 20:58:37 +010037 /// Construct a simple compile functor with the given target.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010038 SimpleCompiler(TargetMachine &TM, ObjectCache *ObjCache = nullptr)
39 : TM(TM), ObjCache(ObjCache) {}
40
Andrew Scullcdfcccc2018-10-05 20:58:37 +010041 /// Set an ObjectCache to query before compiling.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010042 void setObjectCache(ObjectCache *NewCache) { ObjCache = NewCache; }
43
Andrew Scullcdfcccc2018-10-05 20:58:37 +010044 /// Compile a Module to an ObjectFile.
Andrew Walbran3d2c1972020-04-07 12:24:26 +010045 CompileResult operator()(Module &M);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010046
47private:
Andrew Walbran3d2c1972020-04-07 12:24:26 +010048 CompileResult tryToLoadFromObjectCache(const Module &M);
49 void notifyObjectCompiled(const Module &M, const MemoryBuffer &ObjBuffer);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010050
51 TargetMachine &TM;
52 ObjectCache *ObjCache = nullptr;
53};
54
Andrew Scullcdfcccc2018-10-05 20:58:37 +010055/// A thread-safe version of SimpleCompiler.
56///
57/// This class creates a new TargetMachine and SimpleCompiler instance for each
58/// compile.
Andrew Walbran16937d02019-10-22 13:54:20 +010059class ConcurrentIRCompiler {
Andrew Scullcdfcccc2018-10-05 20:58:37 +010060public:
Andrew Walbran16937d02019-10-22 13:54:20 +010061 ConcurrentIRCompiler(JITTargetMachineBuilder JTMB,
Andrew Walbran3d2c1972020-04-07 12:24:26 +010062 ObjectCache *ObjCache = nullptr);
Andrew Scullcdfcccc2018-10-05 20:58:37 +010063
64 void setObjectCache(ObjectCache *ObjCache) { this->ObjCache = ObjCache; }
65
Andrew Walbran3d2c1972020-04-07 12:24:26 +010066 std::unique_ptr<MemoryBuffer> operator()(Module &M);
Andrew Scullcdfcccc2018-10-05 20:58:37 +010067
68private:
69 JITTargetMachineBuilder JTMB;
70 ObjectCache *ObjCache = nullptr;
71};
72
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010073} // end namespace orc
74
75} // end namespace llvm
76
77#endif // LLVM_EXECUTIONENGINE_ORC_COMPILEUTILS_H