https://github.com/pedroclobo created https://github.com/llvm/llvm-project/pull/240201
Add support for vector `bitinsert` and `bitextract`. >From c593ffa7d2e8b8884af5d10d0888f9a6540e3fdc Mon Sep 17 00:00:00 2001 From: Pedro Lobo <[email protected]> Date: Fri, 6 Nov 2026 13:10:23 +0000 Subject: [PATCH] [llubi] Add support for vector `bitinsert` and `bitextract` --- .../tools/llubi/bitinsert_bitextract_be.ll | 32 +++++++++++++ .../tools/llubi/bitinsert_bitextract_le.ll | 32 +++++++++++++ llvm/tools/llubi/lib/Interpreter.cpp | 48 ++++++++++--------- 3 files changed, 90 insertions(+), 22 deletions(-) diff --git a/llvm/test/tools/llubi/bitinsert_bitextract_be.ll b/llvm/test/tools/llubi/bitinsert_bitextract_be.ll index 8b867e2c026a8c..c6dbc8ff469a6f 100644 --- a/llvm/test/tools/llubi/bitinsert_bitextract_be.ll +++ b/llvm/test/tools/llubi/bitinsert_bitextract_be.ll @@ -39,6 +39,22 @@ entry: %bitextract_ptr_bytes = bitextract b64, b128 %bitinsert_ptr, i32 32 %bitinsert_ptr_bytes = bitinsert b128 0, b64 %bitextract_ptr_bytes, i32 5 %bitextract_ptr_via_bytes = bitextract ptr, b128 %bitinsert_ptr_bytes, i32 5 + %vec_bitextract = bitextract <2 x i8>, <2 x b32> splat (b32 u0x12345678), <2 x i32> <i32 0, i32 8> + %vec_bitextract_unaligned = bitextract <2 x i8>, <2 x b32> splat (b32 u0x12345678), <2 x i32> <i32 4, i32 12> + %vec_bitextract_oob = bitextract <3 x i8>, <3 x b32> splat (b32 u0x12345678), <3 x i32> <i32 8, i32 32, i32 poison> + %vec_bitextract_float = bitextract <2 x float>, <2 x b64> <b64 u0x3F80000000000000, b64 u0x000000003F800000>, <2 x i32> <i32 32, i32 0> + %vec_bitinsert = bitinsert <2 x b32> splat (b32 u0x12345678), <2 x i8> <i8 0, i8 -1>, <2 x i32> <i32 0, i32 8> + %vec_bitinsert_oob = bitinsert <3 x b32> splat (b32 u0x12345678), <3 x i8> zeroinitializer, <3 x i32> <i32 8, i32 32, i32 poison> + %vec_bitinsert_poison = bitinsert <2 x b32> zeroinitializer, <2 x i16> <i16 poison, i16 0>, <2 x i32> splat (i32 4) + %vec_bitinsert_roundtrip = bitinsert <2 x b64> zeroinitializer, <2 x i16> <i16 1, i16 2>, <2 x i32> <i32 16, i32 48> + %vec_bitextract_roundtrip = bitextract <2 x i16>, <2 x b64> %vec_bitinsert_roundtrip, <2 x i32> <i32 16, i32 48> + %vec_bitcast = bitcast <8 x i8> <i8 0, i8 1, i8 2, i8 3, i8 4, i8 5, i8 6, i8 7> to <2 x b32> + %vec_bitextract_bitcast = bitextract <2 x i8>, <2 x b32> %vec_bitcast, <2 x i32> <i32 8, i32 24> + %vec_ptrs = insertelement <2 x ptr> poison, ptr %ptr, i64 1 + %vec_bitinsert_ptr = bitinsert <2 x b128> zeroinitializer, <2 x ptr> %vec_ptrs, <2 x i32> splat (i32 32) + %vec_bitextract_ptr = bitextract <2 x ptr>, <2 x b128> %vec_bitinsert_ptr, <2 x i32> splat (i32 32) + %vec_bitextract_scalable = bitextract <vscale x 2 x i8>, <vscale x 2 x b32> splat (b32 u0x12345678), <vscale x 2 x i32> splat (i32 8) + %vec_bitinsert_scalable = bitinsert <vscale x 2 x b32> splat (b32 u0x12345678), <vscale x 2 x i8> splat (i8 -1), <vscale x 2 x i32> splat (i32 8) ret void } ; CHECK: Entering function: main @@ -74,5 +90,21 @@ entry: ; CHECK-NEXT: %bitextract_ptr_bytes = bitextract b64, b128 %bitinsert_ptr, i32 32 => b64 ptr 0x8 [ptr] ; CHECK-NEXT: %bitinsert_ptr_bytes = bitinsert b128 0, b64 %bitextract_ptr_bytes, i32 5 => b128 0x00 0x00 0x00 0x00 0x00 0x00 0x00 00000000(!!!00101) 00000000(00011101) 00000000(00000110) 00000000(11111000) 00000000(10111001) 00000000(01101000) 00000000(00111011) 00000001(10000111) 00000000(110!!!!!) ; CHECK-NEXT: %bitextract_ptr_via_bytes = bitextract ptr, b128 %bitinsert_ptr_bytes, i32 5 => ptr 0x8 [ptr] +; CHECK-NEXT: %vec_bitextract = bitextract <2 x i8>, <2 x b32> splat (b32 305419896), <2 x i32> <i32 0, i32 8> => { i8 120, i8 86 } +; CHECK-NEXT: %vec_bitextract_unaligned = bitextract <2 x i8>, <2 x b32> splat (b32 305419896), <2 x i32> <i32 4, i32 12> => { i8 103, i8 69 } +; CHECK-NEXT: %vec_bitextract_oob = bitextract <3 x i8>, <3 x b32> splat (b32 305419896), <3 x i32> <i32 8, i32 32, i32 poison> => { i8 86, poison, poison } +; CHECK-NEXT: %vec_bitextract_float = bitextract <2 x float>, <2 x b64> <b64 4575657221408423936, b64 1065353216>, <2 x i32> <i32 32, i32 0> => { float 1.000000e+00, float 1.000000e+00 } +; CHECK-NEXT: %vec_bitinsert = bitinsert <2 x b32> splat (b32 305419896), <2 x i8> <i8 0, i8 -1>, <2 x i32> <i32 0, i32 8> => { b32 0x12 0x34 0x56 0x00 , b32 0x12 0x34 0xFF 0x78 } +; CHECK-NEXT: %vec_bitinsert_oob = bitinsert <3 x b32> splat (b32 305419896), <3 x i8> zeroinitializer, <3 x i32> <i32 8, i32 32, i32 poison> => { b32 0x12 0x34 0x00 0x78 , b32 0x!! 0x!! 0x!! 0x!! , b32 0x!! 0x!! 0x!! 0x!! } +; CHECK-NEXT: %vec_bitinsert_poison = bitinsert <2 x b32> zeroinitializer, <2 x i16> <i16 poison, i16 0>, <2 x i32> splat (i32 4) => { b32 0x00 0000!!!! 0x!! !!!!0000 , b32 0x00 0x00 0x00 0x00 } +; CHECK-NEXT: %vec_bitinsert_roundtrip = bitinsert <2 x b64> zeroinitializer, <2 x i16> <i16 1, i16 2>, <2 x i32> <i32 16, i32 48> => { b64 0x00 0x00 0x00 0x00 0x00 0x01 0x00 0x00 , b64 0x00 0x02 0x00 0x00 0x00 0x00 0x00 0x00 } +; CHECK-NEXT: %vec_bitextract_roundtrip = bitextract <2 x i16>, <2 x b64> %vec_bitinsert_roundtrip, <2 x i32> <i32 16, i32 48> => { i16 1, i16 2 } +; CHECK-NEXT: %vec_bitcast = bitcast <8 x i8> <i8 0, i8 1, i8 2, i8 3, i8 4, i8 5, i8 6, i8 7> to <2 x b32> => { b32 0x00 0x01 0x02 0x03 , b32 0x04 0x05 0x06 0x07 } +; CHECK-NEXT: %vec_bitextract_bitcast = bitextract <2 x i8>, <2 x b32> %vec_bitcast, <2 x i32> <i32 8, i32 24> => { i8 2, i8 4 } +; CHECK-NEXT: %vec_ptrs = insertelement <2 x ptr> poison, ptr %ptr, i64 1 => { poison, ptr 0x8 [ptr] } +; CHECK-NEXT: %vec_bitinsert_ptr = bitinsert <2 x b128> zeroinitializer, <2 x ptr> %vec_ptrs, <2 x i32> splat (i32 32) => { b128 0x00 0x00 0x00 0x00 0x!! 0x!! 0x!! 0x!! 0x!! 0x!! 0x!! 0x!! 0x00 0x00 0x00 0x00 , b128 0x00 0x00 0x00 0x00 00000000(00101000) 00000000(11101000) 00000000(00110111) 00000000(11000101) 00000000(11001011) 00000000(01000001) 00000000(11011100) 00001000(00111110) 0x00 0x00 0x00 0x00 } +; CHECK-NEXT: %vec_bitextract_ptr = bitextract <2 x ptr>, <2 x b128> %vec_bitinsert_ptr, <2 x i32> splat (i32 32) => { poison, ptr 0x8 [ptr] } +; CHECK-NEXT: %vec_bitextract_scalable = bitextract <vscale x 2 x i8>, <vscale x 2 x b32> splat (b32 305419896), <vscale x 2 x i32> splat (i32 8) => { i8 86, i8 86, i8 86, i8 86, i8 86, i8 86, i8 86, i8 86 } +; CHECK-NEXT: %vec_bitinsert_scalable = bitinsert <vscale x 2 x b32> splat (b32 305419896), <vscale x 2 x i8> splat (i8 -1), <vscale x 2 x i32> splat (i32 8) => { b32 0x12 0x34 0xFF 0x78 , b32 0x12 0x34 0xFF 0x78 , b32 0x12 0x34 0xFF 0x78 , b32 0x12 0x34 0xFF 0x78 , b32 0x12 0x34 0xFF 0x78 , b32 0x12 0x34 0xFF 0x78 , b32 0x12 0x34 0xFF 0x78 , b32 0x12 0x34 0xFF 0x78 } ; CHECK-NEXT: ret void ; CHECK-NEXT: Exiting function: main diff --git a/llvm/test/tools/llubi/bitinsert_bitextract_le.ll b/llvm/test/tools/llubi/bitinsert_bitextract_le.ll index 4561aebb931dfb..b557c9d4e51a8f 100644 --- a/llvm/test/tools/llubi/bitinsert_bitextract_le.ll +++ b/llvm/test/tools/llubi/bitinsert_bitextract_le.ll @@ -39,6 +39,22 @@ entry: %bitextract_ptr_bytes = bitextract b64, b128 %bitinsert_ptr, i32 32 %bitinsert_ptr_bytes = bitinsert b128 0, b64 %bitextract_ptr_bytes, i32 5 %bitextract_ptr_via_bytes = bitextract ptr, b128 %bitinsert_ptr_bytes, i32 5 + %vec_bitextract = bitextract <2 x i8>, <2 x b32> splat (b32 u0x12345678), <2 x i32> <i32 0, i32 8> + %vec_bitextract_unaligned = bitextract <2 x i8>, <2 x b32> splat (b32 u0x12345678), <2 x i32> <i32 4, i32 12> + %vec_bitextract_oob = bitextract <3 x i8>, <3 x b32> splat (b32 u0x12345678), <3 x i32> <i32 8, i32 32, i32 poison> + %vec_bitextract_float = bitextract <2 x float>, <2 x b64> <b64 u0x3F80000000000000, b64 u0x000000003F800000>, <2 x i32> <i32 32, i32 0> + %vec_bitinsert = bitinsert <2 x b32> splat (b32 u0x12345678), <2 x i8> <i8 0, i8 -1>, <2 x i32> <i32 0, i32 8> + %vec_bitinsert_oob = bitinsert <3 x b32> splat (b32 u0x12345678), <3 x i8> zeroinitializer, <3 x i32> <i32 8, i32 32, i32 poison> + %vec_bitinsert_poison = bitinsert <2 x b32> zeroinitializer, <2 x i16> <i16 poison, i16 0>, <2 x i32> splat (i32 4) + %vec_bitinsert_roundtrip = bitinsert <2 x b64> zeroinitializer, <2 x i16> <i16 1, i16 2>, <2 x i32> <i32 16, i32 48> + %vec_bitextract_roundtrip = bitextract <2 x i16>, <2 x b64> %vec_bitinsert_roundtrip, <2 x i32> <i32 16, i32 48> + %vec_bitcast = bitcast <8 x i8> <i8 0, i8 1, i8 2, i8 3, i8 4, i8 5, i8 6, i8 7> to <2 x b32> + %vec_bitextract_bitcast = bitextract <2 x i8>, <2 x b32> %vec_bitcast, <2 x i32> <i32 8, i32 24> + %vec_ptrs = insertelement <2 x ptr> poison, ptr %ptr, i64 1 + %vec_bitinsert_ptr = bitinsert <2 x b128> zeroinitializer, <2 x ptr> %vec_ptrs, <2 x i32> splat (i32 32) + %vec_bitextract_ptr = bitextract <2 x ptr>, <2 x b128> %vec_bitinsert_ptr, <2 x i32> splat (i32 32) + %vec_bitextract_scalable = bitextract <vscale x 2 x i8>, <vscale x 2 x b32> splat (b32 u0x12345678), <vscale x 2 x i32> splat (i32 8) + %vec_bitinsert_scalable = bitinsert <vscale x 2 x b32> splat (b32 u0x12345678), <vscale x 2 x i8> splat (i8 -1), <vscale x 2 x i32> splat (i32 8) ret void } ; CHECK: Entering function: main @@ -74,5 +90,21 @@ entry: ; CHECK-NEXT: %bitextract_ptr_bytes = bitextract b64, b128 %bitinsert_ptr, i32 32 => b64 ptr 0x8 [ptr] ; CHECK-NEXT: %bitinsert_ptr_bytes = bitinsert b128 0, b64 %bitextract_ptr_bytes, i32 5 => b128 00000000(110!!!!!) 00000001(10000111) 00000000(00111011) 00000000(01101000) 00000000(10111001) 00000000(11111000) 00000000(00000110) 00000000(00011101) 00000000(!!!00101) 0x00 0x00 0x00 0x00 0x00 0x00 0x00 ; CHECK-NEXT: %bitextract_ptr_via_bytes = bitextract ptr, b128 %bitinsert_ptr_bytes, i32 5 => ptr 0x8 [ptr] +; CHECK-NEXT: %vec_bitextract = bitextract <2 x i8>, <2 x b32> splat (b32 305419896), <2 x i32> <i32 0, i32 8> => { i8 120, i8 86 } +; CHECK-NEXT: %vec_bitextract_unaligned = bitextract <2 x i8>, <2 x b32> splat (b32 305419896), <2 x i32> <i32 4, i32 12> => { i8 103, i8 69 } +; CHECK-NEXT: %vec_bitextract_oob = bitextract <3 x i8>, <3 x b32> splat (b32 305419896), <3 x i32> <i32 8, i32 32, i32 poison> => { i8 86, poison, poison } +; CHECK-NEXT: %vec_bitextract_float = bitextract <2 x float>, <2 x b64> <b64 4575657221408423936, b64 1065353216>, <2 x i32> <i32 32, i32 0> => { float 1.000000e+00, float 1.000000e+00 } +; CHECK-NEXT: %vec_bitinsert = bitinsert <2 x b32> splat (b32 305419896), <2 x i8> <i8 0, i8 -1>, <2 x i32> <i32 0, i32 8> => { b32 0x00 0x56 0x34 0x12 , b32 0x78 0xFF 0x34 0x12 } +; CHECK-NEXT: %vec_bitinsert_oob = bitinsert <3 x b32> splat (b32 305419896), <3 x i8> zeroinitializer, <3 x i32> <i32 8, i32 32, i32 poison> => { b32 0x78 0x00 0x34 0x12 , b32 0x!! 0x!! 0x!! 0x!! , b32 0x!! 0x!! 0x!! 0x!! } +; CHECK-NEXT: %vec_bitinsert_poison = bitinsert <2 x b32> zeroinitializer, <2 x i16> <i16 poison, i16 0>, <2 x i32> splat (i32 4) => { b32 !!!!0000 0x!! 0000!!!! 0x00 , b32 0x00 0x00 0x00 0x00 } +; CHECK-NEXT: %vec_bitinsert_roundtrip = bitinsert <2 x b64> zeroinitializer, <2 x i16> <i16 1, i16 2>, <2 x i32> <i32 16, i32 48> => { b64 0x00 0x00 0x01 0x00 0x00 0x00 0x00 0x00 , b64 0x00 0x00 0x00 0x00 0x00 0x00 0x02 0x00 } +; CHECK-NEXT: %vec_bitextract_roundtrip = bitextract <2 x i16>, <2 x b64> %vec_bitinsert_roundtrip, <2 x i32> <i32 16, i32 48> => { i16 1, i16 2 } +; CHECK-NEXT: %vec_bitcast = bitcast <8 x i8> <i8 0, i8 1, i8 2, i8 3, i8 4, i8 5, i8 6, i8 7> to <2 x b32> => { b32 0x00 0x01 0x02 0x03 , b32 0x04 0x05 0x06 0x07 } +; CHECK-NEXT: %vec_bitextract_bitcast = bitextract <2 x i8>, <2 x b32> %vec_bitcast, <2 x i32> <i32 8, i32 24> => { i8 1, i8 7 } +; CHECK-NEXT: %vec_ptrs = insertelement <2 x ptr> poison, ptr %ptr, i64 1 => { poison, ptr 0x8 [ptr] } +; CHECK-NEXT: %vec_bitinsert_ptr = bitinsert <2 x b128> zeroinitializer, <2 x ptr> %vec_ptrs, <2 x i32> splat (i32 32) => { b128 0x00 0x00 0x00 0x00 0x!! 0x!! 0x!! 0x!! 0x!! 0x!! 0x!! 0x!! 0x00 0x00 0x00 0x00 , b128 0x00 0x00 0x00 0x00 00001000(00111110) 00000000(11011100) 00000000(01000001) 00000000(11001011) 00000000(11000101) 00000000(00110111) 00000000(11101000) 00000000(00101000) 0x00 0x00 0x00 0x00 } +; CHECK-NEXT: %vec_bitextract_ptr = bitextract <2 x ptr>, <2 x b128> %vec_bitinsert_ptr, <2 x i32> splat (i32 32) => { poison, ptr 0x8 [ptr] } +; CHECK-NEXT: %vec_bitextract_scalable = bitextract <vscale x 2 x i8>, <vscale x 2 x b32> splat (b32 305419896), <vscale x 2 x i32> splat (i32 8) => { i8 86, i8 86, i8 86, i8 86, i8 86, i8 86, i8 86, i8 86 } +; CHECK-NEXT: %vec_bitinsert_scalable = bitinsert <vscale x 2 x b32> splat (b32 305419896), <vscale x 2 x i8> splat (i8 -1), <vscale x 2 x i32> splat (i32 8) => { b32 0x78 0xFF 0x34 0x12 , b32 0x78 0xFF 0x34 0x12 , b32 0x78 0xFF 0x34 0x12 , b32 0x78 0xFF 0x34 0x12 , b32 0x78 0xFF 0x34 0x12 , b32 0x78 0xFF 0x34 0x12 , b32 0x78 0xFF 0x34 0x12 , b32 0x78 0xFF 0x34 0x12 } ; CHECK-NEXT: ret void ; CHECK-NEXT: Exiting function: main diff --git a/llvm/tools/llubi/lib/Interpreter.cpp b/llvm/tools/llubi/lib/Interpreter.cpp index d74c43cc3f5005..22ada1bda0f1b2 100644 --- a/llvm/tools/llubi/lib/Interpreter.cpp +++ b/llvm/tools/llubi/lib/Interpreter.cpp @@ -3097,32 +3097,36 @@ class InstExecutor : public InstVisitor<InstExecutor, void>, } void visitBitInsertInst(BitInsertInst &BII) { - ByteValue Res = getValue(BII.getOperand(0)).asByte(); - auto &Val = getValue(BII.getOperand(1)); - auto &Offset = getValue(BII.getOperand(2)); - Type *ValTy = BII.getOperand(1)->getType(); + Type *BaseTy = BII.getType()->getScalarType(); + Type *ValTy = BII.getOperand(1)->getType()->getScalarType(); uint64_t NumBits = DL.getTypeSizeInBits(ValTy).getFixedValue(); - uint64_t OffsetInBits = - Offset.isPoison() ? 0 : Offset.asInteger().getZExtValue(); - if (Offset.isPoison() || OffsetInBits + NumBits > Res.getBitWidth()) { - setResult(BII, AnyValue::getPoisonValue(Ctx, BII.getType())); - return; - } - Ctx.insertBits(Res, OffsetInBits, Val, ValTy); - setResult(BII, std::move(Res)); + visitTriOp(BII, + [&](const AnyValue &Base, const AnyValue &Val, + const AnyValue &Offset) -> AnyValue { + if (Offset.isPoison()) + return AnyValue::getPoisonValue(Ctx, BaseTy); + ByteValue Res = Base.asByte(); + uint64_t OffsetInBits = Offset.asInteger().getZExtValue(); + if (OffsetInBits + NumBits > Res.getBitWidth()) + return AnyValue::getPoisonValue(Ctx, BaseTy); + Ctx.insertBits(Res, OffsetInBits, Val, ValTy); + return Res; + }); } void visitBitExtractInst(BitExtractInst &BEI) { - auto &Src = getValue(BEI.getOperand(0)).asByte(); - auto &Offset = getValue(BEI.getOperand(1)); - uint64_t NumBits = DL.getTypeSizeInBits(BEI.getType()).getFixedValue(); - uint64_t OffsetInBits = - Offset.isPoison() ? 0 : Offset.asInteger().getZExtValue(); - if (Offset.isPoison() || OffsetInBits + NumBits > Src.getBitWidth()) { - setResult(BEI, AnyValue::getPoisonValue(Ctx, BEI.getType())); - return; - } - setResult(BEI, Ctx.extractBits(Src, OffsetInBits, BEI.getType())); + Type *Ty = BEI.getType()->getScalarType(); + uint64_t NumBits = DL.getTypeSizeInBits(Ty).getFixedValue(); + visitBinOp(BEI, + [&](const AnyValue &Src, const AnyValue &Offset) -> AnyValue { + if (Offset.isPoison()) + return AnyValue::getPoisonValue(Ctx, Ty); + const ByteValue &Bytes = Src.asByte(); + uint64_t OffsetInBits = Offset.asInteger().getZExtValue(); + if (OffsetInBits + NumBits > Bytes.getBitWidth()) + return AnyValue::getPoisonValue(Ctx, Ty); + return Ctx.extractBits(Bytes, OffsetInBits, Ty); + }); } void visitFreezeInst(FreezeInst &FI) { _______________________________________________ llvm-branch-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
