Skip to content

Commit c21ed7f

Browse files
authored
[CIR][CIRGen][Builtin][X86] Lower lzcnt_u16, lzcnt_u32, lzcnt_u64 (#1683)
No test cases provided for `lzcnt_u16` as presented in the OG codegen equivalent: `test/CodeGen/X86/lzcnt-builtins.c`. related: #1404
1 parent dd5986c commit c21ed7f

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,5 +144,20 @@ mlir::Value CIRGenFunction::emitX86BuiltinExpr(unsigned BuiltinID,
144144
getLoc(E->getExprLoc()), builder.getStringAttr("x86.rdtsc"), intTy)
145145
.getResult();
146146
}
147+
case X86::BI__builtin_ia32_rdtscp: {
148+
llvm_unreachable("__rdtscp NYI");
149+
}
150+
case X86::BI__builtin_ia32_lzcnt_u16:
151+
case X86::BI__builtin_ia32_lzcnt_u32:
152+
case X86::BI__builtin_ia32_lzcnt_u64: {
153+
mlir::Value V = builder.create<cir::ConstantOp>(
154+
getLoc(E->getExprLoc()), cir::BoolAttr::get(&getMLIRContext(), false));
155+
156+
return builder
157+
.create<cir::LLVMIntrinsicCallOp>(
158+
getLoc(E->getExprLoc()), builder.getStringAttr("ctlz"),
159+
Ops[0].getType(), mlir::ValueRange{Ops[0], V})
160+
.getResult();
161+
}
147162
}
148163
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// RUN: %clang_cc1 -ffreestanding -triple x86_64-unknown-linux -Wno-implicit-function-declaration -fclangir -emit-cir -o %t.cir %s
2+
// RUN: FileCheck --check-prefix=CIR --input-file=%t.cir %s
3+
// RUN: %clang_cc1 -ffreestanding -triple x86_64-unknown-linux -Wno-implicit-function-declaration -fclangir -emit-llvm -o %t.ll %s
4+
// RUN: FileCheck --check-prefix=LLVM --input-file=%t.ll %s
5+
6+
#include <immintrin.h>
7+
8+
unsigned int test_lzcnt_u32(unsigned int __X)
9+
{
10+
// CIR-LABEL: _lzcnt_u32
11+
// LLVM-LABEL: _lzcnt_u32
12+
return _lzcnt_u32(__X);
13+
// CIR: {{%.*}} = cir.llvm.intrinsic "ctlz" {{%.*}} : (!u32i, !cir.bool) -> !u32i
14+
// LLVM: @llvm.ctlz.i32(i32 %{{.*}}, i1 false)
15+
}
16+
17+
unsigned long long test__lzcnt_u64(unsigned long long __X)
18+
{
19+
// CIR-LABEL: _lzcnt_u64
20+
// LLVM-LABEL: _lzcnt_u64
21+
return _lzcnt_u64(__X);
22+
// CIR: {{%.*}} = cir.llvm.intrinsic "ctlz" {{%.*}} : (!u64i, !cir.bool) -> !u64i
23+
// LLVM: @llvm.ctlz.i64(i64 %{{.*}}, i1 false)
24+
}

0 commit comments

Comments
 (0)