From patchwork Thu Nov 18 07:37:26 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hongyu Wang X-Patchwork-Id: 1556501 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.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=Ufuwu5fU; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Received: from 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 RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4Hvs8g0kCqz9s1l for ; Thu, 18 Nov 2021 18:37:53 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 14B723858420 for ; Thu, 18 Nov 2021 07:37:51 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 14B723858420 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1637221071; bh=c7mWFZVRcbK1zfvcoCMEcp6VsCgSD964GSrNqx89O6A=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=Ufuwu5fU9s79kudEYreAooq86s8uzkjgyNnTqkW8nXoHuE9q8GAFlW+zhEh4dSEtT qfeVauH9Chm540AWP2iTvXyN8z+XJXUlqfC/6adFqI+Gi+Pqam1fc8Bizzo2j/DG0s 6d3b7DI8ly2H1MZ3glkE6SCAtau4eE2GqSPM43WU= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mga18.intel.com (mga18.intel.com [134.134.136.126]) by sourceware.org (Postfix) with ESMTPS id 867193858C2C for ; Thu, 18 Nov 2021 07:37:30 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 867193858C2C X-IronPort-AV: E=McAfee;i="6200,9189,10171"; a="221013996" X-IronPort-AV: E=Sophos;i="5.87,243,1631602800"; d="scan'208";a="221013996" Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by orsmga106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Nov 2021 23:37:29 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.87,243,1631602800"; d="scan'208";a="568378041" Received: from scymds01.sc.intel.com ([10.148.94.138]) by fmsmga004.fm.intel.com with ESMTP; 17 Nov 2021 23:37:28 -0800 Received: from shliclel320.sh.intel.com (shliclel320.sh.intel.com [10.239.236.50]) by scymds01.sc.intel.com with ESMTP id 1AI7bQju003050; Wed, 17 Nov 2021 23:37:27 -0800 To: ubizjak@gmail.com Subject: [PATCH] i386: Fix wrong codegen for -mrelax-cmpxchg-loop Date: Thu, 18 Nov 2021 15:37:26 +0800 Message-Id: <20211118073726.21865-1-hongyu.wang@intel.com> X-Mailer: git-send-email 2.18.1 X-Spam-Status: No, score=-10.3 required=5.0 tests=BAYES_00, FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM, GIT_PATCH_0, HEADER_FROM_DIFFERENT_DOMAINS, KAM_DMARC_NONE, KAM_DMARC_STATUS, KAM_SHORT, SPF_HELO_NONE, SPF_SOFTFAIL, SPOOFED_FREEMAIL, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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: Hongyu Wang via Gcc-patches From: Hongyu Wang Reply-To: Hongyu Wang Cc: hongtao.liu@intel.com, gcc-patches@gcc.gnu.org Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" Hi Uros, For -mrelax-cmpxchg-loop introduced by PR 103069/r12-5265, it would produce infinite loop. The correct code should be .L84: movl (%rdi), %ecx movl %eax, %edx orl %esi, %edx cmpl %eax, %ecx jne .L82 lock cmpxchgl %edx, (%rdi) jne .L84 movl %r8d, %eax <<< retval is missing in previous impl ret .L82: rep nop jmp .L84 Adjust corresponding expander to fix such issue, and fix runtime test so the problem would be exposed. Bootstrapped/regtested on x86_64-pc-linux-gnu{-m32,}. Ok for master? gcc/ChangeLog: * config/i386/i386-expand.c (ix86_expand_atomic_fetch_op_loop): Adjust generated cfg to avoid infinite loop. gcc/testsuite/ChangeLog: * gcc.target/i386/pr103069-2.c: Adjust. --- gcc/config/i386/i386-expand.c | 7 ++++++- gcc/testsuite/gcc.target/i386/pr103069-2.c | 11 ++++++----- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/gcc/config/i386/i386-expand.c b/gcc/config/i386/i386-expand.c index 3e4de64ec24..0d5d1a0e205 100644 --- a/gcc/config/i386/i386-expand.c +++ b/gcc/config/i386/i386-expand.c @@ -23143,13 +23143,14 @@ void ix86_expand_atomic_fetch_op_loop (rtx target, rtx mem, rtx val, bool doubleword) { rtx old_reg, new_reg, old_mem, success, oldval, new_mem; - rtx_code_label *loop_label, *pause_label; + rtx_code_label *loop_label, *pause_label, *done_label; machine_mode mode = GET_MODE (target); old_reg = gen_reg_rtx (mode); new_reg = old_reg; loop_label = gen_label_rtx (); pause_label = gen_label_rtx (); + done_label = gen_label_rtx (); old_mem = copy_to_reg (mem); emit_label (loop_label); emit_move_insn (old_reg, old_mem); @@ -23207,11 +23208,15 @@ void ix86_expand_atomic_fetch_op_loop (rtx target, rtx mem, rtx val, GET_MODE (success), 1, loop_label, profile_probability::guessed_never ()); + emit_jump_insn (gen_jump (done_label)); + emit_barrier (); + /* If mem is not expected, pause and loop back. */ emit_label (pause_label); emit_insn (gen_pause ()); emit_jump_insn (gen_jump (loop_label)); emit_barrier (); + emit_label (done_label); } #include "gt-i386-expand.h" diff --git a/gcc/testsuite/gcc.target/i386/pr103069-2.c b/gcc/testsuite/gcc.target/i386/pr103069-2.c index 8ac824cc8e8..b3f2235fd55 100644 --- a/gcc/testsuite/gcc.target/i386/pr103069-2.c +++ b/gcc/testsuite/gcc.target/i386/pr103069-2.c @@ -1,5 +1,5 @@ -/* PR target/103068 */ -/* { dg-do compile } */ +/* PR target/103069 */ +/* { dg-do run } */ /* { dg-additional-options "-O2 -march=x86-64 -mtune=generic" } */ #include @@ -37,13 +37,14 @@ FUNC_ATOMIC_RELAX (char, xor) #define TEST_ATOMIC_FETCH_LOGIC(TYPE, OP) \ { \ TYPE a = 11, b = 101, res, exp; \ + TYPE c = 11, d = 101; \ res = relax_##TYPE##_##OP##_fetch (&a, b); \ - exp = f_##TYPE##_##OP##_fetch (&a, b); \ + exp = f_##TYPE##_##OP##_fetch (&c, d); \ if (res != exp) \ abort (); \ - a = 21, b = 92; \ + a = c = 21, b = d = 92; \ res = relax_##TYPE##_fetch_##OP (&a, b); \ - exp = f_##TYPE##_fetch_##OP (&a, b); \ + exp = f_##TYPE##_fetch_##OP (&c, d); \ if (res != exp) \ abort (); \ }