mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2024-12-15 20:29:48 +00:00
9cd9e4bb2c
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! Test Plan: Added one test case to check the threshold is in effect Reviewers: nadav, eliben, meheff, resistor, hfinkel Reviewed By: hfinkel Subscribers: hfinkel, llvm-commits Differential Revision: http://reviews.llvm.org/D5529 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@218711 91177308-0d34-0410-b5e6-96231b3b80d8
29 lines
760 B
LLVM
29 lines
760 B
LLVM
; RUN: opt %s -simplifycfg -S | FileCheck %s --check-prefix=NORMAL
|
|
; RUN: opt %s -simplifycfg -S -bonus-inst-threshold=2 | FileCheck %s --check-prefix=AGGRESSIVE
|
|
|
|
define i32 @foo(i32 %a, i32 %b, i32 %c, i32 %d, i32* %input) {
|
|
; NORMAL-LABEL: @foo(
|
|
; AGGRESSIVE-LABEL: @foo(
|
|
entry:
|
|
%cmp = icmp sgt i32 %d, 3
|
|
br i1 %cmp, label %cond.end, label %lor.lhs.false
|
|
; NORMAL: br i1
|
|
; AGGRESSIVE: br i1
|
|
|
|
lor.lhs.false:
|
|
%mul = shl i32 %c, 1
|
|
%add = add nsw i32 %mul, %a
|
|
%cmp1 = icmp slt i32 %add, %b
|
|
br i1 %cmp1, label %cond.false, label %cond.end
|
|
; NORMAL: br i1
|
|
; AGGRESSIVE-NOT: br i1
|
|
|
|
cond.false:
|
|
%0 = load i32* %input, align 4
|
|
br label %cond.end
|
|
|
|
cond.end:
|
|
%cond = phi i32 [ %0, %cond.false ], [ 0, %lor.lhs.false ], [ 0, %entry ]
|
|
ret i32 %cond
|
|
}
|