LLVM  14.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 
11 #include "llvm/ADT/SmallVector.h"
14 #include "llvm/IR/Module.h"
15 #include "llvm/Object/ObjectFile.h"
16 #include "llvm/Support/Error.h"
21 
22 #include <algorithm>
23 
24 namespace llvm {
25 namespace orc {
26 
27 IRSymbolMapper::ManglingOptions
30 
31  MO.EmulatedTLS = Opts.EmulatedTLS;
32 
33  return MO;
34 }
35 
36 /// Compile a Module to an ObjectFile.
38  CompileResult CachedObject = tryToLoadFromObjectCache(M);
39  if (CachedObject)
40  return std::move(CachedObject);
41 
42  SmallVector<char, 0> ObjBufferSV;
43 
44  {
45  raw_svector_ostream ObjStream(ObjBufferSV);
46 
48  MCContext *Ctx;
49  if (TM.addPassesToEmitMC(PM, Ctx, ObjStream))
50  return make_error<StringError>("Target does not support MC emission",
52  PM.run(M);
53  }
54 
55  auto ObjBuffer = std::make_unique<SmallVectorMemoryBuffer>(
56  std::move(ObjBufferSV), M.getModuleIdentifier() + "-jitted-objectbuffer");
57 
58  auto Obj = object::ObjectFile::createObjectFile(ObjBuffer->getMemBufferRef());
59 
60  if (!Obj)
61  return Obj.takeError();
62 
63  notifyObjectCompiled(M, *ObjBuffer);
64  return std::move(ObjBuffer);
65 }
66 
68 SimpleCompiler::tryToLoadFromObjectCache(const Module &M) {
69  if (!ObjCache)
70  return CompileResult();
71 
72  return ObjCache->getObject(&M);
73 }
74 
75 void SimpleCompiler::notifyObjectCompiled(const Module &M,
76  const MemoryBuffer &ObjBuffer) {
77  if (ObjCache)
78  ObjCache->notifyObjectCompiled(&M, ObjBuffer.getMemBufferRef());
79 }
80 
82  ObjectCache *ObjCache)
83  : IRCompiler(irManglingOptionsFromTargetOptions(JTMB.getOptions())),
84  JTMB(std::move(JTMB)), ObjCache(ObjCache) {}
85 
88  auto TM = cantFail(JTMB.createTargetMachine());
89  SimpleCompiler C(*TM, ObjCache);
90  return C(M);
91 }
92 
93 } // end namespace orc
94 } // end namespace llvm
MemoryBuffer.h
llvm::orc::ConcurrentIRCompiler::operator()
Expected< std::unique_ptr< MemoryBuffer > > operator()(Module &M) override
Definition: CompileUtils.cpp:87
llvm::orc::JITTargetMachineBuilder
A utility class for building TargetMachines for JITs.
Definition: JITTargetMachineBuilder.h:34
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
M
We currently emits eax Perhaps this is what we really should generate is Is imull three or four cycles eax eax The current instruction priority is based on pattern complexity The former is more complex because it folds a load so the latter will not be emitted Perhaps we should use AddedComplexity to give LEA32r a higher priority We should always try to match LEA first since the LEA matching code does some estimate to determine whether the match is profitable if we care more about code then imull is better It s two bytes shorter than movl leal On a Pentium M
Definition: README.txt:252
llvm::orc::JITTargetMachineBuilder::createTargetMachine
Expected< std::unique_ptr< TargetMachine > > createTargetMachine()
Create a TargetMachine.
Definition: JITTargetMachineBuilder.cpp:43
llvm::TargetOptions
Definition: TargetOptions.h:124
llvm::orc::ConcurrentIRCompiler::ConcurrentIRCompiler
ConcurrentIRCompiler(JITTargetMachineBuilder JTMB, ObjectCache *ObjCache=nullptr)
Definition: CompileUtils.cpp:81
llvm::MCContext
Context object for machine code objects.
Definition: MCContext.h:72
llvm::ObjectCache::notifyObjectCompiled
virtual void notifyObjectCompiled(const Module *M, MemoryBufferRef Obj)=0
notifyObjectCompiled - Provides a pointer to compiled code for Module M.
llvm::SmallVector< char, 0 >
ErrorHandling.h
SmallVectorMemoryBuffer.h
Error.h
Module.h
llvm::ObjectCache
This is the base ObjectCache type which can be provided to an ExecutionEngine for the purpose of avoi...
Definition: ObjectCache.h:23
llvm::orc::SimpleCompiler::CompileResult
std::unique_ptr< MemoryBuffer > CompileResult
Definition: CompileUtils.h:39
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:42
LegacyPassManager.h
llvm::MemoryBuffer
This interface provides simple read-only access to a block of memory, and provides simple methods for...
Definition: MemoryBuffer.h:50
llvm::orc::SimpleCompiler
Simple compile functor: Takes a single IR module and returns an ObjectFile.
Definition: CompileUtils.h:37
llvm::ObjectCache::getObject
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...
ObjectCache.h
TargetMachine.h
C
(vector float) vec_cmpeq(*A, *B) C
Definition: README_ALTIVEC.txt:86
llvm::TargetOptions::EmulatedTLS
unsigned EmulatedTLS
EmulatedTLS - This flag enables emulated TLS model, using emutls function in the runtime library.
Definition: TargetOptions.h:279
llvm::TargetMachine::addPassesToEmitMC
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.
Definition: TargetMachine.h:366
llvm::orc::irManglingOptionsFromTargetOptions
IRSymbolMapper::ManglingOptions irManglingOptionsFromTargetOptions(const TargetOptions &Opts)
Definition: CompileUtils.cpp:28
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
llvm::MemoryBuffer::getMemBufferRef
MemoryBufferRef getMemBufferRef() const
Definition: MemoryBuffer.cpp:540
llvm::object::ObjectFile::createObjectFile
static Expected< OwningBinary< ObjectFile > > createObjectFile(StringRef ObjectPath)
Definition: ObjectFile.cpp:185
llvm::move
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:1605
ObjectFile.h
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
llvm::orc::IRSymbolMapper::ManglingOptions::EmulatedTLS
bool EmulatedTLS
Definition: Mangling.h:42
llvm::cantFail
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
Definition: Error.h:737
llvm::orc::IRSymbolMapper::ManglingOptions
Definition: Mangling.h:41
std
Definition: BitVector.h:838
llvm::inconvertibleErrorCode
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition: Error.cpp:77
llvm::orc::SimpleCompiler::operator()
Expected< CompileResult > operator()(Module &M) override
Compile a Module to an ObjectFile.
Definition: CompileUtils.cpp:37
llvm::legacy::PassManager
PassManager manages ModulePassManagers.
Definition: LegacyPassManager.h:52
SmallVector.h
llvm::raw_svector_ostream
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:658
CompileUtils.h
llvm::legacy::PassManager::run
bool run(Module &M)
run - Execute all of the passes scheduled for execution.
Definition: LegacyPassManager.cpp:1680
TM
const char LLVMTargetMachineRef TM
Definition: PassBuilderBindings.cpp:47