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