LLVM API Documentation

NVPTXAssignValidGlobalNames.cpp
Go to the documentation of this file.
00001 //===-- NVPTXAssignValidGlobalNames.cpp - Assign valid names to globals ---===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // Clean up the names of global variables in the module to not contain symbols
00011 // that are invalid in PTX.
00012 //
00013 // Currently NVPTX, like other backends, relies on generic symbol name
00014 // sanitizing done by MC. However, the ptxas assembler is more stringent and
00015 // disallows some additional characters in symbol names. This pass makes sure
00016 // such names do not reach MC at all.
00017 //
00018 //===----------------------------------------------------------------------===//
00019 
00020 #include "NVPTX.h"
00021 #include "llvm/IR/GlobalVariable.h"
00022 #include "llvm/IR/Module.h"
00023 #include "llvm/PassManager.h"
00024 #include "llvm/Support/raw_ostream.h"
00025 #include <string>
00026 
00027 using namespace llvm;
00028 
00029 namespace {
00030 /// \brief NVPTXAssignValidGlobalNames
00031 class NVPTXAssignValidGlobalNames : public ModulePass {
00032 public:
00033   static char ID;
00034   NVPTXAssignValidGlobalNames() : ModulePass(ID) {}
00035 
00036   bool runOnModule(Module &M) override;
00037 
00038   /// \brief Clean up the name to remove symbols invalid in PTX.
00039   std::string cleanUpName(StringRef Name);
00040 };
00041 }
00042 
00043 char NVPTXAssignValidGlobalNames::ID = 0;
00044 
00045 namespace llvm {
00046 void initializeNVPTXAssignValidGlobalNamesPass(PassRegistry &);
00047 }
00048 
00049 INITIALIZE_PASS(NVPTXAssignValidGlobalNames, "nvptx-assign-valid-global-names",
00050                 "Assign valid PTX names to globals", false, false)
00051 
00052 bool NVPTXAssignValidGlobalNames::runOnModule(Module &M) {
00053   for (GlobalVariable &GV : M.globals()) {
00054     // We are only allowed to rename local symbols.
00055     if (GV.hasLocalLinkage()) {
00056       // setName doesn't do extra work if the name does not change.
00057       // Note: this does not create collisions - if setName is asked to set the
00058       // name to something that already exists, it adds a proper postfix to
00059       // avoid collisions.
00060       GV.setName(cleanUpName(GV.getName()));
00061     }
00062   }
00063 
00064   return true;
00065 }
00066 
00067 std::string NVPTXAssignValidGlobalNames::cleanUpName(StringRef Name) {
00068   std::string ValidName;
00069   raw_string_ostream ValidNameStream(ValidName);
00070   for (unsigned I = 0, E = Name.size(); I != E; ++I) {
00071     char C = Name[I];
00072     if (C == '.' || C == '@') {
00073       ValidNameStream << "_$_";
00074     } else {
00075       ValidNameStream << C;
00076     }
00077   }
00078 
00079   return ValidNameStream.str();
00080 }
00081 
00082 ModulePass *llvm::createNVPTXAssignValidGlobalNamesPass() {
00083   return new NVPTXAssignValidGlobalNames();
00084 }