From patchwork Tue Feb 27 01:17:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Li Xu X-Patchwork-Id: 1904798 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4TkKRg3dH3z1yX4 for ; Tue, 27 Feb 2024 12:20:30 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8DEC03858C5E for ; Tue, 27 Feb 2024 01:20:27 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from sgoci-sdnproxy-4.icoremail.net (sgoci-sdnproxy-4.icoremail.net [129.150.39.64]) by sourceware.org (Postfix) with ESMTP id 517103858C98 for ; Tue, 27 Feb 2024 01:20:06 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 517103858C98 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=eswincomputing.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=eswincomputing.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 517103858C98 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=129.150.39.64 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1708996811; cv=none; b=xlsPxY3qa6YR3XFJMCQuLTqTn6stOLO1zyoBwlymGh/Ir5Hrw/VBtOvlTLz9aOUsTCoG1e0ylGqz4jIq2ZMMqEJRPcUFhi09Zur0BvDjCRk+XMuBrSvnu1HxdlGAGOqH/A8nfWSDSbCYMTKR64jtXuUkzvBbF/Csd3ijbuPUBNA= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1708996811; c=relaxed/simple; bh=+zRoSMvPzyX3kCY0Em65ztcHG1u/WbWMcH3QZIvM4Bc=; h=From:To:Subject:Date:Message-Id; b=w+iERXhXqH0eXKDYQNPB/LKW+WJ3YQ0EhUAtOMgwt5P1cBzBu1gNwGRTk8kmFuMHhguTiyf0HiXMNEreFKuPodvbtxhb4HXSm2k1WkX/PEG06YAQ8y0cynotPqT7/uZ4zTVef0BqXf1156glGvBoWXFvCb4gHEK+K/UrEsKapR4= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from host014-ubuntu-1804.lxd (unknown [10.12.130.31]) by app1 (Coremail) with SMTP id TAJkCgA3w_0DON1l9HIZAA--.5846S4; Tue, 27 Feb 2024 09:16:52 +0800 (CST) From: Li Xu To: gcc-patches@gcc.gnu.org Cc: kito.cheng@gmail.com, palmer@dabbelt.com, juzhe.zhong@rivai.ai, zhengyu@eswincomputing.com, xuli Subject: [PATCH] RISC-V: Add riscv_vector_cc function attribute Date: Tue, 27 Feb 2024 01:17:14 +0000 Message-Id: <20240227011714.25482-1-xuli1@eswincomputing.com> X-Mailer: git-send-email 2.17.1 X-CM-TRANSID: TAJkCgA3w_0DON1l9HIZAA--.5846S4 X-Coremail-Antispam: 1UD129KBjvJXoW3uw1kJr1DAry8trWUWrWrAFb_yoWkAF4rpF WUGry2yw15JFs7XanIq3yrX3W3GrZ3ury5JrnxJw4jkrW5JanrtFWkGws0y3W7GFWUurnx Za1UCw4Iy398ArUanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUk214x267AKxVW8JVW5JwAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2ocxC64kIII0Yj41l84x0c7CEw4AK67xGY2AK02 1l84ACjcxK6xIIjxv20xvE14v26w1j6s0DM28EF7xvwVC0I7IYx2IY6xkF7I0E14v26r4U JVWxJr1l84ACjcxK6I8E87Iv67AKxVW0oVCq3wA2z4x0Y4vEx4A2jsIEc7CjxVAFwI0_Gc CE3s1le2I262IYc4CY6c8Ij28IcVAaY2xG8wAqx4xG64xvF2IEw4CE5I8CrVC2j2WlYx0E 2Ix0cI8IcVAFwI0_Jr0_Jr4lYx0Ex4A2jsIE14v26r1j6r4UMcvjeVCFs4IE7xkEbVWUJV W8JwACjcxG0xvY0x0EwIxGrwACjI8F5VA0II8E6IAqYI8I648v4I1lc2xSY4AK6svPMxAI w28IcxkI7VAKI48JMxC20s026xCaFVCjc4AY6r1j6r4UMI8I3I0E5I8CrVAFwI0_Jr0_Jr 4lx2IqxVCjr7xvwVAFwI0_JrI_JrWlx4CE17CEb7AF67AKxVWUAVWUtwCIc40Y0x0EwIxG rwCI42IY6xIIjxv20xvE14v26r1j6r1xMIIF0xvE2Ix0cI8IcVCY1x0267AKxVWUJVW8Jw CI42IY6xAIw20EY4v20xvaj40_Jr0_JF4lIxAIcVC2z280aVAFwI0_Jr0_Gr1lIxAIcVC2 z280aVCY1x0267AKxVWUJVW8JbIYCTnIWIevJa73UjIFyTuYvjfUoOJ5UUUUU X-CM-SenderInfo: 50xoxi46hv4xpqfrz1xxwl0woofrz/ X-Spam-Status: No, score=-12.0 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, RCVD_IN_VALIDITY_RPBL, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org From: xuli Standard vector calling convention variant will only enabled when function has vector argument or returning value by default, however user may also want to invoke function without that during a vectorized loop at some situation, but it will cause a huge performance penalty due to vector register store/restore. So user can declare function with this riscv_vector_cc attribute like below, that could enforce function will use standard vector calling convention variant. void foo() __attribute__((riscv_vector_cc)); [[riscv::vector_cc]] void foo(); // For C++11 and C23 For more details please reference the below link. https://github.com/riscv-non-isa/riscv-c-api-doc/pull/67 gcc/ChangeLog: * config/riscv/riscv.cc (TARGET_GNU_ATTRIBUTES): Add riscv_vector_cc attribute to riscv_attribute_table. (riscv_vector_cc_function_p): Return true if FUNC is a riscv_vector_cc function. (riscv_fntype_abi): Add riscv_vector_cc attribute check. * doc/extend.texi: Add riscv_vector_cc attribute description. gcc/testsuite/ChangeLog: * g++.target/riscv/rvv/base/attribute-riscv_vector_cc-error.C: New test. * gcc.target/riscv/rvv/base/attribute-riscv_vector_cc-callee-saved.c: New test. * gcc.target/riscv/rvv/base/attribute-riscv_vector_cc-error.c: New test. --- gcc/config/riscv/riscv.cc | 55 ++++++-- gcc/doc/extend.texi | 12 ++ .../base/attribute-riscv_vector_cc-error.C | 22 ++++ .../attribute-riscv_vector_cc-callee-saved.c | 117 ++++++++++++++++++ .../base/attribute-riscv_vector_cc-error.c | 11 ++ 5 files changed, 209 insertions(+), 8 deletions(-) create mode 100644 gcc/testsuite/g++.target/riscv/rvv/base/attribute-riscv_vector_cc-error.C create mode 100644 gcc/testsuite/gcc.target/riscv/rvv/base/attribute-riscv_vector_cc-callee-saved.c create mode 100644 gcc/testsuite/gcc.target/riscv/rvv/base/attribute-riscv_vector_cc-error.c diff --git a/gcc/config/riscv/riscv.cc b/gcc/config/riscv/riscv.cc index 4100abc9dd1..7f37f231796 100644 --- a/gcc/config/riscv/riscv.cc +++ b/gcc/config/riscv/riscv.cc @@ -537,24 +537,52 @@ static tree riscv_handle_fndecl_attribute (tree *, tree, tree, int, bool *); static tree riscv_handle_type_attribute (tree *, tree, tree, int, bool *); /* Defining target-specific uses of __attribute__. */ -TARGET_GNU_ATTRIBUTES (riscv_attribute_table, +static const attribute_spec riscv_gnu_attributes[] = { /* Syntax: { name, min_len, max_len, decl_required, type_required, function_type_required, affects_type_identity, handler, exclude } */ /* The attribute telling no prologue/epilogue. */ - { "naked", 0, 0, true, false, false, false, - riscv_handle_fndecl_attribute, NULL }, + {"naked", 0, 0, true, false, false, false, riscv_handle_fndecl_attribute, + NULL}, /* This attribute generates prologue/epilogue for interrupt handlers. */ - { "interrupt", 0, 1, false, true, true, false, - riscv_handle_type_attribute, NULL }, + {"interrupt", 0, 1, false, true, true, false, riscv_handle_type_attribute, + NULL}, /* The following two are used for the built-in properties of the Vector type and are not used externally */ {"RVV sizeless type", 4, 4, false, true, false, true, NULL, NULL}, - {"RVV type", 0, 0, false, true, false, true, NULL, NULL} -}); + {"RVV type", 0, 0, false, true, false, true, NULL, NULL}, + /* This attribute is used to declare a function, forcing it to use the + standard vector calling convention variant. Syntax: + __attribute__((riscv_vector_cc)). */ + {"riscv_vector_cc", 0, 0, false, true, true, true, NULL, NULL} +}; + +static const scoped_attribute_specs riscv_gnu_attribute_table = +{ + "gnu", {riscv_gnu_attributes} +}; + +static const attribute_spec riscv_attributes[] = +{ + /* This attribute is used to declare a function, forcing it to use the + standard vector calling convention variant. Syntax: + [[riscv::vector_cc]]. */ + {"vector_cc", 0, 0, false, true, true, true, NULL, NULL} +}; + +static const scoped_attribute_specs riscv_nongnu_attribute_table = +{ + "riscv", {riscv_attributes} +}; + +static const scoped_attribute_specs *const riscv_attribute_table[] = +{ + &riscv_gnu_attribute_table, + &riscv_nongnu_attribute_table +}; /* Order for the CLOBBERs/USEs of gpr_save. */ static const unsigned gpr_save_reg_order[] = { @@ -5425,6 +5453,16 @@ riscv_arguments_is_vector_type_p (const_tree fntype) return false; } +/* Return true if FUNC is a riscv_vector_cc function. + For more details please reference the below link. + https://github.com/riscv-non-isa/riscv-c-api-doc/pull/67 */ +static bool +riscv_vector_cc_function_p (const_tree fntype) +{ + return lookup_attribute ("vector_cc", TYPE_ATTRIBUTES (fntype)) != NULL_TREE + || lookup_attribute ("riscv_vector_cc", TYPE_ATTRIBUTES (fntype)) != NULL_TREE; +} + /* Implement TARGET_FNTYPE_ABI. */ static const predefined_function_abi & @@ -5434,7 +5472,8 @@ riscv_fntype_abi (const_tree fntype) reference the below link. https://github.com/riscv-non-isa/riscv-elf-psabi-doc/pull/389 */ if (riscv_return_value_is_vector_type_p (fntype) - || riscv_arguments_is_vector_type_p (fntype)) + || riscv_arguments_is_vector_type_p (fntype) + || riscv_vector_cc_function_p (fntype)) return riscv_v_abi (); return default_function_abi; diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi index 2135dfde9c8..afe486ba47b 100644 --- a/gcc/doc/extend.texi +++ b/gcc/doc/extend.texi @@ -6314,6 +6314,18 @@ Permissible values for this parameter are @code{user}, @code{supervisor}, and @code{machine}. If there is no parameter, then it defaults to @code{machine}. +@cindex @code{riscv_vector_cc} function attribute, RISC-V +@item riscv_vector_cc +Use this attribute to force the function to use the vector calling +convention variant. +For more information on riscv_vector_cc, please see +@uref{https://github.com/riscv-non-isa/riscv-c-api-doc/pull/67} + +@smallexample +void foo() __attribute__((riscv_vector_cc)); +[[riscv::vector_cc]] void foo(); // For C++11 and C23 +@end smallexample + @end table The following target-specific function attributes are available for the diff --git a/gcc/testsuite/g++.target/riscv/rvv/base/attribute-riscv_vector_cc-error.C b/gcc/testsuite/g++.target/riscv/rvv/base/attribute-riscv_vector_cc-error.C new file mode 100644 index 00000000000..7a547b5e7ff --- /dev/null +++ b/gcc/testsuite/g++.target/riscv/rvv/base/attribute-riscv_vector_cc-error.C @@ -0,0 +1,22 @@ +/* { dg-do compile } */ +/* { dg-options "-march=rv64gcv -mabi=lp64d -O1" } */ + + +[[riscv::vector_cc]] void foo();// For C++11 and C23 + +[[riscv::vector_cc]] int var; /* { dg-warning "'vector_cc' attribute only applies to function types" } */ + +void __attribute__((riscv_vector_cc)) func(); +void __attribute__((riscv_vector_cc(1))) func_invalid(); /* { dg-error "wrong number of arguments specified for 'riscv_vector_cc' attribute" } */ + +void test_no_attribute(int); +void __attribute__((riscv_vector_cc)) test_no_attribute(int x) { } + +class test_cc { + __attribute__((riscv_vector_cc)) void member_func(); +}; + +void test_lambda() { + __attribute__((riscv_vector_cc)) auto lambda = []() { /* { dg-warning "'riscv_vector_cc' attribute only applies to function types" } */ + }; +} diff --git a/gcc/testsuite/gcc.target/riscv/rvv/base/attribute-riscv_vector_cc-callee-saved.c b/gcc/testsuite/gcc.target/riscv/rvv/base/attribute-riscv_vector_cc-callee-saved.c new file mode 100644 index 00000000000..7db9d874bcd --- /dev/null +++ b/gcc/testsuite/gcc.target/riscv/rvv/base/attribute-riscv_vector_cc-callee-saved.c @@ -0,0 +1,117 @@ +/* { dg-do compile } */ +/* { dg-options "-march=rv64gcv -mabi=lp64d -O1" } */ +/* { dg-final { check-function-bodies "**" "" } } */ + +#include + +void __attribute__((riscv_vector_cc)) bar1 (int a); +void bar2 (); + +/* +** foo1: +** addi\tsp,sp,-16 +** sd\tra,8\(sp\) +** call\tbar1 +** ld\tra,8\(sp\) +** addi\tsp,sp,16 +** jr\tra +*/ +void __attribute__((riscv_vector_cc)) +foo1 (int a) +{ + bar1 (a); +} + +/* +** foo2: +** addi\tsp,sp,-16 +** sd\tra,8\(sp\) +** csrr\tt0,vlenb +** sub\tsp,sp,t0 +** vs1r\.v\tv1,0\(sp\) +** sub\tsp,sp,t0 +** vs1r\.v\tv2,0\(sp\) +** sub\tsp,sp,t0 +** vs1r\.v\tv3,0\(sp\) +** sub\tsp,sp,t0 +** vs1r\.v\tv4,0\(sp\) +** sub\tsp,sp,t0 +** vs1r\.v\tv5,0\(sp\) +** sub\tsp,sp,t0 +** vs1r\.v\tv6,0\(sp\) +** sub\tsp,sp,t0 +** vs1r\.v\tv7,0\(sp\) +** sub\tsp,sp,t0 +** vs1r\.v\tv24,0\(sp\) +** sub\tsp,sp,t0 +** vs1r\.v\tv25,0\(sp\) +** sub\tsp,sp,t0 +** vs1r\.v\tv26,0\(sp\) +** sub\tsp,sp,t0 +** vs1r\.v\tv27,0\(sp\) +** sub\tsp,sp,t0 +** vs1r\.v\tv28,0\(sp\) +** sub\tsp,sp,t0 +** vs1r\.v\tv29,0\(sp\) +** sub\tsp,sp,t0 +** vs1r\.v\tv30,0\(sp\) +** sub\tsp,sp,t0 +** vs1r\.v\tv31,0\(sp\) +** call\tbar2 +** csrr\tt0,vlenb +** vl1re64\.v\tv31,0\(sp\) +** add\tsp,sp,t0 +** vl1re64\.v\tv30,0\(sp\) +** add\tsp,sp,t0 +** vl1re64\.v\tv29,0\(sp\) +** add\tsp,sp,t0 +** vl1re64\.v\tv28,0\(sp\) +** add\tsp,sp,t0 +** vl1re64\.v\tv27,0\(sp\) +** add\tsp,sp,t0 +** vl1re64\.v\tv26,0\(sp\) +** add\tsp,sp,t0 +** vl1re64\.v\tv25,0\(sp\) +** add\tsp,sp,t0 +** vl1re64\.v\tv24,0\(sp\) +** add\tsp,sp,t0 +** vl1re64\.v\tv7,0\(sp\) +** add\tsp,sp,t0 +** vl1re64\.v\tv6,0\(sp\) +** add\tsp,sp,t0 +** vl1re64\.v\tv5,0\(sp\) +** add\tsp,sp,t0 +** vl1re64\.v\tv4,0\(sp\) +** add\tsp,sp,t0 +** vl1re64\.v\tv3,0\(sp\) +** add\tsp,sp,t0 +** vl1re64\.v\tv2,0\(sp\) +** add\tsp,sp,t0 +** vl1re64\.v\tv1,0\(sp\) +** add\tsp,sp,t0 +** ld\tra,8\(sp\) +** addi\tsp,sp,16 +** jr\tra +*/ +void __attribute__((riscv_vector_cc)) +foo2 (int a) +{ + int8_t data[1024]; + bar2 (); +} + +/* +** foo3: +** addi\tsp,sp,-16 +** sd\tra,8\(sp\) +** lw\ta0,0\(a0\) +** call\tbar1 +** ld\tra,8\(sp\) +** addi\tsp,sp,16 +** jr\tra +*/ +void +foo3 (int *a) +{ + bar1 (*a); +} diff --git a/gcc/testsuite/gcc.target/riscv/rvv/base/attribute-riscv_vector_cc-error.c b/gcc/testsuite/gcc.target/riscv/rvv/base/attribute-riscv_vector_cc-error.c new file mode 100644 index 00000000000..ceb5e5bf6dd --- /dev/null +++ b/gcc/testsuite/gcc.target/riscv/rvv/base/attribute-riscv_vector_cc-error.c @@ -0,0 +1,11 @@ +/* { dg-do compile } */ +/* { dg-options "-march=rv64gcv -mabi=lp64d -O1" } */ + +__attribute__((riscv_vector_cc)) int var; /* { dg-warning "'riscv_vector_cc' attribute only applies to function types" } */ +[[riscv::vector_cc]] int var1; /* { dg-warning "'vector_cc' attribute only applies to function types" } */ + +void __attribute__((riscv_vector_cc)) func(); +void __attribute__((riscv_vector_cc(1))) func_invalid(); /* { dg-error "wrong number of arguments specified for 'riscv_vector_cc' attribute" } */ + +void test_no_attribute(int); +void __attribute__((riscv_vector_cc)) test_no_attribute(int x) { }