Skip to content

Commit 5db24d7

Browse files
committedDec 4, 2017
[NVPTX] Assign valid global names
PTX requires that identifiers consist only of [a-zA-Z0-9_$]. The existing pass already ensured this for globals and this patch adds the cleanup for functions with local linkage. However, there was a different problem in the case of collisions of the adjusted name: The ValueSymbolTable then automatically appended ".N" with increasing Ns to get a unique name while helping the ABI demangling. Special case this behavior to omit the dots and append N directly. This will always give us legal names according to the PTX requirements. Differential Revision: https://reviews.llvm.org/D40573 llvm-svn: 319657
1 parent 64774ba commit 5db24d7

File tree

3 files changed

+43
-11
lines changed

3 files changed

+43
-11
lines changed
 

‎llvm/lib/IR/ValueSymbolTable.cpp

+13-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313

1414
#include "llvm/IR/ValueSymbolTable.h"
1515
#include "llvm/ADT/SmallString.h"
16+
#include "llvm/ADT/Triple.h"
1617
#include "llvm/IR/GlobalValue.h"
18+
#include "llvm/IR/Module.h"
1719
#include "llvm/IR/Type.h"
1820
#include "llvm/IR/Value.h"
1921
#include "llvm/Support/Casting.h"
@@ -45,8 +47,17 @@ ValueName *ValueSymbolTable::makeUniqueName(Value *V,
4547
// Trim any suffix off and append the next number.
4648
UniqueName.resize(BaseSize);
4749
raw_svector_ostream S(UniqueName);
48-
if (isa<GlobalValue>(V))
49-
S << ".";
50+
if (auto *GV = dyn_cast<GlobalValue>(V)) {
51+
// A dot is appended to mark it as clone during ABI demangling so that
52+
// for example "_Z1fv" and "_Z1fv.1" both demangle to "f()", the second
53+
// one being a clone.
54+
// On NVPTX we cannot use a dot because PTX only allows [A-Za-z0-9_$] for
55+
// identifiers. This breaks ABI demangling but at least ptxas accepts and
56+
// compiles the program.
57+
const Module *M = GV->getParent();
58+
if (!(M && Triple(M->getTargetTriple()).isNVPTX()))
59+
S << ".";
60+
}
5061
S << ++LastUnique;
5162

5263
// Try insert the vmap entry with this suffix.

‎llvm/lib/Target/NVPTX/NVPTXAssignValidGlobalNames.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
//===----------------------------------------------------------------------===//
1919

2020
#include "NVPTX.h"
21+
#include "llvm/IR/Function.h"
2122
#include "llvm/IR/GlobalVariable.h"
2223
#include "llvm/IR/LegacyPassManager.h"
2324
#include "llvm/IR/Module.h"
@@ -61,6 +62,11 @@ bool NVPTXAssignValidGlobalNames::runOnModule(Module &M) {
6162
}
6263
}
6364

65+
// Do the same for local functions.
66+
for (Function &F : M.functions())
67+
if (F.hasLocalLinkage())
68+
F.setName(cleanUpName(F.getName()));
69+
6470
return true;
6571
}
6672

+24-9
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
; RUN: llc < %s -march=nvptx -mcpu=sm_20 | FileCheck %s --check-prefix=PTX32
2-
; RUN: llc < %s -march=nvptx64 -mcpu=sm_20 | FileCheck %s --check-prefix=PTX64
1+
; RUN: llc < %s -march=nvptx -mcpu=sm_20 | FileCheck %s
2+
; RUN: llc < %s -march=nvptx64 -mcpu=sm_20 | FileCheck %s
33

44
; Verify that the NVPTX target removes invalid symbol names prior to emitting
55
; PTX.
66

7-
; PTX32-NOT: .str
8-
; PTX64-NOT: .str
7+
; CHECK-NOT: .str
8+
; CHECK-NOT: .function.
99

10-
; PTX32-DAG: _$_str.1
11-
; PTX32-DAG: _$_str
10+
; CHECK-DAG: _$_str
11+
; CHECK-DAG: _$_str1
1212

13-
; PTX64-DAG: _$_str.1
14-
; PTX64-DAG: _$_str
13+
; CHECK-DAG: _$_function_$_
14+
; CHECK-DAG: _$_function_$_2
1515

1616
target datalayout = "e-i64:64-v16:16-v32:32-n16:32:64"
1717
target triple = "nvptx64-unknown-unknown"
@@ -22,10 +22,25 @@ target triple = "nvptx64-unknown-unknown"
2222

2323

2424
; Function Attrs: nounwind
25-
define void @foo(i32 %a, float %b, i8 signext %c, i32 %e) {
25+
define internal void @.function.() {
2626
entry:
2727
%call = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i32 0, i32 0))
2828
ret void
2929
}
3030

31+
; Function Attrs: nounwind
32+
define internal void @_$_function_$_() {
33+
entry:
34+
%call = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @_$_str, i32 0, i32 0))
35+
ret void
36+
}
37+
38+
; Function Attrs: nounwind
39+
define void @global_function() {
40+
entry:
41+
call void @.function.()
42+
call void @_$_function_$_()
43+
ret void
44+
}
45+
3146
declare i32 @printf(i8*, ...)

0 commit comments

Comments
 (0)
Please sign in to comment.