- Store of zero in lowering did not properly handle optionals in some cases #2837

This commit is contained in:
Christoffer Lerno
2026-01-25 04:57:35 +01:00
parent 109e15b5a0
commit 8bd942c1b8
5 changed files with 38 additions and 1 deletions

View File

@@ -125,6 +125,7 @@
- Raw vaargs with optional return not lowered correctly #2819
- Early exit in macro call crashes codegen #2820
- Empty enums would return the values as zero sized arrays #2838
- Store of zero in lowering did not properly handle optionals in some cases #2837
### Stdlib changes
- Add `ThreadPool` join function to wait for all threads to finish in the pool without destroying the threads.

View File

@@ -1430,6 +1430,7 @@ static inline void llvm_emit_const_initialize_bitstruct_ref(GenContext *c, BEVal
return;
}
ASSERT(initializer->kind == CONST_INIT_STRUCT);
llvm_store_no_fault(c, ref);
llvm_store_raw(c, ref, llvm_emit_const_bitstruct(c, initializer));
}

View File

@@ -459,6 +459,7 @@ LLVMValueRef llvm_load_value_store(GenContext *c, BEValue *value);
// -- Store ---
LLVMValueRef llvm_store(GenContext *c, BEValue *destination, BEValue *value);
LLVMValueRef llvm_store_zero(GenContext *c, BEValue *ref);
void llvm_store_no_fault(GenContext *c, BEValue *ref);
INLINE LLVMValueRef llvm_store_raw(GenContext *c, BEValue *destination, LLVMValueRef raw_value);
INLINE LLVMValueRef llvm_store_decl(GenContext *c, Decl *decl, BEValue *value);
INLINE LLVMValueRef llvm_store_decl_raw(GenContext *context, Decl *decl, LLVMValueRef value);

View File

@@ -164,11 +164,19 @@ LLVMValueRef llvm_load_value_store(GenContext *c, BEValue *value)
return LLVMBuildZExt(c->builder, val, c->byte_type, "");
}
void llvm_store_no_fault(GenContext *c, BEValue *ref)
{
if (ref->kind == BE_ADDRESS_OPTIONAL)
{
llvm_store_to_ptr_raw_aligned(c, ref->optional, llvm_get_zero(c, type_fault), type_abi_alignment(type_fault));
}
}
LLVMValueRef llvm_store_zero(GenContext *c, BEValue *ref)
{
llvm_value_addr(c, ref);
if (!llvm_value_is_addr(ref)) llvm_value_addr(c, ref);
Type *type = ref->type;
llvm_store_no_fault(c, ref);
if (!type_is_aggregate(type) || type_is_builtin(type->type_kind))
{
if (type_kind_is_real_vector(type->type_kind))

View File

@@ -0,0 +1,26 @@
// #target: macos-x64
module test;
bitstruct Foo : int
{}
typedef Foo2 = Foo;
fn void main()
{
Foo2? y, z;
y = Foo.methodsof;
}
/* #expect: test.ll
%y = alloca i32, align 4
%y.f = alloca i64, align 8
%z = alloca i32, align 4
%z.f = alloca i64, align 8
store i64 0, ptr %y.f, align 8
store i32 0, ptr %y, align 4
store i64 0, ptr %z.f, align 8
store i32 0, ptr %z, align 4
store i64 0, ptr %y.f, align 8
store i32 0, ptr %y, align 4
store i64 0, ptr %y.f, align 8
ret void
}