Bug 92063

Summary: [10 Regression] ICE in operation_could_trap_p, at tree-eh.c:2528 when compiling Python's Python/_warnings.c
Product: gcc Reporter: Romain Geissler <romain.geissler>
Component: middle-endAssignee: Jakub Jelinek <jakub>
Status: RESOLVED FIXED    
Severity: normal CC: dcb314, jakub, krebbel1, krebbel
Priority: P3    
Version: 10.0   
Target Milestone: 10.0   
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed: 2019-10-11 00:00:00
Attachments: preprocessed source
gcc10-pr92063.patch
gcc10-pr92063.patch

Description Romain Geissler 2019-10-11 07:52:00 UTC
Hi,

I have an ICE in gcc 10 when trying to setup a GNU toolchain with current sources. It fails when trying to build Python 3.7.4. I don't have access to any build artefacts though, just the job logs.

Gcc is built from r276854 on x64 Linux. Gcc itself is bootstrapped with LTO + PGO. Building gcc itself works fine (as well as building the LLVM toolchain, glibc, and binutils with that gcc).
binutils: 2.33
glibc: 2.30

I am trying to build Python 3.7.4 from sources:


gcc -pthread -c -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -O2 -msse -msse2 -msse3 -I/workdir/build/build-pack/build-pack-temporary-static-dependencies/install/include -I/workdir/build/build-pack/build-pack-temporary-static-dependencies/install/include/ncursesw -I/workdir/build/build-pack/build-pack-temporary-static-dependencies/install/lib/libffi-3.2.1/include -I/opt/1A/toolchain/x86_64-v20.0.3/build-pack/20.0.3.0/internal-python-only-for-build-pack/include -Wno-error -O2 -msse -msse2 -msse3 -I/workdir/build/build-pack/build-pack-temporary-static-dependencies/install/include -I/workdir/build/build-pack/build-pack-temporary-static-dependencies/install/include/ncursesw -I/workdir/build/build-pack/build-pack-temporary-static-dependencies/install/lib/libffi-3.2.1/include -I/opt/1A/toolchain/x86_64-v20.0.3/build-pack/20.0.3.0/internal-python-only-for-build-pack/include -Wno-error  -std=c99 -Wextra -Wno-unused-result -Wno-unused-parameter -Wno-missing-field-initializers -Wno-cast-function-type -Wstrict-prototypes -Werror=implicit-function-declaration  -IObjects -IInclude -IPython -I. -I/workdir/src/Python-3.7.4/Include -I/workdir/build/build-pack/build-pack-temporary-static-dependencies/install/include -I/workdir/build/build-pack/build-pack-temporary-static-dependencies/install/include/ncursesw -I/workdir/build/build-pack/build-pack-temporary-static-dependencies/install/lib/libffi-3.2.1/include -I/opt/1A/toolchain/x86_64-v20.0.3/build-pack/20.0.3.0/internal-python-only-for-build-pack/include  -I/workdir/build/build-pack/build-pack-temporary-static-dependencies/install/include -I/workdir/build/build-pack/build-pack-temporary-static-dependencies/install/include/ncursesw -I/workdir/build/build-pack/build-pack-temporary-static-dependencies/install/lib/libffi-3.2.1/include -I/opt/1A/toolchain/x86_64-v20.0.3/build-pack/20.0.3.0/internal-python-only-for-build-pack/include  -DPy_BUILD_CORE -o Python/_warnings.o /workdir/src/Python-3.7.4/Python/_warnings.c


/workdir/src/Python-3.7.4/Python/_warnings.c: In function 'setup_context':
/workdir/src/Python-3.7.4/Python/_warnings.c:753:13: internal compiler error: in operation_could_trap_p, at tree-eh.c:2528
  753 |             ascii_lower(PyUnicode_READ(kind, data, len-1)) == 'c')
      |             ^~~~~~~~~~~



0x5f2081 operation_could_trap_p(tree_code, bool, bool, tree_node*)
	/workdir/src/gcc-10.0.0/gcc/tree-eh.c:2528
0x5f2081 operation_could_trap_p(tree_code, bool, bool, tree_node*)
	/workdir/src/gcc-10.0.0/gcc/tree-eh.c:2518
0xef269a tree_could_trap_p(tree_node*)
	/workdir/src/gcc-10.0.0/gcc/tree-eh.c:2635
