Skip to content

Commit fbf67bf

Browse files
stefankoncarevickrzysz00
authored andcommitted
[mlir][GPU] Handle LLVM pointer attributes on memref arguments.
Handle pointer attributes (noalias, nonnull, readonly, writeonly, dereferencable, dereferencable_or_null). "noalias" attribute is ignore for non-bare pointer. Reviewed By: krzysz00 Differential Revision: https://reviews.llvm.org/D157082
1 parent 9272aa9 commit fbf67bf

File tree

4 files changed

+145
-0
lines changed

4 files changed

+145
-0
lines changed

mlir/lib/Conversion/GPUCommon/GPUOpsLowering.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ GPUFuncOpLowering::matchAndRewrite(gpu::GPUFuncOp gpuFuncOp, OpAdaptor adaptor,
6767
// Create the new function operation. Only copy those attributes that are
6868
// not specific to function modeling.
6969
SmallVector<NamedAttribute, 4> attributes;
70+
ArrayAttr argAttrs;
7071
for (const auto &attr : gpuFuncOp->getAttrs()) {
7172
if (attr.getName() == SymbolTable::getSymbolAttrName() ||
7273
attr.getName() == gpuFuncOp.getFunctionTypeAttrName() ||
@@ -75,6 +76,10 @@ GPUFuncOpLowering::matchAndRewrite(gpu::GPUFuncOp gpuFuncOp, OpAdaptor adaptor,
7576
attr.getName() == gpuFuncOp.getWorkgroupAttribAttrsAttrName() ||
7677
attr.getName() == gpuFuncOp.getPrivateAttribAttrsAttrName())
7778
continue;
79+
if (attr.getName() == gpuFuncOp.getArgAttrsAttrName()) {
80+
argAttrs = gpuFuncOp.getArgAttrsAttr();
81+
continue;
82+
}
7883
attributes.push_back(attr);
7984
}
8085
// Add a dialect specific kernel attribute in addition to GPU kernel
@@ -190,6 +195,49 @@ GPUFuncOpLowering::matchAndRewrite(gpu::GPUFuncOp gpuFuncOp, OpAdaptor adaptor,
190195
}
191196
}
192197

198+
// Get memref type from function arguments and set the noalias to
199+
// pointer arguments.
200+
for (const auto &en : llvm::enumerate(gpuFuncOp.getArgumentTypes())) {
201+
auto memrefTy = en.value().dyn_cast<MemRefType>();
202+
NamedAttrList argAttr = argAttrs
203+
? argAttrs[en.index()].cast<DictionaryAttr>()
204+
: NamedAttrList();
205+
206+
auto copyPointerAttribute = [&](StringRef attrName) {
207+
Attribute attr = argAttr.erase(attrName);
208+
209+
// This is a proxy for the bare pointer calling convention.
210+
if (!attr)
211+
return;
212+
auto remapping = signatureConversion.getInputMapping(en.index());
213+
if (remapping->size > 1 &&
214+
attrName == LLVM::LLVMDialect::getNoAliasAttrName()) {
215+
emitWarning(llvmFuncOp.getLoc(),
216+
"Cannot copy noalias with non-bare pointers.\n");
217+
return;
218+
}
219+
for (size_t i = 0, e = remapping->size; i < e; ++i) {
220+
if (llvmFuncOp.getArgument(remapping->inputNo + i)
221+
.getType()
222+
.isa<LLVM::LLVMPointerType>()) {
223+
llvmFuncOp.setArgAttr(remapping->inputNo + i, attrName, attr);
224+
}
225+
}
226+
};
227+
228+
if (argAttr.empty())
229+
continue;
230+
231+
if (memrefTy) {
232+
copyPointerAttribute(LLVM::LLVMDialect::getNoAliasAttrName());
233+
copyPointerAttribute(LLVM::LLVMDialect::getReadonlyAttrName());
234+
copyPointerAttribute(LLVM::LLVMDialect::getWriteOnlyAttrName());
235+
copyPointerAttribute(LLVM::LLVMDialect::getNonNullAttrName());
236+
copyPointerAttribute(LLVM::LLVMDialect::getDereferenceableAttrName());
237+
copyPointerAttribute(
238+
LLVM::LLVMDialect::getDereferenceableOrNullAttrName());
239+
}
240+
}
193241
rewriter.eraseOp(gpuFuncOp);
194242
return success();
195243
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// RUN: mlir-opt %s -split-input-file -convert-gpu-to-rocdl='use-opaque-pointers=1 use-bare-ptr-memref-call-conv=0' | FileCheck %s --check-prefixes=CHECK,ROCDL
2+
// RUN: mlir-opt %s -split-input-file -convert-gpu-to-nvvm='use-opaque-pointers=1 use-bare-ptr-memref-call-conv=0' | FileCheck %s --check-prefixes=CHECK,NVVM
3+
4+
gpu.module @kernel {
5+
gpu.func @test_func_readonly(%arg0 : memref<f32> {llvm.readonly} ) {
6+
gpu.return
7+
}
8+
}
9+
10+
// CHECK-LABEL: llvm.func @test_func_readonly
11+
// ROCDL-SAME: !llvm.ptr {llvm.readonly}
12+
// NVVM-SAME: !llvm.ptr {llvm.readonly}
13+
14+
15+
// -----
16+
17+
gpu.module @kernel {
18+
gpu.func @test_func_writeonly(%arg0 : memref<f32> {llvm.writeonly} ) {
19+
gpu.return
20+
}
21+
}
22+
23+
// CHECK-LABEL: llvm.func @test_func_writeonly
24+
// ROCDL-SAME: !llvm.ptr {llvm.writeonly}
25+
// NVVM-SAME: !llvm.ptr {llvm.writeonly}
26+
27+
28+
// -----
29+
30+
gpu.module @kernel {
31+
gpu.func @test_func_nonnull(%arg0 : memref<f32> {llvm.nonnull} ) {
32+
gpu.return
33+
}
34+
}
35+
36+
// CHECK-LABEL: llvm.func @test_func_nonnull
37+
// ROCDL-SAME: !llvm.ptr {llvm.nonnull}
38+
// NVVM-SAME: !llvm.ptr {llvm.nonnull}
39+
40+
41+
// -----
42+
43+
gpu.module @kernel {
44+
gpu.func @test_func_dereferenceable(%arg0 : memref<f32> {llvm.dereferenceable = 4 : i64} ) {
45+
gpu.return
46+
}
47+
}
48+
49+
// CHECK-LABEL: llvm.func @test_func_dereferenceable
50+
// ROCDL-SAME: !llvm.ptr {llvm.dereferenceable = 4 : i64}
51+
// NVVM-SAME: !llvm.ptr {llvm.dereferenceable = 4 : i64}
52+
53+
54+
// -----
55+
56+
gpu.module @kernel {
57+
gpu.func @test_func_dereferenceable_or_null(%arg0 : memref<f32> {llvm.dereferenceable_or_null = 4 : i64} ) {
58+
gpu.return
59+
}
60+
}
61+
62+
// CHECK-LABEL: llvm.func @test_func_dereferenceable_or_null
63+
// ROCDL-SAME: !llvm.ptr {llvm.dereferenceable_or_null = 4 : i64}
64+
// NVVM-SAME: !llvm.ptr {llvm.dereferenceable_or_null = 4 : i64}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// RUN: mlir-opt %s -split-input-file -convert-gpu-to-rocdl='use-opaque-pointers=1 use-bare-ptr-memref-call-conv=1' | FileCheck %s --check-prefixes=CHECK,ROCDL
2+
// RUN: mlir-opt %s -split-input-file -convert-gpu-to-nvvm='use-opaque-pointers=1 use-bare-ptr-memref-call-conv=1' | FileCheck %s --check-prefixes=CHECK,NVVM
3+
4+
gpu.module @kernel {
5+
gpu.func @func_with_noalias_attr(%arg0 : memref<f32> {llvm.noalias} ) {
6+
gpu.return
7+
}
8+
}
9+
10+
// CHECK-LABEL: llvm.func @func_with_noalias_attr
11+
// ROCDL-SAME: !llvm.ptr {llvm.noalias}
12+
// NVVM-SAME: !llvm.ptr {llvm.noalias}
13+
14+
15+
// -----
16+
17+
gpu.module @kernel {
18+
gpu.func @func_without_any_attr(%arg0 : memref<f32> ) {
19+
gpu.return
20+
}
21+
}
22+
23+
// CHECK-LABEL: llvm.func @func_without_any_attr
24+
// ROCDL-SAME: !llvm.ptr
25+
// NVVM-SAME: !llvm.ptr
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// RUN: mlir-opt %s -split-input-file -convert-gpu-to-rocdl='use-opaque-pointers=1 use-bare-ptr-memref-call-conv=0' -verify-diagnostics
2+
3+
gpu.module @kernel {
4+
// expected-warning @+1 {{Cannot copy noalias with non-bare pointers.}}
5+
gpu.func @func_warning_for_not_bare_pointer(%arg0 : memref<f32> {llvm.noalias} ) {
6+
gpu.return
7+
}
8+
}

0 commit comments

Comments
 (0)