diff mbox series

[1/2] RISC-V: Do not let -mtune allow misaligned accesses [PR126640]

Message ID 20261105042154.1546368-2-kito.cheng@sifive.com
State New
Headers show
Series RISC-V: Do not let -mtune allow misaligned accesses [PR126640] | expand

Commit Message

Kito Cheng Nov. 5, 2026, 4:21 a.m. UTC
We take whether misaligned accesses work from the -mtune table, but this
depends on the hardware.  So -mtune=generic-ooo emits misaligned
vle32.v/vse32.v for any -march, and these can trap.  It also overrides an
explicit -mvector-strict-align.

Now -mcpu says if misaligned accesses are allowed, and -mtune only says if
they are slow:

- Scalar: unless -m[no-]strict-align is given, use -mstrict-align if -mtune
  says they are slow or -mcpu does not support them.
- Vector: -m[no-]vector-strict-align wins if given, otherwise -mcpu decides.
  The option now defaults to -1, so we can tell if it was given.

Without -mcpu, misaligned accesses are not allowed.

	PR target/126640

gcc/ChangeLog:

	* config/riscv/riscv.cc (riscv_scalar_unaligned_access_allowed_p):
	New function.
	(riscv_vector_unaligned_access_allowed_p): New function.
	(riscv_can_inline_p): Compare whether misaligned vector accesses
	are allowed instead of the raw -mvector-strict-align setting.
	(riscv_override_options_internal): Take whether misaligned
	accesses are allowed from -mcpu, not -mtune, and let an explicit
	-m[no-]vector-strict-align win.
	* config/riscv/riscv.opt (mvector-strict-align): Init to -1.
	* doc/invoke.texi (RISC-V Options): Update -mstrict-align,
	-mscalar-strict-align and -mvector-strict-align.

gcc/testsuite/ChangeLog:

	* gcc.target/riscv/cpymem-32-ooo.c: Add -mno-strict-align.
	* gcc.target/riscv/cpymem-64-ooo.c: Likewise.
	* gcc.target/riscv/movmem-scalar-32.c: Likewise.
	* gcc.target/riscv/movmem-scalar-64.c: Likewise.
	* gcc.target/riscv/setmem-scalar-32.c: Likewise.
	* gcc.target/riscv/setmem-scalar-64.c: Likewise.
	* gcc.target/riscv/rvv/autovec/pr122445.c: Add
	-mno-vector-strict-align.
	* gcc.target/riscv/predef-align-1.c: Use -mcpu=thead-c906.
	* gcc.target/riscv/predef-align-7.c: New test.
	* gcc.target/riscv/pr126640-1.c: New test.
	* gcc.target/riscv/pr126640-2.c: New test.
	* gcc.target/riscv/rvv/autovec/pr126640-1.c: New test.
	* gcc.target/riscv/rvv/autovec/pr126640-2.c: New test.
	* gcc.target/riscv/rvv/autovec/pr126640-3.c: New test.
---
 gcc/config/riscv/riscv.cc                     | 66 ++++++++++++++-----
 gcc/config/riscv/riscv.opt                    |  2 +-
 gcc/doc/invoke.texi                           | 19 +++---
 .../gcc.target/riscv/cpymem-32-ooo.c          |  2 +-
 .../gcc.target/riscv/cpymem-64-ooo.c          |  2 +-
 .../gcc.target/riscv/movmem-scalar-32.c       |  2 +-
 .../gcc.target/riscv/movmem-scalar-64.c       |  2 +-
 gcc/testsuite/gcc.target/riscv/pr126640-1.c   | 15 +++++
 gcc/testsuite/gcc.target/riscv/pr126640-2.c   | 15 +++++
 .../gcc.target/riscv/predef-align-1.c         |  2 +-
 .../gcc.target/riscv/predef-align-7.c         | 16 +++++
 .../gcc.target/riscv/rvv/autovec/pr122445.c   |  2 +-
 .../gcc.target/riscv/rvv/autovec/pr126640-1.c | 16 +++++
 .../gcc.target/riscv/rvv/autovec/pr126640-2.c | 16 +++++
 .../gcc.target/riscv/rvv/autovec/pr126640-3.c | 16 +++++
 .../gcc.target/riscv/setmem-scalar-32.c       |  2 +-
 .../gcc.target/riscv/setmem-scalar-64.c       |  2 +-
 17 files changed, 164 insertions(+), 33 deletions(-)
 create mode 100644 gcc/testsuite/gcc.target/riscv/pr126640-1.c
 create mode 100644 gcc/testsuite/gcc.target/riscv/pr126640-2.c
 create mode 100644 gcc/testsuite/gcc.target/riscv/predef-align-7.c
 create mode 100644 gcc/testsuite/gcc.target/riscv/rvv/autovec/pr126640-1.c
 create mode 100644 gcc/testsuite/gcc.target/riscv/rvv/autovec/pr126640-2.c
 create mode 100644 gcc/testsuite/gcc.target/riscv/rvv/autovec/pr126640-3.c

