| 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 | INITIALIZE_PASS(NVPTXAssignValidGlobalNames, "nvptx-assign-valid-global-names" , |
| 42 | "Assign valid PTX names to globals" , false, false) |
| 43 | |
| 44 | bool NVPTXAssignValidGlobalNames::runOnModule(Module &M) { |
| 45 | for (GlobalVariable &GV : M.globals()) { |
| 46 | // We are only allowed to rename symbols that are not externally linked by |
| 47 | // name |
| 48 | // - local symbols, as all references will be renamed |
| 49 | // - .extern .shared symbols, as they're the same regardless of name |
| 50 | if (GV.hasLocalLinkage() || |
| 51 | (GV.hasExternalLinkage() && |
| 52 | GV.getAddressSpace() == NVPTX::AddressSpace::Shared)) { |
| 53 | // setName doesn't do extra work if the name does not change. |
| 54 | // Note: this does not create collisions - if setName is asked to set the |
| 55 | // name to something that already exists, it adds a proper postfix to |
| 56 | // avoid collisions. |
| 57 | GV.setName(NVPTX::getValidPTXIdentifier(Name: GV.getName())); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | // Do the same for local functions. |
| 62 | for (Function &F : M.functions()) |
| 63 | if (F.hasLocalLinkage()) |
| 64 | F.setName(NVPTX::getValidPTXIdentifier(Name: F.getName())); |
| 65 | |
| 66 | return true; |
| 67 | } |
| 68 | |
| 69 | ModulePass *llvm::createNVPTXAssignValidGlobalNamesPass() { |
| 70 | return new NVPTXAssignValidGlobalNames(); |
| 71 | } |
| 72 | |