From patchwork Fri Jul 12 05:23:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: liuhongt X-Patchwork-Id: 1959674 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=gNAkUnM9; 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 4WL0Pm5k2Fz1xqj for ; Fri, 12 Jul 2024 15:23:56 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id F2088385DDEA for ; Fri, 12 Jul 2024 05:23:54 +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.15]) by sourceware.org (Postfix) with ESMTPS id B41D73858D39 for ; Fri, 12 Jul 2024 05:23:33 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B41D73858D39 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 B41D73858D39 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=198.175.65.15 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1720761816; cv=none; b=AvFdcwIIoyo28PQcWgIXKiX8vVExB7/kWNiEamBvu0scnkvsfVX5UZ2yxDu8c7dW3Pg/z6PwcuqFUn6KZttAdoMmzHNJ4Ad7WbPqffsLFo5DscAGlRa2avULgZLIpdjWUCBTPh5FvUj3jnpZX4FsqRIaQwi5EpGXkDz/6kSLcqY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1720761816; c=relaxed/simple; bh=4TVyNIZkQU8wv3VSBqm4uX73O15vCqP5mVeiSxzXqWI=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=AV0fGIzrC9RWh+KFh6FyXXfDn96+DooYZMw9KfEwp/bC+0PoP4AQuRFPf23b/EyLbjlrQfUV4XAksSJ3EKWTFETcs9xqelCPdYHRwQqWC3szQjlEmlufAKdCmtU14Z1jFtXi+tGsp3+4liog2+Y6d7ogyfe1C9ZV/jKASNaL1xg= 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=1720761814; x=1752297814; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=4TVyNIZkQU8wv3VSBqm4uX73O15vCqP5mVeiSxzXqWI=; b=gNAkUnM9T4MmctjVFKsmhg7VBdroIk7MEkCiXU195t9JzlJkvE4hThpk E6DjiVP4T6H4nH+CaygkxlEgtNz8ryZLHiYP5WeHVsQ0wMPYTfUex8wp7 D3C0nf1JEAFGYNnL4t4skb+/nifCZ1PmYigGu9+nQFlq0BoeqWfk9c8S3 1Pd8vRnnznbIzHAC3nArNf3BmLciSd/ew5aPb+7KP+q3GPXskUmU+ZQfd VoZTdaP7/1BbCXTkuCAaXsJIk43OR+bSFRr3UW9Uc2jHa8ipoQYixML5A B46LQfs+TULMN50lEoc4XINp3l3YNhfrMRpeCvSsG0ccG0LqBm3nFV6OZ Q==; X-CSE-ConnectionGUID: k+CfD767SCK/M2raTHHhtw== X-CSE-MsgGUID: W3p3IXdtSfqHv3ODgBj6rA== X-IronPort-AV: E=McAfee;i="6700,10204,11130"; a="21947301" X-IronPort-AV: E=Sophos;i="6.09,202,1716274800"; d="scan'208";a="21947301" Received: from fmviesa001.fm.intel.com ([10.60.135.141]) by orvoesa107.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 Jul 2024 22:23:32 -0700 X-CSE-ConnectionGUID: 67dk5mHdQKWsWn58W9GaoQ== X-CSE-MsgGUID: 7thnUb4USW2VrrAianL4KA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.09,202,1716274800"; d="scan'208";a="79916916" Received: from shvmail03.sh.intel.com ([10.239.245.20]) by fmviesa001.fm.intel.com with ESMTP; 11 Jul 2024 22:23:30 -0700 Received: from shliclel4217.sh.intel.com (shliclel4217.sh.intel.com [10.239.240.127]) by shvmail03.sh.intel.com (Postfix) with ESMTP id 85D661005698; Fri, 12 Jul 2024 13:23:29 +0800 (CST) From: liuhongt To: gcc-patches@gcc.gnu.org Cc: crazylht@gmail.com, hjl.tools@gmail.com Subject: [PATCH] Fix SSA_NAME leak due to def_stmt is removed before use_stmt. Date: Fri, 12 Jul 2024 13:23:29 +0800 Message-Id: <20240712052329.116815-1-hongtao.liu@intel.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Spam-Status: No, score=-12.2 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, SPF_HELO_NONE, SPF_NONE, 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.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 >- _5 = __atomic_fetch_or_8 (&set_work_pending_p, 1, 0); >- # DEBUG old => (long int) _5 >+ _6 = .ATOMIC_BIT_TEST_AND_SET (&set_work_pending_p, 0, 1, 0, __atomic_fetch_or_8); >+ # DEBUG old => NULL > # DEBUG BEGIN_STMT >- # DEBUG D#2 => _5 & 1 >+ # DEBUG D#2 => NULL >... >- _10 = ~_5; >- _8 = (_Bool) _10; >- # DEBUG ret => _8 >+ _8 = _6 == 0; >+ # DEBUG ret => (_Bool) _10 > >confirmed. convert_atomic_bit_not does this, it checks for single_use >and removes the def, failing to release the name (which would fix this up >IIRC). > >Note the function removes stmts in "wrong" order (before uses of LHS >are removed), so it requires larger surgery. And it leaks SSA names. Bootstrapped and regtested on x86_64-pc-linux-gnu{-m32,}. Ok for trunk? gcc/ChangeLog: PR target/115872 * tree-ssa-ccp.cc (convert_atomic_bit_not): Remove use_stmt after use_nop_stmt is removed. (optimize_atomic_bit_test_and): Ditto. gcc/testsuite/ChangeLog: * gcc.target/i386/pr115872.c: New test. --- gcc/testsuite/gcc.target/i386/pr115872.c | 16 ++++++++++++++++ gcc/tree-ssa-ccp.cc | 12 ++++++++---- 2 files changed, 24 insertions(+), 4 deletions(-) create mode 100644 gcc/testsuite/gcc.target/i386/pr115872.c diff --git a/gcc/testsuite/gcc.target/i386/pr115872.c b/gcc/testsuite/gcc.target/i386/pr115872.c new file mode 100644 index 00000000000..937004456d3 --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/pr115872.c @@ -0,0 +1,16 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -g" } */ + +long set_work_pending_p; +_Bool set_work_pending() { + _Bool __trans_tmp_1; + long mask = 1, old = __atomic_fetch_or(&set_work_pending_p, mask, 0); + __trans_tmp_1 = old & mask; + return !__trans_tmp_1; +} +void __queue_work() { + _Bool ret = set_work_pending(); + if (ret) + __queue_work(); +} + diff --git a/gcc/tree-ssa-ccp.cc b/gcc/tree-ssa-ccp.cc index 3749126b5f7..de83d26d311 100644 --- a/gcc/tree-ssa-ccp.cc +++ b/gcc/tree-ssa-ccp.cc @@ -3332,9 +3332,10 @@ convert_atomic_bit_not (enum internal_fn fn, gimple *use_stmt, return nullptr; gimple_stmt_iterator gsi; - gsi = gsi_for_stmt (use_stmt); - gsi_remove (&gsi, true); tree var = make_ssa_name (TREE_TYPE (lhs)); + /* use_stmt need to be removed after use_nop_stmt, + so use_lhs can be released. */ + gimple *use_stmt_removal = use_stmt; use_stmt = gimple_build_assign (var, BIT_AND_EXPR, lhs, and_mask); gsi = gsi_for_stmt (use_not_stmt); gsi_insert_before (&gsi, use_stmt, GSI_NEW_STMT); @@ -3344,6 +3345,8 @@ convert_atomic_bit_not (enum internal_fn fn, gimple *use_stmt, gsi_insert_after (&gsi, g, GSI_NEW_STMT); gsi = gsi_for_stmt (use_not_stmt); gsi_remove (&gsi, true); + gsi = gsi_for_stmt (use_stmt_removal); + gsi_remove (&gsi, true); return use_stmt; } @@ -3646,8 +3649,7 @@ optimize_atomic_bit_test_and (gimple_stmt_iterator *gsip, */ } var = make_ssa_name (TREE_TYPE (use_rhs)); - gsi = gsi_for_stmt (use_stmt); - gsi_remove (&gsi, true); + gimple* use_stmt_removal = use_stmt; g = gimple_build_assign (var, BIT_AND_EXPR, use_rhs, and_mask); gsi = gsi_for_stmt (use_nop_stmt); @@ -3664,6 +3666,8 @@ optimize_atomic_bit_test_and (gimple_stmt_iterator *gsip, gsi_insert_after (&gsi, g, GSI_NEW_STMT); gsi = gsi_for_stmt (use_nop_stmt); gsi_remove (&gsi, true); + gsi = gsi_for_stmt (use_stmt_removal); + gsi_remove (&gsi, true); } } else