Skip to content

[mlir][linalg]: Fixed possible memory leak in cloneToCollapsedOp #87595

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
Apr 7, 2024

Conversation

AviadCo
Copy link
Contributor

@AviadCo AviadCo commented Apr 4, 2024

  • Direct call to clone function leads to memory leak. Instead, we would can use RewriterBase clone function.

@llvmbot
Copy link
Member

llvmbot commented Apr 4, 2024

@llvm/pr-subscribers-mlir

Author: Aviad Cohen (AviadCo)

Changes
  • Direct call to clone function leads to memory leak. Instead, we would can use RewriterBase clone function.

Full diff: https://github.com/llvm/llvm-project/pull/87595.diff

1 Files Affected:

  • (modified) mlir/lib/Dialect/Linalg/Transforms/ElementwiseOpFusion.cpp (+6-3)
diff --git a/mlir/lib/Dialect/Linalg/Transforms/ElementwiseOpFusion.cpp b/mlir/lib/Dialect/Linalg/Transforms/ElementwiseOpFusion.cpp
index 9453502a253f16..34a5adec07bbea 100644
--- a/mlir/lib/Dialect/Linalg/Transforms/ElementwiseOpFusion.cpp
+++ b/mlir/lib/Dialect/Linalg/Transforms/ElementwiseOpFusion.cpp
@@ -1497,9 +1497,12 @@ LinalgOp cloneToCollapsedOp<LinalgOp>(RewriterBase &rewriter, LinalgOp origOp,
   SmallVector<Type> resultTypes;
   collapseOperandsAndResults(origOp, collapsingInfo, rewriter, inputOperands,
                              outputOperands, resultTypes);
-  return cast<LinalgOp>(clone(
-      rewriter, origOp, resultTypes,
-      llvm::to_vector(llvm::concat<Value>(inputOperands, outputOperands))));
+  IRMapping mapper;
+  for (auto [orig, input] : llvm::zip(origOp.getDpsInputs(), inputOperands))
+    mapper.map(orig, input);
+  for (auto [orig, output] : llvm::zip(origOp.getDpsInits(), outputOperands))
+    mapper.map(orig, output);
+  return cast<LinalgOp>(rewriter.clone(*origOp.getOperation(), mapper));
 }
 
 /// Collapse a `GenericOp`

@llvmbot
Copy link
Member

llvmbot commented Apr 4, 2024

@llvm/pr-subscribers-mlir-linalg

Author: Aviad Cohen (AviadCo)

Changes
  • Direct call to clone function leads to memory leak. Instead, we would can use RewriterBase clone function.

Full diff: https://github.com/llvm/llvm-project/pull/87595.diff

1 Files Affected:

  • (modified) mlir/lib/Dialect/Linalg/Transforms/ElementwiseOpFusion.cpp (+6-3)
diff --git a/mlir/lib/Dialect/Linalg/Transforms/ElementwiseOpFusion.cpp b/mlir/lib/Dialect/Linalg/Transforms/ElementwiseOpFusion.cpp
index 9453502a253f16..34a5adec07bbea 100644
--- a/mlir/lib/Dialect/Linalg/Transforms/ElementwiseOpFusion.cpp
+++ b/mlir/lib/Dialect/Linalg/Transforms/ElementwiseOpFusion.cpp
@@ -1497,9 +1497,12 @@ LinalgOp cloneToCollapsedOp<LinalgOp>(RewriterBase &rewriter, LinalgOp origOp,
   SmallVector<Type> resultTypes;
   collapseOperandsAndResults(origOp, collapsingInfo, rewriter, inputOperands,
                              outputOperands, resultTypes);
-  return cast<LinalgOp>(clone(
-      rewriter, origOp, resultTypes,
-      llvm::to_vector(llvm::concat<Value>(inputOperands, outputOperands))));
+  IRMapping mapper;
+  for (auto [orig, input] : llvm::zip(origOp.getDpsInputs(), inputOperands))
+    mapper.map(orig, input);
+  for (auto [orig, output] : llvm::zip(origOp.getDpsInits(), outputOperands))
+    mapper.map(orig, output);
+  return cast<LinalgOp>(rewriter.clone(*origOp.getOperation(), mapper));
 }
 
 /// Collapse a `GenericOp`

@AviadCo
Copy link
Contributor Author

AviadCo commented Apr 4, 2024

@srcarroll I found this memory leak using Asan checks. Can you please take a look on the fix?

Copy link
Contributor

@MaheshRavishankar MaheshRavishankar left a comment

Choose a reason for hiding this comment

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

Thanks!

Copy link
Contributor

@srcarroll srcarroll left a comment

Choose a reason for hiding this comment

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

Oh interesting. Good catch. Thanks for the fix!

@AviadCo AviadCo force-pushed the linalg/fix-memory-leak branch from f09c16e to 32d51f1 Compare April 4, 2024 07:28
@AviadCo
Copy link
Contributor Author

AviadCo commented Apr 4, 2024

@MaheshRavishankar @srcarroll
I missed the right place of the memory leak, it actaully was due to direct call Region::cloneInto instead of using BuilderOp::cloneRegionBefore that also handle the listener, pushed new patch set for review.

@AviadCo AviadCo force-pushed the linalg/fix-memory-leak branch from 32d51f1 to 06fe902 Compare April 4, 2024 11:31
@srcarroll
Copy link
Contributor

since I dont understand why the original code causes memory leaks and why this fixes it, you should have @MaheshRavishankar or @nicolasvasilache give the final approval.

* Direct call to `clone` function leads to memory leak. Instead, we
would can use `RewriterBase` clone function.
@AviadCo AviadCo force-pushed the linalg/fix-memory-leak branch from 06fe902 to d03112b Compare April 6, 2024 05:07
@AviadCo AviadCo merged commit ccc0256 into llvm:main Apr 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants