Skip to content

Commit 2a73774

Browse files
kimsh02dvbuka
authored andcommitted
[CIR] Upstream handling for __builtin_prefetch (llvm#164387)
Fix llvm#163886
1 parent ac3ae2c commit 2a73774

File tree

4 files changed

+110
-0
lines changed

4 files changed

+110
-0
lines changed

clang/include/clang/CIR/Dialect/IR/CIROps.td

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4052,6 +4052,43 @@ def CIR_ExpectOp : CIR_Op<"expect", [
40524052
}];
40534053
}
40544054

4055+
//===----------------------------------------------------------------------===//
4056+
// PrefetchOp
4057+
//===----------------------------------------------------------------------===//
4058+
4059+
def CIR_PrefetchOp : CIR_Op<"prefetch"> {
4060+
let summary = "Prefetch operation";
4061+
let description = [{
4062+
The `cir.prefetch` operation is a hint to the code generator to insert a
4063+
prefetch instruction if supported; otherwise, it is a noop. Prefetches
4064+
have no effect on the behavior of the program but can change its
4065+
performance characteristics.
4066+
4067+
```mlir
4068+
cir.prefetch(%0 : !cir.ptr<!void>) locality(1) write
4069+
```
4070+
4071+
$locality is a temporal locality specifier ranging from (0) - no locality,
4072+
to (3) - extremely local, keep in cache. If $locality is not present, the
4073+
default value is 3.
4074+
4075+
$isWrite specifies whether the prefetch is for a 'read' or 'write'. If
4076+
$isWrite is not specified, it means that prefetch is prepared for 'read'.
4077+
}];
4078+
4079+
let arguments = (ins CIR_VoidPtrType:$addr,
4080+
DefaultValuedAttr<ConfinedAttr<I32Attr, [IntMinValue<0>, IntMaxValue<3>]>,
4081+
"3">:$locality,
4082+
UnitAttr:$isWrite);
4083+
4084+
let assemblyFormat = [{
4085+
(`write` $isWrite^) : (`read`)?
4086+
`locality` `(` $locality `)`
4087+
$addr `:` qualified(type($addr))
4088+
attr-dict
4089+
}];
4090+
}
4091+
40554092
//===----------------------------------------------------------------------===//
40564093
// PtrDiffOp
40574094
//===----------------------------------------------------------------------===//

clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,27 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl &gd, unsigned builtinID,
454454
assert(!cir::MissingFeatures::coroSizeBuiltinCall());
455455
return getUndefRValue(e->getType());
456456
}
457+
case Builtin::BI__builtin_prefetch: {
458+
auto evaluateOperandAsInt = [&](const Expr *arg) {
459+
Expr::EvalResult res;
460+
[[maybe_unused]] bool evalSucceed =
461+
arg->EvaluateAsInt(res, cgm.getASTContext());
462+
assert(evalSucceed && "expression should be able to evaluate as int");
463+
return res.Val.getInt().getZExtValue();
464+
};
465+
466+
bool isWrite = false;
467+
if (e->getNumArgs() > 1)
468+
isWrite = evaluateOperandAsInt(e->getArg(1));
469+
470+
int locality = 3;
471+
if (e->getNumArgs() > 2)
472+
locality = evaluateOperandAsInt(e->getArg(2));
473+
474+
mlir::Value address = emitScalarExpr(e->getArg(0));
475+
cir::PrefetchOp::create(builder, loc, address, locality, isWrite);
476+
return RValue::get(nullptr);
477+
}
457478
}
458479

459480
// If this is an alias for a lib function (e.g. __builtin_sin), emit

clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1695,6 +1695,15 @@ static uint64_t getTypeSize(mlir::Type type, mlir::Operation &op) {
16951695
return llvm::divideCeil(layout.getTypeSizeInBits(type), 8);
16961696
}
16971697

1698+
mlir::LogicalResult CIRToLLVMPrefetchOpLowering::matchAndRewrite(
1699+
cir::PrefetchOp op, OpAdaptor adaptor,
1700+
mlir::ConversionPatternRewriter &rewriter) const {
1701+
rewriter.replaceOpWithNewOp<mlir::LLVM::Prefetch>(
1702+
op, adaptor.getAddr(), adaptor.getIsWrite(), adaptor.getLocality(),
1703+
/*DataCache=*/1);
1704+
return mlir::success();
1705+
}
1706+
16981707
mlir::LogicalResult CIRToLLVMPtrDiffOpLowering::matchAndRewrite(
16991708
cir::PtrDiffOp op, OpAdaptor adaptor,
17001709
mlir::ConversionPatternRewriter &rewriter) const {
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-cir %s -o - | FileCheck %s -check-prefix=CIR
2+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o - | FileCheck %s -check-prefix=LLVM
3+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm %s -o - | FileCheck %s -check-prefix=OGCG
4+
5+
void foo(void *a) {
6+
__builtin_prefetch(a); // rw=0, locality=3
7+
__builtin_prefetch(a, 0); // rw=0, locality=3
8+
__builtin_prefetch(a, 1); // rw=1, locality=3
9+
__builtin_prefetch(a, 1, 1); // rw=1, locality=1
10+
}
11+
12+
// CIR-LABEL: cir.func dso_local @foo(
13+
// CIR: %[[ALLOCA:.*]] = cir.alloca !cir.ptr<!void>
14+
// CIR: cir.store %arg0, %[[ALLOCA]] : !cir.ptr<!void>, !cir.ptr<!cir.ptr<!void>>
15+
// CIR: %[[P1:.*]] = cir.load{{.*}} %[[ALLOCA]] : !cir.ptr<!cir.ptr<!void>>, !cir.ptr<!void>
16+
// CIR: cir.prefetch read locality(3) %[[P1]] : !cir.ptr<!void>
17+
// CIR: %[[P2:.*]] = cir.load{{.*}} %[[ALLOCA]] : !cir.ptr<!cir.ptr<!void>>, !cir.ptr<!void>
18+
// CIR: cir.prefetch read locality(3) %[[P2]] : !cir.ptr<!void>
19+
// CIR: %[[P3:.*]] = cir.load{{.*}} %[[ALLOCA]] : !cir.ptr<!cir.ptr<!void>>, !cir.ptr<!void>
20+
// CIR: cir.prefetch write locality(3) %[[P3]] : !cir.ptr<!void>
21+
// CIR: %[[P4:.*]] = cir.load{{.*}} %[[ALLOCA]] : !cir.ptr<!cir.ptr<!void>>, !cir.ptr<!void>
22+
// CIR: cir.prefetch write locality(1) %[[P4]] : !cir.ptr<!void>
23+
// CIR: cir.return
24+
25+
// LLVM-LABEL: define dso_local void @foo(
26+
// LLVM: [[ALLOCA:%.*]] = alloca ptr, i64 1
27+
// LLVM: store ptr {{.*}}, ptr [[ALLOCA]]
28+
// LLVM: [[LP1:%.*]] = load ptr, ptr [[ALLOCA]]
29+
// LLVM: call void @llvm.prefetch.p0(ptr [[LP1]], i32 0, i32 3, i32 1)
30+
// LLVM: [[LP2:%.*]] = load ptr, ptr [[ALLOCA]]
31+
// LLVM: call void @llvm.prefetch.p0(ptr [[LP2]], i32 0, i32 3, i32 1)
32+
// LLVM: [[LP3:%.*]] = load ptr, ptr [[ALLOCA]]
33+
// LLVM: call void @llvm.prefetch.p0(ptr [[LP3]], i32 1, i32 3, i32 1)
34+
// LLVM: [[LP4:%.*]] = load ptr, ptr [[ALLOCA]]
35+
// LLVM: call void @llvm.prefetch.p0(ptr [[LP4]], i32 1, i32 1, i32 1)
36+
// LLVM: ret void
37+
38+
// OGCG-LABEL: define dso_local void @foo(ptr
39+
// OGCG: call void @llvm.prefetch.p0(ptr {{.*}}, i32 0, i32 3, i32 1)
40+
// OGCG: call void @llvm.prefetch.p0(ptr {{.*}}, i32 0, i32 3, i32 1)
41+
// OGCG: call void @llvm.prefetch.p0(ptr {{.*}}, i32 1, i32 3, i32 1)
42+
// OGCG: call void @llvm.prefetch.p0(ptr {{.*}}, i32 1, i32 1, i32 1)
43+
// OGCG: ret void

0 commit comments

Comments
 (0)