Skip to content

[mlir] Add res() method to linalg::ContractionOpInterface #76539

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
Jan 4, 2024
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
8 changes: 8 additions & 0 deletions mlir/include/mlir/Dialect/Linalg/IR/LinalgInterfaces.td
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ def LinalgContractionOpInterface : OpInterface<"ContractionOpInterface"> {
return $_op.getOperation()->getOperand(1);
}]>,
InterfaceMethod<
/*desc=*/"Returns the result value.",
/*retTy=*/"OpResult",
/*methodName=*/"res",
/*args=*/(ins),
/*methodBody=*/[{
return $_op.getOperation()->getResult(0);
}]>,
InterfaceMethod<
/*desc=*/[{
Returns whether the given op has indexing maps that correspond to a
row-major matmul operation.
Expand Down
1 change: 1 addition & 0 deletions mlir/unittests/Dialect/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ target_link_libraries(MLIRDialectTests

add_subdirectory(ArmSME)
add_subdirectory(Index)
add_subdirectory(Linalg)
add_subdirectory(LLVMIR)
add_subdirectory(MemRef)
add_subdirectory(SCF)
Expand Down
8 changes: 8 additions & 0 deletions mlir/unittests/Dialect/Linalg/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
add_mlir_unittest(MLIRLinalgTests
LinalgInterfacesTest.cpp
)
target_link_libraries(MLIRLinalgTests
PRIVATE
MLIRLinalgDialect
)

43 changes: 43 additions & 0 deletions mlir/unittests/Dialect/Linalg/LinalgInterfacesTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//===- LinalgInterfacesTest.cpp - LinalgInterfaces unit tests ----------===//
//
// Part of the LLVM Project, 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 "mlir/Dialect/Linalg/IR/Linalg.h"
#include "mlir/Dialect/Tensor/IR/Tensor.h"

#include "gtest/gtest.h"

using namespace mlir;

class LinalgInterfacesTest : public ::testing::Test {
protected:
LinalgInterfacesTest() {
context.getOrLoadDialect<mlir::linalg::LinalgDialect>();
}

mlir::MLIRContext context;
};

TEST_F(LinalgInterfacesTest, ContractionOpOperandResultAccessor) {
OpBuilder b(&context);
SmallVector<int64_t> lhsShape = {1, 2};
SmallVector<int64_t> rhsShape = {2, 4};
SmallVector<int64_t> resShape = {1, 4};
auto lhs = b.create<tensor::EmptyOp>(UnknownLoc::get(&context), lhsShape,
b.getF32Type());
auto rhs = b.create<tensor::EmptyOp>(UnknownLoc::get(&context), rhsShape,
b.getF32Type());
auto out = b.create<tensor::EmptyOp>(UnknownLoc::get(&context), resShape,
b.getF32Type());
Operation *op = b.create<linalg::MatmulOp>(
UnknownLoc::get(&context), ValueRange{lhs, rhs}, ValueRange{out});
auto contractOp = llvm::cast<linalg::ContractionOpInterface>(op);

EXPECT_EQ(contractOp.lhs(), op->getOperand(0));
EXPECT_EQ(contractOp.rhs(), op->getOperand(1));
EXPECT_EQ(contractOp.res(), op->getResult(0));
}