This is an archive of the discontinued LLVM Phabricator instance.

[NFC][LoopPredication] Extract guard parsing to GuardUtils
ClosedPublic

Authored by aleksandr.popov on Aug 7 2023, 5:35 AM.

Details

Summary

Extract logic of parsing guards and widenable branches in order to reuse
it in the GuardWidening

Diff Detail

Event Timeline

aleksandr.popov created this revision.Aug 7 2023, 5:35 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 7 2023, 5:35 AM
Herald added a subscriber: hiraditya. · View Herald Transcript
aleksandr.popov requested review of this revision.Aug 7 2023, 5:35 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 7 2023, 5:35 AM
anna added inline comments.Aug 8 2023, 9:25 AM
llvm/lib/Analysis/GuardUtils.cpp
121

Pls leave default value for SmallVector (i.e. SmallVector<Value *> Worklist = {Condition};). If we happen to have more than 4 conditions, we end up resizing.

123

You can delay adding Condition into Visited and make this entire loop as:

do {
    Value *Check = Worklist.pop_back_val();
     if (!Visited.insert(Check).second)
       continue;
    Value *LHS, *RHS;
    if (match(Check, m_And(m_Value(LHS), m_Value(RHS)))) {
        Worklist.push_back(LHS);
        Worklist.push_back(RHS);
      continue;
    }
    if (!Callback(Check))
      break;
  } while (!Worklist.empty());
145

This always returns true. Why do you need a templated function for parseCondition in this patch? I'd suggest the non-templated version and once you add support for parsing it in GuardWidening, we can see if we need a templated version or not.

llvm/lib/Transforms/Scalar/LoopPredication.cpp
778

Pls land this and the added debug statement in the test separately (can land without review), since it is unrelated to patch.

aleksandr.popov added inline comments.Aug 9 2023, 3:30 AM
llvm/lib/Analysis/GuardUtils.cpp
123

Do you mind if I do that refactoring in the next separate patch?
I've intended just to extract logic from LoopPredication::collectChecks and move it here straightforwardly.

145

Yep, returning true was change in advance. I'll turn callback's type into void.
The reason why I need templated function for parseCondition - is an intent to separate condition parsing and checks collecting.
So in the parseCondition I just want to parse the condition while in the parseWidenableGuard I want to make a decision whether to add check in the resulting list or not (in case if it's widenable-condition)

llvm/lib/Transforms/Scalar/LoopPredication.cpp
778

Ok, will do it in the separate patch, thanks!

aleksandr.popov added inline comments.Aug 9 2023, 4:33 AM
llvm/lib/Analysis/GuardUtils.cpp
123
llvm/lib/Transforms/Scalar/LoopPredication.cpp
778
anna accepted this revision.Aug 9 2023, 10:23 AM
anna added inline comments.
llvm/lib/Analysis/GuardUtils.cpp
145

had a chat with Aleksandr offline. The need for the template arises because a later patch will use the same logic but with a different Callback function (which will be modified to return a bool).

This revision is now accepted and ready to land.Aug 9 2023, 10:23 AM