Bug 115591 - internal error on global variable-length array
Summary: internal error on global variable-length array
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: target (show other bugs)
Version: 14.1.0
: P3 normal
Target Milestone: 14.2
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2024-06-22 13:23 UTC by simon
Modified: 2024-07-06 15:20 UTC (History)
3 users (show)

See Also:
Host:
Target: riscv64-elf
Build:
Known to work:
Known to fail:
Last reconfirmed: 2024-06-23 00:00:00


Attachments
Reproducer (7.35 KB, application/zip)
2024-06-22 13:23 UTC, simon
Details

Note You need to log in before you can comment on or make changes to this bug.
Description simon 2024-06-22 13:23:41 UTC
Created attachment 58487 [details]
Reproducer

Compiler in use: gnat-riscv64-elf-darwin-14.1.0-3 from
https://github.com/alire-project/GNAT-FSF-builds/releases

Compiling the attached reproducer with riscv64-elf-gcc -c --RTS=. bug.adb results
in

during RTL pass: expand
+===========================GNAT BUG DETECTED==============================+
| 14.1.0 (riscv64-elf) in tree_to_uhwi, at tree.h:4951                     |
| Error detected around bug.adb:29:24                                      |
| Compiling bug.adb                                                        |
| Please submit a bug report; see https://gcc.gnu.org/bugs/ .              |
| Use a subject line meaningful to you and us to track the bug.            |
| Include the entire contents of this bug box in the report.               |
| Include the exact command that you entered.                              |
| Also include sources listed below.                                       |
+==========================================================================+

Please include these source files with error report
Note that list may not be accurate in some cases,
so please double check that the problem can still
be reproduced with the set of files listed.
Consider also -gnatd.n switch (see debug.adb).

system.ads
bug.adb
bug.ads
s-stoele.ads
s-stoele.adb
ada.ads
a-unccon.ads


raised TYPES.UNRECOVERABLE_ERROR : comperr.adb:412

Note that
* this error doesn’t happen with an arm-eabi cross compiler
* it was present from GCC 12.2.0
Comment 1 Eric Botcazou 2024-06-23 10:38:53 UTC
It's a buglet in the RISC-V back-end:

(gdb) bt
#0  internal_error (gmsgid=0x584aa67 "in %s, at %s:%d")
    at /home/eric/cvs/gcc/gcc/diagnostic.cc:2255
#1  0x0000000004ad30a2 in fancy_abort (
    file=0x4ea1be0 "/home/eric/cvs/gcc/gcc/tree.cc", line=6488, 
    function=0x4ea3cc1 "tree_to_uhwi")
    at /home/eric/cvs/gcc/gcc/diagnostic.cc:2380
#2  0x0000000002469386 in tree_to_uhwi (t=0x7ffff705aca8)
    at /home/eric/cvs/gcc/gcc/tree.cc:6488
#3  0x0000000002555b2e in riscv_valid_lo_sum_p (sym_type=SYMBOL_ABSOLUTE, 
    mode=E_BLKmode, x=0x7ffff717bb58)
    at /home/eric/cvs/gcc/gcc/config/riscv/riscv.cc:1706
Comment 2 Eric Botcazou 2024-06-23 10:47:32 UTC
The immediate fix is:

