[DispatchCreation] Fuse packed contractions with epilogues and unpack (#24967)

Packed contractions lose their fused epilogues when they arrive at
dispatch formation as mmt4d or a data-tiled convolution. The result
unpack can also become a separate dispatch.

Add opt-in fusion (`fuse-mmt4d`, `fuse-data-tiled-convolution`) of
layout-preserving elementwise consumers and the result unpack. Notes for
review:

- Epilogues must read the contraction result and write their own result
through identity maps. Epilogues that update the result through their
init operand are not matched.
- Only the unpack layouts that data-tiling materialization produces are
matched. For mmt4d that is the row-major form and the narrow-N
transposed form. For convolutions it is NHWC, NCHW and batchless HWC;
batchless CHW is not matched.
- The result unpack is the last op of the group, so a consumer of a
claimed unpack gets its own dispatch instead of fusing with the unpack.
- An unpack that its root does not claim becomes a root at its own
position in program order, as without the options, so it fuses with its
own consumers as before.
- A group holds pack-like ops (`linalg.pack`, `set_encoding`) or the
result unpack, but not both, whichever joins first. A unit-batch
collapse only joins together with the unpack that consumes it, so a
group never ends in the collapse.
- The matchers hardcode the CPU layouts; a TODO describes moving the
layout decision into its own component that dispatch formation queries.

No pipeline enables the options until #24969.

This is one of five PRs adding an experimental CPU path that
materializes data-tiling layouts before dispatch creation, so that
`linalg.mmt4d` and data-tiled convolutions fuse with their elementwise
epilogues and result unpacks.


Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
diff --git a/compiler/src/iree/compiler/DispatchCreation/BUILD.bazel b/compiler/src/iree/compiler/DispatchCreation/BUILD.bazel
index 9b13191..f8f5212 100644
--- a/compiler/src/iree/compiler/DispatchCreation/BUILD.bazel
+++ b/compiler/src/iree/compiler/DispatchCreation/BUILD.bazel
@@ -62,6 +62,7 @@
         ":PassesIncGen",
         "//compiler/src/iree/compiler/Codegen/Common",
         "//compiler/src/iree/compiler/Codegen/Dialect/Codegen/IR:IREECodegenDialect",
+        "//compiler/src/iree/compiler/Codegen/Dialect/Codegen/Utils",
         "//compiler/src/iree/compiler/Dialect/Encoding/IR",
         "//compiler/src/iree/compiler/Dialect/Encoding/Utils",
         "//compiler/src/iree/compiler/Dialect/Flow/Conversion/TensorToFlow",
diff --git a/compiler/src/iree/compiler/DispatchCreation/CMakeLists.txt b/compiler/src/iree/compiler/DispatchCreation/CMakeLists.txt
index 4b12433..cc6a56d 100644
--- a/compiler/src/iree/compiler/DispatchCreation/CMakeLists.txt
+++ b/compiler/src/iree/compiler/DispatchCreation/CMakeLists.txt
@@ -81,6 +81,7 @@
     MLIRTransforms
     iree::compiler::Codegen::Common
     iree::compiler::Codegen::Dialect::Codegen::IR::IREECodegenDialect
+    iree::compiler::Codegen::Dialect::Codegen::Utils
     iree::compiler::Dialect::Encoding::IR
     iree::compiler::Dialect::Encoding::Utils
     iree::compiler::Dialect::Flow::Conversion::TensorToFlow
diff --git a/compiler/src/iree/compiler/DispatchCreation/FormDispatchRegions.cpp b/compiler/src/iree/compiler/DispatchCreation/FormDispatchRegions.cpp
index 20d3a06..fdefa55 100644
--- a/compiler/src/iree/compiler/DispatchCreation/FormDispatchRegions.cpp
+++ b/compiler/src/iree/compiler/DispatchCreation/FormDispatchRegions.cpp
@@ -5,6 +5,7 @@
 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 
 #include "iree/compiler/Dialect/Flow/Transforms/FormDispatchRegions.h"
+#include "iree/compiler/Codegen/Dialect/Codegen/Utils/Utils.h"
 #include "iree/compiler/Dialect/Encoding/IR/EncodingOps.h"
 #include "iree/compiler/Dialect/Flow/IR/FlowDialect.h"
 #include "iree/compiler/Dialect/Flow/IR/FlowOps.h"
@@ -18,6 +19,7 @@
 #include "iree/compiler/DispatchCreation/FusionUtils.h"
 #include "iree/compiler/DispatchCreation/Passes.h"
 #include "llvm/ADT/STLExtras.h"
+#include "llvm/ADT/SmallPtrSet.h"
 #include "llvm/ADT/TypeSwitch.h"
 #include "llvm/Support/Casting.h"
 #include "llvm/Support/Debug.h"
@@ -31,6 +33,7 @@
 #include "mlir/Dialect/MemRef/Transforms/Transforms.h"
 #include "mlir/Dialect/SCF/IR/SCF.h"
 #include "mlir/Dialect/Tensor/IR/Tensor.h"
+#include "mlir/Dialect/Utils/IndexingUtils.h"
 #include "mlir/Dialect/Utils/StructuredOpsUtils.h"
 #include "mlir/IR/Block.h"
 #include "mlir/IR/Builders.h"
@@ -141,10 +144,12 @@
   /// group. This means that some transitive dependency of `consumerOp` (not in
   /// the fusion group) itself uses an operation in the fusion group. This is
   /// required for fusion because it must be legal to take a program slice that
-  /// contains only the ops in the fusion group.
+  /// contains only the ops in the fusion group. A non-null `pendingOp` is
+  /// treated as a member of the group, for a consumer that joins with it.
   bool
   hasTransitiveDependencyOnFusionGroup(Operation *consumerOp,
-                                       DominanceInfo const &dominance) const {
+                                       DominanceInfo const &dominance,
+                                       Operation *pendingOp = nullptr) const {
     BackwardSliceOptions options;
     options.inclusive = true;
     options.omitUsesFromAbove = false;
@@ -160,7 +165,9 @@
     auto populateSlice = [&](OpOperand *operand) {
       // It's okay if the consumer directly uses an operation in the fusion
       // group.
-      if (loopMaps.contains(operand->get().getDefiningOp())) {
+      Operation *definingOp = operand->get().getDefiningOp();
+      if (loopMaps.contains(definingOp) ||
+          (pendingOp && definingOp == pendingOp)) {
         return;
       }
       LogicalResult result = getBackwardSlice(operand->get(), &slice, options);
@@ -477,10 +484,177 @@
   return true;
 }
 
+/// Returns true if `operand` feeds a data-tiled contraction result into an
+/// elementwise epilogue that keeps the physical result layout. The operand and
+/// the single init must use identity maps, so transposes and reductions are
+/// rejected; other inputs may use any projected permutation, such as a
+/// broadcast bias. Epilogues that update the result in place through their
+/// init operand are not matched.
+static bool isDataTiledEpilogueOperand(OpOperand &operand) {
+  auto generic = dyn_cast<linalg::GenericOp>(operand.getOwner());
+  if (!generic || generic.getNumDpsInits() != 1 ||
+      !linalg::isElementwise(generic) || generic.isDpsInit(&operand)) {
+    return false;
+  }
+  return generic.getMatchingIndexingMap(&operand).isIdentity() &&
+         generic.getMatchingIndexingMap(generic.getDpsInitOperand(0))
+             .isIdentity();
+}
+
+// TODO(#25031): The matchers for data-tiled roots and their result unpacks
+// hardcode the ops and layouts that CPU data-tiling materialization produces:
+// mmt4d, data-tiled convolution generics, the narrow-N transposition and the
+// convolution layouts of getEncodingInfoForConv. The layout decision should be
+// its own component that materialization and dispatch formation both query,
+// for example by marking the ops it materializes, instead of dispatch formation
+// re-deriving the layouts. That would also drop the dependency on
+// Codegen/Dialect/Codegen/Utils.
+static bool isDataTiledRoot(Operation *op) {
+  return isa<linalg::Mmt4DOp, linalg::BatchMmt4DOp>(op) ||
+         IREE::Codegen::isDataTiledConvGeneric(op);
+}
+
+/// Returns true if `op` is a data-tiled root and `options` enable fusion for
+/// its kind of contraction.
+static bool
+isDataTiledFusionEnabled(Operation *op,
+                         FormDispatchRegionsPassOptions const &options) {
+  if (isa<linalg::Mmt4DOp, linalg::BatchMmt4DOp>(op)) {
+    return options.fuseMmt4d;
+  }
+  return options.fuseDataTiledConvolution && isDataTiledRoot(op);
+}
+
+/// Returns true if `collapse` folds a unit batch dimension of a data-tiled
+/// convolution result into its outer channel dimension, i.e.
+/// [1, OC/k0, OH, OW, k0] into [OC/k0, OH, OW, k0]. Batchless convolutions
+/// reach dispatch formation in this form. Reshapes of channel tiles or spatial
+/// dimensions are not matched.
+static bool isUnitBatchCollapse(tensor::CollapseShapeOp collapse) {
+  return collapse.getSrcType().getRank() == 5 &&
+         collapse.getSrcType().getDimSize(0) == 1 &&
+         collapse.getReassociationIndices() ==
+             SmallVector<ReassociationIndices>{{0, 1}, {2}, {3}, {4}};
+}
+
+/// Returns the data-tiled root that produces `value` through
+/// layout-preserving epilogues, or null. This only defers the root selection of
+/// a potential result unpack; the normal use, dependency and fusion-group
+/// checks still decide whether the root claims it.
+static Operation *
+findDataTiledRoot(Value value, FormDispatchRegionsPassOptions const &options) {
+  SmallVector<Value> worklist{value};
+  llvm::SmallPtrSet<Operation *, 8> visited;
+  Block *block = value.getParentBlock();
+  while (!worklist.empty()) {
+    Operation *producer = worklist.pop_back_val().getDefiningOp();
+    if (!producer || producer->getBlock() != block ||
+        !visited.insert(producer).second) {
+      continue;
+    }
+    if (isDataTiledFusionEnabled(producer, options)) {
+      return producer;
+    }
+    if (auto collapse = dyn_cast<tensor::CollapseShapeOp>(producer);
+        collapse && options.fuseDataTiledConvolution &&
+        isUnitBatchCollapse(collapse)) {
+      worklist.push_back(collapse.getSrc());
+      continue;
+    }
+    for (OpOperand &operand : producer->getOpOperands()) {
+      if (isDataTiledEpilogueOperand(operand)) {
+        worklist.push_back(operand.get());
+      }
+    }
+  }
+  return nullptr;
+}
+
+/// Returns true if `unpack` restores an mmt4d or batch_mmt4d result
+/// [(B,) M1, N1, M0, N0] with static tiles into [(B,) M, N]. Besides this
+/// row-major form, accept the transposed form that data-tiling materialization
+/// uses for narrow N, where the last two entries of both inner_dims_pos and
+/// outer_dims_perm are swapped. Other permutations, and dynamic or scalable
+/// tiles, keep their separate dispatch.
+static bool isMmt4dResultUnpack(linalg::UnPackOp unpack) {
+  int64_t destRank = unpack.getDestType().getRank();
+  if (!unpack.getInnerTiles().empty() || unpack.getInnerDimsPos().size() != 2 ||
+      (destRank != 2 && destRank != 3)) {
+    return false;
+  }
+  SmallVector<int64_t> innerDimsPos(unpack.getInnerDimsPos());
+  SmallVector<int64_t> outerDimsPerm(unpack.getOuterDimsPerm());
+  if (outerDimsPerm.empty()) {
+    outerDimsPerm = llvm::to_vector(llvm::seq<int64_t>(0, destRank));
+  }
+  if (innerDimsPos[0] > innerDimsPos[1]) {
+    std::swap(innerDimsPos[0], innerDimsPos[1]);
+    std::swap(outerDimsPerm[destRank - 2], outerDimsPerm[destRank - 1]);
+  }
+  return innerDimsPos[0] == destRank - 2 && innerDimsPos[1] == destRank - 1 &&
+         isIdentityPermutation(outerDimsPerm);
+}
+
+// Result unpacks of a data-tiled convolution. The convolution produces
+// [N, OC/k0, OH, OW, k0]; these layouts only unpack the k0 tile, mirroring the
+// outer_dims_perm and inner_dims_pos that data-tiling materialization computes
+// for convolution results (see getEncodingInfoForConv in
+// Codegen/Dialect/Codegen/Utils/Utils.cpp).
+// NHWC: [N, OC/k0, OH, OW, k0] into [N, OH, OW, OC].
+constexpr int64_t kNhwcChannelPos = 3;
+constexpr int64_t kNhwcOuterDimsPerm[] = {0, 3, 1, 2};
+// NCHW: [N, OC/k0, OH, OW, k0] into [N, OC, OH, OW].
+constexpr int64_t kNchwChannelPos = 1;
+// Batchless HWC, after isUnitBatchCollapse: [OC/k0, OH, OW, k0] into
+// [OH, OW, OC]. Batchless CHW is not matched.
+constexpr int64_t kHwcChannelPos = 2;
+constexpr int64_t kHwcOuterDimsPerm[] = {2, 0, 1};
+
+/// Returns true if `unpack` restores a data-tiled convolution result into one
+/// of the layouts above. Arbitrary permutations keep their separate dispatch.
+static bool isDataTiledConvResultUnpack(linalg::UnPackOp unpack) {
+  if (!unpack.getInnerTiles().empty() || unpack.getInnerDimsPos().size() != 1) {
+    return false;
+  }
+  ArrayRef<int64_t> outerDimsPerm = unpack.getOuterDimsPerm();
+  int64_t channelPos = unpack.getInnerDimsPos()[0];
+  switch (unpack.getSourceType().getRank()) {
+  case 5:
+    return (channelPos == kNhwcChannelPos &&
+            outerDimsPerm == ArrayRef(kNhwcOuterDimsPerm)) ||
+           (channelPos == kNchwChannelPos &&
+            (outerDimsPerm.empty() || isIdentityPermutation(outerDimsPerm)));
+  case 4: {
+    auto collapse = unpack.getSource().getDefiningOp<tensor::CollapseShapeOp>();
+    return collapse && isUnitBatchCollapse(collapse) &&
+           channelPos == kHwcChannelPos &&
+           outerDimsPerm == ArrayRef(kHwcOuterDimsPerm);
+  }
+  default:
+    return false;
+  }
+}
+
+static bool isDataTiledResultUnpack(linalg::UnPackOp unpack, Operation *root) {
+  if (isa<linalg::Mmt4DOp, linalg::BatchMmt4DOp>(root)) {
+    return isMmt4dResultUnpack(unpack);
+  }
+  return isDataTiledConvResultUnpack(unpack);
+}
+
+/// Returns true if `unpack` can join `group`, whose root is a data-tiled
+/// contraction.
+static bool canFuseDataTiledResultUnpack(linalg::UnPackOp unpack,
+                                         const FusionGroup &group) {
+  return isDataTiledResultUnpack(unpack, group.getRoot()) &&
+         !group.wouldExceedOperandLimit(unpack);
+}
+
 /// Returns true if this is a fusable use, while fusing a root with its
 /// consumer.
 static bool
 isFusableWithConsumer(OpOperand &fusedOperand, const FusionTracker &tracker,
+                      DominanceInfo const &dominanceInfo,
                       FormDispatchRegionsPassOptions const &options) {
   Operation *producer = fusedOperand.get().getDefiningOp();
   Operation *consumer = fusedOperand.getOwner();
@@ -493,6 +667,50 @@
     return false;
   }
 
+  const FusionGroup &group = tracker.getFusionGroup(producer);
+  Operation *groupRoot = group.getRoot();
+  if (isDataTiledFusionEnabled(groupRoot, options)) {
+    // Only a pack that relayouts the result unpack for the next packed
+    // contraction follows it, as unset_encoding -> set_encoding fuses in
+    // dispatch-time data tiling, and the pack ends the group. Dynamic inner
+    // tiles are excluded as in producer fusion (#12746).
+    if (isa<linalg::PackOp>(producer)) {
+      return false;
+    }
+    if (isUnpackLikeOp(producer)) {
+      auto pack = dyn_cast<linalg::PackOp>(consumer);
+      return pack && isa<linalg::UnPackOp>(producer) &&
+             pack.getInnerTiles().empty() &&
+             !group.wouldExceedOperandLimit(pack);
+    }
+    if (auto unpack = dyn_cast<linalg::UnPackOp>(consumer)) {
+      return canFuseDataTiledResultUnpack(unpack, group);
+    }
+    // A unit-batch collapse only joins together with the unpack that consumes
+    // it, so that a group never ends in the collapse. Check the unpack as
+    // fuseRootsWithConsumers will once the collapse joined. The unpack adds no
+    // operands, so it passes the operand limit whenever the collapse does.
+    if (auto collapse = dyn_cast<tensor::CollapseShapeOp>(consumer);
+        collapse && isa<linalg::GenericOp>(groupRoot) &&
+        isUnitBatchCollapse(collapse) && collapse->hasOneUse()) {
+      auto unpack = dyn_cast<linalg::UnPackOp>(*collapse->getUsers().begin());
+      return unpack && unpack->getBlock() == collapse->getBlock() &&
+             !tracker.isFusedOp(unpack) &&
+             !group.hasTransitiveDependencyOnFusionGroup(unpack, dominanceInfo,
+                                                         collapse) &&
+             canFuseDataTiledResultUnpack(unpack, group) &&
+             !group.wouldExceedOperandLimit(consumer);
+    }
+    // Default fusion never adds consumers to mmt4d ops, so their groups only
+    // gain layout-preserving epilogues. Data-tiled convolutions are generic
+    // ops that the rules below already fuse with their consumers; keep those
+    // decisions so that the option only adds the result unpack and its pack.
+    if (isa<linalg::Mmt4DOp, linalg::BatchMmt4DOp>(groupRoot) &&
+        !isDataTiledEpilogueOperand(fusedOperand)) {
+      return false;
+    }
+  }
+
   // Fuse unset_encoding operations with `tensor.extract_slice` and elementwise
   // generic ops.
   if (isUnpackLikeOp(producer)) {
@@ -547,9 +765,13 @@
     return insertSliceOp.getDest().getDefiningOp<linalg::FillOp>();
   }
 
-  // TODO(#16025): Enable mmt4d fusion. It is disabled because the backends
-  // can not set multi lowering_config properly. See the issue for more details.
-  if (isa<linalg::Mmt4DOp, linalg::BatchMmt4DOp>(producer)) {
+  // mmt4d consumer fusion is opt-in through `fuseMmt4d`. The CPU codegen
+  // limitation that originally disabled it (#16025) is fixed, but the other
+  // flows that reach dispatch formation with mmt4d ops have not been validated
+  // with fused dispatches.
+  // TODO: Enable mmt4d consumer fusion by default once they are.
+  if (isa<linalg::Mmt4DOp, linalg::BatchMmt4DOp>(producer) &&
+      !options.fuseMmt4d) {
     return false;
   }
 
@@ -682,7 +904,8 @@
         }
 
         if (!llvm::all_of(operands, [&](OpOperand *use) {
-              return isFusableWithConsumer(*use, tracker, options);
+              return isFusableWithConsumer(*use, tracker, dominanceInfo,
+                                           options);
             })) {
           continue;
         }
@@ -854,6 +1077,10 @@
     // - First find the roots
     // - To fuse with consumers make the consumer the root.
     SmallVector<Operation *> roots;
+    // Result unpacks left for their data-tiled root to claim as a consumer.
+    // They keep their position in `roots`, but only get a fusion group if the
+    // root does not claim them.
+    llvm::SmallPtrSet<Operation *, 4> deferredUnpacks;
     for (Operation &op : llvm::reverse(block)) {
       if (isa<scf::SCFDialect>(op.getDialect())) {
         for (auto &region : op.getRegions()) {
@@ -864,6 +1091,17 @@
       }
 
       // Start with a root operation and fuse its producers.
+      // Let a packed contraction claim its final unpack as a consumer.
+      if (options.fuseMmt4d || options.fuseDataTiledConvolution) {
+        if (auto unpack = dyn_cast<linalg::UnPackOp>(op)) {
+          Operation *root = findDataTiledRoot(unpack.getSource(), options);
+          if (root && isDataTiledResultUnpack(unpack, root)) {
+            deferredUnpacks.insert(&op);
+            roots.push_back(&op);
+            continue;
+          }
+        }
+      }
       if (tracker.isFusedOp(&op) || !isRootLikeOp(&op)) {
         continue;
       }
@@ -872,8 +1110,24 @@
                              /*fuseWithTruncate=*/false);
       roots.push_back(&op);
     }
-    roots = llvm::to_vector(llvm::reverse(roots));
-    fuseRootsWithConsumers(context, roots, dominanceInfo, options, tracker);
+    // Fuse consumers in program order. A deferred unpack that its root did not
+    // claim becomes a root at its own position, as it would be without
+    // deferral. Roots before it cannot claim it instead: only data-tiled
+    // groups take unpack consumers.
+    SmallVector<Operation *> orderedRoots;
+    for (Operation *root : llvm::reverse(roots)) {
+      if (deferredUnpacks.contains(root)) {
+        if (tracker.isFusedOp(root)) {
+          continue;
+        }
+        FusionGroup &newGroup = tracker.createFusionGroup(context, root);
+        fuseRootsWithProducers(context, root, newGroup, options, tracker,
+                               /*fuseWithTruncate=*/false);
+      }
+      fuseRootsWithConsumers(context, root, dominanceInfo, options, tracker);
+      orderedRoots.push_back(root);
+    }
+    roots = std::move(orderedRoots);
     for (Operation *root : roots) {
       FusionGroup &fusionGroup = tracker.getFusionGroup(root);
       fuseRootsWithProducers(context, root, fusionGroup, options, tracker,
@@ -1071,9 +1325,12 @@
   mlir::FunctionOpInterface funcOp = getOperation();
   DominanceInfo &dominanceInfo = getAnalysis<DominanceInfo>();
   TensorDimTrackingRewriter rewriter(funcOp);
-  FormDispatchRegionsPassOptions options{
-      aggressiveFusion, fuseMultiUseProducers, fusePadWithConsumers,
-      fusePadWithProducers};
+  FormDispatchRegionsPassOptions options{aggressiveFusion,
+                                         fuseMultiUseProducers,
+                                         fusePadWithConsumers,
+                                         fusePadWithProducers,
+                                         fuseMmt4d,
+                                         fuseDataTiledConvolution};
   if (failed(createFusionGroups(rewriter, funcOp, dominanceInfo, options))) {
     funcOp->emitOpError("failed to create fusion groups");
     return signalPassFailure();
diff --git a/compiler/src/iree/compiler/DispatchCreation/Passes.td b/compiler/src/iree/compiler/DispatchCreation/Passes.td
index 8c23809..123d9f3 100644
--- a/compiler/src/iree/compiler/DispatchCreation/Passes.td
+++ b/compiler/src/iree/compiler/DispatchCreation/Passes.td
@@ -322,7 +322,11 @@
     Option<"fusePadWithConsumers", "fuse-pad-with-consumers", "bool",
            /*default=*/"false", "Enable fusing pad with consumer">,
     Option<"fusePadWithProducers", "fuse-pad-with-producers", "bool",
-           /*default=*/"false", "Enable fusion of pad with producers">
+           /*default=*/"false", "Enable fusion of pad with producers">,
+    Option<"fuseMmt4d", "fuse-mmt4d", "bool",
+           /*default=*/"false", "Fuse layout-preserving elementwise consumers and the result unpack into mmt4d and batch_mmt4d dispatches">,
+    Option<"fuseDataTiledConvolution", "fuse-data-tiled-convolution", "bool",
+           /*default=*/"false", "Fuse the result unpack, after layout-preserving elementwise consumers, into data-tiled convolution dispatches">
   ];
   let description = [{
     Pass to form dispatch.region ops from Linalg on tensor ops. A dispatch region
diff --git a/compiler/src/iree/compiler/DispatchCreation/test/BUILD.bazel b/compiler/src/iree/compiler/DispatchCreation/test/BUILD.bazel
index e42d518..fcec46d 100644
--- a/compiler/src/iree/compiler/DispatchCreation/test/BUILD.bazel
+++ b/compiler/src/iree/compiler/DispatchCreation/test/BUILD.bazel
@@ -28,6 +28,7 @@
             "collapse_linalg_generic_on_tensors.mlir",
             "convert_encoding_to_flow.mlir",
             "convert_region_to_workgroups.mlir",
+            "data_tiled_convolution_fusion.mlir",
             "dispatch_block_matmul_producers.mlir",
             "dispatch_linalg_ext_fusion.mlir",
             "dispatch_linalg_on_tensors.mlir",
@@ -51,6 +52,7 @@
             "hoist_uniform_scalar_compute.mlir",
             "insert_tensor_barriers.mlir",
             "materialize_default_workgroup_count_region.mlir",
+            "mmt4d_fusion.mlir",
             "pad_fusion_with_consumer.mlir",
             "pad_fusion_with_producer.mlir",
             "pipeline_tests.mlir",
diff --git a/compiler/src/iree/compiler/DispatchCreation/test/CMakeLists.txt b/compiler/src/iree/compiler/DispatchCreation/test/CMakeLists.txt
index 08a75bc..34bd6d5 100644
--- a/compiler/src/iree/compiler/DispatchCreation/test/CMakeLists.txt
+++ b/compiler/src/iree/compiler/DispatchCreation/test/CMakeLists.txt
@@ -25,6 +25,7 @@
     "collapse_linalg_generic_on_tensors.mlir"
     "convert_encoding_to_flow.mlir"
     "convert_region_to_workgroups.mlir"
+    "data_tiled_convolution_fusion.mlir"
     "dispatch_block_matmul_producers.mlir"
     "dispatch_linalg_ext_fusion.mlir"
     "dispatch_linalg_on_tensors.mlir"
@@ -48,6 +49,7 @@
     "hoist_uniform_scalar_compute.mlir"
     "insert_tensor_barriers.mlir"
     "materialize_default_workgroup_count_region.mlir"
+    "mmt4d_fusion.mlir"
     "pad_fusion_with_consumer.mlir"
     "pad_fusion_with_producer.mlir"
     "pipeline_tests.mlir"
diff --git a/compiler/src/iree/compiler/DispatchCreation/test/data_tiled_convolution_fusion.mlir b/compiler/src/iree/compiler/DispatchCreation/test/data_tiled_convolution_fusion.mlir
new file mode 100644
index 0000000..fe7bf7a
--- /dev/null
+++ b/compiler/src/iree/compiler/DispatchCreation/test/data_tiled_convolution_fusion.mlir
@@ -0,0 +1,430 @@
+// RUN: iree-opt --split-input-file --pass-pipeline='builtin.module(util.func(iree-dispatch-creation-form-dispatch-regions))' %s | FileCheck %s --check-prefixes=CHECK,DEFAULT
+// RUN: iree-opt --split-input-file --pass-pipeline='builtin.module(util.func(iree-dispatch-creation-form-dispatch-regions{fuse-data-tiled-convolution=true}))' %s | FileCheck %s --check-prefixes=CHECK,FUSED
+// RUN: iree-opt --split-input-file --pass-pipeline='builtin.module(util.func(iree-dispatch-creation-form-dispatch-regions{aggressive-fusion=true fuse-data-tiled-convolution=true}))' %s | FileCheck %s --check-prefix=AGGRESSIVE
+
+// The data-tiled convolution produces [N, OC/k0, OH, OW, k0]. Unpacking the k0
+// tile into NHWC joins the convolution dispatch only when the option is set.
+#input = affine_map<(n, oc, h, w, ic, kh, kw, o, i) -> (n, ic, h + kh, w + kw, i)>
+#filter = affine_map<(n, oc, h, w, ic, kh, kw, o, i) -> (oc, ic, kh, kw, i, o)>
+#output = affine_map<(n, oc, h, w, ic, kh, kw, o, i) -> (n, oc, h, w, o)>
+// DEFAULT-LABEL: util.func public @nhwc_unpack(
+// DEFAULT:         %[[CONV_DISPATCH:.+]] = flow.dispatch.region
+// DEFAULT:           %[[CONV:.+]] = linalg.generic
+// DEFAULT:           flow.return %[[CONV]]
+// DEFAULT:         flow.dispatch.region
+// DEFAULT:           linalg.unpack %[[CONV_DISPATCH]]
+// FUSED-LABEL: util.func public @nhwc_unpack(
+// FUSED:         %[[RESULT:.+]] = flow.dispatch.region -> (tensor<2x14x14x16xf32>)
+// FUSED:           %[[CONV:.+]] = linalg.generic
+// FUSED:           %[[UNPACK:.+]] = linalg.unpack %[[CONV]] outer_dims_perm = [0, 3, 1, 2] inner_dims_pos = [3]
+// FUSED:           flow.return %[[UNPACK]]
+// FUSED-NOT:     flow.dispatch.region
+// FUSED:         util.return %[[RESULT]]
+util.func public @nhwc_unpack(%input: tensor<2x1x16x16x8xf32>, %filter: tensor<2x1x3x3x8x8xf32>) -> tensor<2x14x14x16xf32> {
+  %zero = arith.constant 0.0 : f32
+  %empty = tensor.empty() : tensor<2x2x14x14x8xf32>
+  %init = linalg.fill ins(%zero : f32) outs(%empty : tensor<2x2x14x14x8xf32>) -> tensor<2x2x14x14x8xf32>
+  %conv = linalg.generic {indexing_maps = [#input, #filter, #output],
+    iterator_types = ["parallel", "parallel", "parallel", "parallel", "reduction", "reduction", "reduction", "parallel", "reduction"]}
+    ins(%input, %filter : tensor<2x1x16x16x8xf32>, tensor<2x1x3x3x8x8xf32>) outs(%init : tensor<2x2x14x14x8xf32>) {
+  ^bb0(%lhs: f32, %rhs: f32, %acc: f32):
+    %mul = arith.mulf %lhs, %rhs : f32
+    %sum = arith.addf %mul, %acc : f32
+    linalg.yield %sum : f32
+  } -> tensor<2x2x14x14x8xf32>
+  %dest = tensor.empty() : tensor<2x14x14x16xf32>
+  %unpack = linalg.unpack %conv outer_dims_perm = [0, 3, 1, 2] inner_dims_pos = [3] inner_tiles = [8] into %dest : tensor<2x2x14x14x8xf32> -> tensor<2x14x14x16xf32>
+  util.return %unpack : tensor<2x14x14x16xf32>
+}
+
+// -----
+
+// Unpacking into NCHW keeps the outer dimension order.
+#input = affine_map<(n, oc, h, w, ic, kh, kw, o, i) -> (n, ic, h + kh, w + kw, i)>
+#filter = affine_map<(n, oc, h, w, ic, kh, kw, o, i) -> (oc, ic, kh, kw, i, o)>
+#output = affine_map<(n, oc, h, w, ic, kh, kw, o, i) -> (n, oc, h, w, o)>
+// FUSED-LABEL: util.func public @nchw_unpack(
+// FUSED:         %[[RESULT:.+]] = flow.dispatch.region -> (tensor<2x16x14x14xf32>)
+// FUSED:           %[[CONV:.+]] = linalg.generic
+// FUSED:           %[[UNPACK:.+]] = linalg.unpack %[[CONV]] inner_dims_pos = [1]
+// FUSED:           flow.return %[[UNPACK]]
+// FUSED-NOT:     flow.dispatch.region
+// FUSED:         util.return %[[RESULT]]
+util.func public @nchw_unpack(%input: tensor<2x1x16x16x8xf32>, %filter: tensor<2x1x3x3x8x8xf32>) -> tensor<2x16x14x14xf32> {
+  %zero = arith.constant 0.0 : f32
+  %empty = tensor.empty() : tensor<2x2x14x14x8xf32>
+  %init = linalg.fill ins(%zero : f32) outs(%empty : tensor<2x2x14x14x8xf32>) -> tensor<2x2x14x14x8xf32>
+  %conv = linalg.generic {indexing_maps = [#input, #filter, #output],
+    iterator_types = ["parallel", "parallel", "parallel", "parallel", "reduction", "reduction", "reduction", "parallel", "reduction"]}
+    ins(%input, %filter : tensor<2x1x16x16x8xf32>, tensor<2x1x3x3x8x8xf32>) outs(%init : tensor<2x2x14x14x8xf32>) {
+  ^bb0(%lhs: f32, %rhs: f32, %acc: f32):
+    %mul = arith.mulf %lhs, %rhs : f32
+    %sum = arith.addf %mul, %acc : f32
+    linalg.yield %sum : f32
+  } -> tensor<2x2x14x14x8xf32>
+  %dest = tensor.empty() : tensor<2x16x14x14xf32>
+  %unpack = linalg.unpack %conv inner_dims_pos = [1] inner_tiles = [8] into %dest : tensor<2x2x14x14x8xf32> -> tensor<2x16x14x14xf32>
+  util.return %unpack : tensor<2x16x14x14xf32>
+}
+
+// -----
+
+// A batchless convolution arrives with its unit batch dimension collapsed into
+// the outer channel dimension. The collapse and the HWC unpack join together
+// with an elementwise epilogue.
+#input = affine_map<(n, oc, h, w, ic, kh, kw, o, i) -> (n, ic, h + kh, w + kw, i)>
+#filter = affine_map<(n, oc, h, w, ic, kh, kw, o, i) -> (oc, ic, kh, kw, i, o)>
+#output = affine_map<(n, oc, h, w, ic, kh, kw, o, i) -> (n, oc, h, w, o)>
+#identity = affine_map<(d0, d1, d2, d3, d4) -> (d0, d1, d2, d3, d4)>
+// FUSED-LABEL: util.func public @unit_batch_relu_hwc_unpack(
+// FUSED:         %[[RESULT:.+]] = flow.dispatch.region -> (tensor<14x14x16xf32>)
+// FUSED:           %[[CONV:.+]] = linalg.generic
+// FUSED:           %[[RELU:.+]] = linalg.generic
+// FUSED-SAME:        ins(%[[CONV]] : tensor<1x2x14x14x8xf32>)
+// FUSED:           %[[COLLAPSED:.+]] = tensor.collapse_shape %[[RELU]] {{\[}}[0, 1], [2], [3], [4]]
+// FUSED:           %[[UNPACK:.+]] = linalg.unpack %[[COLLAPSED]] outer_dims_perm = [2, 0, 1] inner_dims_pos = [2]
+// FUSED:           flow.return %[[UNPACK]]
+// FUSED-NOT:     flow.dispatch.region
+// FUSED:         util.return %[[RESULT]]
+util.func public @unit_batch_relu_hwc_unpack(%input: tensor<1x1x16x16x8xf32>, %filter: tensor<2x1x3x3x8x8xf32>) -> tensor<14x14x16xf32> {
+  %zero = arith.constant 0.0 : f32
+  %empty = tensor.empty() : tensor<1x2x14x14x8xf32>
+  %init = linalg.fill ins(%zero : f32) outs(%empty : tensor<1x2x14x14x8xf32>) -> tensor<1x2x14x14x8xf32>
+  %conv = linalg.generic {indexing_maps = [#input, #filter, #output],
+    iterator_types = ["parallel", "parallel", "parallel", "parallel", "reduction", "reduction", "reduction", "parallel", "reduction"]}
+    ins(%input, %filter : tensor<1x1x16x16x8xf32>, tensor<2x1x3x3x8x8xf32>) outs(%init : tensor<1x2x14x14x8xf32>) {
+  ^bb0(%lhs: f32, %rhs: f32, %acc: f32):
+    %mul = arith.mulf %lhs, %rhs : f32
+    %sum = arith.addf %mul, %acc : f32
+    linalg.yield %sum : f32
+  } -> tensor<1x2x14x14x8xf32>
+  %relu = linalg.generic {indexing_maps = [#identity, #identity],
+    iterator_types = ["parallel", "parallel", "parallel", "parallel", "parallel"]}
+    ins(%conv : tensor<1x2x14x14x8xf32>) outs(%empty : tensor<1x2x14x14x8xf32>) {
+  ^bb0(%in: f32, %out: f32):
+    %max = arith.maximumf %in, %zero : f32
+    linalg.yield %max : f32
+  } -> tensor<1x2x14x14x8xf32>
+  %collapsed = tensor.collapse_shape %relu [[0, 1], [2], [3], [4]] : tensor<1x2x14x14x8xf32> into tensor<2x14x14x8xf32>
+  %dest = tensor.empty() : tensor<14x14x16xf32>
+  %unpack = linalg.unpack %collapsed outer_dims_perm = [2, 0, 1] inner_dims_pos = [2] inner_tiles = [8] into %dest : tensor<2x14x14x8xf32> -> tensor<14x14x16xf32>
+  util.return %unpack : tensor<14x14x16xf32>
+}
+
+// -----
+
+// Permuting the batch dimension is not a layout that materialization produces.
+#input = affine_map<(n, oc, h, w, ic, kh, kw, o, i) -> (n, ic, h + kh, w + kw, i)>
+#filter = affine_map<(n, oc, h, w, ic, kh, kw, o, i) -> (oc, ic, kh, kw, i, o)>
+#output = affine_map<(n, oc, h, w, ic, kh, kw, o, i) -> (n, oc, h, w, o)>
+// FUSED-LABEL: util.func public @no_fuse_permuted_unpack(
+// FUSED:         %[[CONV_DISPATCH:.+]] = flow.dispatch.region
+// FUSED:           %[[CONV:.+]] = linalg.generic
+// FUSED:           flow.return %[[CONV]]
+// FUSED:         flow.dispatch.region
+// FUSED:           linalg.unpack %[[CONV_DISPATCH]]
+util.func public @no_fuse_permuted_unpack(%input: tensor<2x1x16x16x8xf32>, %filter: tensor<2x1x3x3x8x8xf32>) -> tensor<2x14x14x16xf32> {
+  %zero = arith.constant 0.0 : f32
+  %empty = tensor.empty() : tensor<2x2x14x14x8xf32>
+  %init = linalg.fill ins(%zero : f32) outs(%empty : tensor<2x2x14x14x8xf32>) -> tensor<2x2x14x14x8xf32>
+  %conv = linalg.generic {indexing_maps = [#input, #filter, #output],
+    iterator_types = ["parallel", "parallel", "parallel", "parallel", "reduction", "reduction", "reduction", "parallel", "reduction"]}
+    ins(%input, %filter : tensor<2x1x16x16x8xf32>, tensor<2x1x3x3x8x8xf32>) outs(%init : tensor<2x2x14x14x8xf32>) {
+  ^bb0(%lhs: f32, %rhs: f32, %acc: f32):
+    %mul = arith.mulf %lhs, %rhs : f32
+    %sum = arith.addf %mul, %acc : f32
+    linalg.yield %sum : f32
+  } -> tensor<2x2x14x14x8xf32>
+  %dest = tensor.empty() : tensor<2x14x14x16xf32>
+  %unpack = linalg.unpack %conv outer_dims_perm = [3, 0, 1, 2] inner_dims_pos = [3] inner_tiles = [8] into %dest : tensor<2x2x14x14x8xf32> -> tensor<2x14x14x16xf32>
+  util.return %unpack : tensor<2x14x14x16xf32>
+}
+
+// -----
+
+// Collapsing a non-unit batch dimension mixes batch and channel data.
+#input = affine_map<(n, oc, h, w, ic, kh, kw, o, i) -> (n, ic, h + kh, w + kw, i)>
+#filter = affine_map<(n, oc, h, w, ic, kh, kw, o, i) -> (oc, ic, kh, kw, i, o)>
+#output = affine_map<(n, oc, h, w, ic, kh, kw, o, i) -> (n, oc, h, w, o)>
+// FUSED-LABEL: util.func public @no_fuse_nonunit_batch_collapse(
+// FUSED:         %[[CONV_DISPATCH:.+]] = flow.dispatch.region
+// FUSED:           %[[CONV:.+]] = linalg.generic
+// FUSED:           flow.return %[[CONV]]
+// FUSED:         %[[COLLAPSED:.+]] = tensor.collapse_shape %[[CONV_DISPATCH]]
+// FUSED:         flow.dispatch.region
+// FUSED:           linalg.unpack %[[COLLAPSED]]
+util.func public @no_fuse_nonunit_batch_collapse(%input: tensor<2x1x16x16x8xf32>, %filter: tensor<2x1x3x3x8x8xf32>) -> tensor<14x14x32xf32> {
+  %zero = arith.constant 0.0 : f32
+  %empty = tensor.empty() : tensor<2x2x14x14x8xf32>
+  %init = linalg.fill ins(%zero : f32) outs(%empty : tensor<2x2x14x14x8xf32>) -> tensor<2x2x14x14x8xf32>
+  %conv = linalg.generic {indexing_maps = [#input, #filter, #output],
+    iterator_types = ["parallel", "parallel", "parallel", "parallel", "reduction", "reduction", "reduction", "parallel", "reduction"]}
+    ins(%input, %filter : tensor<2x1x16x16x8xf32>, tensor<2x1x3x3x8x8xf32>) outs(%init : tensor<2x2x14x14x8xf32>) {
+  ^bb0(%lhs: f32, %rhs: f32, %acc: f32):
+    %mul = arith.mulf %lhs, %rhs : f32
+    %sum = arith.addf %mul, %acc : f32
+    linalg.yield %sum : f32
+  } -> tensor<2x2x14x14x8xf32>
+  %collapsed = tensor.collapse_shape %conv [[0, 1], [2], [3], [4]] : tensor<2x2x14x14x8xf32> into tensor<4x14x14x8xf32>
+  %dest = tensor.empty() : tensor<14x14x32xf32>
+  %unpack = linalg.unpack %collapsed outer_dims_perm = [2, 0, 1] inner_dims_pos = [2] inner_tiles = [8] into %dest : tensor<4x14x14x8xf32> -> tensor<14x14x32xf32>
+  util.return %unpack : tensor<14x14x32xf32>
+}
+
+// -----
+
+// Batchless CHW is not matched. The unit-batch collapse only joins together
+// with an unpack that joins, so neither of them is fused.
+#input = affine_map<(n, oc, h, w, ic, kh, kw, o, i) -> (n, ic, h + kh, w + kw, i)>
+#filter = affine_map<(n, oc, h, w, ic, kh, kw, o, i) -> (oc, ic, kh, kw, i, o)>
+#output = affine_map<(n, oc, h, w, ic, kh, kw, o, i) -> (n, oc, h, w, o)>
+// FUSED-LABEL: util.func public @no_fuse_unit_batch_chw_unpack(
+// FUSED:         %[[CONV_DISPATCH:.+]] = flow.dispatch.region
+// FUSED:           %[[CONV:.+]] = linalg.generic
+// FUSED:           flow.return %[[CONV]]
+// FUSED:         %[[COLLAPSED:.+]] = tensor.collapse_shape %[[CONV_DISPATCH]]
+// FUSED:         flow.dispatch.region
+// FUSED:           linalg.unpack %[[COLLAPSED]]
+util.func public @no_fuse_unit_batch_chw_unpack(%input: tensor<1x1x16x16x8xf32>, %filter: tensor<2x1x3x3x8x8xf32>) -> tensor<16x14x14xf32> {
+  %zero = arith.constant 0.0 : f32
+  %empty = tensor.empty() : tensor<1x2x14x14x8xf32>
+  %init = linalg.fill ins(%zero : f32) outs(%empty : tensor<1x2x14x14x8xf32>) -> tensor<1x2x14x14x8xf32>
+  %conv = linalg.generic {indexing_maps = [#input, #filter, #output],
+    iterator_types = ["parallel", "parallel", "parallel", "parallel", "reduction", "reduction", "reduction", "parallel", "reduction"]}
+    ins(%input, %filter : tensor<1x1x16x16x8xf32>, tensor<2x1x3x3x8x8xf32>) outs(%init : tensor<1x2x14x14x8xf32>) {
+  ^bb0(%lhs: f32, %rhs: f32, %acc: f32):
+    %mul = arith.mulf %lhs, %rhs : f32
+    %sum = arith.addf %mul, %acc : f32
+    linalg.yield %sum : f32
+  } -> tensor<1x2x14x14x8xf32>
+  %collapsed = tensor.collapse_shape %conv [[0, 1], [2], [3], [4]] : tensor<1x2x14x14x8xf32> into tensor<2x14x14x8xf32>
+  %dest = tensor.empty() : tensor<16x14x14xf32>
+  %unpack = linalg.unpack %collapsed inner_dims_pos = [0] inner_tiles = [8] into %dest : tensor<2x14x14x8xf32> -> tensor<16x14x14xf32>
+  util.return %unpack : tensor<16x14x14xf32>
+}
+
+// -----
+
+// Consumers that do not preserve the data-tiled layout fuse with the
+// convolution as they do without the option, which only adds the result
+// unpack.
+#input = affine_map<(n, oc, h, w, ic, kh, kw, o, i) -> (n, ic, h + kh, w + kw, i)>
+#filter = affine_map<(n, oc, h, w, ic, kh, kw, o, i) -> (oc, ic, kh, kw, i, o)>
+#output = affine_map<(n, oc, h, w, ic, kh, kw, o, i) -> (n, oc, h, w, o)>
+#id = affine_map<(d0, d1, d2, d3, d4) -> (d0, d1, d2, d3, d4)>
+#swap_hw = affine_map<(d0, d1, d2, d3, d4) -> (d0, d1, d3, d2, d4)>
+// CHECK-LABEL: util.func public @permuted_consumer(
+// CHECK:         %[[RESULT:.+]] = flow.dispatch.region -> (tensor<2x2x14x14x8xf32>)
+// CHECK:           %[[CONV:.+]] = linalg.generic
+// CHECK:           %[[NEG:.+]] = linalg.generic
+// CHECK-SAME:        ins(%[[CONV]] : tensor<2x2x14x14x8xf32>)
+// CHECK:           flow.return %[[NEG]]
+// CHECK-NOT:     flow.dispatch.region
+// CHECK:         util.return %[[RESULT]]
+util.func public @permuted_consumer(%input: tensor<2x1x16x16x8xf32>, %filter: tensor<2x1x3x3x8x8xf32>) -> tensor<2x2x14x14x8xf32> {
+  %zero = arith.constant 0.0 : f32
+  %empty = tensor.empty() : tensor<2x2x14x14x8xf32>
+  %init = linalg.fill ins(%zero : f32) outs(%empty : tensor<2x2x14x14x8xf32>) -> tensor<2x2x14x14x8xf32>
+  %conv = linalg.generic {indexing_maps = [#input, #filter, #output],
+    iterator_types = ["parallel", "parallel", "parallel", "parallel", "reduction", "reduction", "reduction", "parallel", "reduction"]}
+    ins(%input, %filter : tensor<2x1x16x16x8xf32>, tensor<2x1x3x3x8x8xf32>) outs(%init : tensor<2x2x14x14x8xf32>) {
+  ^bb0(%lhs: f32, %rhs: f32, %acc: f32):
+    %mul = arith.mulf %lhs, %rhs : f32
+    %sum = arith.addf %mul, %acc : f32
+    linalg.yield %sum : f32
+  } -> tensor<2x2x14x14x8xf32>
+  %neg = linalg.generic {indexing_maps = [#swap_hw, #id], iterator_types = ["parallel", "parallel", "parallel", "parallel", "parallel"]}
+    ins(%conv : tensor<2x2x14x14x8xf32>) outs(%empty : tensor<2x2x14x14x8xf32>) {
+  ^bb0(%in: f32, %out: f32):
+    %0 = arith.negf %in : f32
+    linalg.yield %0 : f32
+  } -> tensor<2x2x14x14x8xf32>
+  util.return %neg : tensor<2x2x14x14x8xf32>
+}
+
+// -----
+
+// The same holds for a consumer that updates the convolution result in place.
+#input = affine_map<(n, oc, h, w, ic, kh, kw, o, i) -> (n, ic, h + kh, w + kw, i)>
+#filter = affine_map<(n, oc, h, w, ic, kh, kw, o, i) -> (oc, ic, kh, kw, i, o)>
+#output = affine_map<(n, oc, h, w, ic, kh, kw, o, i) -> (n, oc, h, w, o)>
+#id = affine_map<(d0, d1, d2, d3, d4) -> (d0, d1, d2, d3, d4)>
+// CHECK-LABEL: util.func public @init_consumer(
+// CHECK:         %[[RESULT:.+]] = flow.dispatch.region -> (tensor<2x2x14x14x8xf32>)
+// CHECK:           %[[CONV:.+]] = linalg.generic
+// CHECK:           %[[ADD:.+]] = linalg.generic
+// CHECK-SAME:        outs(%[[CONV]] : tensor<2x2x14x14x8xf32>)
+// CHECK:           flow.return %[[ADD]]
+// CHECK-NOT:     flow.dispatch.region
+// CHECK:         util.return %[[RESULT]]
+util.func public @init_consumer(%input: tensor<2x1x16x16x8xf32>, %filter: tensor<2x1x3x3x8x8xf32>, %other: tensor<2x2x14x14x8xf32>) -> tensor<2x2x14x14x8xf32> {
+  %zero = arith.constant 0.0 : f32
+  %empty = tensor.empty() : tensor<2x2x14x14x8xf32>
+  %init = linalg.fill ins(%zero : f32) outs(%empty : tensor<2x2x14x14x8xf32>) -> tensor<2x2x14x14x8xf32>
+  %conv = linalg.generic {indexing_maps = [#input, #filter, #output],
+    iterator_types = ["parallel", "parallel", "parallel", "parallel", "reduction", "reduction", "reduction", "parallel", "reduction"]}
+    ins(%input, %filter : tensor<2x1x16x16x8xf32>, tensor<2x1x3x3x8x8xf32>) outs(%init : tensor<2x2x14x14x8xf32>) {
+  ^bb0(%lhs: f32, %rhs: f32, %acc: f32):
+    %mul = arith.mulf %lhs, %rhs : f32
+    %sum = arith.addf %mul, %acc : f32
+    linalg.yield %sum : f32
+  } -> tensor<2x2x14x14x8xf32>
+  %add = linalg.generic {indexing_maps = [#id, #id], iterator_types = ["parallel", "parallel", "parallel", "parallel", "parallel"]}
+    ins(%other : tensor<2x2x14x14x8xf32>) outs(%conv : tensor<2x2x14x14x8xf32>) {
+  ^bb0(%in: f32, %out: f32):
+    %0 = arith.addf %in, %out : f32
+    linalg.yield %0 : f32
+  } -> tensor<2x2x14x14x8xf32>
+  util.return %add : tensor<2x2x14x14x8xf32>
+}
+
+// -----
+
+// The collapse has a second use, so the unpack could not follow it into the
+// convolution dispatch, and the dispatch would end in the collapse. Neither
+// joins the dispatch.
+#input = affine_map<(n, oc, h, w, ic, kh, kw, o, i) -> (n, ic, h + kh, w + kw, i)>
+#filter = affine_map<(n, oc, h, w, ic, kh, kw, o, i) -> (oc, ic, kh, kw, i, o)>
+#output = affine_map<(n, oc, h, w, ic, kh, kw, o, i) -> (n, oc, h, w, o)>
+// CHECK-LABEL: util.func public @no_fuse_multi_use_collapse(
+// CHECK:         %[[CONV_DISPATCH:.+]] = flow.dispatch.region
+// CHECK:           %[[CONV:.+]] = linalg.generic
+// CHECK:           flow.return %[[CONV]]
+// CHECK:         %[[COLLAPSE:.+]] = tensor.collapse_shape %[[CONV_DISPATCH]]
+// CHECK:         %[[BARRIER:.+]] = util.optimization_barrier %[[COLLAPSE]]
+// CHECK:         %[[UNPACK_DISPATCH:.+]] = flow.dispatch.region
+// CHECK:           %[[UNPACK:.+]] = linalg.unpack %[[COLLAPSE]]
+// CHECK:           flow.return %[[UNPACK]]
+// CHECK:         util.return %[[UNPACK_DISPATCH]], %[[BARRIER]]
+util.func public @no_fuse_multi_use_collapse(%input: tensor<1x1x16x16x8xf32>, %filter: tensor<2x1x3x3x8x8xf32>) -> (tensor<14x14x16xf32>, tensor<2x14x14x8xf32>) {
+  %zero = arith.constant 0.0 : f32
+  %empty = tensor.empty() : tensor<1x2x14x14x8xf32>
+  %init = linalg.fill ins(%zero : f32) outs(%empty : tensor<1x2x14x14x8xf32>) -> tensor<1x2x14x14x8xf32>
+  %conv = linalg.generic {indexing_maps = [#input, #filter, #output],
+    iterator_types = ["parallel", "parallel", "parallel", "parallel", "reduction", "reduction", "reduction", "parallel", "reduction"]}
+    ins(%input, %filter : tensor<1x1x16x16x8xf32>, tensor<2x1x3x3x8x8xf32>) outs(%init : tensor<1x2x14x14x8xf32>) {
+  ^bb0(%lhs: f32, %rhs: f32, %acc: f32):
+    %mul = arith.mulf %lhs, %rhs : f32
+    %sum = arith.addf %mul, %acc : f32
+    linalg.yield %sum : f32
+  } -> tensor<1x2x14x14x8xf32>
+  %collapsed = tensor.collapse_shape %conv [[0, 1], [2], [3], [4]] : tensor<1x2x14x14x8xf32> into tensor<2x14x14x8xf32>
+  %barrier = util.optimization_barrier %collapsed : tensor<2x14x14x8xf32>
+  %dest = tensor.empty() : tensor<14x14x16xf32>
+  %unpack = linalg.unpack %collapsed outer_dims_perm = [2, 0, 1] inner_dims_pos = [2] inner_tiles = [8] into %dest : tensor<2x14x14x8xf32> -> tensor<14x14x16xf32>
+  util.return %unpack, %barrier : tensor<14x14x16xf32>, tensor<2x14x14x8xf32>
+}
+
+// -----
+
+// The unpack is nested in an scf.if, where the convolution dispatch cannot
+// reach it, so the collapse does not join either.
+#input = affine_map<(n, oc, h, w, ic, kh, kw, o, i) -> (n, ic, h + kh, w + kw, i)>
+#filter = affine_map<(n, oc, h, w, ic, kh, kw, o, i) -> (oc, ic, kh, kw, i, o)>
+#output = affine_map<(n, oc, h, w, ic, kh, kw, o, i) -> (n, oc, h, w, o)>
+// CHECK-LABEL: util.func public @no_fuse_collapse_of_nested_unpack(
+// CHECK:         %[[CONV_DISPATCH:.+]] = flow.dispatch.region
+// CHECK:           %[[CONV:.+]] = linalg.generic
+// CHECK:           flow.return %[[CONV]]
+// CHECK:         %[[COLLAPSE:.+]] = tensor.collapse_shape %[[CONV_DISPATCH]]
+// CHECK:         scf.if
+// CHECK:           flow.dispatch.region
+// CHECK:             linalg.unpack %[[COLLAPSE]]
+util.func public @no_fuse_collapse_of_nested_unpack(%input: tensor<1x1x16x16x8xf32>, %filter: tensor<2x1x3x3x8x8xf32>, %cond: i1) -> tensor<14x14x16xf32> {
+  %zero = arith.constant 0.0 : f32
+  %empty = tensor.empty() : tensor<1x2x14x14x8xf32>
+  %init = linalg.fill ins(%zero : f32) outs(%empty : tensor<1x2x14x14x8xf32>) -> tensor<1x2x14x14x8xf32>
+  %conv = linalg.generic {indexing_maps = [#input, #filter, #output],
+    iterator_types = ["parallel", "parallel", "parallel", "parallel", "reduction", "reduction", "reduction", "parallel", "reduction"]}
+    ins(%input, %filter : tensor<1x1x16x16x8xf32>, tensor<2x1x3x3x8x8xf32>) outs(%init : tensor<1x2x14x14x8xf32>) {
+  ^bb0(%lhs: f32, %rhs: f32, %acc: f32):
+    %mul = arith.mulf %lhs, %rhs : f32
+    %sum = arith.addf %mul, %acc : f32
+    linalg.yield %sum : f32
+  } -> tensor<1x2x14x14x8xf32>
+  %collapsed = tensor.collapse_shape %conv [[0, 1], [2], [3], [4]] : tensor<1x2x14x14x8xf32> into tensor<2x14x14x8xf32>
+  %dest = tensor.empty() : tensor<14x14x16xf32>
+  %result = scf.if %cond -> tensor<14x14x16xf32> {
+    %unpack = linalg.unpack %collapsed outer_dims_perm = [2, 0, 1] inner_dims_pos = [2] inner_tiles = [8] into %dest : tensor<2x14x14x8xf32> -> tensor<14x14x16xf32>
+    scf.yield %unpack : tensor<14x14x16xf32>
+  } else {
+    scf.yield %dest : tensor<14x14x16xf32>
+  }
+  util.return %result : tensor<14x14x16xf32>
+}
+
+// -----
+
+// The unpack destination is another reshape of the convolution result, so the
+// unpack cannot join the dispatch, and the collapse does not join either.
+#input = affine_map<(n, oc, h, w, ic, kh, kw, o, i) -> (n, ic, h + kh, w + kw, i)>
+#filter = affine_map<(n, oc, h, w, ic, kh, kw, o, i) -> (oc, ic, kh, kw, i, o)>
+#output = affine_map<(n, oc, h, w, ic, kh, kw, o, i) -> (n, oc, h, w, o)>
+// AGGRESSIVE-LABEL: util.func public @no_fuse_collapse_of_unpack_with_dependent_dest(
+// AGGRESSIVE:         %[[CONV_DISPATCH:.+]] = flow.dispatch.region
+// AGGRESSIVE:           %[[CONV:.+]] = linalg.generic
+// AGGRESSIVE:           flow.return %[[CONV]]
+// AGGRESSIVE:         %[[COLLAPSE:.+]] = tensor.collapse_shape %[[CONV_DISPATCH]] {{\[}}[0, 1], [2], [3], [4]]
+// AGGRESSIVE:         flow.dispatch.region
+// AGGRESSIVE:           linalg.unpack %[[COLLAPSE]]
+util.func public @no_fuse_collapse_of_unpack_with_dependent_dest(%input: tensor<1x1x16x16x8xf32>, %filter: tensor<2x1x3x3x8x8xf32>) -> tensor<14x14x16xf32> {
+  %zero = arith.constant 0.0 : f32
+  %empty = tensor.empty() : tensor<1x2x14x14x8xf32>
+  %init = linalg.fill ins(%zero : f32) outs(%empty : tensor<1x2x14x14x8xf32>) -> tensor<1x2x14x14x8xf32>
+  %conv = linalg.generic {indexing_maps = [#input, #filter, #output],
+    iterator_types = ["parallel", "parallel", "parallel", "parallel", "reduction", "reduction", "reduction", "parallel", "reduction"]}
+    ins(%input, %filter : tensor<1x1x16x16x8xf32>, tensor<2x1x3x3x8x8xf32>) outs(%init : tensor<1x2x14x14x8xf32>) {
+  ^bb0(%lhs: f32, %rhs: f32, %acc: f32):
+    %mul = arith.mulf %lhs, %rhs : f32
+    %sum = arith.addf %mul, %acc : f32
+    linalg.yield %sum : f32
+  } -> tensor<1x2x14x14x8xf32>
+  %collapsed = tensor.collapse_shape %conv [[0, 1], [2], [3], [4]] : tensor<1x2x14x14x8xf32> into tensor<2x14x14x8xf32>
+  %flat = tensor.collapse_shape %conv [[0, 1, 2, 3, 4]] : tensor<1x2x14x14x8xf32> into tensor<3136xf32>
+  %dest = tensor.expand_shape %flat [[0, 1, 2]] output_shape [14, 14, 16] : tensor<3136xf32> into tensor<14x14x16xf32>
+  %unpack = linalg.unpack %collapsed outer_dims_perm = [2, 0, 1] inner_dims_pos = [2] inner_tiles = [8] into %dest : tensor<2x14x14x8xf32> -> tensor<14x14x16xf32>
+  util.return %unpack : tensor<14x14x16xf32>
+}
+
+// -----
+
+// The permuted consumer joins without the option, so the unpack does not reach
+// the convolution through epilogues and becomes a root of its own. The collapse
+// then does not join the convolution dispatch either.
+#input = affine_map<(n, oc, h, w, ic, kh, kw, o, i) -> (n, ic, h + kh, w + kw, i)>
+#filter = affine_map<(n, oc, h, w, ic, kh, kw, o, i) -> (oc, ic, kh, kw, i, o)>
+#output = affine_map<(n, oc, h, w, ic, kh, kw, o, i) -> (n, oc, h, w, o)>
+#id = affine_map<(d0, d1, d2, d3, d4) -> (d0, d1, d2, d3, d4)>
+#swap_hw = affine_map<(d0, d1, d2, d3, d4) -> (d0, d1, d3, d2, d4)>
+// CHECK-LABEL: util.func public @no_fuse_collapse_of_unclaimed_unpack(
+// CHECK:         %[[NEG_DISPATCH:.+]] = flow.dispatch.region
+// CHECK:           %[[CONV:.+]] = linalg.generic
+// CHECK:           %[[NEG:.+]] = linalg.generic
+// CHECK-SAME:        ins(%[[CONV]] : tensor<1x2x14x14x8xf32>)
+// CHECK:           flow.return %[[NEG]]
+// CHECK:         %[[COLLAPSE:.+]] = tensor.collapse_shape %[[NEG_DISPATCH]]
+// CHECK:         flow.dispatch.region
+// CHECK:           linalg.unpack %[[COLLAPSE]]
+util.func public @no_fuse_collapse_of_unclaimed_unpack(%input: tensor<1x1x16x16x8xf32>, %filter: tensor<2x1x3x3x8x8xf32>) -> tensor<14x14x16xf32> {
+  %zero = arith.constant 0.0 : f32
+  %empty = tensor.empty() : tensor<1x2x14x14x8xf32>
+  %init = linalg.fill ins(%zero : f32) outs(%empty : tensor<1x2x14x14x8xf32>) -> tensor<1x2x14x14x8xf32>
+  %conv = linalg.generic {indexing_maps = [#input, #filter, #output],
+    iterator_types = ["parallel", "parallel", "parallel", "parallel", "reduction", "reduction", "reduction", "parallel", "reduction"]}
+    ins(%input, %filter : tensor<1x1x16x16x8xf32>, tensor<2x1x3x3x8x8xf32>) outs(%init : tensor<1x2x14x14x8xf32>) {
+  ^bb0(%lhs: f32, %rhs: f32, %acc: f32):
+    %mul = arith.mulf %lhs, %rhs : f32
+    %sum = arith.addf %mul, %acc : f32
+    linalg.yield %sum : f32
+  } -> tensor<1x2x14x14x8xf32>
+  %neg = linalg.generic {indexing_maps = [#swap_hw, #id], iterator_types = ["parallel", "parallel", "parallel", "parallel", "parallel"]}
+    ins(%conv : tensor<1x2x14x14x8xf32>) outs(%empty : tensor<1x2x14x14x8xf32>) {
+  ^bb0(%in: f32, %out: f32):
+    %0 = arith.negf %in : f32
+    linalg.yield %0 : f32
+  } -> tensor<1x2x14x14x8xf32>
+  %collapsed = tensor.collapse_shape %neg [[0, 1], [2], [3], [4]] : tensor<1x2x14x14x8xf32> into tensor<2x14x14x8xf32>
+  %dest = tensor.empty() : tensor<14x14x16xf32>
+  %unpack = linalg.unpack %collapsed outer_dims_perm = [2, 0, 1] inner_dims_pos = [2] inner_tiles = [8] into %dest : tensor<2x14x14x8xf32> -> tensor<14x14x16xf32>
+  util.return %unpack : tensor<14x14x16xf32>
+}
diff --git a/compiler/src/iree/compiler/DispatchCreation/test/form_dispatch_regions.mlir b/compiler/src/iree/compiler/DispatchCreation/test/form_dispatch_regions.mlir
index e0f5f29..17c0af7 100644
--- a/compiler/src/iree/compiler/DispatchCreation/test/form_dispatch_regions.mlir
+++ b/compiler/src/iree/compiler/DispatchCreation/test/form_dispatch_regions.mlir
@@ -1,6 +1,7 @@
 // RUN: iree-opt --pass-pipeline="builtin.module(util.func(iree-dispatch-creation-form-dispatch-regions{aggressive-fusion=true}))" --split-input-file %s | FileCheck %s
 // RUN: iree-opt --pass-pipeline="builtin.module(util.func(iree-dispatch-creation-form-dispatch-regions{aggressive-fusion=true fuse-multi-use-producers=false}))" --split-input-file %s | FileCheck %s --check-prefix=NO-MULTI-USE
 // RUN: iree-opt --pass-pipeline="builtin.module(util.func(iree-dispatch-creation-form-dispatch-regions))" --split-input-file %s | FileCheck %s --check-prefix=DEFAULT
+// RUN: iree-opt --pass-pipeline="builtin.module(util.func(iree-dispatch-creation-form-dispatch-regions{fuse-mmt4d=true}))" --split-input-file %s | FileCheck %s --check-prefix=FUSE-MMT4D
 
 util.func public @pack_elementwise_fusion(%arg0 : tensor<?xf32>,
     %arg1 : tensor<?x?xf32>) -> tensor<?x?x8x32xf32> {
@@ -771,6 +772,79 @@
 //       CHECK:   flow.return %[[GEN]] : tensor<1x1x32x1x4xf32>
 //       CHECK:   util.return %[[DISP1]] : tensor<1x1x32x1x4xf32>
 
+// With fuse-mmt4d, the elementwise consumer joins the batch_mmt4d dispatch.
+// FUSE-MMT4D-LABEL: util.func public @no_batch_mmt4d_fusion
+//  FUSE-MMT4D-SAME:   %[[ARG0:[a-zA-Z0-9_]+]]: tensor<1x1x64x1x1xf32>
+//  FUSE-MMT4D-SAME:   %[[ARG1:[a-zA-Z0-9_]+]]: tensor<1x32x64x4x1xf32>
+//  FUSE-MMT4D-SAME:   %[[ARG2:[a-zA-Z0-9_]+]]: tensor<1x1x32x1x4xf32>
+//       FUSE-MMT4D:   %[[DISP:.+]] = flow.dispatch.region -> (tensor<1x1x32x1x4xf32>)
+//       FUSE-MMT4D:     %[[MMT4D:.+]] = linalg.batch_mmt4d
+//  FUSE-MMT4D-SAME:         ins(%[[ARG0]], %[[ARG1]] : tensor<1x1x64x1x1xf32>, tensor<1x32x64x4x1xf32>)
+//       FUSE-MMT4D:     %[[GEN:.+]] = linalg.generic
+//  FUSE-MMT4D-SAME:         ins(%[[ARG2]], %[[MMT4D]] : tensor<1x1x32x1x4xf32>, tensor<1x1x32x1x4xf32>)
+//       FUSE-MMT4D:     flow.return %[[GEN]] : tensor<1x1x32x1x4xf32>
+//   FUSE-MMT4D-NOT:   flow.dispatch.region
+//       FUSE-MMT4D:   util.return %[[DISP]] : tensor<1x1x32x1x4xf32>
+
+
+// -----
+
+// The mmt4d result has a second use, so its dispatch does not claim the unpack.
+// The unclaimed unpack then fuses with its consumer before the later matmul
+// can, as it does without fuse-mmt4d.
+#map = affine_map<(d0, d1, d2, d3) -> (d0, d1, d2, d3)>
+#map1 = affine_map<(d0, d1) -> (d0, d1)>
+util.func public @unclaimed_unpack_program_order(%lhs: tensor<8x128x8x1xf32>, %rhs: tensor<8x128x8x1xf32>,
+    %a: tensor<64x32xf32>, %b: tensor<32x64xf32>) -> (tensor<64x64xf32>, tensor<8x8x8x8xf32>) {
+  %zero = arith.constant 0.000000e+00 : f32
+  %empty = tensor.empty() : tensor<8x8x8x8xf32>
+  %fill = linalg.fill ins(%zero : f32) outs(%empty : tensor<8x8x8x8xf32>) -> tensor<8x8x8x8xf32>
+  %mmt4d = linalg.mmt4d ins(%lhs, %rhs : tensor<8x128x8x1xf32>, tensor<8x128x8x1xf32>) outs(%fill : tensor<8x8x8x8xf32>) -> tensor<8x8x8x8xf32>
+  %relu = linalg.generic {indexing_maps = [#map, #map], iterator_types = ["parallel", "parallel", "parallel", "parallel"]} ins(%mmt4d : tensor<8x8x8x8xf32>) outs(%empty : tensor<8x8x8x8xf32>) {
+  ^bb0(%in: f32, %out: f32):
+    %max = arith.maximumf %in, %zero : f32
+    linalg.yield %max : f32
+  } -> tensor<8x8x8x8xf32>
+  %dest = tensor.empty() : tensor<64x64xf32>
+  %unpack = linalg.unpack %relu outer_dims_perm = [0, 1] inner_dims_pos = [0, 1] inner_tiles = [8, 8] into %dest : tensor<8x8x8x8xf32> -> tensor<64x64xf32>
+  %matmul_fill = linalg.fill ins(%zero : f32) outs(%dest : tensor<64x64xf32>) -> tensor<64x64xf32>
+  %matmul = linalg.matmul ins(%a, %b : tensor<64x32xf32>, tensor<32x64xf32>) outs(%matmul_fill : tensor<64x64xf32>) -> tensor<64x64xf32>
+  %add = linalg.generic {indexing_maps = [#map1, #map1, #map1], iterator_types = ["parallel", "parallel"]} ins(%unpack, %matmul : tensor<64x64xf32>, tensor<64x64xf32>) outs(%dest : tensor<64x64xf32>) {
+  ^bb0(%x: f32, %y: f32, %out: f32):
+    %sum = arith.addf %x, %y : f32
+    linalg.yield %sum : f32
+  } -> tensor<64x64xf32>
+  util.return %add, %mmt4d : tensor<64x64xf32>, tensor<8x8x8x8xf32>
+}
+//    DEFAULT-LABEL: util.func public @unclaimed_unpack_program_order
+//          DEFAULT:   %[[MMT4D_DISPATCH:.+]] = flow.dispatch.region
+//          DEFAULT:     linalg.mmt4d
+//          DEFAULT:   %[[RELU_DISPATCH:.+]] = flow.dispatch.region
+//          DEFAULT:     linalg.generic
+//          DEFAULT:   %[[MATMUL_DISPATCH:.+]] = flow.dispatch.region
+//          DEFAULT:     %[[MATMUL:.+]] = linalg.matmul
+//          DEFAULT:     flow.return %[[MATMUL]]
+//          DEFAULT:   %[[ADD_DISPATCH:.+]] = flow.dispatch.region
+//          DEFAULT:     %[[UNPACK:.+]] = linalg.unpack %[[RELU_DISPATCH]]
+//          DEFAULT:     %[[ADD:.+]] = linalg.generic
+//     DEFAULT-SAME:       ins(%[[UNPACK]], %[[MATMUL_DISPATCH]] :
+//          DEFAULT:     flow.return %[[ADD]]
+//          DEFAULT:   util.return %[[ADD_DISPATCH]], %[[MMT4D_DISPATCH]]
+// FUSE-MMT4D-LABEL: util.func public @unclaimed_unpack_program_order
+//       FUSE-MMT4D:   %[[MMT4D_DISPATCH:.+]] = flow.dispatch.region
+//       FUSE-MMT4D:     linalg.mmt4d
+//       FUSE-MMT4D:   %[[RELU_DISPATCH:.+]] = flow.dispatch.region
+//       FUSE-MMT4D:     linalg.generic
+//       FUSE-MMT4D:   %[[MATMUL_DISPATCH:.+]] = flow.dispatch.region
+//       FUSE-MMT4D:     %[[MATMUL:.+]] = linalg.matmul
+//       FUSE-MMT4D:     flow.return %[[MATMUL]]
+//       FUSE-MMT4D:   %[[ADD_DISPATCH:.+]] = flow.dispatch.region
+//       FUSE-MMT4D:     %[[UNPACK:.+]] = linalg.unpack %[[RELU_DISPATCH]]
+//       FUSE-MMT4D:     %[[ADD:.+]] = linalg.generic
+//  FUSE-MMT4D-SAME:       ins(%[[UNPACK]], %[[MATMUL_DISPATCH]] :
+//       FUSE-MMT4D:     flow.return %[[ADD]]
+//       FUSE-MMT4D:   util.return %[[ADD_DISPATCH]], %[[MMT4D_DISPATCH]]
+
 // -----
 
 util.func @custom_op_consumer_fusion(%arg0 : tensor<?x?xf32>, %arg1 : tensor<?xf32>) -> tensor<?xf32> {
diff --git a/compiler/src/iree/compiler/DispatchCreation/test/mmt4d_fusion.mlir b/compiler/src/iree/compiler/DispatchCreation/test/mmt4d_fusion.mlir
new file mode 100644
index 0000000..ed0a0e1
--- /dev/null
+++ b/compiler/src/iree/compiler/DispatchCreation/test/mmt4d_fusion.mlir
@@ -0,0 +1,408 @@
+// RUN: iree-opt --split-input-file --pass-pipeline="builtin.module(util.func(iree-dispatch-creation-form-dispatch-regions{fuse-mmt4d=true}))" %s | FileCheck %s
+// RUN: iree-opt --split-input-file --pass-pipeline="builtin.module(util.func(iree-dispatch-creation-form-dispatch-regions{aggressive-fusion=true fuse-mmt4d=true}))" %s | FileCheck %s --check-prefix=AGGRESSIVE
+
+// The elementwise epilogue and the row-major result unpack join the mmt4d
+// dispatch.
+#map = affine_map<(d0, d1, d2, d3) -> (d0, d1, d2, d3)>
+// CHECK-LABEL: util.func public @mmt4d_relu_unpack(
+// CHECK-SAME:    %[[LHS:[a-zA-Z0-9]+]]: tensor<8x128x8x1xf32>
+// CHECK-SAME:    %[[RHS:[a-zA-Z0-9]+]]: tensor<8x128x8x1xf32>
+// CHECK:         %[[RESULT:.+]] = flow.dispatch.region -> (tensor<64x64xf32>)
+// CHECK:           %[[MMT4D:.+]] = linalg.mmt4d ins(%[[LHS]], %[[RHS]]
+// CHECK:           %[[RELU:.+]] = linalg.generic
+// CHECK-SAME:        ins(%[[MMT4D]] : tensor<8x8x8x8xf32>)
+// CHECK:           %[[UNPACK:.+]] = linalg.unpack %[[RELU]]
+// CHECK:           flow.return %[[UNPACK]] : tensor<64x64xf32>
+// CHECK-NOT:     flow.dispatch.region
+// CHECK:         util.return %[[RESULT]]
+util.func public @mmt4d_relu_unpack(%lhs: tensor<8x128x8x1xf32>, %rhs: tensor<8x128x8x1xf32>) -> tensor<64x64xf32> {
+  %zero = arith.constant 0.000000e+00 : f32
+  %empty = tensor.empty() : tensor<8x8x8x8xf32>
+  %fill = linalg.fill ins(%zero : f32) outs(%empty : tensor<8x8x8x8xf32>) -> tensor<8x8x8x8xf32>
+  %mmt4d = linalg.mmt4d ins(%lhs, %rhs : tensor<8x128x8x1xf32>, tensor<8x128x8x1xf32>) outs(%fill : tensor<8x8x8x8xf32>) -> tensor<8x8x8x8xf32>
+  %relu = linalg.generic {indexing_maps = [#map, #map], iterator_types = ["parallel", "parallel", "parallel", "parallel"]} ins(%mmt4d : tensor<8x8x8x8xf32>) outs(%empty : tensor<8x8x8x8xf32>) {
+  ^bb0(%in: f32, %out: f32):
+    %max = arith.maximumf %in, %zero : f32
+    linalg.yield %max : f32
+  } -> tensor<8x8x8x8xf32>
+  %dest = tensor.empty() : tensor<64x64xf32>
+  %unpack = linalg.unpack %relu outer_dims_perm = [0, 1] inner_dims_pos = [0, 1] inner_tiles = [8, 8] into %dest : tensor<8x8x8x8xf32> -> tensor<64x64xf32>
+  util.return %unpack : tensor<64x64xf32>
+}
+
+// -----
+
+// Side operands of the epilogue may broadcast, such as a bias along N.
+#map = affine_map<(d0, d1, d2, d3) -> (d0, d1, d2, d3)>
+#bias = affine_map<(d0, d1, d2, d3) -> (d1, d3)>
+// CHECK-LABEL: util.func public @mmt4d_bias_relu_unpack(
+// CHECK-SAME:    %[[BIAS:[a-zA-Z0-9]+]]: tensor<8x8xf32>
+// CHECK:         %[[RESULT:.+]] = flow.dispatch.region -> (tensor<64x64xf32>)
+// CHECK:           %[[MMT4D:.+]] = linalg.mmt4d
+// CHECK:           %[[RELU:.+]] = linalg.generic
+// CHECK-SAME:        ins(%[[MMT4D]], %[[BIAS]] : tensor<8x8x8x8xf32>, tensor<8x8xf32>)
+// CHECK:           %[[UNPACK:.+]] = linalg.unpack %[[RELU]]
+// CHECK:           flow.return %[[UNPACK]] : tensor<64x64xf32>
+// CHECK-NOT:     flow.dispatch.region
+// CHECK:         util.return %[[RESULT]]
+util.func public @mmt4d_bias_relu_unpack(%lhs: tensor<8x128x8x1xf32>, %rhs: tensor<8x128x8x1xf32>, %bias: tensor<8x8xf32>) -> tensor<64x64xf32> {
+  %zero = arith.constant 0.000000e+00 : f32
+  %empty = tensor.empty() : tensor<8x8x8x8xf32>
+  %fill = linalg.fill ins(%zero : f32) outs(%empty : tensor<8x8x8x8xf32>) -> tensor<8x8x8x8xf32>
+  %mmt4d = linalg.mmt4d ins(%lhs, %rhs : tensor<8x128x8x1xf32>, tensor<8x128x8x1xf32>) outs(%fill : tensor<8x8x8x8xf32>) -> tensor<8x8x8x8xf32>
+  %relu = linalg.generic {indexing_maps = [#map, #bias, #map], iterator_types = ["parallel", "parallel", "parallel", "parallel"]} ins(%mmt4d, %bias : tensor<8x8x8x8xf32>, tensor<8x8xf32>) outs(%empty : tensor<8x8x8x8xf32>) {
+  ^bb0(%in: f32, %b: f32, %out: f32):
+    %add = arith.addf %in, %b : f32
+    %max = arith.maximumf %add, %zero : f32
+    linalg.yield %max : f32
+  } -> tensor<8x8x8x8xf32>
+  %dest = tensor.empty() : tensor<64x64xf32>
+  %unpack = linalg.unpack %relu outer_dims_perm = [0, 1] inner_dims_pos = [0, 1] inner_tiles = [8, 8] into %dest : tensor<8x8x8x8xf32> -> tensor<64x64xf32>
+  util.return %unpack : tensor<64x64xf32>
+}
+
+// -----
+
+// Dynamic outer dimensions fuse like static ones.
+#map = affine_map<(d0, d1, d2, d3) -> (d0, d1, d2, d3)>
+// CHECK-LABEL: util.func public @mmt4d_dynamic_relu_unpack(
+// CHECK-SAME:    %[[M:[a-zA-Z0-9]+]]: index
+// CHECK:         %[[RESULT:.+]] = flow.dispatch.region -> (tensor<?x64xf32>{%[[M]]})
+// CHECK:           %[[MMT4D:.+]] = linalg.mmt4d
+// CHECK:           %[[RELU:.+]] = linalg.generic
+// CHECK-SAME:        ins(%[[MMT4D]] : tensor<?x8x8x8xf32>)
+// CHECK:           %[[UNPACK:.+]] = linalg.unpack %[[RELU]]
+// CHECK:           flow.return %[[UNPACK]] : tensor<?x64xf32>
+// CHECK-NOT:     flow.dispatch.region
+// CHECK:         util.return %[[RESULT]]
+util.func public @mmt4d_dynamic_relu_unpack(%lhs: tensor<?x128x8x1xf32>, %rhs: tensor<8x128x8x1xf32>, %m: index) -> tensor<?x64xf32> {
+  %c0 = arith.constant 0 : index
+  %zero = arith.constant 0.000000e+00 : f32
+  %m1 = tensor.dim %lhs, %c0 : tensor<?x128x8x1xf32>
+  %empty = tensor.empty(%m1) : tensor<?x8x8x8xf32>
+  %fill = linalg.fill ins(%zero : f32) outs(%empty : tensor<?x8x8x8xf32>) -> tensor<?x8x8x8xf32>
+  %mmt4d = linalg.mmt4d ins(%lhs, %rhs : tensor<?x128x8x1xf32>, tensor<8x128x8x1xf32>) outs(%fill : tensor<?x8x8x8xf32>) -> tensor<?x8x8x8xf32>
+  %relu = linalg.generic {indexing_maps = [#map, #map], iterator_types = ["parallel", "parallel", "parallel", "parallel"]} ins(%mmt4d : tensor<?x8x8x8xf32>) outs(%empty : tensor<?x8x8x8xf32>) {
+  ^bb0(%in: f32, %out: f32):
+    %max = arith.maximumf %in, %zero : f32
+    linalg.yield %max : f32
+  } -> tensor<?x8x8x8xf32>
+  %dest = tensor.empty(%m) : tensor<?x64xf32>
+  %unpack = linalg.unpack %relu outer_dims_perm = [0, 1] inner_dims_pos = [0, 1] inner_tiles = [8, 8] into %dest : tensor<?x8x8x8xf32> -> tensor<?x64xf32>
+  util.return %unpack : tensor<?x64xf32>
+}
+
+// -----
+
+#map = affine_map<(d0, d1, d2, d3, d4) -> (d0, d1, d2, d3, d4)>
+// CHECK-LABEL: util.func public @batch_mmt4d_relu_unpack(
+// CHECK:         %[[RESULT:.+]] = flow.dispatch.region -> (tensor<2x64x64xf32>)
+// CHECK:           %[[MMT4D:.+]] = linalg.batch_mmt4d
+// CHECK:           %[[RELU:.+]] = linalg.generic
+// CHECK-SAME:        ins(%[[MMT4D]] : tensor<2x8x8x8x8xf32>)
+// CHECK:           %[[UNPACK:.+]] = linalg.unpack %[[RELU]]
+// CHECK:           flow.return %[[UNPACK]] : tensor<2x64x64xf32>
+// CHECK-NOT:     flow.dispatch.region
+// CHECK:         util.return %[[RESULT]]
+util.func public @batch_mmt4d_relu_unpack(%lhs: tensor<2x8x128x8x1xf32>, %rhs: tensor<2x8x128x8x1xf32>) -> tensor<2x64x64xf32> {
+  %zero = arith.constant 0.000000e+00 : f32
+  %empty = tensor.empty() : tensor<2x8x8x8x8xf32>
+  %fill = linalg.fill ins(%zero : f32) outs(%empty : tensor<2x8x8x8x8xf32>) -> tensor<2x8x8x8x8xf32>
+  %mmt4d = linalg.batch_mmt4d ins(%lhs, %rhs : tensor<2x8x128x8x1xf32>, tensor<2x8x128x8x1xf32>) outs(%fill : tensor<2x8x8x8x8xf32>) -> tensor<2x8x8x8x8xf32>
+  %relu = linalg.generic {indexing_maps = [#map, #map], iterator_types = ["parallel", "parallel", "parallel", "parallel", "parallel"]} ins(%mmt4d : tensor<2x8x8x8x8xf32>) outs(%empty : tensor<2x8x8x8x8xf32>) {
+  ^bb0(%in: f32, %out: f32):
+    %max = arith.maximumf %in, %zero : f32
+    linalg.yield %max : f32
+  } -> tensor<2x8x8x8x8xf32>
+  %dest = tensor.empty() : tensor<2x64x64xf32>
+  %unpack = linalg.unpack %relu outer_dims_perm = [0, 1, 2] inner_dims_pos = [1, 2] inner_tiles = [8, 8] into %dest : tensor<2x8x8x8x8xf32> -> tensor<2x64x64xf32>
+  util.return %unpack : tensor<2x64x64xf32>
+}
+
+// -----
+
+// Chained elementwise epilogues, including a narrowing cast, join as well.
+#map = affine_map<(d0, d1, d2, d3) -> (d0, d1, d2, d3)>
+// CHECK-LABEL: util.func public @mmt4d_relu_truncf_unpack(
+// CHECK:         %[[RESULT:.+]] = flow.dispatch.region -> (tensor<64x64xbf16>)
+// CHECK:           %[[MMT4D:.+]] = linalg.mmt4d
+// CHECK:           %[[RELU:.+]] = linalg.generic
+// CHECK-SAME:        ins(%[[MMT4D]] : tensor<8x8x8x8xf32>)
+// CHECK:           %[[TRUNCF:.+]] = linalg.generic
+// CHECK-SAME:        ins(%[[RELU]] : tensor<8x8x8x8xf32>)
+// CHECK:             arith.truncf
+// CHECK:           %[[UNPACK:.+]] = linalg.unpack %[[TRUNCF]]
+// CHECK:           flow.return %[[UNPACK]] : tensor<64x64xbf16>
+// CHECK-NOT:     flow.dispatch.region
+// CHECK:         util.return %[[RESULT]]
+util.func public @mmt4d_relu_truncf_unpack(%lhs: tensor<8x128x8x1xf32>, %rhs: tensor<8x128x8x1xf32>) -> tensor<64x64xbf16> {
+  %zero = arith.constant 0.000000e+00 : f32
+  %empty = tensor.empty() : tensor<8x8x8x8xf32>
+  %fill = linalg.fill ins(%zero : f32) outs(%empty : tensor<8x8x8x8xf32>) -> tensor<8x8x8x8xf32>
+  %mmt4d = linalg.mmt4d ins(%lhs, %rhs : tensor<8x128x8x1xf32>, tensor<8x128x8x1xf32>) outs(%fill : tensor<8x8x8x8xf32>) -> tensor<8x8x8x8xf32>
+  %relu = linalg.generic {indexing_maps = [#map, #map], iterator_types = ["parallel", "parallel", "parallel", "parallel"]} ins(%mmt4d : tensor<8x8x8x8xf32>) outs(%empty : tensor<8x8x8x8xf32>) {
+  ^bb0(%in: f32, %out: f32):
+    %max = arith.maximumf %in, %zero : f32
+    linalg.yield %max : f32
+  } -> tensor<8x8x8x8xf32>
+  %narrow_empty = tensor.empty() : tensor<8x8x8x8xbf16>
+  %truncf = linalg.generic {indexing_maps = [#map, #map], iterator_types = ["parallel", "parallel", "parallel", "parallel"]} ins(%relu : tensor<8x8x8x8xf32>) outs(%narrow_empty : tensor<8x8x8x8xbf16>) {
+  ^bb0(%in: f32, %out: bf16):
+    %narrow = arith.truncf %in : f32 to bf16
+    linalg.yield %narrow : bf16
+  } -> tensor<8x8x8x8xbf16>
+  %dest = tensor.empty() : tensor<64x64xbf16>
+  %unpack = linalg.unpack %truncf outer_dims_perm = [0, 1] inner_dims_pos = [0, 1] inner_tiles = [8, 8] into %dest : tensor<8x8x8x8xbf16> -> tensor<64x64xbf16>
+  util.return %unpack : tensor<64x64xbf16>
+}
+
+// -----
+
+// For narrow N, materialization transposes the result by swapping the last
+// two entries of inner_dims_pos and outer_dims_perm. That unpack joins too.
+// CHECK-LABEL: util.func public @mmt4d_narrow_n_transposed_unpack(
+// CHECK:         %[[RESULT:.+]] = flow.dispatch.region -> (tensor<64x64xf32>)
+// CHECK:           %[[MMT4D:.+]] = linalg.mmt4d
+// CHECK:           %[[UNPACK:.+]] = linalg.unpack %[[MMT4D]] outer_dims_perm = [1, 0] inner_dims_pos = [1, 0]
+// CHECK:           flow.return %[[UNPACK]] : tensor<64x64xf32>
+// CHECK-NOT:     flow.dispatch.region
+// CHECK:         util.return %[[RESULT]]
+util.func public @mmt4d_narrow_n_transposed_unpack(%lhs: tensor<8x128x8x1xf32>, %rhs: tensor<8x128x8x1xf32>) -> tensor<64x64xf32> {
+  %zero = arith.constant 0.000000e+00 : f32
+  %empty = tensor.empty() : tensor<8x8x8x8xf32>
+  %fill = linalg.fill ins(%zero : f32) outs(%empty : tensor<8x8x8x8xf32>) -> tensor<8x8x8x8xf32>
+  %mmt4d = linalg.mmt4d ins(%lhs, %rhs : tensor<8x128x8x1xf32>, tensor<8x128x8x1xf32>) outs(%fill : tensor<8x8x8x8xf32>) -> tensor<8x8x8x8xf32>
+  %dest = tensor.empty() : tensor<64x64xf32>
+  %unpack = linalg.unpack %mmt4d outer_dims_perm = [1, 0] inner_dims_pos = [1, 0] inner_tiles = [8, 8] into %dest : tensor<8x8x8x8xf32> -> tensor<64x64xf32>
+  util.return %unpack : tensor<64x64xf32>
+}
+
+// -----
+
+// Permuting only the outer dimensions is not a layout that materialization
+// produces, so the unpack keeps its own dispatch. The epilogue still joins.
+#map = affine_map<(d0, d1, d2, d3) -> (d0, d1, d2, d3)>
+// CHECK-LABEL: util.func public @no_fuse_permuted_unpack(
+// CHECK:         %[[RELU_DISPATCH:.+]] = flow.dispatch.region -> (tensor<8x8x8x8xf32>)
+// CHECK:           %[[MMT4D:.+]] = linalg.mmt4d
+// CHECK:           %[[RELU:.+]] = linalg.generic
+// CHECK-SAME:        ins(%[[MMT4D]] : tensor<8x8x8x8xf32>)
+// CHECK:           flow.return %[[RELU]]
+// CHECK:         %[[RESULT:.+]] = flow.dispatch.region -> (tensor<64x64xf32>)
+// CHECK:           %[[UNPACK:.+]] = linalg.unpack %[[RELU_DISPATCH]] outer_dims_perm = [1, 0] inner_dims_pos = [0, 1]
+// CHECK:           flow.return %[[UNPACK]]
+// CHECK:         util.return %[[RESULT]]
+util.func public @no_fuse_permuted_unpack(%lhs: tensor<8x128x8x1xf32>, %rhs: tensor<8x128x8x1xf32>) -> tensor<64x64xf32> {
+  %zero = arith.constant 0.000000e+00 : f32
+  %empty = tensor.empty() : tensor<8x8x8x8xf32>
+  %fill = linalg.fill ins(%zero : f32) outs(%empty : tensor<8x8x8x8xf32>) -> tensor<8x8x8x8xf32>
+  %mmt4d = linalg.mmt4d ins(%lhs, %rhs : tensor<8x128x8x1xf32>, tensor<8x128x8x1xf32>) outs(%fill : tensor<8x8x8x8xf32>) -> tensor<8x8x8x8xf32>
+  %relu = linalg.generic {indexing_maps = [#map, #map], iterator_types = ["parallel", "parallel", "parallel", "parallel"]} ins(%mmt4d : tensor<8x8x8x8xf32>) outs(%empty : tensor<8x8x8x8xf32>) {
+  ^bb0(%in: f32, %out: f32):
+    %max = arith.maximumf %in, %zero : f32
+    linalg.yield %max : f32
+  } -> tensor<8x8x8x8xf32>
+  %dest = tensor.empty() : tensor<64x64xf32>
+  %unpack = linalg.unpack %relu outer_dims_perm = [1, 0] inner_dims_pos = [0, 1] inner_tiles = [8, 8] into %dest : tensor<8x8x8x8xf32> -> tensor<64x64xf32>
+  util.return %unpack : tensor<64x64xf32>
+}
+
+// -----
+
+// Dynamic inner tiles keep the unpack in its own dispatch.
+// CHECK-LABEL: util.func public @no_fuse_dynamic_tile_unpack(
+// CHECK:         %[[MMT4D_DISPATCH:.+]] = flow.dispatch.region -> (tensor<8x8x?x8xf32>{%{{.+}}})
+// CHECK:           %[[MMT4D:.+]] = linalg.mmt4d
+// CHECK:           flow.return %[[MMT4D]]
+// CHECK:         %[[RESULT:.+]] = flow.dispatch.region
+// CHECK:           %[[UNPACK:.+]] = linalg.unpack %[[MMT4D_DISPATCH]]
+// CHECK:           flow.return %[[UNPACK]]
+// CHECK:         util.return %[[RESULT]]
+util.func public @no_fuse_dynamic_tile_unpack(%lhs: tensor<8x128x?x1xf32>, %rhs: tensor<8x128x8x1xf32>, %tile: index) -> tensor<64x64xf32> {
+  %zero = arith.constant 0.000000e+00 : f32
+  %empty = tensor.empty(%tile) : tensor<8x8x?x8xf32>
+  %fill = linalg.fill ins(%zero : f32) outs(%empty : tensor<8x8x?x8xf32>) -> tensor<8x8x?x8xf32>
+  %mmt4d = linalg.mmt4d ins(%lhs, %rhs : tensor<8x128x?x1xf32>, tensor<8x128x8x1xf32>) outs(%fill : tensor<8x8x?x8xf32>) -> tensor<8x8x?x8xf32>
+  %dest = tensor.empty() : tensor<64x64xf32>
+  %unpack = linalg.unpack %mmt4d inner_dims_pos = [0, 1] inner_tiles = [%tile, 8] into %dest : tensor<8x8x?x8xf32> -> tensor<64x64xf32>
+  util.return %unpack : tensor<64x64xf32>
+}
+
+// -----
+
+// A transposing epilogue does not preserve the physical result layout, so it
+// does not join the mmt4d dispatch, and neither does the unpack.
+#map = affine_map<(d0, d1, d2, d3) -> (d0, d1, d2, d3)>
+// CHECK-LABEL: util.func public @no_fuse_transposed_epilogue(
+// CHECK:         %[[MMT4D_DISPATCH:.+]] = flow.dispatch.region
+// CHECK:           %[[MMT4D:.+]] = linalg.mmt4d
+// CHECK-NEXT:      flow.return %[[MMT4D]]
+// CHECK:         %[[RELU_DISPATCH:.+]] = flow.dispatch.region
+// CHECK:           %[[RELU:.+]] = linalg.generic
+// CHECK-SAME:        ins(%[[MMT4D_DISPATCH]] : tensor<8x8x8x8xf32>)
+// CHECK:           flow.return %[[RELU]]
+// CHECK:         %[[RESULT:.+]] = flow.dispatch.region -> (tensor<64x64xf32>)
+// CHECK:           %[[UNPACK:.+]] = linalg.unpack %[[RELU_DISPATCH]]
+// CHECK-NEXT:      flow.return %[[UNPACK]]
+util.func public @no_fuse_transposed_epilogue(%lhs: tensor<8x128x8x1xf32>, %rhs: tensor<8x128x8x1xf32>) -> tensor<64x64xf32> {
+  %zero = arith.constant 0.000000e+00 : f32
+  %empty = tensor.empty() : tensor<8x8x8x8xf32>
+  %fill = linalg.fill ins(%zero : f32) outs(%empty : tensor<8x8x8x8xf32>) -> tensor<8x8x8x8xf32>
+  %mmt4d = linalg.mmt4d ins(%lhs, %rhs : tensor<8x128x8x1xf32>, tensor<8x128x8x1xf32>) outs(%fill : tensor<8x8x8x8xf32>) -> tensor<8x8x8x8xf32>
+  %relu = linalg.generic {indexing_maps = [affine_map<(d0, d1, d2, d3) -> (d1, d0, d2, d3)>, #map], iterator_types = ["parallel", "parallel", "parallel", "parallel"]} ins(%mmt4d : tensor<8x8x8x8xf32>) outs(%empty : tensor<8x8x8x8xf32>) {
+  ^bb0(%in: f32, %out: f32):
+    %max = arith.maximumf %in, %zero : f32
+    linalg.yield %max : f32
+  } -> tensor<8x8x8x8xf32>
+  %dest = tensor.empty() : tensor<64x64xf32>
+  %unpack = linalg.unpack %relu outer_dims_perm = [0, 1] inner_dims_pos = [0, 1] inner_tiles = [8, 8] into %dest : tensor<8x8x8x8xf32> -> tensor<64x64xf32>
+  util.return %unpack : tensor<64x64xf32>
+}
+
+// -----
+
+// An epilogue that takes the mmt4d result as its init updates it in place, so
+// it does not join the mmt4d dispatch.
+#map = affine_map<(d0, d1, d2, d3) -> (d0, d1, d2, d3)>
+// CHECK-LABEL: util.func public @no_fuse_init_epilogue(
+// CHECK:         %[[MMT4D_DISPATCH:.+]] = flow.dispatch.region
+// CHECK:           %[[MMT4D:.+]] = linalg.mmt4d
+// CHECK-NEXT:      flow.return %[[MMT4D]]
+// CHECK:         %[[ADD_DISPATCH:.+]] = flow.dispatch.region
+// CHECK:           %[[ADD:.+]] = linalg.generic
+// CHECK-SAME:        outs(%[[MMT4D_DISPATCH]] : tensor<8x8x8x8xf32>)
+// CHECK:           flow.return %[[ADD]]
+// CHECK:         util.return %[[ADD_DISPATCH]]
+util.func public @no_fuse_init_epilogue(%lhs: tensor<8x128x8x1xf32>, %rhs: tensor<8x128x8x1xf32>, %other: tensor<8x8x8x8xf32>) -> tensor<8x8x8x8xf32> {
+  %zero = arith.constant 0.000000e+00 : f32
+  %empty = tensor.empty() : tensor<8x8x8x8xf32>
+  %fill = linalg.fill ins(%zero : f32) outs(%empty : tensor<8x8x8x8xf32>) -> tensor<8x8x8x8xf32>
+  %mmt4d = linalg.mmt4d ins(%lhs, %rhs : tensor<8x128x8x1xf32>, tensor<8x128x8x1xf32>) outs(%fill : tensor<8x8x8x8xf32>) -> tensor<8x8x8x8xf32>
+  %add = linalg.generic {indexing_maps = [#map, #map], iterator_types = ["parallel", "parallel", "parallel", "parallel"]} ins(%other : tensor<8x8x8x8xf32>) outs(%mmt4d : tensor<8x8x8x8xf32>) {
+  ^bb0(%in: f32, %out: f32):
+    %sum = arith.addf %in, %out : f32
+    linalg.yield %sum : f32
+  } -> tensor<8x8x8x8xf32>
+  util.return %add : tensor<8x8x8x8xf32>
+}
+
+// -----
+
+// The epilogue reads the mmt4d result through an identity map but writes a
+// permuted result, so it does not preserve the physical layout.
+#map = affine_map<(d0, d1, d2, d3) -> (d0, d1, d2, d3)>
+#perm = affine_map<(d0, d1, d2, d3) -> (d1, d0, d2, d3)>
+// CHECK-LABEL: util.func public @no_fuse_permuted_init_epilogue(
+// CHECK:         %[[MMT4D_DISPATCH:.+]] = flow.dispatch.region
+// CHECK:           %[[MMT4D:.+]] = linalg.mmt4d
+// CHECK-NEXT:      flow.return %[[MMT4D]]
+// CHECK:         %[[RELU_DISPATCH:.+]] = flow.dispatch.region
+// CHECK:           %[[RELU:.+]] = linalg.generic
+// CHECK-SAME:        ins(%[[MMT4D_DISPATCH]] : tensor<8x8x8x8xf32>)
+// CHECK:           flow.return %[[RELU]]
+// CHECK:         %[[UNPACK_DISPATCH:.+]] = flow.dispatch.region
+// CHECK:           %[[UNPACK:.+]] = linalg.unpack %[[RELU_DISPATCH]]
+// CHECK:           flow.return %[[UNPACK]]
+// CHECK:         util.return %[[UNPACK_DISPATCH]]
+util.func public @no_fuse_permuted_init_epilogue(%lhs: tensor<8x128x8x1xf32>, %rhs: tensor<8x128x8x1xf32>) -> tensor<64x64xf32> {
+  %zero = arith.constant 0.000000e+00 : f32
+  %empty = tensor.empty() : tensor<8x8x8x8xf32>
+  %fill = linalg.fill ins(%zero : f32) outs(%empty : tensor<8x8x8x8xf32>) -> tensor<8x8x8x8xf32>
+  %mmt4d = linalg.mmt4d ins(%lhs, %rhs : tensor<8x128x8x1xf32>, tensor<8x128x8x1xf32>) outs(%fill : tensor<8x8x8x8xf32>) -> tensor<8x8x8x8xf32>
+  %relu = linalg.generic {indexing_maps = [#map, #perm], iterator_types = ["parallel", "parallel", "parallel", "parallel"]} ins(%mmt4d : tensor<8x8x8x8xf32>) outs(%empty : tensor<8x8x8x8xf32>) {
+  ^bb0(%in: f32, %out: f32):
+    %max = arith.maximumf %in, %zero : f32
+    linalg.yield %max : f32
+  } -> tensor<8x8x8x8xf32>
+  %dest = tensor.empty() : tensor<64x64xf32>
+  %unpack = linalg.unpack %relu outer_dims_perm = [0, 1] inner_dims_pos = [0, 1] inner_tiles = [8, 8] into %dest : tensor<8x8x8x8xf32> -> tensor<64x64xf32>
+  util.return %unpack : tensor<64x64xf32>
+}
+
+// -----
+
+// The mmt4d result has a second use, so in the default mode nothing joins the
+// mmt4d dispatch, and the unclaimed unpack still fuses with its own consumer.
+// Aggressive fusion accepts multi-use roots and forms a single dispatch with two
+// results, which ends at the unpack.
+#map = affine_map<(d0, d1, d2, d3) -> (d0, d1, d2, d3)>
+// CHECK-LABEL: util.func public @no_fuse_multi_use_result(
+// CHECK:         %[[MMT4D_DISPATCH:.+]] = flow.dispatch.region
+// CHECK:           %[[MMT4D:.+]] = linalg.mmt4d
+// CHECK-NEXT:      flow.return %[[MMT4D]]
+// CHECK:         %[[RELU_DISPATCH:.+]] = flow.dispatch.region
+// CHECK:           %[[RELU:.+]] = linalg.generic
+// CHECK-SAME:        ins(%[[MMT4D_DISPATCH]] : tensor<8x8x8x8xf32>)
+// CHECK:           flow.return %[[RELU]]
+// CHECK:         %[[RESULT:.+]] = flow.dispatch.region -> (tensor<64x64xf32>)
+// CHECK:           %[[UNPACK:.+]] = linalg.unpack %[[RELU_DISPATCH]]
+// CHECK:           %[[NEG:.+]] = linalg.generic
+// CHECK-SAME:        ins(%[[UNPACK]] : tensor<64x64xf32>)
+// CHECK:           flow.return %[[NEG]]
+// CHECK:         util.return %[[RESULT]], %[[MMT4D_DISPATCH]]
+// AGGRESSIVE-LABEL: util.func public @no_fuse_multi_use_result(
+// AGGRESSIVE:         %[[DISPATCH:.+]]:2 = flow.dispatch.region -> (tensor<8x8x8x8xf32>, tensor<64x64xf32>)
+// AGGRESSIVE:           %[[MMT4D:.+]] = linalg.mmt4d
+// AGGRESSIVE:           %[[RELU:.+]] = linalg.generic
+// AGGRESSIVE-SAME:        ins(%[[MMT4D]] : tensor<8x8x8x8xf32>)
+// AGGRESSIVE:           %[[UNPACK:.+]] = linalg.unpack %[[RELU]]
+// AGGRESSIVE:           flow.return %[[MMT4D]], %[[UNPACK]]
+// AGGRESSIVE:         %[[NEG_DISPATCH:.+]] = flow.dispatch.region -> (tensor<64x64xf32>)
+// AGGRESSIVE:           linalg.generic
+// AGGRESSIVE-SAME:        ins(%[[DISPATCH]]#1 : tensor<64x64xf32>)
+// AGGRESSIVE:         util.return %[[NEG_DISPATCH]], %[[DISPATCH]]#0
+util.func public @no_fuse_multi_use_result(%lhs: tensor<8x128x8x1xf32>, %rhs: tensor<8x128x8x1xf32>) -> (tensor<64x64xf32>, tensor<8x8x8x8xf32>) {
+  %zero = arith.constant 0.000000e+00 : f32
+  %empty = tensor.empty() : tensor<8x8x8x8xf32>
+  %fill = linalg.fill ins(%zero : f32) outs(%empty : tensor<8x8x8x8xf32>) -> tensor<8x8x8x8xf32>
+  %mmt4d = linalg.mmt4d ins(%lhs, %rhs : tensor<8x128x8x1xf32>, tensor<8x128x8x1xf32>) outs(%fill : tensor<8x8x8x8xf32>) -> tensor<8x8x8x8xf32>
+  %relu = linalg.generic {indexing_maps = [#map, #map], iterator_types = ["parallel", "parallel", "parallel", "parallel"]} ins(%mmt4d : tensor<8x8x8x8xf32>) outs(%empty : tensor<8x8x8x8xf32>) {
+  ^bb0(%in: f32, %out: f32):
+    %max = arith.maximumf %in, %zero : f32
+    linalg.yield %max : f32
+  } -> tensor<8x8x8x8xf32>
+  %dest = tensor.empty() : tensor<64x64xf32>
+  %unpack = linalg.unpack %relu outer_dims_perm = [0, 1] inner_dims_pos = [0, 1] inner_tiles = [8, 8] into %dest : tensor<8x8x8x8xf32> -> tensor<64x64xf32>
+  %neg = linalg.generic {indexing_maps = [affine_map<(d0, d1) -> (d0, d1)>, affine_map<(d0, d1) -> (d0, d1)>], iterator_types = ["parallel", "parallel"]} ins(%unpack : tensor<64x64xf32>) outs(%dest : tensor<64x64xf32>) {
+  ^bb0(%in: f32, %out: f32):
+    %0 = arith.negf %in : f32
+    linalg.yield %0 : f32
+  } -> tensor<64x64xf32>
+  util.return %neg, %mmt4d : tensor<64x64xf32>, tensor<8x8x8x8xf32>
+}
+
+// -----
+
+// Between chained contractions, the pack that relayouts the result unpack for
+// the next mmt4d joins the first dispatch and ends it.
+#map = affine_map<(d0, d1, d2, d3) -> (d0, d1, d2, d3)>
+// CHECK-LABEL: util.func public @mmt4d_unpack_pack_mmt4d(
+// CHECK-SAME:    %[[RHS2:[a-zA-Z0-9]+]]: tensor<8x64x8x1xf32>
+// CHECK:         %[[PACK_DISPATCH:.+]] = flow.dispatch.region -> (tensor<8x64x8x1xf32>)
+// CHECK:           %[[MMT4D:.+]] = linalg.mmt4d
+// CHECK:           %[[RELU:.+]] = linalg.generic
+// CHECK-SAME:        ins(%[[MMT4D]] : tensor<8x8x8x8xf32>)
+// CHECK:           %[[UNPACK:.+]] = linalg.unpack %[[RELU]]
+// CHECK:           %[[PACK:.+]] = linalg.pack %[[UNPACK]]
+// CHECK:           flow.return %[[PACK]]
+// CHECK:         %[[RESULT:.+]] = flow.dispatch.region -> (tensor<8x8x8x8xf32>)
+// CHECK:           linalg.mmt4d ins(%[[PACK_DISPATCH]], %[[RHS2]]
+// CHECK:         util.return %[[RESULT]]
+util.func public @mmt4d_unpack_pack_mmt4d(%lhs: tensor<8x128x8x1xf32>, %rhs: tensor<8x128x8x1xf32>, %rhs2: tensor<8x64x8x1xf32>) -> tensor<8x8x8x8xf32> {
+  %zero = arith.constant 0.000000e+00 : f32
+  %empty = tensor.empty() : tensor<8x8x8x8xf32>
+  %fill = linalg.fill ins(%zero : f32) outs(%empty : tensor<8x8x8x8xf32>) -> tensor<8x8x8x8xf32>
+  %mmt4d = linalg.mmt4d ins(%lhs, %rhs : tensor<8x128x8x1xf32>, tensor<8x128x8x1xf32>) outs(%fill : tensor<8x8x8x8xf32>) -> tensor<8x8x8x8xf32>
+  %relu = linalg.generic {indexing_maps = [#map, #map], iterator_types = ["parallel", "parallel", "parallel", "parallel"]} ins(%mmt4d : tensor<8x8x8x8xf32>) outs(%empty : tensor<8x8x8x8xf32>) {
+  ^bb0(%in: f32, %out: f32):
+    %max = arith.maximumf %in, %zero : f32
+    linalg.yield %max : f32
+  } -> tensor<8x8x8x8xf32>
+  %dest = tensor.empty() : tensor<64x64xf32>
+  %unpack = linalg.unpack %relu outer_dims_perm = [0, 1] inner_dims_pos = [0, 1] inner_tiles = [8, 8] into %dest : tensor<8x8x8x8xf32> -> tensor<64x64xf32>
+  %lhs2_dest = tensor.empty() : tensor<8x64x8x1xf32>
+  %pack = linalg.pack %unpack outer_dims_perm = [0, 1] inner_dims_pos = [0, 1] inner_tiles = [8, 1] into %lhs2_dest : tensor<64x64xf32> -> tensor<8x64x8x1xf32>
+  %fill2 = linalg.fill ins(%zero : f32) outs(%empty : tensor<8x8x8x8xf32>) -> tensor<8x8x8x8xf32>
+  %mmt4d2 = linalg.mmt4d ins(%pack, %rhs2 : tensor<8x64x8x1xf32>, tensor<8x64x8x1xf32>) outs(%fill2 : tensor<8x8x8x8xf32>) -> tensor<8x8x8x8xf32>
+  util.return %mmt4d2 : tensor<8x8x8x8xf32>
+}