[Mlir-commits] [mlir] [mlir][bufferization]-Replace only one use in TensorEmptyElimination (PR #118958)
Amir Bishara
llvmlistbot at llvm.org
Tue Dec 17 07:02:58 PST 2024
================
@@ -28,47 +28,72 @@ namespace bufferization {
using namespace mlir;
using namespace mlir::bufferization;
+/// Return true if `val` is in scope at the given
+/// `insertionPoint`.
+static bool valueDominateInsertionPoint(const DominanceInfo &domInfo,
+ Operation *insertionPoint, Value val) {
+ if (auto bbArg = dyn_cast<BlockArgument>(val)) {
+ Block *owner = bbArg.getOwner();
+ if (!owner->findAncestorOpInBlock(*insertionPoint))
+ return false;
+ } else {
+ auto opResult = cast<OpResult>(val);
+ if (!domInfo.properlyDominates(opResult.getOwner(), insertionPoint))
+ return false;
+ }
+ return true;
+}
+
/// Return true if all `neededValues` are in scope at the given
/// `insertionPoint`.
static bool
neededValuesDominateInsertionPoint(const DominanceInfo &domInfo,
Operation *insertionPoint,
const SmallVector<Value> &neededValues) {
- for (Value val : neededValues) {
- if (auto bbArg = dyn_cast<BlockArgument>(val)) {
- Block *owner = bbArg.getOwner();
- if (!owner->findAncestorOpInBlock(*insertionPoint))
- return false;
- } else {
- auto opResult = cast<OpResult>(val);
- if (!domInfo.properlyDominates(opResult.getOwner(), insertionPoint))
- return false;
- }
- }
- return true;
-}
+ for (Value val : neededValues)
+ if (!valueDominateInsertionPoint(domInfo, insertionPoint, val))
+ return false;
-/// Return true if the given `insertionPoint` dominates all uses of
-/// `emptyTensorOp`.
-static bool insertionPointDominatesUses(const DominanceInfo &domInfo,
- Operation *insertionPoint,
- Operation *emptyTensorOp) {
- return llvm::all_of(emptyTensorOp->getUsers(), [&](Operation *user) {
- return domInfo.dominates(insertionPoint, user);
- });
+ return true;
}
-/// Find a valid insertion point for a replacement of `emptyTensorOp`, assuming
-/// that the replacement may use any value from `neededValues`.
+/// Find a valid insertion point for a replacement of `useToBeEliminated`,
+/// assuming that the replacement may use any value from `neededValues`.
static Operation *
-findValidInsertionPoint(Operation *emptyTensorOp,
+findValidInsertionPoint(OpOperand *useToBeEliminated,
const SmallVector<Value> &neededValues) {
DominanceInfo domInfo;
- // Gather all possible insertion points: the location of `emptyTensorOp` and
- // right after the definition of each value in `neededValues`.
+ Operation *candidateInsertionPoint = useToBeEliminated->getOwner();
+ assert(isa<OpResult>(useToBeEliminated->get()) && "expected a result value");
+ // Both `tensor.empty` and its user are within different blocks.
+ if (useToBeEliminated->getOwner()->getBlock() !=
+ useToBeEliminated->get().getDefiningOp()->getBlock())
+ candidateInsertionPoint = useToBeEliminated->get().getDefiningOp();
+
+ // Trying to move the needed values before the `emptyTensorOp`.
+ for (Value val : neededValues) {
+ if (valueDominateInsertionPoint(domInfo, candidateInsertionPoint, val))
+ continue;
+ Operation *definingOp = val.getDefiningOp();
+ if (!definingOp)
+ continue;
+
+ bool isItSafeToMoveOp =
+ llvm::all_of(definingOp->getOperands(), [&](Value operand) {
----------------
amirBish wrote:
Nice, For now will only include the changes of the replacing only one use. And after submitting this one will adopt the approach you have suggested to have a lambda of `buildSubsetExtraction` that user can control. I guess it will abstract the different implementation of finding the valid insertion point and let user have custom support.
https://github.com/llvm/llvm-project/pull/118958
More information about the Mlir-commits
mailing list