1//===-------------------- Layer.cpp - Layer interfaces --------------------===//
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
9#include "llvm/ExecutionEngine/Orc/Layer.h"
10
11#include "llvm/ExecutionEngine/Orc/DebugUtils.h"
12#include "llvm/ExecutionEngine/Orc/ExecutionUtils.h"
13#include "llvm/ExecutionEngine/Orc/ObjectFileInterface.h"
14#include "llvm/IR/Constants.h"
15#include "llvm/Support/Debug.h"
16
17#define DEBUG_TYPE "orc"
18
19namespace llvm {
20namespace orc {
21
22IRLayer::~IRLayer() = default;
23
24Error IRLayer::add(ResourceTrackerSP RT, ThreadSafeModule TSM) {
25 assert(RT && "RT can not be null");
26 auto &JD = RT->getJITDylib();
27 return JD.define(MU: std::make_unique<BasicIRLayerMaterializationUnit>(
28 args&: *this, args: *getManglingOptions(), args: std::move(TSM)),
29 RT: std::move(RT));
30}
31
32IRMaterializationUnit::IRMaterializationUnit(
33 ExecutionSession &ES, const IRSymbolMapper::ManglingOptions &MO,
34 ThreadSafeModule TSM)
35 : MaterializationUnit(Interface()), TSM(std::move(TSM)) {
36
37 assert(this->TSM && "Module must not be null");
38
39 MangleAndInterner Mangle(ES, this->TSM.getModuleUnlocked()->getDataLayout());
40 this->TSM.withModuleDo(F: [&](Module &M) {
41 for (auto &G : M.global_values()) {
42 // Skip globals that don't generate symbols.
43
44 if (!G.hasName() || G.isDeclaration() || G.hasLocalLinkage() ||
45 G.hasAvailableExternallyLinkage() || G.hasAppendingLinkage())
46 continue;
47
48 // thread locals generate different symbols depending on whether or not
49 // emulated TLS is enabled.
50 if (G.isThreadLocal() && MO.EmulatedTLS) {
51 auto &GV = cast<GlobalVariable>(Val&: G);
52
53 auto Flags = JITSymbolFlags::fromGlobalValue(GV);
54
55 auto EmuTLSV = Mangle(("__emutls_v." + GV.getName()).str());
56 SymbolFlags[EmuTLSV] = Flags;
57 SymbolToDefinition[EmuTLSV] = &GV;
58
59 // If this GV has a non-zero initializer we'll need to emit an
60 // __emutls.t symbol too.
61 if (GV.hasInitializer()) {
62 const auto *InitVal = GV.getInitializer();
63
64 // Skip zero-initializers.
65 if (isa<ConstantAggregateZero>(Val: InitVal))
66 continue;
67 const auto *InitIntValue = dyn_cast<ConstantInt>(Val: InitVal);
68 if (InitIntValue && InitIntValue->isZero())
69 continue;
70
71 auto EmuTLST = Mangle(("__emutls_t." + GV.getName()).str());
72 SymbolFlags[EmuTLST] = Flags;
73 SymbolToDefinition[EmuTLST] = &GV;
74 }
75 continue;
76 }
77
78 // Otherwise we just need a normal linker mangling.
79 auto MangledName = Mangle(G.getName());
80 auto &Flags = SymbolFlags[MangledName];
81 Flags = JITSymbolFlags::fromGlobalValue(GV: G);
82 if (G.getComdat() &&
83 G.getComdat()->getSelectionKind() != Comdat::NoDeduplicate)
84 Flags |= JITSymbolFlags::Weak;
85 SymbolToDefinition[MangledName] = &G;
86 }
87
88 // If we need an init symbol for this module then create one.
89 if (!getStaticInitGVs(M).empty()) {
90 size_t Counter = 0;
91
92 do {
93 std::string InitSymbolName;
94 raw_string_ostream(InitSymbolName)
95 << "$." << M.getModuleIdentifier() << ".__inits." << Counter++;
96 InitSymbol = ES.intern(SymName: InitSymbolName);
97 } while (SymbolFlags.count(Val: InitSymbol));
98
99 SymbolFlags[InitSymbol] = JITSymbolFlags::MaterializationSideEffectsOnly;
100 }
101 });
102}
103
104IRMaterializationUnit::IRMaterializationUnit(
105 ThreadSafeModule TSM, Interface I,
106 SymbolNameToDefinitionMap SymbolToDefinition)
107 : MaterializationUnit(std::move(I)), TSM(std::move(TSM)),
108 SymbolToDefinition(std::move(SymbolToDefinition)) {}
109
110StringRef IRMaterializationUnit::getName() const {
111 if (TSM)
112 return TSM.withModuleDo(
113 F: [](const Module &M) -> StringRef { return M.getModuleIdentifier(); });
114 return "<null module>";
115}
116
117void IRMaterializationUnit::discard(const JITDylib &JD,
118 const SymbolStringPtr &Name) {
119 LLVM_DEBUG(JD.getExecutionSession().runSessionLocked([&]() {
120 dbgs() << "In " << JD.getName() << " discarding " << *Name << " from MU@"
121 << this << " (" << getName() << ")\n";
122 }););
123
124 auto I = SymbolToDefinition.find(x: Name);
125 assert(I != SymbolToDefinition.end() &&
126 "Symbol not provided by this MU, or previously discarded");
127 assert(!I->second->isDeclaration() &&
128 "Discard should only apply to definitions");
129 I->second->setLinkage(GlobalValue::AvailableExternallyLinkage);
130 // According to the IR verifier, "Declaration[s] may not be in a Comdat!"
131 // Remove it, if this is a GlobalObject.
132 if (auto *GO = dyn_cast<GlobalObject>(Val: I->second))
133 GO->setComdat(nullptr);
134 SymbolToDefinition.erase(position: I);
135}
136
137BasicIRLayerMaterializationUnit::BasicIRLayerMaterializationUnit(
138 IRLayer &L, const IRSymbolMapper::ManglingOptions &MO, ThreadSafeModule TSM)
139 : IRMaterializationUnit(L.getExecutionSession(), MO, std::move(TSM)), L(L) {
140}
141
142void BasicIRLayerMaterializationUnit::materialize(
143 std::unique_ptr<MaterializationResponsibility> R) {
144
145 // Throw away the SymbolToDefinition map: it's not usable after we hand
146 // off the module.
147 SymbolToDefinition.clear();
148
149 // If cloneToNewContextOnEmit is set, clone the module now.
150 if (L.getCloneToNewContextOnEmit())
151 TSM = cloneToNewContext(TSMW: TSM);
152
153#ifndef NDEBUG
154 auto &ES = R->getTargetJITDylib().getExecutionSession();
155 auto &N = R->getTargetJITDylib().getName();
156#endif // NDEBUG
157
158 LLVM_DEBUG(ES.runSessionLocked(
159 [&]() { dbgs() << "Emitting, for " << N << ", " << *this << "\n"; }););
160 L.emit(R: std::move(R), TSM: std::move(TSM));
161 LLVM_DEBUG(ES.runSessionLocked([&]() {
162 dbgs() << "Finished emitting, for " << N << ", " << *this << "\n";
163 }););
164}
165
166char ObjectLayer::ID;
167
168ObjectLayer::ObjectLayer(ExecutionSession &ES) : ES(ES) {}
169
170ObjectLayer::~ObjectLayer() = default;
171
172Error ObjectLayer::add(ResourceTrackerSP RT, std::unique_ptr<MemoryBuffer> O,
173 MaterializationUnit::Interface I) {
174 assert(RT && "RT can not be null");
175 auto &JD = RT->getJITDylib();
176 return JD.define(MU: std::make_unique<BasicObjectLayerMaterializationUnit>(
177 args&: *this, args: std::move(O), args: std::move(I)),
178 RT: std::move(RT));
179}
180
181Error ObjectLayer::add(ResourceTrackerSP RT, std::unique_ptr<MemoryBuffer> O) {
182 auto I = getObjectFileInterface(ES&: getExecutionSession(), ObjBuffer: O->getMemBufferRef());
183 if (!I)
184 return I.takeError();
185 return add(RT: std::move(RT), O: std::move(O), I: std::move(*I));
186}
187
188Error ObjectLayer::add(JITDylib &JD, std::unique_ptr<MemoryBuffer> O) {
189 auto I = getObjectFileInterface(ES&: getExecutionSession(), ObjBuffer: O->getMemBufferRef());
190 if (!I)
191 return I.takeError();
192 return add(JD, O: std::move(O), I: std::move(*I));
193}
194
195Expected<std::unique_ptr<BasicObjectLayerMaterializationUnit>>
196BasicObjectLayerMaterializationUnit::Create(ObjectLayer &L,
197 std::unique_ptr<MemoryBuffer> O) {
198
199 auto ObjInterface =
200 getObjectFileInterface(ES&: L.getExecutionSession(), ObjBuffer: O->getMemBufferRef());
201
202 if (!ObjInterface)
203 return ObjInterface.takeError();
204
205 return std::make_unique<BasicObjectLayerMaterializationUnit>(
206 args&: L, args: std::move(O), args: std::move(*ObjInterface));
207}
208
209BasicObjectLayerMaterializationUnit::BasicObjectLayerMaterializationUnit(
210 ObjectLayer &L, std::unique_ptr<MemoryBuffer> O, Interface I)
211 : MaterializationUnit(std::move(I)), L(L), O(std::move(O)) {}
212
213StringRef BasicObjectLayerMaterializationUnit::getName() const {
214 if (O)
215 return O->getBufferIdentifier();
216 return "<null object>";
217}
218
219void BasicObjectLayerMaterializationUnit::materialize(
220 std::unique_ptr<MaterializationResponsibility> R) {
221 L.emit(R: std::move(R), O: std::move(O));
222}
223
224void BasicObjectLayerMaterializationUnit::discard(const JITDylib &JD,
225 const SymbolStringPtr &Name) {
226 // This is a no-op for object files: Having removed 'Name' from SymbolFlags
227 // the symbol will be dead-stripped by the JIT linker.
228}
229
230} // End namespace orc.
231} // End namespace llvm.
232