Bug 115277 - [13 regression] ICF needs to match loop bound estimates
Summary: [13 regression] ICF needs to match loop bound estimates
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: ipa (show other bugs)
Version: 14.0
: P3 normal
Target Milestone: 13.4
Assignee: Jan Hubicka
URL:
Keywords: wrong-code
Depends on:
Blocks: icf
  Show dependency treegraph
 
Reported: 2024-05-29 14:11 UTC by Jan Hubicka
Modified: 2025-02-25 01:32 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work: 14.1.1, 15.0
Known to fail:
Last reconfirmed: 2024-07-22 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jan Hubicka 2024-05-29 14:11:29 UTC
jan@localhost:/tmp> cat tt.c
int array[1000];
void
test (int a)
{
        if (__builtin_expect (a > 3, 1))
                return;
        for (int i = 0; i < a; i++)
                array[i]=i;
}
void
test2 (int a)
{
        if (__builtin_expect (a > 10, 1))
                return;
        for (int i = 0; i < a; i++)
                array[i]=i;
}
int
main()
{
        test(1);
        test(2);
        test(3);
        test2(10);
        if (array[9] != 9)
                __builtin_abort ();
        return 0;
}
jan@localhost:/tmp> gcc -O2 tt.c ; ./a.out
jan@localhost:/tmp> gcc -O3 tt.c ; ./a.out
Aborted (core dumped)


The problem here is that we do not match value ranges and thus we can end up with different estimates on number of iterations.
Comment 1 Jan Hubicka 2024-05-29 14:16:55 UTC
Reproduces on 14 and trunk. GCC 12 is not able to determine the loop bound during early optimizations
Comment 2 Martin Jambor 2024-05-30 12:53:53 UTC
(In reply to Jan Hubicka from comment #1)
> Reproduces on 14 and trunk. GCC 12 is not able to determine the loop bound
> during early optimizations

What about gcc 13?
Comment 3 Jan Hubicka 2024-05-30 20:36:54 UTC
> What about gcc 13?
GCC 13 also misoptimizes.
Honza
Comment 4 GCC Commits 2024-07-22 16:02:22 UTC
The master branch has been updated by Jan Hubicka <hubicka@gcc.gnu.org>:

https://gcc.gnu.org/g:0d19fbc7b0760ce665fa6a88cd40cfa0311358d7

commit r15-2200-g0d19fbc7b0760ce665fa6a88cd40cfa0311358d7
Author: Jan Hubicka <hubicka@ucw.cz>
Date:   Mon Jul 22 18:01:57 2024 +0200

    Compare loop bounds in ipa-icf
    
    Hi,
    this testcase shows another poblem with missing comparators for metadata
    in ICF. With value ranges available to loop optimizations during early
    opts we can estimate number of iterations based on guarding condition that
    can be split away by the fnsplit pass. This patch disables ICF when
    number of iteraitons does not match.
    
    Bootstrapped/regtesed x86_64-linux, will commit it shortly
    
    gcc/ChangeLog:
    
            PR ipa/115277
            * ipa-icf-gimple.cc (func_checker::compare_loops): compare loop
            bounds.
    
    gcc/testsuite/ChangeLog:
    
            * gcc.c-torture/compile/pr115277.c: New test.
Comment 5 Sam James 2024-07-22 16:10:54 UTC
honza, should that be in execute/ instead?
Comment 6 GCC Commits 2024-07-22 16:55:47 UTC
The releases/gcc-14 branch has been updated by Jan Hubicka <hubicka@gcc.gnu.org>:

https://gcc.gnu.org/g:c5397d343ff1365fcebcf3ebabe140608874aac3

commit r14-10487-gc5397d343ff1365fcebcf3ebabe140608874aac3
Author: Jan Hubicka <hubicka@ucw.cz>
Date:   Mon Jul 22 18:01:57 2024 +0200

    Compare loop bounds in ipa-icf
    
    Hi,
    this testcase shows another poblem with missing comparators for metadata
    in ICF. With value ranges available to loop optimizations during early
    opts we can estimate number of iterations based on guarding condition that
    can be split away by the fnsplit pass. This patch disables ICF when
    number of iteraitons does not match.
    
    Bootstrapped/regtesed x86_64-linux, will commit it shortly
    
    gcc/ChangeLog:
    
            PR ipa/115277
            * ipa-icf-gimple.cc (func_checker::compare_loops): compare loop
            bounds.
    
    gcc/testsuite/ChangeLog:
    
            * gcc.c-torture/compile/pr115277.c: New test.
    
    (cherry picked from commit 0d19fbc7b0760ce665fa6a88cd40cfa0311358d7)
Comment 7 Jan Hubicka 2024-07-22 16:56:52 UTC
Fixed on 14/15 so far
Comment 8 GCC Commits 2024-07-29 16:15:30 UTC
The master branch has been updated by Sam James <sjames@gcc.gnu.org>:

https://gcc.gnu.org/g:ca255ca2760a5e6176031ea62a9c29c7bb92c212

commit r15-2383-gca255ca2760a5e6176031ea62a9c29c7bb92c212
Author: Sam James <sam@gentoo.org>
Date:   Mon Jul 29 16:47:09 2024 +0100

    testsuite: make PR115277 test an execute one
    
            PR middle-end/115277
            * gcc.c-torture/compile/pr115277.c: Rename to...
            * gcc.c-torture/execute/pr115277.c: ...this.
Comment 9 GCC Commits 2024-08-19 20:12:02 UTC
The releases/gcc-13 branch has been updated by Martin Jambor <jamborm@gcc.gnu.org>:

https://gcc.gnu.org/g:e469654e5e7bdd823c5aa996075e903c6b4d47e2

commit r13-8982-ge469654e5e7bdd823c5aa996075e903c6b4d47e2
Author: Jan Hubicka <hubicka@ucw.cz>
Date:   Mon Aug 19 17:10:25 2024 +0200

    Compare loop bounds in ipa-icf
    
    Hi,
    this testcase shows another poblem with missing comparators for metadata
    in ICF. With value ranges available to loop optimizations during early
    opts we can estimate number of iterations based on guarding condition that
    can be split away by the fnsplit pass. This patch disables ICF when
    number of iteraitons does not match.
    
    Bootstrapped/regtesed x86_64-linux, will commit it shortly
    
    gcc/ChangeLog:
    
            PR ipa/115277
            * ipa-icf-gimple.cc (func_checker::compare_loops): compare loop
            bounds.
    
    gcc/testsuite/ChangeLog:
    
            * gcc.c-torture/compile/pr115277.c: New test.
    
    (cherry picked from commit 0d19fbc7b0760ce665fa6a88cd40cfa0311358d7)
Comment 10 Martin Jambor 2024-08-19 20:13:13 UTC
Fixed backported to the last branch where it was needed.