LLVM 20.0.0git
NVPTXAssignValidGlobalNames.cpp
Go to the documentation of this file.
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"
24#include "llvm/IR/Module.h"
25
26using namespace llvm;
27
28namespace {
29/// NVPTXAssignValidGlobalNames
30class NVPTXAssignValidGlobalNames : public ModulePass {
31public:
32 static char ID;
33 NVPTXAssignValidGlobalNames() : ModulePass(ID) {}
34
35 bool runOnModule(Module &M) override;
36};
37} // namespace
38
39char NVPTXAssignValidGlobalNames::ID = 0;
40
41namespace llvm {
43}
44
45INITIALIZE_PASS(NVPTXAssignValidGlobalNames, "nvptx-assign-valid-global-names",
46 "Assign valid PTX names to globals", false, false)
47
48bool 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
69 return new NVPTXAssignValidGlobalNames();
70}
Module.h This file contains the declarations for the Module class.
#define F(x, y, z)
Definition: MD5.cpp:55
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:38
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:251
virtual bool runOnModule(Module &M)=0
runOnModule - Virtual method overriden by subclasses to process the module being operated on.
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
Definition: PassRegistry.h:37
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
ModulePass * createNVPTXAssignValidGlobalNamesPass()
void initializeNVPTXAssignValidGlobalNamesPass(PassRegistry &)