From c168508f2c43ebc6635e228acb1bd4061d49346d Mon Sep 17 00:00:00 2001 From: Ayokunle Amodu <121697771+ayokunle321@users.noreply.github.com> Date: Wed, 18 Jun 2025 13:20:01 -0600 Subject: [PATCH 1/2] add support for X86 builtin tzcnt --- clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp | 11 ++++ clang/test/CIR/CodeGen/X86/amd-bmi-builtins.c | 39 +++++++++++++ .../test/CIR/CodeGen/X86/intel-bmi-builtins.c | 55 +++++++++++++++++++ 3 files changed, 105 insertions(+) create mode 100644 clang/test/CIR/CodeGen/X86/amd-bmi-builtins.c create mode 100644 clang/test/CIR/CodeGen/X86/intel-bmi-builtins.c diff --git a/clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp b/clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp index 96469248535b..a1e2648636ec 100644 --- a/clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp @@ -144,5 +144,16 @@ mlir::Value CIRGenFunction::emitX86BuiltinExpr(unsigned BuiltinID, getLoc(E->getExprLoc()), builder.getStringAttr("x86.rdtsc"), intTy) .getResult(); } + case X86::BI__builtin_ia32_tzcnt_u16: + case X86::BI__builtin_ia32_tzcnt_u32: + case X86::BI__builtin_ia32_tzcnt_u64: { + mlir::Value V = builder.create( + getLoc(E->getExprLoc()), cir::BoolAttr::get(&getMLIRContext(), false)); + return builder + .create( + getLoc(E->getExprLoc()), builder.getStringAttr("cttz"), + Ops[0].getType(), mlir::ValueRange{Ops[0], V}) + .getResult(); + } } } diff --git a/clang/test/CIR/CodeGen/X86/amd-bmi-builtins.c b/clang/test/CIR/CodeGen/X86/amd-bmi-builtins.c new file mode 100644 index 000000000000..a97eb230e0b1 --- /dev/null +++ b/clang/test/CIR/CodeGen/X86/amd-bmi-builtins.c @@ -0,0 +1,39 @@ +// RUN: %clang_cc1 -ffreestanding -triple x86_64-unknown-linux -Wno-implicit-function-declaration -fclangir -emit-cir -o %t.cir %s +// RUN: FileCheck --check-prefix=CIR --input-file=%t.cir %s +// RUN: %clang_cc1 -ffreestanding -triple x86_64-unknown-linux -Wno-implicit-function-declaration -fclangir -emit-llvm -o %t.ll %s +// RUN: FileCheck --check-prefix=LLVM --input-file=%t.ll %s + +// Intel and AMD use different names for the same BMI intrinsics: +// Intel uses single underscores (e.g. _tzcnt_u16), +// AMD uses double underscores (e.g. __tzcnt_u16). +// Unlike the traditinal tests in clang/test/CodeGen/X86/bmi-builtins.c +// which combines both, we split them into separate files to avoid symbol +// conflicts and keep tests isolated. + +#include + +unsigned short test__tzcnt_u16(unsigned short __X) { + // CIR-LABEL: __tzcnt_u16 + // LLVM-LABEL: __tzcnt_u16 + return __tzcnt_u16(__X); + // CIR: {{%.*}} = cir.llvm.intrinsic "cttz" {{%.*}} : (!u16i, !cir.bool) -> !u16i + // LLVM: i16 @llvm.cttz.i16(i16 %{{.*}}, i1 false) +} + +unsigned int test__tzcnt_u32(unsigned int __X) { + // CIR-LABEL: __tzcnt_u32 + // LLVM-LABEL: __tzcnt_u32 + return __tzcnt_u32(__X); + // CIR: {{%.*}} = cir.llvm.intrinsic "cttz" {{%.*}} : (!u32i, !cir.bool) -> !u32i + // LLVM: i32 @llvm.cttz.i32(i32 %{{.*}}, i1 false) +} + +#ifdef __x86_64__ +unsigned long long test__tzcnt_u64(unsigned long long __X) { + // CIR-LABEL: __tzcnt_u64 + // LLVM-LABEL: __tzcnt_u64 + return __tzcnt_u64(__X); + // CIR: {{%.*}} = cir.llvm.intrinsic "cttz" {{%.*}} : (!u64i, !cir.bool) -> !u64i + // LLVM: i64 @llvm.cttz.i64(i64 %{{.*}}, i1 false) +} +#endif \ No newline at end of file diff --git a/clang/test/CIR/CodeGen/X86/intel-bmi-builtins.c b/clang/test/CIR/CodeGen/X86/intel-bmi-builtins.c new file mode 100644 index 000000000000..5b9d06f35990 --- /dev/null +++ b/clang/test/CIR/CodeGen/X86/intel-bmi-builtins.c @@ -0,0 +1,55 @@ +// RUN: %clang_cc1 -ffreestanding -triple x86_64-unknown-linux -Wno-implicit-function-declaration -fclangir -emit-cir -o %t.cir %s +// RUN: FileCheck --check-prefix=CIR --input-file=%t.cir %s +// RUN: %clang_cc1 -ffreestanding -triple x86_64-unknown-linux -Wno-implicit-function-declaration -fclangir -emit-llvm -o %t.ll %s +// RUN: FileCheck --check-prefix=LLVM --input-file=%t.ll %s + +// Intel and AMD use different names for the same BMI intrinsics: +// Intel uses single underscores (e.g. _tzcnt_u16), +// AMD uses double underscores (e.g. __tzcnt_u16). +// Unlike the traditinal tests in clang/test/CodeGen/X86/bmi-builtins.c +// which combines both, we split them into separate files to avoid symbol +// conflicts and keep tests isolated. + +#include + +unsigned short test_tzcnt_u16(unsigned short __X) { + // CIR-LABEL: _tzcnt_u16 + // LLVM-LABEL: _tzcnt_u16 + return _tzcnt_u16(__X); + // CIR: {{%.*}} = cir.llvm.intrinsic "cttz" {{%.*}} : (!u16i, !cir.bool) -> !u16i + // LLVM: i16 @llvm.cttz.i16(i16 %{{.*}}, i1 false) +} + +unsigned int test_tzcnt_u32(unsigned int __X) { + // CIR-LABEL: _tzcnt_u32 + // LLVM-LABEL: _tzcnt_u32 + return _tzcnt_u32(__X); + // CIR: {{%.*}} = cir.llvm.intrinsic "cttz" {{%.*}} : (!u32i, !cir.bool) -> !u32i + // LLVM: i32 @llvm.cttz.i32(i32 %{{.*}}, i1 false) +} + +int test_mm_tzcnt_32(unsigned int __X) { + // CIR-LABEL: _mm_tzcnt_32 + // LLVM-LABEL: _mm_tzcnt_32 + return _mm_tzcnt_32(__X); + // CIR: {{%.*}} = cir.llvm.intrinsic "cttz" {{%.*}} : (!u32i, !cir.bool) -> !u32i + // LLVM: i32 @llvm.cttz.i32(i32 %{{.*}}, i1 false) +} + +#ifdef __x86_64__ +unsigned long long test_tzcnt_u64(unsigned long long __X) { + // CIR-LABEL: _tzcnt_u64 + // LLVM-LABEL: _tzcnt_u64 + return _tzcnt_u64(__X); + // CIR: {{%.*}} = cir.llvm.intrinsic "cttz" {{%.*}} : (!u64i, !cir.bool) -> !u64i + // LLVM: i64 @llvm.cttz.i64(i64 %{{.*}}, i1 false) +} + +long long test_mm_tzcnt_64(unsigned long long __X) { + // CIR-LABEL: _mm_tzcnt_64 + // LLVM-LABEL: _mm_tzcnt_64 + return _mm_tzcnt_64(__X); + // CIR: {{%.*}} = cir.llvm.intrinsic "cttz" {{%.*}} : (!u64i, !cir.bool) -> !u64i + // LLVM: i64 @llvm.cttz.i64(i64 %{{.*}}, i1 false) +} +#endif \ No newline at end of file From 657845a8e3a5aa9347cf9cfc94572d88c40daff4 Mon Sep 17 00:00:00 2001 From: Ayokunle Amodu <121697771+ayokunle321@users.noreply.github.com> Date: Wed, 18 Jun 2025 21:04:14 -0600 Subject: [PATCH 2/2] remove test with intel's variants --- .../{amd-bmi-builtins.c => bmi-builtins.c} | 7 --- .../test/CIR/CodeGen/X86/intel-bmi-builtins.c | 55 ------------------- 2 files changed, 62 deletions(-) rename clang/test/CIR/CodeGen/X86/{amd-bmi-builtins.c => bmi-builtins.c} (78%) delete mode 100644 clang/test/CIR/CodeGen/X86/intel-bmi-builtins.c diff --git a/clang/test/CIR/CodeGen/X86/amd-bmi-builtins.c b/clang/test/CIR/CodeGen/X86/bmi-builtins.c similarity index 78% rename from clang/test/CIR/CodeGen/X86/amd-bmi-builtins.c rename to clang/test/CIR/CodeGen/X86/bmi-builtins.c index a97eb230e0b1..52d555bcd59f 100644 --- a/clang/test/CIR/CodeGen/X86/amd-bmi-builtins.c +++ b/clang/test/CIR/CodeGen/X86/bmi-builtins.c @@ -3,13 +3,6 @@ // RUN: %clang_cc1 -ffreestanding -triple x86_64-unknown-linux -Wno-implicit-function-declaration -fclangir -emit-llvm -o %t.ll %s // RUN: FileCheck --check-prefix=LLVM --input-file=%t.ll %s -// Intel and AMD use different names for the same BMI intrinsics: -// Intel uses single underscores (e.g. _tzcnt_u16), -// AMD uses double underscores (e.g. __tzcnt_u16). -// Unlike the traditinal tests in clang/test/CodeGen/X86/bmi-builtins.c -// which combines both, we split them into separate files to avoid symbol -// conflicts and keep tests isolated. - #include unsigned short test__tzcnt_u16(unsigned short __X) { diff --git a/clang/test/CIR/CodeGen/X86/intel-bmi-builtins.c b/clang/test/CIR/CodeGen/X86/intel-bmi-builtins.c deleted file mode 100644 index 5b9d06f35990..000000000000 --- a/clang/test/CIR/CodeGen/X86/intel-bmi-builtins.c +++ /dev/null @@ -1,55 +0,0 @@ -// RUN: %clang_cc1 -ffreestanding -triple x86_64-unknown-linux -Wno-implicit-function-declaration -fclangir -emit-cir -o %t.cir %s -// RUN: FileCheck --check-prefix=CIR --input-file=%t.cir %s -// RUN: %clang_cc1 -ffreestanding -triple x86_64-unknown-linux -Wno-implicit-function-declaration -fclangir -emit-llvm -o %t.ll %s -// RUN: FileCheck --check-prefix=LLVM --input-file=%t.ll %s - -// Intel and AMD use different names for the same BMI intrinsics: -// Intel uses single underscores (e.g. _tzcnt_u16), -// AMD uses double underscores (e.g. __tzcnt_u16). -// Unlike the traditinal tests in clang/test/CodeGen/X86/bmi-builtins.c -// which combines both, we split them into separate files to avoid symbol -// conflicts and keep tests isolated. - -#include - -unsigned short test_tzcnt_u16(unsigned short __X) { - // CIR-LABEL: _tzcnt_u16 - // LLVM-LABEL: _tzcnt_u16 - return _tzcnt_u16(__X); - // CIR: {{%.*}} = cir.llvm.intrinsic "cttz" {{%.*}} : (!u16i, !cir.bool) -> !u16i - // LLVM: i16 @llvm.cttz.i16(i16 %{{.*}}, i1 false) -} - -unsigned int test_tzcnt_u32(unsigned int __X) { - // CIR-LABEL: _tzcnt_u32 - // LLVM-LABEL: _tzcnt_u32 - return _tzcnt_u32(__X); - // CIR: {{%.*}} = cir.llvm.intrinsic "cttz" {{%.*}} : (!u32i, !cir.bool) -> !u32i - // LLVM: i32 @llvm.cttz.i32(i32 %{{.*}}, i1 false) -} - -int test_mm_tzcnt_32(unsigned int __X) { - // CIR-LABEL: _mm_tzcnt_32 - // LLVM-LABEL: _mm_tzcnt_32 - return _mm_tzcnt_32(__X); - // CIR: {{%.*}} = cir.llvm.intrinsic "cttz" {{%.*}} : (!u32i, !cir.bool) -> !u32i - // LLVM: i32 @llvm.cttz.i32(i32 %{{.*}}, i1 false) -} - -#ifdef __x86_64__ -unsigned long long test_tzcnt_u64(unsigned long long __X) { - // CIR-LABEL: _tzcnt_u64 - // LLVM-LABEL: _tzcnt_u64 - return _tzcnt_u64(__X); - // CIR: {{%.*}} = cir.llvm.intrinsic "cttz" {{%.*}} : (!u64i, !cir.bool) -> !u64i - // LLVM: i64 @llvm.cttz.i64(i64 %{{.*}}, i1 false) -} - -long long test_mm_tzcnt_64(unsigned long long __X) { - // CIR-LABEL: _mm_tzcnt_64 - // LLVM-LABEL: _mm_tzcnt_64 - return _mm_tzcnt_64(__X); - // CIR: {{%.*}} = cir.llvm.intrinsic "cttz" {{%.*}} : (!u64i, !cir.bool) -> !u64i - // LLVM: i64 @llvm.cttz.i64(i64 %{{.*}}, i1 false) -} -#endif \ No newline at end of file