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 local symbols. |
47 | if (GV.hasLocalLinkage()) { |
48 | // setName doesn't do extra work if the name does not change. |
49 | // Note: this does not create collisions - if setName is asked to set the |
50 | // name to something that already exists, it adds a proper postfix to |
51 | // avoid collisions. |
52 | GV.setName(NVPTX::getValidPTXIdentifier(Name: GV.getName())); |
53 | } |
54 | } |
55 | |
56 | // Do the same for local functions. |
57 | for (Function &F : M.functions()) |
58 | if (F.hasLocalLinkage()) |
59 | F.setName(NVPTX::getValidPTXIdentifier(Name: F.getName())); |
60 | |
61 | return true; |
62 | } |
63 | |
64 | ModulePass *llvm::createNVPTXAssignValidGlobalNamesPass() { |
65 | return new NVPTXAssignValidGlobalNames(); |
66 | } |
67 |