From patchwork Wed Nov 1 16:14:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick O'Neill X-Patchwork-Id: 1858007 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=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.a=rsa-sha256 header.s=20230601 header.b=rmifLtP0; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; 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 [8.43.85.97]) (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 4SLBtX6bprz1yQ4 for ; Thu, 2 Nov 2023 03:15:15 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 6214A3858C5E for ; Wed, 1 Nov 2023 16:15:12 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-pf1-x435.google.com (mail-pf1-x435.google.com [IPv6:2607:f8b0:4864:20::435]) by sourceware.org (Postfix) with ESMTPS id BC16E3858D1E for ; Wed, 1 Nov 2023 16:14:51 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org BC16E3858D1E Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=rivosinc.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=rivosinc.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org BC16E3858D1E Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::435 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698855302; cv=none; b=Ed6mVKKEz0bw4MR+xPLiiBt4U83lznMjA7dJ4J9rHhuweOilBKW86sUTXLJGVGdl4NJVnyapZy/HIbOsVhjm3nPrHVeD+7l2JEKzIUcB2Esj6EFBNFrzYw1fecWi9uE4qr9ZIXX45rcoZ00q4Zm1/uopXiMad9kLBgHtHPqzhXs= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698855302; c=relaxed/simple; bh=maKWoGm/f7VyIs4w8S8L8QGcu0IXynWdlwqI1b95EZE=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=KbW6IW7aPgdSra29WKayN7KDzMLrOMXS7GtqDJEZ377AvuBH5cHi5wfa/wFyVMd0VJ+KF5tcPYQygEW9Hw4abZTLv72UfdHdHmjASREieRfnxqyEVj+oechlHmikTdFB9Rb3bgEPxlxgVyzA1encBWKZmwt4GcvO1B36KKHLwyI= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pf1-x435.google.com with SMTP id d2e1a72fcca58-6c311ca94ccso2354b3a.1 for ; Wed, 01 Nov 2023 09:14:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1698855290; x=1699460090; darn=gcc.gnu.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=TFetKB7gzJDjYad60zD3COxsxg1NpaUz50SVfke4gUc=; b=rmifLtP0f79t1RlNxj9WW2MDiJ56+DwpGVH2v9JFG/luU/Qag7qJY5bdVMQJeWUW0J g0j1fvLRMbcu2v3hFRwIZigi28IKr3KVId1230Xmjr9Vy1lYF1GTI/Mn35/X2Sf3moeI EcgeGS73On2i82O4/ffnNg7bFMHuobkF40rCBm52TcsRpWNAkW1+uOItCtEOQFFch0Xe ZboDCmnoTe7rbWkLWauPZyzC3ELOxSJcnPjhwjqkv+/xRVu/2BwpA+fwcj/bVj/IpMoK jTTyXt/ywE2dsnpGhei1996cYHhCTCJEC9aRUOABveNqrOlYwzopGdqFJfWW1qPegM1p RhGg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698855290; x=1699460090; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=TFetKB7gzJDjYad60zD3COxsxg1NpaUz50SVfke4gUc=; b=kWhRg9Pzvy3moBdCFXqfCGKW/mQdHk0jal0mjHembRZ4A18ltC67J99yId/oLlzSMu 54rL7Sy4wH2CnkC3+5nBZ3mDSaCa5Hlsyk2OpqlHVMXVJmoPdpafVgLHaK+MZ8LM1tpp JCYzzwQYAxvS4N1tAIH37lGmI96AEintEG2uNpFRTik89QbmMIBZbfTvVcWkYiOBlrCs aVA7P2PYbzAGTnYwCwIgd5Om1EiOdePk9OzIKqA1kLwIJWH6cULOBQoz1dAWscrzKKqH xq/9fX2ifNNgZrvmfArUsoSHwH4ZDK4GHcfm4ZtF46UIRoBgF339+7OCsPehW9wQeFVX JOxg== X-Gm-Message-State: AOJu0YwEKhQRxsz5UTrU95shgkUPidV634govq8IgGWMUazoiEQtTj8f c98aGzGTFEQmPPLwfgfFxLpmLV2H6ewMNBKnnEc= X-Google-Smtp-Source: AGHT+IEDWGG1ovMfTqpG7DBv0DtgbKuqF4UfTEDKaqRYUyiIblOSQ58UAxtlna6bdun+lqu1Jyg9RQ== X-Received: by 2002:a05:6a00:1797:b0:6b4:6b34:8cef with SMTP id s23-20020a056a00179700b006b46b348cefmr15091977pfg.34.1698855290224; Wed, 01 Nov 2023 09:14:50 -0700 (PDT) Received: from patrick-ThinkPad-X1-Carbon-Gen-8.ba.rivosinc.com ([2601:647:5700:6860:da54:3d80:42ed:3861]) by smtp.gmail.com with ESMTPSA id ey2-20020a056a0038c200b0065a1b05193asm1455281pfb.185.2023.11.01.09.14.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Nov 2023 09:14:49 -0700 (PDT) From: Patrick O'Neill To: gcc-patches@gcc.gnu.org Cc: Patrick O'Neill Subject: [PATCH v2] RISC-V: Use riscv_subword_address for atomic_test_and_set Date: Wed, 1 Nov 2023 09:14:39 -0700 Message-ID: <20231101161439.41429-1-patrick@rivosinc.com> X-Mailer: git-send-email 2.42.0 MIME-Version: 1.0 X-Spam-Status: No, score=-11.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE 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 Other subword atomic patterns use riscv_subword_address to calculate the aligned address, shift amount, mask and !mask. atomic_test_and_set was implemented before the common function was added. After this patch all subword atomic patterns use riscv_subword_address. gcc/ChangeLog: * config/riscv/sync.md: Use riscv_subword_address function to calculate the address and shift in atomic_test_and_set. Signed-off-by: Patrick O'Neill --- Changelog: v2: Comment out the diff in the foreword so git doesn't get confused when applying the patch --- Tested using r14-5040-g5dc2ba333f8. This patch causes this codegen to regress (adds a mv) but *only* on -O0. extern void abort(); short x; int main() { if ( __atomic_test_and_set(&x, __ATOMIC_SEQ_CST)) abort(); } Baseline: main: addi sp,sp,-16 sd ra,8(sp) sd s0,0(sp) addi s0,sp,16 lui a5,%hi(x) addi a5,a5,%lo(x) andi a4,a5,-4 andi a5,a5,3 li a3,1 slliw a5,a5,3 sllw a2,a3,a5 amoor.w.aqrl a3,a2,0(a4) srlw a5,a3,a5 andi a5,a5,0xff beq a5,zero,.L2 call abort .L2: li a5,0 mv a0,a5 ld ra,8(sp) ld s0,0(sp) addi sp,sp,16 jr ra After patch there is an additional mv: main: addi sp,sp,-16 sd ra,8(sp) sd s0,0(sp) addi s0,sp,16 lui a5,%hi(x) addi a5,a5,%lo(x) andi a3,a5,-4 andi a5,a5,3 slliw a5,a5,3 li a4,1 sllw a2,a4,a5 amoor.w.aqrl a4,a2,0(a3) sraw a4,a4,a5 > mv a5,a4 andi a5,a5,0xff beq a5,zero,.L2 call abort .L2: li a5,0 mv a0,a5 ld ra,8(sp) ld s0,0(sp) addi sp,sp,16 jr ra This can be fixed using: > diff --git a/gcc/config/riscv/sync.md b/gcc/config/riscv/sync.md > index ad4751febd2..a9539977321 100644 > --- a/gcc/config/riscv/sync.md > +++ b/gcc/config/riscv/sync.md > @@ -530,10 +530,9 @@ > emit_insn (gen_atomic_fetch_orsi (old, aligned_mem, shifted_set, model)); > - emit_move_insn (old, gen_rtx_ASHIFTRT (SImode, old, > - gen_lowpart (QImode, shift))); > - > - emit_move_insn (operands[0], gen_lowpart (QImode, old)); > + emit_move_insn (gen_lowpart (SImode, operands[0]), > + gen_rtx_ASHIFTRT (SImode, old, > + gen_lowpart (QImode, shift))); > DONE; > }) But I think it hurts read/grokability of the .md sequence. If it's worth changing for -O0 generated sequences, let me know and I'll send a follow up patch. --- gcc/config/riscv/sync.md | 41 +++++++++++++++++----------------------- 1 file changed, 17 insertions(+), 24 deletions(-) -- 2.34.1 diff --git a/gcc/config/riscv/sync.md b/gcc/config/riscv/sync.md index 6ff3493b5ce..ad4751febd2 100644 --- a/gcc/config/riscv/sync.md +++ b/gcc/config/riscv/sync.md @@ -504,43 +504,36 @@ (set (attr "length") (const_int 28))]) (define_expand "atomic_test_and_set" - [(match_operand:QI 0 "register_operand" "") ;; bool output + [(match_operand:QI 0 "register_operand" "") ;; bool output (match_operand:QI 1 "memory_operand" "+A") ;; memory - (match_operand:SI 2 "const_int_operand" "")] ;; model + (match_operand:SI 2 "const_int_operand" "")] ;; model "TARGET_ATOMIC" { /* We have no QImode atomics, so use the address LSBs to form a mask, then use an aligned SImode atomic. */ - rtx result = operands[0]; + rtx old = gen_reg_rtx (SImode); rtx mem = operands[1]; rtx model = operands[2]; - rtx addr = force_reg (Pmode, XEXP (mem, 0)); - - rtx aligned_addr = gen_reg_rtx (Pmode); - emit_move_insn (aligned_addr, gen_rtx_AND (Pmode, addr, GEN_INT (-4))); + rtx set = gen_reg_rtx (QImode); + rtx aligned_mem = gen_reg_rtx (SImode); + rtx shift = gen_reg_rtx (SImode); - rtx aligned_mem = change_address (mem, SImode, aligned_addr); - set_mem_alias_set (aligned_mem, 0); + /* Unused. */ + rtx _mask = gen_reg_rtx (SImode); + rtx _not_mask = gen_reg_rtx (SImode); - rtx offset = gen_reg_rtx (SImode); - emit_move_insn (offset, gen_rtx_AND (SImode, gen_lowpart (SImode, addr), - GEN_INT (3))); + riscv_subword_address (mem, &aligned_mem, &shift, &_mask, &_not_mask); - rtx tmp = gen_reg_rtx (SImode); - emit_move_insn (tmp, GEN_INT (1)); + emit_move_insn (set, GEN_INT (1)); + rtx shifted_set = gen_reg_rtx (SImode); + riscv_lshift_subword (QImode, set, shift, &shifted_set); - rtx shmt = gen_reg_rtx (SImode); - emit_move_insn (shmt, gen_rtx_ASHIFT (SImode, offset, GEN_INT (3))); + emit_insn (gen_atomic_fetch_orsi (old, aligned_mem, shifted_set, model)); - rtx word = gen_reg_rtx (SImode); - emit_move_insn (word, gen_rtx_ASHIFT (SImode, tmp, - gen_lowpart (QImode, shmt))); + emit_move_insn (old, gen_rtx_ASHIFTRT (SImode, old, + gen_lowpart (QImode, shift))); - tmp = gen_reg_rtx (SImode); - emit_insn (gen_atomic_fetch_orsi (tmp, aligned_mem, word, model)); + emit_move_insn (operands[0], gen_lowpart (QImode, old)); - emit_move_insn (gen_lowpart (SImode, result), - gen_rtx_LSHIFTRT (SImode, tmp, - gen_lowpart (QImode, shmt))); DONE; })