Comments

Robin Dapp Nov. 5, 2026, 2:16 p.m. UTC | #1
Hi Kito,

> We take whether misaligned accesses work from the -mtune table, but this
> depends on the hardware.  So -mtune=generic-ooo emits misaligned
> vle32.v/vse32.v for any -march, and these can trap.  It also overrides an
> explicit -mvector-strict-align.

I haven't read the rest yet but this one caught my eye right away.  I 
think there is an unwritten rule "explicit wins over implicit", so an 
explicit -mvector-strict-align should IMHO not be overridden, except by 
a later -mno-vector-strict-align.  Or will that break things?
Kito Cheng Nov. 5, 2026, 2:26 p.m. UTC | #2
Hi Robin:

On Thu, Nov 5, 2026 at 10:16 PM Robin Dapp <rdapp.gcc@gmail.com> wrote:
>
> Hi Kito,
>
> > We take whether misaligned accesses work from the -mtune table, but this
> > depends on the hardware.  So -mtune=generic-ooo emits misaligned
> > vle32.v/vse32.v for any -march, and these can trap.  It also overrides an
> > explicit -mvector-strict-align.
>
> I haven't read the rest yet but this one caught my eye right away.  I
> think there is an unwritten rule "explicit wins over implicit", so an
> explicit -mvector-strict-align should IMHO not be overridden, except by
> a later -mno-vector-strict-align.  Or will that break things?

The current code is:
 riscv_vector_unaligned_access_p = opts->x_rvv_vector_strict_align == 0
   || cpu->tune_param->vector_unaligned_access;

So -mtune/-mcpu will always win because `||`.

and the new logic (w/ this patch) is:

static bool
riscv_vector_unaligned_access_allowed_p (int vector_strict_align,
                                        const char *cpu_string)
{
  if (vector_strict_align != -1)
    return vector_strict_align == 0;

  const riscv_tune_info *cpu
    = cpu_string ? riscv_parse_tune (cpu_string, true) : nullptr;
  return cpu && cpu->tune_param->vector_unaligned_access;
}

And vector_strict_align is inited to -1 in the patch-set to present
user isn't give  -m[no-]vector-strict-align,

which means the behavior with this patch set should meet your (and
mine) expectations :)
Jeff Law Nov. 10, 2026, 5:29 p.m. UTC | #3
On 11/4/26 9:21 PM, Kito Cheng wrote:
> We take whether misaligned accesses work from the -mtune table, but this
> depends on the hardware.  So -mtune=generic-ooo emits misaligned
> vle32.v/vse32.v for any -march, and these can trap.  It also overrides an
> explicit -mvector-strict-align.
>
> Now -mcpu says if misaligned accesses are allowed, and -mtune only says if
> they are slow:
>
> - Scalar: unless -m[no-]strict-align is given, use -mstrict-align if -mtune
>    says they are slow or -mcpu does not support them.
> - Vector: -m[no-]vector-strict-align wins if given, otherwise -mcpu decides.
>    The option now defaults to -1, so we can tell if it was given.
>
> Without -mcpu, misaligned accesses are not allowed.
>
> 	PR target/126640
>
> gcc/ChangeLog:
>
> 	* config/riscv/riscv.cc (riscv_scalar_unaligned_access_allowed_p):
> 	New function.
> 	(riscv_vector_unaligned_access_allowed_p): New function.
> 	(riscv_can_inline_p): Compare whether misaligned vector accesses
> 	are allowed instead of the raw -mvector-strict-align setting.
> 	(riscv_override_options_internal): Take whether misaligned
> 	accesses are allowed from -mcpu, not -mtune, and let an explicit
> 	-m[no-]vector-strict-align win.
> 	* config/riscv/riscv.opt (mvector-strict-align): Init to -1.
> 	* doc/invoke.texi (RISC-V Options): Update -mstrict-align,
> 	-mscalar-strict-align and -mvector-strict-align.
>
> gcc/testsuite/ChangeLog:
>
> 	* gcc.target/riscv/cpymem-32-ooo.c: Add -mno-strict-align.
> 	* gcc.target/riscv/cpymem-64-ooo.c: Likewise.
> 	* gcc.target/riscv/movmem-scalar-32.c: Likewise.
> 	* gcc.target/riscv/movmem-scalar-64.c: Likewise.
> 	* gcc.target/riscv/setmem-scalar-32.c: Likewise.
> 	* gcc.target/riscv/setmem-scalar-64.c: Likewise.
> 	* gcc.target/riscv/rvv/autovec/pr122445.c: Add
> 	-mno-vector-strict-align.
> 	* gcc.target/riscv/predef-align-1.c: Use -mcpu=thead-c906.
> 	* gcc.target/riscv/predef-align-7.c: New test.
> 	* gcc.target/riscv/pr126640-1.c: New test.
> 	* gcc.target/riscv/pr126640-2.c: New test.
> 	* gcc.target/riscv/rvv/autovec/pr126640-1.c: New test.
> 	* gcc.target/riscv/rvv/autovec/pr126640-2.c: New test.
> 	* gcc.target/riscv/rvv/autovec/pr126640-3.c: New test.
It looks like Sashiko has findings that are worth addressing:

