[llvm] [ConstantFold] Support byte values in `bitcast` constant folding (PR #188030)
Pedro Lobo via llvm-commits
llvm-commits at lists.llvm.org
Sat Apr 4 09:05:40 PDT 2026
================
@@ -292,3 +292,131 @@ define <2 x i64> @bitcast_constexpr_4f32_2i64_1111() {
%res = bitcast <4 x float> splat (float 1.0) to <2 x i64>
ret <2 x i64> %res
}
+
+define <2 x b64> @bitcast_constexpr_16i8_2b64() {
+; CHECK-LABEL: @bitcast_constexpr_16i8_2b64(
+; CHECK-NEXT: ret <2 x b64> splat (b64 144680345676153346)
+;
+ %res = bitcast <16 x i8> splat (i8 2) to <2 x b64>
+ ret <2 x b64> %res
+}
+
+define <2 x b64> @bitcast_constexpr_16b8_2b64() {
+; CHECK-LABEL: @bitcast_constexpr_16b8_2b64(
+; CHECK-NEXT: ret <2 x b64> splat (b64 144680345676153346)
+;
+ %res = bitcast <16 x b8> splat (b8 2) to <2 x b64>
+ ret <2 x b64> %res
+}
+
+define <2 x i64> @bitcast_constexpr_4b32_2i64() {
+; CHECK-LABEL: @bitcast_constexpr_4b32_2i64(
+; CHECK-NEXT: ret <2 x i64> <i64 4294967296, i64 12884901890>
+;
+ %res = bitcast <4 x b32> <b32 0, b32 1, b32 2, b32 3> to <2 x i64>
+ ret <2 x i64> %res
+}
+
+define <4 x i32> @bitcast_constexpr_2b64_4i32() {
+; CHECK-LABEL: @bitcast_constexpr_2b64_4i32(
+; CHECK-NEXT: ret <4 x i32> <i32 0, i32 0, i32 1, i32 0>
+;
+ %res = bitcast <2 x b64> <b64 0, b64 1> to <4 x i32>
+ ret <4 x i32> %res
+}
+
+define <2 x b64> @bitcast_constexpr_4i32_2b64() {
+; CHECK-LABEL: @bitcast_constexpr_4i32_2b64(
+; CHECK-NEXT: ret <2 x b64> <b64 4294967296, b64 12884901890>
+;
+ %res = bitcast <4 x i32> <i32 0, i32 1, i32 2, i32 3> to <2 x b64>
+ ret <2 x b64> %res
+}
+
+define <4 x b32> @bitcast_constexpr_2i64_4b32() {
+; CHECK-LABEL: @bitcast_constexpr_2i64_4b32(
+; CHECK-NEXT: ret <4 x b32> <b32 0, b32 0, b32 1, b32 0>
+;
+ %res = bitcast <2 x i64> <i64 0, i64 1> to <4 x b32>
+ ret <4 x b32> %res
+}
+
+define <2 x b64> @bitcast_constexpr_4b32_2b64() {
+; CHECK-LABEL: @bitcast_constexpr_4b32_2b64(
+; CHECK-NEXT: ret <2 x b64> <b64 4294967296, b64 12884901890>
+;
+ %res = bitcast <4 x b32> <b32 0, b32 1, b32 2, b32 3> to <2 x b64>
----------------
pedroclobo wrote:
Folding through integers isn't right if we combine smaller elements (with `poison`) into larger byte values, e.g. `bitcast <4 x b32> <b32 poison, b32 1, b32 2, b32 3> to <2 x b64>`. In such cases, I think the best we can do is not fold the cast, as we cannot express individual `poison` bits with `ConstantByte`.
I've added tests with `poison` elements and prevented the fold when it mixes `poison` and non-`poison` elements in an output lane.
https://github.com/llvm/llvm-project/pull/188030
More information about the llvm-commits
mailing list