Bug 112869 - [14 Regression] ICE at gimplify_expr, at gimplify.cc:17531 on libopenmpt-0.7.3
Summary: [14 Regression] ICE at gimplify_expr, at gimplify.cc:17531 on libopenmpt-0.7.3
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 14.0
: P1 normal
Target Milestone: 14.0
Assignee: Marek Polacek
URL:
Keywords: ice-on-valid-code
: 112907 (view as bug list)
Depends on:
Blocks:
 
Reported: 2023-12-05 18:30 UTC by Sergei Trofimovich
Modified: 2023-12-14 21:06 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2023-12-05 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Sergei Trofimovich 2023-12-05 18:30:03 UTC
Initially observed ICE on gcc-master at r14-6191-g9c3a880feecf81 compiling libopenmpt-0.7.3.

Extracted example:

$ cat a.cpp.cpp
void min(long, long);
template <class T> void Binaryread(int &, T, unsigned long);
template <> void Binaryread(int &, float, unsigned long bytecount) {
  min(bytecount, sizeof(int));
}

Crashing:

$ g++ -std=c++20 -c a.cpp.cpp -o a.o

a.cpp.cpp: In function 'void Binaryread(int&, T, long unsigned int) [with T = float]':
a.cpp.cpp:4:18: internal compiler error: in gimplify_expr, at gimplify.cc:17531
    4 |   min(bytecount, sizeof(int));
      |                  ^~~~~~~~~~~
0x1fe24d4 diagnostic_impl(rich_location*, diagnostic_metadata const*, int, char const*, __va_list_tag (*) [1], diagnostic_t)
        ???:0
0x1fe2cb9 internal_error(char const*, ...)
        ???:0
0x76d566 fancy_abort(char const*, int, char const*)
        ???:0
0x7387b4 gimplify_expr(tree_node**, gimple**, gimple**, bool (*)(tree_node*), int) [clone .cold]
        ???:0
0xc30cb6 gimplify_expr(tree_node**, gimple**, gimple**, bool (*)(tree_node*), int)
        ???:0
0xc35c39 gimplify_arg(tree_node**, gimple**, unsigned int, bool)
        ???:0
0xc35e9d gimplify_call_expr(tree_node**, gimple**, bool)
        ???:0
0xc3158e gimplify_expr(tree_node**, gimple**, gimple**, bool (*)(tree_node*), int)
        ???:0
0xc34b33 gimplify_cleanup_point_expr(tree_node**, gimple**)
        ???:0
0xc315e2 gimplify_expr(tree_node**, gimple**, gimple**, bool (*)(tree_node*), int)
        ???:0
0xc3428e gimplify_body(tree_node*, bool)
        ???:0
0xc34650 gimplify_function_tree(tree_node*)
        ???:0
0xa9bf07 cgraph_node::analyze()
        ???:0
0xa9e607 analyze_functions(bool)
        ???:0
0xa9f27d symbol_table::finalize_compilation_unit()
        ???:0

$ LANG=C gcc/xg++ -Bgcc -v
Reading specs from gcc/specs
COLLECT_GCC=gcc/xg++
COLLECT_LTO_WRAPPER=gcc/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: /home/slyfox/dev/git/gcc/configure --disable-multilib --disable-bootstrap --disable-lto --disable-libsanitizer --disable-libstdcxx-pch --enable-languages=c,c++ --disable-libgomp --disable-libquadmath --disable-libvtv CFLAGS='-O1 -g0' CXXFLAGS='-O1 -g0' LDFLAGS='-O1 -g0'
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 14.0.0 20231205 (experimental) (GCC)
Comment 1 Marek Polacek 2023-12-05 18:34:50 UTC
commit 1f1c432226cf3db399b2a2a627e3c5720b02b1d6
Author: Marek Polacek <polacek@redhat.com>
Date:   Tue Sep 19 16:31:17 2023 -0400

    c++: implement P2564, consteval needs to propagate up [PR107687]
Comment 2 Marek Polacek 2023-12-05 19:01:19 UTC
This should fix it:

--- a/gcc/cp/cp-gimplify.cc
+++ b/gcc/cp/cp-gimplify.cc
@@ -1177,13 +1177,9 @@ cp_fold_immediate_r (tree *stmt_p, int *walk_subtrees, void *data_)
                   ? tf_error : tf_none);
   const tree_code code = TREE_CODE (stmt);
 
-  /* No need to look into types or unevaluated operands.
-     NB: This affects cp_fold_r as well.  */
+  /* No need to look into types or unevaluated operands.  */
   if (TYPE_P (stmt) || unevaluated_p (code) || in_immediate_context ())
-    {
-      *walk_subtrees = 0;
-      return NULL_TREE;
-    }
+    return NULL_TREE;
 
   tree decl = NULL_TREE;
   bool call_p = false;
Comment 3 Sergei Trofimovich 2023-12-06 20:50:43 UTC
I confirm the proposed change fixes build of libopenmpt-0.7.3 as well. Thank you!
Comment 4 Andrew Pinski 2023-12-07 18:34:07 UTC
*** Bug 112907 has been marked as a duplicate of this bug. ***
Comment 5 David Binderman 2023-12-08 17:16:30 UTC
I see this one also, in a build of tlx-0.6.1 package.
Comment 6 GCC Commits 2023-12-14 21:06:03 UTC
The trunk branch has been updated by Marek Polacek <mpolacek@gcc.gnu.org>:

https://gcc.gnu.org/g:8cccdc2176740f3e034ee6caa49552cf2f142744

commit r14-6561-g8cccdc2176740f3e034ee6caa49552cf2f142744
Author: Marek Polacek <polacek@redhat.com>
Date:   Tue Dec 5 15:23:52 2023 -0500

    c++: fix ICE with sizeof in a template [PR112869]
    
    This test shows that we cannot clear *walk_subtrees in
    cp_fold_immediate_r when we're in_immediate_context, because that
    checks even e.g. sk_template_parms, and, as the comment says, affects
    cp_fold_r as well.  Here we had an expression with
    
      min ((long int) VIEW_CONVERT_EXPR<long unsigned int>(bytecount), (long int) <<< Unknown tree: sizeof_expr
        (int) <<< error >>> >>>)
    
    as its sub-expression, and we never evaluated that into
    
      min ((long int) bytecount, 4)
    
    so the SIZEOF_EXPR leaked into the middle end.  We need to make sure
    we are calling cp_fold on the SIZEOF_EXPR.
    
            PR c++/112869
    
    gcc/cp/ChangeLog:
    
            * cp-gimplify.cc (cp_fold_immediate_r): Check cp_unevaluated_operand
            and DECL_IMMEDIATE_FUNCTION_P rather than in_immediate_context.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/template/sizeof18.C: New test.
Comment 7 Marek Polacek 2023-12-14 21:06:41 UTC
Fixed.