Index: mlir/lib/Dialect/Linalg/Transforms/Hoisting.cpp =================================================================== --- mlir/lib/Dialect/Linalg/Transforms/Hoisting.cpp +++ mlir/lib/Dialect/Linalg/Transforms/Hoisting.cpp @@ -391,6 +391,13 @@ } void mlir::linalg::hoistRedundantVectorTransfers(FuncOp func) { + // First move loop invariant ops outside of their loop. This needs to be done + // before as we cannot move ops without interputing the function walk. + func.walk([&](LoopLikeOpInterface loopLike) { + if (failed(moveLoopInvariantCode(loopLike))) + llvm_unreachable("Unexpected failure to move invariant code out of loop"); + }); + bool changed = true; while (changed) { changed = false; @@ -407,11 +414,6 @@ if (!loop) return WalkResult::advance(); - if (failed(moveLoopInvariantCode( - cast(loop.getOperation())))) - llvm_unreachable( - "Unexpected failure to move invariant code out of loop"); - LLVM_DEBUG(DBGS() << "Candidate read: " << *transferRead.getOperation() << "\n");