xref: /aosp_15_r20/external/llvm/include/llvm/ExecutionEngine/Orc/CompileUtils.h (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker //===-- CompileUtils.h - Utilities for compiling IR in the JIT --*- C++ -*-===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker //                     The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // Contains utilities for compiling IR to object files.
11*9880d681SAndroid Build Coastguard Worker //
12*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
13*9880d681SAndroid Build Coastguard Worker 
14*9880d681SAndroid Build Coastguard Worker #ifndef LLVM_EXECUTIONENGINE_ORC_COMPILEUTILS_H
15*9880d681SAndroid Build Coastguard Worker #define LLVM_EXECUTIONENGINE_ORC_COMPILEUTILS_H
16*9880d681SAndroid Build Coastguard Worker 
17*9880d681SAndroid Build Coastguard Worker #include "llvm/ExecutionEngine/ObjectMemoryBuffer.h"
18*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/LegacyPassManager.h"
19*9880d681SAndroid Build Coastguard Worker #include "llvm/MC/MCContext.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/Object/ObjectFile.h"
21*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetMachine.h"
22*9880d681SAndroid Build Coastguard Worker 
23*9880d681SAndroid Build Coastguard Worker namespace llvm {
24*9880d681SAndroid Build Coastguard Worker namespace orc {
25*9880d681SAndroid Build Coastguard Worker 
26*9880d681SAndroid Build Coastguard Worker /// @brief Simple compile functor: Takes a single IR module and returns an
27*9880d681SAndroid Build Coastguard Worker ///        ObjectFile.
28*9880d681SAndroid Build Coastguard Worker class SimpleCompiler {
29*9880d681SAndroid Build Coastguard Worker public:
30*9880d681SAndroid Build Coastguard Worker   /// @brief Construct a simple compile functor with the given target.
SimpleCompiler(TargetMachine & TM)31*9880d681SAndroid Build Coastguard Worker   SimpleCompiler(TargetMachine &TM) : TM(TM) {}
32*9880d681SAndroid Build Coastguard Worker 
33*9880d681SAndroid Build Coastguard Worker   /// @brief Compile a Module to an ObjectFile.
operator()34*9880d681SAndroid Build Coastguard Worker   object::OwningBinary<object::ObjectFile> operator()(Module &M) const {
35*9880d681SAndroid Build Coastguard Worker     SmallVector<char, 0> ObjBufferSV;
36*9880d681SAndroid Build Coastguard Worker     raw_svector_ostream ObjStream(ObjBufferSV);
37*9880d681SAndroid Build Coastguard Worker 
38*9880d681SAndroid Build Coastguard Worker     legacy::PassManager PM;
39*9880d681SAndroid Build Coastguard Worker     MCContext *Ctx;
40*9880d681SAndroid Build Coastguard Worker     if (TM.addPassesToEmitMC(PM, Ctx, ObjStream))
41*9880d681SAndroid Build Coastguard Worker       llvm_unreachable("Target does not support MC emission.");
42*9880d681SAndroid Build Coastguard Worker     PM.run(M);
43*9880d681SAndroid Build Coastguard Worker     std::unique_ptr<MemoryBuffer> ObjBuffer(
44*9880d681SAndroid Build Coastguard Worker         new ObjectMemoryBuffer(std::move(ObjBufferSV)));
45*9880d681SAndroid Build Coastguard Worker     Expected<std::unique_ptr<object::ObjectFile>> Obj =
46*9880d681SAndroid Build Coastguard Worker         object::ObjectFile::createObjectFile(ObjBuffer->getMemBufferRef());
47*9880d681SAndroid Build Coastguard Worker     typedef object::OwningBinary<object::ObjectFile> OwningObj;
48*9880d681SAndroid Build Coastguard Worker     if (Obj)
49*9880d681SAndroid Build Coastguard Worker       return OwningObj(std::move(*Obj), std::move(ObjBuffer));
50*9880d681SAndroid Build Coastguard Worker     // TODO: Actually report errors helpfully.
51*9880d681SAndroid Build Coastguard Worker     consumeError(Obj.takeError());
52*9880d681SAndroid Build Coastguard Worker     return OwningObj(nullptr, nullptr);
53*9880d681SAndroid Build Coastguard Worker   }
54*9880d681SAndroid Build Coastguard Worker 
55*9880d681SAndroid Build Coastguard Worker private:
56*9880d681SAndroid Build Coastguard Worker   TargetMachine &TM;
57*9880d681SAndroid Build Coastguard Worker };
58*9880d681SAndroid Build Coastguard Worker 
59*9880d681SAndroid Build Coastguard Worker } // End namespace orc.
60*9880d681SAndroid Build Coastguard Worker } // End namespace llvm.
61*9880d681SAndroid Build Coastguard Worker 
62*9880d681SAndroid Build Coastguard Worker #endif // LLVM_EXECUTIONENGINE_ORC_COMPILEUTILS_H
63