This is an archive of the discontinued LLVM Phabricator instance.

[pipelines] Require GlobalsAA after sanitizers
ClosedPublic

Authored by vitalybuka on Sep 8 2022, 3:46 PM.

Details

Summary

Restore GlobalsAA if sanitizers inserted at early optimize callback.
The analysis can be useful for the following FunctionPassManager.

Diff Detail

Event Timeline

vitalybuka created this revision.Sep 8 2022, 3:46 PM
Herald added a project: Restricted Project. · View Herald TranscriptSep 8 2022, 3:46 PM
vitalybuka requested review of this revision.Sep 8 2022, 3:46 PM
Herald added a project: Restricted Project. · View Herald TranscriptSep 8 2022, 3:46 PM
vitalybuka edited the summary of this revision. (Show Details)Sep 8 2022, 3:49 PM
vitalybuka added reviewers: asbirlea, aeubanks.

I think the question is whether or not people will use OptimizerEarlyEPCallbacks to add optimization passes (e.g. some sort of loop vectorization) which may want an up-to-date GlobalsAA, or if the passes will end up helping make GlobalsAA more precise via more simplification.
Is this in preparation for actually using OptimizerEarlyEPCallbacks? Note that this won't help with having GlobalsAA updated if you add sanitizer passes here that invalidate GlobalsAA since RecomputeGlobalsAAPass only has an effect if GlobalsAA hasn't been explicitly invalidated (RecomputeGlobalsAAPass::run).

I think the question is whether or not people will use OptimizerEarlyEPCallbacks to add optimization passes (e.g. some sort of loop vectorization) which may want an up-to-date GlobalsAA,

Yes, we need to choose either:

  1. Callback which needs GlobalsAA will also insert RecomputeGlobalsAAPass (but how many times if we have multiple callbacks?)
  2. Callback which corrupt GlobalAA will insert RecomputeGlobalsAAPass. (but how many times if we have multiple callbacks?)
  3. Run RecomputeGlobalsAAPass before and after callbacks, but then it's unnecessary if we have no callbacks relevant to analysis.

This patch is to go no. 1.

Is this in preparation for actually using OptimizerEarlyEPCallbacks?

Yes.

Note that this won't help with having GlobalsAA updated if you add sanitizer passes here that invalidate GlobalsAA since RecomputeGlobalsAAPass only has an effect if GlobalsAA hasn't been explicitly invalidated (RecomputeGlobalsAAPass::run).

I assumed that D133394 invalidates (it's even in description). Then RecomputeGlobalsAAPass will recalculated it, but if RecomputeGlobalsAAPass is missing we will proceed without GlobalsAA.
But now I see that abandon<> removes it forever.

What would be the right solution for that?
I see the following:

  1. Just for clang, I revert D133394 and just make sanitizer callbacks to insert RecomputeGlobalsAAPass after sanitizers.
  2. Extract RecomputeGlobalsAAPass::run into utility function and call it from sanitizers instead of abandon<>

RecomputeGlobalsAAPass is essentially equivalent to "if GlobalsAA exists in the cache, abandon it and recompute", so it's similar to

if (GlobalsAA in analysis cache) {
 MPM.addPass(RequireAnalysisPass<GlobalsAA, Module>());
 MPM.addPass(InvalidateAnalysisPass<GlobalsAA>());
}

since RecomputeGlobalsAAPass doesn't work if the analysis doesn't exist in the cache, if we've added a sanitizer that may abandon GlobalsAA, we can just add a
MPM.addPass(RequireAnalysisPass<GlobalsAA, Module>()); in BackendUtil.cpp/SanitizersCallback which will fully compute GlobalsAA from scratch

that would be better than this patch because OptimizerEarlyEPCallbacks still has an up-to-date GlobalsAA, but if we add sanitizers we'll still have an up-to-date GlobalsAA right after

RecomputeGlobalsAAPass is essentially equivalent to "if GlobalsAA exists in the cache, abandon it and recompute", so it's similar to

if (GlobalsAA in analysis cache) {
 MPM.addPass(RequireAnalysisPass<GlobalsAA, Module>());
 MPM.addPass(InvalidateAnalysisPass<GlobalsAA>());
}

since RecomputeGlobalsAAPass doesn't work if the analysis doesn't exist in the cache, if we've added a sanitizer that may abandon GlobalsAA, we can just add a
MPM.addPass(RequireAnalysisPass<GlobalsAA, Module>()); in BackendUtil.cpp/SanitizersCallback which will fully compute GlobalsAA from scratch

that would be better than this patch because OptimizerEarlyEPCallbacks still has an up-to-date GlobalsAA, but if we add sanitizers we'll still have an up-to-date GlobalsAA right after

Wouldn't be better to do so on PassBuilderPipelines like this, so we can cover any other future abandoning hooks?

Wouldn't be better to do so on PassBuilderPipelines like this, so we can cover any other future abandoning hooks?

We don't do that for all callbacks, I don't see why OptimizerEarlyEPCallbacks should be special. It's not typical to add passes that add globals. That's why it feels like it belongs wherever we add sanitizers.

Wouldn't be better to do so on PassBuilderPipelines like this, so we can cover any other future abandoning hooks?

We don't do that for all callbacks, I don't see why OptimizerEarlyEPCallbacks should be special. It's not typical to add passes that add globals. That's why it feels like it belongs wherever we add sanitizers.

It's fine to me either way. However we don't do RecomputeGlobalsAAPass before other callback either.

Wouldn't be better to do so on PassBuilderPipelines like this, so we can cover any other future abandoning hooks?

We don't do that for all callbacks, I don't see why OptimizerEarlyEPCallbacks should be special. It's not typical to add passes that add globals. That's why it feels like it belongs wherever we add sanitizers.

It's fine to me either way. However we don't do RecomputeGlobalsAAPass before other callback either.

The existing RecomputeGlobalsAAPass is specifically because we want the module optimization pipeline to have as refined AA as possible.
I'd still prefer adding this wherever we add sanitizers.

Herald added a project: Restricted Project. · View Herald TranscriptSep 14 2022, 1:04 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript
vitalybuka edited the summary of this revision. (Show Details)Sep 14 2022, 1:11 PM
vitalybuka edited the summary of this revision. (Show Details)

remove typo

aeubanks accepted this revision.Sep 14 2022, 1:15 PM

lg with comments addressed

title needs update

clang/lib/CodeGen/BackendUtil.cpp
443

unrelated?

This revision is now accepted and ready to land.Sep 14 2022, 1:15 PM

fix capture

vitalybuka retitled this revision from [pipelines] RecomputeGlobalsAAPass after OptimizerEarlyEPCallbacks to [pipelines] Require GlobalsAA after sanitizers.Sep 14 2022, 1:32 PM
vitalybuka edited the summary of this revision. (Show Details)
vitalybuka marked an inline comment as done.
vitalybuka added inline comments.
clang/lib/CodeGen/BackendUtil.cpp
443

yes, already fixed.

This revision was landed with ongoing or failed builds.Sep 14 2022, 1:34 PM
This revision was automatically updated to reflect the committed changes.
vitalybuka marked an inline comment as done.