Skip to content

Commit

Permalink
Optimize greedy rewrite passes (openqasm#298)
Browse files Browse the repository at this point in the history
Optimize a number of greedy rewrite passes.
- QUIRAngleConversionPass: Goes from about ~7.5s for 100x100 to 0.9s on
my machine
  • Loading branch information
taalexander committed Mar 15, 2024
1 parent b49d19f commit dc5252f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
4 changes: 3 additions & 1 deletion lib/Dialect/QUIR/IR/QUIRInterfaces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ std::set<uint32_t>
interfaces_impl::getQubitsBetweenOperations(mlir::Operation *first,
mlir::Operation *second) {
std::set<uint32_t> 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();
Expand Down
11 changes: 9 additions & 2 deletions lib/Dialect/QUIR/Transforms/AngleConversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,15 @@ void QUIRAngleConversionPass::runOnOperation() {
RewritePatternSet patterns(&getContext());
patterns.add<AngleConversion>(&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();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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.

0 comments on commit dc5252f

Please sign in to comment.