From patchwork Wed Nov 1 05:05:12 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: 160437 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:abcd:0:b0:403:3b70:6f57 with SMTP id f13csp193637vqx; Tue, 31 Oct 2023 22:06:36 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHyFFhEKCPinIRy6Fzz80PVPJYZpYFcxGHkbkYEW9FIDJNbB+x8DfthbT6BF/YUa25ZRZcF X-Received: by 2002:a67:c195:0:b0:458:3bd3:9ad7 with SMTP id h21-20020a67c195000000b004583bd39ad7mr15265842vsj.18.1698815196722; Tue, 31 Oct 2023 22:06:36 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1698815196; cv=pass; d=google.com; s=arc-20160816; b=LDX94sWP/cQibPhlFOAl6FnYYsE0kLvRcSgh+os5loeOKH4oxHBbKbfm206MAmkEPD /JMM4TPQsLJgQfd7gZhC7lTIykigfW/UDA5ud2GL5CtqEuMCiImYr97liEdZ/sgRmGR8 CP3sQ8YSxRLktT85T8FalqEBmgydEwQsR/3CQS6NQ2QbRUfvXS3FCwaz1K3TtDrIjWCX B6bo+uPbURJ7y9yXQk/vKtjcTspP+Lv45usN8dl97yksJSHs+2gH76wBU856Xx1Pl5nW tIQgPzPajzM+6L52gDI1vgrjuilNe4G0ku9v6DyME0HQfyHkvHgRRyXPdHPAoxxLB43L zfgQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:cc:to:from:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=WogAB0+RtvopB6KoJVSdOP2+qJVy/m7fiz+Eeq9sNEs=; fh=AMD4FSz6cUc4n6xCKkDOe8Mgn0A5ubP59ONK3itePwA=; b=OW14DMMPoG6c395LvNq34nr7OciTeD50RgLCPt4J9QFcrqBoEr085VFZi29ZdqfoKz OdN3J6+CBPGwmVSjPA6bg982EyXvoKxhhVfNjgznTl6Gr8M33NBmoheYBjl/K2Jizuj4 P800OHii7mecY3KVphPEbKcD2+d1NfkD5S1rMSpIxC/gBj37RjBhHkuQz4l/0LzGumS2 vthAZlUFsFvvgsm67F2gfGikDJsT53FV8ZXx/o2aKdezCjP81zC8l8w/kExUQObGCLrp TAd8FvsSKRbkjXk22p40ML1nqOENmeaS9GXEqdfEjAb3oJL3vBHqulUyOsLnv56clvP9 g6Hg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b="v7Vn0C/n"; arc=pass (i=1); 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 (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id x11-20020a05620a258b00b0077578612983si2218784qko.6.2023.10.31.22.06.36 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 31 Oct 2023 22:06:36 -0700 (PDT) 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; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b="v7Vn0C/n"; arc=pass (i=1); 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 3EDC5385770A for ; Wed, 1 Nov 2023 05:06:36 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-pg1-x532.google.com (mail-pg1-x532.google.com [IPv6:2607:f8b0:4864:20::532]) by sourceware.org (Postfix) with ESMTPS id DDBEA3858439 for ; Wed, 1 Nov 2023 05:06:00 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org DDBEA3858439 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 DDBEA3858439 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::532 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698815171; cv=none; b=I8RXJgKB2Vev5WX+v+SU+0VZVRkL7pDxucK0aZCZq+pex/UWOLzuBuc3sW+IYfqoB148bwXX3u5aKpSoYxhHUr0NoSo8loUWfi4JCNQnOa+IV5KbAABdj3V1Tuh+207woWkWTbV91i/Bmlz+COhG182qG3oOWJmxArpYD/MBJ64= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698815171; c=relaxed/simple; bh=GHvlIyOJIuXiP7O6S6+O6vgDESbYMwFav5vjHBV+3Jk=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=tcsK582kJkVhIOHa9tIpx8565v/T3sqbO2Cy/fo/wC3qBwTikDAV4FZgoS4ui4t5XgpXx0bGCx/mZ7IWI7XY04kQGGh0uoB9y+cC99A2eC7Kyb5mgmC5GsUsCgBgX94E2zCXydLN8pPeC5sIdtQw6Ipk3ggQbfAxwyqaRxnnEVQ= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pg1-x532.google.com with SMTP id 41be03b00d2f7-5b97ca78da8so2238228a12.3 for ; Tue, 31 Oct 2023 22:06:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1698815159; x=1699419959; 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=WogAB0+RtvopB6KoJVSdOP2+qJVy/m7fiz+Eeq9sNEs=; b=v7Vn0C/nZrhNy/o04etjrZOgZB+eNJJW38CBgNJ99kQosjxTtVi7yw8pwUC18bVy/F 35UdoEYOt0crEVsTkWbD5DwRunI/CKnXO9yHjZeytdnaTZns339nS6ipnZsct7RCoBHs em2XdSoNtuWPnk3kI4mBwrbmfmHC7RE8NS23EEC2Sk7OGaViLdj8MtpVhP4ugSIt0WO1 QHWeZq4L1lN5HN1gHX0EPUuNPRABLIzNIxI7eZMKMEV4gFuHsL5Ubr94eNBHA1wtiwi2 TT0ZMNZlU4BsJzJQ4PAGv9CGmvFy3G5WIyPIX57es54JON9kFu48Q3iUX3gk22QOdkAT SVRA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698815159; x=1699419959; 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=WogAB0+RtvopB6KoJVSdOP2+qJVy/m7fiz+Eeq9sNEs=; b=xFdFKTJgM1S985MNpNwUBo33vk/HvNs4AqwN/AuMxCj8l8y3IAKE3/SoL7nC+Ckkgc vbWVBQKcg4T7cvamLwJ3+N4xcws/1XqlwciLWAZ94F2EdK3dw20GdaaoGWradQBhQ5wG 6R0uiBa5E1yH9xe88W8Q3RV7YwZH/CEiP+xZZ1ERgp9oPgf41hHm7/VVxyg8JBqsb6GF pPIXiWbLoQv9zbHs6rx5/+YLu8KWCGyCoGTSV8ez8J872sEXeTquY1iJo9aCW9mw2/bM ubsP9q+3Fi9TzSEZivzzUjPBZbETfSbu0r11U4pSqMdiFNp+wz9TYi2XzVL7n4eOAafI WPqw== X-Gm-Message-State: AOJu0Yxfl8kK7KGbW0Z2mHMwyH+ZNFd9lsS5mcTs9wuqTkAolzEEnlM0 JLgvkN+nzJwSqoEKlrbOeyUgSOJBhjmmrfuxyDY= X-Received: by 2002:a05:6a20:daa6:b0:16b:afc2:3b69 with SMTP id iy38-20020a056a20daa600b0016bafc23b69mr16672454pzb.36.1698815158800; Tue, 31 Oct 2023 22:05:58 -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 h21-20020a170902eed500b001c6052152fdsm436959plb.50.2023.10.31.22.05.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 31 Oct 2023 22:05:58 -0700 (PDT) From: Patrick O'Neill To: gcc-patches@gcc.gnu.org Cc: Patrick O'Neill Subject: [PATCH] RISC-V: Use riscv_subword_address for atomic_test_and_set Date: Tue, 31 Oct 2023 22:05:12 -0700 Message-ID: <20231101050512.33961-1-patrick@rivosinc.com> X-Mailer: git-send-email 2.42.0 MIME-Version: 1.0 X-Spam-Status: No, score=-12.3 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+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1781336843895875537 X-GMAIL-MSGID: 1781336843895875537 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 --- 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: -- 2.34.1 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(-) 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; })