Skip to content

Commit

Permalink
[NVPTX] improve identifier renaming for PTX (#79459)
Browse files Browse the repository at this point in the history
Update `NVPTXAssignValidGlobalNames` to convert all characters which are
illegal in PTX identifiers to `_$_`. ([PTX ISA: 4.4
Identifiers](https://docs.nvidia.com/cuda/parallel-thread-execution/index.html#identifiers)).
  • Loading branch information
AlexMaclean authored Jan 26, 2024
1 parent 2b25e40 commit 1d5820a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
9 changes: 6 additions & 3 deletions llvm/lib/Target/NVPTX/NVPTXAssignValidGlobalNames.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
//===----------------------------------------------------------------------===//

#include "NVPTX.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/GlobalVariable.h"
#include "llvm/IR/LegacyPassManager.h"
Expand Down Expand Up @@ -73,10 +74,12 @@ std::string NVPTXAssignValidGlobalNames::cleanUpName(StringRef Name) {
std::string ValidName;
raw_string_ostream ValidNameStream(ValidName);
for (char C : Name) {
if (C == '.' || C == '@' || C == '<' || C == '>') {
ValidNameStream << "_$_";
} else {
// While PTX also allows '%' at the start of identifiers, LLVM will throw a
// fatal error for '%' in symbol names in MCSymbol::print. Exclude for now.
if (isAlnum(C) || C == '_' || C == '$') {
ValidNameStream << C;
} else {
ValidNameStream << "_$_";
}
}

Expand Down
5 changes: 4 additions & 1 deletion llvm/test/CodeGen/NVPTX/symbol-naming.ll
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@

; CHECK-NOT: .str
; CHECK-NOT: <str>
; CHECK-NOT: another-str
; CHECK-NOT: .function.

; CHECK-DAG: _$_str
; CHECK-DAG: _$_str_$_
; CHECK-DAG: _$_str1
; CHECK-DAG: another_$_str

; CHECK-DAG: _$_function_$_
; CHECK-DAG: _$_function_$_2
Expand All @@ -24,6 +26,7 @@ target triple = "nvptx64-unknown-unknown"
@.str = private unnamed_addr constant [13 x i8] c"%d %f %c %d\0A\00", align 1
@"<str>" = private unnamed_addr constant [13 x i8] c"%d %f %c %d\0A\00", align 1
@_$_str = private unnamed_addr constant [13 x i8] c"%d %f %c %d\0A\00", align 1
@another-str = private unnamed_addr constant [13 x i8] c"%d %f %c %d\0A\00", align 1


; Function Attrs: nounwind
Expand All @@ -38,7 +41,7 @@ entry:
define internal void @_$_function_$_() {
entry:
%call = call i32 (ptr, ...) @printf(ptr @_$_str)
%call2 = call i32 (ptr, ...) @printf(ptr @"<str>")
%call2 = call i32 (ptr, ...) @printf(ptr @another-str)
ret void
}

Expand Down

0 comments on commit 1d5820a

Please sign in to comment.