Skip to content

[CIR][CodeGen] Support for _mm_prefetch #1560

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
2 changes: 2 additions & 0 deletions clang/include/clang/CIR/Dialect/IR/CIROps.td
Original file line number Diff line number Diff line change
Expand Up @@ -5304,6 +5304,8 @@ def PrefetchOp : CIR_Op<"prefetch"> {
IntMaxValue<3>]>:$locality,
UnitAttr:$isWrite);

let results = (outs VoidPtr:$result);

let assemblyFormat = [{
`(` $addr `:` qualified(type($addr)) `)`
`locality``(` $locality `)`
Expand Down
16 changes: 15 additions & 1 deletion clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,21 @@ mlir::Value CIRGenFunction::emitX86BuiltinExpr(unsigned BuiltinID,
default:
return nullptr;
case X86::BI_mm_prefetch: {
llvm_unreachable("_mm_prefetch NYI");
mlir::Location loc = getLoc(E->getExprLoc());
auto C = dyn_cast<cir::IntAttr>(
cast<cir::ConstantOp>(Ops[1].getDefiningOp()).getValue());
auto Locality = mlir::IntegerAttr::get(
mlir::IntegerType::get(builder.getContext(), 32), C.getUInt() & 0x3);
bool ReadWrite = ((C.getUInt() >> 2) & 0x1) == 1;
mlir::UnitAttr isWrite =
ReadWrite ? mlir::UnitAttr::get(&getMLIRContext()) : nullptr;
mlir::Type voidPtrType =
cir::PointerType::get(cir::VoidType::get(&getMLIRContext()));
auto CastToVoid = builder.create<cir::CastOp>(
loc, voidPtrType, cir::CastKind::bitcast, Ops[0]);
auto res =
builder.create<cir::PrefetchOp>(loc, CastToVoid, Locality, isWrite);
return res->getResult(0);
}
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.prefetch({{%.*}} : !cir.ptr<!void>) locality(0) read
// 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
Loading