This is an archive of the discontinued LLVM Phabricator instance.

Assign correct edge weights to unwind destinations when lowering invoke statement.
ClosedPublic

Authored by congh on Oct 1 2015, 10:36 AM.

Details

Summary

When lowering invoke statement, all unwind destinations are directly added as successors of call site block, and the weight of those new edges are not assigned properly. Actually, default weight 16 are used for those edges. This patch calculates the proper edge weights for those edges when collecting all unwind destinations.

Diff Detail

Repository
rL LLVM

Event Timeline

congh updated this revision to Diff 36168.Oct 1 2015, 10:36 AM
congh retitled this revision from to Assign correct edge weights to unwind destinations when lowering invoke statement..
congh updated this object.
congh added reviewers: hans, davidxl.
congh added a subscriber: llvm-commits.
hans added a reviewer: rnk.Oct 1 2015, 1:21 PM

I'm not familiar with this code. Reid is probably a better reviewer here.

congh added a comment.Oct 1 2015, 1:25 PM
In D13354#258017, @hans wrote:

I'm not familiar with this code. Reid is probably a better reviewer here.

Thanks for the reviewer recommendation, Hans!

Reid, could you please take a look at this patch?

rnk edited edge metadata.Oct 12 2015, 12:51 PM

Sorry, I missed this the first time around. Looking...

rnk added a comment.Oct 12 2015, 1:17 PM

This seems like the right approach. My first thought was to add logic similar to findUnwindDestinations in BPI::getEdgeWeight, but after thinking more I like this is better.

lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
1203 ↗(On Diff #36168)

This is just code that runs on every iteration of the while loop. We don't need a lambda, just put it before the end of the loop. You'll have to create a NewEHPadBB local variable and assign it to EHPadBB, but that seems OK to me.

test/CodeGen/X86/catchpad-weight.ll
1 ↗(On Diff #36168)

I think the IR for this C++ code would make a better test case:

struct A {};
struct B {};
struct C {};
struct HasDtor {
  ~HasDtor();
};
void may_throw();
int main() {
  HasDtor o;
  try {
    may_throw();
  } catch (A) {
  } catch (B) {
  } catch (C) {
  }
}

Then the invoke for may_throw has five total successors: the normal edge, the three catchpads, and the cleanup.

You can get the catchpad IR like this:

$ clang -c --target=x86_64-windows-msvc t.cpp -S -emit-llvm -o - -fexceptions -O2
congh added inline comments.Oct 12 2015, 3:42 PM
lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
1203 ↗(On Diff #36168)

I have removed the lambda by putting it to the end of the loop (the else case should escape from this part).

test/CodeGen/X86/catchpad-weight.ll
1 ↗(On Diff #36168)

Yes, this test case is more complete. I have replaced the old test case with this one. Thanks!

congh updated this revision to Diff 37192.Oct 12 2015, 3:44 PM
congh edited edge metadata.

Update the patch according to Reid's comment. Adjust some test cases after merging with the up-to-date trunk version.

rnk accepted this revision.Oct 12 2015, 3:53 PM
rnk edited edge metadata.

lgtm

Correcting the weights seems to have fixed the strange, non-source order block placement. Thanks!

This revision is now accepted and ready to land.Oct 12 2015, 3:53 PM
congh added a comment.Oct 12 2015, 4:03 PM

Thanks for the review, Reid!

This revision was automatically updated to reflect the committed changes.