Skip to content

Commit 5b94037

Browse files
committed
[PhaseOrdering] Add test for incorrect merge function scheduling
Add an -enable-merge-functions option to allow testing of function merging as it will actually happen in the optimization pipeline. Based on that add a test where we currently produce two identical functions without merging them due to incorrect pass scheduling under the new pass manager.
1 parent 946eb7a commit 5b94037

File tree

2 files changed

+131
-1
lines changed

2 files changed

+131
-1
lines changed

llvm/lib/Passes/PassBuilderPipelines.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,10 @@ static cl::opt<bool> EnableNoRerunSimplificationPipeline(
178178
"than once in the case that SCC mutations cause a function to be "
179179
"visited multiple times as long as the function has not been changed"));
180180

181+
static cl::opt<bool> EnableMergeFunctions(
182+
"enable-merge-functions", cl::init(false), cl::Hidden,
183+
cl::desc("Enable function merging as part of the optimization pipeline"));
184+
181185
PipelineTuningOptions::PipelineTuningOptions() {
182186
LoopInterleaving = true;
183187
LoopVectorization = true;
@@ -187,7 +191,7 @@ PipelineTuningOptions::PipelineTuningOptions() {
187191
LicmMssaOptCap = SetLicmMssaOptCap;
188192
LicmMssaNoAccForPromotionCap = SetLicmMssaNoAccForPromotionCap;
189193
CallGraphProfile = true;
190-
MergeFunctions = false;
194+
MergeFunctions = EnableMergeFunctions;
191195
EagerlyInvalidateAnalyses = EnableEagerlyInvalidateAnalyses;
192196
}
193197

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2+
; RUN: opt -passes='default<O3>' -enable-merge-functions -S < %s | FileCheck %s
3+
4+
; TODO: These two functions should get merged, but currently aren't, because
5+
; the function merging pass is scheduled too early.
6+
7+
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
8+
target triple = "x86_64-unknown-linux-gnu"
9+
10+
define i1 @test1(i32 %c) {
11+
; CHECK-LABEL: @test1(
12+
; CHECK-NEXT: entry:
13+
; CHECK-NEXT: [[SWITCH_TABLEIDX:%.*]] = add i32 [[C:%.*]], -100
14+
; CHECK-NEXT: [[TMP0:%.*]] = icmp ult i32 [[SWITCH_TABLEIDX]], 20
15+
; CHECK-NEXT: [[SWITCH_CAST:%.*]] = trunc i32 [[SWITCH_TABLEIDX]] to i20
16+
; CHECK-NEXT: [[SWITCH_DOWNSHIFT:%.*]] = lshr i20 -490991, [[SWITCH_CAST]]
17+
; CHECK-NEXT: [[TMP1:%.*]] = and i20 [[SWITCH_DOWNSHIFT]], 1
18+
; CHECK-NEXT: [[SWITCH_MASKED:%.*]] = icmp ne i20 [[TMP1]], 0
19+
; CHECK-NEXT: [[I_0:%.*]] = select i1 [[TMP0]], i1 [[SWITCH_MASKED]], i1 false
20+
; CHECK-NEXT: ret i1 [[I_0]]
21+
;
22+
entry:
23+
%_4 = alloca i8, align 1
24+
%_3 = alloca i8, align 1
25+
%_2 = alloca i8, align 1
26+
%i = alloca i8, align 1
27+
%i1 = icmp eq i32 %c, 115
28+
br i1 %i1, label %bb10, label %bb11
29+
30+
bb10: ; preds = %entry
31+
store i8 1, i8* %_4, align 1
32+
br label %bb12
33+
34+
bb11: ; preds = %entry
35+
%_6 = icmp eq i32 %c, 109
36+
%i2 = zext i1 %_6 to i8
37+
store i8 %i2, i8* %_4, align 1
38+
br label %bb12
39+
40+
bb12: ; preds = %bb11, %bb10
41+
%i3 = load i8, i8* %_4, align 1
42+
%i4 = trunc i8 %i3 to i1
43+
br i1 %i4, label %bb7, label %bb8
44+
45+
bb8: ; preds = %bb12
46+
%_8 = icmp eq i32 %c, 104
47+
%i5 = zext i1 %_8 to i8
48+
store i8 %i5, i8* %_3, align 1
49+
br label %bb9
50+
51+
bb7: ; preds = %bb12
52+
store i8 1, i8* %_3, align 1
53+
br label %bb9
54+
55+
bb9: ; preds = %bb7, %bb8
56+
%i6 = load i8, i8* %_3, align 1
57+
%i7 = trunc i8 %i6 to i1
58+
br i1 %i7, label %bb4, label %bb5
59+
60+
bb5: ; preds = %bb9
61+
%_10 = icmp eq i32 %c, 100
62+
%i8 = zext i1 %_10 to i8
63+
store i8 %i8, i8* %_2, align 1
64+
br label %bb6
65+
66+
bb4: ; preds = %bb9
67+
store i8 1, i8* %_2, align 1
68+
br label %bb6
69+
70+
bb6: ; preds = %bb4, %bb5
71+
%i9 = load i8, i8* %_2, align 1
72+
%i10 = trunc i8 %i9 to i1
73+
br i1 %i10, label %bb1, label %bb2
74+
75+
bb2: ; preds = %bb6
76+
%_12 = icmp eq i32 %c, 119
77+
%i11 = zext i1 %_12 to i8
78+
store i8 %i11, i8* %i, align 1
79+
br label %bb3
80+
81+
bb1: ; preds = %bb6
82+
store i8 1, i8* %i, align 1
83+
br label %bb3
84+
85+
bb3: ; preds = %bb1, %bb2
86+
%i12 = load i8, i8* %i, align 1
87+
%i13 = trunc i8 %i12 to i1
88+
ret i1 %i13
89+
}
90+
91+
define i1 @test2(i32 %c) {
92+
; CHECK-LABEL: @test2(
93+
; CHECK-NEXT: entry:
94+
; CHECK-NEXT: [[SWITCH_TABLEIDX:%.*]] = add i32 [[C:%.*]], -100
95+
; CHECK-NEXT: [[TMP0:%.*]] = icmp ult i32 [[SWITCH_TABLEIDX]], 20
96+
; CHECK-NEXT: [[SWITCH_CAST:%.*]] = trunc i32 [[SWITCH_TABLEIDX]] to i20
97+
; CHECK-NEXT: [[SWITCH_DOWNSHIFT:%.*]] = lshr i20 -490991, [[SWITCH_CAST]]
98+
; CHECK-NEXT: [[TMP1:%.*]] = and i20 [[SWITCH_DOWNSHIFT]], 1
99+
; CHECK-NEXT: [[SWITCH_MASKED:%.*]] = icmp ne i20 [[TMP1]], 0
100+
; CHECK-NEXT: [[I_0:%.*]] = select i1 [[TMP0]], i1 [[SWITCH_MASKED]], i1 false
101+
; CHECK-NEXT: ret i1 [[I_0]]
102+
;
103+
entry:
104+
%i = alloca i8, align 1
105+
switch i32 %c, label %bb1 [
106+
i32 115, label %bb2
107+
i32 109, label %bb2
108+
i32 104, label %bb2
109+
i32 100, label %bb2
110+
i32 119, label %bb2
111+
]
112+
113+
bb1: ; preds = %entry
114+
store i8 0, i8* %i, align 1
115+
br label %bb3
116+
117+
bb2: ; preds = %entry, %entry, %entry, %entry, %entry
118+
store i8 1, i8* %i, align 1
119+
br label %bb3
120+
121+
bb3: ; preds = %bb2, %bb1
122+
%i1 = load i8, i8* %i, align 1
123+
%i2 = trunc i8 %i1 to i1
124+
ret i1 %i2
125+
}
126+

0 commit comments

Comments
 (0)