[AMDGPU] prevent shrinking udiv/urem if either operand is in (SignedMax,UnsignedMax...
[llvm-project.git] / llvm / lib / ExecutionEngine / Orc / CompileUtils.cpp
blob5d2f3cd4a8be8a9e268505c1683473417f648e69
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 //===----------------------------------------------------------------------===//
9 #include "llvm/ExecutionEngine/Orc/CompileUtils.h"
11 #include "llvm/ADT/SmallVector.h"
12 #include "llvm/ExecutionEngine/ObjectCache.h"
13 #include "llvm/IR/LegacyPassManager.h"
14 #include "llvm/IR/Module.h"
15 #include "llvm/MC/MCContext.h"
16 #include "llvm/Object/ObjectFile.h"
17 #include "llvm/Support/Error.h"
18 #include "llvm/Support/MemoryBuffer.h"
19 #include "llvm/Support/SmallVectorMemoryBuffer.h"
20 #include "llvm/Target/TargetMachine.h"
22 namespace llvm {
23 namespace orc {
25 IRSymbolMapper::ManglingOptions
26 irManglingOptionsFromTargetOptions(const TargetOptions &Opts) {
27 IRSymbolMapper::ManglingOptions MO;
29 MO.EmulatedTLS = Opts.EmulatedTLS;
31 return MO;
34 /// Compile a Module to an ObjectFile.
35 Expected<SimpleCompiler::CompileResult> SimpleCompiler::operator()(Module &M) {
36 CompileResult CachedObject = tryToLoadFromObjectCache(M);
37 if (CachedObject)
38 return std::move(CachedObject);
40 SmallVector<char, 0> ObjBufferSV;
43 raw_svector_ostream ObjStream(ObjBufferSV);
45 legacy::PassManager PM;
46 MCContext *Ctx;
47 if (TM.addPassesToEmitMC(PM, Ctx, ObjStream))
48 return make_error<StringError>("Target does not support MC emission",
49 inconvertibleErrorCode());
50 PM.run(M);
53 auto ObjBuffer = std::make_unique<SmallVectorMemoryBuffer>(
54 std::move(ObjBufferSV), M.getModuleIdentifier() + "-jitted-objectbuffer",
55 /*RequiresNullTerminator=*/false);
57 auto Obj = object::ObjectFile::createObjectFile(ObjBuffer->getMemBufferRef());
59 if (!Obj)
60 return Obj.takeError();
62 notifyObjectCompiled(M, *ObjBuffer);
63 return std::move(ObjBuffer);
66 SimpleCompiler::CompileResult
67 SimpleCompiler::tryToLoadFromObjectCache(const Module &M) {
68 if (!ObjCache)
69 return CompileResult();
71 return ObjCache->getObject(&M);
74 void SimpleCompiler::notifyObjectCompiled(const Module &M,
75 const MemoryBuffer &ObjBuffer) {
76 if (ObjCache)
77 ObjCache->notifyObjectCompiled(&M, ObjBuffer.getMemBufferRef());
80 ConcurrentIRCompiler::ConcurrentIRCompiler(JITTargetMachineBuilder JTMB,
81 ObjectCache *ObjCache)
82 : IRCompiler(irManglingOptionsFromTargetOptions(JTMB.getOptions())),
83 JTMB(std::move(JTMB)), ObjCache(ObjCache) {}
85 Expected<std::unique_ptr<MemoryBuffer>>
86 ConcurrentIRCompiler::operator()(Module &M) {
87 auto TM = cantFail(JTMB.createTargetMachine());
88 SimpleCompiler C(*TM, ObjCache);
89 return C(M);
92 } // end namespace orc
93 } // end namespace llvm