LLVM  14.0.0git
NVPTXAssignValidGlobalNames.cpp
Go to the documentation of this file.
1 //===-- NVPTXAssignValidGlobalNames.cpp - Assign valid names to globals ---===//
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 // Clean up the names of global variables in the module to not contain symbols
10 // that are invalid in PTX.
11 //
12 // Currently NVPTX, like other backends, relies on generic symbol name
13 // sanitizing done by MC. However, the ptxas assembler is more stringent and
14 // disallows some additional characters in symbol names. This pass makes sure
15 // such names do not reach MC at all.
16 //
17 //===----------------------------------------------------------------------===//
18 
19 #include "NVPTX.h"
20 #include "llvm/IR/Function.h"
21 #include "llvm/IR/GlobalVariable.h"
23 #include "llvm/IR/Module.h"
25 #include <string>
26 
27 using namespace llvm;
28 
29 namespace {
30 /// NVPTXAssignValidGlobalNames
31 class NVPTXAssignValidGlobalNames : public ModulePass {
32 public:
33  static char ID;
34  NVPTXAssignValidGlobalNames() : ModulePass(ID) {}
35 
36  bool runOnModule(Module &M) override;
37 
38  /// Clean up the name to remove symbols invalid in PTX.
39  std::string cleanUpName(StringRef Name);
40 };
41 }
42 
44 
45 namespace llvm {
47 }
48 
49 INITIALIZE_PASS(NVPTXAssignValidGlobalNames, "nvptx-assign-valid-global-names",
50  "Assign valid PTX names to globals", false, false)
51 
52 bool NVPTXAssignValidGlobalNames::runOnModule(Module &M) {
53  for (GlobalVariable &GV : M.globals()) {
54  // We are only allowed to rename local symbols.
55  if (GV.hasLocalLinkage()) {
56  // setName doesn't do extra work if the name does not change.
57  // Note: this does not create collisions - if setName is asked to set the
58  // name to something that already exists, it adds a proper postfix to
59  // avoid collisions.
60  GV.setName(cleanUpName(GV.getName()));
61  }
62  }
63 
64  // Do the same for local functions.
65  for (Function &F : M.functions())
66  if (F.hasLocalLinkage())
67  F.setName(cleanUpName(F.getName()));
68 
69  return true;
70 }
71 
72 std::string NVPTXAssignValidGlobalNames::cleanUpName(StringRef Name) {
73  std::string ValidName;
74  raw_string_ostream ValidNameStream(ValidName);
75  for (unsigned I = 0, E = Name.size(); I != E; ++I) {
76  char C = Name[I];
77  if (C == '.' || C == '@') {
78  ValidNameStream << "_$_";
79  } else {
80  ValidNameStream << C;
81  }
82  }
83 
84  return ValidNameStream.str();
85 }
86 
88  return new NVPTXAssignValidGlobalNames();
89 }
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
M
We currently emits eax Perhaps this is what we really should generate is Is imull three or four cycles eax eax The current instruction priority is based on pattern complexity The former is more complex because it folds a load so the latter will not be emitted Perhaps we should use AddedComplexity to give LEA32r a higher priority We should always try to match LEA first since the LEA matching code does some estimate to determine whether the match is profitable if we care more about code then imull is better It s two bytes shorter than movl leal On a Pentium M
Definition: README.txt:252
llvm::ModulePass
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:238
llvm::Function
Definition: Function.h:61
llvm::raw_string_ostream
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:625
llvm::GlobalVariable
Definition: GlobalVariable.h:40
Module.h
LegacyPassManager.h
llvm::createNVPTXAssignValidGlobalNamesPass
ModulePass * createNVPTXAssignValidGlobalNamesPass()
Definition: NVPTXAssignValidGlobalNames.cpp:87
F
#define F(x, y, z)
Definition: MD5.cpp:56
NVPTX.h
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
C
(vector float) vec_cmpeq(*A, *B) C
Definition: README_ALTIVEC.txt:86
INITIALIZE_PASS
INITIALIZE_PASS(NVPTXAssignValidGlobalNames, "nvptx-assign-valid-global-names", "Assign valid PTX names to globals", false, false) bool NVPTXAssignValidGlobalNames
Definition: NVPTXAssignValidGlobalNames.cpp:49
llvm::PassRegistry
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
Definition: PassRegistry.h:38
I
#define I(x, y, z)
Definition: MD5.cpp:59
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:52
GlobalVariable.h
Function.h
raw_ostream.h
llvm::initializeNVPTXAssignValidGlobalNamesPass
void initializeNVPTXAssignValidGlobalNamesPass(PassRegistry &)
llvm::Intrinsic::ID
unsigned ID
Definition: TargetTransformInfo.h:37