LLVM  10.0.0
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 IRMaterializationUnit::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
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
Definition: Error.h:703
uint64_t CallInst * C
static Expected< OwningBinary< ObjectFile > > createObjectFile(StringRef ObjectPath)
Definition: ObjectFile.cpp:175
This class represents lattice values for constants.
Definition: AllocatorList.h:23
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.
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:66
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...
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:543
Definition: BitVector.h:937
Tagged union holding either a T or a Error.
Definition: APFloat.h:41
Simple compile functor: Takes a single IR module and returns an ObjectFile.
Definition: CompileUtils.h:39
Context object for machine code objects.
Definition: MCContext.h:65
Expected< CompileResult > operator()(Module &M) override
Compile a Module to an ObjectFile.
PassManager manages ModulePassManagers.
Expected< std::unique_ptr< MemoryBuffer > > operator()(Module &M) override
bool run(Module &M)
run - Execute all of the passes scheduled for execution.
const std::string & getModuleIdentifier() const
Get the module identifier which is, essentially, the name of the module.
Definition: Module.h:211
virtual void notifyObjectCompiled(const Module *M, MemoryBufferRef Obj)=0
notifyObjectCompiled - Provides a pointer to compiled code for Module M.
ConcurrentIRCompiler(JITTargetMachineBuilder JTMB, ObjectCache *ObjCache=nullptr)
Module.h This file contains the declarations for the Module class.
MemoryBufferRef getMemBufferRef() const
This interface provides simple read-only access to a block of memory, and provides simple methods for...
Definition: MemoryBuffer.h:41
IRCompiler(IRMaterializationUnit::ManglingOptions MO)
unsigned EmulatedTLS
EmulatedTLS - This flag enables emulated TLS model, using emutls function in the runtime library...
IRMaterializationUnit::ManglingOptions irManglingOptionsFromTargetOptions(const TargetOptions &Opts)
Expected< std::unique_ptr< TargetMachine > > createTargetMachine()
Create a TargetMachine.
This is the base ObjectCache type which can be provided to an ExecutionEngine for the purpose of avoi...
Definition: ObjectCache.h:22
std::unique_ptr< MemoryBuffer > CompileResult
Definition: CompileUtils.h:41
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition: Error.cpp:77
A utility class for building TargetMachines for JITs.