Skip to content

Commit 93de2a1

Browse files
committedJan 23, 2015
[Orc] New JIT APIs.
This patch adds a new set of JIT APIs to LLVM. The aim of these new APIs is to cleanly support a wider range of JIT use cases in LLVM, and encourage the development and contribution of re-usable infrastructure for LLVM JIT use-cases. These APIs are intended to live alongside the MCJIT APIs, and should not affect existing clients. Included in this patch: 1) New headers in include/llvm/ExecutionEngine/Orc that provide a set of components for building JIT infrastructure. Implementation code for these headers lives in lib/ExecutionEngine/Orc. 2) A prototype re-implementation of MCJIT (OrcMCJITReplacement) built out of the new components. 3) Minor changes to RTDyldMemoryManager needed to support the new components. These changes should not impact existing clients. 4) A new flag for lli, -use-orcmcjit, which will cause lli to use the OrcMCJITReplacement class as its underlying execution engine, rather than MCJIT itself. Tests to follow shortly. Special thanks to Michael Ilseman, Pete Cooper, David Blaikie, Eric Christopher, Justin Bogner, and Jim Grosbach for extensive feedback and discussion. llvm-svn: 226940
1 parent 4bd0a0c commit 93de2a1

29 files changed

+2428
-8
lines changed
 

‎llvm/include/llvm/ExecutionEngine/ExecutionEngine.h

+16
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,12 @@ class ExecutionEngine {
143143
std::string *ErrorStr,
144144
std::unique_ptr<RTDyldMemoryManager> MCJMM,
145145
std::unique_ptr<TargetMachine> TM);
146+
147+
static ExecutionEngine *(*OrcMCJITReplacementCtor)(
148+
std::string *ErrorStr,
149+
std::unique_ptr<RTDyldMemoryManager> OrcJMM,
150+
std::unique_ptr<TargetMachine> TM);
151+
146152
static ExecutionEngine *(*InterpCtor)(std::unique_ptr<Module> M,
147153
std::string *ErrorStr);
148154

@@ -464,6 +470,7 @@ class ExecutionEngine {
464470
}
465471

466472
protected:
473+
ExecutionEngine() : EEState(*this) {}
467474
explicit ExecutionEngine(std::unique_ptr<Module> M);
468475