0xf960d1 simple_operand_p_2
	/workdir/src/gcc-10.0.0/gcc/fold-const.c:4451
0xf91e62 fold_truth_andor
	/workdir/src/gcc-10.0.0/gcc/fold-const.c:8290
0xf1d2fd fold_binary_loc(unsigned int, tree_code, tree_node*, tree_node*, tree_node*)
	/workdir/src/gcc-10.0.0/gcc/fold-const.c:10606
0xf1be99 fold_build2_loc(unsigned int, tree_code, tree_node*, tree_node*, tree_node*)
	/workdir/src/gcc-10.0.0/gcc/fold-const.c:12382
0xf0bd53 c_fully_fold_internal
	/workdir/src/gcc-10.0.0/gcc/c/c-fold.c:535
0xf0bc4c c_fully_fold_internal
	/workdir/src/gcc-10.0.0/gcc/c/c-fold.c:513
0xf0bc4c c_fully_fold_internal
	/workdir/src/gcc-10.0.0/gcc/c/c-fold.c:513
0xf0ae6b c_fully_fold(tree_node*, bool, bool*, bool)
	/workdir/src/gcc-10.0.0/gcc/c/c-fold.c:125
0xf42a8a c_parser_condition
	/workdir/src/gcc-10.0.0/gcc/c/c-parser.c:5677
0xf420ce c_parser_paren_condition
	/workdir/src/gcc-10.0.0/gcc/c/c-parser.c:5695
0xf0793e c_parser_if_statement
	/workdir/src/gcc-10.0.0/gcc/c/c-parser.c:5874
0xf0793e c_parser_statement_after_labels
	/workdir/src/gcc-10.0.0/gcc/c/c-parser.c:5506
0xf033da c_parser_compound_statement_nostart
	/workdir/src/gcc-10.0.0/gcc/c/c-parser.c:5185
0xf00ac7 c_parser_compound_statement
	/workdir/src/gcc-10.0.0/gcc/c/c-parser.c:5019
0xf07f09 c_parser_if_body
	/workdir/src/gcc-10.0.0/gcc/c/c-parser.c:5749
0xf07f09 c_parser_if_statement
	/workdir/src/gcc-10.0.0/gcc/c/c-parser.c:5877
0xf07f09 c_parser_statement_after_labels
	/workdir/src/gcc-10.0.0/gcc/c/c-parser.c:5506
Please submit a full bug report,
with preprocessed source if appropriate.
Please include the complete backtrace with any bug report.
See <https://gcc.gnu.org/bugs/> for instructions.
make: *** [Makefile:1643: Python/_warnings.o] Error 1
make: *** Waiting for unfinished jobs....


Note: it was working with a gcc 10 snapshot from 12 September 2019, which was configured similarly, and which "gcc -v" yields (this is working case):
Reading specs from /remote/tools/Linux/2.6/1A/toolchain/x86_64-v20.0.2/bin/../lib/gcc/x86_64-1a-linux-gnu/10.0.0/specs
COLLECT_GCC=/opt/1A/toolchain/x86_64-v20/bin/gcc
COLLECT_LTO_WRAPPER=/remote/tools/Linux/2.6/1A/toolchain/x86_64-v20.0.2/bin/../libexec/gcc/x86_64-1a-linux-gnu/10.0.0/lto-wrapper
Target: x86_64-1a-linux-gnu
Configured with: /workdir/src/gcc-10.0.0/configure --build=x86_64-1a-linux-gnu --host=x86_64-1a-linux-gnu --target=x86_64-1a-linux-gnu --prefix=/opt/1A/toolchain/x86_64-v20.0.2 --with-local-prefix=/opt/1A/toolchain/x86_64-v20.0.2 --with-native-system-header-dir=/opt/1A/toolchain/x86_64-v20.0.2/include --enable-languages=c,c++,fortran --disable-multilib --enable-linker-build-id --with-linker-hash-style=gnu --enable-gnu-indirect-function --with-build-config=bootstrap-lto --enable-install-libiberty --enable-checking=release
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 10.0.0 20190912 (experimental) (GCC)

Cheers,
Romain
Comment 1 Romain Geissler 2019-10-11 08:00:15 UTC
Python code is here:

https://github.com/python/cpython/blob/v3.7.4/Python/_warnings.c#L753

