blob: 7d4be8e809cf0d32e7805f47db6acf23805cf768 [file] [log] [blame]
Eli Bendersky264cd462014-03-31 15:56:26 +00001//===-- 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/GlobalVariable.h"
Chandler Carruth30d69c22015-02-13 10:01:29 +000022#include "llvm/IR/LegacyPassManager.h"
Eli Bendersky264cd462014-03-31 15:56:26 +000023#include "llvm/IR/Module.h"
Eli Bendersky264cd462014-03-31 15:56:26 +000024#include "llvm/Support/raw_ostream.h"
25#include <string>
26
27using namespace llvm;
28
29namespace {
30/// \brief NVPTXAssignValidGlobalNames
31class NVPTXAssignValidGlobalNames : public ModulePass {
32public:
33 static char ID;
34 NVPTXAssignValidGlobalNames() : ModulePass(ID) {}
35
Craig Topper2865c982014-04-29 07:57:44 +000036 bool runOnModule(Module &M) override;
Eli Bendersky264cd462014-03-31 15:56:26 +000037
38 /// \brief Clean up the name to remove symbols invalid in PTX.
39 std::string cleanUpName(StringRef Name);
40};
Alexander Kornienkof00654e2015-06-23 09:49:53 +000041}
Eli Bendersky264cd462014-03-31 15:56:26 +000042
43char NVPTXAssignValidGlobalNames::ID = 0;
44
45namespace llvm {
46void initializeNVPTXAssignValidGlobalNamesPass(PassRegistry &);
47}
48
49INITIALIZE_PASS(NVPTXAssignValidGlobalNames, "nvptx-assign-valid-global-names",
50 "Assign valid PTX names to globals", false, false)
51
52bool 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 return true;
65}
66
67std::string NVPTXAssignValidGlobalNames::cleanUpName(StringRef Name) {
68 std::string ValidName;
69 raw_string_ostream ValidNameStream(ValidName);
70 for (unsigned I = 0, E = Name.size(); I != E; ++I) {
71 char C = Name[I];
72 if (C == '.' || C == '@') {
73 ValidNameStream << "_$_";
74 } else {
75 ValidNameStream << C;
76 }
77 }
78
79 return ValidNameStream.str();
80}
81
82ModulePass *llvm::createNVPTXAssignValidGlobalNamesPass() {
83 return new NVPTXAssignValidGlobalNames();
84}