From patchwork Tue May 2 20:25:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 89443 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp880969vqo; Tue, 2 May 2023 13:37:57 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7tzAHJ+dyiSsABgeLgef8N+HWaAJ7hIkiJG0c5yr327aKagCMW0nNm3nk8LejVlos/9yHa X-Received: by 2002:aa7:df94:0:b0:4be:b39b:ea8f with SMTP id b20-20020aa7df94000000b004beb39bea8fmr10389543edy.2.1683059877754; Tue, 02 May 2023 13:37:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683059877; cv=none; d=google.com; s=arc-20160816; b=rSq4q6BiWRyOealecLP/e0vtCOgJd8ZiKPhoTzJ5DK5Fli/fXvoLyXMdyIp1147x47 Abvnf1RtmQzbRi6EAjjFFcc0rcp1QU1OS6ap3kWI6/K0eA2So8qoQ+fdzaUN/Jprim+9 mMn2Ks7FbNc+sDoTjZvV5t06p/+8fhYDvKgUYuc6x8twGxe6nfg3Vo27WzBYn8Z2TVqP IxElHygBQbm1r9vf2lU56yek7apUc8lJ8wJZNfZ61INLVWgVKSnE5rAYi6wWodrty9ve 0+ww1mB/ZXSpZoZ2xYBdsZ414AKvxwpaP9+VGFEMyrlhzEpSUOwb/5y1zos7PtmR9Axf ts5g== 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:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=OQ5RhG3bz76gvrJ9XBq+ocfTACzGBmhItO3UW8YvDLo=; b=sNzUH4UmZqnd2SpFC9TmgOANRntvyqgEtgTNVI3m5GJN6B3cQeaO/KmhiXTPc5vXjg 7StBeH7PHVDVKqLVUvMvKX45Ij5JSeJ8bN5KNdy7ovxr/ltnAdPn5V0SihfA8Pp7bzXl /BhNw4isPkwbjECMAeAt7diPC9IEV0cndSw0murykVzvu2nAVtBZC+qs8eEpEZ+Kl4cs P3bhiUpJ4lBBNELL7BjYUbzwP9USuHT1UQNMC2NtUxDv4Sz0yCpIAEnz4T8TvYSZLhq5 eL7bjbQrFtyUHzzqethEJD9xN0FPhzZOCsT5OihkWtQILmN02CXicwJTiDbmy35q0OL6 WmnQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=k0yTrA1c; 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 c7-20020aa7c747000000b0050bd4b8ca87si1377924eds.170.2023.05.02.13.37.57 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 May 2023 13:37:57 -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=k0yTrA1c; 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 0B168393BA4F for ; Tue, 2 May 2023 20:26:32 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0B168393BA4F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1683059192; bh=OQ5RhG3bz76gvrJ9XBq+ocfTACzGBmhItO3UW8YvDLo=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=k0yTrA1cdyeRNRIGA1NNFPFfPPloTKUcfLpm1WLQkowyyley5eBG5KO2K4unY6KXa Jhb/OjWFKK/Ffy0YMRSUml4LzLbk21dfUrKuKhh4KOpuj7kW94zZl+zHxD7A378y/G pW2xHxXhWvXnT2uq3cPP1QhBQ0sRKjGidNZdFFig= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id E87B83858C83 for ; Tue, 2 May 2023 20:25:36 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E87B83858C83 Received: from mail-qk1-f199.google.com (mail-qk1-f199.google.com [209.85.222.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-151-OLNxPPTYNgiZeUv_hU1lFw-1; Tue, 02 May 2023 16:25:35 -0400 X-MC-Unique: OLNxPPTYNgiZeUv_hU1lFw-1 Received: by mail-qk1-f199.google.com with SMTP id af79cd13be357-74df1fa3064so273963985a.1 for ; Tue, 02 May 2023 13:25:35 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683059134; x=1685651134; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=OQ5RhG3bz76gvrJ9XBq+ocfTACzGBmhItO3UW8YvDLo=; b=gv1bNK5W4zrwXunw7VVDvH23rs5hmqq61O/98nDnqmaS1MzLUqnAmu7t9bz1loLDmV toHsEp/7/94xCW65edI904nVoxchzLzUq1lbTq8RZoVDEXonvk48XIcYZrrcT+SHwGiw HYwKxPeHh6FR90Jbea5iNr/JOdPVhi3HIcM0pjQTKFvCJoP6ybI3/dSVD9k74KqCZPLl CpmJGLSeHzI1D5tE1dIjapjfvc17D0gcew7KzL5H/1M+EOMiRdSlim4ZUFtQqMLM8Is6 jgFXqPuN6YFODD5qsgB1whtj/UM9UzNTkTetE3ON6rdjo/MUIeqzD23Cc5q0CWpW4CN/ pJJw== X-Gm-Message-State: AC+VfDwACEGPPb08c8+dSZjgOUgaQttx5LYfkpvxxHRpl1O69o3UBa8v jTjCvFfkPXfwevRWRvcSUPJcQId7os9joxy6l5755TkXu7tFstTWT+QyP8A/d4p1ltrQm5ein7X 3nPpQxl4AjwhlxBd291Qe/t51Bm0Ia2t9di52wuF/3LXKPkpLkZGfvpQJ8TZy9t8beaPkX4y2nQ == X-Received: by 2002:a05:622a:f:b0:3e3:937b:2a64 with SMTP id x15-20020a05622a000f00b003e3937b2a64mr30344449qtw.18.1683059134641; Tue, 02 May 2023 13:25:34 -0700 (PDT) X-Received: by 2002:a05:622a:f:b0:3e3:937b:2a64 with SMTP id x15-20020a05622a000f00b003e3937b2a64mr30344388qtw.18.1683059134071; Tue, 02 May 2023 13:25:34 -0700 (PDT) Received: from jason.com (130-44-146-16.s12558.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.146.16]) by smtp.gmail.com with ESMTPSA id y11-20020ac8128b000000b003ef6582d971sm9060696qti.47.2023.05.02.13.25.33 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 May 2023 13:25:33 -0700 (PDT) To: gcc-patches@gcc.gnu.org Subject: [PATCH 1/2] c++: std::variant slow to compile [PR109678] Date: Tue, 2 May 2023 16:25:24 -0400 Message-Id: <20230502202525.1964821-1-jason@redhat.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.9 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, 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.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jason Merrill via Gcc-patches From: Jason Merrill Reply-To: Jason Merrill 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?1764816194338905406?= X-GMAIL-MSGID: =?utf-8?q?1764816194338905406?= Tested x86_64-pc-linux-gnu, applying to trunk. -- 8< -- Here, when dealing with a class with a complex subobject structure, we would try and fail to find the relevant FIELD_DECL for an empty base before giving up. And we would do this at each level, in a combinatorially problematic way. Instead, we should check for an empty base first. PR c++/109678 gcc/cp/ChangeLog: * constexpr.cc (cxx_fold_indirect_ref_1): Handle empty base first. gcc/testsuite/ChangeLog: * g++.dg/cpp1z/variant1.C: New test. --- gcc/cp/constexpr.cc | 23 +++++++------ gcc/testsuite/g++.dg/cpp1z/variant1.C | 47 +++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 10 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp1z/variant1.C base-commit: bc24c51c0ccd64617864897ad071c98004ffc0a4 diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc index d1097764b10..37d1c444c9e 100644 --- a/gcc/cp/constexpr.cc +++ b/gcc/cp/constexpr.cc @@ -5446,6 +5446,19 @@ cxx_fold_indirect_ref_1 (const constexpr_ctx *ctx, location_t loc, tree type, return ret; } } + + /* Handle conversion to an empty base class, which is represented with a + NOP_EXPR. Do this before spelunking into the non-empty subobjects, + which is likely to be a waste of time (109678). */ + if (is_empty_class (type) + && CLASS_TYPE_P (optype) + && DERIVED_FROM_P (type, optype)) + { + if (empty_base) + *empty_base = true; + return op; + } + for (tree field = TYPE_FIELDS (optype); field; field = DECL_CHAIN (field)) if (TREE_CODE (field) == FIELD_DECL @@ -5468,16 +5481,6 @@ cxx_fold_indirect_ref_1 (const constexpr_ctx *ctx, location_t loc, tree type, return ret; } } - /* Also handle conversion to an empty base class, which - is represented with a NOP_EXPR. */ - if (is_empty_class (type) - && CLASS_TYPE_P (optype) - && DERIVED_FROM_P (type, optype)) - { - if (empty_base) - *empty_base = true; - return op; - } } return NULL_TREE; diff --git a/gcc/testsuite/g++.dg/cpp1z/variant1.C b/gcc/testsuite/g++.dg/cpp1z/variant1.C new file mode 100644 index 00000000000..9b18cc233ca --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1z/variant1.C @@ -0,0 +1,47 @@ +// PR c++/109678 +// With the bug, compiling this testcase takes more than the typical timeout. +// { dg-do compile { target c++17 } } + +#include + +struct A {}; +struct B {}; +struct C {}; +struct D {}; +struct E {}; +struct F {}; +struct G {}; +struct H {}; +struct I {}; +struct J {}; +struct K {}; +struct L {}; +struct M {}; +struct N {}; +struct O {}; +struct P {}; +struct Q {}; +struct R {}; +struct S {}; +struct T {}; +struct U {}; +struct V {}; +struct W { + // gcc13 + compiler explorer = 20000ms + // gcc12.2 + compiler explorer = 400ms + int i; +}; +struct X {}; +struct Y {}; +struct Z {}; + +using Foo = std::variant; + +struct Bar { + Foo f; + static Bar dummy() { + // issue is triggered by this initialization + return {Z{}}; + // return {A{}}; // would be very quick + } +};