authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-02-27 10:00:07-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-02-27 14:57:49-05:00
logc2f5634fb3df51622cf74f23b4ae0d4a7d2bbbe9
tree18dec1e751c7383933b9fca4ddb435ae739c6a49
parent4e43bde924694a640cfec13141df1f3f611ffe0f

another llvm workaround for getelementptr


3 files changed, 81 insertions(+), 6 deletions(-)

src/all_types.hpp+10-1
......@@ -1461,6 +1461,14 @@ struct LinkLib {
14611461 bool provided_explicitly;
14621462};
14631463
1464struct WorkaroundStructGEPId {
1465 LLVMTypeRef struct_ptr_type;
1466 uint32_t index;
1467};
1468
1469uint32_t workaround_struct_gep_hash(WorkaroundStructGEPId x);
1470bool workaround_struct_gep_eq(WorkaroundStructGEPId a, WorkaroundStructGEPId b);
1471
14641472struct CodeGen {
14651473 LLVMModuleRef module;
14661474 ZigList<ErrorMsg*> errors;
......@@ -1491,7 +1499,7 @@ struct CodeGen {
14911499 HashMap<Buf *, AstNode *, buf_hash, buf_eql_buf> exported_symbol_names;
14921500 HashMap<Buf *, Tld *, buf_hash, buf_eql_buf> external_prototypes;
14931501 HashMap<Buf *, ConstExprValue *, buf_hash, buf_eql_buf> string_literals_table;
1494
1502 HashMap<WorkaroundStructGEPId, LLVMValueRef, workaround_struct_gep_hash, workaround_struct_gep_eq> workaround_struct_gep_table;
14951503
14961504 ZigList<ImportTableEntry *> import_queue;
14971505 size_t import_queue_index;
......@@ -1603,6 +1611,7 @@ struct CodeGen {
16031611 LLVMValueRef cur_ret_ptr;
16041612 LLVMValueRef cur_fn_val;
16051613 LLVMValueRef cur_err_ret_trace_val;
1614 bool cur_workaround_gep_on;
16061615 bool c_want_stdint;
16071616 bool c_want_stdbool;
16081617 AstNode *root_export_decl;
src/analyze.cpp+8
......@@ -5821,3 +5821,11 @@ bool type_is_global_error_set(TypeTableEntry *err_set_type) {
58215821uint32_t get_coro_frame_align_bytes(CodeGen *g) {
58225822 return g->pointer_size_bytes * 2;
58235823}
5824
5825uint32_t workaround_struct_gep_hash(WorkaroundStructGEPId x) {
5826 return ptr_hash(x.struct_ptr_type) ^ x.index;
5827}
5828
5829bool workaround_struct_gep_eq(WorkaroundStructGEPId a, WorkaroundStructGEPId b) {
5830 return a.struct_ptr_type == b.struct_ptr_type && a.index == b.index;
5831}
src/codegen.cpp+63-5
......@@ -88,6 +88,7 @@ CodeGen *codegen_create(Buf *root_src_path, const ZigTarget *target, OutType out
8888 g->exported_symbol_names.init(8);
8989 g->external_prototypes.init(8);
9090 g->string_literals_table.init(16);
91 g->workaround_struct_gep_table.init(8);
9192 g->is_test_build = false;
9293 g->want_h_file = (out_type == OutTypeObj || out_type == OutTypeLib);
9394 buf_resize(&g->global_asm, 0);
......@@ -2781,6 +2782,52 @@ static LLVMValueRef ir_render_call(CodeGen *g, IrExecutable *executable, IrInstr
27812782 }
27822783}
27832784
2785static LLVMValueRef get_workaround_struct_gep_fn_val(CodeGen *g, LLVMTypeRef struct_ptr_type, uint32_t index) {
2786 WorkaroundStructGEPId hash_id = {struct_ptr_type, index};
2787 auto existing_entry = g->workaround_struct_gep_table.maybe_get(hash_id);
2788 if (existing_entry)
2789 return existing_entry->value;
2790
2791 LLVMTypeRef arg_types[] = {
2792 struct_ptr_type,
2793 };
2794 LLVMTypeRef result_type = LLVMStructGetTypeAtIndex(LLVMGetElementType(struct_ptr_type), index);
2795 LLVMTypeRef fn_type_ref = LLVMFunctionType(LLVMPointerType(result_type, 0), arg_types, 1, false);
2796
2797 Buf *fn_name = get_mangled_name(g, buf_create_from_str("__zig_workaround_llvm_struct_gep"), false);
2798 LLVMValueRef fn_val = LLVMAddFunction(g->module, buf_ptr(fn_name), fn_type_ref);
2799 LLVMSetLinkage(fn_val, LLVMInternalLinkage);
2800 LLVMSetFunctionCallConv(fn_val, get_llvm_cc(g, CallingConventionUnspecified));
2801 addLLVMFnAttr(fn_val, "nounwind");
2802 addLLVMArgAttr(fn_val, (unsigned)0, "nonnull");
2803
2804 LLVMBasicBlockRef entry_block = LLVMAppendBasicBlock(fn_val, "Entry");
2805 LLVMBasicBlockRef prev_block = LLVMGetInsertBlock(g->builder);
2806 LLVMValueRef prev_debug_location = LLVMGetCurrentDebugLocation(g->builder);
2807 LLVMPositionBuilderAtEnd(g->builder, entry_block);
2808 ZigLLVMClearCurrentDebugLocation(g->builder);
2809
2810 LLVMValueRef result = LLVMBuildStructGEP(g->builder, LLVMGetParam(fn_val, 0), index, "");
2811 LLVMBuildRet(g->builder, result);
2812
2813 LLVMPositionBuilderAtEnd(g->builder, prev_block);
2814 LLVMSetCurrentDebugLocation(g->builder, prev_debug_location);
2815
2816 g->workaround_struct_gep_table.put(hash_id, fn_val);
2817 return fn_val;
2818}
2819
2820static LLVMValueRef gen_workaround_struct_gep(CodeGen *g, LLVMValueRef struct_ptr, uint32_t field_index) {
2821 if (g->cur_workaround_gep_on) {
2822 // We need to generate a normal StructGEP but due to llvm bugs we have to workaround it by
2823 // putting the GEP in a function call
2824 LLVMValueRef fn_val = get_workaround_struct_gep_fn_val(g, LLVMTypeOf(struct_ptr), field_index);
2825 return LLVMBuildCall(g->builder, fn_val, &struct_ptr, 1, "");
2826 } else {
2827 return LLVMBuildStructGEP(g->builder, struct_ptr, field_index, "");
2828 }
2829}
2830
27842831static LLVMValueRef ir_render_struct_field_ptr(CodeGen *g, IrExecutable *executable,
27852832 IrInstructionStructFieldPtr *instruction)
27862833{
......@@ -2799,7 +2846,7 @@ static LLVMValueRef ir_render_struct_field_ptr(CodeGen *g, IrExecutable *executa
27992846 }
28002847
28012848 assert(field->gen_index != SIZE_MAX);
2802 return LLVMBuildStructGEP(g->builder, struct_ptr, (unsigned)field->gen_index, "");
2849 return gen_workaround_struct_gep(g, struct_ptr, field->gen_index);
28032850}
28042851
28052852static LLVMValueRef ir_render_union_field_ptr(CodeGen *g, IrExecutable *executable,
......@@ -3655,7 +3702,7 @@ static LLVMValueRef ir_render_test_err(CodeGen *g, IrExecutable *executable, IrI
36553702
36563703 LLVMValueRef err_val;
36573704 if (type_has_bits(payload_type)) {
3658 LLVMValueRef err_val_ptr = LLVMBuildStructGEP(g->builder, err_union_handle, err_union_err_index, "");
3705 LLVMValueRef err_val_ptr = gen_workaround_struct_gep(g, err_union_handle, err_union_err_index);
36593706 err_val = gen_load_untyped(g, err_val_ptr, 0, false, "");
36603707 } else {
36613708 err_val = err_union_handle;
......@@ -3674,7 +3721,12 @@ static LLVMValueRef ir_render_unwrap_err_code(CodeGen *g, IrExecutable *executab
36743721 LLVMValueRef err_union_handle = get_handle_value(g, err_union_ptr, err_union_type, ptr_type);
36753722
36763723 if (type_has_bits(payload_type)) {
3677 LLVMValueRef err_val_ptr = LLVMBuildStructGEP(g->builder, err_union_handle, err_union_err_index, "");
3724 LLVMValueRef err_val_ptr;
3725 if (g->cur_workaround_gep_on) {
3726 err_val_ptr = gen_workaround_struct_gep(g, err_union_handle, err_union_err_index);
3727 } else {
3728 err_val_ptr = LLVMBuildStructGEP(g->builder, err_union_handle, err_union_err_index, "");
3729 }
36783730 return gen_load_untyped(g, err_val_ptr, 0, false, "");
36793731 } else {
36803732 return err_union_handle;
......@@ -3696,7 +3748,7 @@ static LLVMValueRef ir_render_unwrap_err_payload(CodeGen *g, IrExecutable *execu
36963748 if (ir_want_runtime_safety(g, &instruction->base) && instruction->safety_check_on && g->errors_by_index.length > 1) {
36973749 LLVMValueRef err_val;
36983750 if (type_has_bits(payload_type)) {
3699 LLVMValueRef err_val_ptr = LLVMBuildStructGEP(g->builder, err_union_handle, err_union_err_index, "");
3751 LLVMValueRef err_val_ptr = gen_workaround_struct_gep(g, err_union_handle, err_union_err_index);
37003752 err_val = gen_load_untyped(g, err_val_ptr, 0, false, "");
37013753 } else {
37023754 err_val = err_union_handle;
......@@ -3714,7 +3766,11 @@ static LLVMValueRef ir_render_unwrap_err_payload(CodeGen *g, IrExecutable *execu
37143766 }
37153767
37163768 if (type_has_bits(payload_type)) {
3717 return LLVMBuildStructGEP(g->builder, err_union_handle, err_union_payload_index, "");
3769 if (g->cur_workaround_gep_on) {
3770 return gen_workaround_struct_gep(g, err_union_handle, err_union_payload_index);
3771 } else {
3772 return LLVMBuildStructGEP(g->builder, err_union_handle, err_union_payload_index, "");
3773 }
37183774 } else {
37193775 return nullptr;
37203776 }
......@@ -3934,6 +3990,7 @@ static LLVMValueRef ir_render_coro_begin(CodeGen *g, IrExecutable *executable, I
39343990 coro_id,
39353991 coro_mem_ptr,
39363992 };
3993 g->cur_workaround_gep_on = false;
39373994 return LLVMBuildCall(g->builder, get_coro_begin_fn_val(g), params, 2, "");
39383995}
39393996
......@@ -5073,6 +5130,7 @@ static void do_code_gen(CodeGen *g) {
50735130 LLVMValueRef fn = fn_llvm_value(g, fn_table_entry);
50745131 g->cur_fn = fn_table_entry;
50755132 g->cur_fn_val = fn;
5133 g->cur_workaround_gep_on = fn_table_entry->type_entry->data.fn.fn_type_id.cc == CallingConventionAsync;
50765134 TypeTableEntry *return_type = fn_table_entry->type_entry->data.fn.fn_type_id.return_type;
50775135 if (handle_is_ptr(return_type)) {
50785136 g->cur_ret_ptr = LLVMGetParam(fn, 0);