From: Jason Merrill <jason@redhat.com>
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 [thread overview]
Message-ID: <20230502202525.1964821-1-jason@redhat.com> (raw)
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 <variant>
+
+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<A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z>;
+
+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
next reply other threads:[~2023-05-02 20:25 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-05-02 20:25 Jason Merrill [this message]
2023-05-02 20:25 ` [PATCH 2/2] c++: look for empty base at specific offset [PR109678] Jason Merrill
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230502202525.1964821-1-jason@redhat.com \
--to=jason@redhat.com \
--cc=gcc-patches@gcc.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).