Skip to content

[Clang][ARM]Ensure both -mno-unaligned-access and -munaligned-access are passed to multilib selection logic #134099

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 3, 2025

Conversation

simpal01
Copy link
Contributor

@simpal01 simpal01 commented Apr 2, 2025

Previously, alignment option was passed to multilib selection logic only when -mno-unaligned-access was explicitly specified on the command line.

Now this change ensure both -mno-unaligned-access and -munaligned-access are passed to the multilib selection logic, which now also considers the target architecture when determining alignment access policy.

…eatures.

Update the multilib selection logic to bypass the
alignment option based on each architecture’s
feature set, rather than relying solely on command-line
options.

Previously, alignment option was bypassed only when
-mno-unaligned-access was explicitly specified on the
commandline. This change makes the selection more robust
and architecture aware.
@simpal01 simpal01 requested a review from vhscampos April 2, 2025 15:39
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' labels Apr 2, 2025
@llvmbot
Copy link
Member

llvmbot commented Apr 2, 2025

@llvm/pr-subscribers-clang-driver

@llvm/pr-subscribers-clang

Author: Simi Pallipurath (simpal01)

Changes

Update the multilib selection logic to bypass the alignment option based on each architecture’s feature set, rather than relying on command-line options.

Previously, alignment option was bypassed only when -mno-unaligned-access was explicitly specified on the command line. This change makes the selection more robust and architecture aware.


Full diff: https://github.com/llvm/llvm-project/pull/134099.diff

2 Files Affected:

  • (modified) clang/lib/Driver/ToolChain.cpp (+12-12)
  • (modified) clang/test/Driver/print-multi-selection-flags.c (+6)
diff --git a/clang/lib/Driver/ToolChain.cpp b/clang/lib/Driver/ToolChain.cpp
index 8a922b283daf5..476026f82b32b 100644
--- a/clang/lib/Driver/ToolChain.cpp
+++ b/clang/lib/Driver/ToolChain.cpp
@@ -239,12 +239,12 @@ static void getAArch64MultilibFlags(const Driver &D,
     Result.push_back(BranchProtectionArg->getAsString(Args));
   }
 
