LLVM 24.0.0git
Mangling.cpp
Go to the documentation of this file.
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
11#include "llvm/ADT/StringRef.h"
12#include "llvm/IR/Constants.h"
13#include "llvm/IR/Mangler.h"
15
16#define DEBUG_TYPE "orc"
17
18namespace llvm::orc {
19
21 : ES(ES), Mode(fromTriple(ES.getTargetTriple(), ABIName)) {}
22
25
27 : ES(ES), Mode(fromDataLayout(DL)) {}
28
29// TODO: The prefixing rules below, and the mangling-mode derivation in
30// fromDataLayoutStr, duplicate logic that already lives in llvm::Mangler
31// (getNameWithPrefix) and DataLayout (ManglingModeT and its "m:" spec
32// parsing). They are re-implemented here only because those APIs require a
33// full DataLayout, which this class is meant to work without. We should
34// refactor to have one copy of this code, probably best defined in
35// TargetParser, shared between all users.
37 if (Name.empty())
38 return ES.intern(Name);
39
40 if (Name.front() == '\1')
41 return ES.intern(Name.substr(1));
42
43 if (Name[0] == '?' && doNotMangleLeadingQuestionMark())
44 return ES.intern(Name);
45
46 if (Mode == ManglingMode::MachO || Mode == ManglingMode::WinCOFFX86)
47 return ES.intern(("_" + Name).str());
48
49 return ES.intern(Name);
50}
51
53MangleAndInterner::fromDataLayoutStr(StringRef DLStr) {
54 for (StringRef Spec : split(DLStr, '-')) {
55 if (!Spec.starts_with("m:"))
56 continue;
57 auto ModeStr = Spec.drop_front(2);
58 assert(ModeStr.size() == 1 &&
59 "invalid data layout string from Triple::computeDataLayout");
60 switch (ModeStr[0]) {
61 case 'e':
62 return ManglingMode::ELF;
63 case 'l':
64 return ManglingMode::GOFF;
65 case 'o':
67 case 'm':
68 return ManglingMode::Mips;
69 case 'w':
71 case 'x':
73 case 'a':
75 default:
76 llvm_unreachable("Invalid mangling mode from Triple::computeDataLayout");
77 }
78 }
79 return ManglingMode::None;
80}
81
83MangleAndInterner::fromTriple(const Triple &TT, StringRef ABIName) {
84 return fromDataLayoutStr(TT.computeDataLayout(ABIName));
85}
86
88MangleAndInterner::fromDataLayout(const DataLayout &DL) {
89 return fromDataLayoutStr(DL.getStringRepresentation());
90}
91
92bool MangleAndInterner::doNotMangleLeadingQuestionMark() const {
93 return Mode == ManglingMode::WinCOFF || Mode == ManglingMode::WinCOFFX86;
94}
95
98 SymbolFlagsMap &SymbolFlags,
99 SymbolNameToDefinitionMap *SymbolToDefinition) {
100 if (GVs.empty())
101 return;
102
103 MangleAndInterner Mangle(ES, GVs[0]->getDataLayout());
104 for (auto *G : GVs) {
105 assert(G && "GVs cannot contain null elements");
106 if (!G->hasName() || G->isDeclaration() || G->hasLocalLinkage() ||
107 G->hasAvailableExternallyLinkage() || G->hasAppendingLinkage())
108 continue;
109
110 if (G->isThreadLocal() && MO.EmulatedTLS) {
111 auto *GV = cast<GlobalVariable>(G);
112
113 auto Flags = JITSymbolFlags::fromGlobalValue(*GV);
114
115 auto EmuTLSV = Mangle(("__emutls_v." + GV->getName()).str());
116 SymbolFlags[EmuTLSV] = Flags;
117 if (SymbolToDefinition)
118 (*SymbolToDefinition)[EmuTLSV] = GV;
119
120 // If this GV has a non-zero initializer we'll need to emit an
121 // __emutls.t symbol too.
122 if (GV->hasInitializer()) {
123 const auto *InitVal = GV->getInitializer();
124
125 // Skip zero-initializers.
126 if (isa<ConstantAggregateZero>(InitVal))
127 continue;
128 const auto *InitIntValue = dyn_cast<ConstantInt>(InitVal);
129 if (InitIntValue && InitIntValue->isZero())
130 continue;
131
132 auto EmuTLST = Mangle(("__emutls_t." + GV->getName()).str());
133 SymbolFlags[EmuTLST] = Flags;
134 if (SymbolToDefinition)
135 (*SymbolToDefinition)[EmuTLST] = GV;
136 }
137 continue;
138 }
139
140 // Otherwise we just need a normal linker mangling.
141 auto MangledName = Mangle(G->getName());
142 SymbolFlags[MangledName] = JITSymbolFlags::fromGlobalValue(*G);
143 if (SymbolToDefinition)
144 (*SymbolToDefinition)[MangledName] = G;
145 }
146}
147
148} // namespace llvm::orc
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
This file contains the declarations for the subclasses of Constant, which represent the different fla...
#define G(x, y, z)
Definition MD5.cpp:55
This file contains some functions that are useful when dealing with strings.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
bool empty() const
Check if the array is empty.
Definition ArrayRef.h:136
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
static LLVM_ABI JITSymbolFlags fromGlobalValue(const GlobalValue &GV)
Construct a JITSymbolFlags value based on the flags of the given global value.
Definition JITSymbol.cpp:22
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
An ExecutionSession represents a running JIT program.
Definition Core.h:1111
static LLVM_ABI void add(ExecutionSession &ES, const ManglingOptions &MO, ArrayRef< GlobalValue * > GVs, SymbolFlagsMap &SymbolFlags, SymbolNameToDefinitionMap *SymbolToDefinition=nullptr)
Add mangled symbols for the given GlobalValues to SymbolFlags.
Definition Mangling.cpp:96
std::map< SymbolStringPtr, GlobalValue * > SymbolNameToDefinitionMap
Definition Mangling.h:64
Mangles symbol names then uniques them in the context of an ExecutionSession.
Definition Mangling.h:27
LLVM_ABI SymbolStringPtr operator()(StringRef Name)
Definition Mangling.cpp:36
LLVM_ABI MangleAndInterner(ExecutionSession &ES, StringRef ABIName="")
Definition Mangling.cpp:20
Pointer to a pooled string representing a symbol name.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
DenseMap< SymbolStringPtr, JITSymbolFlags > SymbolFlagsMap
A map from symbol names (as SymbolStringPtrs) to JITSymbolFlags.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
iterator_range< SplittingIterator > split(StringRef Str, StringRef Separator)
Split the specified string over a separator and return a range-compatible iterable over its partition...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559