Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert Loop Metadata to Asserts to help Loop rotation #227

Open
wants to merge 2 commits into
base: aie-public
Choose a base branch
from

Conversation

F-Stuckmann
Copy link
Collaborator

@F-Stuckmann F-Stuckmann commented Oct 28, 2024

note the first commit will be removed once the ( #225 ) is merged

Please note: I am still going through QoR to make sure I am not introducing any functional errors.

@F-Stuckmann
Copy link
Collaborator Author

Only add assumption if the assumption at the first iteration can be guaranteed to be loop invariant.

@F-Stuckmann F-Stuckmann force-pushed the stuckmann.loop.metadata.asserts branch from 3909dd4 to 33c04f6 Compare December 3, 2024 10:59
@F-Stuckmann
Copy link
Collaborator Author

rebased to aie-public

@F-Stuckmann F-Stuckmann force-pushed the stuckmann.loop.metadata.asserts branch from 060a2a8 to d244f5c Compare December 4, 2024 13:50
@F-Stuckmann F-Stuckmann force-pushed the stuckmann.loop.metadata.asserts branch 2 times, most recently from 1e4b876 to e8f1b47 Compare December 5, 2024 12:51
@andcarminati
Copy link
Collaborator

Maybe you can fix the CI failure by running your test under x86?

@F-Stuckmann F-Stuckmann force-pushed the stuckmann.loop.metadata.asserts branch from e8f1b47 to f9835c9 Compare December 10, 2024 09:49
@F-Stuckmann
Copy link
Collaborator Author

I moved the pass between LICM and loop rotate, so that the loop invariance check is simplified and performed by LICM. This behavior is demonstrated by unittests.

Additionally, the assumption is inserted into the Preheader, so that the loop header is not evaluated to true and converted into an endless loop.

; NOTE: Example file for converting loop iter count to assumptions in the Loop
;
; RUN: opt -S -passes='loop-iter-count-assumptions' -verify-each < %s | FileCheck %s
; RUN: opt -S -passes='loop-mssa(licm,loop-iter-count-assumptions)' -verify-each < %s | FileCheck %s --check-prefix=LICM
Copy link
Collaborator

Choose a reason for hiding this comment

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

That's a really huge file now. I think the first RUN line isn't all that useful anymore, because your pass is now inserted after LICM in the pass pipeline. Testing it independently outside of its expected conditions doesn't show too much.

Maybe it would make sense to only keep:

  • opt -S -passes='loop-mssa(licm,loop-iter-count-assumptions)' -verify-each < %s | FileCheck %s --check-prefix=ASSUME
  • opt -S -passes='default<O2>' --enable-loop-iter-count-assumptions=1 -start-before='licm' -stop-after='loop-rotate' -verify-each -o - < %s | FileCheck %s --check-prefix=ROTATE

Nit: I would even only test loop-mssa(licm,loop-iter-count-assumptions) in that file to keep it concise and easy to read. Then we can have a separate "integration test" which runs from licm to loop-rotate with one or two cases. But that's a personal preference at this point :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I wanted to have the end to end test, so that non-obvious errors get shown immediately, i.e. endless loops.

But sure, now that i am through the testing and know what the expected results are, I can remove it. The integration test is only interesting for edge cases, which would duplicates large parts of the unittest, which is the original reason I had them in the same file.

// return LHS
Value *LHS = expandValueAtZeroIteration(LoopCmpInstr.getOperand(0), SE,
Expander, &LoopCmpInstr);
LHS = getLoopInvariant(LHS, DT, &CurrentLoop);
Copy link
Collaborator

Choose a reason for hiding this comment

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

See my comment here https://github.com/Xilinx/llvm-aie/pull/227/files#r1869145688

I think it is a bit confusing that expandValueAtZeroIteration potentially returns something that isn't the value at iteration 0. This LHS = getLoopInvariant(LHS, DT, &CurrentLoop); should be inlined into expandValueAtZeroIteration. If the latter cannot be sure that the returned value is exactly the one at iteration 0, it should return nullptr.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

That is a more concise way of looking at expandValueAtZeroIteration

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I updated it accordingly.

@F-Stuckmann F-Stuckmann force-pushed the stuckmann.loop.metadata.asserts branch 2 times, most recently from f2d9afe to abe2846 Compare December 10, 2024 13:45
@@ -163,6 +163,9 @@ void AIEToolChain::addClangTargetOptions(

// Extend the max limit of the search depth in BasicAA
CC1Args.append({"-mllvm", "-basic-aa-max-lookup-search-depth=10"});

// enable Loop Iteration Count Assumptions
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: Enable.

@@ -163,6 +163,9 @@ void AIEToolChain::addClangTargetOptions(

// Extend the max limit of the search depth in BasicAA
CC1Args.append({"-mllvm", "-basic-aa-max-lookup-search-depth=10"});

// enable Loop Iteration Count Assumptions
CC1Args.append({"-mllvm", "-enable-loop-iter-count-assumptions=1"});
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: it is better to use boolean because it is the correct type for the flag.

@F-Stuckmann F-Stuckmann force-pushed the stuckmann.loop.metadata.asserts branch from abe2846 to 0c6fa61 Compare December 11, 2024 14:44
@F-Stuckmann
Copy link
Collaborator Author

F-Stuckmann commented Dec 11, 2024

  • Assumption Insertion: Evaluate at MinIterCount to not introduce loop guards after loop unrolling
  • Assumption Insertion: Evaluate at Iteration 0, so that EQ/NE and variable stepsizes can be processed. Hint: EQ/NE only get this assumption inserted

I also only lazily insert the Iteration 0 assumption so that the IR changes are minimal.

QoR improves by 1.14% and program memory by 0.73%, when this pass is enabled vs with the builtin_assumes

@F-Stuckmann F-Stuckmann force-pushed the stuckmann.loop.metadata.asserts branch from 0c6fa61 to 6bf41ef Compare December 11, 2024 16:46
@F-Stuckmann F-Stuckmann force-pushed the stuckmann.loop.metadata.asserts branch from 6bf41ef to d8d9dba Compare December 11, 2024 17:04
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.

4 participants