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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 @@ -65,6 +65,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 @@ -95,7 +103,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
8 changes: 8 additions & 0 deletions clang/test/CIR/CodeGen/X86/builtins-x86.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@
// This test mimics clang/test/CodeGen/builtins-x86.c, which eventually
// CIR shall be able to support fully.

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

void test_mm_clflush(const void* tmp_vCp) {
// CIR-LABEL: test_mm_clflush
// LLVM-LABEL: test_mm_clflush
Expand Down