From e96dce92cd03e0fddfab680491e5a8f66a0089f2 Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Thu, 13 Feb 2025 21:36:28 +0100 Subject: [PATCH] Issue when scalar expanding a boolean from a conditional to a bool vector #1954. --- releasenotes.md | 1 + src/compiler/llvm_codegen_expr.c | 4 +-- test/test_suite/vector/vector_from_i1.c3t | 31 +++++++++++++++++++++++ 3 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 test/test_suite/vector/vector_from_i1.c3t diff --git a/releasenotes.md b/releasenotes.md index d6bbaaba8..78a962790 100644 --- a/releasenotes.md +++ b/releasenotes.md @@ -67,6 +67,7 @@ - Fix issue where compiling both for asm and object file would corrupt the obj file output. - Fix `poll` and `POLL_FOREVER`. - Missing end padding when including a packed struct #1966. +- Issue when scalar expanding a boolean from a conditional to a bool vector #1954. ### Stdlib changes - Added '%h' and '%H' for printing out binary data in hexadecimal using the formatter. diff --git a/src/compiler/llvm_codegen_expr.c b/src/compiler/llvm_codegen_expr.c index 698988fd0..c20982ff4 100644 --- a/src/compiler/llvm_codegen_expr.c +++ b/src/compiler/llvm_codegen_expr.c @@ -6863,13 +6863,13 @@ void llvm_emit_enum_from_ord(GenContext *c, BEValue *value, Expr *expr) void llvm_emit_scalar_to_vector(GenContext *c, BEValue *value, Expr *expr) { llvm_emit_expr(c, value, expr->inner_expr); - llvm_value_rvalue(c, value); + LLVMValueRef val = llvm_load_value_store(c, value); LLVMTypeRef type = llvm_get_type(c, expr->type); unsigned elements = LLVMGetVectorSize(type); LLVMValueRef res = LLVMGetUndef(type); for (unsigned i = 0; i < elements; i++) { - res = LLVMBuildInsertElement(c->builder, res, value->value, llvm_const_int(c, type_usz, i), ""); + res = LLVMBuildInsertElement(c->builder, res, val, llvm_const_int(c, type_usz, i), ""); } llvm_value_set(value, res, expr->type); } diff --git a/test/test_suite/vector/vector_from_i1.c3t b/test/test_suite/vector/vector_from_i1.c3t new file mode 100644 index 000000000..d48f1a0f7 --- /dev/null +++ b/test/test_suite/vector/vector_from_i1.c3t @@ -0,0 +1,31 @@ +// #target: macos-x64 +module test; +import std; + +// issue 1954 +macro splat($Type, x) +{ + return $Type {x,x,x,x}; +} + +fn void main() +{ + int[<4>] v1 = splat(int[<4>], 2); + int[<4>] v2 = splat(int[<4>], 1); + bool[<4>] vb = (v1 == v2); +} + +/* #expect: test.ll + + store <4 x i32> , ptr %v1, align 16 + store <4 x i32> , ptr %v2, align 16 + %0 = load <4 x i32>, ptr %v1, align 16 + %1 = load <4 x i32>, ptr %v2, align 16 + %eq = icmp eq <4 x i32> %0, %1 + %2 = call i1 @llvm.vector.reduce.and.v4i1(<4 x i1> %eq) + %3 = zext i1 %2 to i8 + %4 = insertelement <4 x i8> undef, i8 %3, i64 0 + %5 = insertelement <4 x i8> %4, i8 %3, i64 1 + %6 = insertelement <4 x i8> %5, i8 %3, i64 2 + %7 = insertelement <4 x i8> %6, i8 %3, i64 3 + store <4 x i8> %7, ptr %vb, align 4