LLVM  8.0.1
NVPTXAssignValidGlobalNames.cpp
Go to the documentation of this file.
1 //===-- NVPTXAssignValidGlobalNames.cpp - Assign valid names to globals ---===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Clean up the names of global variables in the module to not contain symbols
11 // that are invalid in PTX.
12 //
13 // Currently NVPTX, like other backends, relies on generic symbol name
14 // sanitizing done by MC. However, the ptxas assembler is more stringent and
15 // disallows some additional characters in symbol names. This pass makes sure
16 // such names do not reach MC at all.
17 //
18 //===----------------------------------------------------------------------===//
19 
20 #include "NVPTX.h"
21 #include "llvm/IR/Function.h"
22 #include "llvm/IR/GlobalVariable.h"
24 #include "llvm/IR/Module.h"
26 #include <string>
27 
28 using namespace llvm;
29 
30 namespace {
31 /// NVPTXAssignValidGlobalNames
32 class NVPTXAssignValidGlobalNames : public ModulePass {
33 public:
34  static char ID;
35  NVPTXAssignValidGlobalNames() : ModulePass(ID) {}
36 
37  bool runOnModule(Module &M) override;
38 
39  /// Clean up the name to remove symbols invalid in PTX.
40  std::string cleanUpName(StringRef Name);
41 };
42 }
43 
45 
46 namespace llvm {
48 }
49 
50 INITIALIZE_PASS(NVPTXAssignValidGlobalNames, "nvptx-assign-valid-global-names",
51  "Assign valid PTX names to globals", false, false)
52 
53 bool NVPTXAssignValidGlobalNames::runOnModule(Module &M) {
54  for (GlobalVariable &GV : M.globals()) {
55  // We are only allowed to rename local symbols.
56  if (GV.hasLocalLinkage()) {
57  // setName doesn't do extra work if the name does not change.
58  // Note: this does not create collisions - if setName is asked to set the
59  // name to something that already exists, it adds a proper postfix to
60  // avoid collisions.
61  GV.setName(cleanUpName(GV.getName()));
62  }
63  }
64 
65  // Do the same for local functions.
66  for (Function &F : M.functions())
67  if (F.hasLocalLinkage())
68  F.setName(cleanUpName(F.getName()));
69 
70  return true;
71 }
72 
73 std::string NVPTXAssignValidGlobalNames::cleanUpName(StringRef Name) {
74  std::string ValidName;
75  raw_string_ostream ValidNameStream(ValidName);
76  for (unsigned I = 0, E = Name.size(); I != E; ++I) {
77  char C = Name[I];
78  if (C == '.' || C == '@') {
79  ValidNameStream << "_$_";
80  } else {
81  ValidNameStream << C;
82  }
83  }
84 
85  return ValidNameStream.str();
86 }
87 
89  return new NVPTXAssignValidGlobalNames();
90 }
uint64_t CallInst * C
This class represents lattice values for constants.
Definition: AllocatorList.h:24
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:65
ModulePass * createNVPTXAssignValidGlobalNamesPass()
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
Definition: StringRef.h:138
F(f)
void initializeNVPTXAssignValidGlobalNamesPass(PassRegistry &)
amdgpu Simplify well known AMD library false Value Value const Twine & Name
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
std::string & str()
Flushes the stream contents to the target string and returns the string&#39;s reference.
Definition: raw_ostream.h:499
Module.h This file contains the declarations for the Module class.
INITIALIZE_PASS(NVPTXAssignValidGlobalNames, "nvptx-assign-valid-global-names", "Assign valid PTX names to globals", false, false) bool NVPTXAssignValidGlobalNames
#define I(x, y, z)
Definition: MD5.cpp:58
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:225
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:483
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
Definition: PassRegistry.h:39