Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Target/NVPTX/NVPTXAssignValidGlobalNames.cpp
Line
Count
Source
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 "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>
26
27
using namespace llvm;
28
29
namespace {
30
/// NVPTXAssignValidGlobalNames
31
class NVPTXAssignValidGlobalNames : public ModulePass {
32
public:
33
  static char ID;
34
263
  NVPTXAssignValidGlobalNames() : ModulePass(ID) {}
35
36
  bool runOnModule(Module &M) override;
37
38
  /// Clean up the name to remove symbols invalid in PTX.
39
  std::string cleanUpName(StringRef Name);
40
};
41
}
42
43
char NVPTXAssignValidGlobalNames::ID = 0;
44
45
namespace llvm {
46
void initializeNVPTXAssignValidGlobalNamesPass(PassRegistry &);
47
}
48
49
INITIALIZE_PASS(NVPTXAssignValidGlobalNames, "nvptx-assign-valid-global-names",
50
                "Assign valid PTX names to globals", false, false)
51
52
258
bool NVPTXAssignValidGlobalNames::runOnModule(Module &M) {
53
258
  for (GlobalVariable &GV : M.globals()) {
54
80
    // We are only allowed to rename local symbols.
55
80
    if (GV.hasLocalLinkage()) {
56
30
      // setName doesn't do extra work if the name does not change.
57
30
      // Note: this does not create collisions - if setName is asked to set the
58
30
      // name to something that already exists, it adds a proper postfix to
59
30
      // avoid collisions.
60
30
      GV.setName(cleanUpName(GV.getName()));
61
30
    }
62
80
  }
63
258
64
258
  // Do the same for local functions.
65
258
  for (Function &F : M.functions())
66
2.27k
    if (F.hasLocalLinkage())
67
8
      F.setName(cleanUpName(F.getName()));
68
258
69
258
  return true;
70
258
}
71
72
38
std::string NVPTXAssignValidGlobalNames::cleanUpName(StringRef Name) {
73
38
  std::string ValidName;
74
38
  raw_string_ostream ValidNameStream(ValidName);
75
330
  for (unsigned I = 0, E = Name.size(); I != E; 
++I292
) {
76
292
    char C = Name[I];
77
292
    if (C == '.' || 
C == '@'286
) {
78
6
      ValidNameStream << "_$_";
79
286
    } else {
80
286
      ValidNameStream << C;
81
286
    }
82
292
  }
83
38
84
38
  return ValidNameStream.str();
85
38
}
86
87
263
ModulePass *llvm::createNVPTXAssignValidGlobalNamesPass() {
88
263
  return new NVPTXAssignValidGlobalNames();
89
263
}