[llvm] [NVPTX] improve identifier renaming for PTX (PR #79459)
Alex MacLean via llvm-commits
llvm-commits at lists.llvm.org
Fri Jan 26 13:29:57 PST 2024
https://github.com/AlexMaclean updated https://github.com/llvm/llvm-project/pull/79459
>From 1e0cb7e58d11ad1745eb3161cc9ec80a34c85772 Mon Sep 17 00:00:00 2001
From: Alex MacLean <amaclean at nvidia.com>
Date: Wed, 24 Jan 2024 19:46:46 +0000
Subject: [PATCH 1/2] [NVPTX] improve identifier renaming for PTX
---
llvm/lib/Target/NVPTX/NVPTXAssignValidGlobalNames.cpp | 7 ++++---
llvm/test/CodeGen/NVPTX/symbol-naming.ll | 5 ++++-
2 files changed, 8 insertions(+), 4 deletions(-)
diff --git a/llvm/lib/Target/NVPTX/NVPTXAssignValidGlobalNames.cpp b/llvm/lib/Target/NVPTX/NVPTXAssignValidGlobalNames.cpp
index 789995743861270..229bfa20ad740ec 100644
--- a/llvm/lib/Target/NVPTX/NVPTXAssignValidGlobalNames.cpp
+++ b/llvm/lib/Target/NVPTX/NVPTXAssignValidGlobalNames.cpp
@@ -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"
@@ -73,10 +74,10 @@ 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 {
+ if (isAlnum(C) || C == '_' || C == '$') {
ValidNameStream << C;
+ } else {
+ ValidNameStream << "_$_";
}
}
diff --git a/llvm/test/CodeGen/NVPTX/symbol-naming.ll b/llvm/test/CodeGen/NVPTX/symbol-naming.ll
index ba85e1fc73743c0..780bc7422f739d7 100644
--- a/llvm/test/CodeGen/NVPTX/symbol-naming.ll
+++ b/llvm/test/CodeGen/NVPTX/symbol-naming.ll
@@ -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
@@ -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
+ at another-str = private unnamed_addr constant [13 x i8] c"%d %f %c %d\0A\00", align 1
; Function Attrs: nounwind
@@ -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
}
>From a422079561e0bbf9f885939119ea9f0cab570e1a Mon Sep 17 00:00:00 2001
From: Alex MacLean <amaclean at nvidia.com>
Date: Fri, 26 Jan 2024 21:29:02 +0000
Subject: [PATCH 2/2] add comment explaining '%'
---
llvm/lib/Target/NVPTX/NVPTXAssignValidGlobalNames.cpp | 2 ++
1 file changed, 2 insertions(+)
diff --git a/llvm/lib/Target/NVPTX/NVPTXAssignValidGlobalNames.cpp b/llvm/lib/Target/NVPTX/NVPTXAssignValidGlobalNames.cpp
index 229bfa20ad740ec..9f5a217795183b7 100644
--- a/llvm/lib/Target/NVPTX/NVPTXAssignValidGlobalNames.cpp
+++ b/llvm/lib/Target/NVPTX/NVPTXAssignValidGlobalNames.cpp
@@ -74,6 +74,8 @@ std::string NVPTXAssignValidGlobalNames::cleanUpName(StringRef Name) {
std::string ValidName;
raw_string_ostream ValidNameStream(ValidName);
for (char C : Name) {
+ // 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 {
More information about the llvm-commits
mailing list