From patchwork Thu Oct 6 14:34:00 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Iannetta X-Patchwork-Id: 1783 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4ac7:0:0:0:0:0 with SMTP id y7csp347106wrs; Thu, 6 Oct 2022 07:35:20 -0700 (PDT) X-Google-Smtp-Source: AMsMyM58TOUSptFF90ffAOak1JrgFtV74mH6kjUCjy8SY31oAiw7/9884iLmtVTD+ObS3ImxtPeu X-Received: by 2002:a17:907:1b1f:b0:72f:56db:cce9 with SMTP id mp31-20020a1709071b1f00b0072f56dbcce9mr86102ejc.605.1665066920331; Thu, 06 Oct 2022 07:35:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1665066920; cv=none; d=google.com; s=arc-20160816; b=qLrY6J1h9d95M4erU0EfHUoHI4ljWop2rzmJGgSPz0piWnOEBQzA11KjKBRfHtsEKl B5fsdzeFxBj8jZ1gntHEILU8CZZ4UX5gwWwcqQmj/iOr+541isDWQSTqD7z1a/e9/ozp cPy/ITJcStSJ5Z+yKFPU+O5K+mC5Gr03AwAzBQDKDakNu8vCMVeOSUjb8lwTT0OZ123w 0KofeqQtCOGgrKPvrbBIVKqo1b3ncT0fF/T0MPF6bJfEk0edOtmUVG8q1UzWwB5pMLWL uk3UKTBjuA5BXaLcwlhlB73CVGs7OJ+1o63tFeNctpass+2FJhQlHq6WE15EI3HtcEYa fRWA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:user-agent:content-disposition :mime-version:message-id:subject:to:date:dkim-filter:secumail-id :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=H1GqmfhGP4yhIfBh+ZCWhmM7RAqrrtroVN6JT3IvUn4=; b=TPXueE3D2L0HQ2iOCt8//3rjMdlaLDH4k2OPqqPMiynHscQJEkPmyuN7feJO6HlaUd vnA0RC8VfH4wPev5yuxfSCTDpkJN26H8Hj7El9dHfThQmJWE1h5OIsfcRuQC1HmEfINR Z+lNnUKYadGR36gUNylYpHmJt2MR8gw778hCL9rAixmxhcVMjNg4DISxtrjhrSGr59U6 BIv0dPg6xe1HpeKJpFo6Bk/Zyp4RKEZ63Nf+U+jOU3QRmJ3v8c33pJuQ1psiM5HAmMkY lskrKxXVq29tIj1esr/ozf3/qQe5mOEXluxZYMkIN0ToNw8R1xBADxV1xyL62Vn674gv f6Cw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=cGk05PBR; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id x9-20020a05640226c900b00456fa16e075si17823239edd.457.2022.10.06.07.35.20 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 06 Oct 2022 07:35:20 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=cGk05PBR; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8CAA83898380 for ; Thu, 6 Oct 2022 14:35:18 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8CAA83898380 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1665066918; bh=H1GqmfhGP4yhIfBh+ZCWhmM7RAqrrtroVN6JT3IvUn4=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=cGk05PBRQWMoM7ID9Z7i5R/VDkAkFW75ZIRkY9x76tCda4w23mPvClPc4maqm+fK/ Fmk3heXQrttN4y9qJCa+8gIKXim1H6UOYhoD/1Y2f13+BDYj8h5Pcjr4eu0z0FKz31 95QsSCWJIGWqk4uAVm62SSnOavjXTX8xBSPujb+A= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from fx305.security-mail.net (smtpout30.security-mail.net [85.31.212.35]) by sourceware.org (Postfix) with ESMTPS id 270AC3898385 for ; Thu, 6 Oct 2022 14:34:05 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 270AC3898385 Received: from localhost (fx305.security-mail.net [127.0.0.1]) by fx305.security-mail.net (Postfix) with ESMTP id 1EE4030FFE1 for ; Thu, 6 Oct 2022 16:34:04 +0200 (CEST) Received: from fx305 (fx305.security-mail.net [127.0.0.1]) by fx305.security-mail.net (Postfix) with ESMTP id E66EE30FFE2; Thu, 6 Oct 2022 16:34:03 +0200 (CEST) Received: from zimbra2.kalray.eu (unknown [217.181.231.53]) by fx305.security-mail.net (Postfix) with ESMTPS id B6DAA30FFE1; Thu, 6 Oct 2022 16:34:01 +0200 (CEST) Received: from zimbra2.kalray.eu (localhost [127.0.0.1]) by zimbra2.kalray.eu (Postfix) with ESMTPS id 8F4D027E032A; Thu, 6 Oct 2022 16:34:01 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by zimbra2.kalray.eu (Postfix) with ESMTP id 7695527E0444; Thu, 6 Oct 2022 16:34:01 +0200 (CEST) Received: from zimbra2.kalray.eu ([127.0.0.1]) by localhost (zimbra2.kalray.eu [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id f1WhR5nyhXFE; Thu, 6 Oct 2022 16:34:01 +0200 (CEST) Received: from localhost (unknown [192.168.37.51]) by zimbra2.kalray.eu (Postfix) with ESMTPSA id 5A7C027E032A; Thu, 6 Oct 2022 16:34:01 +0200 (CEST) X-Virus-Scanned: E-securemail, by Secumail Secumail-id: DKIM-Filter: OpenDKIM Filter v2.10.3 zimbra2.kalray.eu 7695527E0444 Date: Thu, 6 Oct 2022 16:34:00 +0200 To: gcc-patches@gcc.gnu.org Subject: [RFC] c++: parser - Support for target address spaces in C++ Message-ID: <20221006143400.es3u6ebqt3xkw6jp@ws2202.lin.mbt.kalray.eu> MIME-Version: 1.0 Content-Disposition: inline User-Agent: NeoMutt/20171215 X-ALTERMIMEV2_out: done X-Spam-Status: No, score=-11.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, GIT_PATCH_0, HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_LOW, SPF_HELO_NONE, SPF_PASS, TXREP 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.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Paul Iannetta via Gcc-patches From: Paul Iannetta Reply-To: Paul Iannetta Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1745949210940993871?= X-GMAIL-MSGID: =?utf-8?q?1745949210940993871?= Hi, Presently, GCC supports target address spaces as a GNU extension (cf. `info -n "(gcc)Named Address Spaces"`). This is however supported only by the C frontend, which is a bit sad, since all the GIMPLE machinery is readily available and, moreover, LLVM supports this GNU extension both for C and C++. Here is a first attempt at a patch to enable the support for target address spaces in C++. The basic idea is only to make the parser recognize address spaces, and lower them into GIMPLE, in the same fashion as the C parser. This also makes it possible to merge the function `c_register_addr_space` in one place which is better than before. The patch still has some drawbacks compared to its C counterpart. Namely, much like the `__restrict__` keyword, it is always enabled and -std=c++11 won't disable it (whereas -std=c11) would reject address spaces. Not also that the mangler ignores address spaces, much like it ignores __restrict__. Depending on the reception, I'll add further testcases and will update the relevant part of the documentation. Cheers, Paul Author: Paul Iannetta Date: Wed Oct 5 16:44:36 2022 +0200 Add support for custom address spaces in C++ gcc/ * tree.h (ENCODE_QUAL_ADDR_SPACE): Missing parentheses. gcc/c/ * c-decl.c: Remove c_register_addr_space. gcc/c-family/ * c-common.c (c_register_addr_space): Imported from c-decl.c * c-common.h: Remove the FIXME. gcc/cp/ * cp-tree.h (enum cp_decl_spec): Add addr_space support. (struct cp_decl_specifier_seq): Likewise. * decl.c (get_type_quals): Likewise. * parser.c (cp_parser_type_specifier): Likewise. (cp_parser_cv_qualifier_seq_opt): Likewise * tree.c: Remove c_register_addr_space stub. Signed-off-by: Paul Iannetta diff --git a/gcc/c-family/c-common.c b/gcc/c-family/c-common.c index 064c2f263f0..282ba54ab70 100644 --- a/gcc/c-family/c-common.c +++ b/gcc/c-family/c-common.c @@ -2615,6 +2615,25 @@ c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp) return build_nonstandard_integer_type (width, unsignedp); } +/* Register reserved keyword WORD as qualifier for address space AS. */ + +void +c_register_addr_space (const char *word, addr_space_t as) +{ + int rid = RID_FIRST_ADDR_SPACE + as; + tree id; + + /* Address space qualifiers are only supported + in C with GNU extensions enabled. */ + if (c_dialect_objc () || flag_no_asm) + return; + + id = get_identifier (word); + C_SET_RID_CODE (id, rid); + TREE_LANG_FLAG_0 (id) = 1; + ridpointers[rid] = id; +} + /* The C version of the register_builtin_type langhook. */ void diff --git a/gcc/c-family/c-common.h b/gcc/c-family/c-common.h index ed39b7764bf..f2c1df0c8de 100644 --- a/gcc/c-family/c-common.h +++ b/gcc/c-family/c-common.h @@ -808,9 +808,6 @@ extern const struct attribute_spec c_common_format_attribute_table[]; extern tree (*make_fname_decl) (location_t, tree, int); -/* In c-decl.c and cp/tree.c. FIXME. */ -extern void c_register_addr_space (const char *str, addr_space_t as); - /* In c-common.c. */ extern bool in_late_binary_op; extern const char *c_addr_space_name (addr_space_t as); @@ -926,6 +923,7 @@ extern void c_common_finish (void); extern void c_common_parse_file (void); extern FILE *get_dump_info (int, dump_flags_t *); extern alias_set_type c_common_get_alias_set (tree); +extern void c_register_addr_space (const char *, addr_space_t); extern void c_register_builtin_type (tree, const char*); extern bool c_promoting_integer_type_p (const_tree); extern bool self_promoting_args_p (const_tree); diff --git a/gcc/c/c-decl.c b/gcc/c/c-decl.c index 8e24b522ee4..278d1248d1c 100644 --- a/gcc/c/c-decl.c +++ b/gcc/c/c-decl.c @@ -11927,25 +11927,6 @@ c_parse_final_cleanups (void) ext_block = NULL; } -/* Register reserved keyword WORD as qualifier for address space AS. */ - -void -c_register_addr_space (const char *word, addr_space_t as) -{ - int rid = RID_FIRST_ADDR_SPACE + as; - tree id; - - /* Address space qualifiers are only supported - in C with GNU extensions enabled. */ - if (c_dialect_objc () || flag_no_asm) - return; - - id = get_identifier (word); - C_SET_RID_CODE (id, rid); - C_IS_RESERVED_WORD (id) = 1; - ridpointers [rid] = id; -} - /* Return identifier to look up for omp declare reduction. */ tree diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index 15ec4cd199f..4ae971ccb90 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -5974,6 +5974,7 @@ enum cp_decl_spec { ds_const, ds_volatile, ds_restrict, + ds_addr_space, ds_inline, ds_virtual, ds_explicit, @@ -6046,6 +6047,8 @@ struct cp_decl_specifier_seq { /* True iff the alternate "__intN__" form of the __intN type has been used. */ BOOL_BITFIELD int_n_alt: 1; + /* The address space that the declaration belongs to. */ + addr_space_t address_space; }; /* The various kinds of declarators. */ diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c index 03cd0a3a238..b8fc8b58ac7 100644 --- a/gcc/cp/decl.c +++ b/gcc/cp/decl.c @@ -4954,6 +4954,8 @@ get_type_quals (const cp_decl_specifier_seq *declspecs) type_quals |= TYPE_QUAL_VOLATILE; if (decl_spec_seq_has_spec_p (declspecs, ds_restrict)) type_quals |= TYPE_QUAL_RESTRICT; + if (decl_spec_seq_has_spec_p (declspecs, ds_addr_space)) + type_quals |= ENCODE_QUAL_ADDR_SPACE (declspecs->address_space); return type_quals; } diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c index f48c856fa94..8d6b2a44086 100644 --- a/gcc/cp/parser.c +++ b/gcc/cp/parser.c @@ -17796,6 +17796,16 @@ cp_parser_type_specifier (cp_parser* parser, break; } + + if (RID_FIRST_ADDR_SPACE <= keyword && keyword <= RID_LAST_ADDR_SPACE) + { + ds = ds_addr_space; + if (is_cv_qualifier) + *is_cv_qualifier = true; + decl_specs->address_space = keyword - RID_FIRST_ADDR_SPACE; + } + + /* Handle simple keywords. */ if (ds != ds_last) { @@ -21853,6 +21863,7 @@ cp_parser_ptr_operator (cp_parser* parser, GNU Extension: cv-qualifier: + address-space-qualifier __restrict__ Returns a bitmask representing the cv-qualifiers. */ @@ -21889,6 +21900,13 @@ cp_parser_cv_qualifier_seq_opt (cp_parser* parser) break; } + if (RID_FIRST_ADDR_SPACE <= token->keyword && + token->keyword <= RID_LAST_ADDR_SPACE) + { + cv_qualifier = + ENCODE_QUAL_ADDR_SPACE (token->keyword - RID_FIRST_ADDR_SPACE); + } + if (!cv_qualifier) break; diff --git a/gcc/cp/tree.c b/gcc/cp/tree.c index 10b818d1370..55b57085618 100644 --- a/gcc/cp/tree.c +++ b/gcc/cp/tree.c @@ -5749,15 +5749,6 @@ cp_free_lang_data (tree t) DECL_CHAIN (t) = NULL_TREE; } -/* Stub for c-common. Please keep in sync with c-decl.c. - FIXME: If address space support is target specific, then this - should be a C target hook. But currently this is not possible, - because this function is called via REGISTER_TARGET_PRAGMAS. */ -void -c_register_addr_space (const char * /*word*/, addr_space_t /*as*/) -{ -} - /* Return the number of operands in T that we care about for things like mangling. */ diff --git a/gcc/tree.h b/gcc/tree.h index bb80e81d389..210ef7b85d7 100644 --- a/gcc/tree.h +++ b/gcc/tree.h @@ -2108,7 +2108,7 @@ extern machine_mode vector_type_mode (const_tree); /* Encode/decode the named memory support as part of the qualifier. If more than 8 qualifiers are added, these macros need to be adjusted. */ -#define ENCODE_QUAL_ADDR_SPACE(NUM) ((NUM & 0xFF) << 8) +#define ENCODE_QUAL_ADDR_SPACE(NUM) (((NUM) & 0xFF) << 8) #define DECODE_QUAL_ADDR_SPACE(X) (((X) >> 8) & 0xFF) /* Return all qualifiers except for the address space qualifiers. */