https://gcc.sashiko.dev/#/patchset/20261105042154.1546368-1-kito.cheng%40sifive.com

More importantly there was some concern about inconsistency in behavior 
vs the patch description.  Just to be clear I think everyone expects the 
explicit -m option to override any implicit/default behavior.

Jeff
diff mbox series

Patch

diff --git a/gcc/config/riscv/riscv.cc b/gcc/config/riscv/riscv.cc
index 7aca11d4daa..595506d6288 100644
--- a/gcc/config/riscv/riscv.cc
+++ b/gcc/config/riscv/riscv.cc
@@ -1345,6 +1345,34 @@  riscv_parse_tune (const char *tune_string, bool null_p)
   return riscv_tune_info_table;
 }
 
+/* Return true if the processor named by -mcpu=CPU_STRING supports
+   unaligned scalar memory accesses.  CPU_STRING may be NULL.  */
+
+static bool
+riscv_scalar_unaligned_access_allowed_p (const char *cpu_string)
+{
+  const riscv_tune_info *cpu
+    = cpu_string ? riscv_parse_tune (cpu_string, true) : nullptr;
+  return cpu && !cpu->tune_param->slow_unaligned_access;
+}
+
+/* Return true if unaligned vector memory accesses may be generated.
+   VECTOR_STRICT_ALIGN is the -m[no-]vector-strict-align setting, -1 if
+   unspecified, in which case it is up to -mcpu=CPU_STRING.  CPU_STRING
+   may be NULL.  */
+
+static bool
+riscv_vector_unaligned_access_allowed_p (int vector_strict_align,
+					 const char *cpu_string)
+{
+  if (vector_strict_align != -1)
+    return vector_strict_align == 0;
+
+  const riscv_tune_info *cpu
+    = cpu_string ? riscv_parse_tune (cpu_string, true) : nullptr;
+  return cpu && cpu->tune_param->vector_unaligned_access;
+}
+
 /* Helper function for riscv_build_integer; arguments are as for
    riscv_build_integer.  */
 
@@ -9778,8 +9806,12 @@  riscv_can_inline_p (tree caller, tree callee)
       != callee_opts->x_riscv_stack_protector_guard_offset)
     return false;
 
-  if (caller_opts->x_rvv_vector_strict_align
-      != callee_opts->x_rvv_vector_strict_align)
+  if (riscv_vector_unaligned_access_allowed_p
+	(caller_opts->x_rvv_vector_strict_align,
+	 caller_opts->x_riscv_cpu_string)
+      != riscv_vector_unaligned_access_allowed_p
+	   (callee_opts->x_rvv_vector_strict_align,
+	    callee_opts->x_riscv_cpu_string))
     return false;
 
   if (riscv_zilsd_required_align (caller_opts)
@@ -12536,28 +12568,30 @@  riscv_override_options_internal (struct gcc_options *opts)
 	opts->x_str_align_functions = tune_param->function_align;
     }
 
-  /* Use -mtune's setting for slow_unaligned_access, even when optimizing
-     for size.  For architectures that trap and emulate unaligned accesses,
-     the performance cost is too great, even for -Os.  Similarly, if
-     -m[no-]strict-align is left unspecified, heed -mtune's advice.  */
-  riscv_slow_unaligned_access_p = (cpu->tune_param->slow_unaligned_access
-				   || TARGET_STRICT_ALIGN);
-
-  /* By default, when -mno-vector-strict-align is not specified, do not allow
-     unaligned vector memory accesses except if -mtune's setting explicitly
-     allows it.  */
-  riscv_vector_unaligned_access_p = opts->x_rvv_vector_strict_align == 0
-    || cpu->tune_param->vector_unaligned_access;
-
   /* Make a note if user explicitly passed -mstrict-align for later
      builtin macro generation.  Can't use target_flags_explicitly since
      it is set even for -mno-strict-align.  */
   riscv_user_wants_strict_align = TARGET_STRICT_ALIGN_OPTS_P (opts);
 
+  /* -mtune only says whether unaligned accesses are fast; whether they
+     work at all comes from -mcpu.  If -m[no-]strict-align is left
+     unspecified, avoid unaligned accesses unless both agree.  */
   if ((target_flags_explicit & MASK_STRICT_ALIGN) == 0
-      && cpu->tune_param->slow_unaligned_access)
+      && (cpu->tune_param->slow_unaligned_access
+	  || !riscv_scalar_unaligned_access_allowed_p
+		(opts->x_riscv_cpu_string)))
     opts->x_target_flags |= MASK_STRICT_ALIGN;
 
+  /* Use -mtune's setting for slow_unaligned_access, even when optimizing
+     for size.  For architectures that trap and emulate unaligned accesses,
+     the performance cost is too great, even for -Os.  */
+  riscv_slow_unaligned_access_p = (cpu->tune_param->slow_unaligned_access
+				   || TARGET_STRICT_ALIGN_OPTS_P (opts));
+
+  riscv_vector_unaligned_access_p
+    = riscv_vector_unaligned_access_allowed_p (opts->x_rvv_vector_strict_align,
+					       opts->x_riscv_cpu_string);
+
   /* If the user hasn't specified a branch cost, use the processor's
      default.  */
   if (opts->x_riscv_branch_cost == 0)
diff --git a/gcc/config/riscv/riscv.opt b/gcc/config/riscv/riscv.opt
index 3226367bf8a..81e97d0be0d 100644
--- a/gcc/config/riscv/riscv.opt
+++ b/gcc/config/riscv/riscv.opt
@@ -141,7 +141,7 @@  Target RejectNegative
 Force Zilsd/Zclsd memory accesses to be 8-byte aligned.
 
 mvector-strict-align
-Target Save Var(rvv_vector_strict_align) Init(1)
+Target Save Var(rvv_vector_strict_align) Init(-1)
 Do not create element-misaligned vector memory accesses.
 
 Enum
diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
index 8a2c65a861e..41dd4aebc68 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -31635,25 +31635,28 @@  integer load/stores only.
 @opindex mno-strict-align
 @item -mstrict-align
 @itemx -mno-strict-align
-Do not or do generate unaligned memory accesses.  The default is set depending
-on whether the processor we are optimizing for supports fast unaligned access
-or not.
+Do not or do generate unaligned memory accesses.  By default, unaligned
+accesses are generated only if the processor selected by @option{-mcpu}
+supports them and the processor we are optimizing for supports fast unaligned
+access.
 
 @opindex mscalar-strict-align
 @opindex mno-scalar-strict-align
 @item -mscalar-strict-align
 @itemx -mno-scalar-strict-align
-Do not or do generate unaligned memory accesses.  The default is set depending
-on whether the processor we are optimizing for supports fast unaligned access
-or not.  This is an alias for @option{-mstrict-align}.
+Do not or do generate unaligned memory accesses.  By default, unaligned
+accesses are generated only if the processor selected by @option{-mcpu}
+supports them and the processor we are optimizing for supports fast unaligned
+access.  This is an alias for @option{-mstrict-align}.
 
 @opindex mvector-strict-align
 @opindex mno-vector-strict-align
 @item -mvector-strict-align
 @itemx -mno-vector-strict-align
 Do not or do generate unaligned vector memory accesses.  The default is set
-to off unless the processor we are optimizing for explicitly supports
-element-misaligned vector memory access.
+to off unless the processor selected by @option{-mcpu} supports
+element-misaligned vector memory access.  @option{-mtune} does not change
+the default.
 
 @opindex mzilsd-word-align
 @item -mzilsd-word-align
