Skip to content

Commit 4165146

Browse files
Add more testing
1 parent a56bab6 commit 4165146

File tree

2 files changed

+103
-2
lines changed

2 files changed

+103
-2
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// RUN: fir-opt --split-input-file --constant-argument-globalisation-opt < %s | FileCheck %s
2+
3+
module {
4+
// Test for "two conditional writes to the same alloca doesn't get replaced."
5+
func.func @func(%arg0: i32, %arg1: i1) {
6+
%c2_i32 = arith.constant 2 : i32
7+
%addr = fir.alloca i32 {adapt.valuebyref}
8+
fir.if %arg1 {
9+
fir.store %c2_i32 to %addr : !fir.ref<i32>
10+
} else {
11+
fir.store %arg0 to %addr : !fir.ref<i32>
12+
}
13+
fir.call @sub2(%addr) : (!fir.ref<i32>) -> ()
14+
return
15+
}
16+
func.func private @sub2(!fir.ref<i32>)
17+
18+
// CHECK-LABEL: func.func @func
19+
// CHECK-SAME: [[ARG0:%.*]]: i32
20+
// CHECK-SAME: [[ARG1:%.*]]: i1)
21+
// CHECK: [[CONST:%.*]] = arith.constant
22+
// CHECK: [[ADDR:%.*]] = fir.alloca i32
23+
// CHECK: fir.if [[ARG1]]
24+
// CHECK: fir.store [[CONST]] to [[ADDR]]
25+
// CHECK: } else {
26+
// CHECK: fir.store [[ARG0]] to [[ADDR]]
27+
// CHECK: fir.call @sub2([[ADDR]])
28+
// CHECK: return
29+
30+
}
31+
32+
// -----
33+
34+
module {
35+
// Test for "two writes to the same alloca doesn't get replaced."
36+
func.func @func() {
37+
%c1_i32 = arith.constant 1 : i32
38+
%c2_i32 = arith.constant 2 : i32
39+
%addr = fir.alloca i32 {adapt.valuebyref}
40+
fir.store %c1_i32 to %addr : !fir.ref<i32>
41+
fir.store %c2_i32 to %addr : !fir.ref<i32>
42+
fir.call @sub2(%addr) : (!fir.ref<i32>) -> ()
43+
return
44+
}
45+
func.func private @sub2(!fir.ref<i32>)
46+
47+
// CHECK-LABEL: func.func @func
48+
// CHECK: [[CONST1:%.*]] = arith.constant
49+
// CHECK: [[CONST2:%.*]] = arith.constant
50+
// CHECK: [[ADDR:%.*]] = fir.alloca i32
51+
// CHECK: fir.store [[CONST1]] to [[ADDR]]
52+
// CHECK: fir.store [[CONST2]] to [[ADDR]]
53+
// CHECK: fir.call @sub2([[ADDR]])
54+
// CHECK: return
55+
56+
}
57+
58+
// -----
59+
60+
module {
61+
// Test for "one write to the the alloca gets replaced."
62+
func.func @func() {
63+
%c1_i32 = arith.constant 1 : i32
64+
%addr = fir.alloca i32 {adapt.valuebyref}
65+
fir.store %c1_i32 to %addr : !fir.ref<i32>
66+
fir.call @sub2(%addr) : (!fir.ref<i32>) -> ()
67+
return
68+
}
69+
func.func private @sub2(!fir.ref<i32>)
70+
71+
// CHECK-LABEL: func.func @func
72+
// CHECK: [[ADDR:%.*]] = fir.address_of([[EXTR:@.*]]) : !fir.ref<i32>
73+
// CHECK: fir.call @sub2([[ADDR]])
74+
// CHECK: return
75+
// CHECK: fir.global internal [[EXTR]] constant : i32 {
76+
// CHECK: %{{.*}} = arith.constant 1 : i32
77+
// CHECK: fir.has_value %{{.*}} : i32
78+
// CHECK: }
79+
80+
}

flang/test/Transforms/constant-argument-globalisation.fir

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// RUN: fir-opt --constant-argument-globalisation-opt < %s | FileCheck %s
2+
// RUN: %flang_fc1 -emit-llvm -flang-deprecated-no-hlfir -O2 -mllvm --disable-constant-argument-globalisation -o - %s | FileCheck --check-prefix=DISABLE %s
23
module {
34
func.func @sub1(%arg0: !fir.ref<i32> {fir.bindc_name = "x"}, %arg1: !fir.ref<i32> {fir.bindc_name = "y"}) {
45
%0 = fir.alloca i32 {adapt.valuebyref}
@@ -19,8 +20,8 @@ module {
1920
return
2021
}
2122
func.func private @sub2(!fir.ref<f64>, !fir.ref<f64>, !fir.ref<i32>, !fir.ref<i32>, !fir.ref<i32>)
22-
}
23-
// CHECK-LABEL: func.func @sub1
23+
24+
// CHECK-LABEL: func.func @sub1(
2425
// CHECK-SAME: [[ARG0:%.*]]: !fir.ref<i32> {{{.*}}},
2526
// CHECK-SAME: [[ARG1:%.*]]: !fir.ref<i32> {{{.*}}}) {
2627
// CHECK: [[X:%.*]] = fir.declare [[ARG0]] {{.*}}
@@ -43,3 +44,23 @@ module {
4344
// CHECK: %{{.*}} = arith.constant 1 : i32
4445
// CHECK: fir.has_value %{{.*}} : i32
4546
// CHECK: }
47+
48+
// DISABLE-LABEL: ; ModuleID =
49+
// DISABLE-NOT: @_extruded
50+
// DISABLE: define void @sub1(
51+
// DISABLE-SAME: ptr [[ARG0:%.*]],
52+
// DISABLE-SAME: ptr [[ARG1:%.*]])
53+
// DISABLE-SMAE: {
54+
// DISABLE: [[CONST_I:%.*]] = alloca i32
55+
// DISABLE: [[CONST_R1:%.*]] = alloca double
56+
// DISABLE: [[CONST_R0:%.*]] = alloca double
57+
// DISABLE: store double 0.0{{.*}}+00, ptr [[CONST_R0]]
58+
// DISABLE: store double 1.0{{.*}}+00, ptr [[CONST_R1]]
59+
// DISABLE: store i32 1, ptr [[CONST_I]]
60+
// DISABLE: call void @sub2(ptr nonnull [[CONST_R0]],
61+
// DISABLE-SAME: ptr nonnull [[CONST_R1]],
62+
// DISABLE-SAME: ptr [[ARG0]], ptr [[ARG1]],
63+
// DISABLE-SAME: ptr nonnull [[CONST_I]])
64+
// DISABLE: ret void
65+
// DISABLE: }
66+
}

0 commit comments

Comments
 (0)