#define ascii_lower(c) ((c <= 127) ? Py_TOLOWER(c) : 0)
        /* if filename.lower().endswith(".pyc"): */
        if (len >= 4 &&
            PyUnicode_READ(kind, data, len-4) == '.' &&
            ascii_lower(PyUnicode_READ(kind, data, len-3)) == 'p' &&
            ascii_lower(PyUnicode_READ(kind, data, len-2)) == 'y' &&
            ascii_lower(PyUnicode_READ(kind, data, len-1)) == 'c')
        {
            *filename = PyUnicode_Substring(*filename, 0,
                                            PyUnicode_GET_LENGTH(*filename)-1);
            if (*filename == NULL)
                goto handle_error;
        }
        else
            Py_INCREF(*filename);
Comment 2 Jakub Jelinek 2019-10-11 08:28:25 UTC
Please attach preprocessed source, see http://gcc.gnu.org/bugs/ for details.
Comment 3 Jan 2019-10-11 09:02:09 UTC
Same problem here is the backtrace:
COLLECT_GCC_OPTIONS='-pthread' '-c' '-Wsign-compare' '-D' 'NDEBUG' '-v' '-save-temps' '-pipe' '-march=native' '-O3' '-fwrapv' '-std=c99' '-Wextra' '-Wno-unused-result' '-Wno-unuse
d-parameter' '-Wno-missing-field-initializers' '-Wno-cast-function-type' '-Werror=implicit-function-declaration' '-I' '.' '-I' './Include' '-I' '/usr/include/ncursesw' '-fPIC' '-D
' 'Py_BUILD_CORE' '-o' 'Python/_warnings.o'
 /usr/libexec/gcc/x86_64-pc-linux-gnu/10.0.0-pre9999/cc1 -fpreprocessed _warnings.i -march=skylake-avx512 -mmmx -mno-3dnow -msse -msse2 -msse3 -mssse3 -mno-sse4a -mcx16 -msahf -mm
ovbe -maes -mno-sha -mpclmul -mpopcnt -mabm -mno-lwp -mfma -mno-fma4 -mno-xop -mbmi -mno-sgx -mbmi2 -mno-pconfig -mno-wbnoinvd -mno-tbm -mavx -mavx2 -msse4.2 -msse4.1 -mlzcnt -mrt
m -mhle -mrdrnd -mf16c -mfsgsbase -mrdseed -mprfchw -madx -mfxsr -mxsave -mxsaveopt -mavx512f -mno-avx512er -mavx512cd -mno-avx512pf -mno-prefetchwt1 -mclflushopt -mxsavec -mxsave
s -mavx512dq -mavx512bw -mavx512vl -mno-avx512ifma -mno-avx512vbmi -mno-avx5124fmaps -mno-avx5124vnniw -mclwb -mno-mwaitx -mno-clzero -mno-pku -mno-rdpid -mno-gfni -mno-shstk -mno
-avx512vbmi2 -mno-avx512vnni -mno-vaes -mno-vpclmulqdq -mno-avx512bitalg -mno-movdiri -mno-movdir64b -mno-waitpkg -mno-cldemote -mno-ptwrite -mno-avx512bf16 -mno-enqcmd -mno-avx51
2vp2intersect --param l1-cache-size=32 --param l1-cache-line-size=64 --param l2-cache-size=14080 -mtune=skylake-avx512 -quiet -dumpbase _warnings.c -auxbase-strip Python/_warnings
.o -O3 -Wsign-compare -Wextra -Wno-unused-result -Wno-unused-parameter -Wno-missing-field-initializers -Wno-cast-function-type -Werror=implicit-function-declaration -std=c99 -vers
ion -fwrapv -fPIC -o _warnings.s
GNU C99 (Gentoo 10.0.0_pre9999) version 10.0.0-pre9999 20191011 (experimental) (x86_64-pc-linux-gnu)
        compiled by GNU C version 10.0.0-pre9999 20191011 (experimental), GMP version 6.1.2, MPFR version 4.0.2, MPC version 1.1.0, isl version isl-0.21-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
GNU C99 (Gentoo 10.0.0_pre9999) version 10.0.0-pre9999 20191011 (experimental) (x86_64-pc-linux-gnu)
        compiled by GNU C version 10.0.0-pre9999 20191011 (experimental), GMP version 6.1.2, MPFR version 4.0.2, MPC version 1.1.0, isl version isl-0.21-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 0221118538dfe651be51fb82b7b0281e
