Skip to content

[X86] Allow using the lzcnt intrinsics for non-LZCNT targets #128284

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 2 commits into from
Feb 22, 2025

Conversation

phoebewang
Copy link
Contributor

Similar to D14748, we can relax lzcnt intrinsics too, especially with improved BSR lowering by #123623

Similar to D14748, we can relax lzcnt intrinsics too, especially with
improved BSR lowering by llvm#123623
@llvmbot llvmbot added clang Clang issues not falling into any other category backend:X86 clang:frontend Language frontend issues, e.g. anything involving "Sema" clang:headers Headers provided by Clang, e.g. for intrinsics labels Feb 22, 2025
@llvmbot
Copy link
Member

llvmbot commented Feb 22, 2025

@llvm/pr-subscribers-backend-x86

@llvm/pr-subscribers-clang

Author: Phoebe Wang (phoebewang)

Changes

Similar to D14748, we can relax lzcnt intrinsics too, especially with improved BSR lowering by #123623


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

4 Files Affected:

  • (modified) clang/include/clang/Basic/BuiltinsX86.td (+2-5)
  • (modified) clang/include/clang/Basic/BuiltinsX86_64.td (+1-4)
  • (modified) clang/lib/Headers/lzcntintrin.h (+2-3)
  • (modified) clang/test/CodeGen/X86/lzcnt-builtins.c (+3-3)
diff --git a/clang/include/clang/Basic/BuiltinsX86.td b/clang/include/clang/Basic/BuiltinsX86.td
index 572ac7235be02..e2252620bab56 100644
--- a/clang/include/clang/Basic/BuiltinsX86.td
+++ b/clang/include/clang/Basic/BuiltinsX86.td
@@ -832,11 +832,6 @@ let Features = "rdseed", Attributes = [NoThrow] in {
   def rdseed32_step : X86Builtin<"unsigned int(unsigned int *)">;
 }
 
-let Features = "lzcnt", Attributes = [NoThrow, Const, Constexpr] in {
-  def lzcnt_u16 : X86Builtin<"unsigned short(unsigned short)">;
-  def lzcnt_u32 : X86Builtin<"unsigned int(unsigned int)">;
-}
-
 let Features = "bmi", Attributes = [NoThrow, Const, Constexpr] in {
   def bextr_u32 : X86Builtin<"unsigned int(unsigned int, unsigned int)">;
 }
