diff --git a/lib/Dialect/QUIR/IR/QUIRInterfaces.cpp b/lib/Dialect/QUIR/IR/QUIRInterfaces.cpp index da324ebf3..0d9ee20fc 100644 --- a/lib/Dialect/QUIR/IR/QUIRInterfaces.cpp +++ b/lib/Dialect/QUIR/IR/QUIRInterfaces.cpp @@ -105,7 +105,9 @@ std::set interfaces_impl::getQubitsBetweenOperations(mlir::Operation *first, mlir::Operation *second) { std::set operatedQubits; - if (!first->isBeforeInBlock(second)) + // Don't use isBeforeInBlock if the op order is invalid as + // this is O(n) in the worst case. + if (first->getBlock()->isOpOrderValid() && !first->isBeforeInBlock(second)) return operatedQubits; Operation *curOp = first->getNextNode(); diff --git a/lib/Dialect/QUIR/Transforms/AngleConversion.cpp b/lib/Dialect/QUIR/Transforms/AngleConversion.cpp index 2e41d2cec..755cc86b3 100644 --- a/lib/Dialect/QUIR/Transforms/AngleConversion.cpp +++ b/lib/Dialect/QUIR/Transforms/AngleConversion.cpp @@ -84,8 +84,15 @@ void QUIRAngleConversionPass::runOnOperation() { RewritePatternSet patterns(&getContext()); patterns.add(&getContext(), functionOps); - if (failed( - applyPatternsAndFoldGreedily(getOperation(), std::move(patterns)))) { + mlir::GreedyRewriteConfig config; + // Disable to improve performance + config.enableRegionSimplification = false; + config.strictMode = mlir::GreedyRewriteStrictness::ExistingOps; + // Each operation can only be modified once so limit + config.maxIterations = 1; + + if (failed(applyPatternsAndFoldGreedily(getOperation(), std::move(patterns), + config))) { ; // TODO why would this call to applyPatternsAndFoldGreedily fail? // signalPassFailure(); } diff --git a/releasenotes/notes/optimize-greedy-rewrites-b042d4e824642f9a.yaml b/releasenotes/notes/optimize-greedy-rewrites-b042d4e824642f9a.yaml new file mode 100644 index 000000000..e67b236bb --- /dev/null +++ b/releasenotes/notes/optimize-greedy-rewrites-b042d4e824642f9a.yaml @@ -0,0 +1,8 @@ +--- +features: + - | + The performance of the QUIRAngleConversionPass has been improved by a factor of + ~8-10x by optimizing the greedy rewrite behaviour. + - | + Parallelize control flow improves by ~8-10x due to a quadratic scaling factor + that was uncovered.