Skip to content

Performance: Avoid rewrite work in passes when no patterns can match - #5315

Open
taalexander wants to merge 3 commits into
NVIDIA:mainfrom
taalexander:perf/skip-root-free-greedy-passes
Open

Performance: Avoid rewrite work in passes when no patterns can match#5315
taalexander wants to merge 3 commits into
NVIDIA:mainfrom
taalexander:perf/skip-root-free-greedy-passes

Conversation

@taalexander

Copy link
Copy Markdown
Collaborator

Six passes currently invoke MLIR's greedy rewrite driver
even when the IR contains none of the operation types their patterns can
match. On large modules, that turns a pass with no relevant work into a full IR
traversal.

This change gives those passes a small typed check. It walks the IR
once, stops at the first relevant operation, and returns early when there is no
possible pattern root. If a matching op is present, the pass runs exactly as it did
before.

I've applied this to:

  • LiftArrayAlloc
  • GlobalizeArrayValues
  • PyFrontendCleanup
  • GetConcreteMatrix
  • ConstantPropagation
  • StatePreparation

Performance

Benchmarking results a fixed number of operations:

Gates Baseline pass time New pass time Time saved Reduction Speedup Baseline process wall New process wall
500,000 5.7525 s 1.7238 s 4.0287 s 70.0% 3.34x 8.73 s 4.42 s
1,000,000 12.8370 s 2.9031 s 9.9339 s 77.4% 4.42x 18.83 s 8.03 s
1,500,000 20.4164 s 7.5225 s 12.8939 s 63.2% 2.71x 31.29 s 20.50 s

At 1.5 million gates, every individual pass spent substantially less time:

Pass Baseline New Time saved Reduction Speedup
LiftArrayAlloc 2.1607 s 0.5827 s 1.5780 s 73.0% 3.71x
GlobalizeArrayValues 2.3258 s 0.5546 s 1.7712 s 76.2% 4.19x
PyFrontendCleanup 2.1621 s 0.4843 s 1.6778 s 77.6% 4.46x
GetConcreteMatrix 2.5007 s 0.7070 s 1.7937 s 71.7% 3.54x
ConstantPropagation 2.6656 s 0.7523 s 1.9133 s 71.8% 3.54x
StatePreparation 3.2388 s 1.1060 s 2.1328 s 65.9% 2.93x

Avoid invoking six narrow greedy rewrite passes when their typed pattern roots are absent. A private variadic helper performs one interrupting operation walk, while root-present execution retains the complete existing rewrite path.

This reduces root-free MLIR pass time by 66% to 78% across 100k to 1.5M gate inputs in the initial benchmark matrix. Baseline and candidate outputs were byte-identical in every measured root-free case and existing root-present comparisons.

Verified with the focused transform and AST lit tests, all 41 optimizer unit tests, full hardware and emulation pipeline comparisons, clang-format, and git diff --check.

Signed-off-by: Thomas Alexander <talexander@nvidia.com>
@github-actions

github-actions Bot commented Aug 28, 2026

Copy link
Copy Markdown

CI Summary (push) — ✅ passed

Run #33203169644 · ✅ 7 · ⏩ 7 · ❌ 0 · ⛔ 0

