From 685b5c26b703d54381a74b71361816ad85ec8584 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Fri, 26 Feb 2021 13:32:04 -0700 Subject: [PATCH] stage1: upgrade to new LLVM sret attribute requirement LLVM 12 requires sret attributes to have the struct type as a parameter, and provides no C function for supplying it. Therefore, we must add another C++ wrapper API for adding the sret attribute. Fixes ability to build from source in the llvm12 branch. --- src/stage1/codegen.cpp | 2 +- src/zig_llvm.cpp | 10 ++++++++++ src/zig_llvm.h | 1 + 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/stage1/codegen.cpp b/src/stage1/codegen.cpp index fcb3282d06..3e9fbe20dd 100644 --- a/src/stage1/codegen.cpp +++ b/src/stage1/codegen.cpp @@ -549,7 +549,7 @@ static LLVMValueRef make_fn_llvm_value(CodeGen *g, ZigFn *fn) { } else if (want_first_arg_sret(g, &fn_type->data.fn.fn_type_id)) { // Sret pointers must not be address 0 addLLVMArgAttr(llvm_fn, 0, "nonnull"); - addLLVMArgAttr(llvm_fn, 0, "sret"); + ZigLLVMAddSRetAttr(llvm_fn, get_llvm_type(g, fn_type->data.fn.fn_type_id.return_type)); if (cc_want_sret_attr(cc)) { addLLVMArgAttr(llvm_fn, 0, "noalias"); } diff --git a/src/zig_llvm.cpp b/src/zig_llvm.cpp index b4bdf7c033..9b10f25d47 100644 --- a/src/zig_llvm.cpp +++ b/src/zig_llvm.cpp @@ -802,6 +802,16 @@ void ZigLLVMAddByValAttr(LLVMValueRef fn_ref, unsigned ArgNo, LLVMTypeRef type_v func->setAttributes(new_attr_set); } +void ZigLLVMAddSRetAttr(LLVMValueRef fn_ref, LLVMTypeRef type_val) { + Function *func = unwrap(fn_ref); + const AttributeList attr_set = func->getAttributes(); + AttrBuilder attr_builder; + Type *llvm_type = unwrap(type_val); + attr_builder.addStructRetAttr(llvm_type); + const AttributeList new_attr_set = attr_set.addAttributes(func->getContext(), 1, attr_builder); + func->setAttributes(new_attr_set); +} + void ZigLLVMAddFunctionAttr(LLVMValueRef fn_ref, const char *attr_name, const char *attr_value) { Function *func = unwrap(fn_ref); const AttributeList attr_set = func->getAttributes(); diff --git a/src/zig_llvm.h b/src/zig_llvm.h index fc9f39d9e9..78909ad019 100644 --- a/src/zig_llvm.h +++ b/src/zig_llvm.h @@ -269,6 +269,7 @@ ZIG_EXTERN_C void ZigLLVMFunctionSetCallingConv(LLVMValueRef function, enum ZigL ZIG_EXTERN_C void ZigLLVMAddFunctionAttr(LLVMValueRef fn, const char *attr_name, const char *attr_value); ZIG_EXTERN_C void ZigLLVMAddByValAttr(LLVMValueRef fn_ref, unsigned ArgNo, LLVMTypeRef type_val); +ZIG_EXTERN_C void ZigLLVMAddSRetAttr(LLVMValueRef fn_ref, LLVMTypeRef type_val); ZIG_EXTERN_C void ZigLLVMAddFunctionAttrCold(LLVMValueRef fn); ZIG_EXTERN_C void ZigLLVMParseCommandLineOptions(size_t argc, const char *const *argv);