diff --git a/gcc/testsuite/gcc.target/riscv/cpymem-32-ooo.c b/gcc/testsuite/gcc.target/riscv/cpymem-32-ooo.c
index 2a48567353a..00472dfcdc4 100644
--- a/gcc/testsuite/gcc.target/riscv/cpymem-32-ooo.c
+++ b/gcc/testsuite/gcc.target/riscv/cpymem-32-ooo.c
@@ -1,6 +1,6 @@ 
 /* { dg-do compile } */
 /* { dg-require-effective-target rv32 } */
-/* { dg-options "-march=rv32gc -mabi=ilp32d -mtune=generic-ooo" } */
+/* { dg-options "-march=rv32gc -mabi=ilp32d -mtune=generic-ooo -mno-strict-align" } */
 /* { dg-skip-if "" { *-*-* } {"-O0" "-Os" "-Og" "-Oz" "-flto" } } */
 /* { dg-final { check-function-bodies "**" "" } } */
 /* { dg-allow-blank-lines-in-output 1 } */
diff --git a/gcc/testsuite/gcc.target/riscv/cpymem-64-ooo.c b/gcc/testsuite/gcc.target/riscv/cpymem-64-ooo.c
index 77d38d5e573..6bfb245aed8 100644
--- a/gcc/testsuite/gcc.target/riscv/cpymem-64-ooo.c
+++ b/gcc/testsuite/gcc.target/riscv/cpymem-64-ooo.c
@@ -1,6 +1,6 @@ 
 /* { dg-do compile } */
 /* { dg-require-effective-target rv64 } */
-/* { dg-options "-march=rv64gc -mabi=lp64d -mtune=generic-ooo" } */
+/* { dg-options "-march=rv64gc -mabi=lp64d -mtune=generic-ooo -mno-strict-align" } */
 /* { dg-skip-if "" { *-*-* } {"-O0" "-Os" "-Og" "-Oz" "-flto" } } */
 /* { dg-final { check-function-bodies "**" "" } } */
 /* { dg-allow-blank-lines-in-output 1 } */
diff --git a/gcc/testsuite/gcc.target/riscv/movmem-scalar-32.c b/gcc/testsuite/gcc.target/riscv/movmem-scalar-32.c
index 323beb2ccb4..00d351374d8 100644
--- a/gcc/testsuite/gcc.target/riscv/movmem-scalar-32.c
+++ b/gcc/testsuite/gcc.target/riscv/movmem-scalar-32.c
@@ -1,5 +1,5 @@ 
 /* { dg-do compile } */
-/* { dg-options "-march=rv32gc -mabi=ilp32d -mtune=generic-ooo -mstringop-strategy=scalar" } */
+/* { dg-options "-march=rv32gc -mabi=ilp32d -mtune=generic-ooo -mno-strict-align -mstringop-strategy=scalar" } */
 /* { dg-skip-if "" { *-*-* } { "-O0" "-Os" "-Oz" "-Og" "-flto" } } */
 
 #define MEMMOVE(N)				\
diff --git a/gcc/testsuite/gcc.target/riscv/movmem-scalar-64.c b/gcc/testsuite/gcc.target/riscv/movmem-scalar-64.c
index 9558c9eb14e..df7738c5845 100644
--- a/gcc/testsuite/gcc.target/riscv/movmem-scalar-64.c
+++ b/gcc/testsuite/gcc.target/riscv/movmem-scalar-64.c
@@ -1,5 +1,5 @@ 
 /* { dg-do compile } */
-/* { dg-options "-march=rv64gc -mabi=lp64d -mtune=generic-ooo -mstringop-strategy=scalar" } */
+/* { dg-options "-march=rv64gc -mabi=lp64d -mtune=generic-ooo -mno-strict-align -mstringop-strategy=scalar" } */
 /* { dg-skip-if "" { *-*-* } { "-O0" "-Os" "-Oz" "-Og" "-flto" } } */
 
 #define MEMMOVE(N)				\
