From 72404db31f76635f11d0aa4dcc02c149ef885b3d Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sun, 28 Feb 2021 22:01:13 -0700 Subject: [PATCH] stage1: update to LLVM 12 sret callsite requirements Without this, the LLVM IR that zig generates cannot be compiled by LLVM. --- src/stage1/codegen.cpp | 8 +------- src/zig_llvm.cpp | 9 +++++++++ src/zig_llvm.h | 1 + 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/stage1/codegen.cpp b/src/stage1/codegen.cpp index 389ede91b7..1338f58e30 100644 --- a/src/stage1/codegen.cpp +++ b/src/stage1/codegen.cpp @@ -4037,12 +4037,6 @@ static void gen_set_stack_pointer(CodeGen *g, LLVMValueRef aligned_end_addr) { LLVMBuildCall(g->builder, write_register_fn_val, params, 2, ""); } -static void set_call_instr_sret(CodeGen *g, LLVMValueRef call_instr) { - unsigned attr_kind_id = LLVMGetEnumAttributeKindForName("sret", 4); - LLVMAttributeRef sret_attr = LLVMCreateEnumAttribute(LLVMGetGlobalContext(), attr_kind_id, 0); - LLVMAddCallSiteAttribute(call_instr, 1, sret_attr); -} - static void render_async_spills(CodeGen *g) { ZigType *fn_type = g->cur_fn->type_entry; ZigType *import = get_scope_import(&g->cur_fn->fndef_scope->base); @@ -4558,7 +4552,7 @@ static LLVMValueRef ir_render_call(CodeGen *g, IrExecutableGen *executable, IrIn } else if (!ret_has_bits) { return nullptr; } else if (first_arg_ret) { - set_call_instr_sret(g, result); + ZigLLVMSetCallSret(result, get_llvm_type(g, src_return_type)); return result_loc; } else if (handle_is_ptr(g, src_return_type)) { LLVMValueRef store_instr = LLVMBuildStore(g->builder, result, result_loc); diff --git a/src/zig_llvm.cpp b/src/zig_llvm.cpp index 55e507c6d0..5a7d60f83b 100644 --- a/src/zig_llvm.cpp +++ b/src/zig_llvm.cpp @@ -940,6 +940,15 @@ void ZigLLVMSetTailCall(LLVMValueRef Call) { unwrap(Call)->setTailCallKind(CallInst::TCK_MustTail); } +void ZigLLVMSetCallSret(LLVMValueRef Call, LLVMTypeRef return_type) { + const AttributeList attr_set = unwrap(Call)->getAttributes(); + AttrBuilder attr_builder; + Type *llvm_type = unwrap(return_type); + attr_builder.addStructRetAttr(llvm_type); + const AttributeList new_attr_set = attr_set.addAttributes(unwrap(Call)->getContext(), 1, attr_builder); + unwrap(Call)->setAttributes(new_attr_set); +} + void ZigLLVMFunctionSetPrefixData(LLVMValueRef function, LLVMValueRef data) { unwrap(function)->setPrefixData(unwrap(data)); } diff --git a/src/zig_llvm.h b/src/zig_llvm.h index c8ffa32e81..0d08980835 100644 --- a/src/zig_llvm.h +++ b/src/zig_llvm.h @@ -264,6 +264,7 @@ ZIG_EXTERN_C struct ZigLLVMDILocation *ZigLLVMGetDebugLoc(unsigned line, unsigne ZIG_EXTERN_C void ZigLLVMSetFastMath(LLVMBuilderRef builder_wrapped, bool on_state); ZIG_EXTERN_C void ZigLLVMSetTailCall(LLVMValueRef Call); +ZIG_EXTERN_C void ZigLLVMSetCallSret(LLVMValueRef Call, LLVMTypeRef return_type); ZIG_EXTERN_C void ZigLLVMFunctionSetPrefixData(LLVMValueRef fn, LLVMValueRef data); ZIG_EXTERN_C void ZigLLVMFunctionSetCallingConv(LLVMValueRef function, enum ZigLLVM_CallingConv cc);