[llvm] 3255d4d - [Bitcode] Decode small byte constants as signed values (#203408)

via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 12 07:35:33 PDT 2026


Author: Drew Kersnar
Date: 2026-06-12T09:35:28-05:00
New Revision: 3255d4d7628719c4b6254db5780ec50862584896

URL: https://github.com/llvm/llvm-project/commit/3255d4d7628719c4b6254db5780ec50862584896
DIFF: https://github.com/llvm/llvm-project/commit/3255d4d7628719c4b6254db5780ec50862584896.diff

LOG: [Bitcode] Decode small byte constants as signed values (#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.

Added: 
    llvm/test/Bitcode/byte-constants.ll

Modified: 
    llvm/lib/Bitcode/Reader/BitcodeReader.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
index 87ae36afb4e65..f46d240e7ed9b 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