LLVM 24.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
28/// Give \p GV a name that is a valid PTX identifier, returning whether it had
29/// to be renamed. Invalid names are rare, so checking first lets the pass
30/// report accurately that it left the module alone.
31///
32/// Note: this does not create collisions - if setName is asked to set the name
33/// to something that already exists, it adds a proper postfix to avoid
34/// collisions.
35static bool assignValidName(GlobalValue &GV) {
36 std::string ValidName = NVPTX::getValidPTXIdentifier(GV.getName());
37 if (ValidName == GV.getName())
38 return false;
39 GV.setName(ValidName);
40 return true;
41}
42
44 bool Changed = false;
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))
54 }
55
56 // Do the same for local functions.
57 for (Function &F : M.functions())
58 if (F.hasLocalLinkage())
60
61 return Changed;
62}
63
64namespace {
65/// NVPTXAssignValidGlobalNamesLegacyPass
66class NVPTXAssignValidGlobalNamesLegacyPass : public ModulePass {
67public:
68 static char ID;
69 NVPTXAssignValidGlobalNamesLegacyPass() : ModulePass(ID) {}
70
71 bool runOnModule(Module &M) override { return assignValidGlobalNames(M); }
72};
73} // namespace
74
75char NVPTXAssignValidGlobalNamesLegacyPass::ID = 0;
76
77INITIALIZE_PASS(NVPTXAssignValidGlobalNamesLegacyPass,
78 "nvptx-assign-valid-global-names",
79 "Assign valid PTX names to globals", false, false)
80
82 return new NVPTXAssignValidGlobalNamesLegacyPass();
83}
84
Module.h This file contains the declarations for the Module class.
#define F(x, y, z)
Definition MD5.cpp:54
static bool assignValidName(GlobalValue &GV)
Give GV a name that is a valid PTX identifier, returning whether it had to be renamed.
static bool assignValidGlobalNames(Module &M)
ModuleAnalysisManager MAM
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
Represents analyses that only rely on functions' control flow.
Definition Analysis.h:73
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition Pass.h:255
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM)
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition Analysis.h:115
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
Definition Analysis.h:151
LLVM_ABI void setName(const Twine &Name)
Change the name of the value.
Definition Value.cpp:394
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:319
Changed
std::string getValidPTXIdentifier(StringRef Name)
This is an optimization pass for GlobalISel generic memory operations.
ModulePass * createNVPTXAssignValidGlobalNamesLegacyPass()
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
Definition MIRParser.h:39