[llvm] [Bitcode] Decode small byte constants as signed values (PR #203408)

Drew Kersnar via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 11 14:52:17 PDT 2026


https://github.com/dakersnar created https://github.com/llvm/llvm-project/pull/203408

Decode small byte constants the same way we encode them. The bitcode writer stores ConstantByte values as signed integers, so the reader must rebuild them using the signed ConstantByte::get path. This has high-bit values like b8 255 round-trip as their canonical signed form, b8 -1, instead of tripping the APInt width assertion. This matches current i8 behavior.

Before the fix, the new test crashes in llvm-dis with: "APInt.h: Assertion `llvm::isUIntN(BitWidth, val) && "Value is not an N-bit unsigned value"' failed."

Bug found while investigating this PR (https://github.com/llvm/llvm-project/pull/177908), which transitions the LSV to emitting the byte type. Fix assisted by AI.

>From be4e95407da7d341d8c9ce6a17b98715da9d0ae2 Mon Sep 17 00:00:00 2001
From: Drew Kersnar <dkersnar at nvidia.com>
Date: Thu, 11 Jun 2026 19:33:22 +0000
Subject: [PATCH] [Bitcode] Decode small byte constants as signed values

Match the byte constant reader with the signed-rotated encoding used by the bitcode writer so high-bit byte constants round-trip without asserting.
---
 llvm/lib/Bitcode/Reader/BitcodeReader.cpp | 3 ++-
 llvm/test/Bitcode/byte-constants.ll       | 7 +++++++
 2 files changed, 9 insertions(+), 1 deletion(-)
 create mode 100644 llvm/test/Bitcode/byte-constants.ll

diff --git a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
index 2cee5ab00cfcf..ee20bb87fecd7 100644
--- a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
+++ b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
@@ -3370,7 +3370,8 @@ Error BitcodeReader::parseConstants() {
     case bitc::CST_CODE_BYTE: // BYTE: [byteval]
       if (!CurTy->isByteOrByteVectorTy() || Record.empty())
         return error("Invalid byte const record");
-      V = ConstantByte::get(CurTy, decodeSignRotatedValue(Record[0]));
+      V = ConstantByte::get(CurTy, decodeSignRotatedValue(Record[0]),
+                            /*isSigned=*/true);
       break;
     case bitc::CST_CODE_WIDE_BYTE: { // WIDE_BYTE: [n x byteval]
       if (!CurTy->isByteOrByteVectorTy() || Record.empty())
diff --git a/llvm/test/Bitcode/byte-constants.ll b/llvm/test/Bitcode/byte-constants.ll
new file mode 100644
index 0000000000000..a0ab974ed3639
--- /dev/null
+++ b/llvm/test/Bitcode/byte-constants.ll
@@ -0,0 +1,7 @@
+; RUN: llvm-as < %s | llvm-dis | FileCheck %s
+
+; CHECK: @byte.highbit = constant b8 -1
+ at byte.highbit = constant b8 255
+
+; CHECK: @byte.vector.highbit = constant <2 x b8> <b8 -128, b8 -1>
+ at byte.vector.highbit = constant <2 x b8> <b8 128, b8 255>



More information about the llvm-commits mailing list