diff --git a/gcc/config/riscv/riscv.cc b/gcc/config/riscv/riscv.cc
index c17141d909a..5e34dc92210 100644
--- a/gcc/config/riscv/riscv.cc
+++ b/gcc/config/riscv/riscv.cc
@@ -1702,7 +1702,9 @@ riscv_valid_lo_sum_p (enum riscv_symbol_type sym_type, machine_mode mode,
       align = (SYMBOL_REF_DECL (x)
 	       ? DECL_ALIGN (SYMBOL_REF_DECL (x))
 	       : 1);
-      size = (SYMBOL_REF_DECL (x) && DECL_SIZE (SYMBOL_REF_DECL (x))
+      size = (SYMBOL_REF_DECL (x)
+	      && DECL_SIZE (SYMBOL_REF_DECL (x))
+	      && tree_fits_uhwi_p (DECL_SIZE (SYMBOL_REF_DECL (x)))
 	      ? tree_to_uhwi (DECL_SIZE (SYMBOL_REF_DECL (x)))
 	      : 2*BITS_PER_WORD);
     }

but I cannot really test it.
Comment 3 simon 2024-06-23 14:42:03 UTC
On 23 Jun 2024, at 11:47, ebotcazou at gcc dot gnu.org <gcc-bugzilla@gcc.gnu.org> wrote:
> 
> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115591
> 
> --- Comment #2 from Eric Botcazou <ebotcazou at gcc dot gnu.org> ---
> The immediate fix is:
> 
> diff --git a/gcc/config/riscv/riscv.cc b/gcc/config/riscv/riscv.cc
> index c17141d909a..5e34dc92210 100644
> --- a/gcc/config/riscv/riscv.cc
> +++ b/gcc/config/riscv/riscv.cc
> @@ -1702,7 +1702,9 @@ riscv_valid_lo_sum_p (enum riscv_symbol_type sym_type,
> machine_mode mode,
>       align = (SYMBOL_REF_DECL (x)
>               ? DECL_ALIGN (SYMBOL_REF_DECL (x))
>               : 1);
> -      size = (SYMBOL_REF_DECL (x) && DECL_SIZE (SYMBOL_REF_DECL (x))
> +      size = (SYMBOL_REF_DECL (x)
> +             && DECL_SIZE (SYMBOL_REF_DECL (x))
> +             && tree_fits_uhwi_p (DECL_SIZE (SYMBOL_REF_DECL (x)))
>              ? tree_to_uhwi (DECL_SIZE (SYMBOL_REF_DECL (x)))
>              : 2*BITS_PER_WORD);
>     }
> 
> but I cannot really test it.

Can I help with testing? (beyond applying the patch and checking that the error is fixed)
Comment 4 simon 2024-06-24 10:54:00 UTC
bug.adb compiles without error after applying the patch.
Comment 5 Eric Botcazou 2024-06-25 09:06:36 UTC
Jeff, this looks like an obvious fix given the comment just above the modified code, but only a maintainer can probably assess that.
Comment 6 Jeffrey A. Law 2024-07-02 16:56:39 UTC
Eric,
Just threw this into my tester.  Figure ~90 minutes to get back the cross results.

I assume that if we go forward that you'll handle putting together a regression test since it's Ada source?
Comment 7 Eric Botcazou 2024-07-02 17:15:00 UTC
> Just threw this into my tester.  Figure ~90 minutes to get back the cross
> results.

Thanks!

> I assume that if we go forward that you'll handle putting together a
> regression test since it's Ada source?

Yes, will do.
Comment 8 Jeffrey A. Law 2024-07-02 19:19:08 UTC
Passed without issue.  I'll go ahead and ACK the patch here.  It's good to go IMHO.
Comment 9 GCC Commits 2024-07-06 10:00:39 UTC
The master branch has been updated by Eric Botcazou <ebotcazou@gcc.gnu.org>:

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

commit r15-1870-g8bc5561c43b195e1638e5acace8b41b3f7512be3
Author: Eric Botcazou <ebotcazou@adacore.com>
Date:   Sat Jul 6 11:56:19 2024 +0200

    RISC-V: fix internal error on global variable-length array
    
    This is an ICE in the RISC-V back-end calling tree_to_uhwi on the DECL_SIZE
    of a global variable-length array.
    
    gcc/
            PR target/115591
            * config/riscv/riscv.cc (riscv_valid_lo_sum_p): Add missing test on
            tree_fits_uhwi_p before calling tree_to_uhwi.
    
    gcc/testsuite/
            * gnat.dg/array41.ads, gnat.dg/array41.adb: New test.
Comment 10 Eric Botcazou 2024-07-06 10:02:58 UTC
Thanks Jeff!  Any objection to backporting the fix onto the 14 branch?
Comment 11 Jeffrey A. Law 2024-07-06 13:37:06 UTC
No objections at all.  Go for it whenever it's convenient for you.
Comment 12 GCC Commits 2024-07-06 15:19:17 UTC
The releases/gcc-14 branch has been updated by Eric Botcazou <ebotcazou@gcc.gnu.org>:

https://gcc.gnu.org/g:6c5ef4944e3858e7daf1ef75a3afa93a3ece37c5

commit r14-10385-g6c5ef4944e3858e7daf1ef75a3afa93a3ece37c5
Author: Eric Botcazou <ebotcazou@adacore.com>
Date:   Sat Jul 6 11:56:19 2024 +0200

    RISC-V: fix internal error on global variable-length array
    
    This is an ICE in the RISC-V back-end calling tree_to_uhwi on the DECL_SIZE
    of a global variable-length array.
    
    gcc/
            PR target/115591
            * config/riscv/riscv.cc (riscv_valid_lo_sum_p): Add missing test on
            tree_fits_uhwi_p before calling tree_to_uhwi.
    
    gcc/testsuite/
            * gnat.dg/array41.ads, gnat.dg/array41.adb: New test.
Comment 13 Eric Botcazou 2024-07-06 15:20:11 UTC
Thanks!  Fixed on mainline and 14 branch.