This is an archive of the discontinued LLVM Phabricator instance.

Skip unreachable blocks for CFIInstrInserter verify
ClosedPublic

Authored by petarj on May 3 2018, 11:08 AM.

Details

Summary

Iterate only through reachable blocks. This finetunes r330706 and
resolves build issue reported by Craig Topper.

Diff Detail

Repository
rL LLVM

Event Timeline

petarj created this revision.May 3 2018, 11:08 AM
craig.topper added inline comments.May 3 2018, 11:15 AM
lib/CodeGen/CFIInstrInserter.cpp
295

pred_empty()?

295

Can't you just check CurrMBB == MF.front()? This applies to several other places in this pass.

This fix fixes the original case, but its easy to break it again

target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

; Function Attrs: noinline nounwind optnone uwtable
define hidden void @foo() #0 {
bb:
  br label %bb1

bb1:                                              ; preds = %bb3, %bb
  %tmp = icmp ne i32 0, 0
  br i1 %tmp, label %bb2, label %bb3

bb2:                                              ; preds = %bb1
  br label %bb4

bb4:                                              ; preds = %bb2
  br label %bb3

bb3:                                              ; preds = %bb4, %bb1
  br label %bb1
}

attributes #0 = { noinline nounwind optnone uwtable "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2,+x87" }
petarj added a comment.EditedMay 3 2018, 3:51 PM

This fix fixes the original case, but its easy to break it again

target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

; Function Attrs: noinline nounwind optnone uwtable
define hidden void @foo() #0 {
bb:
  br label %bb1

bb1:                                              ; preds = %bb3, %bb
  %tmp = icmp ne i32 0, 0
  br i1 %tmp, label %bb2, label %bb3

bb2:                                              ; preds = %bb1
  br label %bb4

bb4:                                              ; preds = %bb2
  br label %bb3

bb3:                                              ; preds = %bb4, %bb1
  br label %bb1
}

attributes #0 = { noinline nounwind optnone uwtable "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2,+x87" }

Does this example come from a real test case? The issue here is that bb2 will not have predecessors after X86 DAG->DAG Instruction Selection, and this way we reach bb3 from either:

bb0->bb1->bb3 or
bb2->bb4->bb3.

What should be a correct offset for bb3 in this case?

It's not real. But bb4 is only reachable from bb2. I just wedged an empty block in the path from bb2 to bb3 in my previous case. So neither bb2 or bb4 are actually reachable from the start of the function.

petarj added a comment.May 3 2018, 4:11 PM

It's not real. But bb4 is only reachable from bb2. I just wedged an empty block in the path from bb2 to bb3 in my previous case. So neither bb2 or bb4 are actually reachable from the start of the function.

I made a typo (bb3 vs bb4). I wanted to say:

and this way we reach bb3 from either:

bb0->bb1->bb3 or
bb2->bb4->bb3.

What should be a correct offset for bb3 in this case?

petarj updated this revision to Diff 145115.May 3 2018, 4:30 PM
petarj marked 2 inline comments as done.

Updated the patch according to the comments.

Here is a C program rather than hacked IR that still crashes with this patch. I don't know anything about CFI so I don't know what the right answer is.

int __attribute((__always_inline__)) bar() {
  return 0;
}

void foo() {
  if (bar() != 0) {
    if (bar() != 0) {
    }
  }
}
petarj updated this revision to Diff 145232.May 4 2018, 11:33 AM
petarj retitled this revision from Skip blocks with no predecessors for CFIInstrInserter verify to Skip unreachable blocks for CFIInstrInserter verify.
petarj edited the summary of this revision. (Show Details)

Thanks Craig for the test example. The issues were related to unreachable blocks that exist with -O0, this patch now skips all of them and hopefully it will resolve issues you are seeing.

thegameg added inline comments.May 4 2018, 2:24 PM
lib/CodeGen/CFIInstrInserter.cpp
298

How about range for on depth_first(&MF)?

craig.topper added inline comments.May 4 2018, 2:58 PM
lib/CodeGen/CFIInstrInserter.cpp
297

Do you need an external set? You aren't accessing it are you?

petarj updated this revision to Diff 145317.May 4 2018, 4:13 PM
petarj marked an inline comment as done.

Update the patch per comments.

lib/CodeGen/CFIInstrInserter.cpp
297

No, in this version of the patch I do not need it. I had some asserts in local copy.
Removing it.

This revision is now accepted and ready to land.May 6 2018, 6:48 PM
violetav accepted this revision.May 7 2018, 4:47 AM

LGTM

petarj closed this revision.May 7 2018, 4:56 AM

Committed in r331628. Closing the issue.

https://reviews.llvm.org/rL331628