[llvm] [DebugInfo] Escape strings echoed into .debug_str comments (PR #223384)

via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 14 05:28:24 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-debuginfo

Author: Tim Besard (maleadt)

<details>
<summary>Changes</summary>

Since 1a23bca645dc, `DwarfStringPool::emit` echoes each string into the assembler comment next to its bytes. Strings can contain arbitrary bytes, e.g. non-ASCII identifiers, and some assemblers reject those even inside comments: `ptxas` (before CUDA 13.1) aborts with "Unexpected non-ASCII character encountered" (JuliaGPU/CUDA.jl#<!-- -->3274). Escape the string, as is done for the strings echoed into `.debug_info` comments.

---
Full diff: https://github.com/llvm/llvm-project/pull/223384.diff


2 Files Affected:

- (modified) llvm/lib/CodeGen/AsmPrinter/DwarfStringPool.cpp (+12-5) 
- (added) llvm/test/DebugInfo/NVPTX/debug-str-nonascii.ll (+31) 


``````````diff
diff --git a/llvm/lib/CodeGen/AsmPrinter/DwarfStringPool.cpp b/llvm/lib/CodeGen/AsmPrinter/DwarfStringPool.cpp
index 4ccad65bdba91..7c32ea80e8af4 100644
--- a/llvm/lib/CodeGen/AsmPrinter/DwarfStringPool.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/DwarfStringPool.cpp
@@ -7,11 +7,12 @@
 //===----------------------------------------------------------------------===//
 
 #include "DwarfStringPool.h"
+#include "llvm/ADT/SmallString.h"
 #include "llvm/ADT/SmallVector.h"
-#include "llvm/ADT/Twine.h"
 #include "llvm/CodeGen/AsmPrinter.h"
 #include "llvm/MC/MCAsmInfo.h"
 #include "llvm/MC/MCStreamer.h"
+#include "llvm/Support/raw_ostream.h"
 #include <cassert>
 
 using namespace llvm;
@@ -96,10 +97,16 @@ void DwarfStringPool::emit(AsmPrinter &Asm, MCSection *StrSection,
     if (ShouldCreateSymbols)
       Asm.OutStreamer->emitLabel(Entry->getValue().Symbol);
 
-    // Emit a comment with the string offset and the string itself.
-    Asm.OutStreamer->AddComment(
-        "string offset=" + Twine(Entry->getValue().Offset) + " ; " +
-        StringRef(Entry->getKeyData(), Entry->getKeyLength()));
+    // Emit a comment with the string offset and the string itself. The string
+    // is escaped: it can contain arbitrary bytes (e.g. non-ASCII identifiers),
+    // and some assemblers, like ptxas, reject those even inside comments.
+    SmallString<128> Comment;
+    raw_svector_ostream CommentOS(Comment);
+    CommentOS << "string offset=" << Entry->getValue().Offset << " ; ";
+    CommentOS.write_escaped(
+        StringRef(Entry->getKeyData(), Entry->getKeyLength()),
+        /*UseHexEscapes=*/true);
+    Asm.OutStreamer->AddComment(Comment);
 
     // Emit the string itself with a terminating null byte.
     Asm.OutStreamer->emitBytes(
diff --git a/llvm/test/DebugInfo/NVPTX/debug-str-nonascii.ll b/llvm/test/DebugInfo/NVPTX/debug-str-nonascii.ll
new file mode 100644
index 0000000000000..589548f9d3475
--- /dev/null
+++ b/llvm/test/DebugInfo/NVPTX/debug-str-nonascii.ll
@@ -0,0 +1,31 @@
+; RUN: llc < %s -mtriple=nvptx64-nvidia-cuda -mattr=+ptx72 | FileCheck %s
+;
+;; The comments next to .debug_str entries must escape the string: ptxas
+;; (before CUDA 13.1) rejects non-ASCII bytes anywhere in its input, even
+;; in comments, and function names can contain arbitrary UTF-8.
+
+; CHECK: .loc {{[0-9]+}} 2 0, function_name [[NAME:\$L__info_string[0-9]+]], inlined_at
+; CHECK: .section .debug_str
+; CHECK: [[NAME]]:
+; CHECK-NEXT: .b8 206 // string offset={{[0-9]+}} ; \xCE\x94x
+; CHECK-NOT: Δ
+
+target triple = "nvptx64-nvidia-cuda"
+
+define ptx_kernel void @kernel(ptr %a) !dbg !4 {
+  store i64 1, ptr %a, !dbg !7
+  ret void, !dbg !8
+}
+
+!llvm.module.flags = !{!0, !1}
+!llvm.dbg.cu = !{!2}
+!0 = !{i32 2, !"Dwarf Version", i32 2}
+!1 = !{i32 2, !"Debug Info Version", i32 3}
+!2 = distinct !DICompileUnit(language: DW_LANG_C, file: !3, producer: "test", isOptimized: true, runtimeVersion: 0, emissionKind: LineTablesOnly)
+!3 = !DIFile(filename: "test.cu", directory: ".")
+!4 = distinct !DISubprogram(name: "kernel", linkageName: "kernel", scope: null, file: !3, line: 1, type: !5, scopeLine: 1, spFlags: DISPFlagDefinition, unit: !2)
+!5 = !DISubroutineType(types: !6)
+!6 = !{}
+!7 = !DILocation(line: 2, scope: !9, inlinedAt: !8)
+!8 = !DILocation(line: 3, scope: !4)
+!9 = distinct !DISubprogram(name: "Δx", linkageName: "Δx", scope: null, file: !3, line: 10, type: !5, scopeLine: 10, spFlags: DISPFlagDefinition, unit: !2)

``````````

</details>


https://github.com/llvm/llvm-project/pull/223384


More information about the llvm-commits mailing list