From patchwork Fri Nov 1 20:59:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yangyu Chen X-Patchwork-Id: 2005316 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=qq.com header.i=@qq.com header.a=rsa-sha256 header.s=s201512 header.b=hOsvfIpk; dkim-atps=neutral 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 4XgCtG0jSZz1xwc for ; Sat, 2 Nov 2024 08:00:06 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4F7703857836 for ; Fri, 1 Nov 2024 21:00:04 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from out203-205-221-221.mail.qq.com (out203-205-221-221.mail.qq.com [203.205.221.221]) by sourceware.org (Postfix) with UTF8SMTPS id C78B23858428 for ; Fri, 1 Nov 2024 20:59:29 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C78B23858428 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=cyyself.name Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=cyyself.name ARC-Filter: OpenARC Filter v1.0.0 sourceware.org C78B23858428 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=203.205.221.221 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1730494773; cv=none; b=TekexlhsmrL1Gkd1sF/cUOQEUaER9Eosjx600JmHmPRE8WoC2RPbTS6dVwR37j8gffhBErsxETVhw3iUUNYQDUH5ZdjSw1i9203DGAyVZhKfOLeWpWpyXQRoDOpAOmrkNYamR2Jc/8Zm0VexB7ATMsuvZzL9KIFG173EbSp+Tyw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1730494773; c=relaxed/simple; bh=It88nyQ5WkotpkMm1jPQNChH3GvrXj1TLHOUcZJXISQ=; h=DKIM-Signature:Message-ID:From:To:Subject:Date:MIME-Version; b=hMTaPBAtCiU7Bh7Uf58eilejnomBO29o4+dd8I841iOeEC9Az32PjDh+lggulUuTOBgVQin9jte+C4XvhUjV37Zxx4nsEhKFg0BKBvnDQBMsnKKbKiMeFxdJFIC6ZeggbxQUQr5TyUwEfuGFOwlXrSzGsHpbRgMbXEADZd4neJ0= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=qq.com; s=s201512; t=1730494752; bh=5J9PSjlvmY5luUIQljJ5hp4NZe+4pLj2LjqvZGs5TZQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=hOsvfIpknw468j6gOCB8Todstwlcppha8y8W6d9iVXIDz5iK/4tZV8Oh20epHFn6m PLnKSdvUXg/utw8i4KXn2E0pNmnmdB6wWTs0wzEiaDdFlxH8Zl4IgmcJy7SQ6fGHXf uj59KFpgmB5epsNWFFVzHfU0BSDRX9Ciy4DydFes= Received: from cyy-pc.lan ([240e:379:2276:5d00:cda0:97a5:84f4:5191]) by newxmesmtplogicsvrsza29-0.qq.com (NewEsmtp) with SMTP id EC9276F1; Sat, 02 Nov 2024 04:59:09 +0800 X-QQ-mid: xmsmtpt1730494749t2sqc24mj Message-ID: X-QQ-XMAILINFO: MDbayGdXPuoe3UvUho3xPMH4xoY8euHvg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angyu Chen To: gcc-patches@gcc.gnu.org Cc: Kito Cheng , Palmer Dabbelt , Jiawei , Jeff Law , Evgeny Stupachenko , Andrew Carlotti , Yangyu Chen , Hank Chang Subject: [PATCH v4 4/8] RISC-V: Implement TARGET_OPTION_VALID_VERSION_ATTRIBUTE_P Date: Sat, 2 Nov 2024 04:59:07 +0800 X-OQ-MSGID: <20241101205907.1533688-1-cyy@cyyself.name> X-Mailer: git-send-email 2.45.2 In-Reply-To: References: MIME-Version: 1.0 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 This patch implements the TARGET_OPTION_VALID_VERSION_ATTRIBUTE_P for RISC-V. This hook is used to process attribute ((target_version ("..."))). As it is the first patch which introduces the target_version attribute, we also set TARGET_HAS_FMV_TARGET_ATTRIBUTE to 0 to use "target_version" for function versioning. Co-Developed-by: Hank Chang Signed-off-by: Yangyu Chen gcc/ChangeLog: * config/riscv/riscv-protos.h (riscv_process_target_attr): Remove as it is not used. (riscv_option_valid_version_attribute_p): Declare. (riscv_process_target_version_attr): Declare. * config/riscv/riscv-target-attr.cc (riscv_target_attrs): Renamed from riscv_attributes. (riscv_target_version_attrs): New attributes for target_version. (riscv_process_one_target_attr): New arguments to select attrs. (riscv_process_target_attr): Likewise. (riscv_option_valid_attribute_p): Likewise. (riscv_process_target_version_attr): New function. (riscv_option_valid_version_attribute_p): New function. * config/riscv/riscv.cc (TARGET_OPTION_VALID_VERSION_ATTRIBUTE_P): Implement it. * config/riscv/riscv.h (TARGET_HAS_FMV_TARGET_ATTRIBUTE): Define it to 0 to use "target_version" for function versioning. --- gcc/config/riscv/riscv-protos.h | 4 +- gcc/config/riscv/riscv-target-attr.cc | 118 +++++++++++++++++++++++--- gcc/config/riscv/riscv.cc | 4 + gcc/config/riscv/riscv.h | 2 + 4 files changed, 115 insertions(+), 13 deletions(-) diff --git a/gcc/config/riscv/riscv-protos.h b/gcc/config/riscv/riscv-protos.h index 4ed04321d32..500b357f6eb 100644 --- a/gcc/config/riscv/riscv-protos.h +++ b/gcc/config/riscv/riscv-protos.h @@ -806,7 +806,9 @@ void riscv_init_cumulative_args (CUMULATIVE_ARGS *, tree, rtx, tree, int); extern bool riscv_option_valid_attribute_p (tree, tree, tree, int); extern bool -riscv_process_target_attr (const char *, location_t); +riscv_option_valid_version_attribute_p (tree, tree, tree, int); +extern bool +riscv_process_target_version_attr (tree, location_t); extern void riscv_override_options_internal (struct gcc_options *); extern void riscv_option_override (void); diff --git a/gcc/config/riscv/riscv-target-attr.cc b/gcc/config/riscv/riscv-target-attr.cc index 293d5321215..2979d63a011 100644 --- a/gcc/config/riscv/riscv-target-attr.cc +++ b/gcc/config/riscv/riscv-target-attr.cc @@ -82,10 +82,16 @@ struct riscv_attribute_info /* The target attributes that we support. */ -static const struct riscv_attribute_info riscv_attributes[] +static const struct riscv_attribute_info riscv_target_attrs[] = {{"arch", &riscv_target_attr_parser::handle_arch}, {"cpu", &riscv_target_attr_parser::handle_cpu}, - {"tune", &riscv_target_attr_parser::handle_tune}}; + {"tune", &riscv_target_attr_parser::handle_tune}, + {NULL, NULL}}; + +static const struct riscv_attribute_info riscv_target_version_attrs[] + = {{"arch", &riscv_target_attr_parser::handle_arch}, + {"priority", &riscv_target_attr_parser::handle_priority}, + {NULL, NULL}}; bool riscv_target_attr_parser::parse_arch (const char *str) @@ -268,7 +274,8 @@ riscv_target_attr_parser::update_settings (struct gcc_options *opts) const static bool riscv_process_one_target_attr (char *arg_str, location_t loc, - riscv_target_attr_parser &attr_parser) + riscv_target_attr_parser &attr_parser, + const struct riscv_attribute_info *attrs) { size_t len = strlen (arg_str); @@ -295,15 +302,17 @@ riscv_process_one_target_attr (char *arg_str, arg[0] = '\0'; ++arg; - for (const auto &attr : riscv_attributes) + for (const auto *attr = attrs; + attr->name; + ++attr) { /* If the names don't match up, or the user has given an argument to an attribute that doesn't accept one, or didn't give an argument to an attribute that expects one, fail to match. */ - if (strncmp (str_to_check, attr.name, strlen (attr.name)) != 0) + if (strncmp (str_to_check, attr->name, strlen (attr->name)) != 0) continue; - return (&attr_parser->*attr.handler) (arg); + return (&attr_parser->*attr->handler) (arg); } error_at (loc, "Got unknown attribute %", str_to_check); @@ -332,7 +341,9 @@ num_occurrences_in_str (char c, char *str) and update the global target options space. */ bool -riscv_process_target_attr (const char *args, location_t loc) +riscv_process_target_attr (const char *args, + location_t loc, + const struct riscv_attribute_info *attrs) { size_t len = strlen (args); @@ -359,7 +370,7 @@ riscv_process_target_attr (const char *args, location_t loc) while (token) { num_attrs++; - if (!riscv_process_one_target_attr (token, loc, attr_parser)) + if (!riscv_process_one_target_attr (token, loc, attr_parser, attrs)) return false; token = strtok_r (NULL, ";", &str_to_check); @@ -382,7 +393,9 @@ riscv_process_target_attr (const char *args, location_t loc) and update the global target options space. */ static bool -riscv_process_target_attr (tree args, location_t loc) +riscv_process_target_attr (tree args, + location_t loc, + const struct riscv_attribute_info *attrs) { if (TREE_CODE (args) == TREE_LIST) { @@ -391,7 +404,7 @@ riscv_process_target_attr (tree args, location_t loc) tree head = TREE_VALUE (args); if (head) { - if (!riscv_process_target_attr (head, loc)) + if (!riscv_process_target_attr (head, loc, attrs)) return false; } args = TREE_CHAIN (args); @@ -406,7 +419,7 @@ riscv_process_target_attr (tree args, location_t loc) return false; } - return riscv_process_target_attr (TREE_STRING_POINTER (args), loc); + return riscv_process_target_attr (TREE_STRING_POINTER (args), loc, attrs); } /* Implement TARGET_OPTION_VALID_ATTRIBUTE_P. @@ -444,7 +457,7 @@ riscv_option_valid_attribute_p (tree fndecl, tree, tree args, int) TREE_TARGET_OPTION (target_option_default_node)); /* Now we can parse the attributes and set &global_options accordingly. */ - ret = riscv_process_target_attr (args, loc); + ret = riscv_process_target_attr (args, loc, riscv_target_attrs); if (ret) { riscv_override_options_internal (&global_options); @@ -457,3 +470,84 @@ riscv_option_valid_attribute_p (tree fndecl, tree, tree args, int) cl_target_option_restore (&global_options, &global_options_set, &cur_target); return ret; } + +/* Parse the tree in ARGS that contains the target_version attribute + information and update the global target options space. */ + +bool +riscv_process_target_version_attr (tree args, location_t loc) +{ + if (TREE_CODE (args) == TREE_LIST) + { + if (TREE_CHAIN (args)) + { + error ("attribute % has multiple values"); + return false; + } + args = TREE_VALUE (args); + } + + if (!args || TREE_CODE (args) != STRING_CST) + { + error ("attribute % argument not a string"); + return false; + } + + const char *str = TREE_STRING_POINTER (args); + if (strcmp (str, "default") == 0) + return true; + + return riscv_process_target_attr (str, loc, riscv_target_version_attrs); +} + + +/* Implement TARGET_OPTION_VALID_VERSION_ATTRIBUTE_P. This is used to + process attribute ((target_version ("..."))). */ + +bool +riscv_option_valid_version_attribute_p (tree fndecl, tree, tree args, int) +{ + struct cl_target_option cur_target; + bool ret; + tree new_target; + tree existing_target = DECL_FUNCTION_SPECIFIC_TARGET (fndecl); + location_t loc = DECL_SOURCE_LOCATION (fndecl); + + /* Save the current target options to restore at the end. */ + cl_target_option_save (&cur_target, &global_options, &global_options_set); + + /* If fndecl already has some target attributes applied to it, unpack + them so that we add this attribute on top of them, rather than + overwriting them. */ + if (existing_target) + { + struct cl_target_option *existing_options + = TREE_TARGET_OPTION (existing_target); + + if (existing_options) + cl_target_option_restore (&global_options, &global_options_set, + existing_options); + } + else + cl_target_option_restore (&global_options, &global_options_set, + TREE_TARGET_OPTION (target_option_current_node)); + + ret = riscv_process_target_version_attr (args, loc); + + /* Set up any additional state. */ + if (ret) + { + riscv_override_options_internal (&global_options); + new_target = build_target_option_node (&global_options, + &global_options_set); + } + else + new_target = NULL; + + if (fndecl && ret) + DECL_FUNCTION_SPECIFIC_TARGET (fndecl) = new_target; + + cl_target_option_restore (&global_options, &global_options_set, &cur_target); + + return ret; +} diff --git a/gcc/config/riscv/riscv.cc b/gcc/config/riscv/riscv.cc index 0b3b2c4cba9..23062a482c2 100644 --- a/gcc/config/riscv/riscv.cc +++ b/gcc/config/riscv/riscv.cc @@ -13060,6 +13060,10 @@ riscv_stack_clash_protection_alloca_probe_range (void) #undef TARGET_C_MODE_FOR_FLOATING_TYPE #define TARGET_C_MODE_FOR_FLOATING_TYPE riscv_c_mode_for_floating_type +#undef TARGET_OPTION_VALID_VERSION_ATTRIBUTE_P +#define TARGET_OPTION_VALID_VERSION_ATTRIBUTE_P \ + riscv_option_valid_version_attribute_p + struct gcc_target targetm = TARGET_INITIALIZER; #include "gt-riscv.h" diff --git a/gcc/config/riscv/riscv.h b/gcc/config/riscv/riscv.h index 2ff9c1024f3..8a8b08b6b51 100644 --- a/gcc/config/riscv/riscv.h +++ b/gcc/config/riscv/riscv.h @@ -1303,4 +1303,6 @@ extern void riscv_remove_unneeded_save_restore_calls (void); the target attribute. */ #define TARGET_CLONES_ATTR_SEPARATOR '#' +#define TARGET_HAS_FMV_TARGET_ATTRIBUTE 0 + #endif /* ! GCC_RISCV_H */