1 //===-- CompileUtils.h - Utilities for compiling IR in the JIT --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Contains utilities for compiling IR to object files.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_EXECUTIONENGINE_ORC_COMPILEUTILS_H
15 #define LLVM_EXECUTIONENGINE_ORC_COMPILEUTILS_H
17 #include "llvm/ExecutionEngine/ObjectMemoryBuffer.h"
18 #include "llvm/IR/LegacyPassManager.h"
19 #include "llvm/MC/MCContext.h"
20 #include "llvm/Object/ObjectFile.h"
21 #include "llvm/Target/TargetMachine.h"
26 /// @brief Simple compile functor: Takes a single IR module and returns an
28 class SimpleCompiler {
30 /// @brief Construct a simple compile functor with the given target.
31 SimpleCompiler(TargetMachine &TM) : TM(TM) {}
33 /// @brief Compile a Module to an ObjectFile.
34 object::OwningBinary<object::ObjectFile> operator()(Module &M) const {
35 SmallVector<char, 0> ObjBufferSV;
36 raw_svector_ostream ObjStream(ObjBufferSV);
38 legacy::PassManager PM;
40 if (TM.addPassesToEmitMC(PM, Ctx, ObjStream))
41 llvm_unreachable("Target does not support MC emission.");
43 std::unique_ptr<MemoryBuffer> ObjBuffer(
44 new ObjectMemoryBuffer(std::move(ObjBufferSV)));
45 ErrorOr<std::unique_ptr<object::ObjectFile>> Obj =
46 object::ObjectFile::createObjectFile(ObjBuffer->getMemBufferRef());
47 // TODO: Actually report errors helpfully.
48 typedef object::OwningBinary<object::ObjectFile> OwningObj;
50 return OwningObj(std::move(*Obj), std::move(ObjBuffer));
51 return OwningObj(nullptr, nullptr);
58 } // End namespace orc.
59 } // End namespace llvm.
61 #endif // LLVM_EXECUTIONENGINE_ORC_COMPILEUTILS_H