[llvm] [llubi] Fix wrong assert when writing `poison` at an unaligned bit offset (PR #230819)

Pedro Lobo via llvm-commits llvm-commits at lists.llvm.org
Sat Oct 10 15:32:44 PDT 2026


https://github.com/pedroclobo updated https://github.com/llvm/llvm-project/pull/230819

>From c084c07241d320767d175138112bc3bc8f6ddee8 Mon Sep 17 00:00:00 2001
From: Pedro Lobo <pedro.lobo at tecnico.ulisboa.pt>
Date: Sat, 10 Oct 2026 18:40:22 +0100
Subject: [PATCH 1/2] [llubi] Fix wrong assert when writing `poison` at an
 unaligned bit offset

---
 llvm/test/tools/llubi/bitcast_be.ll | 4 ++++
 llvm/test/tools/llubi/bitcast_le.ll | 4 ++++
 llvm/tools/llubi/lib/Context.cpp    | 9 +++++----
 3 files changed, 13 insertions(+), 4 deletions(-)

diff --git a/llvm/test/tools/llubi/bitcast_be.ll b/llvm/test/tools/llubi/bitcast_be.ll
index d0b3163669c40..5e3a9624b1c1b 100644
--- a/llvm/test/tools/llubi/bitcast_be.ll
+++ b/llvm/test/tools/llubi/bitcast_be.ll
@@ -20,6 +20,8 @@ entry:
   %bitcast_scalar2vec1 = bitcast i64 1 to <2 x i32>
   %bitcast_scalar2vec2 = bitcast i16 32768 to <16 x i1>
   %bitcast_vec2scalar_partial_poison = bitcast <2 x i32> <i32 poison, i32 0> to i64
+  %bitcast_vec2byte_poison_lane0 = bitcast <2 x i12> <i12 poison, i12 0> to b24
+  %bitcast_vec2byte_poison_lane1 = bitcast <2 x i12> <i12 0, i12 poison> to b24
   %bitcast_scalar2vec_poison = bitcast i64 poison to <2 x i32>
 
   %bitcast_vec2vec_up = bitcast <2 x i32> <i32 1, i32 poison> to <4 x i16>
@@ -51,6 +53,8 @@ entry:
 ; CHECK-NEXT:   %bitcast_scalar2vec1 = bitcast i64 1 to <2 x i32> => { i32 0, i32 1 }
 ; CHECK-NEXT:   %bitcast_scalar2vec2 = bitcast i16 -32768 to <16 x i1> => { T, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F }
 ; CHECK-NEXT:   %bitcast_vec2scalar_partial_poison = bitcast <2 x i32> <i32 poison, i32 0> to i64 => poison
+; CHECK-NEXT:   %bitcast_vec2byte_poison_lane0 = bitcast <2 x i12> <i12 poison, i12 0> to b24 => b24 0x!! !!!!0000 0x00
+; CHECK-NEXT:   %bitcast_vec2byte_poison_lane1 = bitcast <2 x i12> <i12 0, i12 poison> to b24 => b24 0x00 0000!!!! 0x!!
 ; CHECK-NEXT:   %bitcast_scalar2vec_poison = bitcast i64 poison to <2 x i32> => { poison, poison }
 ; CHECK-NEXT:   %bitcast_vec2vec_up = bitcast <2 x i32> <i32 1, i32 poison> to <4 x i16> => { i16 0, i16 1, poison, poison }
 ; CHECK-NEXT:   %bitcast_vec2vec_down1 = bitcast <4 x i16> <i16 0, i16 poison, i16 2, i16 3> to <2 x i32> => { poison, i32 131075 }
diff --git a/llvm/test/tools/llubi/bitcast_le.ll b/llvm/test/tools/llubi/bitcast_le.ll
index acb4af6dd73d9..d0492b03db492 100644
--- a/llvm/test/tools/llubi/bitcast_le.ll
+++ b/llvm/test/tools/llubi/bitcast_le.ll
@@ -20,6 +20,8 @@ entry:
   %bitcast_scalar2vec1 = bitcast i64 1 to <2 x i32>
   %bitcast_scalar2vec2 = bitcast i16 32768 to <16 x i1>
   %bitcast_vec2scalar_partial_poison = bitcast <2 x i32> <i32 poison, i32 0> to i64
+  %bitcast_vec2byte_poison_lane0 = bitcast <2 x i12> <i12 poison, i12 0> to b24
+  %bitcast_vec2byte_poison_lane1 = bitcast <2 x i12> <i12 0, i12 poison> to b24
   %bitcast_scalar2vec_poison = bitcast i64 poison to <2 x i32>
 
   %bitcast_vec2vec_up = bitcast <2 x i32> <i32 1, i32 poison> to <4 x i16>
@@ -51,6 +53,8 @@ entry:
 ; CHECK-NEXT:   %bitcast_scalar2vec1 = bitcast i64 1 to <2 x i32> => { i32 1, i32 0 }
 ; CHECK-NEXT:   %bitcast_scalar2vec2 = bitcast i16 -32768 to <16 x i1> => { F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, T }
 ; CHECK-NEXT:   %bitcast_vec2scalar_partial_poison = bitcast <2 x i32> <i32 poison, i32 0> to i64 => poison
+; CHECK-NEXT:   %bitcast_vec2byte_poison_lane0 = bitcast <2 x i12> <i12 poison, i12 0> to b24 => b24 0x!! 0000!!!! 0x00
+; CHECK-NEXT:   %bitcast_vec2byte_poison_lane1 = bitcast <2 x i12> <i12 0, i12 poison> to b24 => b24 0x00 !!!!0000 0x!!
 ; CHECK-NEXT:   %bitcast_scalar2vec_poison = bitcast i64 poison to <2 x i32> => { poison, poison }
 ; CHECK-NEXT:   %bitcast_vec2vec_up = bitcast <2 x i32> <i32 1, i32 poison> to <4 x i16> => { i16 1, i16 0, poison, poison }
 ; CHECK-NEXT:   %bitcast_vec2vec_down1 = bitcast <4 x i16> <i16 0, i16 poison, i16 2, i16 3> to <2 x i32> => { poison, i32 196610 }
diff --git a/llvm/tools/llubi/lib/Context.cpp b/llvm/tools/llubi/lib/Context.cpp
index ff4d14b5a2c1b..ac04257296099 100644
--- a/llvm/tools/llubi/lib/Context.cpp
+++ b/llvm/tools/llubi/lib/Context.cpp
@@ -701,11 +701,12 @@ void Context::toBytes(const AnyValue &Val, Type *Ty, uint32_t OffsetInBits,
   };
   if (Val.isPoison()) {
     for (uint32_t I = 0, E = NewOffsetInBits - OffsetInBits; I < E;) {
-      uint32_t NumBitsInByte = std::min(8 - (OffsetInBits + I) % 8, E - I);
-      assert(((OffsetInBits ^ (OffsetInBits + NumBitsInByte - 1)) & ~7) == 0 &&
+      uint32_t BitsStart = OffsetInBits + I;
+      uint32_t NumBitsInByte = std::min(8 - BitsStart % 8, E - I);
+      assert(((BitsStart ^ (BitsStart + NumBitsInByte - 1)) & ~7) == 0 &&
              "Across byte boundary.");
-      Bytes[(OffsetInBits + I) / 8].poisonBits(static_cast<uint8_t>(
-          ((1U << NumBitsInByte) - 1) << ((OffsetInBits + I) % 8)));
+      Bytes[BitsStart / 8].poisonBits(static_cast<uint8_t>(
+          ((1U << NumBitsInByte) - 1) << (BitsStart % 8)));
       I += NumBitsInByte;
     }
   } else if (Ty->isIntegerTy()) {

>From 82ef6e03385d1cd1a8d62b7067b955641708b998 Mon Sep 17 00:00:00 2001
From: Pedro Lobo <pedro.lobo at tecnico.ulisboa.pt>
Date: Sat, 10 Oct 2026 19:14:10 +0100
Subject: [PATCH 2/2] Fix formatting

---
 llvm/tools/llubi/lib/Context.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/llvm/tools/llubi/lib/Context.cpp b/llvm/tools/llubi/lib/Context.cpp
index ac04257296099..1650d09fa1456 100644
--- a/llvm/tools/llubi/lib/Context.cpp
+++ b/llvm/tools/llubi/lib/Context.cpp
@@ -705,8 +705,8 @@ void Context::toBytes(const AnyValue &Val, Type *Ty, uint32_t OffsetInBits,
       uint32_t NumBitsInByte = std::min(8 - BitsStart % 8, E - I);
       assert(((BitsStart ^ (BitsStart + NumBitsInByte - 1)) & ~7) == 0 &&
              "Across byte boundary.");
-      Bytes[BitsStart / 8].poisonBits(static_cast<uint8_t>(
-          ((1U << NumBitsInByte) - 1) << (BitsStart % 8)));
+      Bytes[BitsStart / 8].poisonBits(
+          static_cast<uint8_t>(((1U << NumBitsInByte) - 1) << (BitsStart % 8)));
       I += NumBitsInByte;
     }
   } else if (Ty->isIntegerTy()) {



More information about the llvm-commits mailing list