Skip to content

Commit 94e46fe

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 7786ac0 commit 94e46fe

File tree

6 files changed

+254
-0
lines changed

6 files changed

+254
-0
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,16 @@ constexpr int kSharedMemoryAlignmentBit = 128;
3636

3737
/// NVVM memory space identifiers.
3838
enum NVVMMemorySpace {
39+
/// Generic memory space identifier.
40+
kGenericMemorySpace = 0,
3941
/// Global memory space identifier.
4042
kGlobalMemorySpace = 1,
4143
/// Shared memory space identifier.
4244
kSharedMemorySpace = 3,
4345
/// Constant memory space identifier.
4446
kConstantMemorySpace = 4,
47+
/// Local memory space identifier.
48+
kLocalMemorySpace = 5,
4549
/// Tensor memory space identifier.
4650
/// Tensor memory is available only in arch-accelerated
4751
/// variants from sm100 onwards.

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

Lines changed: 86 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,72 @@ 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+
Operand `addr` can be a global, local or generic address pointer.
2364+
No operation is performed if `addr` maps to a `shared` memory location.
2365+
2366+
[For more information, see PTX ISA](https://docs.nvidia.com/cuda/parallel-thread-execution/#data-movement-and-conversion-instructions-prefetch-prefetchu)
2367+
}];
2368+
let arguments = (ins AnyTypeOf<[LLVM_PointerGlobal,
2369+
LLVM_PointerLocal,
2370+
LLVM_PointerGeneric]>:$addr);
2371+
let assemblyFormat = "$addr attr-dict `:` type($addr)";
2372+
2373+
let extraClassDeclaration = [{
2374+
static llvm::Intrinsic::ID getIntrinsicID(Operation &op);
2375+
}];
2376+
let llvmBuilder = [{
2377+
auto intId = NVVM::PrefetchL1Op::getIntrinsicID(*op);
2378+
createIntrinsicCall(builder, intId, $addr);
2379+
}];
2380+
}
2381+
2382+
def NVVM_PrefetchL1UniformOp : NVVM_Op<"prefetch.L1.uniform"> {
2383+
let summary = "Brings the cache line containing the specified address into L1 uniform cache";
2384+
let description = [{
2385+
Operand `addr` must be a generic address pointer and no operation is
2386+
performed if `addr` maps to a `const`, `local`, or `shared` memory location.
2387+
2388+
[For more information, see PTX ISA](https://docs.nvidia.com/cuda/parallel-thread-execution/#data-movement-and-conversion-instructions-prefetch-prefetchu)
2389+
}];
2390+
let arguments = (ins LLVM_PointerGeneric:$addr);
2391+
let assemblyFormat = "$addr attr-dict `:` type($addr)";
2392+
2393+
let llvmBuilder = [{
2394+
createIntrinsicCall(builder, llvm::Intrinsic::nvvm_prefetchu_L1, $addr);
2395+
}];
2396+
}
2397+
2398+
def NVVM_PrefetchL2Op : NVVM_Op<"prefetch.L2"> {
2399+
let summary = "Brings the cache line containing the specified address into L2 cache";
2400+
let description = [{
2401+
Operand `addr` can be a global, local or generic address pointer.
2402+
No operation is performed if `addr` maps to a `shared` memory location.
2403+
2404+
[For more information, see PTX ISA](https://docs.nvidia.com/cuda/parallel-thread-execution/#data-movement-and-conversion-instructions-prefetch-prefetchu)
2405+
}];
2406+
let arguments = (ins AnyTypeOf<[LLVM_PointerGlobal,
2407+
LLVM_PointerLocal,
2408+
LLVM_PointerGeneric]>:$addr,
2409+
OptionalAttr<CacheEvictionPriorityAttr>:$evictPriority);
2410+
let assemblyFormat = "$addr (`,` `evict_priority` `=` $evictPriority^)? attr-dict `:` type($addr)";
2411+
let hasVerifier = 1;
2412+
2413+
let extraClassDeclaration = [{
2414+
static llvm::Intrinsic::ID getIntrinsicID(Operation &op);
2415+
}];
2416+
let llvmBuilder = [{
2417+
auto intId = NVVM::PrefetchL2Op::getIntrinsicID(*op);
2418+
createIntrinsicCall(builder, intId, $addr);
2419+
}];
2420+
}
2421+
23362422
def NVVM_PrefetchTensorMapOp : NVVM_Op<"prefetch.tensormap",
23372423
[DeclareOpInterfaceMethods<BasicPtxBuilderOpInterface>]>,
23382424
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 NVVM::NVVMMemorySpace::kGenericMemorySpace:
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 NVVM::NVVMMemorySpace::kGenericMemorySpace:
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: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,3 +248,43 @@ 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_last_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_normal_invalid_addr_space(%local_ptr: !llvm.ptr<5>) {
263+
// expected-error @below {{prefetch with cache eviction priority requires a global pointer}}
264+
nvvm.prefetch.L2 %local_ptr, evict_priority = evict_normal : !llvm.ptr<5>
265+
llvm.return
266+
}
267+
268+
// -----
269+
270+
llvm.func @nvvm_prefetch_L2_with_invalid_evict_first(%global_ptr: !llvm.ptr<1>) {
271+
// expected-error @below {{invalid cache eviction priority}}
272+
nvvm.prefetch.L2 %global_ptr, evict_priority = evict_first : !llvm.ptr<1>
273+
llvm.return
274+
}
275+
276+
// -----
277+
278+
llvm.func @nvvm_prefetch_L2_with_invalid_evict_unchanged(%global_ptr: !llvm.ptr<1>) {
279+
// expected-error @below {{invalid cache eviction priority}}
280+
nvvm.prefetch.L2 %global_ptr, evict_priority = evict_unchanged : !llvm.ptr<1>
281+
llvm.return
282+
}
283+
284+
// -----
285+
286+
llvm.func @nvvm_prefetch_L2_with_invalid_no_allocate(%global_ptr: !llvm.ptr<1>) {
287+
// expected-error @below {{invalid cache eviction priority}}
288+
nvvm.prefetch.L2 %global_ptr, evict_priority = no_allocate : !llvm.ptr<1>
289+
llvm.return
290+
}

0 commit comments

Comments
 (0)