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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions clang/include/clang/Basic/BuiltinsX86.td
Original file line number Diff line number Diff line change
Expand Up @@ -832,18 +832,15 @@ 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)">;
}

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 {
Expand Down
5 changes: 1 addition & 4 deletions clang/include/clang/Basic/BuiltinsX86_64.td
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 6 additions & 4 deletions clang/lib/Headers/lzcntintrin.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@
#ifndef __LZCNTINTRIN_H
#define __LZCNTINTRIN_H

/* Define the default attributes for the functions in this file. */
/* Define the default attributes for the functions in this file.
Allow using the lzcnt intrinsics even for non-LZCNT targets. Since the LZCNT
intrinsics are mapped to llvm.ctlz.*, false, which can be lowered to BSR on
non-LZCNT targets with zero-value input handled correctly. */
#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
Expand Down
6 changes: 3 additions & 3 deletions clang/test/CodeGen/X86/lzcnt-builtins.c
Original file line number Diff line number Diff line change
@@ -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


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

#include <immintrin.h>
Expand Down Expand Up @@ -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
#endif