[llvm] [ThinLTO] Preserve Unicode characters in module paths when writing the combined-index (PR #194320)

Ben Dunbobbin via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 29 00:07:36 PDT 2026


https://github.com/bd1976bris updated https://github.com/llvm/llvm-project/pull/194320

>From f1c8668cc947c1719b2d77de8d3d9aa6a1087006 Mon Sep 17 00:00:00 2001
From: Ben <ben.dunbobbin at sony.com>
Date: Mon, 27 Apr 2026 09:13:23 +0100
Subject: [PATCH 1/2] [ThinLTO] Preserve Unicode characters in module paths

IndexBitcodeWriter::writeModStrings() serializes module path strings
into a SmallVector<unsigned> before emitting MST_CODE_ENTRY records.
When a path contains UTF-8 bytes with the high bit set, appending via
StringRef::begin()/end() can be incorrect. Instead, append the module
path using bytes_begin()/bytes_end() so the bitcode writer always
serializes unsigned bytes.
---
 llvm/lib/Bitcode/Writer/BitcodeWriter.cpp      |  2 +-
 .../Bitcode/thinlto-unicode-module-paths.test  | 18 ++++++++++++++++++
 2 files changed, 19 insertions(+), 1 deletion(-)
 create mode 100644 llvm/test/Bitcode/thinlto-unicode-module-paths.test

diff --git a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
index 71d5a8bc98a4b..a49beaf885e0e 100644
--- a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
+++ b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
@@ -4263,7 +4263,7 @@ void IndexBitcodeWriter::writeModStrings() {
     auto ModuleId = ModuleIdMap.size();
     ModuleIdMap[Key] = ModuleId;
     Vals.push_back(ModuleId);
-    Vals.append(Key.begin(), Key.end());
+    Vals.append(Key.bytes_begin(), Key.bytes_end());
 
     // Emit the finished record.
     Stream.EmitRecord(bitc::MST_CODE_ENTRY, Vals, AbbrevToUse);
diff --git a/llvm/test/Bitcode/thinlto-unicode-module-paths.test b/llvm/test/Bitcode/thinlto-unicode-module-paths.test
new file mode 100644
index 0000000000000..ef339286deda3
--- /dev/null
+++ b/llvm/test/Bitcode/thinlto-unicode-module-paths.test
@@ -0,0 +1,18 @@
+## Test that ThinLTO combined-index handles Unicode characters in module paths.
+
+RUN: rm -rf %t && split-file %s %t && cd %t
+RUN: opt -module-summary α.ll -o α.bc
+RUN: llvm-lto -thinlto-action=thinlink -o index.bc α.bc
+RUN: llvm-bcanalyzer -dump index.bc | FileCheck %s
+
+CHECK:      <MODULE_STRTAB_BLOCK
+CHECK-NEXT:   <ENTRY abbrevid=
+## UTF-8 for "α.bc" is CE B1 2E 62 63.
+CHECK-SAME: op1=206 op2=177 op3=46 op4=98 op5=99/>
+
+#--- α.ll
+target triple = "x86_64-unknown-linux-gnu"
+
+define i32 @f() {
+  ret i32 0
+}

>From 45d2ac99180f136826980f3e1c4119aedee41bac Mon Sep 17 00:00:00 2001
From: Ben <ben.dunbobbin at sony.com>
Date: Wed, 29 Apr 2026 08:07:06 +0100
Subject: [PATCH 2/2] Add comment to explain code change

---
 llvm/lib/Bitcode/Writer/BitcodeWriter.cpp | 1 +
 1 file changed, 1 insertion(+)

diff --git a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
index a49beaf885e0e..cf0aa9befce51 100644
--- a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
+++ b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
@@ -4263,6 +4263,7 @@ void IndexBitcodeWriter::writeModStrings() {
     auto ModuleId = ModuleIdMap.size();
     ModuleIdMap[Key] = ModuleId;
     Vals.push_back(ModuleId);
+    // Use bytes_begin/end() for unsigned char iteration.
     Vals.append(Key.bytes_begin(), Key.bytes_end());
 
     // Emit the finished record.



More information about the llvm-commits mailing list