LLVM 19.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"
22
23#include <algorithm>
24
25namespace llvm {
26namespace orc {
27
28IRSymbolMapper::ManglingOptions
31
32 MO.EmulatedTLS = Opts.EmulatedTLS;
33
34 return MO;
35}
36
37/// Compile a Module to an ObjectFile.
39 CompileResult CachedObject = tryToLoadFromObjectCache(M);
40 if (CachedObject)
41 return std::move(CachedObject);
42
43 SmallVector<char, 0> ObjBufferSV;
44
45 {
46 raw_svector_ostream ObjStream(ObjBufferSV);
47
49 MCContext *Ctx;
50 if (TM.addPassesToEmitMC(PM, Ctx, ObjStream))
51 return make_error<StringError>("Target does not support MC emission",
53 PM.run(M);
54 }
55
56 auto ObjBuffer = std::make_unique<SmallVectorMemoryBuffer>(
57 std::move(ObjBufferSV), M.getModuleIdentifier() + "-jitted-objectbuffer",
58 /*RequiresNullTerminator=*/false);
59
60 auto Obj = object::ObjectFile::createObjectFile(ObjBuffer->getMemBufferRef());
61
62 if (!Obj)
63 return Obj.takeError();
64
65 notifyObjectCompiled(M, *ObjBuffer);
66 return std::move(ObjBuffer);
67}
68
70SimpleCompiler::tryToLoadFromObjectCache(const Module &M) {
71 if (!ObjCache)
72 return CompileResult();
73
74 return ObjCache->getObject(&M);
75}
76
77void SimpleCompiler::notifyObjectCompiled(const Module &M,
78 const MemoryBuffer &ObjBuffer) {
79 if (ObjCache)
80 ObjCache->notifyObjectCompiled(&M, ObjBuffer.getMemBufferRef());
81}
82
84 ObjectCache *ObjCache)
85 : IRCompiler(irManglingOptionsFromTargetOptions(JTMB.getOptions())),
86 JTMB(std::move(JTMB)), ObjCache(ObjCache) {}
87
90 auto TM = cantFail(JTMB.createTargetMachine());
91 SimpleCompiler C(*TM, ObjCache);
92 return C(M);
93}
94
95} // end namespace orc
96} // end namespace llvm
Module.h This file contains the declarations for the Module class.
const char LLVMTargetMachineRef TM
This file defines the SmallVector class.
Tagged union holding either a T or a Error.
Definition: Error.h:474
Context object for machine code objects.
Definition: MCContext.h:81
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:1209
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:690
@ 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:90
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
Definition: Error.h:749
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:1849
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858