From patchwork Wed Feb 1 12:24:44 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "juzhe.zhong@rivai.ai" X-Patchwork-Id: 51315 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp246773wrn; Wed, 1 Feb 2023 04:25:51 -0800 (PST) X-Google-Smtp-Source: AK7set8myOLlfJ1Jnv2Gt+PLmqX6dTHm0RyP/wIbgSUt1QrUZWfH0yfv5iXa+c8FS77pLe39mE28 X-Received: by 2002:a05:6402:1909:b0:49e:210a:65f3 with SMTP id e9-20020a056402190900b0049e210a65f3mr2455398edz.0.1675254351065; Wed, 01 Feb 2023 04:25:51 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675254351; cv=none; d=google.com; s=arc-20160816; b=n1T1Skz25R+JyHOHm0FWubIQcqXWMZmNuFlbHEFwgP1yW/b0wV4tMCJ9F1I1GHmhBO rkl4yXdBKQ07OvkNPpePWyRWspl+85WXdidcBGtorI/Ugt6wkNZYeDqDGQdOtS3Apkj/ Gnxw3wVC0SMXsP+EMC/JP4GxubOC8h2iXwDeDLulM8QYHiltZK4640uN8abrgNDFSNMf 2tYqEHIbzkYNQZf2IScwHKXruq+dzBQriv7G0QO57Rm8oclRAG5nI4LVZtOYe7fg29WA yRlLYiCf1eHydOww8FpLyqA6LWlmPncx/mnmWcM58RqNJdN87Pjbedc4oiL+dJ3uTNG9 5t0w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:feedback-id :content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:dmarc-filter:delivered-to; bh=L4QT+6P0slEVtfAzorR3u4dizefl4e4TBHvXrP4mvXk=; b=pOsPxnFgoKeTtbZBPKunCGkgRYJP88tpFJVmjSF2YAjhF2nLndRJKpk6chSgkjtiM0 LU/m3BZefXTwbkmoUSxpElma8dc7M2E1Q/2K/JgaCbdWGg/CssooxWvPTqzQueQxsjpL wgKYVFB06tGYxq4AcaJH0CPzf0KyayXJql5R6EQCoxwcmIsCoWbkqu/+q49AX0zz3590 e9Hdmir+cxOgk1qOXxEC2WwyWaJgeqwUjlgFWjKlE3Aw3UxqWiUgWTrH42EhK/OvXYcl Bh70MfvBzkLXKEeaxlT9ckt17uz6t4peqPb6nSSF5kjF3voXNPPCtclWusPOIr2vc60m 76Cw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id j18-20020a170906535200b0088d04f159bbsi4114907ejo.1008.2023.02.01.04.25.50 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Feb 2023 04:25:51 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 52274385B50C for ; Wed, 1 Feb 2023 12:25:39 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from smtpbg156.qq.com (smtpbg156.qq.com [15.184.82.18]) by sourceware.org (Postfix) with ESMTPS id 8330A3858C60 for ; Wed, 1 Feb 2023 12:25:00 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 8330A3858C60 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=rivai.ai Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=rivai.ai X-QQ-mid: bizesmtp77t1675254288t133siq9 Received: from server1.localdomain ( [58.60.1.22]) by bizesmtp.qq.com (ESMTP) with id ; Wed, 01 Feb 2023 20:24:47 +0800 (CST) X-QQ-SSF: 01400000000000E0L000000A0000000 X-QQ-FEAT: C46Rb8GPIEf/MMFw/9Wp+alic7bFzqnF6cP1SLmCeRmEerJDRfsa4XuVONxCT fcmJc4fppldcVr2n9YMMbu4ga7dxAV7yySZmZUG7iCHDW85KBwE8GPsAQ6Gr8mDDDpU1vZU b8rN16bAU5qt+VybBipeyUKaAYimXDoI3bREUs2HI/exlH7RDDPPm3xctrKOdrkX/K5hpWm tGFXvwz7Ms1kWRgurilaaEliU3vGRzP8Ta+gG33NNOYEUqXLz8jiTHio4XVZjobPqwg61Zt JQSjm0w5wAYnz9I64zuiMRXeaToPx71w0Rzmmt/K89BStUTWjsTp5AURmrGlv805qq+ArT6 aOsfGqiuXlY96X3sjt41I+w99y/pMF+E9SZKCUy9sWnfAGAH5BC0YMX3We4GofF9dY/l9WI 4m8LXVRRJCs= X-QQ-GoodBg: 2 From: juzhe.zhong@rivai.ai To: gcc-patches@gcc.gnu.org Cc: kito.cheng@gmail.com, richard.sandiford@arm.com, rguenther@suse.de, jeffreyalaw@gmail.com, apinski@marvell.com, Ju-Zhe Zhong Subject: [PATCH] CPROP: Allow cprop optimization when the function has a single block Date: Wed, 1 Feb 2023 20:24:44 +0800 Message-Id: <20230201122444.253620-1-juzhe.zhong@rivai.ai> X-Mailer: git-send-email 2.36.1 MIME-Version: 1.0 X-QQ-SENDSIZE: 520 Feedback-ID: bizesmtp:rivai.ai:qybglogicsvr:qybglogicsvr7 X-Spam-Status: No, score=-10.7 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, RCVD_IN_BARRACUDACENTRAL, RCVD_IN_DNSWL_NONE, SPF_HELO_PASS, TXREP, T_SPF_TEMPERROR 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+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1756631506713325832?= X-GMAIL-MSGID: =?utf-8?q?1756631506713325832?= From: Ju-Zhe Zhong Hi, this patch is present for GCC 14 since I understand it's not appropriate to land it in GCC 13. NUM_FIXED_BLOCKS = 2 since GCC define each function has aleast 2 blocks one is entry block, the other is exit block. So according this code, the function will not do cprop optimization when there is only exactly one single block. I am not sure whether it's correct to fix it like this. Can anyone tell me why forbid cprop optimization if the function only has s single block ? Let's take a look at these 2 examples of RVV intrinsics: 1. void f1 (void * in, void *out, int64_t x, int n) { vint64m1_t v = __riscv_vle64_v_i64m1 (in + 1, 4); vint64m1_t v2 = __riscv_vle64_v_i64m1_tu (v, in + 2, 4); vint64m1_t v3 = __riscv_vadd_vx_i64m1 (v2, x, 4); vint64m1_t v4 = __riscv_vadd_vx_i64m1 (v3, x, 4); __riscv_vse64_v_i64m1 (out + 2, v4, 4); } asm: addi sp,sp,-16 sw a2,8(sp) sw a3,12(sp) sw a2,0(sp) sw a3,4(sp) addi a5,a0,1 vsetivli zero,4,e64,m1,ta,ma addi a0,a0,2 vle64.v v24,0(a5) addi a5,sp,8 vlse64.v v27,0(a5),zero addi a1,a1,2 vsetvli zero,zero,e64,m1,tu,ma vle64.v v24,0(a0) vsetvli zero,zero,e64,m1,ta,ma vlse64.v v25,0(sp),zero vadd.vv v26,v24,v27 vadd.vv v24,v26,v25 vse64.v v24,0(a1) addi sp,sp,16 jr ra you can see here there are 2 vlse64.v instructions that broadcast the scalar value "x" GCC fail to eliminate the second vlse64.v instruction since GCC doesn't do the cprop optimization (the function only has 1 single block). It can be optimized if we apply this patch. 2. void f1 (void * in, void *out, int64_t x, int n) { if (n) { vint64m1_t v = __riscv_vle64_v_i64m1 (in + 1, 4); vint64m1_t v2 = __riscv_vle64_v_i64m1_tu (v, in + 2, 4); vint64m1_t v3 = __riscv_vadd_vx_i64m1 (v2, x, 4); vint64m1_t v4 = __riscv_vadd_vx_i64m1 (v3, x, 4); __riscv_vse64_v_i64m1 (out + 2, v4, 4); } } asm: f1: vsetivli zero,4,e64,m1,ta,ma beq a4,zero,.L7 addi sp,sp,-16 sw a2,8(sp) sw a3,12(sp) addi a5,a0,1 vle64.v v24,0(a5) addi a0,a0,2 addi a5,sp,8 vlse64.v v25,0(a5),zero addi a1,a1,2 vsetvli zero,zero,e64,m1,tu,ma vle64.v v24,0(a0) vadd.vv v26,v24,v25 vadd.vv v24,v26,v25 vse64.v v24,0(a1) addi sp,sp,16 jr ra .L7: ret Here, if we add if (n) condition here, the program will end up with more than 1 block. So GCC will do the cprop optimization and the second vlse64.v instruction is eliminated. I am not sure whether this patch is correct. Can anyone help me with that ? Thanks. gcc/ChangeLog: * cprop.cc (one_cprop_pass): Remove +1. --- gcc/cprop.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gcc/cprop.cc b/gcc/cprop.cc index 6ec0bda4a24..615bc5078b6 100644 --- a/gcc/cprop.cc +++ b/gcc/cprop.cc @@ -1749,7 +1749,7 @@ one_cprop_pass (void) int changed = 0; /* Return if there's nothing to do, or it is too expensive. */ - if (n_basic_blocks_for_fn (cfun) <= NUM_FIXED_BLOCKS + 1 + if (n_basic_blocks_for_fn (cfun) <= NUM_FIXED_BLOCKS || gcse_or_cprop_is_too_expensive (_ ("const/copy propagation disabled"))) return 0;