+++ This bug was initially created as a clone of Bug #117249 +++ C++ static inline bool foo (unsigned x) { return x == 4 || x == 5 || x == 9 || x == 10; } int v; [[gnu::noipa]] void bar (const char *p) { if (p[0] != (v ? 'a' : 'b') || p[1]) __builtin_abort (); } [[gnu::noipa]] void baz (unsigned x) { bool a = x == 5; [[assume (foo (x))]]; bar (a ? "a" : "b"); } int main () { baz (4); v = 1; baz (5); v = 0; baz (9); baz (10); } is miscompiled on the trunk and 14/13 branches with -O2 --param=logical-op-non-short-circuit=0, the vrp1 pass decides to call bar ("b"); unconditionally, even when the assumption should result in [4,5][9,10] range for x.
_Z3bazj._assume.0 assume inferred range of x_2(D) (param x) = [irange] unsigned int [9, 10] MASK 0x3 VALUE 0x8 on-exit update x_2(D) in BB2 : [irange] unsigned int [9, 10] MASK 0x3 VALUE 0x8 The _Z3bazj._assume.0 function body is <bb 2> [local count: 1073741822]: _3 = x_2(D) + 4294967292; if (_3 <= 1) goto <bb 4>; [41.00%] else goto <bb 3>; [59.00%] <bb 3> [local count: 633507679]: if (x_2(D) == 9) goto <bb 4>; [34.00%] else goto <bb 5>; [66.00%] <bb 4> [local count: 797785881]: goto <bb 6>; [100.00%] <bb 5> [local count: 418115066]: if (x_2(D) == 10) goto <bb 4>; [34.00%] else goto <bb 6>; [66.00%] <bb 6> [local count: 1073741824]: # iftmp.3_4 = PHI <1(4), 0(5)> return iftmp.3_4; and so for x - 4U <= 1 should return 1 (i.e. [4,5] range), or for [9,9], or for [10,10].
It seems that until now, we didn't see an empty block feeding a PHI that mattered: <bb 2> _3 = x_2(D) + 4294967292; if (_3 <= 1) goto <bb 5>; [41.00%] else goto <bb 3>; [59.00%] <bb 5> goto <bb 4>; [100.00%] <bb 3> _6 = x_2(D) + 4294967287; _7 = _6 <= 1; <bb 4> # iftmp.3_4 = PHI <_7(3), 1(5)> return iftmp.3_4; we process back the first PHI operand with _7 == [1, 1]and eventually get to x_2 = [9, 10]. the problem comes when we lok at the second phi argument. check_taken_edge looks for a branch on the previous block which it can then evaluate the operands with a LHS of [1,1] with no branch, it assumes (no pun intended), that the previous block doesn't provide any useful information because it has no LHS that it can propagate the [1,1] into. and it simply stops. I looked at checking if the block has only a single pred, to then check that block to see if it has a branch. This causes another issue. We then end up back at the if (_3 <= 1) branch again, however we have already given _3 a value of [2, +INF] from the previous path.. this new path wants to give it a value of [0,1], which would then allow x_2 to be evaluated t0 [4,5] on that path. the current mechanism intersect values when it calculates a new one. so things again break down and are even uglier :-P The assume pass currently has no concept of what values are of interest at the end (ie the parameters), it just performs the "feeding def" walk back and calculates a bunch of ranges, then when its done, the assume pass exports any values it found that happen to be parameters. Im thinking that perhaps we should instead throw away any existing values found during the walk that are local to the function.. And if we calculate a value for a parameter, we union it with any current value. That will get this case correct (I believe), it just unclear to me yet if it would affect anything else negatively. thoughts? It also seems to me that if we encounter this situation, and the block feeding the [1,1] constant phi argument has more than one predecessor, maybe we should punt and assign no assume values.. its an IL where we aren't processing all predecessor blocks which may lead to a TRUE result, so we aren't calculating things properly.. Or if this solution works, maybe we simply process each predecessor the same way.. again its unclear to me at this point, but we shall see.
Whatever is done, it needs to be (worst case conservatively) correct, so if it can't or decides not to process some edge which does or might result into true result, assume the worst (VARYING etc.). Of course, it would be nice if for simple cases like this it actually constructed the exact range rather than trying to be conservatively correct, that is something that can be deferred to much more complex assume functions.
Not sure if should be P1 given it's (more) triggerable on trunk.
Why do you think it is more triggerable on trunk rather than on branches? The testcase ICEs on the 13/14 branches too, and the only reason I saw it on the trunk is that I was considering using [[assume (EXPR)]] for --disable-checking gcc_assert. I'm obviously not going to do that until this bug is fixed and will need to limit such use to GCC_VERSION >= 15000 even when it is.
I misparsed "is miscompiled on the trunk and 14/13 branches with -O2 --param=logical-op-non-short-circuit=0". I now see that the reason you hit it was a different default on rs6000, rather than something else exposing it on trunk.
The master branch has been updated by Andrew Macleod <amacleod@gcc.gnu.org>: https://gcc.gnu.org/g:62420dbd105bbc4fe732881875c5c85aa55b6069 commit r15-4851-g62420dbd105bbc4fe732881875c5c85aa55b6069 Author: Andrew MacLeod <amacleod@redhat.com> Date: Mon Oct 28 10:19:27 2024 -0400 Reimplement 'assume' processing pass. Rework the assume pass to work properly and fail conservatively when it does. Also move it to its own file. PR tree-optimization/117287 gcc/ * Makefile.in (IBJS): Add tree-assume.o * gimple-range.cc (assume_query::assume_range_p): Remove. (assume_query::range_of_expr): Remove. (assume_query::assume_query): Move to tree-assume.cc. (assume_query::~assume_query): Remove. (assume_query::calculate_op): Move to tree-assume.cc. (assume_query::calculate_phi): Likewise. (assume_query::check_taken_edge): Remove. (assume_query::calculate_stmt): Move to tree-assume.cc. (assume_query::dump): Remove. * gimple-range.h (class assume_query): Move to tree-assume.cc * tree-assume.cc: New * tree-vrp.cc (struct pass_data_assumptions): Move to tree-assume.cc. (class pass_assumptions): Likewise. (make_pass_assumptions): Likewise. gcc/testsuite/ * g++.dg/cpp23/pr117287-attr.C: New.
The releases/gcc-14 branch has been updated by Andrew Macleod <amacleod@gcc.gnu.org>: https://gcc.gnu.org/g:393ee6b64579ea5ae5ae88079d271d3a237c7d9f commit r14-10876-g393ee6b64579ea5ae5ae88079d271d3a237c7d9f Author: Andrew MacLeod <amacleod@redhat.com> Date: Fri Nov 1 12:46:39 2024 -0400 Reimplement 'assume' processing pass. Rework the assume pass to work properly and fail conservatively when it does. Also move it to its own file. PR tree-optimization/117287 gcc/ * Makefile.in (IBJS): Add tree-assume.o * gimple-range.cc (assume_query::assume_range_p): Remove. (assume_query::range_of_expr): Remove. (assume_query::assume_query): Move to tree-assume.cc. (assume_query::~assume_query): Remove. (assume_query::calculate_op): Move to tree-assume.cc. (assume_query::calculate_phi): Likewise. (assume_query::check_taken_edge): Remove. (assume_query::calculate_stmt): Move to tree-assume.cc. (assume_query::dump): Remove. * gimple-range.h (class assume_query): Move to tree-assume.cc * tree-assume.cc: New * tree-vrp.cc (struct pass_data_assumptions): Move to tree-assume.cc. (class pass_assumptions): Likewise. (make_pass_assumptions): Likewise. gcc/testsuite/ * g++.dg/cpp23/pr117287-attr.C: New.
The new implementation should resolve it.
The testcase from https://gcc.gnu.org/bugzilla/show_bug.cgi?id=117287#c0 still fails for me on releases/gcc-13. Is the change backportable there too?
Created attachment 60905 [details] potential patch (In reply to Sam James from comment #10) > The testcase from https://gcc.gnu.org/bugzilla/show_bug.cgi?id=117287#c0 > still fails for me on releases/gcc-13. Is the change back portable there too? There are some issues previous to GCC 14. It requires the new lazy_ssa_cache from GCC 14, which is dependent on some other changes in range storage allocators. I did convert the new cache code and isolated it all to the new assume pass file, tree-assume.cc. This should, in theory, not impact any other part of the compiler. I've attached the patch. I will also submit it, although I debated not doing so. I will leave it up to the release managers whether they want this added to GCC 13 or not. Feel free to use it on your own compiler however, it *should* work :-)
*** Bug 120087 has been marked as a duplicate of this bug. ***
The releases/gcc-13 branch has been updated by Andrew Macleod <amacleod@gcc.gnu.org>: https://gcc.gnu.org/g:dce408e28c416f3a34f487e4f8bb2a1e1e586c69 commit r13-9657-gdce408e28c416f3a34f487e4f8bb2a1e1e586c69 Author: Andrew MacLeod <amacleod@redhat.com> Date: Thu Mar 27 10:51:16 2025 -0400 Backport new assume implementation and cache. Rework the assume pass to work properly and fail conservatively when it does. Also move it to its own file. PR tree-optimization/117287 gcc/ * Makefile.in (IBJS): Add tree-assume.o * gimple-range-fold.cc (class fur_edge): Relocate to... * gimple-range-fold.h (class fur_edge): Here. * gimple-range.cc (assume_query::assume_range_p): Remove. (assume_query::range_of_expr): Remove. (assume_query::assume_query): Move to tree-assume.cc. (assume_query::~assume_query): Remove. (assume_query::calculate_op): Move to tree-assume.cc. (assume_query::calculate_phi): Likewise. (assume_query::check_taken_edge): Remove. (assume_query::calculate_stmt): Move to tree-assume.cc. (assume_query::dump): Remove. * gimple-range.h (class assume_query): Move to tree-assume.cc * tree-assume.cc: New * tree-vrp.cc (struct pass_data_assumptions): Move to tree-assume.cc. (class pass_assumptions): Likewise. (make_pass_assumptions): Likewise. gcc/testsuite/ * g++.dg/cpp23/pr117287-attr.C: New.
fix in gcc 13 onward now.