diff --git a/gcc/testsuite/gcc.target/riscv/pr126640-1.c b/gcc/testsuite/gcc.target/riscv/pr126640-1.c
new file mode 100644
index 00000000000..c654ca612bc
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/pr126640-1.c
@@ -0,0 +1,15 @@ 
+/* { dg-do compile } */
+/* { dg-require-effective-target rv64 } */
+/* { dg-options "-march=rv64gc -mabi=lp64d -O2 -mtune=generic-ooo" } */
+
+/* -mtune must not allow misaligned scalar accesses.  */
+
+typedef int __attribute__ ((aligned (1))) uint_u;
+
+int
+f (uint_u *p)
+{
+  return *p;
+}
+
+/* { dg-final { scan-assembler-not {\mlw\M} } } */
diff --git a/gcc/testsuite/gcc.target/riscv/pr126640-2.c b/gcc/testsuite/gcc.target/riscv/pr126640-2.c
new file mode 100644
index 00000000000..551a2a385a9
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/pr126640-2.c
@@ -0,0 +1,15 @@ 
+/* { dg-do compile } */
+/* { dg-require-effective-target rv64 } */
+/* { dg-options "-march=rv64gc -mabi=lp64d -O2 -mcpu=thead-c906" } */
+
+/* -mcpu allows misaligned scalar accesses, even with -march.  */
+
+typedef int __attribute__ ((aligned (1))) uint_u;
+
+int
+f (uint_u *p)
+{
+  return *p;
+}
+
+/* { dg-final { scan-assembler {\mlw\M} } } */
diff --git a/gcc/testsuite/gcc.target/riscv/predef-align-1.c b/gcc/testsuite/gcc.target/riscv/predef-align-1.c
index fb8c5f74035..8185b788898 100644
--- a/gcc/testsuite/gcc.target/riscv/predef-align-1.c
+++ b/gcc/testsuite/gcc.target/riscv/predef-align-1.c
@@ -1,5 +1,5 @@ 
 /* { dg-do compile } */
