This is an archive of the discontinued LLVM Phabricator instance.

[SimplifyCFG] threshold for folding branches with common destination
ClosedPublic

Authored by jingyue on Sep 29 2014, 10:50 AM.

Details

Summary

This patch adds a threshold that controls the number of bonus instructions
allowed for folding branches with common destination. The original code allows
at most one bonus instruction. With this patch, users can customize the
threshold to allow multiple bonus instructions. The default threshold is still
1, so that the code behaves the same as before when users do not specify this
threshold.

The motivation of this change is that tuning this threshold significantly (up
to 25%) improves the performance of some CUDA programs in our internal code
base. In general, branch instructions are very expensive for GPU programs.
Therefore, it is sometimes worth trading more arithmetic computation for a more
straightened control flow. Here's a reduced example:

__global__ void foo(int a, int b, int c, int d, int e, int n,
                    const int *input, int *output) {
  int sum = 0;
  for (int i = 0; i < n; ++i)
    sum += (((i ^ a) > b) && (((i | c ) ^ d) > e)) ? 0 : input[i];
  *output = sum;
}

The select statement in the loop body translates to two branch instructions "if
((i ^ a) > b)" and "if (((i | c) ^ d) > e)" which share a common destination.
With the default threshold, SimplifyCFG is unable to fold them, because
computing the condition of the second branch "(i | c) ^ d > e" requires two
bonus instructions. With the threshold increased, SimplifyCFG can fold the two
branches so that the loop body contains only one branch, making the code
conceptually look like:

sum += (((i ^ a) > b) & (((i | c ) ^ d) > e)) ? 0 : input[i];

Increasing the threshold significantly improves the performance of this
particular example. In the configuration where both conditions are guaranteed
to be true, increasing the threshold from 1 to 2 improves the performance by
18.24%. Even in the configuration where the first condition is false and the
second condition is true, which favors shortcuts, increasing the threshold from
1 to 2 still improves the performance by 4.35%.

We are still looking for a good threshold and maybe a better cost model than
just counting the number of bonus instructions. However, according to the above
numbers, we think it is at least worth adding a threshold to enable more
experiments and tuning. Let me know what you think. Thanks!

Diff Detail

Event Timeline

jingyue updated this revision to Diff 14181.Sep 29 2014, 10:50 AM
jingyue retitled this revision from to [SimplifyCFG] threshold for folding branches with common destination.
jingyue updated this object.
jingyue edited the test plan for this revision. (Show Details)
jingyue added reviewers: nadav, resistor, eliben, meheff.
jingyue added a subscriber: Unknown Object (MLST).
jingyue updated this object.Sep 29 2014, 10:52 AM
resistor edited edge metadata.Sep 29 2014, 11:11 AM

Could this be made a pass parameter rather than (or in addition to) a command line option?

—Owen

hfinkel added inline comments.
lib/Transforms/Utils/SimplifyCFG.cpp
2034

I don't think that the hasOneUse check here really does what you want once we allow for more than once instruction. We used to check that the single bonus instruction had one user and this user specifically was the Cond. Now you'd like to allow for some variable number of single-use instructions (regardless of a relationship to Cond). This could change behavior even when allowing only a single instruction.

I think that what you really want to do is to walk up the operand graph from Cond, accumulating instructions until you reach your limit, keeping the hasOneUse check and the check that the use is Cond for the first instruction.

2042

Please don't remove these blank lines, I think they make the code easier to read.

jingyue updated this revision to Diff 14187.Sep 29 2014, 2:33 PM
jingyue edited edge metadata.

Hi Owen,

I made the threshold a pass paramter in this patch. However, it doesn't look
pretty because there are several layers between createCFGSimplificationPass and
the actual use of this threshold (e.g. CFGSimplifyPass and SimplifyCFGOpt).

What are the benefits of having these pass parameters? I saw other passes such
as JumpThreading and LoopUnroll have such pass parameters too, but none of them
seem actually used. One way I can think of using that is target-specific code
can create these passes with a customized threshold, but in that case, I feel
TargetTransformInfo would be a better home.

Jingyue

jingyue added inline comments.Sep 29 2014, 4:42 PM
lib/Transforms/Utils/SimplifyCFG.cpp
2034

Hi Hal,

Thanks for the careful review! However, I don't think the modified code changes the behavior when allowing only a single bonus instruction. Note that in Line 2037 I check whether the only user is in the same BB (and appears after the potential bonus instruction otherwise def doesn't dominate use). When there is only one potential bonus instruction, it is either used by Cond or BI (DbgInfoIntrinsic only uses MDNode but not Instruction). Being used by BI is impossible, because BI only uses Cond as its first operand and other operands are all BB labels. Therefore, this bonus instruction must be used by Cond. Does this make sense? I agree it is at least worth a comment.

I like your suggestion of early exiting once we reach the limit. I'll change that part.

Thanks,
Jingyue

  • Original Message -----

From: "Jingyue Wu" <jingyue@google.com>
To: jingyue@google.com, nrotem@apple.com, eliben@google.com, meheff@google.com, resistor@mac.com
Cc: hfinkel@anl.gov, llvm-commits@cs.uiuc.edu
Sent: Monday, September 29, 2014 6:43:08 PM
Subject: Re: [PATCH] [SimplifyCFG] threshold for folding branches with common destination

Comment at: lib/Transforms/Utils/SimplifyCFG.cpp:2034
@@ +2033,3 @@
+ continue;
+ if (!I->hasOneUse() || !isSafeToSpeculativelyExecute(I, DL))

+ return false;

hfinkel wrote:

I don't think that the hasOneUse check here really does what you
want once we allow for more than once instruction. We used to
check that the single bonus instruction had one user and this user
specifically was the Cond. Now you'd like to allow for some
variable number of single-use instructions (regardless of a
relationship to Cond). This could change behavior even when
allowing only a single instruction.

I think that what you really want to do is to walk up the operand
graph from Cond, accumulating instructions until you reach your
limit, keeping the hasOneUse check and the check that the use is
Cond for the first instruction.

Hi Hal,

Thanks for the careful review! However, I don't think the modified
code changes the behavior when allowing only a single bonus
instruction. Note that in Line 2037 I check whether the only user is
in the same BB (and appears after the potential bonus instruction
otherwise def doesn't dominate use). When there is only one
potential bonus instruction, it is either used by Cond or BI
(DbgInfoIntrinsic only uses MDNode but not Instruction). Being used
by BI is impossible, because BI only uses Cond as its first operand
and other operands are all BB labels. Therefore, this bonus
instruction must be used by Cond. Does this make sense? I agree it
is at least worth a comment.

Yes, I understand now, thanks! Please do add a comment.

-Hal

I like your suggestion of early exiting once we reach the limit. I'll
change that part.

Thanks,
Jingyue

http://reviews.llvm.org/D5529

jingyue updated this revision to Diff 14197.Sep 29 2014, 6:19 PM

addresses Hal's comments

I understand now. Thanks!

Does the new patch look good to you?

Jingyue

I'm fine with this patch, but you might want to see if Hal has other thoughts on it.

hfinkel accepted this revision.Sep 30 2014, 2:10 PM
hfinkel added a reviewer: hfinkel.

LGTM, thanks!

This revision is now accepted and ready to land.Sep 30 2014, 2:10 PM
jingyue closed this revision.Sep 30 2014, 3:33 PM