From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-qv1-xf2b.google.com (mail-qv1-xf2b.google.com [IPv6:2607:f8b0:4864:20::f2b]) by sourceware.org (Postfix) with ESMTPS id 3DDE83854171 for ; Fri, 21 Oct 2022 12:36:44 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 3DDE83854171 Authentication-Results: sourceware.org; dmarc=fail (p=none dis=none) header.from=acm.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-qv1-xf2b.google.com with SMTP id x15so1724485qvp.1 for ; Fri, 21 Oct 2022 05:36:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :sender:from:to:cc:subject:date:message-id:reply-to; bh=K1oYCjlRlCOrlQDRDkZ1WutkctNPZLoRzqO+juPxXhE=; b=HrRaYWJ/+lKslaJ0XMrlJ+KiyPvXW420yYRIkIkR2+D7nwVSbh8M7DEub7JOGroVQ+ +9c5NqUbp0s0TIYLqBWXe4C+DJdirx0s5+wJRF7YvrzebaKPh001ZF8j/DfaOqYm1fGk JvT2nBfJSjZ86JrbyBJOs0fozBl/u0Mbz+HZ2nm+HPY44cbz+S+4HQNxw2RnfA4t2gMo QazmvTCNoS5I2p8y2rcqtXyteQbHRdYMI0CdtskRi5it9EN+wZUyBd122j3bcpzBcfd9 2TS2xcg0IrVDUkflcmB/3DSIJ089empOsUuEPXV207HJikQ5iXqclW4vocsK8b+tYFBe 6sJg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :sender:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=K1oYCjlRlCOrlQDRDkZ1WutkctNPZLoRzqO+juPxXhE=; b=kTYWspFOt85p672OB4aofwnV526f/gV2E4ZiyM6xFRh8uNvIN3PD1MOwErAJ62TdG0 nblaGV0F4DGO3nsT/Z6EhmFg25hfkClbaedsYbmxZGO/ykzj6hivwIogGrdRNvTQYwgq nP3M/XpnPnmy9z/ToWnjKXV1jTONFPyBnNr+DCQf9mzlH6YDpmlDowRU53ZwQH5vWi8Z BYLX2LO3NbXMTsDNwryLJE/kMQsZdyurLNLrGRPtc7Jcf2o5ag97rIIeZnaNlB1nmx6Y 5xx88HYF4dOafXSr0WXbfnR4uqL6BGJuVAPTQ4GDgN0q5uWvaxyS5nyawdaAxL5gvVVt XDMA== X-Gm-Message-State: ACrzQf0xD346Xw7eKcj7oEI51/VahWQUz5Jn9it5Cia6Cih2CNRhZoHv Rv2K1+jhNeKupsx9TkwZzkg= X-Google-Smtp-Source: AMsMyM5yxLOaEMf6SbCYuYRQtEUcj7B2EPGHYp7lO661jGEc96dKSXyGG/LlHbacU9D9zOYmyw3L/g== X-Received: by 2002:ad4:5dc2:0:b0:4b1:94a0:a6de with SMTP id m2-20020ad45dc2000000b004b194a0a6demr16484596qvh.22.1666355802705; Fri, 21 Oct 2022 05:36:42 -0700 (PDT) Received: from ?IPV6:2601:19c:527f:bfd0::5? ([2601:19c:527f:bfd0::5]) by smtp.googlemail.com with ESMTPSA id f6-20020a05622a114600b0039d0366af44sm4473573qty.1.2022.10.21.05.36.42 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 21 Oct 2022 05:36:42 -0700 (PDT) Sender: Nathan Sidwell Message-ID: <2072c1af-9f73-3c9d-8c52-f0edf82e289f@acm.org> Date: Fri, 21 Oct 2022 08:36:41 -0400 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.3.1 Subject: Re: [PATCH] c++ modules: verify_type failure with typedef enum [PR106848] Content-Language: en-US To: Patrick Palka , Richard Biener Cc: gcc-patches@gcc.gnu.org References: <20221013153921.3795800-1-ppalka@redhat.com> <5cf07598-68ce-efda-05c3-625a1466195f@idea> <5601279b-939f-4492-c14b-c495d7a2a3b2@idea> From: Nathan Sidwell In-Reply-To: <5601279b-939f-4492-c14b-c495d7a2a3b2@idea> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-3038.9 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,GIT_PATCH_0,HEADER_FROM_DIFFERENT_DOMAINS,KAM_SHORT,NICE_REPLY_A,RCVD_IN_DNSWL_NONE,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 List-Id: On 10/19/22 09:55, Patrick Palka wrote: > On Wed, 19 Oct 2022, Richard Biener wrote: > >> On Tue, Oct 18, 2022 at 8:26 PM Patrick Palka wrote: >>> >>> On Fri, 14 Oct 2022, Richard Biener wrote: >>> >>>> On Thu, Oct 13, 2022 at 5:40 PM Patrick Palka via Gcc-patches >>>> wrote: >>>>> >>>>> Here during stream in we end up having created a type variant for the enum >>>>> before we read the enum's definition, and thus the variant inherited stale >>>>> TYPE_VALUES and TYPE_MIN/MAX_VALUES, which leads to an ICE (with -g). The >>>>> stale variant got created from set_underlying_type during earlier stream in >>>>> of the (redundant) typedef for the enum. >>>>> >>>>> This patch works around this by setting TYPE_VALUES and TYPE_MIN/MAX_VALUES >>>>> for all variants when reading in an enum definition. Does this look like >>>>> the right approach? Or perhaps we need to arrange that we read the enum >>>>> definition before reading in the typedef decl? Note that seems to be an >>>>> issue only when the typedef name and enum names are the same (thus the >>>>> typedef is redundant), otherwise we seem to read the enum definition first >>>>> as desired. >>>>> >>>>> PR c++/106848 >>>>> >>>>> gcc/cp/ChangeLog: >>>>> >>>>> * module.cc (trees_in::read_enum_def): Set the TYPE_VALUES, >>>>> TYPE_MIN_VALUE and TYPE_MAX_VALUE of all type variants. >>>>> >>>>> gcc/testsuite/ChangeLog: >>>>> >>>>> * g++.dg/modules/enum-9_a.H: New test. >>>>> * g++.dg/modules/enum-9_b.C: New test. >>>>> --- >>>>> gcc/cp/module.cc | 9 ++++++--- >>>>> gcc/testsuite/g++.dg/modules/enum-9_a.H | 5 +++++ >>>>> gcc/testsuite/g++.dg/modules/enum-9_b.C | 6 ++++++ >>>>> 3 files changed, 17 insertions(+), 3 deletions(-) >>>>> create mode 100644 gcc/testsuite/g++.dg/modules/enum-9_a.H >>>>> create mode 100644 gcc/testsuite/g++.dg/modules/enum-9_b.C >>>>> >>>>> diff --git a/gcc/cp/module.cc b/gcc/cp/module.cc >>>>> index 7ffeefa7c1f..97fb80bcd44 100644 >>>>> --- a/gcc/cp/module.cc >>>>> +++ b/gcc/cp/module.cc >>>>> @@ -12303,9 +12303,12 @@ trees_in::read_enum_def (tree defn, tree maybe_template) >>>>> >>>>> if (installing) >>>>> { >>>>> - TYPE_VALUES (type) = values; >>>>> - TYPE_MIN_VALUE (type) = min; >>>>> - TYPE_MAX_VALUE (type) = max; >>>>> + for (tree t = type; t; t = TYPE_NEXT_VARIANT (t)) >>>>> + { >>>>> + TYPE_VALUES (t) = values; >>>>> + TYPE_MIN_VALUE (t) = min; >>>>> + TYPE_MAX_VALUE (t) = max; >>>>> + } >>>> >>>> it's definitely somewhat ugly but at least type_hash_canon doesn't hash >>>> these for ENUMERAL_TYPE (but it does compare them! which in principle >>>> means it could as well hash them ...) >>>> >>>> I think that if you read both from the same module that you should arrange >>>> to read what you refer to first? But maybe that's not the actual issue here. >>> >>> *nod* reading in the enum before reading in the typedef seems like >>> the most direct solution, though not sure how to accomplish that :/ >> >> For LTO streaming we DFS walk tree edges from all entries into the tree >> graph we want to stream, collecting and streaming SCCs. Not sure if >> doing similar for module streaming would help this case though. > > FWIW I managed to obtain a more interesting reduction for this ICE, one > that doesn't use a typedef bound to the same name as the enum: > > $ cat 106848_a.H > template > struct pair { > using type = void(*)(const _T1&); > }; > struct _ScannerBase { > enum _TokenT { _S_token_anychar }; > pair<_TokenT> _M_token_tbl; > }; > > $ cat 106848_b.C > import "106848_a.H"; > > using type = _ScannerBase; > > $ g++ -fmodules-ts -g 106848_a.H 106848_b.C > 106848_b.C:3:14: error: type variant differs by TYPE_MAX_VALUE > > > > Like in the less interesting testcase, the problem is ultimately that we > create a variant of the enum (as part of reading in pair<_TokenT>::type) > before reading the enum's definition, thus the variant inherits stale > TYPE_MIN/MAX_VALUE. > > Perhaps pair<_TokenT>::type should indirectly depend on the definition > of _TokenT -- but IIUC we generally don't require a type to be defined > in order to refer to it, so enforcing such a dependency would be a > pessimization I think. > > So ISTM this isn't a dependency issue (pair<_TokenT>::type already > implicitly depends on the ENUMERAL_TYPE, just not also the enum's > defining TYPE_DECL), and the true issue is that we're streaming > TYPE_MIN/MAX_VALUE only as part of an enum's definition, which the > linked patch fixes. Thanks for the explanation, it's a situation I didn;t anticipate and your fix is good. Could you add a comment about why you need to propagate the values though? nathan > >> >>> A somewhat orthogonal issue (that incidentally fixes this testcase) is >>> that we stream TYPE_MIN/MAX_VALUE only for enums with a definition, but >>> the frontend sets these fields even for opaque enums. If we make sure >>> to stream these fields for all ENUMERAL_TYPEs, then we won't have to >>> worry about these fields being stale for variants that may have been >>> created before reading in the enum definition (their TYPE_VALUES field >>> will still be stale I guess, but verify_type doesn't worry about that >>> it seems, so we avoid the ICE). >>> >>> patch to that effect is at >>> https://gcc.gnu.org/pipermail/gcc-patches/2022-October/603831.html >>> >>>> >>>> Richard. >>>> >>>>> >>>>> rest_of_type_compilation (type, DECL_NAMESPACE_SCOPE_P (defn)); >>>>> } >>>>> diff --git a/gcc/testsuite/g++.dg/modules/enum-9_a.H b/gcc/testsuite/g++.dg/modules/enum-9_a.H >>>>> new file mode 100644 >>>>> index 00000000000..fb7d10ad3b6 >>>>> --- /dev/null >>>>> +++ b/gcc/testsuite/g++.dg/modules/enum-9_a.H >>>>> @@ -0,0 +1,5 @@ >>>>> +// PR c++/106848 >>>>> +// { dg-additional-options -fmodule-header } >>>>> +// { dg-module-cmi {} } >>>>> + >>>>> +typedef enum memory_order { memory_order_seq_cst } memory_order; >>>>> diff --git a/gcc/testsuite/g++.dg/modules/enum-9_b.C b/gcc/testsuite/g++.dg/modules/enum-9_b.C >>>>> new file mode 100644 >>>>> index 00000000000..63e81675d0a >>>>> --- /dev/null >>>>> +++ b/gcc/testsuite/g++.dg/modules/enum-9_b.C >>>>> @@ -0,0 +1,6 @@ >>>>> +// PR c++/106848 >>>>> +// { dg-additional-options "-fmodules-ts -g" } >>>>> + >>>>> +import "enum-9_a.H"; >>>>> + >>>>> +memory_order x = memory_order_seq_cst; >>>>> -- >>>>> 2.38.0.68.ge85701b4af >>>>> >>>> >>>> >>> >> >> > -- Nathan Sidwell