Skip to content

Commit

Permalink
[Encoding] Implement OpAsmDialectInterface for IREE Encoding dialect. (
Browse files Browse the repository at this point in the history
…#19146)

The revision implements the dialect interface which hoists the encodings
out of the tensor type. It makes IR dumps much easier in terms of
readability because the same encodings are collapsed into a single one
and the alias is shorter.

The revision refines the lit checks for
Encoding/IR/test/roundtrip.mlir which makes the style consistent; it
adapts some tests to hoist the encodings, which matches the IR dumps.

The `IREEEncodingInlinerInterface` is renamed to
`EncodingInlinerInterface` in this patch for consistency. The `IREE`
prefix is redundant because it is already in an anonymous namespace
under the IREE namespace.

Fixes #19024

Signed-off-by: hanhanW <[email protected]>
  • Loading branch information
hanhanW authored Nov 14, 2024
1 parent 9eaa4ef commit 34d9d5f
Show file tree
Hide file tree
Showing 9 changed files with 506 additions and 304 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,24 +43,29 @@ func.func @matmul_lowering_i8i8i32_vmvx_ukernel() attributes {
-> !flow.dispatch.tensor<readwrite:tensor<?x?xi32, #encoding_result>>{%M, %N}
return
}

// CHECK-DAG: #[[MAP_CEILDIV:.+]] = affine_map<()[s0, s1] -> (s0 ceildiv s1)>
// CHECK-DAG: #[[MAP0:.+]] = affine_map<(d0, d1, d2) -> (d0, d2)>
// CHECK-DAG: #[[MAP1:.+]] = affine_map<(d0, d1, d2) -> (d2, d1)>
// CHECK-DAG: #[[MAP2:.+]] = affine_map<(d0, d1, d2) -> (d0, d1)>
// CHECK: #[[LHS_ENCODING:.+]] = #iree_encoding.encoding<operand_index = 0 : i64, op_type = matmul, element_types = [i8, i8, i32], user_indexing_maps = [#[[MAP0]], #[[MAP1]], #[[MAP2]]], round_dims_to = array<i64: 32, 32, 32>>
// CHECK: #[[RHS_ENCODING:.+]] = #iree_encoding.encoding<operand_index = 1 : i64, op_type = matmul, element_types = [i8, i8, i32], user_indexing_maps = [#[[MAP0]], #[[MAP1]], #[[MAP2]]], round_dims_to = array<i64: 32, 32, 32>>
// CHECK: #[[OUT_ENCODING:.+]] = #iree_encoding.encoding<operand_index = 2 : i64, op_type = matmul, element_types = [i8, i8, i32], user_indexing_maps = [#[[MAP0]], #[[MAP1]], #[[MAP2]]], round_dims_to = array<i64: 32, 32, 32>>
// CHECK: func @matmul_lowering_i8i8i32_vmvx_ukernel()
// CHECK-DAG: %[[C0:.+]] = arith.constant 0 : index
// CHECK-DAG: %[[M:.+]] = hal.interface.constant.load layout({{.+}}) ordinal(0)
// CHECK-DAG: %[[N:.+]] = hal.interface.constant.load layout({{.+}}) ordinal(1)
// CHECK-DAG: %[[K:.+]] = hal.interface.constant.load layout({{.+}}) ordinal(2)
// CHECK: %[[LHS_TILE_SIZES:.+]]:2 = iree_codegen.query_tile_sizes tensor<?x?xi8, #iree_encoding.encoding<operand_index = 0 : i64, op_type = matmul, element_types = [i8, i8, i32], user_indexing_maps = [#map, #map1, #map2], round_dims_to = array<i64: 32, 32, 32>>> -> index, index
// CHECK: %[[LHS_TILE_SIZES:.+]]:2 = iree_codegen.query_tile_sizes tensor<?x?xi8, #[[LHS_ENCODING]]> -> index, index
// CHECK-DAG: %[[LHS_OUTER_SIZE0:.+]] = affine.apply #[[MAP_CEILDIV]]()[%[[M]], %[[LHS_TILE_SIZES]]#0]
// CHECK-DAG: %[[LHS_OUTER_SIZE1:.+]] = affine.apply #[[MAP_CEILDIV]]()[%[[K]], %[[LHS_TILE_SIZES]]#1]
// CHECK: %[[LHS_BINDING:.+]] = hal.interface.binding.subspan layout({{.+}}) binding(0)
// CHECK-SAME: !flow.dispatch.tensor<readonly:tensor<?x?x?x?xi8>>{%[[LHS_OUTER_SIZE0]], %[[LHS_OUTER_SIZE1]], %[[LHS_TILE_SIZES]]#0, %[[LHS_TILE_SIZES]]#1}
// CHECK: %[[RHS_TILE_SIZES:.+]]:2 = iree_codegen.query_tile_sizes tensor<?x?xi8, #iree_encoding.encoding<operand_index = 1 : i64, op_type = matmul, element_types = [i8, i8, i32], user_indexing_maps = [#map, #map1, #map2], round_dims_to = array<i64: 32, 32, 32>>> -> index, index
// CHECK: %[[RHS_TILE_SIZES:.+]]:2 = iree_codegen.query_tile_sizes tensor<?x?xi8, #[[RHS_ENCODING]]> -> index, index
// CHECK-DAG: %[[RHS_OUTER_SIZE0:.+]] = affine.apply #[[MAP_CEILDIV]]()[%[[N]], %[[RHS_TILE_SIZES]]#0]
// CHECK-DAG: %[[RHS_OUTER_SIZE1:.+]] = affine.apply #[[MAP_CEILDIV]]()[%[[K]], %[[RHS_TILE_SIZES]]#1]
// CHECK: %[[RHS_BINDING:.+]] = hal.interface.binding.subspan layout({{.+}}) binding(1)
// CHECK-SAME: !flow.dispatch.tensor<readonly:tensor<?x?x?x?xi8>>{%[[RHS_OUTER_SIZE0]], %[[RHS_OUTER_SIZE1]], %[[RHS_TILE_SIZES]]#0, %[[RHS_TILE_SIZES]]#1}
// CHECK: %[[RESULT_TILE_SIZES:.+]]:2 = iree_codegen.query_tile_sizes tensor<?x?xi32, #iree_encoding.encoding<operand_index = 2 : i64, op_type = matmul, element_types = [i8, i8, i32], user_indexing_maps = [#map, #map1, #map2], round_dims_to = array<i64: 32, 32, 32>>> -> index, index
// CHECK: %[[RESULT_TILE_SIZES:.+]]:2 = iree_codegen.query_tile_sizes tensor<?x?xi32, #[[OUT_ENCODING]]> -> index, index
// CHECK-DAG: %[[RESULT_OUTER_SIZE0:.+]] = affine.apply #[[MAP_CEILDIV]]()[%[[M]], %[[RESULT_TILE_SIZES]]#0]
// CHECK-DAG: %[[RESULT_OUTER_SIZE1:.+]] = affine.apply #[[MAP_CEILDIV]]()[%[[N]], %[[RESULT_TILE_SIZES]]#1]
// CHECK: %[[OUTS_BINDING:.+]] = hal.interface.binding.subspan layout({{.+}}) binding(2)
Expand Down
23 changes: 21 additions & 2 deletions compiler/src/iree/compiler/Dialect/Encoding/IR/EncodingDialect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,27 @@
#undef GET_ATTRDEF_CLASSES

namespace mlir::iree_compiler::IREE::Encoding {
namespace {

// Used for custom printing support.
struct EncodingOpAsmInterface : public OpAsmDialectInterface {
using OpAsmDialectInterface::OpAsmDialectInterface;
/// Hooks for getting an alias identifier alias for a given symbol, that is
/// not necessarily a part of this dialect. The identifier is used in place
/// of the symbol when printing textual IR. These aliases must not contain
/// `.` or end with a numeric digit([0-9]+). Returns success if an alias was
/// provided, failure otherwise.
AliasResult getAlias(Attribute attr, raw_ostream &os) const override {
if (llvm::isa<EncodingAttr>(attr)) {
os << "encoding";
return AliasResult::OverridableAlias;
}
return AliasResult::NoAlias;
}
};

// Used to control inlining behavior.
struct IREEEncodingInlinerInterface : public DialectInlinerInterface {
struct EncodingInlinerInterface : public DialectInlinerInterface {
using DialectInlinerInterface::DialectInlinerInterface;

bool isLegalToInline(Operation *call, Operation *callable,
Expand All @@ -45,9 +63,10 @@ struct IREEEncodingInlinerInterface : public DialectInlinerInterface {
return true;
}
};
} // namespace

void IREEEncodingDialect::initialize() {
addInterfaces<IREEEncodingInlinerInterface>();
addInterfaces<EncodingOpAsmInterface, EncodingInlinerInterface>();

addAttributes<
#define GET_ATTRDEF_LIST
Expand Down
Loading

0 comments on commit 34d9d5f

Please sign in to comment.