[Mlir-commits] [mlir] [MLIR][SCF] Add an API to fuse consumer to a producer within scf loop (PR #88712)

Oleksandr Alex Zinenko llvmlistbot at llvm.org
Thu Apr 18 04:58:18 PDT 2024


================
@@ -1100,6 +1101,451 @@ mlir::scf::tileConsumerAndFuseProducersUsingSCF(
                                    replacements};
 }
 
+//===----------------------------------------------------------------------===//
+// tileAndFuseConsumerUsingSCF implementation.
+//===----------------------------------------------------------------------===//
+
+/// We traverse through the use-def chain of the tensor.insert_slice op through
+/// the containing scf.for to fetch the first untiled consumer.  We also return
+/// the operand number of the consumer and the result number of the scf.for
+/// being consumed.
+static Operation *
+getUntiledConsumerFromSliceDestSCFFor(tensor::InsertSliceOp candidateSliceOp,
+                                      unsigned &operandNumber,
+                                      unsigned &resultNumber) {
+  // Step 1. Fetch the corresponding output.
+  Value sliceResult = candidateSliceOp.getResult();
+  Value::user_range users = sliceResult.getUsers();
+  auto yieldOp = cast<scf::YieldOp>(*users.begin());
+  for (Value operand : yieldOp->getOperands()) {
+    if (operand == sliceResult) {
+      break;
+    }
+    resultNumber++;
+  }
+  Value resultingValue =
+      candidateSliceOp->getParentOp()->getResult(resultNumber);
+
+  // Step 2. Get users.
+  Operation *untiledConsumer;
+  for (Operation *user : resultingValue.getUsers()) {
+    // TODO(avarma): Address the case where the consumer op itself can return
+    //               more than one result.
+    for (Value operand : user->getOperands()) {
+      if (operand == resultingValue) {
+        untiledConsumer = user;
+        break;
+      }
----------------
ftynse wrote:

Same as above, iterate over _uses_.

https://github.com/llvm/llvm-project/pull/88712


More information about the Mlir-commits mailing list