Revert some of the union changes

This commit is contained in:
gingerBill
2026-06-29 12:18:40 +01:00
parent 2f3174fe10
commit 42f681baff
2 changed files with 39 additions and 46 deletions

View File

@@ -117,35 +117,35 @@ gb_internal LLVMValueRef llvm_const_cast(lbModule *m, LLVMValueRef val, LLVMType
unsigned dst_n = LLVMCountStructElementTypes(dst);
if (src_n != dst_n) goto failure;
bool skip_cast = true;
for (unsigned i = 0; i < dst_n; i++) {
LLVMTypeKind dt = LLVMGetTypeKind(LLVMStructGetTypeAtIndex(dst, i));
LLVMTypeKind st = LLVMGetTypeKind(LLVMStructGetTypeAtIndex(src, i));
if (dt != st) {
skip_cast = false;
}
if (dt == LLVMIntegerTypeKind) {
continue;
}
if (dt != LLVMArrayTypeKind) {
skip_cast = false;
break;
}
// bool skip_cast = true;
// for (unsigned i = 0; i < dst_n; i++) {
// LLVMTypeKind dt = LLVMGetTypeKind(LLVMStructGetTypeAtIndex(dst, i));
// LLVMTypeKind st = LLVMGetTypeKind(LLVMStructGetTypeAtIndex(src, i));
// if (dt != st) {
// skip_cast = false;
// }
// if (dt == LLVMIntegerTypeKind) {
// continue;
// }
// if (dt != LLVMArrayTypeKind) {
// skip_cast = false;
// break;
// }
LLVMValueRef field_val = llvm_const_extract_value(m, val, i);
if (field_val == nullptr) goto failure;
// LLVMValueRef field_val = llvm_const_extract_value(m, val, i);
// if (field_val == nullptr) goto failure;
LLVMTypeRef dst_elem_ty = LLVMStructGetTypeAtIndex(dst, i);
LLVMTypeRef src_elem_ty = LLVMTypeOf(field_val);
if (lb_sizeof(dst_elem_ty) > lb_sizeof(src_elem_ty)) {
skip_cast = true;
continue;
}
// LLVMTypeRef dst_elem_ty = LLVMStructGetTypeAtIndex(dst, i);
// LLVMTypeRef src_elem_ty = LLVMTypeOf(field_val);
// if (lb_sizeof(dst_elem_ty) > lb_sizeof(src_elem_ty)) {
// skip_cast = true;
// continue;
// }
}
if (skip_cast) {
return val;
}
// }
// if (skip_cast) {
// return val;
// }
LLVMValueRef *field_vals = temporary_alloc_array<LLVMValueRef>(dst_n);
for (unsigned i = 0; i < dst_n; i++) {
@@ -1075,7 +1075,7 @@ gb_internal lbValue lb_const_value(lbModule *m, Type *type, ExactValue value, Ty
values[value_count++] = llvm_const_pad_to_size(m, cv.value, block_type);
#else
values[value_count++] = cv.value;
{
if (block_size != type_size_of(variant_type)) {
LLVMTypeRef padding_type = lb_type_padding_filler(m, block_size - type_size_of(variant_type), 1);
values[value_count++] = LLVMConstNull(padding_type);
}

View File

@@ -1636,32 +1636,22 @@ gb_internal lbValue lb_emit_union_tag_ptr(lbProcedure *p, lbValue u) {
GB_ASSERT_MSG(is_type_pointer(t) &&
is_type_union(type_deref(t)), "%s", type_to_string(t));
Type *ut = type_deref(t);
Type *bt = base_type(ut);
GB_ASSERT(!is_type_union_maybe_pointer_original_alignment(ut));
GB_ASSERT(!is_type_union_maybe_pointer(ut));
GB_ASSERT(type_size_of(ut) > 0);
GB_ASSERT(bt->kind == Type_Union);
Type *tag_type = union_tag_type(ut);
LLVMTypeRef uvt = llvm_addr_type(p->module, u);
unsigned element_count = LLVMCountStructElementTypes(uvt);
if (LLVM_VERSION_MAJOR == 14 || bt->Union.variants.count == 1) {
GB_ASSERT_MSG(element_count >= 2, "element_count=%u (%s) != (%s)", element_count, type_to_string(ut), LLVMPrintTypeToString(uvt));
} else {
GB_ASSERT_MSG(element_count >= 3, "element_count=%u (%s) != (%s)", element_count, type_to_string(ut), LLVMPrintTypeToString(uvt));
}
GB_ASSERT_MSG(element_count >= 2, "element_count=%u (%s) != (%s)", element_count, type_to_string(ut), LLVMPrintTypeToString(uvt));
LLVMValueRef ptr = u.value;
ptr = LLVMBuildPointerCast(p->builder, ptr, LLVMPointerType(uvt, 0), "");
lbValue tag_ptr = {};
if (LLVM_VERSION_MAJOR == 14 || bt->Union.variants.count == 1) {
tag_ptr.value = LLVMBuildStructGEP2(p->builder, uvt, ptr, 1, "");
} else {
tag_ptr.value = LLVMBuildStructGEP2(p->builder, uvt, ptr, 2, "");
}
tag_ptr.value = LLVMBuildStructGEP2(p->builder, uvt, ptr, 1, "");
tag_ptr.type = alloc_type_pointer(tag_type);
return tag_ptr;
}
@@ -2507,6 +2497,7 @@ gb_internal LLVMTypeRef lb_type_internal(lbModule *m, Type *type) {
LLVMTypeRef fields[] = {lb_type(m, type->Union.variants[0])};
return LLVMStructTypeInContext(ctx, fields, gb_count_of(fields), false);
}
bool is_packed = false;
auto fields = array_make<LLVMTypeRef>(temporary_allocator(), 0, 4);
if (is_type_union_maybe_pointer(type)) {
@@ -2524,17 +2515,18 @@ gb_internal LLVMTypeRef lb_type_internal(lbModule *m, Type *type) {
LLVMTypeRef padding_type = lb_type_padding_filler(m, padding, align);
array_add(&fields, padding_type);
}
is_packed = true;
} else {
LLVMTypeRef block_type = lb_type_internal_union_block_type(m, type);
LLVMTypeRef tag_type = lb_type(m, union_tag_type(type));
array_add(&fields, block_type);
#if LLVM_VERSION_MAJOR > 14
{ // NOTE(bill): always add a zero-byte pad to make inline constant unions work
LLVMTypeRef padding_type = lb_type_padding_filler(m, 0, 1);
array_add(&fields, padding_type);
}
#endif
// #if LLVM_VERSION_MAJOR > 14
// { // NOTE(bill): always add a zero-byte pad to make inline constant unions work
// LLVMTypeRef padding_type = lb_type_padding_filler(m, 0, 1);
// array_add(&fields, padding_type);
// }
// #endif
array_add(&fields, tag_type);
i64 used_size = lb_sizeof(block_type) + lb_sizeof(tag_type);
i64 padding = size - used_size;
@@ -2542,9 +2534,10 @@ gb_internal LLVMTypeRef lb_type_internal(lbModule *m, Type *type) {
LLVMTypeRef padding_type = lb_type_padding_filler(m, padding, align);
array_add(&fields, padding_type);
}
is_packed = true;
}
return LLVMStructTypeInContext(ctx, fields.data, cast(unsigned)fields.count, /*packed*/true);
return LLVMStructTypeInContext(ctx, fields.data, cast(unsigned)fields.count, is_packed);
}
break;