From patchwork Tue Nov 5 03:21:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yangyu Chen X-Patchwork-Id: 2006599 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=S40aQPfM; 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 4XjDCr5459z1xyD for ; Tue, 5 Nov 2024 14:22:16 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E84203857400 for ; Tue, 5 Nov 2024 03:22:14 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from out162-62-58-211.mail.qq.com (out162-62-58-211.mail.qq.com [162.62.58.211]) by sourceware.org (Postfix) with UTF8SMTPS id B46313857732 for ; Tue, 5 Nov 2024 03:21:48 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B46313857732 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 B46313857732 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=162.62.58.211 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1730776912; cv=none; b=Um5OiXB6UIaklDiraxQ8RveAkd8K0nmH+6rKTYp3R+/8nUC5jBIEDa+GLUocdiJHrqReMMBpZwzydoYYui8Y5RjGtzMJ5HXCQiAiWvyp6H5rUA65TE+yuAKROFtlTbv0s71vC/23HL0Dfk60RKd/iO0nuyw6dB8pk3oCMKPhnYs= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1730776912; c=relaxed/simple; bh=r55ndoMNrasKlbketuG29eEzBUTvpalpX6U8CWz4siM=; h=DKIM-Signature:Message-ID:From:To:Subject:Date:MIME-Version; b=C/e1OPyezAfMP0oDcPE18JsxLHGKls1XdOqPwLLztjlLc0D5EdVfE+1t1YeoYaEK7Hm23LnOH/gm3Ev45/J96NV7ensj4FObsU4s+B1tHK/SC11EvD5oV8pUEAHAY1QCzFRUegsFTV7zFmuqAlptmyqzkvGiavmnzz1Mepwsxh4= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=qq.com; s=s201512; t=1730776899; bh=Acy4MtSNNALWSkGe5uU1ESwq6o24N3J6VDLDq5OKiSk=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=S40aQPfMIK0lhc/cUs6EI7vDA2YNb9psMZqpiL8zrAUGBPihD7ufrEdgHEtvBEdWj jfi/T4+Q/+Tr/tc5rHHC4gFyV64Vo2YpBQV6s6piRy+K9yDDAvTrNOkR8MauNZbYCh jPxWg5ma5zPh5eEHD83hzhcIaxyk6TO4JaNqPBLw= Received: from cyy-pc.lan ([240e:379:2276:5d00:cda0:97a5:84f4:5191]) by newxmesmtplogicsvrszb20-0.qq.com (NewEsmtp) with SMTP id 5640C867; Tue, 05 Nov 2024 11:21:36 +0800 X-QQ-mid: xmsmtpt1730776896tkvc2ui8y Message-ID: X-QQ-XMAILINFO: MqG4KXyEKpQypsnPJ05m3Fqy6Kjt98JM8l0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: gcc-patches@gcc.gnu.org Cc: Kito Cheng , Palmer Dabbelt , Jiawei , Jeff Law , Evgeny Stupachenko , Andrew Carlotti , Hank Chang , Yangyu Chen Subject: [PATCH v5 1/8] Introduce TARGET_CLONES_ATTR_SEPARATOR for RISC-V Date: Tue, 5 Nov 2024 11:21:22 +0800 X-OQ-MSGID: <20241105032129.2645089-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 Some architectures may use ',' in the attribute string, but it is not used as the separator for different targets. To avoid conflict, we introduce a new macro TARGET_CLONES_ATTR_SEPARATOR to separate different clones. As an example, according to RISC-V C-API Specification [1], RISC-V allows ',' in the attribute string in the "arch=" option to specify one more ISA extensions in the same target function, which conflict with the default separator to separate different clones. This patch introduces TARGET_CLONES_ATTR_SEPARATOR for RISC-V and choose '#' as the separator, since '#' is not allowed in the target_clones option string. [1] https://github.com/riscv-non-isa/riscv-c-api-doc/blob/c6c5d6d9cf96b342293315a5dff3d25e96ef8191/src/c-api.adoc#__attribute__targetattr-string Signed-off-by: Yangyu Chen gcc/ChangeLog: * defaults.h (TARGET_CLONES_ATTR_SEPARATOR): Define new macro. * multiple_target.cc (get_attr_str): Use TARGET_CLONES_ATTR_SEPARATOR to separate attributes. (separate_attrs): Likewise. (expand_target_clones): Likewise. * attribs.cc (attr_strcmp): Likewise. (sorted_attr_string): Likewise. * tree.cc (get_target_clone_attr_len): Likewise. * config/riscv/riscv.h (TARGET_CLONES_ATTR_SEPARATOR): Define TARGET_CLONES_ATTR_SEPARATOR for RISC-V. * doc/tm.texi: Document TARGET_CLONES_ATTR_SEPARATOR. * doc/tm.texi.in: Likewise. --- gcc/attribs.cc | 17 ++++++++++------- gcc/config/riscv/riscv.h | 5 +++++ gcc/defaults.h | 4 ++++ gcc/doc/tm.texi | 7 +++++++ gcc/doc/tm.texi.in | 7 +++++++ gcc/multiple_target.cc | 19 ++++++++++++------- gcc/tree.cc | 4 +++- 7 files changed, 48 insertions(+), 15 deletions(-) diff --git a/gcc/attribs.cc b/gcc/attribs.cc index a290015fb47..1d6589835a1 100644 --- a/gcc/attribs.cc +++ b/gcc/attribs.cc @@ -1103,9 +1103,10 @@ attr_strcmp (const void *v1, const void *v2) } /* ARGLIST is the argument to target attribute. This function tokenizes - the comma separated arguments, sorts them and returns a string which - is a unique identifier for the comma separated arguments. It also - replaces non-identifier characters "=,-" with "_". */ + the TARGET_CLONES_ATTR_SEPARATOR separated arguments, sorts them and + returns a string which is a unique identifier for the + TARGET_CLONES_ATTR_SEPARATOR separated arguments. It also replaces + non-identifier characters "=,-" with "_". */ char * sorted_attr_string (tree arglist) @@ -1117,6 +1118,7 @@ sorted_attr_string (tree arglist) char *attr = NULL; unsigned int argnum = 1; unsigned int i; + static const char separator_str[] = { TARGET_CLONES_ATTR_SEPARATOR, 0 }; for (arg = arglist; arg; arg = TREE_CHAIN (arg)) { @@ -1126,7 +1128,7 @@ sorted_attr_string (tree arglist) if (arg != arglist) argnum++; for (i = 0; i < strlen (str); i++) - if (str[i] == ',') + if (str[i] == TARGET_CLONES_ATTR_SEPARATOR) argnum++; } @@ -1137,7 +1139,8 @@ sorted_attr_string (tree arglist) const char *str = TREE_STRING_POINTER (TREE_VALUE (arg)); size_t len = strlen (str); memcpy (attr_str + str_len_sum, str, len); - attr_str[str_len_sum + len] = TREE_CHAIN (arg) ? ',' : '\0'; + attr_str[str_len_sum + len] + = TREE_CHAIN (arg) ? TARGET_CLONES_ATTR_SEPARATOR : '\0'; str_len_sum += len + 1; } @@ -1152,12 +1155,12 @@ sorted_attr_string (tree arglist) args = XNEWVEC (char *, argnum); i = 0; - attr = strtok (attr_str, ","); + attr = strtok (attr_str, separator_str); while (attr != NULL) { args[i] = attr; i++; - attr = strtok (NULL, ","); + attr = strtok (NULL, separator_str); } qsort (args, argnum, sizeof (char *), attr_strcmp); diff --git a/gcc/config/riscv/riscv.h b/gcc/config/riscv/riscv.h index ca1b8329cdc..2ff9c1024f3 100644 --- a/gcc/config/riscv/riscv.h +++ b/gcc/config/riscv/riscv.h @@ -1298,4 +1298,9 @@ extern void riscv_remove_unneeded_save_restore_calls (void); STACK_BOUNDARY / BITS_PER_UNIT) \ : (crtl->outgoing_args_size + STACK_POINTER_OFFSET)) +/* According to the RISC-V C API, the arch string may contains ','. To avoid + the conflict with the default separator, we choose '#' as the separator for + the target attribute. */ +#define TARGET_CLONES_ATTR_SEPARATOR '#' + #endif /* ! GCC_RISCV_H */ diff --git a/gcc/defaults.h b/gcc/defaults.h index ac2d25852ab..918e3ec2f24 100644 --- a/gcc/defaults.h +++ b/gcc/defaults.h @@ -874,6 +874,10 @@ see the files COPYING3 and COPYING.RUNTIME respectively. If not, see #define TARGET_HAS_FMV_TARGET_ATTRIBUTE 1 #endif +/* Select a attribute separator for function multiversioning. */ +#ifndef TARGET_CLONES_ATTR_SEPARATOR +#define TARGET_CLONES_ATTR_SEPARATOR ',' +#endif /* Select a format to encode pointers in exception handling data. We prefer those that result in fewer dynamic relocations. Assume no diff --git a/gcc/doc/tm.texi b/gcc/doc/tm.texi index 4deb3d2c283..109e40384b6 100644 --- a/gcc/doc/tm.texi +++ b/gcc/doc/tm.texi @@ -10822,6 +10822,13 @@ the function declaration to hold a pointer to a target-specific @code{struct cl_target_option} structure. @end deftypefn +@defmac TARGET_CLONES_ATTR_SEPARATOR +Define this char-typed macro to select a character that separates each +target specific attributes from the @code{attribute(target_clones("..."))} +attribute string. This macro should be carefully chosen to avoid conflicts +with the target specific attributes. The default value is @code{','}. +@end defmac + @deftypefn {Target Hook} void TARGET_OPTION_SAVE (struct cl_target_option *@var{ptr}, struct gcc_options *@var{opts}, struct gcc_options *@var{opts_set}) This hook is called to save any additional target-specific information in the @code{struct cl_target_option} structure for function-specific diff --git a/gcc/doc/tm.texi.in b/gcc/doc/tm.texi.in index 9f147ccb95c..93bcd747e37 100644 --- a/gcc/doc/tm.texi.in +++ b/gcc/doc/tm.texi.in @@ -7101,6 +7101,13 @@ on this implementation detail. @hook TARGET_OPTION_VALID_VERSION_ATTRIBUTE_P +@defmac TARGET_CLONES_ATTR_SEPARATOR +Define this char-typed macro to select a character that separates each +target specific attributes from the @code{attribute(target_clones("..."))} +attribute string. This macro should be carefully chosen to avoid conflicts +with the target specific attributes. The default value is @code{','}. +@end defmac + @hook TARGET_OPTION_SAVE @hook TARGET_OPTION_RESTORE diff --git a/gcc/multiple_target.cc b/gcc/multiple_target.cc index d2c9671fc1b..0cceba68ce9 100644 --- a/gcc/multiple_target.cc +++ b/gcc/multiple_target.cc @@ -181,7 +181,7 @@ create_dispatcher_calls (struct cgraph_node *node) } } -/* Create string with attributes separated by comma. +/* Create string with attributes separated by TARGET_CLONES_ATTR_SEPARATOR. Return number of attributes. */ static int @@ -195,17 +195,21 @@ get_attr_str (tree arglist, char *attr_str) { const char *str = TREE_STRING_POINTER (TREE_VALUE (arg)); size_t len = strlen (str); - for (const char *p = strchr (str, ','); p; p = strchr (p + 1, ',')) + for (const char *p = strchr (str, TARGET_CLONES_ATTR_SEPARATOR); + p; + p = strchr (p + 1, TARGET_CLONES_ATTR_SEPARATOR)) argnum++; memcpy (attr_str + str_len_sum, str, len); - attr_str[str_len_sum + len] = TREE_CHAIN (arg) ? ',' : '\0'; + attr_str[str_len_sum + len] + = TREE_CHAIN (arg) ? TARGET_CLONES_ATTR_SEPARATOR : '\0'; str_len_sum += len + 1; argnum++; } return argnum; } -/* Return number of attributes separated by comma and put them into ARGS. +/* Return number of attributes separated by TARGET_CLONES_ATTR_SEPARATOR + and put them into ARGS. If there is no DEFAULT attribute return -1. If there is an empty string in attribute return -2. If there are multiple DEFAULT attributes return -3. @@ -216,9 +220,10 @@ separate_attrs (char *attr_str, char **attrs, int attrnum) { int i = 0; int default_count = 0; + static const char separator_str[] = { TARGET_CLONES_ATTR_SEPARATOR, 0 }; - for (char *attr = strtok (attr_str, ","); - attr != NULL; attr = strtok (NULL, ",")) + for (char *attr = strtok (attr_str, separator_str); + attr != NULL; attr = strtok (NULL, separator_str)) { if (strcmp (attr, "default") == 0) { @@ -306,7 +311,7 @@ static bool expand_target_clones (struct cgraph_node *node, bool definition) { int i; - /* Parsing target attributes separated by comma. */ + /* Parsing target attributes separated by TARGET_CLONES_ATTR_SEPARATOR. */ tree attr_target = lookup_attribute ("target_clones", DECL_ATTRIBUTES (node->decl)); /* No targets specified. */ diff --git a/gcc/tree.cc b/gcc/tree.cc index b4c059d3b0d..113a669128b 100644 --- a/gcc/tree.cc +++ b/gcc/tree.cc @@ -15229,7 +15229,9 @@ get_target_clone_attr_len (tree arglist) const char *str = TREE_STRING_POINTER (TREE_VALUE (arg)); size_t len = strlen (str); str_len_sum += len + 1; - for (const char *p = strchr (str, ','); p; p = strchr (p + 1, ',')) + for (const char *p = strchr (str, TARGET_CLONES_ATTR_SEPARATOR); + p; + p = strchr (p + 1, TARGET_CLONES_ATTR_SEPARATOR)) argnum++; argnum++; }