469476
void emitGlobals();
@@ -501,11 +508,15 @@ class EngineBuilder {
501508
std::string MCPU;
502509
SmallVector<std::string, 4> MAttrs;
503510
bool VerifyModules;
511+
bool UseOrcMCJITReplacement;
504512

505513
/// InitEngine - Does the common initialization of default options.
506514
void InitEngine();
507515

508516
public:
517+
/// Default constructor for EngineBuilder.
518+
EngineBuilder();
519+
509520
/// Constructor for EngineBuilder.
510521
EngineBuilder(std::unique_ptr<Module> M);
511522

@@ -590,6 +601,11 @@ class EngineBuilder {
590601
return *this;
591602
}
592603

604+
// \brief Use OrcMCJITReplacement instead of MCJIT. Off by default.
605+
void setUseOrcMCJITReplacement(bool UseOrcMCJITReplacement) {
606+
this->UseOrcMCJITReplacement = UseOrcMCJITReplacement;
607+
}
608+
593609
TargetMachine *selectTarget();
594610

595611
/// selectTarget - Pick a target either via -march or by guessing the native
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//===- ObjectMemoryBuffer.h - SmallVector-backed MemoryBuffrer -*- C++ -*-===//
2+
//
3+
// The LLVM Compiler Infrastructure
4+
//
5+
// This file is distributed under the University of Illinois Open Source
6+
// License. See LICENSE.TXT for details.
7+
//
8+
//===----------------------------------------------------------------------===//
9+
//
10+
// This file declares a wrapper class to hold the memory into which an
11+
// object will be generated.
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
#ifndef LLVM_EXECUTIONENGINE_OBJECTMEMORYBUFFER_H
16+
#define LLVM_EXECUTIONENGINE_OBJECTMEMORYBUFFER_H
17+
18+
#include "llvm/ADT/SmallVector.h"
19+
#include "llvm/Support/MemoryBuffer.h"
20+
#include "llvm/Support/raw_ostream.h"
21+
22+
namespace llvm {
23+
24+
/// \brief SmallVector-backed MemoryBuffer instance.
25+
///
26+
/// This class enables efficient construction of MemoryBuffers from SmallVector
27+
/// instances. This is useful for MCJIT and Orc, where object files are streamed
28+
/// into SmallVectors, then inspected using ObjectFile (which takes a
29+
/// MemoryBuffer).
30+
class ObjectMemoryBuffer : public MemoryBuffer {
31+
public:
32+
33+
/// \brief Construct an ObjectMemoryBuffer from the given SmallVector r-value.
34+
///
35+
/// FIXME: It'd be nice for this to be a non-templated constructor taking a
36+
/// SmallVectorImpl here instead of a templated one taking a SmallVector<N>,
37+
/// but SmallVector's move-construction/assignment currently only take
38+
/// SmallVectors. If/when that is fixed we can simplify this constructor and
39+
/// the following one.
40+
ObjectMemoryBuffer(SmallVectorImpl<char> &&SV)
41+
: SV(std::move(SV)), BufferName("<in-memory object>") {
42+
init(this->SV.begin(), this->SV.end(), false);
43+
}
44+
45+
/// \brief Construct a named ObjectMemoryBuffer from the given SmallVector
46+
/// r-value and StringRef.
47+
ObjectMemoryBuffer(SmallVectorImpl<char> &&SV, StringRef Name)
48+
: SV(std::move(SV)), BufferName(Name) {
49+
init(this->SV.begin(), this->SV.end(), false);
50+
}
51+
52+
const char* getBufferIdentifier() const override { return BufferName.c_str(); }
53+
54+
BufferKind getBufferKind() const override { return MemoryBuffer_Malloc; }
55+
56+
private:
57+
SmallVector<char, 0> SV;
58+
std::string BufferName;
59+
};
60+
61+
} // namespace llvm
62+
63+
#endif
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//===-- CloneSubModule.h - Utilities for extracting sub-modules -*- C++ -*-===//
2+
//
3+
// The LLVM Compiler Infrastructure
4+
//
5+
// This file is distributed under the University of Illinois Open Source
6+
// License. See LICENSE.TXT for details.
7+
//
8+
//===----------------------------------------------------------------------===//
9+
//
10+
// Contains utilities for extracting sub-modules. Useful for breaking up modules
11+
// for lazy jitting.
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
#ifndef LLVM_EXECUTIONENGINE_ORC_CLONESUBMODULE_H
16+
#define LLVM_EXECUTIONENGINE_ORC_CLONESUBMODULE_H
17+
18+
#include "llvm/Transforms/Utils/ValueMapper.h"
19+
#include <functional>
20+
21+
namespace llvm {
22+
23+
class Function;
24+
class GlobalVariable;
25+
class Module;
26+
27+
typedef std::function<void(GlobalVariable &, const GlobalVariable &,
28+
ValueToValueMapTy &)> HandleGlobalVariableFtor;
29+
30+
typedef std::function<void(Function &, const Function &, ValueToValueMapTy &)>
31+
HandleFunctionFtor;
32+
33+
void copyGVInitializer(GlobalVariable &New, const GlobalVariable &Orig,
34+
ValueToValueMapTy &VMap);
35+
36+
void copyFunctionBody(Function &New, const Function &Orig,
37+
ValueToValueMapTy &VMap);
38+
39+
std::unique_ptr<Module>
40+
CloneSubModule(const Module &M, HandleGlobalVariableFtor HandleGlobalVariable,
41+
HandleFunctionFtor HandleFunction, bool KeepInlineAsm);
42+
}
43+
44+
#endif // LLVM_EXECUTIONENGINE_ORC_CLONESUBMODULE_H

0 commit comments

Comments
 (0)