Skip to content

[Polly] Use separate DT/LI/SE for outlined subfn. NFC. #102460

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

Merged
merged 4 commits into from
Aug 10, 2024

Conversation

Meinersbur
Copy link
Member

@Meinersbur Meinersbur commented Aug 8, 2024

DominatorTree, LoopInfo, and ScalarEvolution are function-level analyses that expect to be called only on instructions and basic blocks of the function they were original created for. When Polly outlined a parallel loop body into a separate function, it reused the same analyses seemed to work until new checks to be added in #101198.

This patch creates new analyses for the subfunctions. GenDT, GenLI, and GenSE now refer to the analyses of the current region of code. Outside of an outlined function, they refer to the same analysis as used for the SCoP, but are substituted within an outlined function.

Additionally to the cross-function queries of DT/LI/SE, we must not create SCEVs that refer to a mix of expressions for old and generated values. Currently, SCEVs themselves do not "remember" which ScalarEvolution analysis they were created for, but mixing them is just as unexpected as using DT/LI across function boundaries. Some calculations may depend on pointer comparisons, expecting equal SCEV objects to be equal due to SE's uniqueing of SCEV objects, while with a second SE there is a second set of uniqued SCEVs. Hence SCEVLoopAddRecRewriter was combined into ScopExpander. SCEVLoopAddRecRewriter only replaced induction variables but left SCEVUnknowns to reference the old function. SCEVParameterRewriter would have done so but its job was effectively superseded by ScopExpander, and now also SCEVLoopAddRecRewriter. Some issues persist put marked with a FIXME in the code. Changing them would possibly cause this patch to be not NFC anymore.

This patch was also tested with #101198 applied. That is, Polly's regression tests only, more issues may be discovered on other codes.

Copy link
Contributor

@aengelke aengelke left a comment

Choose a reason for hiding this comment

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

Looks good in general, but I don't really feel qualified to review Polly code.


public:
/// Change the function that code is emitted into.
void switchGeneratedFunc(Function *GenFn, DominatorTree *GenDT,
Copy link
Contributor

Choose a reason for hiding this comment

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

GenFn unused? Also in IslExprBuilder.h

Copy link
Member Author

@Meinersbur Meinersbur Aug 8, 2024

Choose a reason for hiding this comment

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

This was intentional since the function is called switchGeneratedFunc. I had stored it in a private field of the class to use it in assertions. Unfortunately, Clang emits a warning for unused fields in non-assert builds and we have some bots with -Werror enabled, so I had to remove the asserts.

I added some assertions to the function itself that use the GenFn.


// FIXME: This emits a SCEV for GenSE (since GenLRepl will refer to the
// induction variable of a generated loop), so we should not use SCEVVisitor
// with it. Howver, it still contains references to the SCoP region.
Copy link
Collaborator

Choose a reason for hiding this comment

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

References, where? NewOps shouldn't contain references to the scop, and GenLRepl shouldn't contain references to the scop, so how does Evaluated contain references to the scop? There aren't any other arguments to evaluateAtIteration.

Copy link
Member Author

@Meinersbur Meinersbur Aug 9, 2024

Choose a reason for hiding this comment

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

GenLRepl contains them. The LoopMap/LTS is constructed somewhere without considering uses in a subfunction. If removed, it fails even in some test cases that do not do outlining, some GlobalMap/BBMap still needs to be applied. Probably because some function adds a map entry for value from GenFn instead of updating an existing map entry. See comments for all the exceptions in BlockGenerator::getNewValue. I tried to clean that up before once.

It does not make a difference in practice since SCEVs don't remember which SE they belong to. visitUnknown has another FIXME for this problem. I don't want to spend too much time hunting behind all the FIXMEs since I think @aengelke wants to land #101198.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Creating derived SCEVs based on a base SCEV from another ScalarEvolution could theoretically cause issues: some places in ScalarEvolution use pointer equality to determine structural equality. But it it doesn't seem to be causing issues, it's probably okay.

@Meinersbur Meinersbur marked this pull request as ready for review August 9, 2024 09:31
Copy link

github-actions bot commented Aug 9, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

Copy link
Collaborator

@efriedma-quic efriedma-quic left a comment

Choose a reason for hiding this comment

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

LGTM

@Meinersbur Meinersbur merged commit 22c77f2 into main Aug 10, 2024
8 checks passed
@Meinersbur Meinersbur deleted the users/meinersbur/polly_codegen_gen-dt branch August 10, 2024 12:25
kartcq added a commit to kartcq/llvm-project that referenced this pull request Dec 18, 2024
The patch llvm#102460 already implements separate DT/LI/SE for
parallel sub function. Crashes have been reported while
region generator tries using oringinal function's DT while
creating new parallel sub function due to checks in llvm#101198.
This patch aims at fixing those cases by switching the DT/LI
while generating parallel function using Region Generator.

Fixes llvm#117877
kartcq added a commit that referenced this pull request Jan 8, 2025
The patch #102460 already implements separate DT/LI/SE for parallel sub
function. Crashes have been reported while region generator tries using
oringinal function's DT while creating new parallel sub function due to
checks in #101198. This patch aims at fixing those cases by switching
the DT/LI while generating parallel function using Region Generator.

Fixes #117877
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.

3 participants