[clang] [llvm] [RISCV] Add a command line option to disable register overlap for vector indexed load. (PR #224739)

Craig Topper via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 12 19:49:57 PST 2026


https://github.com/topperc updated https://github.com/llvm/llvm-project/pull/224739

>From ecfb32ef9bfd8d76c2f485d579f1bee0a2095985 Mon Sep 17 00:00:00 2001
From: Craig Topper <craig.topper at sifive.com>
Date: Fri, 18 Sep 2026 13:22:10 -0700
Subject: [PATCH] [RISCV] Add a command line option to disable register overlap
 for vector index load.

If a trap occurs on an element other than 0, some SiFive cores will write a garbage value to that element and all active elements after it in the destination register before running the trap handler. This is normally allowed by the spec if restarting the instruction would overwrite the elements with the correct results.

For a vector indexed load with overlapping source and destination, writing the later elements with garbage corrupts some of the source indices. This prevents the instruction from restarting correctly once the fault has been handled.

This option allows users to opt out of overlapping registers on
these instructions.

This is an alternative to #220079.
---
 clang/include/clang/Options/Options.td        |  5 ++
 clang/lib/Driver/ToolChains/Arch/RISCV.cpp    |  9 +++
 clang/test/Driver/riscv-features.c            |  5 ++
 llvm/lib/Target/RISCV/RISCVFeatures.td        |  6 ++
 llvm/lib/Target/RISCV/RISCVISelLowering.cpp   | 14 +++++
 .../Target/RISCV/RISCVInstrInfoVPseudos.td    |  2 +
 llvm/lib/Target/RISCV/RISCVProcessors.td      |  3 +-
 llvm/test/CodeGen/RISCV/features-info.ll      |  1 +
 .../RISCV/rvv/vluxei-vloxei-overlap.ll        | 57 +++++++++++++++++++
 9 files changed, 101 insertions(+), 1 deletion(-)
 create mode 100644 llvm/test/CodeGen/RISCV/rvv/vluxei-vloxei-overlap.ll

diff --git a/clang/include/clang/Options/Options.td b/clang/include/clang/Options/Options.td
index 4fc924f848507..6c310b2fa2936 100644
--- a/clang/include/clang/Options/Options.td
+++ b/clang/include/clang/Options/Options.td
@@ -5924,6 +5924,11 @@ def mzilsd_word_align : Flag<["-"], "mzilsd-word-align">, Group<m_Group>,
   HelpText<"Allow Zilsd/Zclsd memory accesses to be 4-byte aligned (RISC-V only)">;
 def mzilsd_strict_align : Flag<["-"], "mzilsd-strict-align">, Group<m_Group>,
   HelpText<"Force all Zilsd/Zclsd memory accesses to be 8-byte aligned (RISC-V only)">;
+defm fix_sifive_mal_9174 : BoolOptionWithoutMarshalling<"m", "fix-sifive-mal-9174",
+  PosFlag<SetFalse, [], [], "Workaround SiFive erratum MAL-9174 by forcing vector indexed loads to have non-">,
+  NegFlag<SetTrue, [], [], "Allow vector indexed loads to have ">,
+  BothFlags<[], [], "overlapping source and destination register groups (RISC-V only)">>,
+  Group<m_Group>;
 def mno_thumb : Flag<["-"], "mno-thumb">, Group<m_arm_Features_Group>;
 def mrestrict_it: Flag<["-"], "mrestrict-it">, Group<m_arm_Features_Group>,
   HelpText<"Disallow generation of complex IT blocks. It is off by default.">;
diff --git a/clang/lib/Driver/ToolChains/Arch/RISCV.cpp b/clang/lib/Driver/ToolChains/Arch/RISCV.cpp
index 8e650ddf92dfc..7fb8641783642 100644
--- a/clang/lib/Driver/ToolChains/Arch/RISCV.cpp
+++ b/clang/lib/Driver/ToolChains/Arch/RISCV.cpp
@@ -178,6 +178,15 @@ void riscv::getRISCVTargetFeatures(const Driver &D, const llvm::Triple &Triple,
     Features.push_back("+unaligned-vector-mem");
   }
 
+  if (const Arg *A =
+          Args.getLastArg(options::OPT_mfix_sifive_mal_9174,
+                          options::OPT_mno_fix_sifive_mal_9174)) {
+    if (A->getOption().matches(options::OPT_mfix_sifive_mal_9174))
+      Features.push_back("+fix-sifive-mal-9174");
+    else
+      Features.push_back("-fix-sifive-mal-9174");
+  }
+
   if (Triple.isRISCV32()) {
     // Handle `-mzilsd-word-align` and `-mzilsd-strict-align` on rv32. These
     // interact with the scalar alignment options - if unaligned scalar memory
diff --git a/clang/test/Driver/riscv-features.c b/clang/test/Driver/riscv-features.c
index 97736ff81c799..f0571e2c5746b 100644
--- a/clang/test/Driver/riscv-features.c
+++ b/clang/test/Driver/riscv-features.c
@@ -48,6 +48,11 @@
 // FAST-VECTOR-UNALIGNED-ACCESS: "-target-feature" "+unaligned-vector-mem"
 // NO-FAST-VECTOR-UNALIGNED-ACCESS: "-target-feature" "-unaligned-vector-mem"
 
+// RUN: %clang --target=riscv32-unknown-elf -### %s -mfix-sifive-mal-9174 2>&1 | FileCheck %s -check-prefix=FIX-SIFIVE-MAL-9174
+// RUN: %clang --target=riscv32-unknown-elf -### %s -mno-fix-sifive-mal-9174 2>&1 | FileCheck %s -check-prefix=NO-FIX-SIFIVE-MAL-9174
+// FIX-SIFIVE-MAL-9174: "-target-feature" "+fix-sifive-mal-9174"
+// NO-FIX-SIFIVE-MAL-9174: "-target-feature" "-fix-sifive-mal-9174"
+
 // RUN: %clang --target=riscv32-unknown-elf -### %s 2>&1 | FileCheck %s -check-prefix=NOUWTABLE
 // RUN: %clang --target=riscv32-unknown-elf -fasynchronous-unwind-tables -### %s 2>&1 | FileCheck %s -check-prefix=UWTABLE
 // RUN: %clang --target=riscv64-unknown-elf -### %s 2>&1 | FileCheck %s -check-prefix=NOUWTABLE
diff --git a/llvm/lib/Target/RISCV/RISCVFeatures.td b/llvm/lib/Target/RISCV/RISCVFeatures.td
index 79b90b3b2defb..9fc64a64d419c 100644
--- a/llvm/lib/Target/RISCV/RISCVFeatures.td
+++ b/llvm/lib/Target/RISCV/RISCVFeatures.td
@@ -2094,6 +2094,12 @@ def FeatureTaggedGlobals : SubtargetFeature<"tagged-globals",
     "true", "Use an instruction sequence for taking the address of a global "
     "that allows a memory tag in the upper address bits">;
 
+def FeatureFixSiFiveMAL9174
+    : SubtargetFeature<"fix-sifive-mal-9174", "FixSiFiveMAL_9174", "true",
+                       "Workaround SiFive erratum MAL-9174 by disallowing "
+                       "overlapping source and destination for vector index "
+                       "loads">;
+
 //===----------------------------------------------------------------------===//
 // Tuning features
 //===----------------------------------------------------------------------===//
diff --git a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
index 8be82d596e884..04caece741cb3 100644
--- a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
+++ b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
@@ -28603,6 +28603,20 @@ RISCVTargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI,
 
 void RISCVTargetLowering::AdjustInstrPostInstrSelection(MachineInstr &MI,
                                                         SDNode *Node) const {
+  if (Subtarget.fixSiFiveMAL_9174()) {
+    const RISCVVPseudosTable::PseudoInfo *RVV =
+        RISCVVPseudosTable::getPseudoInfo(MI.getOpcode());
+    if (RVV && (RVV->BaseInstr == RISCV::VLUXEI8_V ||
+                RVV->BaseInstr == RISCV::VLUXEI16_V ||
+                RVV->BaseInstr == RISCV::VLUXEI32_V ||
+                RVV->BaseInstr == RISCV::VLUXEI64_V ||
+                RVV->BaseInstr == RISCV::VLOXEI8_V ||
+                RVV->BaseInstr == RISCV::VLOXEI16_V ||
+                RVV->BaseInstr == RISCV::VLOXEI32_V ||
+                RVV->BaseInstr == RISCV::VLOXEI64_V))
+      MI.getOperand(0).setIsEarlyClobber(true);
+  }
+
   // If instruction defines FRM operand, conservatively set it as non-dead to
   // express data dependency with FRM users and prevent incorrect instruction
   // reordering.
diff --git a/llvm/lib/Target/RISCV/RISCVInstrInfoVPseudos.td b/llvm/lib/Target/RISCV/RISCVInstrInfoVPseudos.td
index c6241f582de67..e2fc58234e208 100644
--- a/llvm/lib/Target/RISCV/RISCVInstrInfoVPseudos.td
+++ b/llvm/lib/Target/RISCV/RISCVInstrInfoVPseudos.td
@@ -936,6 +936,7 @@ class VPseudoILoadNoMask<VReg RetClass,
   let HasVecPolicyOp = 1;
   let Constraints = !if(!eq(EarlyClobber, 1), "@earlyclobber $rd, $rd = $passthru", "$rd = $passthru");
   let TargetOverlapConstraintType = TargetConstraintType;
+  let hasPostISelHook = 1;
 }
 
 class VPseudoILoadMask<VReg RetClass,
@@ -960,6 +961,7 @@ class VPseudoILoadMask<VReg RetClass,
   let HasVecPolicyOp = 1;
   let UsesMaskPolicy = 1;
   let IncludeInInversePseudoTable = 0;
+  let hasPostISelHook = 1;
 }
 
 class VPseudoUSStoreNoMask<VReg StClass,
diff --git a/llvm/lib/Target/RISCV/RISCVProcessors.td b/llvm/lib/Target/RISCV/RISCVProcessors.td
index 22b478e7fed72..e20db641e259b 100644
--- a/llvm/lib/Target/RISCV/RISCVProcessors.td
+++ b/llvm/lib/Target/RISCV/RISCVProcessors.td
@@ -614,7 +614,8 @@ def SIFIVE_P870_D : RISCVProcessorModel<"sifive-p870-d", SiFiveP800Model,
                                         FeatureStdExtZvksg,
                                         FeatureStdExtZvl128b,
                                         FeatureUnalignedScalarMem,
-                                        FeatureUnalignedVectorMem]),
+                                        FeatureUnalignedVectorMem,
+                                        FeatureFixSiFiveMAL9174]),
                                        [TuneNoDefaultUnroll,
                                         TuneConditionalCompressedMoveFusion,
                                         TuneLUIADDIFusion,
diff --git a/llvm/test/CodeGen/RISCV/features-info.ll b/llvm/test/CodeGen/RISCV/features-info.ll
index 4164f9697af65..a1be482da2528 100644
--- a/llvm/test/CodeGen/RISCV/features-info.ll
+++ b/llvm/test/CodeGen/RISCV/features-info.ll
@@ -69,6 +69,7 @@
 ; CHECK-NEXT:   experimental-zvzip               - 'Zvzip' (Vector Reordering Structured Data).
 ; CHECK-NEXT:   experimental-zyhybrid            - 'Zyhybrid' (Hybrid Extension for RVI Compatibility).
 ; CHECK-NEXT:   f                                - 'F' (Single-Precision Floating-Point).
+; CHECK-NEXT:   fix-sifive-mal-9174              - Workaround SiFive erratum MAL-9174 by disallowing overlapping source and destination for vector index loads.
 ; CHECK-NEXT:   forced-atomics                   - Assume that lock-free native-width atomics are available.
 ; CHECK-NEXT:   fusion-add-load                  - Enable ADD(.UW) + load macrofusion.
 ; CHECK-NEXT:   fusion-add-mem                   - Enable ADD+LOAD/STORE macrofusion.
diff --git a/llvm/test/CodeGen/RISCV/rvv/vluxei-vloxei-overlap.ll b/llvm/test/CodeGen/RISCV/rvv/vluxei-vloxei-overlap.ll
new file mode 100644
index 0000000000000..87d5939ff4c68
--- /dev/null
+++ b/llvm/test/CodeGen/RISCV/rvv/vluxei-vloxei-overlap.ll
@@ -0,0 +1,57 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
+; RUN: sed 's/iXLen/i32/g' %s | llc -mtriple=riscv32 -mattr=+v \
+; RUN:   -verify-machineinstrs -target-abi=ilp32d | FileCheck %s --check-prefixes=OVERLAP
+; RUN: sed 's/iXLen/i64/g' %s | llc -mtriple=riscv64 -mattr=+v \
+; RUN:   -verify-machineinstrs -target-abi=lp64d | FileCheck %s --check-prefixes=OVERLAP
+; RUN: sed 's/iXLen/i32/g' %s | llc -mtriple=riscv32 -mattr=+v,+fix-sifive-mal-9174 \
+; RUN:   -verify-machineinstrs -target-abi=ilp32d | FileCheck %s --check-prefixes=NOOVERLAP
+; RUN: sed 's/iXLen/i64/g' %s | llc -mtriple=riscv64 -mattr=+v,+fix-sifive-mal-9174 \
+; RUN:   -verify-machineinstrs -target-abi=lp64d | FileCheck %s --check-prefixes=NOOVERLAP
+; RUN: sed 's/iXLen/i64/g' %s | llc -mtriple=riscv64 -mcpu=sifive-p870-d \
+; RUN:   -verify-machineinstrs -target-abi=lp64d | FileCheck %s --check-prefixes=NOOVERLAP
+
+define <vscale x 1 x i8> @intrinsic_vluxei_v_nxv1i8_nxv1i8_nxv1i32(ptr %0, <vscale x 1 x i32> %1, iXLen %2) nounwind {
+; OVERLAP-LABEL: intrinsic_vluxei_v_nxv1i8_nxv1i8_nxv1i32:
+; OVERLAP:       # %bb.0: # %entry
+; OVERLAP-NEXT:    vsetvli zero, a1, e8, mf8, ta, ma
+; OVERLAP-NEXT:    vluxei32.v v8, (a0), v8
+; OVERLAP-NEXT:    ret
+;
+; NOOVERLAP-LABEL: intrinsic_vluxei_v_nxv1i8_nxv1i8_nxv1i32:
+; NOOVERLAP:       # %bb.0: # %entry
+; NOOVERLAP-NEXT:    vsetvli zero, a1, e8, mf8, ta, ma
+; NOOVERLAP-NEXT:    vluxei32.v v9, (a0), v8
+; NOOVERLAP-NEXT:    vmv1r.v v8, v9
+; NOOVERLAP-NEXT:    ret
+entry:
+  %a = call <vscale x 1 x i8> @llvm.riscv.vluxei.nxv1i8.nxv1i32(
+    <vscale x 1 x i8> poison,
+    ptr %0,
+    <vscale x 1 x i32> %1,
+    iXLen %2)
+
+  ret <vscale x 1 x i8> %a
+}
+
+define <vscale x 1 x i8> @intrinsic_vloxei_v_nxv1i8_nxv1i8_nxv1i32(ptr %0, <vscale x 1 x i32> %1, iXLen %2) nounwind {
+; OVERLAP-LABEL: intrinsic_vloxei_v_nxv1i8_nxv1i8_nxv1i32:
+; OVERLAP:       # %bb.0: # %entry
+; OVERLAP-NEXT:    vsetvli zero, a1, e8, mf8, ta, ma
+; OVERLAP-NEXT:    vloxei32.v v8, (a0), v8
+; OVERLAP-NEXT:    ret
+;
+; NOOVERLAP-LABEL: intrinsic_vloxei_v_nxv1i8_nxv1i8_nxv1i32:
+; NOOVERLAP:       # %bb.0: # %entry
+; NOOVERLAP-NEXT:    vsetvli zero, a1, e8, mf8, ta, ma
+; NOOVERLAP-NEXT:    vloxei32.v v9, (a0), v8
+; NOOVERLAP-NEXT:    vmv1r.v v8, v9
+; NOOVERLAP-NEXT:    ret
+entry:
+  %a = call <vscale x 1 x i8> @llvm.riscv.vloxei.nxv1i8.nxv1i32(
+    <vscale x 1 x i8> poison,
+    ptr %0,
+    <vscale x 1 x i32> %1,
+    iXLen %2)
+
+  ret <vscale x 1 x i8> %a
+}



More information about the cfe-commits mailing list