This is an archive of the discontinued LLVM Phabricator instance.

[CGP] Limit distance between overflow math and cmp
ClosedPublic

Authored by samparker on Mar 6 2019, 7:03 AM.

Details

Summary

Inserting an overflowing arithmetic intrinsic can increase register pressure by producing two values at a point where only one is needed, while the second use maybe several blocks away. This increase in pressure could be more detrimental on performance than rematerialising one of the original instructions.

So, check that the arithmetic and compare instructions are no further apart than their immediate successor/predecessor. covers the existing test cases.

Diff Detail

Repository
rL LLVM

Event Timeline

samparker created this revision.Mar 6 2019, 7:03 AM

This will need to be rebased to account for D58995 / rL355512,
and we may want to do some preliminary testing to confirm that compile-time is not impacted (cc @tejohnson ).
I'm not opposed to the patch, but I don't know enough about DominatorTree mechanics to approve, so someone else will need to review this patch too.

samparker updated this revision to Diff 189510.Mar 6 2019, 7:21 AM

Okay, rebased.

Hello. I think the idea of this sounds sensible, to limit things if the instruction are too far apart, I'm just not sure of using domtree's to do that.

lib/CodeGen/CodeGenPrepare.cpp
1196 ↗(On Diff #189510)

I'm not sure this will reliably calculate a meaningful distance for a lot of trees. And I'm not sure that the children of a node are necessarily always in the same deterministic order. The node level may be better, but the distance in the domtree is probably not be the best measure.

Maybe, because the Distance is small, can we just search back through preds to find the block? We already know which one dominates, and most times they will be in the same block. We would have to be careful of compile time, but if we don't have to search more that Distance, and Distance is fairly small, hopefully it will work OK.

1199 ↗(On Diff #189510)

2 is a magic number? Is it worth making a cl:opt for it, so it at least has a name?

samparker marked an inline comment as done.Mar 8 2019, 2:44 AM
samparker added inline comments.
lib/CodeGen/CodeGenPrepare.cpp
1196 ↗(On Diff #189510)

This sounds perfectly reasonable to me. I'll do a BFS and limit the depth using the cl::opt.

spatel added a comment.Mar 8 2019, 5:46 AM

Hello. I think the idea of this sounds sensible, to limit things if the instruction are too far apart, I'm just not sure of using domtree's to do that.

This is a hack, but given that this code has been shown to be compile-time sensitive: what if we limit the transform to cases where the instructions are in the same block or a direct predecessor block? IIUC, that would prevent the transform for the motivating cases here, but not affect any of the existing tests.

spatel added a comment.Mar 8 2019, 7:52 AM

Independent of this patch, but may affect how we proceed:
https://bugs.llvm.org/show_bug.cgi?id=41004

samparker updated this revision to Diff 190052.Mar 11 2019, 2:27 AM
samparker edited the summary of this revision. (Show Details)

Thanks Sanjay, a simple search works for us too. I've updated to check the immediate successors.

dmgreen added inline comments.Mar 11 2019, 3:13 AM
lib/CodeGen/CodeGenPrepare.cpp
1193 ↗(On Diff #190052)

Can you use std::find (or llvm::find)?

samparker updated this revision to Diff 190065.Mar 11 2019, 3:59 AM

Cheers Dave, that's far nicer.

dmgreen accepted this revision.Mar 11 2019, 5:14 AM

Looks OK to me.

This revision is now accepted and ready to land.Mar 11 2019, 5:14 AM
This revision was automatically updated to reflect the committed changes.
Herald added a project: Restricted Project. · View Herald TranscriptMar 11 2019, 6:18 AM