https://github.com/adit4443ya created https://github.com/llvm/llvm-project/pull/180714
This PR implements CodeGen for rdtsc builtins in CIR upstream. Towards #167765 >From 050e5e989e5d677de31edd9fc0413562d2fb6ef9 Mon Sep 17 00:00:00 2001 From: Aditya Trivedi <[email protected]> Date: Tue, 10 Feb 2026 10:08:19 +0000 Subject: [PATCH] [CIR] Support x86 builtin rdtsc --- clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp | 22 ++++++++++++--- clang/test/CIR/CodeGenBuiltins/X86/rdtsc.c | 31 ++++++++++++++++++++++ 2 files changed, 49 insertions(+), 4 deletions(-) create mode 100644 clang/test/CIR/CodeGenBuiltins/X86/rdtsc.c diff --git a/clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp b/clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp index cad80317cb870..4d1cf98791d3a 100644 --- a/clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp @@ -846,11 +846,25 @@ CIRGenFunction::emitX86BuiltinExpr(unsigned builtinID, const CallExpr *expr) { case X86::BI_m_prefetchw: return emitPrefetch(*this, builtinID, expr, ops); case X86::BI__rdtsc: + return builder.emitIntrinsicCallOp(getLoc(expr->getExprLoc()), "x86.rdtsc", + builder.getUInt64Ty()); case X86::BI__builtin_ia32_rdtscp: { - cgm.errorNYI(expr->getSourceRange(), - std::string("unimplemented X86 builtin call: ") + - getContext().BuiltinInfo.getName(builtinID)); - return mlir::Value{}; + mlir::Location loc = getLoc(expr->getExprLoc()); + mlir::Type i64Ty = builder.getUInt64Ty(); + mlir::Type i32Ty = builder.getUInt32Ty(); + mlir::Type structTy = builder.getAnonRecordTy({i64Ty, i32Ty}); + mlir::Value result = + builder.emitIntrinsicCallOp(loc, "x86.rdtscp", structTy); + + // Extract and store processor_id (element 1 of the returned struct) + mlir::Value processorId = + cir::ExtractMemberOp::create(builder, loc, i32Ty, result, 1); + // ops[0] is the address to store the processor ID + Address addr(ops[0], CharUnits::fromQuantity(4)); + builder.createStore(loc, processorId, addr); + + // Return timestamp (element 0 of the returned struct) + return cir::ExtractMemberOp::create(builder, loc, i64Ty, result, 0); } case X86::BI__builtin_ia32_lzcnt_u16: case X86::BI__builtin_ia32_lzcnt_u32: diff --git a/clang/test/CIR/CodeGenBuiltins/X86/rdtsc.c b/clang/test/CIR/CodeGenBuiltins/X86/rdtsc.c new file mode 100644 index 0000000000000..b7d203cbcda94 --- /dev/null +++ b/clang/test/CIR/CodeGenBuiltins/X86/rdtsc.c @@ -0,0 +1,31 @@ +// RUN: %clang_cc1 -x c -ffreestanding -triple x86_64-unknown-linux -fclangir -emit-cir -o %t.cir %s +// RUN: FileCheck --check-prefix=CIR --input-file=%t.cir %s + +// RUN: %clang_cc1 -x c -ffreestanding -triple x86_64-unknown-linux -fclangir -emit-llvm -o %t.ll %s +// RUN: FileCheck --check-prefix=LLVM --input-file=%t.ll %s + +unsigned long long test_rdtsc() { + // CIR-LABEL: @test_rdtsc + // CIR: {{%.*}} = cir.call_llvm_intrinsic "x86.rdtsc"() : () -> !u64i + + // LLVM-LABEL: @test_rdtsc + // LLVM: call i64 @llvm.x86.rdtsc() + + return __rdtsc(); +} + +unsigned long long test_rdtscp(unsigned int *aux) { + // CIR-LABEL: @test_rdtscp + // CIR: {{%.*}} = cir.call_llvm_intrinsic "x86.rdtscp"() : () -> !cir.record<!anon + // CIR: {{%.*}} = cir.extract_member {{%.*}}[1] : !cir.record<!anon + // CIR: cir.store {{%.*}}, {{%.*}} : !u32i + // CIR: {{%.*}} = cir.extract_member {{%.*}}[0] : !cir.record<!anon + + // LLVM-LABEL: @test_rdtscp + // LLVM: call { i64, i32 } @llvm.x86.rdtscp() + // LLVM: extractvalue { i64, i32 } {{%.*}}, 1 + // LLVM: store i32 {{%.*}}, ptr {{%.*}} + // LLVM: extractvalue { i64, i32 } {{%.*}}, 0 + + return __builtin_ia32_rdtscp(aux); +} _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
