Skip to content

[mlir][spirv] Implement missing validation rules for ptr variables #67871

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

Merged
merged 2 commits into from
Sep 30, 2023
Merged
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
23 changes: 21 additions & 2 deletions mlir/include/mlir/Dialect/SPIRV/IR/SPIRVMemoryOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,9 @@ def SPIRV_PtrAccessChainOp : SPIRV_Op<"PtrAccessChain", [Pure]> {
MinVersion<SPIRV_V_1_0>,
MaxVersion<SPIRV_V_1_6>,
Extension<[]>,
Capability<[SPIRV_C_Addresses, SPIRV_C_PhysicalStorageBufferAddresses, SPIRV_C_VariablePointers, SPIRV_C_VariablePointersStorageBuffer]>
Capability<[
SPIRV_C_Addresses, SPIRV_C_PhysicalStorageBufferAddresses,
SPIRV_C_VariablePointers, SPIRV_C_VariablePointersStorageBuffer]>
];

let arguments = (ins
Expand Down Expand Up @@ -375,11 +377,16 @@ def SPIRV_VariableOp : SPIRV_Op<"Variable", []> {
must be the `Function` Storage Class.

Initializer is optional. If Initializer is present, it will be the
initial value of the variables memory content. Initializer must be an
initial value of the variable's memory content. Initializer must be an
<id> from a constant instruction or a global (module scope) OpVariable
instruction. Initializer must have the same type as the type pointed to
by Result Type.

From `SPV_KHR_physical_storage_buffer`:
If an OpVariable's pointee type is a pointer (or array of pointers) in
PhysicalStorageBuffer storage class, then the variable must be decorated
with exactly one of AliasedPointer or RestrictPointer.

<!-- End of AutoGen section -->

```
Expand All @@ -396,6 +403,9 @@ def SPIRV_VariableOp : SPIRV_Op<"Variable", []> {

%1 = spirv.Variable : !spirv.ptr<f32, Function>
%2 = spirv.Variable init(%0): !spirv.ptr<f32, Function>

%3 = spirv.Variable {aliased_pointer} :
!spirv.ptr<!spirv.ptr<f32, PhysicalStorageBuffer>, Function>
```
}];

Expand All @@ -407,6 +417,15 @@ def SPIRV_VariableOp : SPIRV_Op<"Variable", []> {
let results = (outs
SPIRV_AnyPtr:$pointer
);

let extraClassDeclaration = [{
::mlir::spirv::PointerType getPointerType() {
return ::llvm::cast<::mlir::spirv::PointerType>(getType());
}
::mlir::Type getPointeeType() {
return getPointerType().getPointeeType();
}
}];
}

#endif // MLIR_DIALECT_SPIRV_IR_MEMORY_OPS
56 changes: 45 additions & 11 deletions mlir/lib/Dialect/SPIRV/IR/MemoryOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@
//
//===----------------------------------------------------------------------===//

#include "mlir/Dialect/SPIRV/IR/SPIRVEnums.h"
#include "mlir/Dialect/SPIRV/IR/SPIRVOps.h"

#include "SPIRVOpUtils.h"
#include "SPIRVParsingUtils.h"
#include "mlir/Dialect/SPIRV/IR/SPIRVTypes.h"
#include "mlir/IR/Diagnostics.h"

#include "llvm/ADT/StringExtras.h"
#include "llvm/Support/Casting.h"

using namespace mlir::spirv::AttrNames;

Expand Down Expand Up @@ -730,19 +734,49 @@ LogicalResult VariableOp::verify() {
"constant or spirv.GlobalVariable op");
}

auto getDecorationAttr = [op = getOperation()](spirv::Decoration decoration) {
return op->getAttr(
llvm::convertToSnakeFromCamelCase(stringifyDecoration(decoration)));
};

// TODO: generate these strings using ODS.
auto *op = getOperation();
auto descriptorSetName = llvm::convertToSnakeFromCamelCase(
stringifyDecoration(spirv::Decoration::DescriptorSet));
auto bindingName = llvm::convertToSnakeFromCamelCase(
stringifyDecoration(spirv::Decoration::Binding));
auto builtInName = llvm::convertToSnakeFromCamelCase(
stringifyDecoration(spirv::Decoration::BuiltIn));

