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: 1738 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4ac7:0:0:0:0:0 with SMTP id y7csp663040wrs; Wed, 5 Oct 2022 08:39:28 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7Kl1Z32K1aD1topgkdM0b8L2YGKSs8WUhc/hxBR6LnWJNfZhKWZhlQr5b6dqTQH985KUV2 X-Received: by 2002:a05:6402:24a0:b0:454:d197:6238 with SMTP id q32-20020a05640224a000b00454d1976238mr349850eda.274.1664984367827; Wed, 05 Oct 2022 08:39:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664984367; cv=none; d=google.com; s=arc-20160816; b=ynuLn5iXRThWHkbKWPzng7dVjucYd3ZoMzWryr8DrYPI17C26/0hSdqcjvyLrsIzmD eGJvIhTktLgboVgepWW6sH3hZysxhZHC+FxI/CKQ7cWdsRcNXEUL2dobDzv59MmwfeZA np18y0T2dRBlTlbg5w9fqCT2w6qGeGqzlCrnSqbrPgnEPirvyZs7zOH/c76h/5ZJYNJI J2IjFSvCGqnEbSEOrHU321+Yo+EU77kaSRENyC7S00IShSB/VIQC3TueX8lw25+7YAfQ bhJT2xLqZK6R0sWnELs3xr0Z/5uPjfBzeuTLMbEIsT9sAYS1wUlj6AXnE8sPWyNAM/dC vhWQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=LYSZLN+Pl3nC0B9K8f3VTR79Z+O/aPj+KjWdCjktJ+Q=; b=zELdPAo2f9jYYIB1k3WZd4DSkSvOv03RlRWCpPa3SBgHtIuPYV35qEWv4moF/sWWkU Y6WVqEUpRC8oICYEWzJcX8svjj1TPVYuSdOduWntWM5apcUU+TC2U04fPANqIkAxNQIY vxKd+KlVhB0Vs84w3m+ztYzCrsLxdn/y1zq4yRb+IRb2unYQ9qHI3XQmvN1OffOczqW/ dItoiHy6wtD6MD6QW4SVbFVXFtkOnnql4wzzQnEeffLgllIk70sU+kF8/odFGeB6LWr1 NEeVi6aQZoy6SdsyHst5ZWUh30ER6INd6gHcVXV3Hqmmlv0WRsgzwMzy7U5YaEznUqKS 1VUg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=H4Y1ZWfR; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id l9-20020a50cbc9000000b00458abbeb000si10917431edi.300.2022.10.05.08.39.27 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Oct 2022 08:39:27 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=H4Y1ZWfR; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C6FFD3857357 for ; Wed, 5 Oct 2022 15:39:26 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C6FFD3857357 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1664984366; 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=H4Y1ZWfRR5N7Dx9a56T+cmJTxo8nkQqJNul99kua9pwfiIv4ngBO7E1wN6bRGhqEj Tm/eF36TsgSJeN1+5mVevAL4pVLRH0E/tT3LtYlEzQkJn7bL8RuHRhDpKEt3Kjlyay OndS4YYeECiuQcW5dgW8hfLjKQ4FaByMVoZol+G8= 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-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+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1745862648459984574?= X-GMAIL-MSGID: =?utf-8?q?1745862648459984574?= 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