1 //===-- NVPTXAssignValidGlobalNames.cpp - Assign valid names to globals ---===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Clean up the names of global variables in the module to not contain symbols
11 // that are invalid in PTX.
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.
18 //===----------------------------------------------------------------------===//
21 #include "llvm/IR/Function.h"
22 #include "llvm/IR/GlobalVariable.h"
23 #include "llvm/IR/LegacyPassManager.h"
24 #include "llvm/IR/Module.h"
25 #include "llvm/Support/raw_ostream.h"
31 /// NVPTXAssignValidGlobalNames
32 class NVPTXAssignValidGlobalNames
: public ModulePass
{
35 NVPTXAssignValidGlobalNames() : ModulePass(ID
) {}
37 bool runOnModule(Module
&M
) override
;
39 /// Clean up the name to remove symbols invalid in PTX.
40 std::string
cleanUpName(StringRef Name
);
44 char NVPTXAssignValidGlobalNames::ID
= 0;
47 void initializeNVPTXAssignValidGlobalNamesPass(PassRegistry
&);
50 INITIALIZE_PASS(NVPTXAssignValidGlobalNames
, "nvptx-assign-valid-global-names",
51 "Assign valid PTX names to globals", false, false)
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
61 GV
.setName(cleanUpName(GV
.getName()));
65 // Do the same for local functions.
66 for (Function
&F
: M
.functions())
67 if (F
.hasLocalLinkage())
68 F
.setName(cleanUpName(F
.getName()));
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
) {
78 if (C
== '.' || C
== '@') {
79 ValidNameStream
<< "_$_";
85 return ValidNameStream
.str();
88 ModulePass
*llvm::createNVPTXAssignValidGlobalNamesPass() {
89 return new NVPTXAssignValidGlobalNames();