Skip to content

[CIR][CIRGen][Builtin][X86] Lower mm_prefetch #1675

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 1 commit into from
Jul 3, 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
26 changes: 25 additions & 1 deletion clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ translateX86ToMsvcIntrin(unsigned BuiltinID) {
llvm_unreachable("must return from switch");
}

/// Get integer from a mlir::Value that is an int constant or a constant op.
static int64_t getIntValueFromConstOp(mlir::Value val) {
auto constOp = mlir::cast<cir::ConstantOp>(val.getDefiningOp());
return (mlir::cast<cir::IntAttr>(constOp.getValue()))
.getValue()
.getSExtValue();
}

mlir::Value CIRGenFunction::emitX86BuiltinExpr(unsigned BuiltinID,
const CallExpr *E) {
if (BuiltinID == Builtin::BI__builtin_cpu_is)
Expand Down Expand Up @@ -96,7 +104,23 @@ mlir::Value CIRGenFunction::emitX86BuiltinExpr(unsigned BuiltinID,
default:
return nullptr;
case X86::BI_mm_prefetch: {
llvm_unreachable("_mm_prefetch NYI");
mlir::Value Address = builder.createPtrBitcast(Ops[0], VoidTy);

int64_t Hint = getIntValueFromConstOp(Ops[1]);
mlir::Value RW = builder.create<cir::ConstantOp>(
getLoc(E->getExprLoc()),
cir::IntAttr::get(SInt32Ty, (Hint >> 2) & 0x1));
mlir::Value Locality = builder.create<cir::ConstantOp>(
getLoc(E->getExprLoc()), cir::IntAttr::get(SInt32Ty, Hint & 0x3));
mlir::Value Data = builder.create<cir::ConstantOp>(
getLoc(E->getExprLoc()), cir::IntAttr::get(SInt32Ty, 1));
mlir::Type voidTy = cir::VoidType::get(&getMLIRContext());

return builder
.create<cir::LLVMIntrinsicCallOp>(
getLoc(E->getExprLoc()), builder.getStringAttr("prefetch"), voidTy,
mlir::ValueRange{Address, RW, Locality, Data})
.getResult();
}
case X86::BI_mm_clflush: {
mlir::Type voidTy = cir::VoidType::get(&getMLIRContext());
Expand Down
20 changes: 20 additions & 0 deletions clang/test/CIR/CodeGen/X86/sse-builtins.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// RUN: %clang_cc1 -x c -flax-vector-conversions=none -ffreestanding %s -triple=x86_64-unknown-linux -target-feature +sse -fclangir -emit-cir -o %t.cir -Wall -Werror
// RUN: FileCheck --check-prefix=CIR --input-file=%t.cir %s
// RUN: %clang_cc1 -x c -flax-vector-conversions=none -ffreestanding %s -triple=x86_64-unknown-linux -target-feature +sse -fclangir -emit-llvm -o %t.ll -Wall -Werror
// RUN: FileCheck --check-prefixes=LLVM --input-file=%t.ll %s

// RUN: %clang_cc1 -x c++ -flax-vector-conversions=none -ffreestanding %s -triple=x86_64-unknown-linux -target-feature +sse -fno-signed-char -fclangir -emit-cir -o %t.cir -Wall -Werror
// RUN: FileCheck --check-prefix=CIR --input-file=%t.cir %s
// RUN: %clang_cc1 -x c++ -flax-vector-conversions=none -ffreestanding %s -triple=x86_64-unknown-linux -target-feature +sse -fclangir -emit-llvm -o %t.ll -Wall -Werror
// RUN: FileCheck --check-prefixes=LLVM --input-file=%t.ll %s

#include <immintrin.h>


void test_mm_prefetch(char const* p) {
// CIR-LABEL: test_mm_prefetch
// LLVM-LABEL: test_mm_prefetch
_mm_prefetch(p, 0);
// CIR: cir.prefetch(%{{.*}} : !cir.ptr<!void>) locality(0) read
// LLVM: call void @llvm.prefetch.p0(ptr {{.*}}, i32 0, i32 0, i32 1)
}
Loading