From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from esa2.mentor.iphmx.com (esa2.mentor.iphmx.com [68.232.141.98]) by sourceware.org (Postfix) with ESMTPS id B2C0C3858423 for ; Fri, 28 Jul 2023 13:05:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B2C0C3858423 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=codesourcery.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=mentor.com X-IronPort-AV: E=Sophos;i="6.01,237,1684828800"; d="scan'208";a="14532024" Received: from orw-gwy-02-in.mentorg.com ([192.94.38.167]) by esa2.mentor.iphmx.com with ESMTP; 28 Jul 2023 05:05:00 -0800 IronPort-SDR: pKxvM/fQWCMJYgtW0mUMitzWhBn2m6zU8lPsX/SY1e/taaBXtMzzjiTs2BCQCqCMIKxS1Avghz qQ3PCTSwBCGlMxi6+NO27cyjnWR/bwna7iOdms5sQc5kC8HWNe2xAUR5g7nLpUYwy/dYoA6A99 oSQKQ0mWpP+9tQLp77ZBKqcKeI1kKtJqbMEeprwzYqWXdnhsEWnFZAcK34Yr4Iw1ZXSFb7bx6g F/L+pDGHT5Sdh/5M5p0At7TzvX3HW045ua5omJExIXz0zlL6q7D5gafafrFezmfqg5NoUoM7bV Kso= From: Frederik Harwath To: , , Subject: [PATCH 4/4] openmp: Fix number of iterations computation for "omp unroll full" Date: Fri, 28 Jul 2023 13:04:33 +0000 Message-ID: <20230728130433.2377366-5-frederik@codesourcery.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230728130433.2377366-1-frederik@codesourcery.com> References: <20230324153046.3996092-1-frederik@codesourcery.com> <20230728130433.2377366-1-frederik@codesourcery.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain X-Originating-IP: [137.202.0.90] X-ClientProxiedBy: svr-ies-mbx-12.mgc.mentorg.com (139.181.222.12) To svr-ies-mbx-10.mgc.mentorg.com (139.181.222.10) X-Spam-Status: No, score=-12.5 required=5.0 tests=BAYES_00,GIT_PATCH_0,HEADER_FROM_DIFFERENT_DOMAINS,KAM_DMARC_STATUS,SPF_HELO_PASS,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: gcc/ChangeLog: * omp-transform-loops.cc (gomp_for_number_of_iterations): Always compute "final - init" and do not take absolute value. Identify non-iterating and infinite loops for constant init, final, step values for better diagnostic messages, consistent behaviour in those corner cases, and better testability. (gomp_for_constant_iterations_p): Add new argument to pass on information about infinite loops, and ... (full_unroll): ... use from here to emit a warning and remove unrolled, known infinite loops consistently. (process_omp_for): Only print dump message if loop has not been removed by transformation. gcc/testsuite/ChangeLog: * c-c++-common/gomp/loop-transforms/unroll-8.c: New test. --- gcc/omp-transform-loops.cc | 94 ++++++++++++++----- .../gomp/loop-transforms/unroll-8.c | 76 +++++++++++++++ 2 files changed, 146 insertions(+), 24 deletions(-) create mode 100644 gcc/testsuite/c-c++-common/gomp/loop-transforms/unroll-= 8.c diff --git a/gcc/omp-transform-loops.cc b/gcc/omp-transform-loops.cc index c8853bcee89..b0645397641 100644 --- a/gcc/omp-transform-loops.cc +++ b/gcc/omp-transform-loops.cc @@ -153,20 +153,27 @@ subst_defs (tree expr, gimple_seq seq) return expr; } -/* Return an expression for the number of iterations of the outermost loop= of - OMP_FOR. */ +/* Return an expression for the number of iterations of the loop at + the given LEVEL of OMP_FOR. + + If the expression is a negative constant, this means that the loop + is infinite. This can only be recognized for loops with constant + initial, final, and step values. In general, according to the + OpenMP specification, the behaviour is unspecified if the number of + iterations does not fit the types used for their computation, and + hence in particular if the loop is infinite. */ tree gomp_for_number_of_iterations (const gomp_for *omp_for, size_t level) { gcc_assert (!non_rectangular_p (omp_for)); - tree init =3D gimple_omp_for_initial (omp_for, level); tree final =3D gimple_omp_for_final (omp_for, level); tree_code cond =3D gimple_omp_for_cond (omp_for, level); tree index =3D gimple_omp_for_index (omp_for, level); tree type =3D gomp_for_iter_count_type (index, final); - tree step =3D TREE_OPERAND (gimple_omp_for_incr (omp_for, level), 1); + tree incr =3D gimple_omp_for_incr (omp_for, level); + tree step =3D omp_get_for_step_from_incr (gimple_location (omp_for), inc= r); init =3D subst_defs (init, gimple_omp_for_pre_body (omp_for)); init =3D fold (init); @@ -181,34 +188,64 @@ gomp_for_number_of_iterations (const gomp_for *omp_fo= r, size_t level) diff_type =3D ptrdiff_type_node; } - tree diff; - if (cond =3D=3D GT_EXPR) - diff =3D fold_build2 (minus_code, diff_type, init, final); - else if (cond =3D=3D LT_EXPR) - diff =3D fold_build2 (minus_code, diff_type, final, init); - else - gcc_unreachable (); - diff =3D fold_build2 (CEIL_DIV_EXPR, type, diff, step); - diff =3D fold_build1 (ABS_EXPR, type, diff); + /* Identify a simple case in which the loop does not iterate. The + computation below could not tell this apart from an infinite + loop, hence we handle this separately for better diagnostic + messages. */ + gcc_assert (cond =3D=3D GT_EXPR || cond =3D=3D LT_EXPR); + if (TREE_CONSTANT (init) && TREE_CONSTANT (final) + && ((cond =3D=3D GT_EXPR && tree_int_cst_le (init, final)) + || (cond =3D=3D LT_EXPR && tree_int_cst_le (final, init)))) + return build_int_cst (diff_type, 0); + + tree diff =3D fold_build2 (minus_code, diff_type, final, init); + + /* Divide diff by the step. + + We could always use CEIL_DIV_EXPR since only non-negative results + correspond to valid number of iterations and the behaviour is + unspecified by the spec otherwise. But we try to get the rounding + right for constant negative values to identify infinite loops + more precisely for better warnings. */ + tree_code div_expr =3D CEIL_DIV_EXPR; + if (TREE_CONSTANT (diff) && TREE_CONSTANT (step)) + { + bool diff_is_neg =3D tree_int_cst_lt (diff, size_zero_node); + bool step_is_neg =3D tree_int_cst_lt (step, size_zero_node); + if ((diff_is_neg && !step_is_neg) + || (!diff_is_neg && step_is_neg)) + div_expr =3D FLOOR_DIV_EXPR; + } + diff =3D fold_build2 (div_expr, type, diff, step); return diff; } -/* Return true if the expression representing the number of iterations for - OMP_FOR is a constant expression, false otherwise. */ +/* Return true if the expression representing the number of iterations + for OMP_FOR is a non-negative constant and set ITERATIONS to the + value of that expression. Otherwise, return false. Set INFINITE to + true if the number of iterations was recognized to be infinite. */ bool gomp_for_constant_iterations_p (gomp_for *omp_for, - unsigned HOST_WIDE_INT *iterations) + unsigned HOST_WIDE_INT *iterations, + bool *infinite =3D NULL) { tree t =3D gomp_for_number_of_iterations (omp_for, 0); - if (!TREE_CONSTANT (t) - || !tree_fits_uhwi_p (t)) + if (!TREE_CONSTANT (t)) return false; - *iterations =3D tree_to_uhwi (t); - return true; + if (infinite && + tree_int_cst_lt (t, size_zero_node)) + *infinite =3D true; + else if (tree_fits_uhwi_p (t)) + { + *iterations =3D tree_to_uhwi (t); + return true; + } + + return false; } static gimple_seq @@ -525,10 +562,18 @@ full_unroll (gomp_for *omp_for, location_t loc, walk_= ctx *ctx ATTRIBUTE_UNUSED) { tree init =3D gimple_omp_for_initial (omp_for, 0); unsigned HOST_WIDE_INT niter =3D 0; - if (!gomp_for_constant_iterations_p (omp_for, &niter)) + bool infinite =3D false; + bool constant =3D gomp_for_constant_iterations_p (omp_for, &niter, &infi= nite); + + if (infinite) + { + warning_at (loc, 0, "Cannot apply full unrolling to infinite loop"); + return NULL; + } + if (!constant) { error_at (loc, "Cannot apply full unrolling to loop with " - "non-constant number of iterations"); + "non-constant number of iterations"); return omp_for; } @@ -1595,8 +1640,9 @@ process_omp_for (gomp_for *omp_for, gimple_seq *conta= ining_seq, walk_ctx *ctx) if (!dump_enabled_p () || !(dump_flags & TDF_DETAILS)) return; - dump_printf_loc (MSG_NOTE | MSG_PRIORITY_INTERNALS, transformed, - "Transformed loop: %G\n\n", transformed); + if (transformed) + dump_printf_loc (MSG_NOTE | MSG_PRIORITY_INTERNALS, transformed, + "Transformed loop: %G\n\n", transformed); } /* Traverse SEQ in depth-first order and apply the loop transformation diff --git a/gcc/testsuite/c-c++-common/gomp/loop-transforms/unroll-8.c b/g= cc/testsuite/c-c++-common/gomp/loop-transforms/unroll-8.c new file mode 100644 index 00000000000..d49d7c42c87 --- /dev/null +++ b/gcc/testsuite/c-c++-common/gomp/loop-transforms/unroll-8.c @@ -0,0 +1,76 @@ +extern void dummy(int); + +void +test1 () +{ +#pragma omp unroll full /* { dg-warning "Cannot apply full unrolling to in= finite loop" } */ + for (int i =3D 101; i > 100; i++) + dummy (i); +} + + +void +test2 () +{ +#pragma omp unroll full + for (int i =3D 101; i !=3D 100; i++) + dummy (i); +} + +void +test3 () +{ +#pragma omp unroll full /* { dg-warning "Cannot apply full unrolling to in= finite loop" } */ + for (int i =3D 0; i <=3D 0; i--) + dummy (i); +} + +void +test4 () +{ +#pragma omp unroll full /* { dg-warning "Cannot apply full unrolling to in= finite loop" } */ + for (int i =3D 101; i > 100; i=3Di+2) + dummy (i); +} + +void +test5 () +{ +#pragma omp unroll full /* { dg-warning "Cannot apply full unrolling to in= finite loop" } */ + for (int i =3D -101; i < 100; i=3Di-10) + dummy (i); +} + +void +test6 () +{ +#pragma omp unroll full /* { dg-warning "Cannot apply full unrolling to in= finite loop" } */ + for (int i =3D -101; i < 100; i=3Di-300) + dummy (i); +} + +void +test7 () +{ +#pragma omp unroll full /* { dg-warning "Cannot apply full unrolling to in= finite loop" } */ + for (int i =3D 101; i > -100; i=3Di+300) + dummy (i); + + /* Loop does not iterate, hence no warning. */ +#pragma omp unroll full + for (int i =3D 101; i > 101; i=3Di+300) + dummy (i); +} + +void +test8 () +{ +#pragma omp unroll full /* { dg-warning "Cannot apply full unrolling to in= finite loop" } */ + for (int i =3D -21; i < -20; i=3Di-40) + dummy (i); + + /* Loop does not iterate, hence no warning. */ +#pragma omp unroll full + for (int i =3D -21; i > 20; i=3Di-40) + dummy (i); +} -- 2.36.1 ----------------- Siemens Electronic Design Automation GmbH; Anschrift: Arnulfstra=DFe 201, 8= 0634 M=FCnchen; Gesellschaft mit beschr=E4nkter Haftung; Gesch=E4ftsf=FChre= r: Thomas Heurung, Frank Th=FCrauf; Sitz der Gesellschaft: M=FCnchen; Regis= tergericht M=FCnchen, HRB 106955