From patchwork Wed Oct 5 15:36:48 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Botcazou X-Patchwork-Id: 1686400 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=cIP+uKqW; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4MjJdp4hzWz1yqn for ; Thu, 6 Oct 2022 02:39:09 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8AB1D3857BA3 for ; Wed, 5 Oct 2022 15:39:03 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8AB1D3857BA3 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1664984343; bh=LYSZLN+Pl3nC0B9K8f3VTR79Z+O/aPj+KjWdCjktJ+Q=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=cIP+uKqWnehoyqsiHJYUuflxBVXqjpyxwh3G7J4w62npmwMX/PGm9RGC0n0jYX1AF O07Cm7l2edZ/R+ynEahn1JuW0djnHJC8ijh6B6aKaXan/2IvXcTj+RaVOgW6NZszAb n16phQb8IkaoRoaS5j5qzcWOvim22Ib+nzI4dFtw= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org 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 Received: by mail-wr1-x433.google.com with SMTP id a3so14254078wrt.0 for ; Wed, 05 Oct 2022 08:38:43 -0700 (PDT) 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) 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 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 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: , X-Patchwork-Original-From: Eric Botcazou via Gcc-patches From: Eric Botcazou Reply-To: Eric Botcazou Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" 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. 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