From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1683059136; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=OQ5RhG3bz76gvrJ9XBq+ocfTACzGBmhItO3UW8YvDLo=; b=efXerworDFCaj25t7dDQ1Yy5i3KMTYvNMf1VLcIJePZaltqHcLLCNrfE766smPrIxSZMa7 XZgt/EbJ4gnPkFiz4tOR4eXRs65+rzgCC785lZ31TobTPSMbz7sTZtYiCRIQEAQ0pjyRhv U/uuLZcwBUSbdH6fTjJQiy1IW4trfI8= 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-Google-Smtp-Source: ACHHUZ5BsxF2+SPmBwDatekyV9pvHPlDonUblBIV2unwoV+coO1a0WjGqCZ6tDHq5tELgXpbjvlydQ== 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) From: Jason Merrill 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 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true 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 List-Id: 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 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 + } +}; base-commit: bc24c51c0ccd64617864897ad071c98004ffc0a4 -- 2.31.1