| Message ID | CAC82fA0bEoVPA3mSGT5WpupseOa9odE2N-EeuBtmL5ii_ejt4A@mail.gmail.com |
|---|---|
| State | New |
| Headers |
Return-Path: <newlib-bounces~patchwork=sourceware.org@sourceware.org> X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from vm01.sourceware.org (localhost [127.0.0.1]) by sourceware.org (Postfix) with ESMTP id 7897443B5538 for <patchwork@sourceware.org>; Fri, 1 May 2026 05:29:46 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7897443B5538 X-Original-To: newlib@sourceware.org Delivered-To: newlib@sourceware.org Received: from mail-oo1-f45.google.com (mail-oo1-f45.google.com [209.85.161.45]) by sourceware.org (Postfix) with ESMTPS id 27746490031E for <newlib@sourceware.org>; Fri, 1 May 2026 05:29:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 27746490031E Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=rtems.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gwmail.gwu.edu ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 27746490031E Authentication-Results: server2.sourceware.org; arc=pass smtp.remote-ip=209.85.161.45 ARC-Seal: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1777613352; cv=pass; b=VeZumTXH3NfANHw6YowRRlW8FjPGWCRA/6pYJvptiOgFZAAiTdJeMKEBQt4SFkEKcq5CvCim8VzcHEB1keuJFoKQQX/iPegcYXLl35BKksGM5Zk8qWogJJyXCwejq54/AdxAanPccGxcQoLGLYmW/9bZ0OirPf1LVHAyAmFD3Ds= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1777613352; c=relaxed/simple; bh=Jy/z83pwvgeti0CrCsAhw7GBt8cJFj7bhW3MwHaEJz8=; h=MIME-Version:From:Date:Message-ID:Subject:To; b=jBCPLmWTzobaVnR1CpArlkrRr9WvvtmsqoOKtivipzHs08+WJvupo7HR7m+ulfEdMN314lt1hAReo3m/cS8SzNgMm7Uf8FmQoIQfpzE5sPW5hpdbMnVviU4r5fATcQJZ+QXux1LJywwh5TlW2VRZyF8lTQ3ce0XSzqquhlf8UM0= ARC-Authentication-Results: i=2; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 27746490031E Received: by mail-oo1-f45.google.com with SMTP id 006d021491bc7-6948fb494a0so940459eaf.2 for <newlib@sourceware.org>; Thu, 30 Apr 2026 22:29:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1777613350; cv=none; d=google.com; s=arc-20240605; b=LkKdfdYlCfgXY/3YpDj62kCnG58g5apavlcnHR/JXlKPxEQ+lrs72mbs1LgGXASavn EfSZc77yMKGyzw4Z3PTw9hzfarvEU2Np4SKahKHev6QXY+Fvs0IZx/F5Nah2ChaffWHd KrMQXIozk6VqnNv/BM5MQJKk1pvhXqZajZdaKnOo+mpWXACxYL8CTPiG3jnwy7a9L20B i8+ueZjKuus7w1OVqcmZc/eJ1ojRLwl4l3u3YBFSeoKt4InfGcoSNDfYeZzn6l9/Rpz1 1NUVaSu6e0+P8FPtaf2CzeY9LXKrkv5cL09Sfsttcxnj37O40ge/ChLalMYkM+Ns7P/J 2YaA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=to:subject:message-id:date:from:mime-version; bh=cmAYlWIvtUfLW+PmyN/Eg/Am+mAICDnJTU0JqOm+kLQ=; fh=hOkFLs4sQ+AWujE2oY/xb5EIjG5/8OaeHTnMhbbl1Cs=; b=L0HYohySeyTPuWUB0AE3LoaDrIlTaNEwzOtLN063U0IfMJoFXafExnBARclyipdq2l digTegi554zwpPBZlRrqflTVJiayPllrTgNHQyqFuKWdjVlCzo+QheNUvkyBFyXh9Vps P9GHyNECC3zB+U8isRDzCZrVKL5AuNL+MUB5pMAUhKJgkAMRnfbWQPTbYbbKCq+u94K2 7HV9+SM9aztAAAEdRScw1y8vMdF3OvqJUsMqW3NZLP6Bie4FjOOq7yZYoGiJqQnWB1Xp DAl4EfMACvyCAgBbVy6/rmaSohoeZeSEcwx9+UmkLil1tybhK0Eled96xMl3Nuel0DWi c8Tg==; darn=sourceware.org ARC-Authentication-Results: i=1; mx.google.com; arc=none X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777613350; x=1778218150; h=to:subject:message-id:date:from:mime-version:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=cmAYlWIvtUfLW+PmyN/Eg/Am+mAICDnJTU0JqOm+kLQ=; b=rTFkGGoY6soUudqAmIwib3QK+PUw0QPevrlv7VmMxVzlv0Q1a/hU+FgiAj+iyFDOPN ipmVnD10LmfCbQAV/7uP02vYr6IFMdOMzER6Hd0ZcyUfmgPdAR120O9nIGoNLF3FEKVA UDLhByOCcxVIo0G4t6SDGLVFzFUpuAzhqw7D0Y9YGgSRsgvVqo4+/B/Bjt4gNlG6XOam 6nTb+HAu10yWx8oYx5f7Qzm0qqQH/kRkRt60lIiL/6gLsc2D7KPhYChH1cKlS3i5VhKi jvtO8B8Rh6pH3YTYK/JE5g29W0azph6c/dxWHcSh+oHTwJv5eJkjFJGF37atK0v8vXBr C4Yg== X-Gm-Message-State: AOJu0YzmwMX5trSP7Tbn+RmWpmpFBMpJMOBQ6E5uaK3EuXbFBR0U8vR5 dliBJ3s9bvigdTPCEdt6Xiuv98mDHtQzOCFgpXp94rvvDeZE2JB9rIavr4YjxwAo87hLMsk9tF4 DFxn5gTT7VvoamCLRR0ReNIxnwaw7ueeKvL+3UqLs0WuN1PsCieEzWjRr X-Gm-Gg: AeBDievvvMm5ou40lulfCvcKA05Epb6/i6K9eebT5gjxX+VtKmQDl2J7U3xCxrJAtOB n6ST9cgiFLo2Yh1qKsU1+aY037+CbhRNgSI5nWYLRmUDcGSfLIpUAV9hs0Ydy/gh5mCTNoiUlDC hOUoie8FNqpcjQNX4VUA7pZnyXB0izKakTLm7ZRxCH6w+kGOTIqhLsezcgBdHDq9ug/BshgpKUl LzzsKkraA9hOKkawgNJ/plI3UZcKm1lexk01sF7hzLaP6x8OZ6po4pugDtogJtlFGqfX7+1KcJR wBnT24Cxo4ZTM6IcI04= X-Received: by 2002:a05:6820:f03:b0:696:1450:ff24 with SMTP id 006d021491bc7-6967a59ee00mr2828192eaf.36.1777613350330; Thu, 30 Apr 2026 22:29:10 -0700 (PDT) MIME-Version: 1.0 From: Gedare Bloom <gedare@rtems.org> Date: Thu, 30 Apr 2026 23:28:59 -0600 X-Gm-Features: AVHnY4KNrX4dZnlqGlE8DFJs2_yGsRDHdFg84Wve-nQaH6dF9ryxCMgDyWDlDhM Message-ID: <CAC82fA0bEoVPA3mSGT5WpupseOa9odE2N-EeuBtmL5ii_ejt4A@mail.gmail.com> Subject: riscv: configure.ac bug fix for misaligned access when __riscv_misaligned_slow To: Newlib <newlib@sourceware.org> Content-Type: multipart/mixed; boundary="0000000000004b87580650badc9b" X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, HEADER_FROM_DIFFERENT_DOMAINS, JMQ_SPF_NEUTRAL, KAM_DMARC_STATUS, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on sourceware.org X-BeenThere: newlib@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Newlib mailing list <newlib.sourceware.org> List-Unsubscribe: <https://sourceware.org/mailman/options/newlib>, <mailto:newlib-request@sourceware.org?subject=unsubscribe> List-Archive: <https://sourceware.org/pipermail/newlib/> List-Post: <mailto:newlib@sourceware.org> List-Help: <mailto:newlib-request@sourceware.org?subject=help> List-Subscribe: <https://sourceware.org/mailman/listinfo/newlib>, <mailto:newlib-request@sourceware.org?subject=subscribe> Errors-To: newlib-bounces~patchwork=sourceware.org@sourceware.org |
| Series |
riscv: configure.ac bug fix for misaligned access when __riscv_misaligned_slow
|
|
Commit Message
Gedare Bloom
May 1, 2026, 5:28 a.m. UTC
memcmp is generating data alignment errors on risc-v targets where the
hw does not allow unaligned access. This behavior was observed on
microchip polarfire with rtems. Here is the code generated:
000000008000bb34 <memcmp>:
8000bb34: 469d li a3,7
8000bb36: 00c6fb63 bgeu a3,a2,8000bb4c <memcmp+0x18>
8000bb3a: 6118 ld a4,0(a0)
8000bb3c: 619c ld a5,0(a1)
You can see that 8000bb3a will cause a fault if a0 is not aligned and
hw does not support it.
riscv-rtems7-gcc -dM -E - < /dev/null | grep aligned
#define __riscv_misaligned_slow 1
Attached fix corrects this. Generated code is now:
00000008000baf6 <memcmp>:
8000baf6: 469d li a3,7
8000baf8: 04c6f063 bgeu a3,a2,8000bb38 <memcmp+0x42>
8000bafc: 00a5e7b3 or a5,a1,a0
8000bb00: 8b9d andi a5,a5,7
8000bb02: c395 beqz a5,8000bb26 <memcmp+0x30>
8000bb04: 167d addi a2,a2,-1
8000bb06: 0605 addi a2,a2,1
8000bb08: 962a add a2,a2,a0
8000bb0a: a019 j 8000bb10 <memcmp+0x1a>
Now we have the check at 8000bb02 that will handle alignment and
falls-thru to byte-by-byte copy, or jumps to aligned long copies.
Gedare
Comments
Hi Gedare, I have a question regarding this inconsistency between the compiler-defined macro and actual hardware behavior. Please do not take this message as a review comment. On 2026/5/1 13:28, Gedare Bloom wrote: > memcmp is generating data alignment errors on risc-v targets where the > hw does not allow unaligned access. This behavior was observed on > microchip polarfire with rtems. Here is the code generated: > > 000000008000bb34 <memcmp>: > 8000bb34: 469d li a3,7 > 8000bb36: 00c6fb63 bgeu a3,a2,8000bb4c <memcmp+0x18> > 8000bb3a: 6118 ld a4,0(a0) > 8000bb3c: 619c ld a5,0(a1) > > You can see that 8000bb3a will cause a fault if a0 is not aligned and > hw does not support it. > > riscv-rtems7-gcc -dM -E - < /dev/null | grep aligned > #define __riscv_misaligned_slow 1 > According to the riscv-c-api-doc[1], "__riscv_misaligned_slow" macro shuold be defined when scalar misaligned *are supported* but slower than aligned accesses. For hardwares that not allow unaligned access at all, "__riscv_misaligned_avoid" seems to be the more appropriate macro. So, I am wondering whether this is actually a compiler issue rather than a C library issue? > Attached fix corrects this. Generated code is now: > 00000008000baf6 <memcmp>: > 8000baf6: 469d li a3,7 > 8000baf8: 04c6f063 bgeu a3,a2,8000bb38 <memcmp+0x42> > 8000bafc: 00a5e7b3 or a5,a1,a0 > 8000bb00: 8b9d andi a5,a5,7 > 8000bb02: c395 beqz a5,8000bb26 <memcmp+0x30> > 8000bb04: 167d addi a2,a2,-1 > 8000bb06: 0605 addi a2,a2,1 > 8000bb08: 962a add a2,a2,a0 > 8000bb0a: a019 j 8000bb10 <memcmp+0x1a> > > Now we have the check at 8000bb02 that will handle alignment and > falls-thru to byte-by-byte copy, or jumps to aligned long copies. > > Gedare Best regards, Pincheng Wang [1] https://github.com/riscv-non-isa/riscv-c-api-doc/blob/main/src/c-api.adoc
On Thu, May 7, 2026 at 6:06 AM Pincheng Wang <pincheng.plct@isrc.iscas.ac.cn> wrote: > > Hi Gedare, > > I have a question regarding this inconsistency between the > compiler-defined macro and actual hardware behavior. Please do not take > this message as a review comment. > > On 2026/5/1 13:28, Gedare Bloom wrote: > > memcmp is generating data alignment errors on risc-v targets where the > > hw does not allow unaligned access. This behavior was observed on > > microchip polarfire with rtems. Here is the code generated: > > > > 000000008000bb34 <memcmp>: > > 8000bb34: 469d li a3,7 > > 8000bb36: 00c6fb63 bgeu a3,a2,8000bb4c <memcmp+0x18> > > 8000bb3a: 6118 ld a4,0(a0) > > 8000bb3c: 619c ld a5,0(a1) > > > > You can see that 8000bb3a will cause a fault if a0 is not aligned and > > hw does not support it. > > > > riscv-rtems7-gcc -dM -E - < /dev/null | grep aligned > > #define __riscv_misaligned_slow 1 > > > > According to the riscv-c-api-doc[1], "__riscv_misaligned_slow" macro > shuold be defined when scalar misaligned *are supported* but slower than > aligned accesses. For hardwares that not allow unaligned access at all, > "__riscv_misaligned_avoid" seems to be the more appropriate macro. > > So, I am wondering whether this is actually a compiler issue rather than > a C library issue? > This is a good point. I was taking my inspiration from the implementations of memcpy and memmove, which only check for __riscv_misaligned_fast. I'm not sure what the right answer is for the optimization. Regarding my problem with alignment error, you are correct that this is probably better fixed by using a multilib with mstrict-align defined so that I get the _avoid variant. > > Attached fix corrects this. Generated code is now: > > 00000008000baf6 <memcmp>: > > 8000baf6: 469d li a3,7 > > 8000baf8: 04c6f063 bgeu a3,a2,8000bb38 <memcmp+0x42> > > 8000bafc: 00a5e7b3 or a5,a1,a0 > > 8000bb00: 8b9d andi a5,a5,7 > > 8000bb02: c395 beqz a5,8000bb26 <memcmp+0x30> > > 8000bb04: 167d addi a2,a2,-1 > > 8000bb06: 0605 addi a2,a2,1 > > 8000bb08: 962a add a2,a2,a0 > > 8000bb0a: a019 j 8000bb10 <memcmp+0x1a> > > > > Now we have the check at 8000bb02 that will handle alignment and > > falls-thru to byte-by-byte copy, or jumps to aligned long copies. > > > > Gedare > > Best regards, > Pincheng Wang > > [1] > https://github.com/riscv-non-isa/riscv-c-api-doc/blob/main/src/c-api.adoc >
On Mon, May 11, 2026 at 10:33 AM Gedare Bloom <gedare@rtems.org> wrote: > > On Thu, May 7, 2026 at 6:06 AM Pincheng Wang > <pincheng.plct@isrc.iscas.ac.cn> wrote: > > > > Hi Gedare, > > > > I have a question regarding this inconsistency between the > > compiler-defined macro and actual hardware behavior. Please do not take > > this message as a review comment. > > > > On 2026/5/1 13:28, Gedare Bloom wrote: > > > memcmp is generating data alignment errors on risc-v targets where the > > > hw does not allow unaligned access. This behavior was observed on > > > microchip polarfire with rtems. Here is the code generated: > > > > > > 000000008000bb34 <memcmp>: > > > 8000bb34: 469d li a3,7 > > > 8000bb36: 00c6fb63 bgeu a3,a2,8000bb4c <memcmp+0x18> > > > 8000bb3a: 6118 ld a4,0(a0) > > > 8000bb3c: 619c ld a5,0(a1) > > > > > > You can see that 8000bb3a will cause a fault if a0 is not aligned and > > > hw does not support it. > > > > > > riscv-rtems7-gcc -dM -E - < /dev/null | grep aligned > > > #define __riscv_misaligned_slow 1 > > > > > > > According to the riscv-c-api-doc[1], "__riscv_misaligned_slow" macro > > shuold be defined when scalar misaligned *are supported* but slower than > > aligned accesses. For hardwares that not allow unaligned access at all, > > "__riscv_misaligned_avoid" seems to be the more appropriate macro. > > > > So, I am wondering whether this is actually a compiler issue rather than > > a C library issue? > > > > This is a good point. I was taking my inspiration from the > implementations of memcpy and memmove, which only check for > __riscv_misaligned_fast. > I'm not sure what the right answer is for the optimization. > > Regarding my problem with alignment error, you are correct that this > is probably better fixed by using a multilib with mstrict-align > defined so that I get the _avoid variant. > This seems to work for me now. However, it does raise the question of whether the other optimized variants should also be checking for __riscv_misaligned_slow. > > > Attached fix corrects this. Generated code is now: > > > 00000008000baf6 <memcmp>: > > > 8000baf6: 469d li a3,7 > > > 8000baf8: 04c6f063 bgeu a3,a2,8000bb38 <memcmp+0x42> > > > 8000bafc: 00a5e7b3 or a5,a1,a0 > > > 8000bb00: 8b9d andi a5,a5,7 > > > 8000bb02: c395 beqz a5,8000bb26 <memcmp+0x30> > > > 8000bb04: 167d addi a2,a2,-1 > > > 8000bb06: 0605 addi a2,a2,1 > > > 8000bb08: 962a add a2,a2,a0 > > > 8000bb0a: a019 j 8000bb10 <memcmp+0x1a> > > > > > > Now we have the check at 8000bb02 that will handle alignment and > > > falls-thru to byte-by-byte copy, or jumps to aligned long copies. > > > > > > Gedare > > > > Best regards, > > Pincheng Wang > > > > [1] > > https://github.com/riscv-non-isa/riscv-c-api-doc/blob/main/src/c-api.adoc > >
From 74564292ffd4c0f2baa4d99715cce614dd4572f6 Mon Sep 17 00:00:00 2001 From: Gedare Bloom <gedare@rtems.org> Date: Thu, 30 Apr 2026 11:10:53 -0600 Subject: [PATCH] riscv: avoid misaligned accesses if __riscv_misaligned_slow The configure rule for misaligned accesses is too wide by including __riscv_misaligned_fast or __riscv_misaligned_slow for enabling the HW supported misaligned access. --- newlib/configure.ac | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/newlib/configure.ac b/newlib/configure.ac index a4807830e..4640e69c5 100644 --- a/newlib/configure.ac +++ b/newlib/configure.ac @@ -555,7 +555,7 @@ if test "x${newlib_hw_misaligned_access}" = "x"; then AC_CACHE_CHECK([if $CC has enabled misaligned hardware access], [newlib_cv_hw_misaligned_access], [dnl cat > conftest.c <<EOF -#if __riscv_misaligned_fast || __riscv_misaligned_slow +#if __riscv_misaligned_fast void misalign_access_supported(void) {} #else #error "misaligned access is not supported" -- 2.47.3