This is an archive of the discontinued LLVM Phabricator instance.

[LoopRotate][WIP] Check code-motion safety before hoisting
Needs ReviewPublic

Authored by RithikSharma on Aug 30 2020, 10:25 AM.

Details

Summary

This patch adds checking of code-motion safety before hoisting, Loop rotate considers moving memory instructions as unsafe we used CodeMoverUtils to check code-motion safety of memory instructions and included other pass independent checks, It hoists more memory instructions resulting in less duplication.

LoopRotate:

define void @test(i32 %n, i32* %A) {
  %cond1 = icmp slt i32 0, %n
  %0 = load i32, i32* %A
  %1 = add nsw i32 %0, 5
  br i1 %cond1, label %body.lr.ph, label %exit

body.lr.ph:                                       ; preds = %entry
  br label %body

body:                                             ; preds = %body.lr.ph, %latch
  %i2 = phi i32 [ 0, %body.lr.ph ], [ %i.next, %latch ]
  br label %latch

latch:                                            ; preds = %body
  %i.next = add nsw i32 %i2, 1
  %cond = icmp slt i32 %i.next, %n
  %2 = load i32, i32* %A
  %3 = add nsw i32 %2, 5
  br i1 %cond, label %body, label %for.header.exit_crit_edge

for.header.exit_crit_edge:                        ; preds = %latch
  br label %exit

exit:                                             ; preds = %for.header.exit_crit_edge, %entry
  ret void
}

CodeMoverUtils:

define void @test(i32 %n, i32* %A) {
entry:
  %cond1 = icmp slt i32 0, %n
  %0 = load i32, i32* %A, align 4
  %1 = add nsw i32 %0, 5
  br i1 %cond1, label %body.lr.ph, label %exit

body.lr.ph:                                       ; preds = %entry
  br label %body

body:                                             ; preds = %body.lr.ph, %latch
  %i2 = phi i32 [ 0, %body.lr.ph ], [ %i.next, %latch ]
  br label %latch

latch:                                            ; preds = %body
  %i.next = add nsw i32 %i2, 1
  %cond = icmp slt i32 %i.next, %n
  br i1 %cond, label %body, label %for.header.exit_crit_edge

for.header.exit_crit_edge:                        ; preds = %latch
  br label %exit

exit:                                             ; preds = %for.header.exit_crit_edge, %entry
  ret void
}

This shows more hoisting of instructions and less duplication after using code-motion checks before hoisting.
PS: Some test case are failing. I'm working to fix them.

Diff Detail

Event Timeline

RithikSharma created this revision.Aug 30 2020, 10:25 AM
RithikSharma requested review of this revision.Aug 30 2020, 10:25 AM
bmahjour added inline comments.
llvm/lib/Transforms/Utils/CodeMoverUtils.cpp
431

This makes it easy to misuse the interface. If someone forgets to pass PDT, we skip checking for control flow equivalence and may return "safe" when it's not the case. I think we should pass a separate boolean to make the user explicitly state whether they care about control-flow equivalence or not.

llvm/lib/Transforms/Utils/LoopRotationUtils.cpp
81

AA is a member variable, why does it need to be passed as an argument too?

84

why pass AA as argument when it's available as a member?

RithikSharma added inline comments.Sep 1 2020, 10:41 AM
llvm/lib/Transforms/Utils/CodeMoverUtils.cpp
431

The basic assumption was that the user will guarantee control flow equivalence as there are not many loop passes that have PDT.

llvm/lib/Transforms/Utils/LoopRotationUtils.cpp
81

Thanks, yes we don't require it as an argument, I will update this asap.

84

Same for this, I will update this soon.

bmahjour added inline comments.Sep 1 2020, 12:08 PM
llvm/lib/Transforms/Utils/CodeMoverUtils.cpp
431

I understand, but the user might pass nullptr (because they don't have PDT) without checking for control flow (by mistake). I would consider this an interface bug to assume that the user checked for control-flow equivalence in the absence of a valid PDT. Instead of assuming it, we should make the user be explicit about it.

RithikSharma added inline comments.Sep 1 2020, 12:59 PM
llvm/lib/Transforms/Utils/CodeMoverUtils.cpp
431

This makes sense to me, instead of assuming about control flow equivalence the pass would tell us more about it. I will update the patch with the required changes asap.