-  if (Arg *AlignArg = Args.getLastArg(
-          options::OPT_mstrict_align, options::OPT_mno_strict_align,
-          options::OPT_mno_unaligned_access, options::OPT_munaligned_access)) {
-    if (AlignArg->getOption().matches(options::OPT_mstrict_align) ||
-        AlignArg->getOption().matches(options::OPT_mno_unaligned_access))
-      Result.push_back(AlignArg->getAsString(Args));
+  if (FeatureSet.contains("+strict-align")) {
+    Result.push_back("-mno-unaligned-access");
+    Result.push_back("-mstrict-align");
+  } else {
+    Result.push_back("-munaligned-access");
+    Result.push_back("-mno-strict-align");
   }
 
   if (Arg *Endian = Args.getLastArg(options::OPT_mbig_endian,
@@ -313,12 +313,12 @@ static void getARMMultilibFlags(const Driver &D,
     Result.push_back(BranchProtectionArg->getAsString(Args));
   }
 
-  if (Arg *AlignArg = Args.getLastArg(
-          options::OPT_mstrict_align, options::OPT_mno_strict_align,
-          options::OPT_mno_unaligned_access, options::OPT_munaligned_access)) {
-    if (AlignArg->getOption().matches(options::OPT_mstrict_align) ||
-        AlignArg->getOption().matches(options::OPT_mno_unaligned_access))
-      Result.push_back(AlignArg->getAsString(Args));
+  if (FeatureSet.contains("+strict-align")) {
+    Result.push_back("-mno-unaligned-access");
+    Result.push_back("-mstrict-align");
+  } else {
+    Result.push_back("-munaligned-access");
+    Result.push_back("-mno-strict-align");
   }
 
   if (Arg *Endian = Args.getLastArg(options::OPT_mbig_endian,
diff --git a/clang/test/Driver/print-multi-selection-flags.c b/clang/test/Driver/print-multi-selection-flags.c
index 5a35ae374f011..b21c2d11c1009 100644
--- a/clang/test/Driver/print-multi-selection-flags.c
+++ b/clang/test/Driver/print-multi-selection-flags.c
@@ -69,9 +69,15 @@
 // CHECK-BRANCH-PROTECTION: -mbranch-protection=standard
 
 // RUN: %clang -multi-lib-config=%S/Inputs/multilib/empty.yaml -print-multi-flags-experimental --target=arm-none-eabi -mno-unaligned-access | FileCheck --check-prefix=CHECK-NO-UNALIGNED-ACCESS %s
+// RUN: %clang -multi-lib-config=%S/Inputs/multilib/empty.yaml -print-multi-flags-experimental --target=arm-none-eabi | FileCheck --check-prefix=CHECK-NO-UNALIGNED-ACCESS %s
 // RUN: %clang -multi-lib-config=%S/Inputs/multilib/empty.yaml -print-multi-flags-experimental --target=aarch64-none-elf -mno-unaligned-access | FileCheck --check-prefix=CHECK-NO-UNALIGNED-ACCESS %s
 // CHECK-NO-UNALIGNED-ACCESS: -mno-unaligned-access
 
+// RUN: %clang -multi-lib-config=%S/Inputs/multilib/empty.yaml -print-multi-flags-experimental --target=arm-none-eabi -munaligned-access | FileCheck --check-prefix=CHECK-UNALIGNED-ACCESS %s
+// RUN: %clang -multi-lib-config=%S/Inputs/multilib/empty.yaml -print-multi-flags-experimental --target=aarch64-none-elf | FileCheck --check-prefix=CHECK-UNALIGNED-ACCESS %s
+// RUN: %clang -multi-lib-config=%S/Inputs/multilib/empty.yaml -print-multi-flags-experimental --target=aarch64-none-elf -munaligned-access | FileCheck --check-prefix=CHECK-UNALIGNED-ACCESS %s
+// CHECK-UNALIGNED-ACCESS: -munaligned-access
+
 // RUN: %clang -multi-lib-config=%S/Inputs/multilib/empty.yaml -print-multi-flags-experimental --target=arm-none-eabi -mbig-endian | FileCheck --check-prefix=CHECK-BIG-ENDIAN %s
 // RUN: %clang -multi-lib-config=%S/Inputs/multilib/empty.yaml -print-multi-flags-experimental --target=aarch64-none-elf -mbig-endian | FileCheck --check-prefix=CHECK-BIG-ENDIAN %s
 // CHECK-BIG-ENDIAN: -mbig-endian

@simpal01 simpal01 changed the title Refine multilib selection to handle alignment based on architecture features. Bypass alignment option based on architecture features instead of command-line flags Apr 2, 2025
Copy link
Member

@vhscampos vhscampos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review of the commit msg:

  • Add some categories to the title, like [Clang][ARM]
  • The new solution still uses the command-line options (inside the function getARMTargetFeatures). However it also takes into account the architecture in order to determine the alignment access policy.
  • I think "bypass" is the wrong term. One correct term I believe could be "pass along", as in "Clang passes along the options to the multilib system".

@simpal01 simpal01 changed the title Bypass alignment option based on architecture features instead of command-line flags [Clang][ARM]Ensure both -mno-unaligned-access and -munaligned-access are passed to multilib selection logic Apr 2, 2025
@simpal01
Copy link
Contributor Author

simpal01 commented Apr 2, 2025

Review of the commit msg:

  • Add some categories to the title, like [Clang][ARM]
  • The new solution still uses the command-line options (inside the function getARMTargetFeatures). However it also takes into account the architecture in order to determine the alignment access policy.
  • I think "bypass" is the wrong term. One correct term I believe could be "pass along", as in "Clang passes along the options to the multilib system".

Done

Copy link
Collaborator

@smithp35 smithp35 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be useful to mention in the Description that -mno-unaligned-access and -mstrict-align are synonyms, and that we pass both through so that the multilib.yaml file can select on either.

Copy link
Member

@vhscampos vhscampos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Please wait for @smithp35 's approval too.

Copy link
Collaborator

@smithp35 smithp35 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for the update

@vhscampos vhscampos merged commit cb0d130 into llvm:main Apr 3, 2025
11 checks passed
simpal01 added a commit to arm/arm-toolchain that referenced this pull request Apr 3, 2025
…builds. (#250)

We don’t have a -mno-unaligned-access specific variant for the v8M libraries. For v8-M with strict aligned access, it’s more appropriate to use the v7-M variants, as they include a version with strict alignment support. It was a deliberate decision to provide strict-alignment variants only for a subset of architectures.

To support this, both -mno-unaligned-access and -munaligned-access options must be passed into the multilib selection logic. This aligns with the approach in llvm/llvm-project#134099, which also considers the target architecture when determining the alignment access policy. Since Armv6-M does not support unaligned accesses, test changes are needed to reflect the updated behaviour when the target architecture is taken into account.

This patch fixes below issues.

1. The naming of the v8-M libraries should explicitly include
‘unaligned’ to reflect their behaviour.

2. When using -mno-unaligned-access, the multilib system should instead
select the v7-M variant.

3. Armv6m test changes are needed to reflect the updated behaviour when
the target architecture is taken into account.
simpal01 added a commit to simpal01/arm-toolchain that referenced this pull request Apr 3, 2025
…are passed to multilib selection logic

We need to incorporate the upstreamed patch
llvm/llvm-project#134099 into the
20.x branch. So applying this as patch file.
simpal01 added a commit to simpal01/arm-toolchain that referenced this pull request Apr 3, 2025
…builds. (arm#250)

We don’t have a -mno-unaligned-access specific variant for the v8M libraries. For v8-M with strict aligned access, it’s more appropriate to use the v7-M variants, as they include a version with strict alignment support. It was a deliberate decision to provide strict-alignment variants only for a subset of architectures.

To support this, both -mno-unaligned-access and -munaligned-access options must be passed into the multilib selection logic. This aligns with the approach in llvm/llvm-project#134099, which also considers the target architecture when determining the alignment access policy. Since Armv6-M does not support unaligned accesses, test changes are needed to reflect the updated behaviour when the target architecture is taken into account.

This patch fixes below issues.

1. The naming of the v8-M libraries should explicitly include
‘unaligned’ to reflect their behaviour.

2. When using -mno-unaligned-access, the multilib system should instead
select the v7-M variant.

3. Armv6m test changes are needed to reflect the updated behaviour when
the target architecture is taken into account.

(cherry picked from commit 8c3bbfc)
simpal01 added a commit to arm/arm-toolchain that referenced this pull request Apr 4, 2025
…are passed to multilib selection logic

We need to incorporate the upstreamed patch
llvm/llvm-project#134099 into the
20.x branch. So applying this as patch file.
simpal01 added a commit to arm/arm-toolchain that referenced this pull request Apr 4, 2025
…builds. (#250)

We don’t have a -mno-unaligned-access specific variant for the v8M libraries. For v8-M with strict aligned access, it’s more appropriate to use the v7-M variants, as they include a version with strict alignment support. It was a deliberate decision to provide strict-alignment variants only for a subset of architectures.

To support this, both -mno-unaligned-access and -munaligned-access options must be passed into the multilib selection logic. This aligns with the approach in llvm/llvm-project#134099, which also considers the target architecture when determining the alignment access policy. Since Armv6-M does not support unaligned accesses, test changes are needed to reflect the updated behaviour when the target architecture is taken into account.

This patch fixes below issues.

1. The naming of the v8-M libraries should explicitly include
‘unaligned’ to reflect their behaviour.

2. When using -mno-unaligned-access, the multilib system should instead
select the v7-M variant.

3. Armv6m test changes are needed to reflect the updated behaviour when
the target architecture is taken into account.

(cherry picked from commit 8c3bbfc)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants