Skip to content

[mlir]: fix a issue and refine some code (#67977) #68129

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 1 commit into from
Oct 5, 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
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ LogicalResult mlir::bufferization::eliminateEmptyTensors(
op.buildSubsetExtraction(rewriter, emptyTensorOp->getLoc());
if (!replacement)
continue;
if (emptyTensorOp == replacement.getDefiningOp())
continue;
if (replacement.getType() != v.getType()) {
rewriter.setInsertionPointAfterValue(replacement);
replacement = rewriter.create<tensor::CastOp>(v.getLoc(), v.getType(),
Expand Down
15 changes: 8 additions & 7 deletions mlir/lib/Dialect/Linalg/IR/LinalgOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -545,16 +545,17 @@ class RegionBuilderHelper {

namespace {

struct EraseSelfCopyOnBuffers : OpRewritePattern<CopyOp> {
struct EraseSelfCopy : OpRewritePattern<CopyOp> {
using OpRewritePattern<CopyOp>::OpRewritePattern;
LogicalResult matchAndRewrite(CopyOp copyOp,
PatternRewriter &rewriter) const override {
if (!copyOp.hasBufferSemantics())
return rewriter.notifyMatchFailure(copyOp,
"does not have buffer semantics");
if (copyOp.getInputs().front() != copyOp.getOutputs().front())
if (copyOp.getInputs() != copyOp.getOutputs())
return rewriter.notifyMatchFailure(copyOp, "not a self copy");
rewriter.eraseOp(copyOp);
if (copyOp.hasBufferSemantics())
rewriter.eraseOp(copyOp);
else
rewriter.replaceOp(copyOp, copyOp.getInputs());

return success();
}
};
Expand All @@ -563,7 +564,7 @@ struct EraseSelfCopyOnBuffers : OpRewritePattern<CopyOp> {

void CopyOp::getCanonicalizationPatterns(RewritePatternSet &results,
MLIRContext *context) {
results.add<EraseSelfCopyOnBuffers>(context);
results.add<EraseSelfCopy>(context);
}

//===----------------------------------------------------------------------===//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,3 +317,14 @@ func.func @linalg_copy(%t: tensor<5xf32>, %f: f32) -> tensor<5xf32> {
%1 = linalg.copy ins(%filled : tensor<5xf32>) outs(%t : tensor<5xf32>) -> tensor<5xf32>
return %1 : tensor<5xf32>
}

// -----

// CHECK-LABEL: func @linalg_copy_empty(
// CHECK: %[[ret:.*]] = memref.alloc()
// CHECK-NEXT: return %[[ret]]
func.func @linalg_copy_empty() -> tensor<26xi32> {
%0 = tensor.empty() : tensor<26xi32>
%1 = linalg.copy ins(%0 : tensor<26xi32>) outs(%0 : tensor<26xi32>) -> tensor<26xi32>
return %1 : tensor<26xi32>
}
14 changes: 7 additions & 7 deletions mlir/tools/mlir-linalg-ods-gen/mlir-linalg-ods-yaml-gen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1029,13 +1029,13 @@ void {0}::regionBuilder(ImplicitLocOpBuilder &b,
// {1}: attribute name
// {2}: default type function name
static const char attrDef[] = R"FMT(
{0} {1}Val = {0}::{2};
auto {1}Iter = llvm::find_if(attrs, [&](const NamedAttribute &attr) {{
return attr.getName() == "{1}"; });
if ({1}Iter != attrs.end()) {{
if (auto attr = llvm::dyn_cast<{0}Attr>({1}Iter->getValue()))
{1}Val = attr.getValue();
}
{0} {1}Val = {0}::{2};
auto {1}Iter = llvm::find_if(attrs, [&](const NamedAttribute &attr) {{
return attr.getName() == "{1}"; });
if ({1}Iter != attrs.end()) {{
if (auto attr = llvm::dyn_cast<{0}Attr>({1}Iter->getValue()))
{1}Val = attr.getValue();
}
)FMT";
std::string enumName = convertOperandKindToEnumName(arg.kind);
attrs.push_back(
Expand Down