From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr1-x433.google.com (mail-wr1-x433.google.com [IPv6:2a00:1450:4864:20::433]) by sourceware.org (Postfix) with ESMTPS id 5AD1B3858C54 for ; Wed, 5 Oct 2022 15:38:43 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 5AD1B3858C54 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=adacore.com Received: by mail-wr1-x433.google.com with SMTP id a3so14254078wrt.0 for ; Wed, 05 Oct 2022 08:38:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:from:to:cc:subject:date; bh=LYSZLN+Pl3nC0B9K8f3VTR79Z+O/aPj+KjWdCjktJ+Q=; b=iOUaAYkk829m6mEslV3WEJpuQzcKYW7O47mhA8ArQrWK/yxh0j5X17f89o7X3aywL3 BRgrzKbMkamDJBfaaAvejmNNNYC5yXwfaKaM3Uu3NxrNc7avFsMUBzjjLqKMlyuiDbQO xEnd57vpwh8aNYNNTHs9Mg47Bw8lxdjjixxIrETlHhHmvSquzY0uaQo1dAYXLAPAcops 1RhRaTPu/7ZwsKl96W5tA1h9K2J7XnjjBz9DYkr0li/bb4Cflf1gxYn3ineizoGOtXxd cavPumnRPP62pULqVEiZZvcRCKhc45vc7h87z7A8TXD7irW+RAWHSwFNBd46GGajIYNm cV9g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date; bh=LYSZLN+Pl3nC0B9K8f3VTR79Z+O/aPj+KjWdCjktJ+Q=; b=Ulf9zKnwAPt80c6fyxd0MuaJv7MCYmY1ZUjuF1UdAzrLqizUwp89SCU9ihWDV1u8/b IvzKZbb357cAWNR8Ezij9fareU9nM5IH5f6/Gv1vrUYjiSeNg5u1GuRQv5aCx2+O6gaP XbPGGc4Rwo5i2rYD/T8g5f6y78adowgk8g8ynL7lmH6k1jV4rLByUG5x7yqHNctWbKkm 2xM9GN93BRy1Co92QPISXjNmouKxhgM6vvjg92c1cy1lx+oISL40bRQ00KfvURIzkBEG hYjETFV+jxkEzLX55ri3IpzqxLSzph5E56DgHKI10HgjFYOI31B1a7iQkYgezrtNDuOt lwoA== X-Gm-Message-State: ACrzQf2HNHf35Cuq7ojlAxifRtTxzpTMvr4x17XZ73LwsAal0vGbJLEc nYm1ev3HiUltbW7sH17/zfrSPjaQSItHZQ== X-Google-Smtp-Source: AMsMyM5zW48vdiIjievzCYHgLE/9dnRrTmUWu1+5RUh4lL/TRewFICOi640J91ReN8L8ykpjRxygvA== X-Received: by 2002:a05:6000:1f81:b0:22c:c692:5c49 with SMTP id bw1-20020a0560001f8100b0022cc6925c49mr223320wrb.630.1664984321738; Wed, 05 Oct 2022 08:38:41 -0700 (PDT) Received: from fomalhaut.localnet ([2a01:e0a:8d5:d990:e654:e8ff:fe8f:2ce6]) by smtp.gmail.com with ESMTPSA id z17-20020a1c4c11000000b003b7b36dcb8dsm2162386wmf.31.2022.10.05.08.38.40 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Oct 2022 08:38:40 -0700 (PDT) From: Eric Botcazou X-Google-Original-From: Eric Botcazou To: gcc-patches@gcc.gnu.org Subject: [PATCH] Fix wrong code generated by unroll-and-jam pass Date: Wed, 05 Oct 2022 17:36:48 +0200 Message-ID: <4094054.1IzOArtZ34@fomalhaut> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="nextPart2600663.BddDVKsqQX" Content-Transfer-Encoding: 7Bit X-Spam-Status: No, score=-10.9 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP 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: This is a multi-part message in MIME format. --nextPart2600663.BddDVKsqQX Content-Transfer-Encoding: 7Bit Content-Type: text/plain; charset="us-ascii" Hi, as shown by the attached testcase, there is a loophole in the unroll-and-jam pass that can quickly result in wrong code generation. The code reads: if (!compute_data_dependences_for_loop (outer, true, &loop_nest, &datarefs, &dependences)) { if (dump_file && (dump_flags & TDF_DETAILS)) fprintf (dump_file, "Cannot analyze data dependencies\n"); free_data_refs (datarefs); free_dependence_relations (dependences); continue; } but compute_data_dependences_for_loop may return true even if the analysis is reported as failing by compute_affine_dependence for some dependence pair: (compute_affine_dependence ref_a: data[_14], stmt_a: data[_14] = i_59; ref_b: data[_14], stmt_b: data[_14] = i_59; Data ref a: #(Data Ref: # bb: 12 # stmt: data[_14] = i_59; # ref: data[_14]; # base_object: data; # Access function 0: scev_not_known; #) Data ref b: #(Data Ref: # bb: 12 # stmt: data[_14] = i_59; # ref: data[_14]; # base_object: data; # Access function 0: scev_not_known; #) affine dependence test not usable: access function not affine or constant. ) -> dependence analysis failed Note that this is a self-dependence pair and the code for them reads: /* Nothing interesting for the self dependencies. */ if (dra == drb) continue; This means that the pass may reorder "complex" accesses to the same memory location in successive iterations, which is OK for reads but not for writes. Proposed fix attached, tested on x86-64/Linux, OK for all active branches? 2022-10-05 Eric Botcazou * gimple-loop-jam.cc (tree_loop_unroll_and_jam): Bail out for a self dependency that is a write-after-write if the access function is not affine or constant. 2022-10-05 Eric Botcazou * gcc.c-torture/execute/20221005-1.c: New test. -- Eric Botcazou --nextPart2600663.BddDVKsqQX Content-Disposition: attachment; filename="p.diff" Content-Transfer-Encoding: 7Bit Content-Type: text/x-patch; charset="utf-8"; name="p.diff" diff --git a/gcc/gimple-loop-jam.cc b/gcc/gimple-loop-jam.cc index a8a57d3d384..4f7a6e5bbae 100644 --- a/gcc/gimple-loop-jam.cc +++ b/gcc/gimple-loop-jam.cc @@ -545,11 +545,25 @@ tree_loop_unroll_and_jam (void) /* If the refs are independend there's nothing to do. */ if (DDR_ARE_DEPENDENT (ddr) == chrec_known) continue; + dra = DDR_A (ddr); drb = DDR_B (ddr); - /* Nothing interesting for the self dependencies. */ + + /* Nothing interesting for the self dependencies, except for WAW if + the access function is not affine or constant because we may end + up reordering writes to the same location. */ if (dra == drb) - continue; + { + if (DR_IS_WRITE (dra) + && !DR_ACCESS_FNS (dra).is_empty () + && DDR_ARE_DEPENDENT (ddr) == chrec_dont_know) + { + unroll_factor = 0; + break; + } + else + continue; + } /* Now check the distance vector, for determining a sensible outer unroll factor, and for validity of merging the inner --nextPart2600663.BddDVKsqQX Content-Disposition: attachment; filename="20221005-1.c" Content-Transfer-Encoding: 7Bit Content-Type: text/x-csrc; charset="utf-8"; name="20221005-1.c" #include int main (int argc, char** argv) { const int len = argc == 2 ? atoi(argv[1]) : 4; int count; int data[64]; int M1[len][len]; int M2[len][len]; for (int i = 0; i < len; i++) for (int j = 0 ; j < len ; j++) M1[i][j] = M2[i][j] = i*len + j; M2[1][0] = M2[0][1]; /* This writes successively 0 and 1 into data[M2[0][1]]. */ for (int i = 0; i < len - 1; i++) for (int j = 0 ; j < len ; j++) if (M1[i+1][j] > M1[i][j]) data[M2[i][j]] = i; if (data [M2[0][1]] != 1) abort (); return 0; } --nextPart2600663.BddDVKsqQX--