Skip to content

Add more patterns to loop unroll pass#2596

Closed
annagrin wants to merge 20 commits intoNVIDIA:mainfrom
annagrin:loop-unroll-additional-opts
Closed

Add more patterns to loop unroll pass#2596
annagrin wants to merge 20 commits intoNVIDIA:mainfrom
annagrin:loop-unroll-additional-opts

Conversation

@annagrin
Copy link
Collaborator

@annagrin annagrin commented Feb 6, 2025

Description

Add more patterns to loop unroll pass

To be able to optimize away loops with boundaries that are computed from array storage, other loops, conditions, and more, we need to run a bunch of optimizations together in the loop unroll pass.

Added following optimizations to cc-loop-unroll:

  • LoopOpt pattern from cc-loop-normalize (to keep normalizing loops in loop unrolling iterations)
  • RewriteIf from lower-to-cfg (to const-prop if statements)
  • SimplifyWritesAnalysis from write-after-write-elimination (remove multiple writes to the same location on the stack)
  • simplifyRegions (to remove dead code)
  • AllocaPattern from lift-array-alloc (to lift constant arrays, helps const prop)

Added tests.

Signed-off-by: Anna Gringauze <agringauze@nvidia.com>
Signed-off-by: Anna Gringauze <agringauze@nvidia.com>
…op-unroll-additional-opts

Signed-off-by: Anna Gringauze <agringauze@nvidia.com>
Signed-off-by: Anna Gringauze <agringauze@nvidia.com>
Signed-off-by: Anna Gringauze <agringauze@nvidia.com>
Signed-off-by: Anna Gringauze <agringauze@nvidia.com>
Signed-off-by: Anna Gringauze <agringauze@nvidia.com>
Signed-off-by: Anna Gringauze <agringauze@nvidia.com>
Signed-off-by: Anna Gringauze <agringauze@nvidia.com>
github-actions bot pushed a commit that referenced this pull request Feb 7, 2025
@github-actions
Copy link

github-actions bot commented Feb 7, 2025

CUDA Quantum Docs Bot: A preview of the documentation can be found here.

Signed-off-by: Anna Gringauze <agringauze@nvidia.com>
…op-unroll-additional-opts

Signed-off-by: Anna Gringauze <agringauze@nvidia.com>
I, Anna Gringauze <agringauze@nvidia.com>, hereby add my Signed-off-by to this commit: f08502e

Signed-off-by: Anna Gringauze <agringauze@nvidia.com>
Signed-off-by: Anna Gringauze <agringauze@nvidia.com>
…op-unroll-additional-opts

Signed-off-by: Anna Gringauze <agringauze@nvidia.com>
@github-actions
Copy link

github-actions bot commented Feb 8, 2025

CUDA Quantum Docs Bot: A preview of the documentation can be found here.

github-actions bot pushed a commit that referenced this pull request Feb 8, 2025
I, Anna Gringauze <agringauze@nvidia.com>, hereby add my Signed-off-by to this commit: 3d3597d

Signed-off-by: Anna Gringauze <agringauze@nvidia.com>
…grin/cuda-quantum into loop-unroll-additional-opts

Signed-off-by: Anna Gringauze <agringauze@nvidia.com>
…op-unroll-additional-opts

Signed-off-by: Anna Gringauze <agringauze@nvidia.com>
@github-actions
Copy link

github-actions bot commented Feb 8, 2025

CUDA Quantum Docs Bot: A preview of the documentation can be found here.

github-actions bot pushed a commit that referenced this pull request Feb 8, 2025
Signed-off-by: Anna Gringauze <agringauze@nvidia.com>
@github-actions
Copy link

github-actions bot commented Feb 8, 2025

CUDA Quantum Docs Bot: A preview of the documentation can be found here.

github-actions bot pushed a commit that referenced this pull request Feb 8, 2025
pm.addNestedPass<func::FuncOp>(cudaq::opt::createClassicalMemToReg());
pm.addNestedPass<func::FuncOp>(createCanonicalizerPass());
cudaq::opt::LoopNormalizeOptions lno{allowClosedInterval, allowBreak};
pm.addNestedPass<func::FuncOp>(cudaq::opt::createLoopNormalize(lno));
Copy link
Collaborator

Choose a reason for hiding this comment

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

Removing loop normalization doesn't look correct.

Copy link
Collaborator Author

@annagrin annagrin Feb 10, 2025

Choose a reason for hiding this comment

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

I added loop normalization pattern into the LoopUnroll pass itself so it is no longer needed here. I've run into a scenario where loop normalization was needed during the loop unroll fix point for it to fully succeed.

Copy link
Collaborator Author

@annagrin annagrin Feb 10, 2025

Choose a reason for hiding this comment

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

I am thinking - given that I also needed to add something similar to the lift-array-alloc pass (in #2601) - instead of modifying the cc-loop-unroll and lift-array-alloc passes, would it be better to create a new classical-optimization-pipeline that would run the following:

classical-optimization-pipeline:

  • memtoreg=quantum=0
  • classical-optimization
  • cse
  • classical-optimization
    (Note: after we update the llvm version we could run cse inside the fixpoint of classical-optimization, so we would not need to run it twice here and would be able to compile more code)

Where classical-optimizations pass runs the following patterns as a part of single fix point (same as the cc-loop-unroll pass in current PR, but run them on code that does not contain loops as well):

  • write-after-write-elimination
  • lift-array-alloc
  • canonicalize
  • loop-normalize patterns
  • simplifyRegions
  • original loop unrolling patterns

We would use classical-optimization-pipeline instead of the initial set of opts in quantum config files:
func.func(const-prop-complex,canonicalize,cse,lift-array-alloc)

And later again after the following passes:
apply-op-specialization,aggressive-early-inlining,expand-measurements

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

So, as an example, the new pipeline for Ionq would look like:

"func.func(classical-optimization-pipeline), globalize-array-values,func.func(state-prep),unitary-synthesis,canonicalize, apply-op-specialization,aggressive-early-inlining,expand-measurements, func.func(classical-optimization-pipeline), decomposition{enable-patterns=U3ToRotations}, func.func(lower-to-cfg,canonicalize,multicontrol-decomposition), ionq-gate-set-mapping"

@annagrin
Copy link
Collaborator Author

Replaced by #2605

@annagrin annagrin closed this Feb 11, 2025
github-actions bot pushed a commit that referenced this pull request Feb 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants