LLVM 20.0.0git
CompileUtils.cpp
Go to the documentation of this file.
1//===------ CompileUtils.cpp - Utilities for compiling IR in the JIT ------===//
2//
3// 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
6//
7//===----------------------------------------------------------------------===//
8
10
14#include "llvm/IR/Module.h"
15#include "llvm/MC/MCContext.h"
17#include "llvm/Support/Error.h"
21
22namespace llvm {
23namespace orc {
24
25IRSymbolMapper::ManglingOptions
28
29 MO.EmulatedTLS = Opts.EmulatedTLS;
30
31 return MO;
32}
33
34/// Compile a Module to an ObjectFile.
36 CompileResult CachedObject = tryToLoadFromObjectCache(M);
37 if (CachedObject)
38 return std::move(CachedObject);
39
40 SmallVector<char, 0> ObjBufferSV;
41
42 {
43 raw_svector_ostream ObjStream(ObjBufferSV);
44
46 MCContext *Ctx;
47 if (TM.addPassesToEmitMC(PM, Ctx, ObjStream))
48 return make_error<StringError>("Target does not support MC emission",
50 PM.run(M);
51 }
52
53 auto ObjBuffer = std::make_unique<SmallVectorMemoryBuffer>(
54 std::move(ObjBufferSV), M.getModuleIdentifier() + "-jitted-objectbuffer",
55 /*RequiresNullTerminator=*/false);
56
57 auto Obj = object::ObjectFile::createObjectFile(ObjBuffer->getMemBufferRef());
58
59 if (!Obj)
60 return Obj.takeError();
61
62 notifyObjectCompiled(M, *ObjBuffer);
63 return std::move(ObjBuffer);
64}
65
67SimpleCompiler::tryToLoadFromObjectCache(const Module &M) {
68 if (!ObjCache)
69 return CompileResult();
70
71 return ObjCache->getObject(&M);
72}
73
74void SimpleCompiler::notifyObjectCompiled(const Module &M,
75 const MemoryBuffer &ObjBuffer) {
76 if (ObjCache)
77 ObjCache->notifyObjectCompiled(&M, ObjBuffer.getMemBufferRef());
78}
79
81 ObjectCache *ObjCache)
82 : IRCompiler(irManglingOptionsFromTargetOptions(JTMB.getOptions())),
83 JTMB(std::move(JTMB)), ObjCache(ObjCache) {}
84
87 auto TM = cantFail(JTMB.createTargetMachine());
88 SimpleCompiler C(*TM, ObjCache);
89 return C(M);
90}
91
92} // end namespace orc
93} // end namespace llvm
Module.h This file contains the declarations for the Module class.
This file defines the SmallVector class.
Tagged union holding either a T or a Error.
Definition: Error.h:481
Context object for machine code objects.
Definition: MCContext.h:83
This interface provides simple read-only access to a block of memory, and provides simple methods for...
Definition: MemoryBuffer.h:51
MemoryBufferRef getMemBufferRef() const
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
This is the base ObjectCache type which can be provided to an ExecutionEngine for the purpose of avoi...
Definition: ObjectCache.h:23
virtual std::unique_ptr< MemoryBuffer > getObject(const Module *M)=0
Returns a pointer to a newly allocated MemoryBuffer that contains the object which corresponds with M...
virtual void notifyObjectCompiled(const Module *M, MemoryBufferRef Obj)=0
notifyObjectCompiled - Provides a pointer to compiled code for Module M.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1196
virtual bool addPassesToEmitMC(PassManagerBase &, MCContext *&, raw_pwrite_stream &, bool=true)
Add passes to the specified pass manager to get machine code emitted with the MCJIT.
unsigned EmulatedTLS
EmulatedTLS - This flag enables emulated TLS model, using emutls function in the runtime library.
PassManager manages ModulePassManagers.
bool run(Module &M)
run - Execute all of the passes scheduled for execution.
static Expected< OwningBinary< ObjectFile > > createObjectFile(StringRef ObjectPath)
Definition: ObjectFile.cpp:209
ConcurrentIRCompiler(JITTargetMachineBuilder JTMB, ObjectCache *ObjCache=nullptr)
Expected< std::unique_ptr< MemoryBuffer > > operator()(Module &M) override
A utility class for building TargetMachines for JITs.
Expected< std::unique_ptr< TargetMachine > > createTargetMachine()
Create a TargetMachine.
Simple compile functor: Takes a single IR module and returns an ObjectFile.
Definition: CompileUtils.h:36
std::unique_ptr< MemoryBuffer > CompileResult
Definition: CompileUtils.h:38
Expected< CompileResult > operator()(Module &M) override
Compile a Module to an ObjectFile.
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:691
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
IRSymbolMapper::ManglingOptions irManglingOptionsFromTargetOptions(const TargetOptions &Opts)
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition: Error.cpp:98
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
Definition: Error.h:756
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1873
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858