Python/_warnings.c: In function ‘setup_context’:
Python/_warnings.c:753:13: internal compiler error: in operation_could_trap_p, at tree-eh.c:2528
  753 |             ascii_lower(PyUnicode_READ(kind, data, len-1)) == 'c')
      |             ^
0x8859dd operation_could_trap_p(tree_code, bool, bool, tree_node*)
        /var/tmp/portage/sys-devel/gcc-10.0.0_pre9999/work/gcc-10.0.0_pre9999/gcc/tree-eh.c:2528
0x8859dd operation_could_trap_p(tree_code, bool, bool, tree_node*)
        /var/tmp/portage/sys-devel/gcc-10.0.0_pre9999/work/gcc-10.0.0_pre9999/gcc/tree-eh.c:2518
0x8859dd tree_could_trap_p(tree_node*)
        /var/tmp/portage/sys-devel/gcc-10.0.0_pre9999/work/gcc-10.0.0_pre9999/gcc/tree-eh.c:2635
0xf419c1 simple_operand_p_2
        /var/tmp/portage/sys-devel/gcc-10.0.0_pre9999/work/gcc-10.0.0_pre9999/gcc/fold-const.c:4451
0xf3dfdf fold_truth_andor
        /var/tmp/portage/sys-devel/gcc-10.0.0_pre9999/work/gcc-10.0.0_pre9999/gcc/fold-const.c:8290
0xf32048 fold_binary_loc(unsigned int, tree_code, tree_node*, tree_node*, tree_node*)
        /var/tmp/portage/sys-devel/gcc-10.0.0_pre9999/work/gcc-10.0.0_pre9999/gcc/fold-const.c:10606
0xf30f29 fold_build2_loc(unsigned int, tree_code, tree_node*, tree_node*, tree_node*)
        /var/tmp/portage/sys-devel/gcc-10.0.0_pre9999/work/gcc-10.0.0_pre9999/gcc/fold-const.c:12382
0xe75b80 c_fully_fold_internal
        /var/tmp/portage/sys-devel/gcc-10.0.0_pre9999/work/gcc-10.0.0_pre9999/gcc/c/c-fold.c:535
0xe75a8d c_fully_fold_internal
        /var/tmp/portage/sys-devel/gcc-10.0.0_pre9999/work/gcc-10.0.0_pre9999/gcc/c/c-fold.c:513
0xe75a8d c_fully_fold_internal
        /var/tmp/portage/sys-devel/gcc-10.0.0_pre9999/work/gcc-10.0.0_pre9999/gcc/c/c-fold.c:513
0xe74fcc c_fully_fold(tree_node*, bool, bool*, bool)
        /var/tmp/portage/sys-devel/gcc-10.0.0_pre9999/work/gcc-10.0.0_pre9999/gcc/c/c-fold.c:125
0x133862b c_parser_condition
        /var/tmp/portage/sys-devel/gcc-10.0.0_pre9999/work/gcc-10.0.0_pre9999/gcc/c/c-parser.c:5677
0x13385ae c_parser_paren_condition
        /var/tmp/portage/sys-devel/gcc-10.0.0_pre9999/work/gcc-10.0.0_pre9999/gcc/c/c-parser.c:5695
0xe724c1 c_parser_if_statement
        /var/tmp/portage/sys-devel/gcc-10.0.0_pre9999/work/gcc-10.0.0_pre9999/gcc/c/c-parser.c:5874
0xe724c1 c_parser_statement_after_labels
        /var/tmp/portage/sys-devel/gcc-10.0.0_pre9999/work/gcc-10.0.0_pre9999/gcc/c/c-parser.c:5506
0xe6f363 c_parser_compound_statement_nostart
        /var/tmp/portage/sys-devel/gcc-10.0.0_pre9999/work/gcc-10.0.0_pre9999/gcc/c/c-parser.c:5185
0x1337970 c_parser_compound_statement
        /var/tmp/portage/sys-devel/gcc-10.0.0_pre9999/work/gcc-10.0.0_pre9999/gcc/c/c-parser.c:5019
0xe729e7 c_parser_if_body
        /var/tmp/portage/sys-devel/gcc-10.0.0_pre9999/work/gcc-10.0.0_pre9999/gcc/c/c-parser.c:5749
