Skip to content

Commit 729ef12

Browse files
committed
[MLIR][NVVM] Add prefetch Ops
This change adds `prefetch.L1`, `prefetch.L2`, and `prefetch.L1.uniform` Ops to the NVVM dialect for the `prefetch` and `prefetchu` group of instructions. PTX Spec Reference: https://docs.nvidia.com/cuda/parallel-thread-execution/#data-movement-and-conversion-instructions-prefetch-prefetchu
1 parent 663aea2 commit 729ef12

File tree

6 files changed

+235
-0
lines changed

6 files changed

+235
-0
lines changed

mlir/include/mlir/Dialect/LLVMIR/NVVMDialect.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ enum NVVMMemorySpace {
4242
kSharedMemorySpace = 3,
4343
/// Constant memory space identifier.
4444
kConstantMemorySpace = 4,
45+
/// Local memory space identifier.
46+
kLocalMemorySpace = 5,
4547
/// Tensor memory space identifier.
4648
/// Tensor memory is available only in arch-accelerated
4749
/// variants from sm100 onwards.

mlir/include/mlir/Dialect/LLVMIR/NVVMOps.td

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ include "mlir/Dialect/LLVMIR/LLVMTypes.td"
2525
def LLVM_PointerGeneric : LLVM_PointerInAddressSpace<0>;
2626
def LLVM_PointerGlobal : LLVM_PointerInAddressSpace<1>;
2727
def LLVM_PointerShared : LLVM_PointerInAddressSpace<3>;
28+
def LLVM_PointerLocal : LLVM_PointerInAddressSpace<5>;
2829
def LLVM_PointerTensor : LLVM_PointerInAddressSpace<6>;
2930
def LLVM_PointerSharedCluster : LLVM_PointerInAddressSpace<7>;
3031

@@ -118,6 +119,25 @@ class NVVM_Attr<string attrName, string attrMnemonic, list<Trait> traits = []>
118119
let mnemonic = attrMnemonic;
119120
}
120121

122+
// Cache Eviction Priority enum definitions
123+
def EvictNormal : I32EnumCase<"EvictNormal", 0, "evict_normal">;
124+
def EvictFirst : I32EnumCase<"EvictFirst", 1, "evict_first">;
125+
def EvictLast : I32EnumCase<"EvictLast", 2, "evict_last">;
126+
def EvictUnchanged : I32EnumCase<"EvictUnchanged", 3, "evict_unchanged">;
127+
def NoAllocate : I32EnumCase<"NoAllocate", 4, "no_allocate">;
128+
129+
def CacheEvictionPriority : I32Enum<"CacheEvictionPriority",
130+
"NVVM Cache Eviction Priority",
131+
[EvictNormal, EvictFirst, EvictLast,
132+
EvictUnchanged, NoAllocate]> {
133+
let cppNamespace = "::mlir::NVVM";
134+
}
135+
136+
def CacheEvictionPriorityAttr : EnumAttr<NVVM_Dialect, CacheEvictionPriority,
137+
"cache_eviction_priority"> {
138+
let assemblyFormat = "$value";
139+
}
140+
121141
//===----------------------------------------------------------------------===//
122142
// NVVM intrinsic operations
123143
//===----------------------------------------------------------------------===//
@@ -2333,6 +2353,79 @@ def NVVM_CpAsyncBulkTensorSharedCTAToGlobalOp :
23332353
let hasVerifier = 1;
23342354
}
23352355