for (const auto &attr : {descriptorSetName, bindingName, builtInName}) {
if (op->getAttr(attr))
for (auto decoration :
{spirv::Decoration::DescriptorSet, spirv::Decoration::Binding,
spirv::Decoration::BuiltIn}) {
if (auto attr = getDecorationAttr(decoration))
return emitOpError("cannot have '")
<< attr << "' attribute (only allowed in spirv.GlobalVariable)";
<< llvm::convertToSnakeFromCamelCase(
stringifyDecoration(decoration))
<< "' attribute (only allowed in spirv.GlobalVariable)";
}

// From SPV_KHR_physical_storage_buffer:
// > If an OpVariable's pointee type is a pointer (or array of pointers) in
// > PhysicalStorageBuffer storage class, then the variable must be decorated
// > with exactly one of AliasedPointer or RestrictPointer.
auto pointeePtrType = dyn_cast<spirv::PointerType>(getPointeeType());
if (!pointeePtrType) {
if (auto pointeeArrayType = dyn_cast<spirv::ArrayType>(getPointeeType())) {
pointeePtrType =
dyn_cast<spirv::PointerType>(pointeeArrayType.getElementType());
}
}

if (pointeePtrType && pointeePtrType.getStorageClass() ==
spirv::StorageClass::PhysicalStorageBuffer) {
bool hasAliasedPtr =
getDecorationAttr(spirv::Decoration::AliasedPointer) != nullptr;
bool hasRestrictPtr =
getDecorationAttr(spirv::Decoration::RestrictPointer) != nullptr;

if (!hasAliasedPtr && !hasRestrictPtr)
return emitOpError() << " with physical buffer pointer must be decorated "
"either 'AliasedPointer' or 'RestrictPointer'";

if (hasAliasedPtr && hasRestrictPtr)
return emitOpError()
<< " with physical buffer pointer must have exactly one "
"aliasing decoration";
}

return success();
Expand Down
55 changes: 55 additions & 0 deletions mlir/test/Dialect/SPIRV/IR/memory-ops.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,61 @@ spirv.module Logical GLSL450 {

// -----

func.func @variable_ptr_physical_buffer() -> () {
%0 = spirv.Variable {aliased_pointer} :
!spirv.ptr<!spirv.ptr<f32, PhysicalStorageBuffer>, Function>
%1 = spirv.Variable {restrict_pointer} :
!spirv.ptr<!spirv.ptr<f32, PhysicalStorageBuffer>, Function>
return
}

// -----

func.func @variable_ptr_physical_buffer_no_decoration() -> () {
// expected-error @+1 {{must be decorated either 'AliasedPointer' or 'RestrictPointer'}}
%0 = spirv.Variable : !spirv.ptr<!spirv.ptr<f32, PhysicalStorageBuffer>, Function>
return
}

// -----

func.func @variable_ptr_physical_buffer_two_alias_decorations() -> () {
// expected-error @+1 {{must have exactly one aliasing decoration}}
%0 = spirv.Variable {aliased_pointer, restrict_pointer} :
!spirv.ptr<!spirv.ptr<f32, PhysicalStorageBuffer>, Function>
return
}

// -----

func.func @variable_ptr_array_physical_buffer() -> () {
%0 = spirv.Variable {aliased_pointer} :
!spirv.ptr<!spirv.array<4x!spirv.ptr<f32, PhysicalStorageBuffer>>, Function>
%1 = spirv.Variable {restrict_pointer} :
!spirv.ptr<!spirv.array<4x!spirv.ptr<f32, PhysicalStorageBuffer>>, Function>
return
}

// -----

func.func @variable_ptr_array_physical_buffer_no_decoration() -> () {
// expected-error @+1 {{must be decorated either 'AliasedPointer' or 'RestrictPointer'}}
%0 = spirv.Variable :
!spirv.ptr<!spirv.array<4x!spirv.ptr<f32, PhysicalStorageBuffer>>, Function>
return
}

// -----

func.func @variable_ptr_array_physical_buffer_two_alias_decorations() -> () {
// expected-error @+1 {{must have exactly one aliasing decoration}}
%0 = spirv.Variable {aliased_pointer, restrict_pointer} :
!spirv.ptr<!spirv.array<4x!spirv.ptr<f32, PhysicalStorageBuffer>>, Function>
return
}

// -----

func.func @variable_bind() -> () {
// expected-error @+1 {{cannot have 'descriptor_set' attribute (only allowed in spirv.GlobalVariable)}}
%0 = spirv.Variable bind(1, 2) : !spirv.ptr<f32, Function>
Expand Down