0xe729e7 c_parser_if_statement
        /var/tmp/portage/sys-devel/gcc-10.0.0_pre9999/work/gcc-10.0.0_pre9999/gcc/c/c-parser.c:5877
0xe729e7 c_parser_statement_after_labels
        /var/tmp/portage/sys-devel/gcc-10.0.0_pre9999/work/gcc-10.0.0_pre9999/gcc/c/c-parser.c:5506
Please submit a full bug report,
with preprocessed source if appropriate.
Please include the complete backtrace with any bug report.
See <https://bugs.gentoo.org/> for instructions.
I will add the preprocessed source.
Comment 4 Jan 2019-10-11 09:07:40 UTC
Created attachment 47019 [details]
preprocessed source
Comment 5 Jakub Jelinek 2019-10-11 09:23:34 UTC
Reduced testcase:
int
foo (int a, int b, int *c, short *d)
{
  return (c[0] ? b : 0) == 'y' && ((a ? d[0] : c[0]) ? b : 0) == 'c';
}
Comment 6 Jakub Jelinek 2019-10-11 09:51:11 UTC
Started with r276659.
The fix for this ICE is easy:
--- gcc/tree-eh.c.jj	2019-10-07 17:30:31.028153702 +0200
+++ gcc/tree-eh.c	2019-10-11 11:46:37.376774860 +0200
@@ -2615,7 +2615,7 @@ tree_could_trap_p (tree expr)
     return false;
 
   /* For COND_EXPR and VEC_COND_EXPR only the condition may trap.  */
-  if (TREE_CODE (expr) == COND_EXPR || TREE_CODE (expr) == VEC_COND_EXPR)
+  while (TREE_CODE (expr) == COND_EXPR || TREE_CODE (expr) == VEC_COND_EXPR)
     expr = TREE_OPERAND (expr, 0);
 
   code = TREE_CODE (expr);
though I'm a little bit worried about the tree-ssa-sccvn.c's operation_could_trap_helper_p call, whether it can't call it on COND_EXPR/VEC_COND_EXPR; while there is no assert in operation_could_trap_helper_p for COND_EXPR/VEC_COND_EXPR, if it is ever called, it might not give the right answer.  Guess for those fp_operation would be whether the second/third operand and lhs are floating, which might not be related to whether condition is actually a floating point operation.
Anyway, will handle the above part and defer the rest to Ilya.
Comment 7 Jakub Jelinek 2019-10-11 10:38:47 UTC
Created attachment 47021 [details]
gcc10-pr92063.patch

Untested fix.
Comment 8 Jan 2019-10-11 11:21:20 UTC
Fix works for me
Comment 9 Jakub Jelinek 2019-10-11 11:53:49 UTC
Created attachment 47022 [details]
gcc10-pr92063.patch

Updated patch based on IRC discussions.
Comment 10 Ilya Leoshkevich 2019-10-11 16:07:20 UTC
Hi Jakub, thanks for fixing this!  FWIW the patch looks good to me.  I
have also run my signaling comparison tests on S/390 with it, and they
still work.

Is there something else I need to look at in context of this problem?
Comment 11 Jakub Jelinek 2019-10-12 12:22:16 UTC
Author: jakub
Date: Sat Oct 12 12:21:45 2019
New Revision: 276915

URL: https://gcc.gnu.org/viewcvs?rev=276915&root=gcc&view=rev
Log:
	PR middle-end/92063
	* tree-eh.c (operation_could_trap_helper_p) <case COND_EXPR>
	<case VEC_COND_EXPR>: Return false with *handled = false.
	(tree_could_trap_p): For {,VEC_}COND_EXPR return false instead of
	recursing on the first operand.
	* fold-const.c (simple_operand_p_2): Use generic_expr_could_trap_p
	instead of tree_could_trap_p.
	* tree-ssa-sccvn.c (vn_nary_may_trap): Formatting fixes.

	* gcc.c-torture/compile/pr92063.c: New test.

Added:
    trunk/gcc/testsuite/gcc.c-torture/compile/pr92063.c
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/fold-const.c
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/tree-eh.c
    trunk/gcc/tree-ssa-sccvn.c
Comment 12 Jakub Jelinek 2019-10-12 12:23:24 UTC
Should be fixed now.
Comment 13 Andrew Pinski 2019-10-13 07:31:49 UTC
*** Bug 92079 has been marked as a duplicate of this bug. ***