forked from iree-org/iree
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement (in)equality operator and serialization for TileSwizzle. (i…
…ree-org#19257) The revision implements the conversion between the TileSwizzle struct and DictionaryAttr. The utilities are tested in unittests. To verify if the deserialization result has the same content, it also implements the (in)equality operators (i.e., `==` and `!=`) for the struct. --------- Signed-off-by: hanhanW <[email protected]>
- Loading branch information
Showing
6 changed files
with
304 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
compiler/src/iree/compiler/Codegen/Dialect/Codegen/Utils/unittests/BUILD.bazel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# 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 | ||
|
||
load("//build_tools/bazel:build_defs.oss.bzl", "iree_compiler_cc_test") | ||
|
||
package( | ||
features = ["layering_check"], | ||
licenses = ["notice"], # Apache 2.0 | ||
) | ||
|
||
iree_compiler_cc_test( | ||
name = "UtilsTest", | ||
testonly = True, | ||
srcs = ["UtilsTest.cpp"], | ||
deps = [ | ||
"//compiler/src/iree/compiler/Codegen/Dialect/Codegen/Utils", | ||
"//compiler/src/iree/testing:gtest_main", | ||
"@com_google_googletest//:gtest", | ||
"@llvm-project//mlir:DialectUtils", | ||
"@llvm-project//mlir:IR", | ||
], | ||
) |
26 changes: 26 additions & 0 deletions
26
compiler/src/iree/compiler/Codegen/Dialect/Codegen/Utils/unittests/CMakeLists.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
################################################################################ | ||
# Autogenerated by build_tools/bazel_to_cmake/bazel_to_cmake.py from # | ||
# compiler/src/iree/compiler/Codegen/Dialect/Codegen/Utils/unittests/BUILD.bazel# | ||
# # | ||
# Use iree_cmake_extra_content from iree/build_defs.oss.bzl to add arbitrary # | ||
# CMake-only content. # | ||
# # | ||
# To disable autogeneration for this file entirely, delete this header. # | ||
################################################################################ | ||
|
||
iree_add_all_subdirs() | ||
|
||
iree_cc_test( | ||
NAME | ||
UtilsTest | ||
SRCS | ||
"UtilsTest.cpp" | ||
DEPS | ||
MLIRIR | ||
gmock | ||
gtest | ||
iree::compiler::Codegen::Dialect::Codegen::Utils | ||
iree::testing::gtest_main | ||
) | ||
|
||
### BAZEL_TO_CMAKE_PRESERVES_ALL_CONTENT_BELOW_THIS_LINE ### |
103 changes: 103 additions & 0 deletions
103
compiler/src/iree/compiler/Codegen/Dialect/Codegen/Utils/unittests/UtilsTest.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
// 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 | ||
|
||
#include <gmock/gmock.h> | ||
#include <gtest/gtest.h> | ||
|
||
#include "iree/compiler/Codegen/Dialect/Codegen/Utils/Utils.h" | ||
#include "mlir/Dialect/Utils/StaticValueUtils.h" | ||
#include "mlir/IR/Attributes.h" | ||
|
||
namespace mlir::iree_compiler::IREE::Codegen { | ||
namespace { | ||
|
||
using testing::Optional; | ||
using Kind = TileSwizzle::Dim::Kind; | ||
|
||
TEST(TileSwizzle, RelationalOperator) { | ||
TileSwizzle swizzle1; | ||
swizzle1.permutation = {1, 2, 0}; | ||
TileSwizzle swizzle2; | ||
EXPECT_NE(swizzle1, swizzle2); | ||
swizzle2.permutation = swizzle1.permutation; | ||
EXPECT_EQ(swizzle1, swizzle2); | ||
swizzle1.expandShape.push_back({TileSwizzle::Dim(Kind::CrossThread, 16)}); | ||
swizzle2.expandShape.push_back({TileSwizzle::Dim(Kind::Internal, 16)}); | ||
EXPECT_NE(swizzle1, swizzle2); | ||
swizzle2.expandShape[0][0].kind = Kind::CrossThread; | ||
EXPECT_EQ(swizzle1, swizzle2); | ||
} | ||
|
||
TEST(TileSwizzle, DimKindToString) { | ||
EXPECT_EQ(convertSwizzleKindToString(Kind::Internal), "Internal"); | ||
EXPECT_EQ(convertSwizzleKindToString(Kind::CrossIntrinsic), "CrossIntrinsic"); | ||
EXPECT_EQ(convertSwizzleKindToString(Kind::CrossThread), "CrossThread"); | ||
} | ||
|
||
TEST(TileSwizzle, StringToDimKind) { | ||
std::optional<TileSwizzle::Dim::Kind> maybeKind; | ||
maybeKind = convertStringToSwizzleKind("Internal"); | ||
EXPECT_THAT(maybeKind, Optional(TileSwizzle::Dim::Kind::Internal)); | ||
maybeKind = convertStringToSwizzleKind("CrossIntrinsic"); | ||
EXPECT_THAT(maybeKind, Optional(TileSwizzle::Dim::Kind::CrossIntrinsic)); | ||
maybeKind = convertStringToSwizzleKind("CrossThread"); | ||
EXPECT_THAT(maybeKind, Optional(TileSwizzle::Dim::Kind::CrossThread)); | ||
maybeKind = convertStringToSwizzleKind("deadbeef"); | ||
EXPECT_FALSE(maybeKind.has_value()); | ||
} | ||
|
||
TEST(TileSwizzle, Serialization) { | ||
TileSwizzle swizzle; | ||
swizzle.expandShape.push_back({TileSwizzle::Dim(Kind::CrossThread, 16)}); | ||
swizzle.expandShape.push_back({TileSwizzle::Dim(Kind::CrossIntrinsic, 4), | ||
TileSwizzle::Dim(Kind::Internal, 4)}); | ||
swizzle.permutation = {1, 2, 0}; | ||
|
||
MLIRContext ctx; | ||
DictionaryAttr dictAttr = serializeTileSwizzle(&ctx, swizzle); | ||
|
||
EXPECT_TRUE(dictAttr.contains("expandShape")); | ||
EXPECT_TRUE(dictAttr.contains("permutation")); | ||
|
||
// Verify if the sizes match. The check of values is done by the comparison | ||
// between deserialzation result and the original struct. | ||
auto expandShapeArrayAttr = | ||
dyn_cast<ArrayAttr>(dictAttr.getNamed("expandShape")->getValue()); | ||
EXPECT_EQ(expandShapeArrayAttr.size(), swizzle.expandShape.size()); | ||
for (auto [expectedShape, actualShape] : llvm::zip_equal( | ||
swizzle.expandShape, expandShapeArrayAttr.getAsRange<ArrayAttr>())) { | ||
EXPECT_EQ(expectedShape.size(), actualShape.size()); | ||
} | ||
|
||
SmallVector<int64_t> extractedPerm = extractFromIntegerArrayAttr<int64_t>( | ||
dictAttr.getNamed("permutation")->getValue()); | ||
EXPECT_EQ(extractedPerm, swizzle.permutation); | ||
|
||
std::optional<TileSwizzle> deserializedSwizzle = | ||
deserializeTileSwizzle(dictAttr); | ||
EXPECT_THAT(deserializedSwizzle, Optional(swizzle)); | ||
} | ||
|
||
TEST(TileSwizzle, Deserialization) { | ||
MLIRContext ctx; | ||
Builder b(&ctx); | ||
|
||
auto emptyDictAttr = b.getDictionaryAttr({}); | ||
EXPECT_FALSE(deserializeTileSwizzle(emptyDictAttr).has_value()); | ||
|
||
SmallVector<NamedAttribute> items; | ||
items.emplace_back(b.getStringAttr("expandShape"), b.getArrayAttr({})); | ||
EXPECT_FALSE(deserializeTileSwizzle(b.getDictionaryAttr(items)).has_value()); | ||
|
||
items.emplace_back(b.getStringAttr("permutation"), b.getArrayAttr({})); | ||
EXPECT_TRUE(deserializeTileSwizzle(b.getDictionaryAttr(items)).has_value()); | ||
|
||
items.back().setValue(b.getUnitAttr()); | ||
EXPECT_FALSE(deserializeTileSwizzle(b.getDictionaryAttr(items)).has_value()); | ||
} | ||
|
||
} // namespace | ||
} // namespace mlir::iree_compiler::IREE::Codegen |