@@ -844,6 +839,8 @@ let Features = "bmi", Attributes = [NoThrow, Const, Constexpr] in {
 let Attributes = [NoThrow, Const, Constexpr] in {
   def tzcnt_u16 : X86Builtin<"unsigned short(unsigned short)">;
   def tzcnt_u32 : X86Builtin<"unsigned int(unsigned int)">;
+  def lzcnt_u16 : X86Builtin<"unsigned short(unsigned short)">;
+  def lzcnt_u32 : X86Builtin<"unsigned int(unsigned int)">;
 }
 
 let Features = "bmi2", Attributes = [NoThrow, Const, Constexpr] in {
diff --git a/clang/include/clang/Basic/BuiltinsX86_64.td b/clang/include/clang/Basic/BuiltinsX86_64.td
index 4958265298d1b..f2b35874e3876 100644
--- a/clang/include/clang/Basic/BuiltinsX86_64.td
+++ b/clang/include/clang/Basic/BuiltinsX86_64.td
@@ -126,16 +126,13 @@ let Features = "rdseed", Attributes = [NoThrow] in {
   def rdseed64_step : X86Builtin<"unsigned int(unsigned long long int *)">;
 }
 
-let Features = "lzcnt", Attributes = [NoThrow, Const, Constexpr] in {
-  def lzcnt_u64 : X86Builtin<"unsigned long long int(unsigned long long int)">;
-}
-
 let Features = "bmi", Attributes = [NoThrow, Const, Constexpr] in {
   def bextr_u64 : X86Builtin<"unsigned long long int(unsigned long long int, unsigned long long int)">;
 }
 
 let Attributes = [NoThrow, Const, Constexpr] in {
   def tzcnt_u64 : X86Builtin<"unsigned long long int(unsigned long long int)">;
+  def lzcnt_u64 : X86Builtin<"unsigned long long int(unsigned long long int)">;
 }
 
 let Features = "bmi2", Attributes = [NoThrow, Const, Constexpr] in {
diff --git a/clang/lib/Headers/lzcntintrin.h b/clang/lib/Headers/lzcntintrin.h
index 27509021ec258..8d2d39f094aa0 100644
--- a/clang/lib/Headers/lzcntintrin.h
+++ b/clang/lib/Headers/lzcntintrin.h
@@ -17,10 +17,9 @@
 /* Define the default attributes for the functions in this file. */
 #if defined(__cplusplus) && (__cplusplus >= 201103L)
 #define __DEFAULT_FN_ATTRS                                                     \
-  __attribute__((__always_inline__, __nodebug__, __target__("lzcnt"))) constexpr
+  __attribute__((__always_inline__, __nodebug__)) constexpr
 #else
-#define __DEFAULT_FN_ATTRS                                                     \
-  __attribute__((__always_inline__, __nodebug__, __target__("lzcnt")))
+#define __DEFAULT_FN_ATTRS __attribute__((__always_inline__, __nodebug__))
 #endif
 
 #ifndef _MSC_VER
diff --git a/clang/test/CodeGen/X86/lzcnt-builtins.c b/clang/test/CodeGen/X86/lzcnt-builtins.c
index 18ced89fc79b1..212155f123adc 100644
--- a/clang/test/CodeGen/X86/lzcnt-builtins.c
+++ b/clang/test/CodeGen/X86/lzcnt-builtins.c
@@ -1,5 +1,5 @@
-// RUN: %clang_cc1 -x c -ffreestanding %s -triple=x86_64-apple-darwin -target-feature +lzcnt -emit-llvm -o - | FileCheck %s
-// RUN: %clang_cc1 -x c++ -std=c++11 -ffreestanding %s -triple=x86_64-apple-darwin -target-feature +lzcnt -emit-llvm -o - | FileCheck %s
+// RUN: %clang_cc1 -x c -ffreestanding %s -triple=x86_64-apple-darwin -emit-llvm -o - | FileCheck %s
+// RUN: %clang_cc1 -x c++ -std=c++11 -ffreestanding %s -triple=x86_64-apple-darwin -emit-llvm -o - | FileCheck %s
 
 
 #include <immintrin.h>
@@ -56,4 +56,4 @@ char lzcntu32_2[_lzcnt_u32(0x00000010) == 27 ? 1 : -1];
 char lzcntu64_0[_lzcnt_u64(0x0000000000000000ULL) == 64 ? 1 : -1];
 char lzcntu64_1[_lzcnt_u64(0x8000000000000000ULL) ==  0 ? 1 : -1];
 char lzcntu64_2[_lzcnt_u64(0x0000000100000000ULL) == 31 ? 1 : -1];
-#endif
\ No newline at end of file
+#endif

// RUN: %clang_cc1 -x c -ffreestanding %s -triple=x86_64-apple-darwin -target-feature +lzcnt -emit-llvm -o - | FileCheck %s
// RUN: %clang_cc1 -x c++ -std=c++11 -ffreestanding %s -triple=x86_64-apple-darwin -target-feature +lzcnt -emit-llvm -o - | FileCheck %s
// RUN: %clang_cc1 -x c -ffreestanding %s -triple=x86_64-apple-darwin -emit-llvm -o - | FileCheck %s
// RUN: %clang_cc1 -x c++ -std=c++11 -ffreestanding %s -triple=x86_64-apple-darwin -emit-llvm -o - | FileCheck %s


Copy link
Collaborator

Choose a reason for hiding this comment

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

Add a comment explaining that these will lower correctly with/without lzcnt

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added in lzcntintrin.h

Copy link
Collaborator

@RKSimon RKSimon left a comment

Choose a reason for hiding this comment

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

LGTM

@phoebewang phoebewang merged commit 7981c29 into llvm:main Feb 22, 2025
11 checks passed
@phoebewang phoebewang deleted the lzcnt branch February 22, 2025 12:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend:X86 clang:frontend Language frontend issues, e.g. anything involving "Sema" clang:headers Headers provided by Clang, e.g. for intrinsics clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants