1
+ // RUN: enzymexlamlir-opt %s -affine-cfg | FileCheck %s
2
+
3
+ // Define a simple affine set
4
+ #set0 = affine_set <(d0 ) : (d0 >= 0 )>
5
+
6
+ // Basic test case - single result with index_cast
7
+ // CHECK-LABEL: func @test_affine_if_index_cast
8
+ func.func @test_affine_if_index_cast (%arg0: index ) -> index {
9
+ %c1_i64 = arith.constant 1 : i64
10
+ %c2_i64 = arith.constant 2 : i64
11
+
12
+ // CHECK: %[[RESULT:.*]] = affine.if
13
+ // CHECK: %[[CAST1:.*]] = arith.index_cast %c1_i64 : i64 to index
14
+ // CHECK: affine.yield %[[CAST1]]
15
+ // CHECK: else
16
+ // CHECK: %[[CAST2:.*]] = arith.index_cast %c2_i64 : i64 to index
17
+ // CHECK: affine.yield %[[CAST2]]
18
+ // CHECK-NOT: arith.index_cast
19
+
20
+ %0 = affine.if #set0 (%arg0 ) -> (i64 ) {
21
+ affine.yield %c1_i64 : i64
22
+ } else {
23
+ affine.yield %c2_i64 : i64
24
+ }
25
+
26
+ %1 = arith.index_cast %0 : i64 to index
27
+ return %1 : index
28
+ }
29
+
30
+ // -----
31
+
32
+ // Multiple results with only one used in index_cast
33
+ // CHECK-LABEL: func @test_multi_result_single_cast
34
+ func.func @test_multi_result_single_cast (%arg0: index ) -> (i64 , index ) {
35
+ %c1_i64 = arith.constant 1 : i64
36
+ %c2_i64 = arith.constant 2 : i64
37
+ %c3_i64 = arith.constant 3 : i64
38
+ %c4_i64 = arith.constant 4 : i64
39
+
40
+ // CHECK: %[[RESULT:.*]]:2 = affine.if
41
+ // CHECK: affine.yield %c1_i64, %{{.*}}
42
+ // CHECK: else
43
+ // CHECK: %[[CAST:.*]] = arith.index_cast %c4_i64 : i64 to index
44
+ // CHECK: affine.yield %c3_i64, %[[CAST]]
45
+ // CHECK-NOT: arith.index_cast %{{.*}}#1
46
+
47
+ %0:2 = affine.if #set0 (%arg0 ) -> (i64 , i64 ) {
48
+ affine.yield %c1_i64 , %c2_i64 : i64 , i64
49
+ } else {
50
+ affine.yield %c3_i64 , %c4_i64 : i64 , i64
51
+ }
52
+
53
+ %1 = arith.index_cast %0#1 : i64 to index
54
+ return %0#0 , %1 : i64 , index
55
+ }
56
+
57
+ // -----
58
+
59
+ // Multiple results with all used in index_cast
60
+ // CHECK-LABEL: func @test_multi_result_all_cast
61
+ func.func @test_multi_result_all_cast (%arg0: index ) -> (index , index ) {
62
+ %c1_i64 = arith.constant 1 : i64
63
+ %c2_i64 = arith.constant 2 : i64
64
+ %c3_i64 = arith.constant 3 : i64
65
+ %c4_i64 = arith.constant 4 : i64
66
+
67
+ // CHECK: %[[RESULT:.*]]:2 = affine.if
68
+ // CHECK: %[[CAST1:.*]] = arith.index_cast %c1_i64 : i64 to index
69
+ // CHECK: %[[CAST2:.*]] = arith.index_cast %c2_i64 : i64 to index
70
+ // CHECK: affine.yield %[[CAST1]], %[[CAST2]]
71
+ // CHECK: else
72
+ // CHECK: %[[CAST3:.*]] = arith.index_cast %c3_i64 : i64 to index
73
+ // CHECK: %[[CAST4:.*]] = arith.index_cast %c4_i64 : i64 to index
74
+ // CHECK: affine.yield %[[CAST3]], %[[CAST4]]
75
+
76
+ %0:2 = affine.if #set0 (%arg0 ) -> (i64 , i64 ) {
77
+ affine.yield %c1_i64 , %c2_i64 : i64 , i64
78
+ } else {
79
+ affine.yield %c3_i64 , %c4_i64 : i64 , i64
80
+ }
81
+
82
+ %1 = arith.index_cast %0#0 : i64 to index
83
+ %2 = arith.index_cast %0#1 : i64 to index
84
+ return %1 , %2 : index , index
85
+ }
86
+
87
+ // -----
88
+
89
+ // Case where result is used in multiple places - should not optimize
90
+ // CHECK-LABEL: func @test_multiple_uses
91
+ func.func @test_multiple_uses (%arg0: index ) -> (index , i64 ) {
92
+ %c1_i64 = arith.constant 1 : i64
93
+ %c2_i64 = arith.constant 2 : i64
94
+
95
+ // CHECK: %[[RESULT:.*]] = affine.if
96
+ // CHECK-NOT: arith.index_cast
97
+ // CHECK: affine.yield %c1_i64
98
+ // CHECK: else
99
+ // CHECK-NOT: arith.index_cast
100
+ // CHECK: affine.yield %c2_i64
101
+ // CHECK: arith.index_cast %[[RESULT]] : i64 to index
102
+
103
+ %0 = affine.if #set0 (%arg0 ) -> (i64 ) {
104
+ affine.yield %c1_i64 : i64
105
+ } else {
106
+ affine.yield %c2_i64 : i64
107
+ }
108
+
109
+ %1 = arith.index_cast %0 : i64 to index
110
+ return %1 , %0 : index , i64
111
+ }
112
+
113
+ // -----
114
+
115
+ // Case with multiple index_casts applied to the same result - should not optimize
116
+ // CHECK-LABEL: func @test_multiple_casts
117
+ func.func @test_multiple_casts (%arg0: index ) -> (index , index ) {
118
+ %c1_i64 = arith.constant 1 : i64
119
+ %c2_i64 = arith.constant 2 : i64
120
+
121
+ // CHECK: %[[RESULT:.*]] = affine.if
122
+ // CHECK-NOT: arith.index_cast
123
+ // CHECK: affine.yield %c1_i64
124
+ // CHECK: else
125
+ // CHECK-NOT: arith.index_cast
126
+ // CHECK: affine.yield %c2_i64
127
+ // CHECK: %[[CAST1:.*]] = arith.index_cast %[[RESULT]] : i64 to index
128
+ // CHECK: %[[CAST2:.*]] = arith.index_cast %[[RESULT]] : i64 to index
129
+
130
+ %0 = affine.if #set0 (%arg0 ) -> (i64 ) {
131
+ affine.yield %c1_i64 : i64
132
+ } else {
133
+ affine.yield %c2_i64 : i64
134
+ }
135
+
136
+ %1 = arith.index_cast %0 : i64 to index
137
+ %2 = arith.index_cast %0 : i64 to index
138
+ return %1 , %2 : index , index
139
+ }
140
+
141
+ // -----
142
+
143
+ // Case where the result is already an index type - should not optimize
144
+ // CHECK-LABEL: func @test_already_index
145
+ func.func @test_already_index (%arg0: index ) -> index {
146
+ %c1 = arith.constant 1 : index
147
+ %c2 = arith.constant 2 : index
148
+
149
+ // CHECK: %[[RESULT:.*]] = affine.if
150
+ // CHECK-NOT: arith.index_cast
151
+ // CHECK: affine.yield %c1
152
+ // CHECK: else
153
+ // CHECK-NOT: arith.index_cast
154
+ // CHECK: affine.yield %c2
155
+
156
+ %0 = affine.if #set0 (%arg0 ) -> (index ) {
157
+ affine.yield %c1 : index
158
+ } else {
159
+ affine.yield %c2 : index
160
+ }
161
+
162
+ return %0 : index
163
+ }
0 commit comments