Path: blob/main/contrib/llvm-project/llvm/lib/Target/NVPTX/NVPTXAssignValidGlobalNames.cpp
35271 views
//===-- NVPTXAssignValidGlobalNames.cpp - Assign valid names to globals ---===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// Clean up the names of global variables in the module to not contain symbols9// that are invalid in PTX.10//11// Currently NVPTX, like other backends, relies on generic symbol name12// sanitizing done by MC. However, the ptxas assembler is more stringent and13// disallows some additional characters in symbol names. This pass makes sure14// such names do not reach MC at all.15//16//===----------------------------------------------------------------------===//1718#include "NVPTX.h"19#include "llvm/ADT/StringExtras.h"20#include "llvm/IR/Function.h"21#include "llvm/IR/GlobalVariable.h"22#include "llvm/IR/LegacyPassManager.h"23#include "llvm/IR/Module.h"24#include "llvm/Support/raw_ostream.h"25#include <string>2627using namespace llvm;2829namespace {30/// NVPTXAssignValidGlobalNames31class NVPTXAssignValidGlobalNames : public ModulePass {32public:33static char ID;34NVPTXAssignValidGlobalNames() : ModulePass(ID) {}3536bool runOnModule(Module &M) override;3738/// Clean up the name to remove symbols invalid in PTX.39std::string cleanUpName(StringRef Name);40};41}4243char NVPTXAssignValidGlobalNames::ID = 0;4445namespace llvm {46void initializeNVPTXAssignValidGlobalNamesPass(PassRegistry &);47}4849INITIALIZE_PASS(NVPTXAssignValidGlobalNames, "nvptx-assign-valid-global-names",50"Assign valid PTX names to globals", false, false)5152bool NVPTXAssignValidGlobalNames::runOnModule(Module &M) {53for (GlobalVariable &GV : M.globals()) {54// We are only allowed to rename local symbols.55if (GV.hasLocalLinkage()) {56// setName doesn't do extra work if the name does not change.57// Note: this does not create collisions - if setName is asked to set the58// name to something that already exists, it adds a proper postfix to59// avoid collisions.60GV.setName(cleanUpName(GV.getName()));61}62}6364// Do the same for local functions.65for (Function &F : M.functions())66if (F.hasLocalLinkage())67F.setName(cleanUpName(F.getName()));6869return true;70}7172std::string NVPTXAssignValidGlobalNames::cleanUpName(StringRef Name) {73std::string ValidName;74raw_string_ostream ValidNameStream(ValidName);75for (char C : Name) {76// While PTX also allows '%' at the start of identifiers, LLVM will throw a77// fatal error for '%' in symbol names in MCSymbol::print. Exclude for now.78if (isAlnum(C) || C == '_' || C == '$') {79ValidNameStream << C;80} else {81ValidNameStream << "_$_";82}83}8485return ValidNameStream.str();86}8788ModulePass *llvm::createNVPTXAssignValidGlobalNamesPass() {89return new NVPTXAssignValidGlobalNames();90}919293