LLVM  15.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 
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 
20  : ES(ES), DL(DL) {}
21 
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 
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());
78  if (SymbolToDefinition)
79  (*SymbolToDefinition)[MangledName] = G;
80  }
81 }
82 
83 } // End namespace orc.
84 } // End namespace llvm.
llvm::orc::IRSymbolMapper::add
static 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:31
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
llvm::DataLayout
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:113
llvm::raw_string_ostream
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:632
llvm::orc::IRSymbolMapper::SymbolNameToDefinitionMap
std::map< SymbolStringPtr, GlobalValue * > SymbolNameToDefinitionMap
Definition: Mangling.h:45
llvm::orc::SymbolStringPtr
Pointer to a pooled string representing a symbol name.
Definition: SymbolStringPool.h:57
llvm::orc::MangleAndInterner::MangleAndInterner
MangleAndInterner(ExecutionSession &ES, const DataLayout &DL)
Definition: Mangling.cpp:19
llvm::MachO::SymbolFlags
SymbolFlags
Symbol flags.
Definition: Symbol.h:24
llvm::ArrayRef::empty
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:159
Constants.h
llvm::Mangler::getNameWithPrefix
void getNameWithPrefix(raw_ostream &OS, const GlobalValue *GV, bool CannotUsePrivateLabel) const
Print the appropriate prefix and the specified global variable's name.
Definition: Mangler.cpp:119
llvm::orc::ExecutionSession::intern
SymbolStringPtr intern(StringRef SymName)
Add a symbol name to the SymbolStringPool and return a pointer to it.
Definition: Core.h:1409
G
const DataFlowGraph & G
Definition: RDFGraph.cpp:200
llvm::DenseMap< SymbolStringPtr, JITSymbolFlags >
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Mangler.h
llvm::orc::IRSymbolMapper::ManglingOptions::EmulatedTLS
bool EmulatedTLS
Definition: Mangling.h:42
llvm::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: APInt.h:32
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::orc::MangleAndInterner::operator()
SymbolStringPtr operator()(StringRef Name)
Definition: Mangling.cpp:22
getParent
static const Function * getParent(const Value *V)
Definition: BasicAliasAnalysis.cpp:845
DL
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Definition: AArch64SLSHardening.cpp:76
llvm::orc::MangleAndInterner
Mangles symbol names then uniques them in the context of an ExecutionSession.
Definition: Mangling.h:26
llvm::orc::IRSymbolMapper::ManglingOptions
Definition: Mangling.h:41
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:50
Mangling.h
llvm::orc::ExecutionSession
An ExecutionSession represents a running JIT program.
Definition: Core.h:1358
llvm::JITSymbolFlags::fromGlobalValue
static JITSymbolFlags fromGlobalValue(const GlobalValue &GV)
Construct a JITSymbolFlags value based on the flags of the given global value.
Definition: JITSymbol.cpp:22
Debug.h