this bug was originally reported against GCC8.5 with profiling feedback. there were multiple similar failures due to this issue for our large application. Although we reduced the testing case to a very small size, and changed the variable names. the failure can only be repeated with -fprofile-use and the .gcda files. As a result, we cannot expose the testing case. With the small testing case, and debugging into GCC8, I finally locate the issue is: this is a bug in tree-ssa-math-opts.cc, when applying the widening mul optimization, The compiler needs to check whether the operand is in a ABNORMAL PHI, if YES, we should avoid the transformation. the following patch against GCC8 can fix the failure very well: diff -u -r -N -p gcc-8.5.0-20210514-org/gcc/tree-ssa-math-opts.c gcc-8.5.0-20210514/gcc/tree-ssa-math-opts.c --- gcc-8.5.0-20210514-org/gcc/tree-ssa-math-opts.c 2023-09-11 21:04:17.891403319 +0000 +++ gcc-8.5.0-20210514/gcc/tree-ssa-math-opts.c 2023-09-13 15:35:44.962336530 +0000 @@ -2346,6 +2346,14 @@ convert_mult_to_widen (gimple *stmt, gim if (!is_widening_mult_p (stmt, &type1, &rhs1, &type2, &rhs2)) return false; + /* if any one of rhs1 and rhs2 is subjust to abnormal coalescing + * avoid the tranform. */ + if ((TREE_CODE (rhs1) == SSA_NAME + && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs1)) + || (TREE_CODE (rhs2) == SSA_NAME + && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs2))) + return false; + to_mode = SCALAR_INT_TYPE_MODE (type); from_mode = SCALAR_INT_TYPE_MODE (type1); if (to_mode == from_mode) I checked the latest upstream GCC14, and found that the function "convert_mult_to_widen" has the same issue, need to be patched as well.
Do you have a testcase?
Also what target is this for? I suspect aarch64 since x86_64 does not have widening multiply ...
(In reply to Andrew Pinski from comment #1) > Do you have a testcase? I have, but I cannot expose it to public. I can provide the Bad IR and Good IR if you think it's okay.
(In reply to Andrew Pinski from comment #2) > Also what target is this for? > I suspect aarch64 since x86_64 does not have widening multiply ... you are right, it's aarch64.
Testcase: ``` enum { SEND_TOFILE } __sigsetjmp(); void fclose(); void foldergets(); void sendpart_stats(int *p1, int a1, int b1) { int *a = p1; fclose(); p1 = 0; long t = b1; if (__sigsetjmp()) { { long t1 = a1; a1+=1; fclose(a1*(long)t1); } } if (p1) fclose(); } ```
(In reply to Andrew Pinski from comment #5) > Testcase: The way I figured out this testcase was trial and error and starting with the testcase from PR 69167 .
(In reply to Andrew Pinski from comment #5) > Testcase: thanks a lot for the testing case. GCC8 failed with this, disable tree-widening_mul fixed the failure. and my patch for GCC8 also fixed the failure. will test GCC14 as well.
the latest GCC14 has the same issue. with the patch proposed in comment #1, the failure has been fixed.
The master branch has been updated by Qing Zhao <qinzhao@gcc.gnu.org>: https://gcc.gnu.org/g:4aca1cfd6235090e48a53dab734437740671bbf3 commit r14-4034-g4aca1cfd6235090e48a53dab734437740671bbf3 Author: Qing Zhao <qing.zhao@oracle.com> Date: Fri Sep 15 13:46:52 2023 +0000 Fix PR111407--SSA corruption due to widening_mul opt on conflict across an abnormal edge This is a bug in tree-ssa-math-opts.cc, when applying the widening mul optimization, the compiler needs to check whether the operand is in a ABNORMAL PHI, if YES, we should avoid the transformation. PR tree-optimization/111407 gcc/ChangeLog: * tree-ssa-math-opts.cc (convert_mult_to_widen): Avoid the transform when one of the operands is subject to abnormal coalescing. gcc/testsuite/ChangeLog: * gcc.dg/pr111407.c: New test.
(In reply to Andrew Pinski from comment #6) the fix has been in GCC14, shall we backport the patch to previous releases?
For aarch64, this has worked in GCC 6.4.0. s/long/long long/ it also fails for arm starting in GCC 7.x. Likewise for mips64 and mips.
(In reply to qinzhao from comment #10) > (In reply to Andrew Pinski from comment #6) > the fix has been in GCC14, shall we backport the patch to previous releases? Since it is a regression, yes please.
(In reply to Andrew Pinski from comment #12) > (In reply to qinzhao from comment #10) > > (In reply to Andrew Pinski from comment #6) > > the fix has been in GCC14, shall we backport the patch to previous releases? > > Since it is a regression, yes please. So, How far back should I add the fix? gcc13, gcc12, till ?
The releases/gcc-13 branch has been updated by Qing Zhao <qinzhao@gcc.gnu.org>: https://gcc.gnu.org/g:2d9a9488e26233eb9497722fa9ccb88258f7402c commit r13-8556-g2d9a9488e26233eb9497722fa9ccb88258f7402c Author: Qing Zhao <qing.zhao@oracle.com> Date: Thu Feb 29 15:07:49 2024 +0000 Fix SSA corruption due to widening_mul opt on conflict across an abnormal edge [PR111407] This is a bug in tree-ssa-math-opts.cc, when applying the widening mul optimization, the compiler needs to check whether the operand is in a ABNORMAL PHI, if YES, we should avoid the transformation. PR tree-optimization/111407 gcc/ChangeLog: * tree-ssa-math-opts.cc (convert_mult_to_widen): Avoid the transform when one of the operands is subject to abnormal coalescing. gcc/testsuite/ChangeLog: * gcc.dg/pr111407.c: New test. (cherry picked from commit 4aca1cfd6235090e48a53dab734437740671bbf3)
The releases/gcc-12 branch has been updated by Qing Zhao <qinzhao@gcc.gnu.org>: https://gcc.gnu.org/g:5f23f9f141c4b52e8f4a9aadc88b8155cf1959a3 commit r12-10306-g5f23f9f141c4b52e8f4a9aadc88b8155cf1959a3 Author: Qing Zhao <qing.zhao@oracle.com> Date: Thu Feb 29 15:07:49 2024 +0000 Fix SSA corruption due to widening_mul opt on conflict across an abnormal edge [PR111407] This is a bug in tree-ssa-math-opts.cc, when applying the widening mul optimization, the compiler needs to check whether the operand is in a ABNORMAL PHI, if YES, we should avoid the transformation. PR tree-optimization/111407 gcc/ChangeLog: * tree-ssa-math-opts.cc (convert_mult_to_widen): Avoid the transform when one of the operands is subject to abnormal coalescing. gcc/testsuite/ChangeLog: * gcc.dg/pr111407.c: New test. (cherry picked from commit 4aca1cfd6235090e48a53dab734437740671bbf3)
The releases/gcc-11 branch has been updated by Qing Zhao <qinzhao@gcc.gnu.org>: https://gcc.gnu.org/g:4de35949e462d89926a171cd1ef7b6f40a308dab commit r11-11306-g4de35949e462d89926a171cd1ef7b6f40a308dab Author: Qing Zhao <qing.zhao@oracle.com> Date: Mon Mar 25 14:17:56 2024 +0000 Fix SSA corruption due to widening_mul opt on conflict across an abnormal edge [PR111407] This is a bug in tree-ssa-math-opts.c, when applying the widening mul optimization, the compiler needs to check whether the operand is in a ABNORMAL PHI, if YES, we should avoid the transformation. PR tree-optimization/111407 gcc/ChangeLog: * tree-ssa-math-opts.c (convert_mult_to_widen): Avoid the transform when one of the operands is subject to abnormal coalescing. gcc/testsuite/ChangeLog: * gcc.dg/pr111407.c: New test. (cherry picked from commit 4aca1cfd6235090e48a53dab734437740671bbf3)
the patch has been back ported to all supported releases: GCC13, GCC12 and GCC11. close it as fixed.