Top-level jobs (14)
Job Result
binaries ⏩ skipped
build_and_test ✅ success
changes ✅ success
config_devdeps ✅ success
config_source_build ⏩ skipped
config_wheeldeps ✅ success
devdeps ✅ success
docker_image ⏩ skipped
gen_code_coverage ⏩ skipped
metadata ✅ success
python_metapackages ⏩ skipped
python_wheels ⏩ skipped
source_build ⏩ skipped
wheeldeps ✅ success
⏩ Skipped jobs (7) — intentionally skipped on PR builds; run on merge_group / workflow_dispatch
Job
binaries
config_source_build
docker_image
gen_code_coverage
python_metapackages
python_wheels
source_build
All sub-jobs (43) — every matrix leg, with links
Job Status Link
Build and test (amd64, gcc12, openmpi) / Dev environment (Debug) ✅ success view
Build and test (amd64, gcc12, openmpi) / Dev environment (Python) ✅ success view
Build and test (amd64, llvm, openmpi) / Dev environment (Debug) ✅ success view
Build and test (amd64, llvm, openmpi) / Dev environment (Python) ✅ success view
Build and test (arm64, llvm, openmpi) / Dev environment (Debug) ✅ success view
Build and test (arm64, llvm, openmpi) / Dev environment (Python) ✅ success view
CI Summary ❔ in_progress view
Check for stable CUDA-Q changes ✅ success view
Configure build (devdeps) ✅ success view
Configure build (source_build) ⏩ skipped view
Configure build (wheeldeps) ✅ success view
Create CUDA Quantum installer ⏩ skipped view
Create Docker images ⏩ skipped view
Create Python metapackages ⏩ skipped view
Create Python wheels ⏩ skipped view
Gen code coverage ⏩ skipped view
Load dependencies (amd64, gcc12) / Caching ✅ success view
Load dependencies (amd64, gcc12) / Finalize ✅ success view
Load dependencies (amd64, gcc12) / Metadata ✅ success view
Load dependencies (amd64, llvm) / Caching ✅ success view
Load dependencies (amd64, llvm) / Finalize ✅ success view
Load dependencies (amd64, llvm) / Metadata ✅ success view
Load dependencies (arm64, gcc12) / Caching ✅ success view
Load dependencies (arm64, gcc12) / Finalize ✅ success view
Load dependencies (arm64, gcc12) / Metadata ✅ success view
Load dependencies (arm64, llvm) / Caching ✅ success view
Load dependencies (arm64, llvm) / Finalize ✅ success view
Load dependencies (arm64, llvm) / Metadata ✅ success view
Load source build cache ⏩ skipped view
Load wheel dependencies (amd64, 12.6) / Caching ✅ success view
Load wheel dependencies (amd64, 12.6) / Finalize ✅ success view
Load wheel dependencies (amd64, 12.6) / Metadata ✅ success view
Load wheel dependencies (amd64, 13.0) / Caching ✅ success view
Load wheel dependencies (amd64, 13.0) / Finalize ✅ success view
Load wheel dependencies (amd64, 13.0) / Metadata ✅ success view
Load wheel dependencies (arm64, 12.6) / Caching ✅ success view
Load wheel dependencies (arm64, 12.6) / Finalize ✅ success view
Load wheel dependencies (arm64, 12.6) / Metadata ✅ success view
Load wheel dependencies (arm64, 13.0) / Caching ✅ success view
Load wheel dependencies (arm64, 13.0) / Finalize ✅ success view
Load wheel dependencies (arm64, 13.0) / Metadata ✅ success view
Prepare cache clean-up ❔ in_progress view
Retrieve PR info ✅ success view
✅ Required checks (6/6) — declared in .github/required-checks.yml for push
Required check Status Link
Build and test (amd64, llvm, openmpi) / Dev environment (Debug) ✅ success view
Build and test (amd64, llvm, openmpi) / Dev environment (Python) ✅ success view
Build and test (arm64, llvm, openmpi) / Dev environment (Debug) ✅ success view
Build and test (arm64, llvm, openmpi) / Dev environment (Python) ✅ success view
Build and test (amd64, gcc12, openmpi) / Dev environment (Debug) ✅ success view
Build and test (amd64, gcc12, openmpi) / Dev environment (Python) ✅ success view

Signed-off-by: Thomas Alexander <talexander@nvidia.com>
@github-actions github-actions Bot added the documentation Improvements or additions to documentation label Aug 28, 2026
@taalexander taalexander added the agentic-autoresearch Tracking automated development by agentic systems label Aug 28, 2026

@atgeller atgeller left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One nit, otherwise LGTM.

Comment thread cudaq/lib/Optimizer/Transforms/PassDetails.h Outdated
Describe scaling checks, rewrite-driver choices, avoiding repeated IR work, and timing and tracing. Rename the operation-presence helper for clarity.

Signed-off-by: Thomas Alexander <talexander@nvidia.com>
LLVM_DEBUG(llvm::dbgs() << "Before constant prop:\n" << func << '\n');

if (failed(
applyPatternsGreedily(func.getOperation(), std::move(patterns)))) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the description, I was surprised to see another pre-traversal being added instead of converting these passes to partial conversion passes in favor of greedy rewrites. Is there a reason the latter was not done?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some of these passes do use the greedy rewrite behaviour so it was not drop in. Another approach would be something like I did in #5318. I'll take another look.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agentic-autoresearch Tracking automated development by agentic systems core compiler documentation Improvements or additions to documentation performance

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants