authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-08-02 00:04:38-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-08-02 00:04:38-07:00
log99318e7a95220c0bba88eb42b17747ef79da176d
tree2ca0337cab98b5d6c99836fb5d1aca0cf73d9636
parent44f833129c81bfa7117743fc7d54c44db09cc240

LLVM backend fixes

Fixing assertions hit after upgrading to opaque pointers API.

2 files changed, 22 insertions(+), 25 deletions(-)

src/codegen/llvm.zig+1-1
...@@ -5803,7 +5803,7 @@ pub const FuncGen = struct {...@@ -5803,7 +5803,7 @@ pub const FuncGen = struct {
5803 .False,5803 .False,
5804 );5804 );
5805 const call = self.builder.buildCall(5805 const call = self.builder.buildCall(
5806 asm_fn.typeOf(),5806 llvm_fn_ty,
5807 asm_fn,5807 asm_fn,
5808 llvm_param_values.ptr,5808 llvm_param_values.ptr,
5809 @intCast(c_uint, param_count),5809 @intCast(c_uint, param_count),
src/stage1/codegen.cpp+21-24
...@@ -1352,7 +1352,7 @@ static LLVMValueRef get_return_err_fn(CodeGen *g) {...@@ -1352,7 +1352,7 @@ static LLVMValueRef get_return_err_fn(CodeGen *g) {
1352 LLVMTypeRef usize_type_ref = g->builtin_types.entry_usize->llvm_type;1352 LLVMTypeRef usize_type_ref = g->builtin_types.entry_usize->llvm_type;
1353 LLVMValueRef zero = LLVMConstNull(get_llvm_type(g, g->builtin_types.entry_i32));1353 LLVMValueRef zero = LLVMConstNull(get_llvm_type(g, g->builtin_types.entry_i32));
1354 LLVMValueRef return_address_ptr = LLVMBuildCall2(g->builder,1354 LLVMValueRef return_address_ptr = LLVMBuildCall2(g->builder,
1355 LLVMTypeOf(get_return_address_fn_val(g)), get_return_address_fn_val(g), &zero, 1, "");1355 LLVMGlobalGetValueType(get_return_address_fn_val(g)), get_return_address_fn_val(g), &zero, 1, "");
1356 LLVMValueRef return_address = LLVMBuildPtrToInt(g->builder, return_address_ptr, usize_type_ref, "");1356 LLVMValueRef return_address = LLVMBuildPtrToInt(g->builder, return_address_ptr, usize_type_ref, "");
13571357
1358 LLVMBasicBlockRef return_block = LLVMAppendBasicBlock(fn_val, "Return");1358 LLVMBasicBlockRef return_block = LLVMAppendBasicBlock(fn_val, "Return");
...@@ -1455,9 +1455,12 @@ static LLVMValueRef get_safety_crash_err_fn(CodeGen *g) {...@@ -1455,9 +1455,12 @@ static LLVMValueRef get_safety_crash_err_fn(CodeGen *g) {
1455 }1455 }
14561456
1457 // Fetch the error name from the global table1457 // Fetch the error name from the global table
1458 LLVMValueRef err_table_indices[] = { err_val };1458 LLVMValueRef err_table_indices[] = {
1459 LLVMConstNull(usize_ty->llvm_type),
1460 err_val,
1461 };
1459 LLVMValueRef err_name_val = LLVMBuildInBoundsGEP2(g->builder,1462 LLVMValueRef err_name_val = LLVMBuildInBoundsGEP2(g->builder,
1460 get_llvm_type(g, str_type),1463 LLVMGlobalGetValueType(g->err_name_table),
1461 g->err_name_table, err_table_indices, 2, "");1464 g->err_name_table, err_table_indices, 2, "");
14621465
1463 LLVMValueRef ptr_field_ptr = LLVMBuildStructGEP2(g->builder,1466 LLVMValueRef ptr_field_ptr = LLVMBuildStructGEP2(g->builder,
...@@ -4088,9 +4091,8 @@ static LLVMValueRef ir_render_ptr_of_array_to_slice(CodeGen *g, Stage1Air *execu...@@ -4088,9 +4091,8 @@ static LLVMValueRef ir_render_ptr_of_array_to_slice(CodeGen *g, Stage1Air *execu
4088 LLVMConstInt(g->builtin_types.entry_usize->llvm_type, 0, false),4091 LLVMConstInt(g->builtin_types.entry_usize->llvm_type, 0, false),
4089 };4092 };
4090 LLVMValueRef expr_val = ir_llvm_value(g, instruction->operand);4093 LLVMValueRef expr_val = ir_llvm_value(g, instruction->operand);
4091 LLVMTypeRef elem_llvm_ty = get_llvm_type(g, array_type->data.array.child_type);4094 LLVMValueRef slice_start_ptr = LLVMBuildInBoundsGEP2(g->builder,
4092 LLVMValueRef slice_start_ptr = LLVMBuildInBoundsGEP2(g->builder, elem_llvm_ty, expr_val,4095 get_llvm_type(g, array_type), expr_val, indices, 2, "");
4093 indices, 2, "");
4094 gen_store_untyped(g, slice_start_ptr, ptr_field_ptr, 0, false);4096 gen_store_untyped(g, slice_start_ptr, ptr_field_ptr, 0, false);
4095 } else if (ir_want_runtime_safety(g, &instruction->base) && ptr_index != SIZE_MAX) {4097 } else if (ir_want_runtime_safety(g, &instruction->base) && ptr_index != SIZE_MAX) {
4096 LLVMValueRef ptr_field_ptr = LLVMBuildStructGEP2(g->builder, get_llvm_type(g, slice_type), result_loc, ptr_index, "");4098 LLVMValueRef ptr_field_ptr = LLVMBuildStructGEP2(g->builder, get_llvm_type(g, slice_type), result_loc, ptr_index, "");
...@@ -4762,7 +4764,7 @@ static LLVMValueRef ir_render_elem_ptr(CodeGen *g, Stage1Air *executable, Stage1...@@ -4762,7 +4764,7 @@ static LLVMValueRef ir_render_elem_ptr(CodeGen *g, Stage1Air *executable, Stage1
4762 LLVMConstNull(g->builtin_types.entry_usize->llvm_type),4764 LLVMConstNull(g->builtin_types.entry_usize->llvm_type),
4763 subscript_value4765 subscript_value
4764 };4766 };
4765 return LLVMBuildInBoundsGEP2(g->builder, get_llvm_type(g, child_type), array_ptr,4767 return LLVMBuildInBoundsGEP2(g->builder, get_llvm_type(g, array_type), array_ptr,
4766 indices, 2, "");4768 indices, 2, "");
4767 } else if (array_type->id == ZigTypeIdPointer) {4769 } else if (array_type->id == ZigTypeIdPointer) {
4768 LLVMValueRef array_ptr = get_handle_value(g, array_ptr_ptr, array_type, array_ptr_type);4770 LLVMValueRef array_ptr = get_handle_value(g, array_ptr_ptr, array_type, array_ptr_type);
...@@ -5440,8 +5442,6 @@ static LLVMValueRef ir_render_struct_field_ptr(CodeGen *g, Stage1Air *executable...@@ -5440,8 +5442,6 @@ static LLVMValueRef ir_render_struct_field_ptr(CodeGen *g, Stage1Air *executable
5440 LLVMValueRef struct_ptr = ir_llvm_value(g, instruction->struct_ptr);5442 LLVMValueRef struct_ptr = ir_llvm_value(g, instruction->struct_ptr);
5441 // not necessarily a pointer. could be ZigTypeIdStruct5443 // not necessarily a pointer. could be ZigTypeIdStruct
5442 ZigType *struct_ptr_type = instruction->struct_ptr->value->type;5444 ZigType *struct_ptr_type = instruction->struct_ptr->value->type;
5443 ZigType *struct_ty = (struct_ptr_type->id == ZigTypeIdPointer) ?
5444 struct_ptr_type->data.pointer.child_type : struct_ptr_type;
5445 TypeStructField *field = instruction->field;5445 TypeStructField *field = instruction->field;
54465446
5447 if (!type_has_bits(g, field->type_entry))5447 if (!type_has_bits(g, field->type_entry))
...@@ -5469,7 +5469,7 @@ static LLVMValueRef ir_render_struct_field_ptr(CodeGen *g, Stage1Air *executable...@@ -5469,7 +5469,7 @@ static LLVMValueRef ir_render_struct_field_ptr(CodeGen *g, Stage1Air *executable
54695469
5470 ir_assert(field->gen_index != SIZE_MAX, &instruction->base);5470 ir_assert(field->gen_index != SIZE_MAX, &instruction->base);
5471 LLVMValueRef field_ptr_val = LLVMBuildStructGEP2(g->builder,5471 LLVMValueRef field_ptr_val = LLVMBuildStructGEP2(g->builder,
5472 get_llvm_type(g, struct_ty), struct_ptr, (unsigned)field->gen_index, "");5472 get_llvm_type(g, struct_type), struct_ptr, (unsigned)field->gen_index, "");
5473 ZigType *res_type = instruction->base.value->type;5473 ZigType *res_type = instruction->base.value->type;
5474 ir_assert(res_type->id == ZigTypeIdPointer, &instruction->base);5474 ir_assert(res_type->id == ZigTypeIdPointer, &instruction->base);
5475 if (res_type->data.pointer.host_int_bytes != 0) {5475 if (res_type->data.pointer.host_int_bytes != 0) {
...@@ -5753,7 +5753,8 @@ static LLVMValueRef ir_render_asm_gen(CodeGen *g, Stage1Air *executable, Stage1A...@@ -5753,7 +5753,8 @@ static LLVMValueRef ir_render_asm_gen(CodeGen *g, Stage1Air *executable, Stage1A
5753 LLVMValueRef asm_fn = LLVMGetInlineAsm(function_type, buf_ptr(&llvm_template), buf_len(&llvm_template),5753 LLVMValueRef asm_fn = LLVMGetInlineAsm(function_type, buf_ptr(&llvm_template), buf_len(&llvm_template),
5754 buf_ptr(&constraint_buf), buf_len(&constraint_buf), is_volatile, false, LLVMInlineAsmDialectATT, false);5754 buf_ptr(&constraint_buf), buf_len(&constraint_buf), is_volatile, false, LLVMInlineAsmDialectATT, false);
57555755
5756 LLVMValueRef built_call = LLVMBuildCall2(g->builder, LLVMGlobalGetValueType(asm_fn), asm_fn, param_values, (unsigned)input_and_output_count, "");5756 LLVMValueRef built_call = LLVMBuildCall2(g->builder, function_type,
5757 asm_fn, param_values, (unsigned)input_and_output_count, "");
57575758
5758 for (size_t i = 0; i < input_and_output_count; i += 1) {5759 for (size_t i = 0; i < input_and_output_count; i += 1) {
5759 if (param_needs_attr[i] != nullptr) {5760 if (param_needs_attr[i] != nullptr) {
...@@ -6073,11 +6074,9 @@ static LLVMValueRef ir_render_err_name(CodeGen *g, Stage1Air *executable, Stage1...@@ -6073,11 +6074,9 @@ static LLVMValueRef ir_render_err_name(CodeGen *g, Stage1Air *executable, Stage1
6073 LLVMConstNull(g->builtin_types.entry_usize->llvm_type),6074 LLVMConstNull(g->builtin_types.entry_usize->llvm_type),
6074 err_val,6075 err_val,
6075 };6076 };
6076 ZigType *u8_ptr_type = get_pointer_to_type_extra(g, g->builtin_types.entry_u8, true, false,6077 return LLVMBuildInBoundsGEP2(g->builder,
6077 PtrLenUnknown, get_abi_alignment(g, g->builtin_types.entry_u8), 0, 0, false);6078 LLVMGlobalGetValueType(g->err_name_table),
6078 ZigType *str_type = get_slice_type(g, u8_ptr_type);6079 g->err_name_table, indices, 2, "");
6079 LLVMTypeRef elem_llvm_ty = get_llvm_type(g, str_type);
6080 return LLVMBuildInBoundsGEP2(g->builder, elem_llvm_ty, g->err_name_table, indices, 2, "");
6081}6080}
60826081
6083static LLVMValueRef get_enum_tag_name_function(CodeGen *g, ZigType *enum_type) {6082static LLVMValueRef get_enum_tag_name_function(CodeGen *g, ZigType *enum_type) {
...@@ -6697,15 +6696,14 @@ static LLVMValueRef ir_render_slice(CodeGen *g, Stage1Air *executable, Stage1Air...@@ -6697,15 +6696,14 @@ static LLVMValueRef ir_render_slice(CodeGen *g, Stage1Air *executable, Stage1Air
6697 codegen_report_errors_and_exit(g);6696 codegen_report_errors_and_exit(g);
66986697
6699 if (value_has_bits) {6698 if (value_has_bits) {
6700 LLVMTypeRef elem_llvm_ty = get_llvm_type(g, array_type->data.array.child_type);6699 LLVMTypeRef array_llvm_ty = get_llvm_type(g, array_type);
6701
6702 if (want_runtime_safety && sentinel != nullptr) {6700 if (want_runtime_safety && sentinel != nullptr) {
6703 LLVMValueRef indices[] = {6701 LLVMValueRef indices[] = {
6704 LLVMConstNull(g->builtin_types.entry_usize->llvm_type),6702 LLVMConstNull(g->builtin_types.entry_usize->llvm_type),
6705 end_val,6703 end_val,
6706 };6704 };
6707 LLVMValueRef sentinel_elem_ptr = LLVMBuildInBoundsGEP2(g->builder,6705 LLVMValueRef sentinel_elem_ptr = LLVMBuildInBoundsGEP2(g->builder,
6708 elem_llvm_ty, array_ptr, indices, 2, "");6706 array_llvm_ty, array_ptr, indices, 2, "");
6709 add_sentinel_check(g, sentinel_elem_ptr, sentinel);6707 add_sentinel_check(g, sentinel_elem_ptr, sentinel);
6710 }6708 }
67116709
...@@ -6713,7 +6711,7 @@ static LLVMValueRef ir_render_slice(CodeGen *g, Stage1Air *executable, Stage1Air...@@ -6713,7 +6711,7 @@ static LLVMValueRef ir_render_slice(CodeGen *g, Stage1Air *executable, Stage1Air
6713 LLVMConstNull(g->builtin_types.entry_usize->llvm_type),6711 LLVMConstNull(g->builtin_types.entry_usize->llvm_type),
6714 start_val,6712 start_val,
6715 };6713 };
6716 slice_start_ptr = LLVMBuildInBoundsGEP2(g->builder, elem_llvm_ty, array_ptr, indices, 2, "");6714 slice_start_ptr = LLVMBuildInBoundsGEP2(g->builder, array_llvm_ty, array_ptr, indices, 2, "");
6717 }6715 }
67186716
6719 len_value = LLVMBuildNUWSub(g->builder, end_val, start_val, "");6717 len_value = LLVMBuildNUWSub(g->builder, end_val, start_val, "");
...@@ -6732,15 +6730,14 @@ static LLVMValueRef ir_render_slice(CodeGen *g, Stage1Air *executable, Stage1Air...@@ -6732,15 +6730,14 @@ static LLVMValueRef ir_render_slice(CodeGen *g, Stage1Air *executable, Stage1Air
6732 codegen_report_errors_and_exit(g);6730 codegen_report_errors_and_exit(g);
67336731
6734 if (value_has_bits) {6732 if (value_has_bits) {
6735 LLVMTypeRef elem_llvm_ty = get_llvm_type(g, array_type->data.pointer.child_type);6733 LLVMTypeRef array_llvm_ty = get_llvm_type(g, array_type);
6736
6737 if (want_runtime_safety && sentinel != nullptr) {6734 if (want_runtime_safety && sentinel != nullptr) {
6738 LLVMValueRef sentinel_elem_ptr = LLVMBuildInBoundsGEP2(g->builder, elem_llvm_ty,6735 LLVMValueRef sentinel_elem_ptr = LLVMBuildInBoundsGEP2(g->builder, array_llvm_ty,
6739 array_ptr, &end_val, 1, "");6736 array_ptr, &end_val, 1, "");
6740 add_sentinel_check(g, sentinel_elem_ptr, sentinel);6737 add_sentinel_check(g, sentinel_elem_ptr, sentinel);
6741 }6738 }
67426739
6743 slice_start_ptr = LLVMBuildInBoundsGEP2(g->builder, elem_llvm_ty, array_ptr,6740 slice_start_ptr = LLVMBuildInBoundsGEP2(g->builder, array_llvm_ty, array_ptr,
6744 &start_val, 1, "");6741 &start_val, 1, "");
6745 }6742 }
67466743