1*0b57cec5SDimitry Andric //===-- NVPTXAssignValidGlobalNames.cpp - Assign valid names to globals ---===// 2*0b57cec5SDimitry Andric // 3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*0b57cec5SDimitry Andric // 7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 8*0b57cec5SDimitry Andric // 9*0b57cec5SDimitry Andric // Clean up the names of global variables in the module to not contain symbols 10*0b57cec5SDimitry Andric // that are invalid in PTX. 11*0b57cec5SDimitry Andric // 12*0b57cec5SDimitry Andric // Currently NVPTX, like other backends, relies on generic symbol name 13*0b57cec5SDimitry Andric // sanitizing done by MC. However, the ptxas assembler is more stringent and 14*0b57cec5SDimitry Andric // disallows some additional characters in symbol names. This pass makes sure 15*0b57cec5SDimitry Andric // such names do not reach MC at all. 16*0b57cec5SDimitry Andric // 17*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 18*0b57cec5SDimitry Andric 19*0b57cec5SDimitry Andric #include "NVPTX.h" 20*0b57cec5SDimitry Andric #include "llvm/IR/Function.h" 21*0b57cec5SDimitry Andric #include "llvm/IR/GlobalVariable.h" 22*0b57cec5SDimitry Andric #include "llvm/IR/LegacyPassManager.h" 23*0b57cec5SDimitry Andric #include "llvm/IR/Module.h" 24*0b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h" 25*0b57cec5SDimitry Andric #include <string> 26*0b57cec5SDimitry Andric 27*0b57cec5SDimitry Andric using namespace llvm; 28*0b57cec5SDimitry Andric 29*0b57cec5SDimitry Andric namespace { 30*0b57cec5SDimitry Andric /// NVPTXAssignValidGlobalNames 31*0b57cec5SDimitry Andric class NVPTXAssignValidGlobalNames : public ModulePass { 32*0b57cec5SDimitry Andric public: 33*0b57cec5SDimitry Andric static char ID; 34*0b57cec5SDimitry Andric NVPTXAssignValidGlobalNames() : ModulePass(ID) {} 35*0b57cec5SDimitry Andric 36*0b57cec5SDimitry Andric bool runOnModule(Module &M) override; 37*0b57cec5SDimitry Andric 38*0b57cec5SDimitry Andric /// Clean up the name to remove symbols invalid in PTX. 39*0b57cec5SDimitry Andric std::string cleanUpName(StringRef Name); 40*0b57cec5SDimitry Andric }; 41*0b57cec5SDimitry Andric } 42*0b57cec5SDimitry Andric 43*0b57cec5SDimitry Andric char NVPTXAssignValidGlobalNames::ID = 0; 44*0b57cec5SDimitry Andric 45*0b57cec5SDimitry Andric namespace llvm { 46*0b57cec5SDimitry Andric void initializeNVPTXAssignValidGlobalNamesPass(PassRegistry &); 47*0b57cec5SDimitry Andric } 48*0b57cec5SDimitry Andric 49*0b57cec5SDimitry Andric INITIALIZE_PASS(NVPTXAssignValidGlobalNames, "nvptx-assign-valid-global-names", 50*0b57cec5SDimitry Andric "Assign valid PTX names to globals", false, false) 51*0b57cec5SDimitry Andric 52*0b57cec5SDimitry Andric bool NVPTXAssignValidGlobalNames::runOnModule(Module &M) { 53*0b57cec5SDimitry Andric for (GlobalVariable &GV : M.globals()) { 54*0b57cec5SDimitry Andric // We are only allowed to rename local symbols. 55*0b57cec5SDimitry Andric if (GV.hasLocalLinkage()) { 56*0b57cec5SDimitry Andric // setName doesn't do extra work if the name does not change. 57*0b57cec5SDimitry Andric // Note: this does not create collisions - if setName is asked to set the 58*0b57cec5SDimitry Andric // name to something that already exists, it adds a proper postfix to 59*0b57cec5SDimitry Andric // avoid collisions. 60*0b57cec5SDimitry Andric GV.setName(cleanUpName(GV.getName())); 61*0b57cec5SDimitry Andric } 62*0b57cec5SDimitry Andric } 63*0b57cec5SDimitry Andric 64*0b57cec5SDimitry Andric // Do the same for local functions. 65*0b57cec5SDimitry Andric for (Function &F : M.functions()) 66*0b57cec5SDimitry Andric if (F.hasLocalLinkage()) 67*0b57cec5SDimitry Andric F.setName(cleanUpName(F.getName())); 68*0b57cec5SDimitry Andric 69*0b57cec5SDimitry Andric return true; 70*0b57cec5SDimitry Andric } 71*0b57cec5SDimitry Andric 72*0b57cec5SDimitry Andric std::string NVPTXAssignValidGlobalNames::cleanUpName(StringRef Name) { 73*0b57cec5SDimitry Andric std::string ValidName; 74*0b57cec5SDimitry Andric raw_string_ostream ValidNameStream(ValidName); 75*0b57cec5SDimitry Andric for (unsigned I = 0, E = Name.size(); I != E; ++I) { 76*0b57cec5SDimitry Andric char C = Name[I]; 77*0b57cec5SDimitry Andric if (C == '.' || C == '@') { 78*0b57cec5SDimitry Andric ValidNameStream << "_$_"; 79*0b57cec5SDimitry Andric } else { 80*0b57cec5SDimitry Andric ValidNameStream << C; 81*0b57cec5SDimitry Andric } 82*0b57cec5SDimitry Andric } 83*0b57cec5SDimitry Andric 84*0b57cec5SDimitry Andric return ValidNameStream.str(); 85*0b57cec5SDimitry Andric } 86*0b57cec5SDimitry Andric 87*0b57cec5SDimitry Andric ModulePass *llvm::createNVPTXAssignValidGlobalNamesPass() { 88*0b57cec5SDimitry Andric return new NVPTXAssignValidGlobalNames(); 89*0b57cec5SDimitry Andric } 90