-/* { dg-options "-mtune=thead-c906" } */
+/* { dg-options "-mcpu=thead-c906" } */
 
 int main() {
 
diff --git a/gcc/testsuite/gcc.target/riscv/predef-align-7.c b/gcc/testsuite/gcc.target/riscv/predef-align-7.c
new file mode 100644
index 00000000000..67377672656
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/predef-align-7.c
@@ -0,0 +1,16 @@ 
+/* { dg-do compile } */
+/* { dg-options "-mtune=thead-c906" } */
+
+int main() {
+
+/* -mtune alone does not allow misaligned access.  */
+#if !defined(__riscv_misaligned_slow)
+#error "__riscv_misaligned_slow is not set"
+#endif
+
+#if defined(__riscv_misaligned_avoid) || defined(__riscv_misaligned_fast)
+#error "__riscv_misaligned_avoid or __riscv_misaligned_fast is unexpectedly set"
+#endif
+
+  return 0;
+}
diff --git a/gcc/testsuite/gcc.target/riscv/rvv/autovec/pr122445.c b/gcc/testsuite/gcc.target/riscv/rvv/autovec/pr122445.c
index cd046647656..0920f592cba 100644
--- a/gcc/testsuite/gcc.target/riscv/rvv/autovec/pr122445.c
+++ b/gcc/testsuite/gcc.target/riscv/rvv/autovec/pr122445.c
@@ -1,5 +1,5 @@ 
 /* { dg-do compile } */
-/* { dg-options "-march=rv64gcbv_zvl256b -mabi=lp64d -O3 -mrvv-vector-bits=zvl --param=riscv-autovec-mode=V4QI -mtune=generic-ooo -fdump-rtl-avlprop-all" } */
+/* { dg-options "-march=rv64gcbv_zvl256b -mabi=lp64d -O3 -mrvv-vector-bits=zvl --param=riscv-autovec-mode=V4QI -mtune=generic-ooo -mno-vector-strict-align -fdump-rtl-avlprop-all" } */
 
 typedef unsigned char uint8_t;
 typedef short int16_t;
diff --git a/gcc/testsuite/gcc.target/riscv/rvv/autovec/pr126640-1.c b/gcc/testsuite/gcc.target/riscv/rvv/autovec/pr126640-1.c
new file mode 100644
index 00000000000..30502d679d4
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/rvv/autovec/pr126640-1.c
@@ -0,0 +1,16 @@ 
+/* { dg-do compile } */
+/* { dg-options "-march=rv64gcv -mabi=lp64d -O3 -mtune=generic-ooo -fdump-tree-vect-details" } */
+
+/* -mtune must not allow misaligned vector accesses.  */
+
+typedef int __attribute__ ((aligned (1))) uint_u;
+
+void
+f (uint_u *a, uint_u *b, int n)
+{
+  for (int i = 0; i < n; i++)
+    a[i] = b[i] + 1;
+}
+
+/* { dg-final { scan-tree-dump "Versioning for alignment will be applied" "vect" } } */
+/* { dg-final { scan-tree-dump-not "Vectorizing an unaligned access" "vect" } } */
diff --git a/gcc/testsuite/gcc.target/riscv/rvv/autovec/pr126640-2.c b/gcc/testsuite/gcc.target/riscv/rvv/autovec/pr126640-2.c
new file mode 100644
index 00000000000..712e545e52a
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/rvv/autovec/pr126640-2.c
@@ -0,0 +1,16 @@ 
+/* { dg-do compile } */
+/* { dg-options "-march=rv64gcv -mabi=lp64d -O3 -mcpu=sifive-p870-d -fdump-tree-vect-details" } */
+
+/* -mcpu allows misaligned vector accesses.  */
+
+typedef int __attribute__ ((aligned (1))) uint_u;
+
+void
+f (uint_u *a, uint_u *b, int n)
+{
+  for (int i = 0; i < n; i++)
+    a[i] = b[i] + 1;
+}
+
+/* { dg-final { scan-tree-dump "Vectorizing an unaligned access" "vect" } } */
+/* { dg-final { scan-tree-dump-not "Versioning for alignment will be applied" "vect" } } */
diff --git a/gcc/testsuite/gcc.target/riscv/rvv/autovec/pr126640-3.c b/gcc/testsuite/gcc.target/riscv/rvv/autovec/pr126640-3.c
new file mode 100644
index 00000000000..a4ec6fce804
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/rvv/autovec/pr126640-3.c
@@ -0,0 +1,16 @@ 
+/* { dg-do compile } */
+/* { dg-options "-march=rv64gcv -mabi=lp64d -O3 -mcpu=sifive-p870-d -mvector-strict-align -fdump-tree-vect-details" } */
+
+/* -mvector-strict-align overrides -mcpu.  */
+
+typedef int __attribute__ ((aligned (1))) uint_u;
+
+void
+f (uint_u *a, uint_u *b, int n)
+{
+  for (int i = 0; i < n; i++)
+    a[i] = b[i] + 1;
+}
+
+/* { dg-final { scan-tree-dump "Versioning for alignment will be applied" "vect" } } */
+/* { dg-final { scan-tree-dump-not "Vectorizing an unaligned access" "vect" } } */
diff --git a/gcc/testsuite/gcc.target/riscv/setmem-scalar-32.c b/gcc/testsuite/gcc.target/riscv/setmem-scalar-32.c
index 3f174c1ffbf..b7ffdaf5b44 100644
--- a/gcc/testsuite/gcc.target/riscv/setmem-scalar-32.c
+++ b/gcc/testsuite/gcc.target/riscv/setmem-scalar-32.c
@@ -1,5 +1,5 @@ 
 /* { dg-do compile } */
-/* { dg-options "-march=rv32gc -mabi=ilp32d -mtune=generic-ooo -mstringop-strategy=scalar" } */
+/* { dg-options "-march=rv32gc -mabi=ilp32d -mtune=generic-ooo -mno-strict-align -mstringop-strategy=scalar" } */
 /* { dg-skip-if "" { *-*-* } { "-O0" "-Os" "-Oz" "-Og" "-flto" } } */
 
 #define SET_VAR(N)				\
diff --git a/gcc/testsuite/gcc.target/riscv/setmem-scalar-64.c b/gcc/testsuite/gcc.target/riscv/setmem-scalar-64.c
index 948f59a3962..fdbe7e06228 100644
--- a/gcc/testsuite/gcc.target/riscv/setmem-scalar-64.c
+++ b/gcc/testsuite/gcc.target/riscv/setmem-scalar-64.c
@@ -1,5 +1,5 @@ 
 /* { dg-do compile } */
-/* { dg-options "-march=rv64gc -mabi=lp64d -mtune=generic-ooo -mstringop-strategy=scalar" } */
+/* { dg-options "-march=rv64gc -mabi=lp64d -mtune=generic-ooo -mno-strict-align -mstringop-strategy=scalar" } */
 /* { dg-skip-if "" { *-*-* } { "-O0" "-Os" "-Oz" "-Og" "-flto" } } */
 
 #define SET_VAR(N)				\