https://gcc.gnu.org/bugzilla/show_bug.cgi?id=122684

lightofmysoul at gmail dot com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |lightofmysoul at gmail dot com

--- Comment #2 from lightofmysoul at gmail dot com ---
Still present on trunk (17.0.0 20261130, r17-6836-g60a806f9c68), and on
16.2.1 20260926 and 15.3.0; all x86_64-linux-gnu.

Single-file reproducer, "gcc -O2 -flto t.c":

int fn(int argc, int (*args)[argc]);
int main() {
  return fn(0, (void *)0);
}
__attribute__((noinline)) int fn(int argc, int (*args)[argc]) {
  return args != 0;
}

Observations, the same on all three versions:
- IPA-CP is needed: no ICE with -fno-ipa-cp or -O1.
- No ICE with -flto-partition=none.
- Any constant pointer argument triggers it ((void *)0, C23 nullptr,
  (void *)256); a non-constant one does not.
- No ICE if the prototype uses "int (*args)[*]", if it is declared
  inside main, or, in the test above, if the definition comes before
  main.

What happens:

1. The PARM_DECLs of a file-scope prototype have no DECL_CONTEXT:
   pushdecl only sets it inside a function, store_parm_decls only for a
   definition.  The call converts the constant to the prototype's
   "int (*)[argc]", whose array bound refers to that PARM_DECL.

2. The streamer writes a decl without a context as if its context were
   the TRANSLATION_UNIT_DECL (DFS::DFS_write_tree_body in
   lto-streamer-out.cc, write_ts_decl_minimal_tree_pointers in
   tree-streamer-out.cc), so that is its DECL_CONTEXT in WPA.

3. In WPA, IPA-CP creates a specialized node of fn ("replacing param #1
   ... with const 0B"), and output_node_opt_summary streams that
   constant from the clone's tree_map.  The walk over its type reaches
   the PARM_DECL, for which tree_is_indexable does

     if ((TREE_CODE (t) == PARM_DECL || TREE_CODE (t) == RESULT_DECL)
         && DECL_CONTEXT (t))
       return lto_variably_modified_type_p (TREE_TYPE (DECL_CONTEXT (t)));

   A TRANSLATION_UNIT_DECL has no type, so lto_variably_modified_type_p
   is called with a null pointer.

In gdb on trunk, with the test from comment 0, debug_tree of t at the
crash (abridged):

 <parm_decl 0x7f0ef120daa0 argc
    type <integer_type 0x7f0ef12175e8 int ...>
    SI main.c:1:13 ...
    context <translation_unit_decl 0x7f0ef10a6000 main.c> ...>

Taking that branch only when the context is a FUNCTION_DECL fixes it
for me (diff with a testcase attached):

   if ((TREE_CODE (t) == PARM_DECL || TREE_CODE (t) == RESULT_DECL)
-      && DECL_CONTEXT (t))
+      && DECL_CONTEXT (t)
+      && TREE_CODE (DECL_CONTEXT (t)) == FUNCTION_DECL)

The PARM_DECL is then indexable, as it already is when the object file
is written, where its DECL_CONTEXT is still null.

Tested on trunk 1d124a757ef0 (20261204), x86_64-pc-linux-gnu,
--enable-languages=c,lto --disable-bootstrap:
- The tests above and variants of them (-O2/-O3/-Os, -g, several
  -flto-partition values, callee noinline or not) link and run
  correctly; 95 of 147 combinations ICE without the change.  IPA-CP
  still specializes fn: ltrans emits fn.constprop with the constants
  folded in.
- The attached gcc.dg/lto/pr122684 test goes from FAIL to PASS.
- make check-gcc RUNTESTFLAGS="lto.exp execute.exp compile.exp
  dg-torture.exp" has no unexpected failures (71382 passes).

Reply via email to