Skip to content
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

[Encoding] Introduce EncodingLayoutAttrInterface. #19216

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
21 changes: 21 additions & 0 deletions compiler/src/iree/compiler/Dialect/Encoding/IR/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ iree_td_library(
[
"EncodingAttrs.td",
"EncodingBase.td",
"EncodingInterfaces.td",
"EncodingOps.td",
],
include = ["*.td"],
Expand All @@ -45,6 +46,7 @@ iree_compiler_cc_library(
"EncodingDialect.cpp",
"EncodingDialect.cpp.inc",
"EncodingEnums.cpp.inc",
"EncodingInterfaces.cpp.inc",
"EncodingOps.cpp",
"EncodingOps.cpp.inc",
"EncodingTypes.cpp.inc",
Expand All @@ -54,13 +56,15 @@ iree_compiler_cc_library(
"EncodingDialect.h",
"EncodingDialect.h.inc",
"EncodingEnums.h.inc",
"EncodingInterfaces.h.inc",
"EncodingOps.h",
"EncodingOps.h.inc",
"EncodingTypes.h",
"EncodingTypes.h.inc",
],
deps = [
":EncodingEnumsGen",
":EncodingInterfacesGen",
":EncodingOpsIncGen",
":EncodingTypesGen",
"@llvm-project//llvm:Support",
Expand Down Expand Up @@ -139,6 +143,23 @@ iree_gentbl_cc_library(
deps = [":td_files"],
)

iree_gentbl_cc_library(
name = "EncodingInterfacesGen",
tbl_outs = [
(
["--gen-attr-interface-decls"],
"EncodingInterfaces.h.inc",
),
(
["--gen-attr-interface-defs"],
"EncodingInterfaces.cpp.inc",
),
],
tblgen = "@llvm-project//mlir:mlir-tblgen",
td_file = "EncodingInterfaces.td",
deps = [":td_files"],
)

iree_gentbl_cc_library(
name = "EncodingTypesGen",
tbl_outs = [
Expand Down
13 changes: 13 additions & 0 deletions compiler/src/iree/compiler/Dialect/Encoding/IR/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ iree_cc_library(
"EncodingDialect.h"
"EncodingDialect.h.inc"
"EncodingEnums.h.inc"
"EncodingInterfaces.h.inc"
"EncodingOps.h"
"EncodingOps.h.inc"
"EncodingTypes.h"
Expand All @@ -28,11 +29,13 @@ iree_cc_library(
"EncodingDialect.cpp"
"EncodingDialect.cpp.inc"
"EncodingEnums.cpp.inc"
"EncodingInterfaces.cpp.inc"
"EncodingOps.cpp"
"EncodingOps.cpp.inc"
"EncodingTypes.cpp.inc"
DEPS
::EncodingEnumsGen
::EncodingInterfacesGen
::EncodingOpsIncGen
::EncodingTypesGen
LLVMSupport
Expand Down Expand Up @@ -83,6 +86,16 @@ iree_tablegen_library(
--dialect=iree_encoding --gen-dialect-defs EncodingDialect.cpp.inc
)

iree_tablegen_library(
NAME
EncodingInterfacesGen
TD_FILE
"EncodingInterfaces.td"
OUTS
--gen-attr-interface-decls EncodingInterfaces.h.inc
--gen-attr-interface-defs EncodingInterfaces.cpp.inc
)

iree_tablegen_library(
NAME
EncodingTypesGen
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#define GET_ATTRDEF_CLASSES
#include "iree/compiler/Dialect/Encoding/IR/EncodingAttrs.cpp.inc"
#include "iree/compiler/Dialect/Encoding/IR/EncodingEnums.cpp.inc"
#include "iree/compiler/Dialect/Encoding/IR/EncodingInterfaces.cpp.inc"
#undef GET_ATTRDEF_CLASSES

namespace mlir::iree_compiler::IREE::Encoding {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2024 The IREE Authors
//
// Licensed under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

#ifndef IREE_DIALECT_ENCODING_INTERFACES
#define IREE_DIALECT_ENCODING_INTERFACES

include "iree/compiler/Dialect/Encoding/IR/EncodingBase.td"
include "mlir/IR/BuiltinAttributeInterfaces.td"

def IREEEncoding_EncodingLayoutAttrInterface :
AttrInterface<"EncodingLayoutAttrInterface"> {
let cppNamespace = "::mlir::iree_compiler::IREE::Encoding";
let description = [{
Interface used to query layout information needed to materialize encoding
attributes.

Any backend can implement the interface to interpret an encoding layout
based on their needs.

TBD. The current expectation of the interface is to propagate layout
information from backends to the host compliation or other targets.
}];

let methods = [
InterfaceMethod<
/*desc=*/[{
Returns the storage size (in bytes) for the tensor types with an
optional encoding.
}],
/*retTy=*/"::mlir::OpFoldResult",
/*methodName=*/"calculateStorageSizeInBytes",
/*args=*/(ins
"::mlir::OpBuilder &":$builder,
"RankedTensorType":$type,
"ValueRange":$dynamicDims
),
/*methodBody=*/"",
/*defaultImplementation=*/[{
return {};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be better to insert an assert to fail here? I assume it must be re-implemented by whoever inherits the interface.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could do it. The other way to think about it is that the caller can check if the return value is nullptr or not; raise the error with proper messages. Adding an assertion is being careful to me, while it could add confusion to users because the error messages and stack dump are all bad. Both work for me because we do not have a working case today..

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a user I would definitely prefer not to check null every time I call this function, if such API call guarantees that it will definitely return something sensible other than nullptr.

}]
>
];
}

#endif // IREE_DIALECT_ENCODING_INTERFACES
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

// clang-format off
#include "iree/compiler/Dialect/Encoding/IR/EncodingEnums.h.inc" // IWYU pragma: export
#include "iree/compiler/Dialect/Encoding/IR/EncodingInterfaces.h.inc" // IWYU pragma: export
#define GET_ATTRDEF_CLASSES
#include "iree/compiler/Dialect/Encoding/IR/EncodingAttrs.h.inc" // IWYU pragma: export
#undef GET_ATTRDEF_CLASSES
Expand Down
Loading