This is an archive of the discontinued LLVM Phabricator instance.

[RegionInfo] Fix dangling references created by moving RegionInfo objects
ClosedPublic

Authored by philip.pfaffe on Apr 5 2017, 11:08 AM.

Details

Summary

Region objects capture the address of the creating RegionInfo instance. Because the RegionInfo class is movable, moving a RegionInfo object creates dangling references. This patch fixes these references by walking the Regions post-move, and updating references to the new parent.

Diff Detail

Repository
rL LLVM

Event Timeline

philip.pfaffe created this revision.Apr 5 2017, 11:08 AM
grosser accepted this revision.Apr 5 2017, 12:04 PM
This revision is now accepted and ready to land.Apr 5 2017, 12:04 PM
Simplify the patch slightly. NFC.
Meinersbur added inline comments.Apr 6 2017, 5:03 AM
include/llvm/Analysis/RegionInfo.h
896–903 ↗(On Diff #94349)

Would it be feasible to remove the move-ctors and instead force users to allocate RegionInfo on the heap?

Meinersbur accepted this revision.Apr 12 2017, 3:25 PM
Meinersbur added inline comments.
include/llvm/Analysis/RegionInfo.h
896–903 ↗(On Diff #94349)

As LoopInfo is also a movable type, this might be required by the new pass manager.

Looking at the implementation of AnalysisManager, analysis are stored as

typedef std::list<std::pair<AnalysisKey *, std::unique_ptr<ResultConceptT>>>
    AnalysisResultListT;

that is, it only has ownership, but does not require it to be movable.

On the other side, the unique_ptr is created using

std::unique_ptr<
    AnalysisResultConcept<IRUnitT, PreservedAnalysesT, InvalidatorT>>
run(IRUnitT &IR, AnalysisManager<IRUnitT, ExtraArgTs...> &AM,
    ExtraArgTs... ExtraArgs) override {
  return llvm::make_unique<ResultModelT>(Pass.run(IR, AM, ExtraArgs...));
}

in PassManagerInternals.h. It is effectively used by bool registerPass(PassBuilderT &&PassBuilder).
Passing an analysis result object by value, only to wrap it into a unique_ptr afterwards seems wasteful. The comment for the AnalysisPassModel class mentions that it is only a wrapper. It should be possible to implement an AnalysisPassConcept without the wrapper, but there is no existing implementation doing that.

I don't expect you to explore this unknown territory. Could you maybe add a comment to the source mentioning why the analysis result needs to be moved so it will be considered when someone is going to improve the situation?

philip.pfaffe added inline comments.Apr 17 2017, 2:15 PM
include/llvm/Analysis/RegionInfo.h
896–903 ↗(On Diff #94349)

The fundamental difference between LoopInfo and RegionInfo in this regard is that Loop does not hold any references to LoopInfo. It might be worthwhile to move the RegionInfo and Region API more towards LoopInfo, meaning to remove the captured reference from the Region-class and pass it in the relevant functions instead.

The comment for the AnalysisPassModel class mentions that it is only a wrapper.

That's not how the PassManager is supposed to be used, and that won't change in the future. The explicit design decision is that Analyses and Passes do not depend on PassManager internals, such as deriving from an interface- or base-class. That is precisely the idea of concept-based programming.

grosser added inline comments.Apr 17 2017, 7:25 PM
include/llvm/Analysis/RegionInfo.h
896–903 ↗(On Diff #94349)

AFAIR the getRegionInfo() function is not used very often, so if we can get rid of it entirely that would be nice.

Now, I believe this is an orthogonal issue. Meaning, the patch as written solves the direct correctness issue and seems consequently worth committing. If we can then also simplify the interface, this is certainly something I would be glad about.

This revision was automatically updated to reflect the committed changes.