From patchwork Fri Jul 28 13:04:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Frederik Harwath X-Patchwork-Id: 73328 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 2EAF7384F4AA for ; Fri, 28 Jul 2023 13:05:25 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org 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 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 X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" 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 -- 2.36.1 ----------------- Siemens Electronic Design Automation GmbH; Anschrift: Arnulfstraße 201, 80634 München; Gesellschaft mit beschränkter Haftung; Geschäftsführer: Thomas Heurung, Frank Thürauf; Sitz der Gesellschaft: München; Registergericht München, HRB 106955 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 = gimple_omp_for_initial (omp_for, level); tree final = gimple_omp_for_final (omp_for, level); tree_code cond = gimple_omp_for_cond (omp_for, level); tree index = gimple_omp_for_index (omp_for, level); tree type = gomp_for_iter_count_type (index, final); - tree step = TREE_OPERAND (gimple_omp_for_incr (omp_for, level), 1); + tree incr = gimple_omp_for_incr (omp_for, level); + tree step = omp_get_for_step_from_incr (gimple_location (omp_for), incr); init = subst_defs (init, gimple_omp_for_pre_body (omp_for)); init = fold (init); @@ -181,34 +188,64 @@ gomp_for_number_of_iterations (const gomp_for *omp_for, size_t level) diff_type = ptrdiff_type_node; } - tree diff; - if (cond == GT_EXPR) - diff = fold_build2 (minus_code, diff_type, init, final); - else if (cond == LT_EXPR) - diff = fold_build2 (minus_code, diff_type, final, init); - else - gcc_unreachable (); - diff = fold_build2 (CEIL_DIV_EXPR, type, diff, step); - diff = 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 == GT_EXPR || cond == LT_EXPR); + if (TREE_CONSTANT (init) && TREE_CONSTANT (final) + && ((cond == GT_EXPR && tree_int_cst_le (init, final)) + || (cond == LT_EXPR && tree_int_cst_le (final, init)))) + return build_int_cst (diff_type, 0); + + tree diff = 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 = CEIL_DIV_EXPR; + if (TREE_CONSTANT (diff) && TREE_CONSTANT (step)) + { + bool diff_is_neg = tree_int_cst_lt (diff, size_zero_node); + bool step_is_neg = tree_int_cst_lt (step, size_zero_node); + if ((diff_is_neg && !step_is_neg) + || (!diff_is_neg && step_is_neg)) + div_expr = FLOOR_DIV_EXPR; + } + diff = 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 = NULL) { tree t = gomp_for_number_of_iterations (omp_for, 0); - if (!TREE_CONSTANT (t) - || !tree_fits_uhwi_p (t)) + if (!TREE_CONSTANT (t)) return false; - *iterations = tree_to_uhwi (t); - return true; + if (infinite && + tree_int_cst_lt (t, size_zero_node)) + *infinite = true; + else if (tree_fits_uhwi_p (t)) + { + *iterations = 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 = gimple_omp_for_initial (omp_for, 0); unsigned HOST_WIDE_INT niter = 0; - if (!gomp_for_constant_iterations_p (omp_for, &niter)) + bool infinite = false; + bool constant = gomp_for_constant_iterations_p (omp_for, &niter, &infinite); + + 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 *containing_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/gcc/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 infinite loop" } */ + for (int i = 101; i > 100; i++) + dummy (i); +} + + +void +test2 () +{ +#pragma omp unroll full + for (int i = 101; i != 100; i++) + dummy (i); +} + +void +test3 () +{ +#pragma omp unroll full /* { dg-warning "Cannot apply full unrolling to infinite loop" } */ + for (int i = 0; i <= 0; i--) + dummy (i); +} + +void +test4 () +{ +#pragma omp unroll full /* { dg-warning "Cannot apply full unrolling to infinite loop" } */ + for (int i = 101; i > 100; i=i+2) + dummy (i); +} + +void +test5 () +{ +#pragma omp unroll full /* { dg-warning "Cannot apply full unrolling to infinite loop" } */ + for (int i = -101; i < 100; i=i-10) + dummy (i); +} + +void +test6 () +{ +#pragma omp unroll full /* { dg-warning "Cannot apply full unrolling to infinite loop" } */ + for (int i = -101; i < 100; i=i-300) + dummy (i); +} + +void +test7 () +{ +#pragma omp unroll full /* { dg-warning "Cannot apply full unrolling to infinite loop" } */ + for (int i = 101; i > -100; i=i+300) + dummy (i); + + /* Loop does not iterate, hence no warning. */ +#pragma omp unroll full + for (int i = 101; i > 101; i=i+300) + dummy (i); +} + +void +test8 () +{ +#pragma omp unroll full /* { dg-warning "Cannot apply full unrolling to infinite loop" } */ + for (int i = -21; i < -20; i=i-40) + dummy (i); + + /* Loop does not iterate, hence no warning. */ +#pragma omp unroll full + for (int i = -21; i > 20; i=i-40) + dummy (i); +}