1//===----------- Mangling.cpp -- Name Mangling Utilities for ORC ----------===//
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/Mangling.h"
10#include "llvm/ADT/StringRef.h"
11#include "llvm/IR/Constants.h"
12
13#define DEBUG_TYPE "orc"
14
15namespace llvm::orc {
16
17void IRSymbolMapper::add(ExecutionSession &ES, const ManglingOptions &MO,
18 ArrayRef<GlobalValue *> GVs,
19 SymbolFlagsMap &SymbolFlags,
20 SymbolNameToDefinitionMap *SymbolToDefinition) {
21 if (GVs.empty())
22 return;
23
24 MangleAndInterner Mangle(ES, GVs[0]->getDataLayout());
25 for (auto *G : GVs) {
26 assert(G && "GVs cannot contain null elements");
27 if (!G->hasName() || G->isDeclaration() || G->hasLocalLinkage() ||
28 G->hasAvailableExternallyLinkage() || G->hasAppendingLinkage())
29 continue;
30
31 if (G->isThreadLocal() && MO.EmulatedTLS) {
32 auto *GV = cast<GlobalVariable>(Val: G);
33
34 auto Flags = JITSymbolFlags::fromGlobalValue(GV: *GV);
35
36 auto EmuTLSV = Mangle(("__emutls_v." + GV->getName()).str());
37 SymbolFlags[EmuTLSV] = Flags;
38 if (SymbolToDefinition)
39 (*SymbolToDefinition)[EmuTLSV] = GV;
40
41 // If this GV has a non-zero initializer we'll need to emit an
42 // __emutls.t symbol too.
43 if (GV->hasInitializer()) {
44 const auto *InitVal = GV->getInitializer();
45
46 // Skip zero-initializers.
47 if (isa<ConstantAggregateZero>(Val: InitVal))
48 continue;
49 const auto *InitIntValue = dyn_cast<ConstantInt>(Val: InitVal);
50 if (InitIntValue && InitIntValue->isZero())
51 continue;
52
53 auto EmuTLST = Mangle(("__emutls_t." + GV->getName()).str());
54 SymbolFlags[EmuTLST] = Flags;
55 if (SymbolToDefinition)
56 (*SymbolToDefinition)[EmuTLST] = GV;
57 }
58 continue;
59 }
60
61 // Otherwise we just need a normal linker mangling.
62 auto MangledName = Mangle(G->getName());
63 SymbolFlags[MangledName] = JITSymbolFlags::fromGlobalValue(GV: *G);
64 if (SymbolToDefinition)
65 (*SymbolToDefinition)[MangledName] = G;
66 }
67}
68
69} // namespace llvm::orc
70