authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-08-02 16:48:58-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-08-02 16:48:58-07:00
log30b98d39731a8645d7a344a24e77a96161cc97a8
tree09ffc14bb1d915b62e4967877c74c94be8312fdc
parent99318e7a95220c0bba88eb42b17747ef79da176d

more LLVM backend fixes

more carnage from opaque pointers API

1 files changed, 83 insertions(+), 45 deletions(-)

src/stage1/codegen.cpp+83-45
......@@ -1968,7 +1968,10 @@ static void gen_assign_raw(CodeGen *g, LLVMValueRef ptr, ZigType *ptr_type,
19681968 if (ptr_type->data.pointer.vector_index != VECTOR_INDEX_NONE) {
19691969 LLVMValueRef index_val = LLVMConstInt(LLVMInt32Type(),
19701970 ptr_type->data.pointer.vector_index, false);
1971 LLVMValueRef loaded_vector = gen_load(g, ptr, ptr_type, "");
1971 uint32_t vec_len = ptr_type->data.pointer.host_int_bytes;
1972 LLVMTypeRef vec_llvm_ty = LLVMVectorType(get_llvm_type(g, ptr_type->data.pointer.child_type), vec_len);
1973 LLVMValueRef loaded_vector = gen_load_untyped(g, vec_llvm_ty, ptr,
1974 get_ptr_align(g, ptr_type), ptr_type->data.pointer.is_volatile, "");
19721975 LLVMValueRef new_vector = LLVMBuildInsertElement(g->builder, loaded_vector, value,
19731976 index_val, "");
19741977 gen_store(g, new_vector, ptr, ptr_type);
......@@ -1985,7 +1988,8 @@ static void gen_assign_raw(CodeGen *g, LLVMValueRef ptr, ZigType *ptr_type,
19851988
19861989 LLVMTypeRef int_ptr_ty = LLVMPointerType(LLVMIntType(host_int_bytes * 8), 0);
19871990 LLVMValueRef int_ptr = LLVMBuildBitCast(g->builder, ptr, int_ptr_ty, "");
1988 LLVMValueRef containing_int = gen_load(g, int_ptr, ptr_type, "");
1991 LLVMValueRef containing_int = gen_load_untyped(g, LLVMIntType(host_int_bytes * 8), int_ptr,
1992 get_ptr_align(g, ptr_type), ptr_type->data.pointer.is_volatile, "");
19891993 uint32_t host_bit_count = LLVMGetIntTypeWidth(LLVMTypeOf(containing_int));
19901994 assert(host_bit_count == host_int_bytes * 8);
19911995 uint32_t size_in_bits = type_size_bits(g, child_type);
......@@ -2296,8 +2300,9 @@ static bool iter_function_params_c_abi(CodeGen *g, ZigType *fn_type, FnWalk *fn_
22962300 fn_walk->data.attrs.gen_i += 1;
22972301 break;
22982302 case FnWalkIdCall: {
2299 LLVMTypeRef ptr_elem_llvm_ty = LLVMIntType((unsigned)ty_size * 8);
2300 LLVMValueRef loaded = LLVMBuildLoad2(g->builder, ptr_elem_llvm_ty, val, "");
2303 LLVMTypeRef int_type_ref = LLVMIntType((unsigned)ty_size * 8);
2304 LLVMValueRef bitcasted = LLVMBuildBitCast(g->builder, val, LLVMPointerType(int_type_ref, 0), "");
2305 LLVMValueRef loaded = LLVMBuildLoad2(g->builder, int_type_ref, bitcasted, "");
23012306 fn_walk->data.call.gen_param_values->append(loaded);
23022307 break;
23032308 }
......@@ -2365,14 +2370,15 @@ static bool iter_function_params_c_abi(CodeGen *g, ZigType *fn_type, FnWalk *fn_
23652370 break;
23662371 }
23672372 case FnWalkIdCall: {
2373 LLVMValueRef abi_ptr_to_struct = LLVMBuildBitCast(g->builder, val, LLVMPointerType(abi_type, 0), "");
23682374 if (number_of_regs == 1) {
2369 LLVMValueRef loaded = LLVMBuildLoad2(g->builder, abi_type, val, "");
2375 LLVMValueRef loaded = LLVMBuildLoad2(g->builder, abi_type, abi_ptr_to_struct, "");
23702376 fn_walk->data.call.gen_param_values->append(loaded);
23712377 break;
23722378 }
23732379 for (uint32_t i = 0; i < number_of_regs; i += 1) {
23742380 LLVMValueRef adjusted_ptr_to_struct = LLVMBuildStructGEP2(g->builder,
2375 abi_type, val, i, "");
2381 abi_type, abi_ptr_to_struct, i, "");
23762382 LLVMValueRef loaded = LLVMBuildLoad2(g->builder,
23772383 ZigLLVMGetGEPResultElementType(adjusted_ptr_to_struct),
23782384 adjusted_ptr_to_struct, "");
......@@ -2410,13 +2416,14 @@ static bool iter_function_params_c_abi(CodeGen *g, ZigType *fn_type, FnWalk *fn_
24102416 LLVMValueRef bitcasted = LLVMBuildBitCast(g->builder, var->value_ref, ptr_to_int_type_ref, "");
24112417 gen_store_untyped(g, arg, bitcasted, var->align_bytes, false);
24122418 } else {
2419 LLVMValueRef abi_ptr_to_struct = LLVMBuildBitCast(g->builder, var->value_ref, LLVMPointerType(abi_type, 0), "");
24132420 for (uint32_t i = 0; i < number_of_regs; i += 1) {
24142421 LLVMValueRef arg = LLVMGetParam(llvm_fn, fn_walk->data.inits.gen_i + i);
24152422 LLVMValueRef zero = LLVMConstInt(LLVMInt32Type(), 0, false);
24162423 LLVMValueRef index = LLVMConstInt(LLVMInt32Type(), i, false);
24172424 LLVMValueRef indices[] = { zero, index };
24182425 LLVMValueRef adjusted_ptr_to_struct = LLVMBuildInBoundsGEP2(g->builder,
2419 abi_type, var->value_ref, indices, 2, "");
2426 abi_type, abi_ptr_to_struct, indices, 2, "");
24202427 LLVMBuildStore(g->builder, arg, adjusted_ptr_to_struct);
24212428 }
24222429 fn_walk->data.inits.gen_i += 1;
......@@ -2745,7 +2752,7 @@ static LLVMValueRef gen_resume(CodeGen *g, LLVMValueRef fn_val, LLVMValueRef tar
27452752 if (fn_val == nullptr) {
27462753 LLVMValueRef fn_ptr_ptr = LLVMBuildStructGEP2(g->builder, g->any_frame_header_llvm_ty,
27472754 target_frame_ptr, frame_fn_ptr_index, "");
2748 fn_val = LLVMBuildLoad2(g->builder, LLVMPointerTypeInContext(LLVMGetGlobalContext(), 0),
2755 fn_val = LLVMBuildLoad2(g->builder, ZigLLVMGetGEPResultElementType(fn_ptr_ptr),
27492756 fn_ptr_ptr, "");
27502757 }
27512758 LLVMValueRef arg_val = LLVMConstSub(LLVMConstAllOnes(usize_type_ref),
......@@ -2855,7 +2862,7 @@ static void gen_async_return(CodeGen *g, Stage1AirInstReturn *instruction) {
28552862 get_llvm_type(g, get_fn_frame_type(g, g->cur_fn)),
28562863 g->cur_frame_ptr, frame_ret_start + 1, "");
28572864 LLVMValueRef awaiter_ret_ptr = LLVMBuildLoad2(g->builder,
2858 LLVMPointerTypeInContext(LLVMGetGlobalContext(), 0), awaiter_ret_ptr_ptr, "");
2865 ZigLLVMGetGEPResultElementType(awaiter_ret_ptr_ptr), awaiter_ret_ptr_ptr, "");
28592866 LLVMValueRef zero_ptr = LLVMConstNull(LLVMTypeOf(awaiter_ret_ptr));
28602867 LLVMValueRef need_copy_bit = LLVMBuildICmp(g->builder, LLVMIntNE, awaiter_ret_ptr, zero_ptr, "");
28612868 LLVMBuildCondBr(g->builder, need_copy_bit, copy_block, copy_end_block);
......@@ -2878,7 +2885,7 @@ static void gen_async_return(CodeGen *g, Stage1AirInstReturn *instruction) {
28782885 get_llvm_type(g, get_fn_frame_type(g, g->cur_fn)),
28792886 g->cur_frame_ptr, frame_index_trace_arg(g, ret_type) + 1, "");
28802887 LLVMValueRef dest_trace_ptr = LLVMBuildLoad2(g->builder,
2881 LLVMPointerTypeInContext(LLVMGetGlobalContext(), 0),
2888 ZigLLVMGetGEPResultElementType(awaiter_trace_ptr_ptr),
28822889 awaiter_trace_ptr_ptr, "");
28832890 bool is_llvm_alloca;
28842891 LLVMValueRef my_err_trace_val = get_cur_err_ret_trace_val(g, instruction->base.scope, &is_llvm_alloca);
......@@ -2902,12 +2909,18 @@ static LLVMValueRef gen_convert_to_c_abi(CodeGen *g, LLVMValueRef location, LLVM
29022909 ZigType *return_type = g->cur_fn->type_entry->data.fn.gen_return_type;
29032910 size_t size = type_size(g, return_type);
29042911 LLVMTypeRef abi_return_type = get_llvm_c_abi_type(g, return_type);
2912 LLVMTypeRef abi_return_type_pointer = LLVMPointerType(abi_return_type, 0);
29052913
29062914 if (size < 8) {
2907 return LLVMBuildLoad2(g->builder, abi_return_type, value, "");
2915 LLVMValueRef bitcast = LLVMBuildBitCast(g->builder, value, abi_return_type_pointer, "");
2916 return LLVMBuildLoad2(g->builder, abi_return_type, bitcast, "");
29082917 } else {
2918 LLVMTypeRef i8ptr = LLVMPointerType(LLVMInt8Type(), 0);
2919 LLVMValueRef bc_location = LLVMBuildBitCast(g->builder, location, i8ptr, "");
2920 LLVMValueRef bc_value = LLVMBuildBitCast(g->builder, value, i8ptr, "");
2921
29092922 LLVMValueRef len = LLVMConstInt(LLVMInt64Type(), size, false);
2910 ZigLLVMBuildMemCpy(g->builder, location, 8, value, return_type->abi_align, len, false);
2923 ZigLLVMBuildMemCpy(g->builder, bc_location, 8, bc_value, return_type->abi_align, len, false);
29112924 return LLVMBuildLoad2(g->builder, abi_return_type, location, "");
29122925 }
29132926}
......@@ -4144,20 +4157,21 @@ static LLVMValueRef ir_render_bit_cast(CodeGen *g, Stage1Air *executable,
41444157 bool actual_is_ptr = handle_is_ptr(g, actual_type);
41454158 if (wanted_is_ptr == actual_is_ptr) {
41464159 // We either bitcast the value directly or bitcast the pointer which does a pointer cast
4147 if (wanted_is_ptr) {
4148 return value;
4149 } else {
4150 LLVMTypeRef wanted_type_ref = get_llvm_type(g, wanted_type);
4151 return LLVMBuildBitCast(g->builder, value, wanted_type_ref, "");
4152 }
4160 LLVMTypeRef wanted_type_ref = wanted_is_ptr ?
4161 LLVMPointerType(get_llvm_type(g, wanted_type), 0) : get_llvm_type(g, wanted_type);
4162 return LLVMBuildBitCast(g->builder, value, wanted_type_ref, "");
41534163 } else if (actual_is_ptr) {
41544164 // A scalar is wanted but we got a pointer
4165 LLVMTypeRef wanted_elem_type_ref = get_llvm_type(g, wanted_type);
4166 LLVMValueRef bitcasted_ptr = LLVMBuildBitCast(g->builder, value,
4167 LLVMPointerType(wanted_elem_type_ref, 0), "");
41554168 uint32_t alignment = get_abi_alignment(g, actual_type);
4156 return gen_load_untyped(g, get_llvm_type(g, wanted_type), value, alignment, false, "");
4169 return gen_load_untyped(g, wanted_elem_type_ref, bitcasted_ptr, alignment, false, "");
41574170 } else {
41584171 // A pointer is wanted but we got a scalar
41594172 assert(actual_type->id == ZigTypeIdPointer);
4160 return value;
4173 LLVMTypeRef wanted_ptr_type_ref = LLVMPointerType(get_llvm_type(g, wanted_type), 0);
4174 return LLVMBuildBitCast(g->builder, value, wanted_ptr_type_ref, "");
41614175 }
41624176}
41634177
......@@ -4434,7 +4448,9 @@ static LLVMValueRef ir_render_load_ptr(CodeGen *g, Stage1Air *executable,
44344448 if (ptr_type->data.pointer.vector_index != VECTOR_INDEX_NONE) {
44354449 LLVMValueRef index_val = LLVMConstInt(LLVMInt32Type(),
44364450 ptr_type->data.pointer.vector_index, false);
4437 LLVMValueRef loaded_vector = LLVMBuildLoad2(g->builder, get_llvm_type(g, child_type), ptr, "");
4451 uint32_t vec_len = ptr_type->data.pointer.host_int_bytes;
4452 LLVMTypeRef vec_llvm_ty = LLVMVectorType(get_llvm_type(g, child_type), vec_len);
4453 LLVMValueRef loaded_vector = LLVMBuildLoad2(g->builder, vec_llvm_ty, ptr, "");
44384454 return LLVMBuildExtractElement(g->builder, loaded_vector, index_val, "");
44394455 }
44404456
......@@ -4446,10 +4462,11 @@ static LLVMValueRef ir_render_load_ptr(CodeGen *g, Stage1Air *executable,
44464462
44474463 LLVMTypeRef int_ptr_ty = LLVMPointerType(LLVMIntType(host_int_bytes * 8), 0);
44484464 LLVMValueRef int_ptr = LLVMBuildBitCast(g->builder, ptr, int_ptr_ty, "");
4449 LLVMValueRef containing_int = gen_load(g, int_ptr, ptr_type, "");
4465 LLVMValueRef containing_int = gen_load_untyped(g, LLVMIntType(host_int_bytes * 8), int_ptr,
4466 get_ptr_align(g, ptr_type), ptr_type->data.pointer.is_volatile, "");
44504467
44514468 uint32_t host_bit_count = LLVMGetIntTypeWidth(LLVMTypeOf(containing_int));
4452 assert(host_bit_count == host_int_bytes * 8);
4469 ir_assert(host_bit_count == host_int_bytes * 8, &instruction->base);
44534470 uint32_t size_in_bits = type_size_bits(g, child_type);
44544471
44554472 uint32_t bit_offset = ptr_type->data.pointer.bit_offset_in_host;
......@@ -4750,13 +4767,15 @@ static LLVMValueRef ir_render_elem_ptr(CodeGen *g, Stage1Air *executable, Stage1
47504767 size_t host_int_bytes = ptr_type->data.pointer.host_int_bytes;
47514768 if (host_int_bytes != 0) {
47524769 uint32_t size_in_bits = type_size_bits(g, ptr_type->data.pointer.child_type);
4770 LLVMTypeRef ptr_u8_type_ref = LLVMPointerType(LLVMInt8Type(), 0);
4771 LLVMValueRef u8_array_ptr = LLVMBuildBitCast(g->builder, array_ptr, ptr_u8_type_ref, "");
47534772 assert(size_in_bits % 8 == 0);
47544773 LLVMValueRef elem_size_bytes = LLVMConstInt(g->builtin_types.entry_usize->llvm_type,
47554774 size_in_bits / 8, false);
47564775 LLVMValueRef byte_offset = LLVMBuildNUWMul(g->builder, subscript_value, elem_size_bytes, "");
47574776 LLVMValueRef indices[] = { byte_offset };
47584777 LLVMValueRef elem_byte_ptr = LLVMBuildInBoundsGEP2(g->builder, LLVMInt8Type(),
4759 array_ptr, indices, 1, "");
4778 u8_array_ptr, indices, 1, "");
47604779 return LLVMBuildBitCast(g->builder, elem_byte_ptr, LLVMPointerType(get_llvm_type(g, child_type), 0), "");
47614780 }
47624781 }
......@@ -4802,8 +4821,8 @@ static LLVMValueRef ir_render_elem_ptr(CodeGen *g, Stage1Air *executable, Stage1
48024821 assert(ptr_index != SIZE_MAX);
48034822 LLVMValueRef ptr_ptr = LLVMBuildStructGEP2(g->builder, get_llvm_type(g, array_type),
48044823 array_ptr, (unsigned)ptr_index, "");
4805 LLVMValueRef ptr = gen_load_untyped(g, ZigLLVMGetGEPResultElementType(ptr_ptr), ptr_ptr, 0,
4806 false, "");
4824 LLVMValueRef ptr = gen_load_untyped(g,
4825 LLVMPointerTypeInContext(LLVMGetGlobalContext(), 0), ptr_ptr, 0, false, "");
48074826 LLVMTypeRef elem_llvm_ty = get_llvm_type(g, ptr_type->data.pointer.child_type);
48084827 return LLVMBuildInBoundsGEP2(g->builder, elem_llvm_ty, ptr, &subscript_value, 1, "");
48094828 } else if (array_type->id == ZigTypeIdVector) {
......@@ -4938,8 +4957,10 @@ static void render_async_var_decls(CodeGen *g, Scope *scope) {
49384957static LLVMValueRef gen_frame_size(CodeGen *g, LLVMValueRef fn_val) {
49394958 assert(g->need_frame_size_prefix_data);
49404959 LLVMTypeRef usize_llvm_type = g->builtin_types.entry_usize->llvm_type;
4960 LLVMTypeRef ptr_usize_llvm_type = LLVMPointerType(usize_llvm_type, 0);
4961 LLVMValueRef casted_fn_val = LLVMBuildBitCast(g->builder, fn_val, ptr_usize_llvm_type, "");
49414962 LLVMValueRef negative_one = LLVMConstInt(LLVMInt32Type(), -1, true);
4942 LLVMValueRef prefix_ptr = LLVMBuildInBoundsGEP2(g->builder, usize_llvm_type, fn_val, &negative_one, 1, "");
4963 LLVMValueRef prefix_ptr = LLVMBuildInBoundsGEP2(g->builder, usize_llvm_type, casted_fn_val, &negative_one, 1, "");
49434964 LLVMValueRef load_inst = LLVMBuildLoad2(g->builder, usize_llvm_type, prefix_ptr, "");
49444965
49454966 // Some architectures (e.g SPARCv9) has different alignment requirements between a
......@@ -4980,17 +5001,20 @@ static LLVMValueRef ir_render_call(CodeGen *g, Stage1Air *executable, Stage1AirI
49805001 LLVMTypeRef usize_type_ref = g->builtin_types.entry_usize->llvm_type;
49815002
49825003 LLVMValueRef fn_val;
5004 LLVMTypeRef fn_llvm_ty;
49835005 ZigType *fn_type;
49845006 bool callee_is_async;
49855007 if (instruction->fn_entry) {
49865008 fn_val = fn_llvm_value(g, instruction->fn_entry);
49875009 fn_type = instruction->fn_entry->type_entry;
49885010 callee_is_async = fn_is_async(instruction->fn_entry);
5011 fn_llvm_ty = LLVMGlobalGetValueType(fn_val);
49895012 } else {
49905013 assert(instruction->fn_ref);
49915014 fn_val = ir_llvm_value(g, instruction->fn_ref);
49925015 fn_type = instruction->fn_ref->value->type;
49935016 callee_is_async = fn_type->data.fn.fn_type_id.cc == CallingConventionAsync;
5017 fn_llvm_ty = fn_type->data.fn.raw_type_ref;
49945018 }
49955019
49965020 FnTypeId *fn_type_id = &fn_type->data.fn.fn_type_id;
......@@ -5251,6 +5275,7 @@ static LLVMValueRef ir_render_call(CodeGen *g, Stage1Air *executable, Stage1AirI
52515275 CalcLLVMFieldIndex arg_calc_start = {0};
52525276 frame_index_arg_calc(g, &arg_calc_start, fn_type->data.fn.fn_type_id.return_type);
52535277
5278 LLVMValueRef casted_frame;
52545279 LLVMTypeRef casted_frame_llvm_ty;
52555280 if (instruction->new_stack != nullptr && instruction->fn_entry == nullptr) {
52565281 // We need the frame type to be a pointer to a struct that includes the args
......@@ -5282,15 +5307,19 @@ static LLVMValueRef ir_render_call(CodeGen *g, Stage1Air *executable, Stage1AirI
52825307 }
52835308 LLVMTypeRef frame_with_args_type = LLVMStructType(field_types, field_count, false);
52845309 heap::c_allocator.deallocate(field_types, field_count);
5310 LLVMTypeRef ptr_frame_with_args_type = LLVMPointerType(frame_with_args_type, 0);
5311
5312 casted_frame = LLVMBuildBitCast(g->builder, frame_result_loc, ptr_frame_with_args_type, "");
52855313 casted_frame_llvm_ty = frame_with_args_type;
52865314 } else {
5315 casted_frame = frame_result_loc;
52875316 casted_frame_llvm_ty = frame_struct_llvm_ty;
52885317 }
52895318
52905319 CalcLLVMFieldIndex arg_calc = arg_calc_start;
52915320 for (size_t arg_i = 0; arg_i < gen_param_values.length; arg_i += 1) {
52925321 calc_llvm_field_index_add(g, &arg_calc, gen_param_types.at(arg_i));
5293 LLVMValueRef arg_ptr = LLVMBuildStructGEP2(g->builder, casted_frame_llvm_ty, frame_result_loc, arg_calc.field_index - 1, "");
5322 LLVMValueRef arg_ptr = LLVMBuildStructGEP2(g->builder, casted_frame_llvm_ty, casted_frame, arg_calc.field_index - 1, "");
52945323 gen_assign_raw(g, arg_ptr, get_pointer_to_type(g, gen_param_types.at(arg_i), true),
52955324 gen_param_values.at(arg_i));
52965325 }
......@@ -5384,7 +5413,7 @@ static LLVMValueRef ir_render_call(CodeGen *g, Stage1Air *executable, Stage1AirI
53845413 }
53855414
53865415 if (instruction->new_stack == nullptr || instruction->is_async_call_builtin) {
5387 result = ZigLLVMBuildCall(g->builder, LLVMGlobalGetValueType(fn_val), fn_val,
5416 result = ZigLLVMBuildCall(g->builder, fn_llvm_ty, fn_val,
53885417 gen_param_values.items, (unsigned)gen_param_values.length, llvm_cc, call_attr, "");
53895418 } else if (instruction->modifier == CallModifierAsync) {
53905419 zig_panic("TODO @asyncCall of non-async function");
......@@ -5398,7 +5427,7 @@ static LLVMValueRef ir_render_call(CodeGen *g, Stage1Air *executable, Stage1AirI
53985427 old_stack_ref = LLVMBuildCall2(g->builder, LLVMGlobalGetValueType(stacksave_fn_val), stacksave_fn_val, nullptr, 0, "");
53995428 }
54005429 gen_set_stack_pointer(g, new_stack_addr);
5401 result = ZigLLVMBuildCall(g->builder, LLVMGlobalGetValueType(fn_val), fn_val,
5430 result = ZigLLVMBuildCall(g->builder, fn_llvm_ty, fn_val,
54025431 gen_param_values.items, (unsigned)gen_param_values.length, llvm_cc, call_attr, "");
54035432 if (src_return_type->id != ZigTypeIdUnreachable) {
54045433 LLVMValueRef stackrestore_fn_val = get_stackrestore_fn_val(g);
......@@ -5567,7 +5596,8 @@ static LLVMValueRef ir_render_union_field_ptr(CodeGen *g, Stage1Air *executable,
55675596
55685597 LLVMValueRef union_field_ptr = LLVMBuildStructGEP2(g->builder, get_llvm_type(g, union_type),
55695598 union_ptr, union_type->data.unionation.gen_union_index, "");
5570 return union_field_ptr;
5599 LLVMValueRef bitcasted_union_field_ptr = LLVMBuildBitCast(g->builder, union_field_ptr, field_type_ref, "");
5600 return bitcasted_union_field_ptr;
55715601}
55725602
55735603static size_t find_asm_index(CodeGen *g, AstNode *node, AsmToken *tok, Buf *src_template) {
......@@ -5707,7 +5737,8 @@ static LLVMValueRef ir_render_asm_gen(CodeGen *g, Stage1Air *executable, Stage1A
57075737 param_values[param_index] = value_ref;
57085738 // In the case of indirect inputs, LLVM requires the callsite to have an elementtype(<ty>) attribute.
57095739 if (buf_ptr(asm_input->constraint)[0] == '*') {
5710 param_needs_attr[param_index] = elem_type_ref;
5740 param_needs_attr[param_index] = elem_type_ref ? elem_type_ref :
5741 get_llvm_type(g, type->data.pointer.child_type);
57115742 }
57125743 }
57135744 for (size_t i = 0; i < asm_expr->clobber_list.length; i += 1, total_index += 1) {
......@@ -6148,7 +6179,7 @@ static LLVMValueRef get_enum_tag_name_function(CodeGen *g, ZigType *enum_type) {
61486179 LLVMSetAlignment(str_global, LLVMABIAlignmentOfType(g->target_data_ref, LLVMTypeOf(str_init)));
61496180
61506181 LLVMValueRef fields[] = {
6151 LLVMConstInBoundsGEP2(LLVMInt8Type(), str_global, array_ptr_indices, 2),
6182 LLVMConstInBoundsGEP2(LLVMGlobalGetValueType(str_global), str_global, array_ptr_indices, 2),
61526183 LLVMConstInt(g->builtin_types.entry_usize->llvm_type, buf_len(name), false),
61536184 };
61546185 LLVMValueRef slice_init_value = LLVMConstNamedStruct(get_llvm_type(g, u8_slice_type), fields, 2);
......@@ -6266,7 +6297,7 @@ static LLVMValueRef ir_render_align_cast(CodeGen *g, Stage1Air *executable, Stag
62666297
62676298 size_t ptr_index = target_type->data.structure.fields[slice_ptr_index]->gen_index;
62686299 LLVMValueRef ptr_val_ptr = LLVMBuildStructGEP2(g->builder,
6269 get_llvm_type(g, instruction->target->value->type->data.pointer.child_type),
6300 get_llvm_type(g, target_type),
62706301 target_val, (unsigned)ptr_index, "");
62716302 ptr_val = gen_load_untyped(g, ZigLLVMGetGEPResultElementType(ptr_val_ptr), ptr_val_ptr, 0, false, "");
62726303 } else {
......@@ -6909,7 +6940,7 @@ static LLVMValueRef get_frame_address_fn_val(CodeGen *g) {
69096940
69106941 LLVMTypeRef fn_type = LLVMFunctionType(get_llvm_type(g, return_type),
69116942 &g->builtin_types.entry_i32->llvm_type, 1, false);
6912 g->frame_address_fn_val = LLVMAddFunction(g->module, "llvm.frameaddress.p0i8", fn_type);
6943 g->frame_address_fn_val = LLVMAddFunction(g->module, "llvm.frameaddress.p0", fn_type);
69136944 assert(LLVMGetIntrinsicID(g->frame_address_fn_val));
69146945
69156946 return g->frame_address_fn_val;
......@@ -7285,9 +7316,10 @@ static LLVMValueRef ir_render_atomic_load(CodeGen *g, Stage1Air *executable,
72857316 LLVMTypeRef actual_abi_type = get_atomic_abi_type(g, instruction->ptr, false);
72867317 if (actual_abi_type != nullptr) {
72877318 // operand needs widening and truncating
7288 ptr = LLVMBuildBitCast(g->builder, ptr,
7289 LLVMPointerType(actual_abi_type, 0), "");
7290 LLVMValueRef load_inst = gen_load(g, ptr, instruction->ptr->value->type, "");
7319 ptr = LLVMBuildBitCast(g->builder, ptr, LLVMPointerType(actual_abi_type, 0), "");
7320 LLVMValueRef load_inst = gen_load_untyped(g, actual_abi_type, ptr,
7321 get_ptr_align(g, instruction->ptr->value->type),
7322 instruction->ptr->value->type->data.pointer.is_volatile, "");
72917323 LLVMSetOrdering(load_inst, ordering);
72927324 return LLVMBuildTrunc(g->builder, load_inst, get_llvm_type(g, operand_type), "");
72937325 }
......@@ -7503,10 +7535,12 @@ static LLVMValueRef ir_render_array_to_vector(CodeGen *g, Stage1Air *executable,
75037535 ZigType *elem_type = vector_type->data.vector.elem_type;
75047536 bool bitcast_ok = elem_type->size_in_bits == elem_type->abi_size * 8;
75057537 if (bitcast_ok) {
7538 LLVMValueRef casted_ptr = LLVMBuildBitCast(g->builder, array_ptr,
7539 LLVMPointerType(vector_type_ref, 0), "");
75067540 ZigType *array_type = instruction->array->value->type;
75077541 assert(array_type->id == ZigTypeIdArray);
75087542 uint32_t alignment = get_abi_alignment(g, array_type->data.array.child_type);
7509 return gen_load_untyped(g, vector_type_ref, array_ptr, alignment, false, "");
7543 return gen_load_untyped(g, vector_type_ref, casted_ptr, alignment, false, "");
75107544 } else {
75117545 // If the ABI size of the element type is not evenly divisible by size_in_bits, a simple bitcast
75127546 // will not work, and we fall back to insertelement.
......@@ -8060,28 +8094,32 @@ static LLVMValueRef gen_const_ptr_array_recursive(CodeGen *g, ZigValue *array_co
80608094 LLVMValueRef base_ptr = gen_parent_ptr(g, array_const_val, parent);
80618095
80628096 ZigType *usize = g->builtin_types.entry_usize;
8097 LLVMTypeRef array_llvm_ty = get_llvm_type(g, array_const_val->type);
8098 LLVMValueRef casted_base_ptr = LLVMConstBitCast(base_ptr, LLVMPointerType(array_llvm_ty, 0));
80638099 LLVMValueRef indices[] = {
80648100 LLVMConstNull(usize->llvm_type),
80658101 LLVMConstInt(usize->llvm_type, index, false),
80668102 };
8067 return LLVMConstInBoundsGEP2(get_llvm_type(g, array_const_val->type), base_ptr, indices, 2);
8103 return LLVMConstInBoundsGEP2(array_llvm_ty, casted_base_ptr, indices, 2);
80688104}
80698105
80708106static LLVMValueRef gen_const_ptr_struct_recursive(CodeGen *g, ZigValue *struct_const_val, size_t field_index) {
80718107 ConstParent *parent = &struct_const_val->parent;
80728108 LLVMValueRef base_ptr = gen_parent_ptr(g, struct_const_val, parent);
80738109
8074 ZigType *u32 = g->builtin_types.entry_u32;
80758110 LLVMValueRef indices[] = {
8076 LLVMConstNull(get_llvm_type(g, u32)),
8077 LLVMConstInt(get_llvm_type(g, u32), field_index, false),
8111 LLVMConstNull(LLVMInt32Type()),
8112 LLVMConstInt(LLVMInt32Type(), field_index, false),
80788113 };
80798114
80808115 // The structure pointed by base_ptr may include trailing padding for
80818116 // alignment purposes and have the following LLVM type: <{ %T, [N x i8] }>.
80828117 // Add an extra bitcast as we're only interested in the %T part.
80838118 assert(handle_is_ptr(g, struct_const_val->type));
8084 return LLVMConstInBoundsGEP2(get_llvm_type(g, struct_const_val->type), base_ptr, indices, 2);
8119
8120 LLVMTypeRef struct_llvm_ty = get_llvm_type(g, struct_const_val->type);
8121 LLVMValueRef casted_base_ptr = LLVMConstBitCast(base_ptr, LLVMPointerType(struct_llvm_ty, 0));
8122 return LLVMConstInBoundsGEP2(struct_llvm_ty, casted_base_ptr, indices, 2);
80858123}
80868124
80878125static LLVMValueRef gen_const_ptr_err_union_code_recursive(CodeGen *g, ZigValue *err_union_const_val) {
......@@ -9398,7 +9436,7 @@ static void do_code_gen(CodeGen *g) {
93989436 get_llvm_type(g, get_fn_frame_type(g, g->cur_fn)),
93999437 g->cur_frame_ptr, frame_ret_start, "");
94009438 g->cur_ret_ptr = LLVMBuildLoad2(g->builder,
9401 LLVMPointerTypeInContext(LLVMGetGlobalContext(), 0), cur_ret_ptr_ptr, "");
9439 ZigLLVMGetGEPResultElementType(cur_ret_ptr_ptr), cur_ret_ptr_ptr, "");
94029440 }
94039441 uint32_t trace_field_index_stack = UINT32_MAX;
94049442 if (codegen_fn_has_err_ret_tracing_stack(g, fn_table_entry, true)) {