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 "NVPTXUtilities.h" 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 26 using namespace llvm; 27 28 namespace { 29 /// NVPTXAssignValidGlobalNames 30 class NVPTXAssignValidGlobalNames : public ModulePass { 31 public: 32 static char ID; 33 NVPTXAssignValidGlobalNames() : ModulePass(ID) {} 34 35 bool runOnModule(Module &M) override; 36 }; 37 } // namespace 38 39 char NVPTXAssignValidGlobalNames::ID = 0; 40 41 namespace llvm { 42 void initializeNVPTXAssignValidGlobalNamesPass(PassRegistry &); 43 } 44 45 INITIALIZE_PASS(NVPTXAssignValidGlobalNames, "nvptx-assign-valid-global-names", 46 "Assign valid PTX names to globals", false, false) 47 48 bool NVPTXAssignValidGlobalNames::runOnModule(Module &M) { 49 for (GlobalVariable &GV : M.globals()) { 50 // We are only allowed to rename local symbols. 51 if (GV.hasLocalLinkage()) { 52 // setName doesn't do extra work if the name does not change. 53 // Note: this does not create collisions - if setName is asked to set the 54 // name to something that already exists, it adds a proper postfix to 55 // avoid collisions. 56 GV.setName(NVPTX::getValidPTXIdentifier(GV.getName())); 57 } 58 } 59 60 // Do the same for local functions. 61 for (Function &F : M.functions()) 62 if (F.hasLocalLinkage()) 63 F.setName(NVPTX::getValidPTXIdentifier(F.getName())); 64 65 return true; 66 } 67 68 ModulePass *llvm::createNVPTXAssignValidGlobalNamesPass() { 69 return new NVPTXAssignValidGlobalNames(); 70 } 71