[clang] [clang-tools-extra] [RISCV] Clang flags for controlling zilsd alignment (PR #181439)

Sam Elliott via cfe-commits cfe-commits at lists.llvm.org
Fri Feb 13 23:48:29 PST 2026


https://github.com/lenary updated https://github.com/llvm/llvm-project/pull/181439

>From 9dd24d3ae3db373dbdb4bbf685da507faae1c81a Mon Sep 17 00:00:00 2001
From: Sam Elliott <aelliott at qti.qualcomm.com>
Date: Fri, 13 Feb 2026 14:19:04 -0800
Subject: [PATCH 1/2] [RISCV] Clang flags for controlling zilsd alignment

Called `-mno-zilsd-4byte-align` and `-mzilsd-4byte-align`. These
interact with scalar/strict alignment, in hopefully a reasonable way.

They cause errors on rv64, where zilsd is not available.
---
 clang-tools-extra/docs/ReleaseNotes.rst     |  4 +++
 clang/include/clang/Options/Options.td      |  4 +++
 clang/lib/Driver/ToolChains/Arch/RISCV.cpp  | 29 +++++++++++++++++++++
 clang/test/Driver/riscv-zilsd-4byte-align.c | 25 ++++++++++++++++++
 4 files changed, 62 insertions(+)
 create mode 100644 clang/test/Driver/riscv-zilsd-4byte-align.c

diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index e7437e62ee77d..aa5d33edad3ce 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -64,6 +64,10 @@ Semantic Highlighting
 Compile flags
 ^^^^^^^^^^^^^
 
+- There are a new pair of flags for riscv32 called ``-mzilsd-4byte-align`` and
+  ``-mzilsd-4byte-align`` which control whether Zilsd accesses are allowed to be
+  aligned to 4-bytes rather than fully unaligned or fully (8-byte) aligned.
+
 Hover
 ^^^^^
 
diff --git a/clang/include/clang/Options/Options.td b/clang/include/clang/Options/Options.td
index a274017953b1d..8faed31813d2b 100644
--- a/clang/include/clang/Options/Options.td
+++ b/clang/include/clang/Options/Options.td
@@ -5454,6 +5454,10 @@ def mvector_strict_align : Flag<["-"], "mvector-strict-align">, Group<m_Group>,
   HelpText<"Force all vector memory accesses to be aligned (RISC-V only)">;
 def mno_vector_strict_align : Flag<["-"], "mno-vector-strict-align">, Group<m_Group>,
   HelpText<"Allow vector memory accesses to be unaligned (RISC-V only)">;
+def mzilsd_4byte_align : Flag<["-"], "mzilsd-4byte-align">, Group<m_Group>,
+  HelpText<"Allow Zilsd/Zclsd accesses to be 4-byte aligned (RISC-V only)">;
+def mno_zilsd_4byte_align : Flag<["-"], "mno-zilsd-4byte-align">, Group<m_Group>,
+  HelpText<"Force Zilsd/Zclsd accesses to be 8-byte aligned (RISC-V only)">;
 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 7fda8ea50223d..582a8e4ce8b66 100644
--- a/clang/lib/Driver/ToolChains/Arch/RISCV.cpp
+++ b/clang/lib/Driver/ToolChains/Arch/RISCV.cpp
@@ -8,6 +8,7 @@
 
 #include "RISCV.h"
 #include "../Clang.h"
+#include "clang/Basic/DiagnosticDriver.h"
 #include "clang/Driver/CommonArgs.h"
 #include "clang/Driver/Driver.h"
 #include "clang/Options/Options.h"
@@ -169,6 +170,34 @@ void riscv::getRISCVTargetFeatures(const Driver &D, const llvm::Triple &Triple,
     Features.push_back("+unaligned-vector-mem");
   }
 
+  if (Triple.isRISCV32()) {
+    // Handle `-mzilsd-4byte-align` and `-mnozilsd-4byte-align` on rv32. These
+    // interact with the scalar alignment options - if unaligned scalar memory
+    // is allowed then that takes precedence over this option, as zilsd accesses
+    // can be 1-byte aligned in this case. Otherwise, the option allows zilsd
+    // accesses to be 4-byte aligned rather than the usual 8-byte aligned.
+    if (const Arg *A = Args.getLastArg(
+            options::OPT_mstrict_align, options::OPT_mscalar_strict_align,
+            options::OPT_mzilsd_4byte_align, options::OPT_mno_strict_align,
+            options::OPT_mno_scalar_strict_align,
+            options::OPT_mno_zilsd_4byte_align)) {
+      if (A->getOption().matches(options::OPT_mno_strict_align) ||
+          A->getOption().matches(options::OPT_mno_scalar_strict_align) ||
+          A->getOption().matches(options::OPT_mzilsd_4byte_align)) {
+        Features.push_back("+zilsd-4byte-align");
+      } else {
+        Features.push_back("-zilsd-4byte-align");
+      }
+    }
+  } else {
+    // Zilsd is not available on RV64, so report an error for these options.
+    if (const Arg *A = Args.getLastArg(options::OPT_mzilsd_4byte_align,
+                                       options::OPT_mno_zilsd_4byte_align)) {
+      D.Diag(clang::diag::err_drv_unsupported_opt_for_target)
+          << A->getSpelling() << Triple.getTriple();
+    }
+  }
+
   // Now add any that the user explicitly requested on the command line,
   // which may override the defaults.
   handleTargetFeaturesGroup(D, Triple, Args, Features,
diff --git a/clang/test/Driver/riscv-zilsd-4byte-align.c b/clang/test/Driver/riscv-zilsd-4byte-align.c
new file mode 100644
index 0000000000000..3cdd679ac56d1
--- /dev/null
+++ b/clang/test/Driver/riscv-zilsd-4byte-align.c
@@ -0,0 +1,25 @@
+
+
+// RUN: %clang --target=riscv32-unknown-elf -### %s -mno-strict-align 2>&1 | FileCheck %s -check-prefixes=ZILSD-4BYTE-ALIGN
+// RUN: %clang --target=riscv32-unknown-elf -### %s -mstrict-align 2>&1 | FileCheck %s -check-prefixes=NO-ZILSD-4BYTE-ALIGN
+
+// RUN: %clang --target=riscv32-unknown-elf -### %s -mno-scalar-strict-align 2>&1 | FileCheck %s -check-prefix=ZILSD-4BYTE-ALIGN
+// RUN: %clang --target=riscv32-unknown-elf -### %s -mscalar-strict-align 2>&1 | FileCheck %s -check-prefix=NO-ZILSD-4BYTE-ALIGN
+
+// RUN: %clang --target=riscv32-unknown-elf -### %s -mzilsd-4byte-align 2>&1 | FileCheck %s -check-prefix=ZILSD-4BYTE-ALIGN
+// RUN: %clang --target=riscv32-unknown-elf -### %s -mno-zilsd-4byte-align 2>&1 | FileCheck %s -check-prefix=NO-ZILSD-4BYTE-ALIGN
+
+// RUN: %clang --target=riscv32-unknown-elf -### %s -mno-zilsd-4byte-align -mno-scalar-strict-align 2>&1 | FileCheck %s -check-prefix=ZILSD-4BYTE-ALIGN
+// RUN: %clang --target=riscv32-unknown-elf -### %s -mzilsd-4byte-align -mscalar-strict-align 2>&1 | FileCheck %s -check-prefix=NO-ZILSD-4BYTE-ALIGN
+
+// RUN: %clang --target=riscv32-unknown-elf -### %s -mno-zilsd-4byte-align -mno-strict-align 2>&1 | FileCheck %s -check-prefix=ZILSD-4BYTE-ALIGN
+// RUN: %clang --target=riscv32-unknown-elf -### %s -mzilsd-4byte-align -mstrict-align 2>&1 | FileCheck %s -check-prefix=NO-ZILSD-4BYTE-ALIGN
+
+// ZILSD-4BYTE-ALIGN: "-target-feature" "+zilsd-4byte-align"
+// NO-ZILSD-4BYTE-ALIGN: "-target-feature" "-zilsd-4byte-align"
+
+// RUN: not %clang --target=riscv64-unknown-elf -### %s -mzilsd-4byte-align 2>&1 | FileCheck %s -check-prefix=ERROR-ZILSD-4BYTE-ALIGN
+// RUN: not %clang --target=riscv64-unknown-elf -### %s -mno-zilsd-4byte-align 2>&1 | FileCheck %s -check-prefix=ERROR-NO-ZILSD-4BYTE-ALIGN
+
+// ERROR-ZILSD-4BYTE-ALIGN: error: unsupported option '-mzilsd-4byte-align' for target
+// ERROR-NO-ZILSD-4BYTE-ALIGN: error: unsupported option '-mno-zilsd-4byte-align' for target

>From 66c60dbe8ce409d15331a011b8f1c19536f76846 Mon Sep 17 00:00:00 2001
From: Sam Elliott <aelliott at qti.qualcomm.com>
Date: Fri, 13 Feb 2026 23:48:18 -0800
Subject: [PATCH 2/2] Put release note in correct file

---
 clang-tools-extra/docs/ReleaseNotes.rst | 4 ----
 clang/docs/ReleaseNotes.rst             | 4 ++++
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index aa5d33edad3ce..e7437e62ee77d 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -64,10 +64,6 @@ Semantic Highlighting
 Compile flags
 ^^^^^^^^^^^^^
 
-- There are a new pair of flags for riscv32 called ``-mzilsd-4byte-align`` and
-  ``-mzilsd-4byte-align`` which control whether Zilsd accesses are allowed to be
-  aligned to 4-bytes rather than fully unaligned or fully (8-byte) aligned.
-
 Hover
 ^^^^^
 
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 83cd562c6f49b..c72c13448af2e 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -152,6 +152,10 @@ New Compiler Flags
   can only generate the reduced BMI as a by-product, e.g, an object files or
   a full BMI.
 
+- There are a new pair of flags for riscv32 called ``-mzilsd-4byte-align`` and
+  ``-mzilsd-4byte-align`` which control whether Zilsd accesses are allowed to be
+  aligned to 4-bytes rather than fully unaligned or fully (8-byte) aligned.
+
 Deprecated Compiler Flags
 -------------------------
 



More information about the cfe-commits mailing list