[clang] [clang][AArch64] Don't #define __ARM_FEATURE_CRC32 when -crc is specified in -target-feature (PR #132167)

via cfe-commits cfe-commits at lists.llvm.org
Thu Mar 20 01:50:28 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang

@llvm/pr-subscribers-backend-aarch64

Author: None (XiaShark)

<details>
<summary>Changes</summary>

Currently, clang incorrectly defines the `__ARM_FEATURE_CRC32` macro even when `-crc` is explicitly specified in `-target-feature`. This can lead to unexpected behavior in code that relies on this macro.
See the example at https://godbolt.org/z/n4xsch3qv

This commit fixed the issue by adding a secondary check to confirm if `-crc` is specified in `-target-feature`.

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


2 Files Affected:

- (modified) clang/lib/Basic/Targets/AArch64.cpp (+5-3) 
- (modified) clang/test/Preprocessor/aarch64-target-features.c (+2) 


``````````diff
diff --git a/clang/lib/Basic/Targets/AArch64.cpp b/clang/lib/Basic/Targets/AArch64.cpp
index 3633bab6e0df9..f21e7d2c4d7b4 100644
--- a/clang/lib/Basic/Targets/AArch64.cpp
+++ b/clang/lib/Basic/Targets/AArch64.cpp
@@ -1192,17 +1192,19 @@ bool AArch64TargetInfo::handleTargetFeatures(std::vector<std::string> &Features,
     }
   }
 
+  setDataLayout();
+  setArchFeatures();
+
   // Check features that are manually disabled by command line options.
   // This needs to be checked after architecture-related features are handled,
   // making sure they are properly disabled when required.
   for (const auto &Feature : Features) {
     if (Feature == "-d128")
       HasD128 = false;
+    if (Feature == "-crc")
+      HasCRC = false;
   }
 
-  setDataLayout();
-  setArchFeatures();
-
   if (HasNoFP) {
     FPU &= ~FPUMode;
     FPU &= ~NeonMode;
diff --git a/clang/test/Preprocessor/aarch64-target-features.c b/clang/test/Preprocessor/aarch64-target-features.c
index b10c55447d9af..6efad0f4983d7 100644
--- a/clang/test/Preprocessor/aarch64-target-features.c
+++ b/clang/test/Preprocessor/aarch64-target-features.c
@@ -120,7 +120,9 @@
 // RUN: %clang -target arm64-none-linux-gnu -march=armv8-a+crc -x c -E -dM %s -o - | FileCheck --check-prefix=CHECK-CRC32 %s
 // RUN: %clang -target aarch64-none-linux-gnu -march=armv8.1-a -x c -E -dM %s -o - | FileCheck --check-prefix=CHECK-CRC32 %s
 // RUN: %clang -target arm64-none-linux-gnu -march=armv8.1-a -x c -E -dM %s -o - | FileCheck --check-prefix=CHECK-CRC32 %s
+// RUN: %clang -target arm64-none-linux-gnu -march=armv8.1-a+nocrc -x c -E -dM %s -o - | FileCheck --check-prefix=CHECK-NOCRC %s
 // CHECK-CRC32: __ARM_FEATURE_CRC32 1
+// CHECK-NOCRC-NOT: __ARM_FEATURE_CRC32 1
 
 // RUN: %clang -target aarch64-none-linux-gnu -fno-math-errno -fno-signed-zeros\
 // RUN:        -fno-trapping-math -fassociative-math -freciprocal-math -fapprox-func\

``````````

</details>


https://github.com/llvm/llvm-project/pull/132167


More information about the cfe-commits mailing list