2356+
//===----------------------------------------------------------------------===//
2357+
// NVVM Prefetch Ops
2358+
//===----------------------------------------------------------------------===//
2359+
2360+
def NVVM_PrefetchL1Op : NVVM_Op<"prefetch.L1"> {
2361+
let summary = "Brings the cache line containing the specified address into L1 cache";
2362+
let description = [{
2363+
Brings the cache line containing the specified address into L1 cache.
2364+
2365+
Operand `addr` can be a global, local or generic address pointer.
2366+
No operation is performed if `addr` maps to a `shared` memory location.
2367+
2368+
[For more information, see PTX ISA](https://docs.nvidia.com/cuda/parallel-thread-execution/#data-movement-and-conversion-instructions-prefetch-prefetchu)
2369+
}];
2370+
let arguments = (ins AnyTypeOf<[LLVM_PointerGlobal,
2371+
LLVM_PointerLocal,
2372+
LLVM_PointerGeneric]>:$addr);
2373+
let assemblyFormat = "$addr attr-dict `:` type($addr)";
2374+
2375+
let extraClassDeclaration = [{
2376+
static llvm::Intrinsic::ID getIntrinsicID(Operation &op);
2377+
}];
2378+
let llvmBuilder = [{
2379+
auto intId = NVVM::PrefetchL1Op::getIntrinsicID(*op);
2380+
createIntrinsicCall(builder, intId, $addr);
2381+
}];
2382+
}
2383+
2384+
def NVVM_PrefetchL2Op : NVVM_Op<"prefetch.L2"> {
2385+
let summary = "Brings the cache line containing the specified address into L2 cache";
2386+
let description = [{
2387+
Brings the cache line containing the specified address into L2 cache.
2388+
2389+
Operand `addr` can be a global, local or generic address pointer.
2390+
No operation is performed if `addr` maps to a `shared` memory location.
2391+
2392+
[For more information, see PTX ISA](https://docs.nvidia.com/cuda/parallel-thread-execution/#data-movement-and-conversion-instructions-prefetch-prefetchu)
2393+
}];
2394+
let arguments = (ins AnyTypeOf<[LLVM_PointerGlobal,
2395+
LLVM_PointerLocal,
2396+
LLVM_PointerGeneric]>:$addr,
2397+
OptionalAttr<CacheEvictionPriorityAttr>:$evictPriority);
2398+
let assemblyFormat = "$addr (`,` `evict_priority` `=` $evictPriority^)? attr-dict `:` type($addr)";
2399+
let hasVerifier = 1;
2400+
2401+
let extraClassDeclaration = [{
2402+
static llvm::Intrinsic::ID getIntrinsicID(Operation &op);
2403+
}];
2404+
let llvmBuilder = [{
2405+
auto intId = NVVM::PrefetchL2Op::getIntrinsicID(*op);
2406+
createIntrinsicCall(builder, intId, $addr);
2407+
}];
2408+
}
2409+
2410+
def NVVM_PrefetchL1UniformOp : NVVM_Op<"prefetch.L1.uniform"> {
2411+
let summary = "Brings the cache line containing the specified address into L1 uniform cache";
2412+
let description = [{
2413+
Brings the cache line containing the specified address into L1 uniform
2414+
cache.
2415+
2416+
Operand `addr` must be a generic address pointer and no operation is
2417+
performed if `addr` maps to a `const`, `local`, or `shared` memory location.
2418+
2419+
[For more information, see PTX ISA](https://docs.nvidia.com/cuda/parallel-thread-execution/#data-movement-and-conversion-instructions-prefetch-prefetchu)
2420+
}];
2421+
let arguments = (ins LLVM_PointerGeneric:$addr);
2422+
let assemblyFormat = "$addr attr-dict `:` type($addr)";
2423+
2424+
let llvmBuilder = [{
2425+
createIntrinsicCall(builder, llvm::Intrinsic::nvvm_prefetchu_L1, $addr);
2426+
}];
2427+
}
2428+
23362429
def NVVM_PrefetchTensorMapOp : NVVM_Op<"prefetch.tensormap",
23372430
[DeclareOpInterfaceMethods<BasicPtxBuilderOpInterface>]>,
23382431
Arguments<(ins LLVM_AnyPointer:$tmaDescriptor, PtxPredicate:$predicate)> {

mlir/lib/Dialect/LLVMIR/IR/NVVMDialect.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,6 +1205,20 @@ LogicalResult NVVM::VoteSyncOp::verify() {
12051205
return success();
12061206
}
12071207

1208+
LogicalResult NVVM::PrefetchL2Op::verify() {
1209+
auto evictPriority = getEvictPriority();
1210+
if (evictPriority &&
1211+
(llvm::cast<LLVM::LLVMPointerType>(getAddr().getType())
1212+
.getAddressSpace() != NVVM::NVVMMemorySpace::kGlobalMemorySpace))
1213+
return emitOpError(
1214+
"prefetch with cache eviction priority requires a global pointer");
1215+
if (evictPriority &&
1216+
*evictPriority != NVVM::CacheEvictionPriority::EvictNormal &&
1217+
*evictPriority != NVVM::CacheEvictionPriority::EvictLast)
1218+
return emitOpError("invalid cache eviction priority");
1219+
return success();
1220+
}
1221+
12081222
/// Packs the given `field` into the `result`.
12091223
/// The `result` is 64-bits and each `field` can be 32-bits or narrower.
12101224
static llvm::Value *
@@ -1712,6 +1726,46 @@ NVVM::IDArgPair DotAccumulate4WayOp::getIntrinsicIDAndArgs(
17121726
return {ids[type], args};
17131727
}
17141728

1729+
llvm::Intrinsic::ID PrefetchL1Op::getIntrinsicID(Operation &op) {
1730+
auto curOp = llvm::cast<NVVM::PrefetchL1Op>(op);
1731+
switch (llvm::cast<LLVM::LLVMPointerType>(curOp.getAddr().getType())
1732+
.getAddressSpace()) {
1733+
case 0:
1734+
return llvm::Intrinsic::nvvm_prefetch_L1;
1735+
case NVVM::NVVMMemorySpace::kGlobalMemorySpace:
1736+
return llvm::Intrinsic::nvvm_prefetch_global_L1;
1737+
case NVVM::NVVMMemorySpace::kLocalMemorySpace:
1738+
return llvm::Intrinsic::nvvm_prefetch_local_L1;
1739+
default:
1740+
llvm_unreachable("Invalid pointer address space");
1741+
}
1742+
}
1743+
1744+
llvm::Intrinsic::ID PrefetchL2Op::getIntrinsicID(Operation &op) {
1745+
auto curOp = llvm::cast<NVVM::PrefetchL2Op>(op);
1746+
auto evictPriority = curOp.getEvictPriority();
1747+
1748+
switch (llvm::cast<LLVM::LLVMPointerType>(curOp.getAddr().getType())
1749+
.getAddressSpace()) {
1750+
case 0:
1751+
return llvm::Intrinsic::nvvm_prefetch_L2;
1752+
case NVVM::NVVMMemorySpace::kGlobalMemorySpace:
1753+
if (evictPriority) {
1754+
if (*evictPriority == NVVM::CacheEvictionPriority::EvictLast)
1755+
return llvm::Intrinsic::nvvm_prefetch_global_L2_evict_last;
1756+
else if (*evictPriority == NVVM::CacheEvictionPriority::EvictNormal)
1757+
return llvm::Intrinsic::nvvm_prefetch_global_L2_evict_normal;
1758+
else
1759+
llvm_unreachable("Invalid cache eviction priority");
1760+
}
1761+
return llvm::Intrinsic::nvvm_prefetch_global_L2;
1762+
case NVVM::NVVMMemorySpace::kLocalMemorySpace:
1763+
return llvm::Intrinsic::nvvm_prefetch_local_L2;
1764+
default:
1765+
llvm_unreachable("Invalid pointer address space");
1766+
}
1767+
}
1768+
17151769
//===----------------------------------------------------------------------===//
17161770
// NVVMDialect initialization, type parsing, and registration.
17171771
//===----------------------------------------------------------------------===//

mlir/test/Dialect/LLVMIR/nvvm.mlir

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,29 @@ func.func @dot_accumulate_4way(%a_vec: vector<4xi8>, %b_vec: vector<4xi8>, %c: i
587587
return
588588
}
589589

590+
// CHECK-LABEL: @prefetch
591+
func.func @prefetch(%gen_ptr: !llvm.ptr, %local_ptr: !llvm.ptr<5>, %global_ptr: !llvm.ptr<1>) {
592+
// CHECK: nvvm.prefetch.L1 %{{.*}}
593+
nvvm.prefetch.L1 %gen_ptr : !llvm.ptr<0>
594+
// CHECK: nvvm.prefetch.L1 %{{.*}}
595+
nvvm.prefetch.L1 %local_ptr : !llvm.ptr<5>
596+
// CHECK: nvvm.prefetch.L1 %{{.*}}
597+
nvvm.prefetch.L1 %global_ptr : !llvm.ptr<1>
598+
// CHECK: nvvm.prefetch.L2 %{{.*}}
599+
nvvm.prefetch.L2 %gen_ptr : !llvm.ptr<0>
600+
// CHECK: nvvm.prefetch.L2 %{{.*}}
601+
nvvm.prefetch.L2 %local_ptr : !llvm.ptr<5>
602+
// CHECK: nvvm.prefetch.L2 %{{.*}}
603+
nvvm.prefetch.L2 %global_ptr : !llvm.ptr<1>
604+
// CHECK: nvvm.prefetch.L2 %{{.*}}
605+
nvvm.prefetch.L2 %global_ptr, evict_priority = evict_last : !llvm.ptr<1>
606+
// CHECK: nvvm.prefetch.L2 %{{.*}}
607+
nvvm.prefetch.L2 %global_ptr, evict_priority = evict_normal : !llvm.ptr<1>
608+
// CHECK: nvvm.prefetch.L1.uniform %{{.*}}
609+
nvvm.prefetch.L1.uniform %gen_ptr : !llvm.ptr
610+
return
611+
}
612+
590613
// -----
591614

592615
// Just check these don't emit errors.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// RUN: mlir-translate -mlir-to-llvmir %s | FileCheck %s
2+
3+
llvm.func @prefetch_L1(%gen_ptr: !llvm.ptr, %local_ptr: !llvm.ptr<5>, %global_ptr: !llvm.ptr<1>) {
4+
// CHECK-LABEL: define void @prefetch_L1(ptr %0, ptr addrspace(5) %1, ptr addrspace(1) %2) {
5+
// CHECK-NEXT: call void @llvm.nvvm.prefetch.L1(ptr %0)
6+
// CHECK-NEXT: call void @llvm.nvvm.prefetch.local.L1(ptr addrspace(5) %1)
7+
// CHECK-NEXT: call void @llvm.nvvm.prefetch.global.L1(ptr addrspace(1) %2)
8+
// CHECK-NEXT: ret void
9+
// CHECK-NEXT: }
10+
nvvm.prefetch.L1 %gen_ptr : !llvm.ptr<0>
11+
nvvm.prefetch.L1 %local_ptr : !llvm.ptr<5>
12+
nvvm.prefetch.L1 %global_ptr : !llvm.ptr<1>
13+
llvm.return
14+
}
15+
16+
llvm.func @prefetch_L2(%gen_ptr: !llvm.ptr, %local_ptr: !llvm.ptr<5>, %global_ptr: !llvm.ptr<1>) {
17+
// CHECK-LABEL: define void @prefetch_L2(ptr %0, ptr addrspace(5) %1, ptr addrspace(1) %2) {
18+
// CHECK-NEXT: call void @llvm.nvvm.prefetch.L2(ptr %0)
19+
// CHECK-NEXT: call void @llvm.nvvm.prefetch.local.L2(ptr addrspace(5) %1)
20+
// CHECK-NEXT: call void @llvm.nvvm.prefetch.global.L2(ptr addrspace(1) %2)
21+
// CHECK-NEXT: ret void
22+
// CHECK-NEXT: }
23+
nvvm.prefetch.L2 %gen_ptr : !llvm.ptr<0>
24+
nvvm.prefetch.L2 %local_ptr : !llvm.ptr<5>
25+
nvvm.prefetch.L2 %global_ptr : !llvm.ptr<1>
26+
llvm.return
27+
}
28+
29+
llvm.func @prefetch_L2_eviction_priority(%global_ptr: !llvm.ptr<1>) {
30+
// CHECK-LABEL: define void @prefetch_L2_eviction_priority(ptr addrspace(1) %0) {
31+
// CHECK-NEXT: call void @llvm.nvvm.prefetch.global.L2.evict.last(ptr addrspace(1) %0)
32+
// CHECK-NEXT: call void @llvm.nvvm.prefetch.global.L2.evict.normal(ptr addrspace(1) %0)
33+
// CHECK-NEXT: ret void
34+
// CHECK-NEXT: }
35+
nvvm.prefetch.L2 %global_ptr, evict_priority = evict_last : !llvm.ptr<1>
36+
nvvm.prefetch.L2 %global_ptr, evict_priority = evict_normal : !llvm.ptr<1>
37+
llvm.return
38+
}
39+
40+
llvm.func @prefetch_L1_uniform(%gen_ptr: !llvm.ptr) {
41+
// CHECK-LABEL: define void @prefetch_L1_uniform(ptr %0) {
42+
// CHECK-NEXT: call void @llvm.nvvm.prefetchu.L1(ptr %0)
43+
// CHECK-NEXT: ret void
44+
// CHECK-NEXT: }
45+
nvvm.prefetch.L1.uniform %gen_ptr : !llvm.ptr
46+
llvm.return
47+
}

mlir/test/Target/LLVMIR/nvvmir-invalid.mlir

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,3 +248,19 @@ llvm.func @nvvm_cvt_bf16x2_to_f8x2_invalid_rounding(%src : vector<2xbf16>) {
248248
%res = nvvm.convert.bf16x2.to.f8x2 <ue8m0> %src {rnd = #nvvm.fp_rnd_mode<rn>} : vector<2xbf16> -> i16
249249
llvm.return
250250
}
251+
252+
// -----
253+
254+
llvm.func @nvvm_prefetch_L2_with_evict_invalid_addr_space(%local_ptr: !llvm.ptr<5>) {
255+
// expected-error @below {{prefetch with cache eviction priority requires a global pointer}}
256+
nvvm.prefetch.L2 %local_ptr, evict_priority = evict_last : !llvm.ptr<5>
257+
llvm.return
258+
}
259+
260+
// -----
261+
262+
llvm.func @nvvm_prefetch_L2_with_evict_invalid_evict_priority(%global_ptr: !llvm.ptr<1>) {
263+
// expected-error @below {{invalid cache eviction priority}}
264+
nvvm.prefetch.L2 %global_ptr, evict_priority = evict_first : !llvm.ptr<1>
265+
llvm.return
266+
}

0 commit comments

Comments
 (0)