From patchwork Fri Sep 27 07:51:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: "Li, Pan2" X-Patchwork-Id: 1990176 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=AiRWhKED; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4XFN434sLxz1xt9 for ; Fri, 27 Sep 2024 17:52:51 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 508CC3858430 for ; Fri, 27 Sep 2024 07:52:48 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.10]) by sourceware.org (Postfix) with ESMTPS id AC2383858D39 for ; Fri, 27 Sep 2024 07:52:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org AC2383858D39 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=intel.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org AC2383858D39 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=198.175.65.10 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1727423550; cv=none; b=SFG6YKovblEkxVJfE51izFkPXkov4kwdrMqqzqlkrMDYJuvOK4PLJbPeGO0KE8Ej4vVHl3uaC1cHvDH3+OCQSb/3Oj/XPdaFm356K2vFRGI7IhFrX+8dJogyhxyJ+LerfwsM8cTY9zDfOAkO5UkMESo8Samr9w2jyZ+ywXqF+4A= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1727423550; c=relaxed/simple; bh=HK7yTt5Fb/cdmRDPSMAS8bSpDkkIvyUdB4PzMHeCp38=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=NBPukdMetcCJQZ4DJ4MtAkx1OpkFw/a9Nv79eP92xsvEip9jPCeKDIJpeE9p3iVNToXAPbRRlH4c91rDCDAA6F38175byjfwIn9JskIyjsSb47pJHH9mbO57i4KHY4rl8xW2OOw0kxnq7Nh+WGfHYb0WuLvzfHC2gXPPsGyxh/Y= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1727423548; x=1758959548; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=HK7yTt5Fb/cdmRDPSMAS8bSpDkkIvyUdB4PzMHeCp38=; b=AiRWhKEDGSXAJLYh4BBqmyCD97r82oHunQP66Mb6Op+W5sR60sGrCqOC LeZm3Zj5PRi4VbrqJInBAcj7V38n7K+2M4GnWQnZWD4ZkWkwg6gjd6muo c0L5AcU/NfIJ+wXbHuZXW1YhTmCkJ3LXsXcC4Dl9xXVZ4sKWk+ksc22nb WO0hItHEix2IQusHyt27VFxD2FEp2UC5AJoAeDLv4YoKhjOGY7Ck+35ga ibGoh4Bop1vGaXo+DtzS9+Q1SuMS2I5OGficdpM+tdi/cz+FZgBk0dBOp 0KplW9EvNfe+LleBfIbsRlLnTf056eu5UnHjy4Mq6I5cEX9kz4aeUNfRT A==; X-CSE-ConnectionGUID: OsG51ZrhQnqnDn+mkgMRGA== X-CSE-MsgGUID: hcMRsgRvSWiJxVfEs4aiuw== X-IronPort-AV: E=McAfee;i="6700,10204,11207"; a="44027801" X-IronPort-AV: E=Sophos;i="6.11,158,1725346800"; d="scan'208";a="44027801" Received: from orviesa002.jf.intel.com ([10.64.159.142]) by orvoesa102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Sep 2024 00:52:26 -0700 X-CSE-ConnectionGUID: /gSpn2rpTtiffiWfqt/Guw== X-CSE-MsgGUID: 2UPQv4pURliMQaVecKBdqA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,158,1725346800"; d="scan'208";a="103241038" Received: from panli.sh.intel.com ([10.239.154.73]) by orviesa002.jf.intel.com with ESMTP; 27 Sep 2024 00:52:24 -0700 From: pan2.li@intel.com To: gcc-patches@gcc.gnu.org Cc: richard.guenther@gmail.com, Tamar.Christina@arm.com, juzhe.zhong@rivai.ai, kito.cheng@gmail.com, jeffreyalaw@gmail.com, rdapp.gcc@gmail.com, Pan Li Subject: [PATCH v1] Widening-Mul: Fix one ICE when iterate on phi node Date: Fri, 27 Sep 2024 15:51:11 +0800 Message-ID: <20240927075111.208305-1-pan2.li@intel.com> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 X-Spam-Status: No, score=-11.6 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_NONE, SPF_NONE, TXREP, WEIRD_PORT 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org From: Pan Li We iterate all phi node of bb to try to match the SAT_* pattern for scalar integer. We also remove the phi mode when the relevant pattern matched. Unfortunately the iterator may have no idea the phi node is removed and continue leverage the free data and then ICE similar as below. [0] psi ptr 0x75216340c000 [0] psi ptr 0x75216340c400 [1] psi ptr 0xa5a5a5a5a5a5a5a5 <=== GC freed pointer. during GIMPLE pass: widening_mul tmp.c: In function ‘f’: tmp.c:45:6: internal compiler error: Segmentation fault 45 | void f(int rows, int cols) { | ^ 0x36e2788 internal_error(char const*, ...) ../../gcc/diagnostic-global-context.cc:517 0x18005f0 crash_signal ../../gcc/toplev.cc:321 0x752163c4531f ??? ./signal/../sysdeps/unix/sysv/linux/x86_64/libc_sigaction.c:0 0x103ae0e bool is_a_helper::test(gimple*) ../../gcc/gimple.h:1256 0x103f9a5 bool is_a(gimple*) ../../gcc/is-a.h:232 0x103dc78 gphi* as_a(gimple*) ../../gcc/is-a.h:255 0x104f12e gphi_iterator::phi() const ../../gcc/gimple-iterator.h:47 0x1a57bef after_dom_children ../../gcc/tree-ssa-math-opts.cc:6140 0x3344482 dom_walker::walk(basic_block_def*) ../../gcc/domwalk.cc:354 0x1a58601 execute ../../gcc/tree-ssa-math-opts.cc:6312 This patch would like to fix the iterate on modified collection problem by backup the next phi in advance. The below test suites are passed for this patch. * The rv64gcv fully regression test. * The x86 bootstrap test. * The x86 fully regression test. PR middle-end/116861 gcc/ChangeLog: * tree-ssa-math-opts.cc (math_opts_dom_walker::after_dom_children): Backup the next psi iterator before remove the phi node. gcc/testsuite/ChangeLog: * gcc.dg/torture/pr116861-1.c: New test. Signed-off-by: Pan Li --- gcc/testsuite/gcc.dg/torture/pr116861-1.c | 76 +++++++++++++++++++++++ gcc/tree-ssa-math-opts.cc | 9 ++- 2 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/torture/pr116861-1.c diff --git a/gcc/testsuite/gcc.dg/torture/pr116861-1.c b/gcc/testsuite/gcc.dg/torture/pr116861-1.c new file mode 100644 index 00000000000..7dcfe664d89 --- /dev/null +++ b/gcc/testsuite/gcc.dg/torture/pr116861-1.c @@ -0,0 +1,76 @@ +/* { dg-do compile } */ +/* { dg-options "-O2" } */ + +void pm_message(void); +struct CmdlineInfo { + _Bool wantCrop[4]; + unsigned int margin; +}; +typedef struct { + unsigned int removeSize; +} CropOp; +typedef struct { + CropOp op[4]; +} CropSet; +static void divideAllBackgroundIntoBorders(unsigned int const totalSz, + _Bool const wantCropSideA, + _Bool const wantCropSideB, + unsigned int const wantMargin, + unsigned int *const sideASzP, + unsigned int *const sideBSzP) { + unsigned int sideASz, sideBSz; + if (wantCropSideA && wantCropSideB) + { + sideASz = totalSz / 2; + if (wantMargin) + sideBSz = totalSz - sideASz; + } + else if (wantCropSideB) + { + sideBSz = 0; + } + *sideASzP = sideASz; + *sideBSzP = sideBSz; +} +static CropOp oneSideCrop(_Bool const wantCrop, unsigned int const borderSz, + unsigned int const margin) { + CropOp retval; + if (wantCrop) + { + if (borderSz >= margin) + retval.removeSize = borderSz - margin; + else + retval.removeSize = 0; + } + return retval; +} +struct CmdlineInfo cmdline1; +void f(int rows, int cols) { + struct CmdlineInfo cmdline0 = cmdline1; + CropSet crop; + struct CmdlineInfo cmdline = cmdline0; + CropSet retval; + unsigned int leftBorderSz, rghtBorderSz; + unsigned int topBorderSz, botBorderSz; + divideAllBackgroundIntoBorders(cols, cmdline.wantCrop[0], + cmdline.wantCrop[1], cmdline.margin > 0, + &leftBorderSz, &rghtBorderSz); + divideAllBackgroundIntoBorders(rows, cmdline.wantCrop[2], + cmdline.wantCrop[3], cmdline.margin > 0, + &topBorderSz, &botBorderSz); + retval.op[0] = + oneSideCrop(cmdline.wantCrop[0], leftBorderSz, cmdline.margin); + retval.op[1] = + oneSideCrop(cmdline.wantCrop[1], rghtBorderSz, cmdline.margin); + retval.op[2] = + oneSideCrop(cmdline.wantCrop[2], topBorderSz, cmdline.margin); + retval.op[3] = + oneSideCrop(cmdline.wantCrop[3], botBorderSz, cmdline.margin); + crop = retval; + unsigned int i = 0; + for (i = 0; i < 4; ++i) + { + if (crop.op[i].removeSize == 0) + pm_message(); + } +} diff --git a/gcc/tree-ssa-math-opts.cc b/gcc/tree-ssa-math-opts.cc index 8c622514dbd..f1cfe7dfab0 100644 --- a/gcc/tree-ssa-math-opts.cc +++ b/gcc/tree-ssa-math-opts.cc @@ -6129,10 +6129,15 @@ math_opts_dom_walker::after_dom_children (basic_block bb) fma_deferring_state fma_state (param_avoid_fma_max_bits > 0); - for (gphi_iterator psi = gsi_start_phis (bb); !gsi_end_p (psi); - gsi_next (&psi)) + for (gphi_iterator psi_next, psi = gsi_start_phis (bb); !gsi_end_p (psi); + psi = psi_next) { + psi_next = psi; + gsi_next (&psi_next); + gimple_stmt_iterator gsi = gsi_after_labels (bb); + + /* The match_* may remove phi node. */ match_saturation_add (&gsi, psi.phi ()); match_unsigned_saturation_sub (&gsi, psi.phi ()); }