blob: 8e02c06a0a293e4f44be92623d33dd3bf493d00e [file] [log] [blame]
Matthias Springere7790fb2023-01-04 10:39:411// RUN: mlir-opt -allow-unregistered-dialect %s -pass-pipeline='builtin.module(func.func(canonicalize{test-convergence}))' -split-input-file | FileCheck %s
Andy Davisa4669cd2019-11-14 20:22:282
Chris Lattnerbbf362b2019-01-02 18:20:003// CHECK-LABEL: func @test_subi_zero
River Riddlecda6aa72022-04-20 23:22:214func.func @test_subi_zero(%arg0: i32) -> i32 {
Mogballa54f4ea2021-10-12 23:14:575 // CHECK-NEXT: %c0_i32 = arith.constant 0 : i32
Chris Lattnera03051b2018-10-22 20:08:276 // CHECK-NEXT: return %c0
Mogballa54f4ea2021-10-12 23:14:577 %y = arith.subi %arg0, %arg0 : i32
Chris Lattnera03051b2018-10-22 20:08:278 return %y: i32
9}
10
Stephan Herhutaf016ba2019-04-08 12:53:5911// CHECK-LABEL: func @test_subi_zero_vector
River Riddlecda6aa72022-04-20 23:22:2112func.func @test_subi_zero_vector(%arg0: vector<4xi32>) -> vector<4xi32> {
Mogballa54f4ea2021-10-12 23:14:5713 //CHECK-NEXT: %cst = arith.constant dense<0> : vector<4xi32>
14 %y = arith.subi %arg0, %arg0 : vector<4xi32>
Stephan Herhutaf016ba2019-04-08 12:53:5915 // CHECK-NEXT: return %cst
16 return %y: vector<4xi32>
17}
18
19// CHECK-LABEL: func @test_subi_zero_tensor
River Riddlecda6aa72022-04-20 23:22:2120func.func @test_subi_zero_tensor(%arg0: tensor<4x5xi32>) -> tensor<4x5xi32> {
Mogballa54f4ea2021-10-12 23:14:5721 //CHECK-NEXT: %cst = arith.constant dense<0> : tensor<4x5xi32>
22 %y = arith.subi %arg0, %arg0 : tensor<4x5xi32>
Stephan Herhutaf016ba2019-04-08 12:53:5923 // CHECK-NEXT: return %cst
24 return %y: tensor<4x5xi32>
25}
26
Chris Lattnerbbf362b2019-01-02 18:20:0027// CHECK-LABEL: func @dim
River Riddlecda6aa72022-04-20 23:22:2128func.func @dim(%arg0: tensor<8x4xf32>) -> index {
Chris Lattner80e884a2018-10-16 16:31:4529
Mogballa54f4ea2021-10-12 23:14:5730 // CHECK: %c4 = arith.constant 4 : index
31 %c1 = arith.constant 1 : index
Matthias Springerc0a63182021-07-01 00:58:4832 %0 = tensor.dim %arg0, %c1 : tensor<8x4xf32>
Chris Lattner80e884a2018-10-16 16:31:4533
34 // CHECK-NEXT: return %c4
35 return %0 : index
36}
37
Chris Lattnerbbf362b2019-01-02 18:20:0038// CHECK-LABEL: func @test_commutative
River Riddlecda6aa72022-04-20 23:22:2139func.func @test_commutative(%arg0: i32) -> (i32, i32) {
Mogballa54f4ea2021-10-12 23:14:5740 // CHECK: %c42_i32 = arith.constant 42 : i32
41 %c42_i32 = arith.constant 42 : i32
42 // CHECK-NEXT: %0 = arith.addi %arg0, %c42_i32 : i32
43 %y = arith.addi %c42_i32, %arg0 : i32
Chris Lattner967d9342018-10-26 05:04:3544
45 // This should not be swapped.
Mogballa54f4ea2021-10-12 23:14:5746 // CHECK-NEXT: %1 = arith.subi %c42_i32, %arg0 : i32
47 %z = arith.subi %c42_i32, %arg0 : i32
Chris Lattner967d9342018-10-26 05:04:3548
49 // CHECK-NEXT: return %0, %1
50 return %y, %z: i32, i32
Chris Lattner9e3b9282018-10-12 00:21:5551}
52
Chris Lattnerbbf362b2019-01-02 18:20:0053// CHECK-LABEL: func @trivial_dce
River Riddlecda6aa72022-04-20 23:22:2154func.func @trivial_dce(%arg0: tensor<8x4xf32>) {
Mogballa54f4ea2021-10-12 23:14:5755 %c1 = arith.constant 1 : index
Matthias Springerc0a63182021-07-01 00:58:4856 %0 = tensor.dim %arg0, %c1 : tensor<8x4xf32>
Chris Lattner78502582018-10-22 02:53:1057 // CHECK-NEXT: return
58 return
59}
60
Tres Popp2d2d6962020-04-23 16:13:4461// CHECK-LABEL: func @load_dce
River Riddlecda6aa72022-04-20 23:22:2162func.func @load_dce(%arg0: index) {
Mogballa54f4ea2021-10-12 23:14:5763 %c4 = arith.constant 4 : index
Julian Grosse2310702021-02-10 12:53:1164 %a = memref.alloc(%c4) : memref<?xf32>
65 %2 = memref.load %a[%arg0] : memref<?xf32>
66 memref.dealloc %a: memref<?xf32>
Tres Popp2d2d6962020-04-23 16:13:4467 // CHECK-NEXT: return
68 return
69}
70
Chris Lattnerbbf362b2019-01-02 18:20:0071// CHECK-LABEL: func @addi_zero
River Riddlecda6aa72022-04-20 23:22:2172func.func @addi_zero(%arg0: i32) -> i32 {
Chris Lattnera03051b2018-10-22 20:08:2773 // CHECK-NEXT: return %arg0
Mogballa54f4ea2021-10-12 23:14:5774 %c0_i32 = arith.constant 0 : i32
75 %y = arith.addi %c0_i32, %arg0 : i32
Chris Lattnera03051b2018-10-22 20:08:2776 return %y: i32
77}
Chris Lattner301f83f2018-10-23 17:12:0078
Stephan Herhute04d4bf2019-11-11 10:33:4979// CHECK-LABEL: func @addi_zero_index
River Riddlecda6aa72022-04-20 23:22:2180func.func @addi_zero_index(%arg0: index) -> index {
Stephan Herhute04d4bf2019-11-11 10:33:4981 // CHECK-NEXT: return %arg0
Mogballa54f4ea2021-10-12 23:14:5782 %c0_index = arith.constant 0 : index
83 %y = arith.addi %c0_index, %arg0 : index
Stephan Herhute04d4bf2019-11-11 10:33:4984 return %y: index
85}
86
87
Chris Lattnerbbf362b2019-01-02 18:20:0088// CHECK-LABEL: func @addi_zero_vector
River Riddlecda6aa72022-04-20 23:22:2189func.func @addi_zero_vector(%arg0: vector<4 x i32>) -> vector<4 x i32> {
Lei Zhang582b0762018-10-29 17:22:4990 // CHECK-NEXT: return %arg0
Mogballa54f4ea2021-10-12 23:14:5791 %c0_v4i32 = arith.constant dense<0> : vector<4 x i32>
92 %y = arith.addi %c0_v4i32, %arg0 : vector<4 x i32>
Lei Zhang582b0762018-10-29 17:22:4993 return %y: vector<4 x i32>
94}
95
Chris Lattnerbbf362b2019-01-02 18:20:0096// CHECK-LABEL: func @addi_zero_tensor
River Riddlecda6aa72022-04-20 23:22:2197func.func @addi_zero_tensor(%arg0: tensor<4 x 5 x i32>) -> tensor<4 x 5 x i32> {
Lei Zhang582b0762018-10-29 17:22:4998 // CHECK-NEXT: return %arg0
Mogballa54f4ea2021-10-12 23:14:5799 %c0_t45i32 = arith.constant dense<0> : tensor<4 x 5 x i32>
100 %y = arith.addi %arg0, %c0_t45i32 : tensor<4 x 5 x i32>
Lei Zhang582b0762018-10-29 17:22:49101 return %y: tensor<4 x 5 x i32>
102}
103
Lei Zhang311af4a2019-01-11 17:12:11104// CHECK-LABEL: func @muli_zero
River Riddlecda6aa72022-04-20 23:22:21105func.func @muli_zero(%arg0: i32) -> i32 {
Mogballa54f4ea2021-10-12 23:14:57106 // CHECK-NEXT: %c0_i32 = arith.constant 0 : i32
107 %c0_i32 = arith.constant 0 : i32
Lei Zhang311af4a2019-01-11 17:12:11108
Mogballa54f4ea2021-10-12 23:14:57109 %y = arith.muli %c0_i32, %arg0 : i32
Lei Zhang311af4a2019-01-11 17:12:11110
111 // CHECK-NEXT: return %c0_i32
112 return %y: i32
113}
114
Stephan Herhute04d4bf2019-11-11 10:33:49115// CHECK-LABEL: func @muli_zero_index
River Riddlecda6aa72022-04-20 23:22:21116func.func @muli_zero_index(%arg0: index) -> index {
Mogballa54f4ea2021-10-12 23:14:57117 // CHECK-NEXT: %[[CST:.*]] = arith.constant 0 : index
118 %c0_index = arith.constant 0 : index
Stephan Herhute04d4bf2019-11-11 10:33:49119
Mogballa54f4ea2021-10-12 23:14:57120 %y = arith.muli %c0_index, %arg0 : index
Stephan Herhute04d4bf2019-11-11 10:33:49121
122 // CHECK-NEXT: return %[[CST]]
123 return %y: index
124}
125
Lei Zhang311af4a2019-01-11 17:12:11126// CHECK-LABEL: func @muli_zero_vector
River Riddlecda6aa72022-04-20 23:22:21127func.func @muli_zero_vector(%arg0: vector<4 x i32>) -> vector<4 x i32> {
Mogballa54f4ea2021-10-12 23:14:57128 // CHECK-NEXT: %cst = arith.constant dense<0> : vector<4xi32>
129 %cst = arith.constant dense<0> : vector<4 x i32>
Lei Zhang311af4a2019-01-11 17:12:11130
Mogballa54f4ea2021-10-12 23:14:57131 %y = arith.muli %cst, %arg0 : vector<4 x i32>
Lei Zhang311af4a2019-01-11 17:12:11132
133 // CHECK-NEXT: return %cst
134 return %y: vector<4 x i32>
135}
136
137// CHECK-LABEL: func @muli_zero_tensor
River Riddlecda6aa72022-04-20 23:22:21138func.func @muli_zero_tensor(%arg0: tensor<4 x 5 x i32>) -> tensor<4 x 5 x i32> {
Mogballa54f4ea2021-10-12 23:14:57139 // CHECK-NEXT: %cst = arith.constant dense<0> : tensor<4x5xi32>
140 %cst = arith.constant dense<0> : tensor<4 x 5 x i32>
Lei Zhang311af4a2019-01-11 17:12:11141
Mogballa54f4ea2021-10-12 23:14:57142 %y = arith.muli %arg0, %cst : tensor<4 x 5 x i32>
Lei Zhang311af4a2019-01-11 17:12:11143
144 // CHECK-NEXT: return %cst
145 return %y: tensor<4 x 5 x i32>
146}
147
Chris Lattnerbbf362b2019-01-02 18:20:00148// CHECK-LABEL: func @muli_one
River Riddlecda6aa72022-04-20 23:22:21149func.func @muli_one(%arg0: i32) -> i32 {
Lei Zhang60b51842018-10-26 18:28:06150 // CHECK-NEXT: return %arg0
Mogballa54f4ea2021-10-12 23:14:57151 %c0_i32 = arith.constant 1 : i32
152 %y = arith.muli %c0_i32, %arg0 : i32
Lei Zhang60b51842018-10-26 18:28:06153 return %y: i32
154}
155
Stephan Herhute04d4bf2019-11-11 10:33:49156// CHECK-LABEL: func @muli_one_index
River Riddlecda6aa72022-04-20 23:22:21157func.func @muli_one_index(%arg0: index) -> index {
Stephan Herhute04d4bf2019-11-11 10:33:49158 // CHECK-NEXT: return %arg0
Mogballa54f4ea2021-10-12 23:14:57159 %c0_index = arith.constant 1 : index
160 %y = arith.muli %c0_index, %arg0 : index
Stephan Herhute04d4bf2019-11-11 10:33:49161 return %y: index
162}
163
Chris Lattnerbbf362b2019-01-02 18:20:00164// CHECK-LABEL: func @muli_one_vector
River Riddlecda6aa72022-04-20 23:22:21165func.func @muli_one_vector(%arg0: vector<4 x i32>) -> vector<4 x i32> {
Lei Zhang582b0762018-10-29 17:22:49166 // CHECK-NEXT: return %arg0
Mogballa54f4ea2021-10-12 23:14:57167 %c1_v4i32 = arith.constant dense<1> : vector<4 x i32>
168 %y = arith.muli %c1_v4i32, %arg0 : vector<4 x i32>
Lei Zhang582b0762018-10-29 17:22:49169 return %y: vector<4 x i32>
170}
171
Chris Lattnerbbf362b2019-01-02 18:20:00172// CHECK-LABEL: func @muli_one_tensor
River Riddlecda6aa72022-04-20 23:22:21173func.func @muli_one_tensor(%arg0: tensor<4 x 5 x i32>) -> tensor<4 x 5 x i32> {
Lei Zhang582b0762018-10-29 17:22:49174 // CHECK-NEXT: return %arg0
Mogballa54f4ea2021-10-12 23:14:57175 %c1_t45i32 = arith.constant dense<1> : tensor<4 x 5 x i32>
176 %y = arith.muli %arg0, %c1_t45i32 : tensor<4 x 5 x i32>
Lei Zhang582b0762018-10-29 17:22:49177 return %y: tensor<4 x 5 x i32>
178}
179
Stephan Herhuta8a5c0692019-04-08 07:00:46180//CHECK-LABEL: func @and_self
River Riddlecda6aa72022-04-20 23:22:21181func.func @and_self(%arg0: i32) -> i32 {
Stephan Herhuta8a5c0692019-04-08 07:00:46182 //CHECK-NEXT: return %arg0
Mogballa54f4ea2021-10-12 23:14:57183 %1 = arith.andi %arg0, %arg0 : i32
Stephan Herhuta8a5c0692019-04-08 07:00:46184 return %1 : i32
185}
186
187//CHECK-LABEL: func @and_self_vector
River Riddlecda6aa72022-04-20 23:22:21188func.func @and_self_vector(%arg0: vector<4xi32>) -> vector<4xi32> {
Stephan Herhuta8a5c0692019-04-08 07:00:46189 //CHECK-NEXT: return %arg0
Mogballa54f4ea2021-10-12 23:14:57190 %1 = arith.andi %arg0, %arg0 : vector<4xi32>
Stephan Herhuta8a5c0692019-04-08 07:00:46191 return %1 : vector<4xi32>
192}
193
194//CHECK-LABEL: func @and_self_tensor
River Riddlecda6aa72022-04-20 23:22:21195func.func @and_self_tensor(%arg0: tensor<4x5xi32>) -> tensor<4x5xi32> {
Stephan Herhuta8a5c0692019-04-08 07:00:46196 //CHECK-NEXT: return %arg0
Mogballa54f4ea2021-10-12 23:14:57197 %1 = arith.andi %arg0, %arg0 : tensor<4x5xi32>
Stephan Herhuta8a5c0692019-04-08 07:00:46198 return %1 : tensor<4x5xi32>
199}
200
201//CHECK-LABEL: func @and_zero
River Riddlecda6aa72022-04-20 23:22:21202func.func @and_zero(%arg0: i32) -> i32 {
Mogballa54f4ea2021-10-12 23:14:57203 // CHECK-NEXT: %c0_i32 = arith.constant 0 : i32
204 %c0_i32 = arith.constant 0 : i32
Stephan Herhuta8a5c0692019-04-08 07:00:46205 // CHECK-NEXT: return %c0_i32
Mogballa54f4ea2021-10-12 23:14:57206 %1 = arith.andi %arg0, %c0_i32 : i32
Stephan Herhuta8a5c0692019-04-08 07:00:46207 return %1 : i32
208}
209
Stephan Herhute04d4bf2019-11-11 10:33:49210//CHECK-LABEL: func @and_zero_index
River Riddlecda6aa72022-04-20 23:22:21211func.func @and_zero_index(%arg0: index) -> index {
Mogballa54f4ea2021-10-12 23:14:57212 // CHECK-NEXT: %[[CST:.*]] = arith.constant 0 : index
213 %c0_index = arith.constant 0 : index
Stephan Herhute04d4bf2019-11-11 10:33:49214 // CHECK-NEXT: return %[[CST]]
Mogballa54f4ea2021-10-12 23:14:57215 %1 = arith.andi %arg0, %c0_index : index
Stephan Herhute04d4bf2019-11-11 10:33:49216 return %1 : index
217}
218
Stephan Herhuta8a5c0692019-04-08 07:00:46219//CHECK-LABEL: func @and_zero_vector
River Riddlecda6aa72022-04-20 23:22:21220func.func @and_zero_vector(%arg0: vector<4xi32>) -> vector<4xi32> {
Mogballa54f4ea2021-10-12 23:14:57221 // CHECK-NEXT: %cst = arith.constant dense<0> : vector<4xi32>
222 %cst = arith.constant dense<0> : vector<4xi32>
Stephan Herhuta8a5c0692019-04-08 07:00:46223 // CHECK-NEXT: return %cst
Mogballa54f4ea2021-10-12 23:14:57224 %1 = arith.andi %arg0, %cst : vector<4xi32>
Stephan Herhuta8a5c0692019-04-08 07:00:46225 return %1 : vector<4xi32>
226}
227
228//CHECK-LABEL: func @and_zero_tensor
River Riddlecda6aa72022-04-20 23:22:21229func.func @and_zero_tensor(%arg0: tensor<4x5xi32>) -> tensor<4x5xi32> {
Mogballa54f4ea2021-10-12 23:14:57230 // CHECK-NEXT: %cst = arith.constant dense<0> : tensor<4x5xi32>
231 %cst = arith.constant dense<0> : tensor<4x5xi32>
Stephan Herhuta8a5c0692019-04-08 07:00:46232 // CHECK-NEXT: return %cst
Mogballa54f4ea2021-10-12 23:14:57233 %1 = arith.andi %arg0, %cst : tensor<4x5xi32>
Stephan Herhuta8a5c0692019-04-08 07:00:46234 return %1 : tensor<4x5xi32>
235}
236
237//CHECK-LABEL: func @or_self
River Riddlecda6aa72022-04-20 23:22:21238func.func @or_self(%arg0: i32) -> i32 {
Stephan Herhuta8a5c0692019-04-08 07:00:46239 //CHECK-NEXT: return %arg0
Mogballa54f4ea2021-10-12 23:14:57240 %1 = arith.ori %arg0, %arg0 : i32
Stephan Herhuta8a5c0692019-04-08 07:00:46241 return %1 : i32
242}
243
244//CHECK-LABEL: func @or_self_vector
River Riddlecda6aa72022-04-20 23:22:21245func.func @or_self_vector(%arg0: vector<4xi32>) -> vector<4xi32> {
Stephan Herhuta8a5c0692019-04-08 07:00:46246 //CHECK-NEXT: return %arg0
Mogballa54f4ea2021-10-12 23:14:57247 %1 = arith.ori %arg0, %arg0 : vector<4xi32>
Stephan Herhuta8a5c0692019-04-08 07:00:46248 return %1 : vector<4xi32>
249}
250
251//CHECK-LABEL: func @or_self_tensor
River Riddlecda6aa72022-04-20 23:22:21252func.func @or_self_tensor(%arg0: tensor<4x5xi32>) -> tensor<4x5xi32> {
Stephan Herhuta8a5c0692019-04-08 07:00:46253 //CHECK-NEXT: return %arg0
Mogballa54f4ea2021-10-12 23:14:57254 %1 = arith.ori %arg0, %arg0 : tensor<4x5xi32>
Stephan Herhuta8a5c0692019-04-08 07:00:46255 return %1 : tensor<4x5xi32>
256}
257
258//CHECK-LABEL: func @or_zero
River Riddlecda6aa72022-04-20 23:22:21259func.func @or_zero(%arg0: i32) -> i32 {
Mogballa54f4ea2021-10-12 23:14:57260 %c0_i32 = arith.constant 0 : i32
Stephan Herhuta8a5c0692019-04-08 07:00:46261 // CHECK-NEXT: return %arg0
Mogballa54f4ea2021-10-12 23:14:57262 %1 = arith.ori %arg0, %c0_i32 : i32
Stephan Herhuta8a5c0692019-04-08 07:00:46263 return %1 : i32
264}
265
Stephan Herhute04d4bf2019-11-11 10:33:49266//CHECK-LABEL: func @or_zero_index
River Riddlecda6aa72022-04-20 23:22:21267func.func @or_zero_index(%arg0: index) -> index {
Mogballa54f4ea2021-10-12 23:14:57268 %c0_index = arith.constant 0 : index
Stephan Herhute04d4bf2019-11-11 10:33:49269 // CHECK-NEXT: return %arg0
Mogballa54f4ea2021-10-12 23:14:57270 %1 = arith.ori %arg0, %c0_index : index
Stephan Herhute04d4bf2019-11-11 10:33:49271 return %1 : index
272}
273
Stephan Herhuta8a5c0692019-04-08 07:00:46274//CHECK-LABEL: func @or_zero_vector
River Riddlecda6aa72022-04-20 23:22:21275func.func @or_zero_vector(%arg0: vector<4xi32>) -> vector<4xi32> {
Stephan Herhuta8a5c0692019-04-08 07:00:46276 // CHECK-NEXT: return %arg0
Mogballa54f4ea2021-10-12 23:14:57277 %cst = arith.constant dense<0> : vector<4xi32>
278 %1 = arith.ori %arg0, %cst : vector<4xi32>
Stephan Herhuta8a5c0692019-04-08 07:00:46279 return %1 : vector<4xi32>
280}
281
282//CHECK-LABEL: func @or_zero_tensor
River Riddlecda6aa72022-04-20 23:22:21283func.func @or_zero_tensor(%arg0: tensor<4x5xi32>) -> tensor<4x5xi32> {
Stephan Herhuta8a5c0692019-04-08 07:00:46284 // CHECK-NEXT: return %arg0
Mogballa54f4ea2021-10-12 23:14:57285 %cst = arith.constant dense<0> : tensor<4x5xi32>
286 %1 = arith.ori %arg0, %cst : tensor<4x5xi32>
Stephan Herhuta8a5c0692019-04-08 07:00:46287 return %1 : tensor<4x5xi32>
288}
289
Uday Bondhugula1e39d322021-10-06 04:39:56290// CHECK-LABEL: func @or_all_ones
River Riddlecda6aa72022-04-20 23:22:21291func.func @or_all_ones(%arg0: i1, %arg1: i4) -> (i1, i4) {
Mogballa54f4ea2021-10-12 23:14:57292 // CHECK-DAG: %c-1_i4 = arith.constant -1 : i4
293 // CHECK-DAG: %true = arith.constant true
294 %c1_i1 = arith.constant 1 : i1
295 %c15 = arith.constant 15 : i4
Uday Bondhugula1e39d322021-10-06 04:39:56296 // CHECK-NEXT: return %true
Mogballa54f4ea2021-10-12 23:14:57297 %1 = arith.ori %arg0, %c1_i1 : i1
298 %2 = arith.ori %arg1, %c15 : i4
Uday Bondhugula1e39d322021-10-06 04:39:56299 return %1, %2 : i1, i4
300}
301
Stephan Herhutaf016ba2019-04-08 12:53:59302//CHECK-LABEL: func @xor_self
River Riddlecda6aa72022-04-20 23:22:21303func.func @xor_self(%arg0: i32) -> i32 {
Mogballa54f4ea2021-10-12 23:14:57304 //CHECK-NEXT: %c0_i32 = arith.constant 0
305 %1 = arith.xori %arg0, %arg0 : i32
Stephan Herhutaf016ba2019-04-08 12:53:59306 //CHECK-NEXT: return %c0_i32
307 return %1 : i32
308}
309
310//CHECK-LABEL: func @xor_self_vector
River Riddlecda6aa72022-04-20 23:22:21311func.func @xor_self_vector(%arg0: vector<4xi32>) -> vector<4xi32> {
Mogballa54f4ea2021-10-12 23:14:57312 //CHECK-NEXT: %cst = arith.constant dense<0> : vector<4xi32>
313 %1 = arith.xori %arg0, %arg0 : vector<4xi32>
Stephan Herhutaf016ba2019-04-08 12:53:59314 //CHECK-NEXT: return %cst
315 return %1 : vector<4xi32>
316}
317
318//CHECK-LABEL: func @xor_self_tensor
River Riddlecda6aa72022-04-20 23:22:21319func.func @xor_self_tensor(%arg0: tensor<4x5xi32>) -> tensor<4x5xi32> {
Mogballa54f4ea2021-10-12 23:14:57320 //CHECK-NEXT: %cst = arith.constant dense<0> : tensor<4x5xi32>
321 %1 = arith.xori %arg0, %arg0 : tensor<4x5xi32>
Stephan Herhutaf016ba2019-04-08 12:53:59322 //CHECK-NEXT: return %cst
323 return %1 : tensor<4x5xi32>
324}
325
Chris Lattnerbbf362b2019-01-02 18:20:00326// CHECK-LABEL: func @memref_cast_folding
River Riddlecda6aa72022-04-20 23:22:21327func.func @memref_cast_folding(%arg0: memref<4 x f32>, %arg1: f32) -> (f32, f32) {
Julian Grosse2310702021-02-10 12:53:11328 %0 = memref.cast %arg0 : memref<4xf32> to memref<?xf32>
Mogballa54f4ea2021-10-12 23:14:57329 // CHECK-NEXT: %c0 = arith.constant 0 : index
330 %c0 = arith.constant 0 : index
Julian Grosse2310702021-02-10 12:53:11331 %dim = memref.dim %0, %c0 : memref<? x f32>
Uday Bondhugula1e6a93b2019-09-14 01:18:21332
Uday Bondhugulabd7de6d2019-09-17 18:49:14333 // CHECK-NEXT: affine.load %arg0[3]
Tres Poppf66c8762020-04-27 14:40:00334 %1 = affine.load %0[%dim - 1] : memref<?xf32>
Chris Lattner301f83f2018-10-23 17:12:00335
Julian Grosse2310702021-02-10 12:53:11336 // CHECK-NEXT: memref.store %arg1, %arg0[%c0] : memref<4xf32>
337 memref.store %arg1, %0[%c0] : memref<?xf32>
Chris Lattner301f83f2018-10-23 17:12:00338
Julian Grosse2310702021-02-10 12:53:11339 // CHECK-NEXT: %{{.*}} = memref.load %arg0[%c0] : memref<4xf32>
340 %2 = memref.load %0[%c0] : memref<?xf32>
Chris Lattner301f83f2018-10-23 17:12:00341
Julian Grosse2310702021-02-10 12:53:11342 // CHECK-NEXT: memref.dealloc %arg0 : memref<4xf32>
343 memref.dealloc %0: memref<?xf32>
Chris Lattner301f83f2018-10-23 17:12:00344
Uday Bondhugula1e6a93b2019-09-14 01:18:21345 // CHECK-NEXT: return %{{.*}}
Tres Poppf66c8762020-04-27 14:40:00346 return %1, %2 : f32, f32
Chris Lattner301f83f2018-10-23 17:12:00347}
348
Alex Zinenko0c782c22020-11-06 09:20:08349// CHECK-LABEL: @fold_memref_cast_in_memref_cast
350// CHECK-SAME: (%[[ARG0:.*]]: memref<42x42xf64>)
River Riddlecda6aa72022-04-20 23:22:21351func.func @fold_memref_cast_in_memref_cast(%0: memref<42x42xf64>) {
Julian Grosse2310702021-02-10 12:53:11352 // CHECK: %[[folded:.*]] = memref.cast %[[ARG0]] : memref<42x42xf64> to memref<?x?xf64>
353 %4 = memref.cast %0 : memref<42x42xf64> to memref<?x42xf64>
354 // CHECK-NOT: memref.cast
355 %5 = memref.cast %4 : memref<?x42xf64> to memref<?x?xf64>
Alex Zinenko0c782c22020-11-06 09:20:08356 // CHECK: "test.user"(%[[folded]])
357 "test.user"(%5) : (memref<?x?xf64>) -> ()
358 return
359}
360
361// CHECK-LABEL: @fold_memref_cast_chain
362// CHECK-SAME: (%[[ARG0:.*]]: memref<42x42xf64>)
River Riddlecda6aa72022-04-20 23:22:21363func.func @fold_memref_cast_chain(%0: memref<42x42xf64>) {
Julian Grosse2310702021-02-10 12:53:11364 // CHECK-NOT: memref.cast
365 %4 = memref.cast %0 : memref<42x42xf64> to memref<?x42xf64>
366 %5 = memref.cast %4 : memref<?x42xf64> to memref<42x42xf64>
Alex Zinenko0c782c22020-11-06 09:20:08367 // CHECK: "test.user"(%[[ARG0]])
368 "test.user"(%5) : (memref<42x42xf64>) -> ()
369 return
370}
371
River Riddleada685f2019-01-16 19:40:37372// CHECK-LABEL: func @dead_alloc_fold
River Riddlecda6aa72022-04-20 23:22:21373func.func @dead_alloc_fold() {
River Riddleada685f2019-01-16 19:40:37374 // CHECK-NEXT: return
Mogballa54f4ea2021-10-12 23:14:57375 %c4 = arith.constant 4 : index
Julian Grosse2310702021-02-10 12:53:11376 %a = memref.alloc(%c4) : memref<?xf32>
River Riddleada685f2019-01-16 19:40:37377 return
378}
Chris Lattner301f83f2018-10-23 17:12:00379
River Riddle5843e5a2019-01-16 20:39:03380// CHECK-LABEL: func @dead_dealloc_fold
River Riddlecda6aa72022-04-20 23:22:21381func.func @dead_dealloc_fold() {
River Riddle5843e5a2019-01-16 20:39:03382 // CHECK-NEXT: return
Julian Grosse2310702021-02-10 12:53:11383 %a = memref.alloc() : memref<4xf32>
384 memref.dealloc %a: memref<4xf32>
River Riddle5843e5a2019-01-16 20:39:03385 return
386}
387
388// CHECK-LABEL: func @dead_dealloc_fold_multi_use
River Riddlecda6aa72022-04-20 23:22:21389func.func @dead_dealloc_fold_multi_use(%cond : i1) {
Mehdi Aminia5062792024-06-14 20:38:56390 // CHECK-NOT: alloc
Julian Grosse2310702021-02-10 12:53:11391 %a = memref.alloc() : memref<4xf32>
Mehdi Aminia5062792024-06-14 20:38:56392 // CHECK: cond_br
River Riddleace01602022-02-04 04:59:43393 cf.cond_br %cond, ^bb1, ^bb2
River Riddle5843e5a2019-01-16 20:39:03394
River Riddle5843e5a2019-01-16 20:39:03395^bb1:
Mehdi Aminia5062792024-06-14 20:38:56396 // CHECK-NOT: alloc
Julian Grosse2310702021-02-10 12:53:11397 memref.dealloc %a: memref<4xf32>
Mehdi Aminia5062792024-06-14 20:38:56398 // CHECK: return
River Riddle5843e5a2019-01-16 20:39:03399 return
400
River Riddle5843e5a2019-01-16 20:39:03401^bb2:
Mehdi Aminia5062792024-06-14 20:38:56402 // CHECK-NOT: alloc
Julian Grosse2310702021-02-10 12:53:11403 memref.dealloc %a: memref<4xf32>
Mehdi Aminia5062792024-06-14 20:38:56404 // CHECK: return
River Riddle5843e5a2019-01-16 20:39:03405 return
406}
407
Butyginf22d3812021-04-10 16:38:11408// CHECK-LABEL: func @write_only_alloc_fold
River Riddlecda6aa72022-04-20 23:22:21409func.func @write_only_alloc_fold(%v: f32) {
Butyginf22d3812021-04-10 16:38:11410 // CHECK-NEXT: return
Mogballa54f4ea2021-10-12 23:14:57411 %c0 = arith.constant 0 : index
412 %c4 = arith.constant 4 : index
Butyginf22d3812021-04-10 16:38:11413 %a = memref.alloc(%c4) : memref<?xf32>
414 memref.store %v, %a[%c0] : memref<?xf32>
415 memref.dealloc %a: memref<?xf32>
416 return
417}
418
419// CHECK-LABEL: func @write_only_alloca_fold
River Riddlecda6aa72022-04-20 23:22:21420func.func @write_only_alloca_fold(%v: f32) {
Butyginf22d3812021-04-10 16:38:11421 // CHECK-NEXT: return
Mogballa54f4ea2021-10-12 23:14:57422 %c0 = arith.constant 0 : index
423 %c4 = arith.constant 4 : index
Butyginf22d3812021-04-10 16:38:11424 %a = memref.alloca(%c4) : memref<?xf32>
425 memref.store %v, %a[%c0] : memref<?xf32>
426 return
427}
428
River Riddlea32f0dc2019-10-30 18:18:51429// CHECK-LABEL: func @dead_block_elim
River Riddlecda6aa72022-04-20 23:22:21430func.func @dead_block_elim() {
Christian Sigg06ddb792020-04-08 06:59:59431 // CHECK-NOT: ^bb
Joshua Cao7d055af2024-02-06 06:59:03432 builtin.module {
433 func.func @nested() {
434 return
River Riddlea32f0dc2019-10-30 18:18:51435
Joshua Cao7d055af2024-02-06 06:59:03436 ^bb1:
437 return
438 }
River Riddlea32f0dc2019-10-30 18:18:51439 }
440 return
River Riddlea32f0dc2019-10-30 18:18:51441}
442
Chris Lattnerbbf362b2019-01-02 18:20:00443// CHECK-LABEL: func @dyn_shape_fold(%arg0: index, %arg1: index)
River Riddlecda6aa72022-04-20 23:22:21444func.func @dyn_shape_fold(%L : index, %M : index) -> (memref<4 x ? x 8 x ? x ? x f32>, memref<? x ? x i32>, memref<? x ? x f32>, memref<4 x ? x 8 x ? x ? x f32>) {
Mogballa54f4ea2021-10-12 23:14:57445 // CHECK: %c0 = arith.constant 0 : index
446 %zero = arith.constant 0 : index
Chris Lattner301f83f2018-10-23 17:12:00447 // The constants below disappear after they propagate into shapes.
Mogballa54f4ea2021-10-12 23:14:57448 %nine = arith.constant 9 : index
449 %N = arith.constant 1024 : index
450 %K = arith.constant 512 : index
Chris Lattner301f83f2018-10-23 17:12:00451
Butyginf22d3812021-04-10 16:38:11452 // CHECK: memref.alloc(%arg0) : memref<?x1024xf32>
Julian Grosse2310702021-02-10 12:53:11453 %a = memref.alloc(%L, %N) : memref<? x ? x f32>
Chris Lattner301f83f2018-10-23 17:12:00454
Butyginf22d3812021-04-10 16:38:11455 // CHECK: memref.alloc(%arg1) : memref<4x1024x8x512x?xf32>
Julian Grosse2310702021-02-10 12:53:11456 %b = memref.alloc(%N, %K, %M) : memref<4 x ? x 8 x ? x ? x f32>
Chris Lattner301f83f2018-10-23 17:12:00457
Butyginf22d3812021-04-10 16:38:11458 // CHECK: memref.alloc() : memref<512x1024xi32>
Julian Grosse2310702021-02-10 12:53:11459 %c = memref.alloc(%K, %N) : memref<? x ? x i32>
Uday Bondhugula03282172018-11-20 21:39:35460
Julian Grosse2310702021-02-10 12:53:11461 // CHECK: memref.alloc() : memref<9x9xf32>
462 %d = memref.alloc(%nine, %nine) : memref<? x ? x f32>
Uday Bondhugula7023f4b2020-03-22 15:50:21463
Julian Grosse2310702021-02-10 12:53:11464 // CHECK: memref.alloca(%arg1) : memref<4x1024x8x512x?xf32>
465 %e = memref.alloca(%N, %K, %M) : memref<4 x ? x 8 x ? x ? x f32>
Uday Bondhugula7023f4b2020-03-22 15:50:21466
River Riddle89bc4492019-07-09 17:40:29467 // CHECK: affine.for
River Riddle832567b2019-03-25 17:14:34468 affine.for %i = 0 to %L {
River Riddle89bc4492019-07-09 17:40:29469 // CHECK-NEXT: affine.for
River Riddle832567b2019-03-25 17:14:34470 affine.for %j = 0 to 10 {
Nicolas Vasilache435debe2022-09-30 15:20:29471 // CHECK-NEXT: memref.load %{{.*}}[%arg2, %arg3] : memref<?x1024xf32>
472 // CHECK-NEXT: memref.store %{{.*}}, %{{.*}}[%c0, %c0, %arg2, %arg3, %c0] : memref<4x1024x8x512x?xf32>
Julian Grosse2310702021-02-10 12:53:11473 %v = memref.load %a[%i, %j] : memref<?x?xf32>
474 memref.store %v, %b[%zero, %zero, %i, %j, %zero] : memref<4x?x8x?x?xf32>
Chris Lattner301f83f2018-10-23 17:12:00475 }
476 }
477
Butyginf22d3812021-04-10 16:38:11478 return %b, %c, %d, %e : memref<4 x ? x 8 x ? x ? x f32>, memref<? x ? x i32>, memref<? x ? x f32>, memref<4 x ? x 8 x ? x ? x f32>
Chris Lattner301f83f2018-10-23 17:12:00479}
Chris Lattnerbd01f952018-10-24 16:22:48480
Nicolas Vasilache8b78c502020-05-15 17:45:06481// CHECK-LABEL: func @dim_op_fold(
482// CHECK-SAME: %[[ARG0:[a-z0-9]*]]: index
483// CHECK-SAME: %[[ARG1:[a-z0-9]*]]: index
484// CHECK-SAME: %[[ARG2:[a-z0-9]*]]: index
485// CHECK-SAME: %[[BUF:[a-z0-9]*]]: memref<?xi8>
River Riddlecda6aa72022-04-20 23:22:21486func.func @dim_op_fold(%arg0: index, %arg1: index, %arg2: index, %BUF: memref<?xi8>, %M : index, %N : index, %K : index) {
Uday Bondhugula3ade6a72019-12-06 13:59:06487// CHECK-SAME: [[M:arg[0-9]+]]: index
488// CHECK-SAME: [[N:arg[0-9]+]]: index
489// CHECK-SAME: [[K:arg[0-9]+]]: index
Mogballa54f4ea2021-10-12 23:14:57490 %c0 = arith.constant 0 : index
491 %c1 = arith.constant 1 : index
492 %c2 = arith.constant 2 : index
Julian Grosse2310702021-02-10 12:53:11493 %0 = memref.alloc(%arg0, %arg1) : memref<?x?xf32>
494 %1 = memref.alloc(%arg1, %arg2) : memref<?x8x?xf32>
495 %2 = memref.dim %1, %c2 : memref<?x8x?xf32>
Uday Bondhugula3ade6a72019-12-06 13:59:06496 affine.for %arg3 = 0 to %2 {
Julian Grosse2310702021-02-10 12:53:11497 %3 = memref.alloc(%arg0) : memref<?xi8>
498 %ub = memref.dim %3, %c0 : memref<?xi8>
Uday Bondhugula3ade6a72019-12-06 13:59:06499 affine.for %arg4 = 0 to %ub {
Julian Grosse2310702021-02-10 12:53:11500 %s = memref.dim %0, %c0 : memref<?x?xf32>
501 %v = memref.view %3[%c0][%arg4, %s] : memref<?xi8> to memref<?x?xf32>
Alex Zinenkof3fae032022-09-16 13:36:40502 %sv = memref.subview %0[%c0, %c0][%s,%arg4][%c1,%c1] : memref<?x?xf32> to memref<?x?xf32, strided<[?, ?], offset: ?>>
Julian Grosse2310702021-02-10 12:53:11503 %l = memref.dim %v, %c1 : memref<?x?xf32>
Alex Zinenkof3fae032022-09-16 13:36:40504 %u = memref.dim %sv, %c0 : memref<?x?xf32, strided<[?, ?], offset: ?>>
Uday Bondhugula3ade6a72019-12-06 13:59:06505 affine.for %arg5 = %l to %u {
506 "foo"() : () -> ()
507 }
Matthias Springer6dc8de72024-06-23 17:05:00508 %sv2 = memref.subview %0[0, 0][17, %arg4][1, 1] : memref<?x?xf32> to memref<17x?xf32, strided<[?, 1]>>
Julian Grosse2310702021-02-10 12:53:11509 %l2 = memref.dim %v, %c1 : memref<?x?xf32>
Matthias Springer6dc8de72024-06-23 17:05:00510 %u2 = memref.dim %sv2, %c1 : memref<17x?xf32, strided<[?, 1]>>
Nicolas Vasilache8b78c502020-05-15 17:45:06511 scf.for %arg5 = %l2 to %u2 step %c1 {
512 "foo"() : () -> ()
513 }
Uday Bondhugula3ade6a72019-12-06 13:59:06514 }
515 }
Nicolas Vasilache8b78c502020-05-15 17:45:06516 // CHECK: affine.for %[[I:.*]] = 0 to %[[ARG2]] {
517 // CHECK-NEXT: affine.for %[[J:.*]] = 0 to %[[ARG0]] {
518 // CHECK-NEXT: affine.for %[[K:.*]] = %[[ARG0]] to %[[ARG0]] {
519 // CHECK-NEXT: "foo"() : () -> ()
520 // CHECK-NEXT: }
521 // CHECK-NEXT: scf.for %[[KK:.*]] = %[[ARG0]] to %[[J]] step %{{.*}} {
Uday Bondhugula3ade6a72019-12-06 13:59:06522 // CHECK-NEXT: "foo"() : () -> ()
523 // CHECK-NEXT: }
524 // CHECK-NEXT: }
525 // CHECK-NEXT: }
526
Julian Grosse2310702021-02-10 12:53:11527 %A = memref.view %BUF[%c0][%M, %K] : memref<?xi8> to memref<?x?xf32>
528 %B = memref.view %BUF[%c0][%K, %N] : memref<?xi8> to memref<?x?xf32>
529 %C = memref.view %BUF[%c0][%M, %N] : memref<?xi8> to memref<?x?xf32>
Uday Bondhugula3ade6a72019-12-06 13:59:06530
Julian Grosse2310702021-02-10 12:53:11531 %M_ = memref.dim %A, %c0 : memref<?x?xf32>
532 %K_ = memref.dim %A, %c1 : memref<?x?xf32>
533 %N_ = memref.dim %C, %c1 : memref<?x?xf32>
Alex Zinenko60f443b2020-05-13 10:12:30534 scf.for %i = %c0 to %M_ step %c1 {
535 scf.for %j = %c0 to %N_ step %c1 {
536 scf.for %k = %c0 to %K_ step %c1 {
Uday Bondhugula3ade6a72019-12-06 13:59:06537 }
538 }
539 }
River Riddle0ddba0b2020-03-12 21:06:41540 // CHECK-NEXT: return
Uday Bondhugula3ade6a72019-12-06 13:59:06541 return
542}
543
Chris Lattnerbbf362b2019-01-02 18:20:00544// CHECK-LABEL: func @merge_constants
River Riddlecda6aa72022-04-20 23:22:21545func.func @merge_constants() -> (index, index) {
Mogballa54f4ea2021-10-12 23:14:57546 // CHECK-NEXT: %c42 = arith.constant 42 : index
547 %0 = arith.constant 42 : index
548 %1 = arith.constant 42 : index
Chris Lattnerbd01f952018-10-24 16:22:48549 // CHECK-NEXT: return %c42, %c42
550 return %0, %1: index, index
551}
552
Chris Lattnerbbf362b2019-01-02 18:20:00553// CHECK-LABEL: func @hoist_constant
River Riddlecda6aa72022-04-20 23:22:21554func.func @hoist_constant(%arg0: memref<8xi32>) {
Mogballa54f4ea2021-10-12 23:14:57555 // CHECK-NEXT: %c42_i32 = arith.constant 42 : i32
River Riddle89bc4492019-07-09 17:40:29556 // CHECK-NEXT: affine.for %arg1 = 0 to 8 {
557 affine.for %arg1 = 0 to 8 {
Julian Grosse2310702021-02-10 12:53:11558 // CHECK-NEXT: memref.store %c42_i32, %arg0[%arg1]
Mogballa54f4ea2021-10-12 23:14:57559 %c42_i32 = arith.constant 42 : i32
Julian Grosse2310702021-02-10 12:53:11560 memref.store %c42_i32, %arg0[%arg1] : memref<8xi32>
Chris Lattnerbd01f952018-10-24 16:22:48561 }
562 return
563}
Chris Lattner967d9342018-10-26 05:04:35564
Chris Lattnerbbf362b2019-01-02 18:20:00565// CHECK-LABEL: func @const_fold_propagate
River Riddlecda6aa72022-04-20 23:22:21566func.func @const_fold_propagate() -> memref<?x?xf32> {
Mogballa54f4ea2021-10-12 23:14:57567 %VT_i = arith.constant 512 : index
Chris Lattner967d9342018-10-26 05:04:35568
River Riddle4268e4f2020-01-13 21:12:37569 %VT_i_s = affine.apply affine_map<(d0) -> (d0 floordiv 8)> (%VT_i)
570 %VT_k_l = affine.apply affine_map<(d0) -> (d0 floordiv 16)> (%VT_i)
Chris Lattner967d9342018-10-26 05:04:35571
Julian Grosse2310702021-02-10 12:53:11572 // CHECK: = memref.alloc() : memref<64x32xf32>
573 %Av = memref.alloc(%VT_i_s, %VT_k_l) : memref<?x?xf32>
Chris Lattner967d9342018-10-26 05:04:35574 return %Av : memref<?x?xf32>
Chris Lattner7983bbc2019-01-04 15:23:28575}
River Riddleed26dd02019-01-14 21:23:18576
River Riddle99411122019-01-30 02:08:28577// CHECK-LABEL: func @indirect_call_folding
River Riddlecda6aa72022-04-20 23:22:21578func.func @indirect_target() {
River Riddle99411122019-01-30 02:08:28579 return
580}
581
River Riddlecda6aa72022-04-20 23:22:21582func.func @indirect_call_folding() {
River Riddle99411122019-01-30 02:08:28583 // CHECK-NEXT: call @indirect_target() : () -> ()
584 // CHECK-NEXT: return
585 %indirect_fn = constant @indirect_target : () -> ()
586 call_indirect %indirect_fn() : () -> ()
587 return
588}
River Riddlea8866252019-02-07 16:26:31589
Stephan Herhut9c7bceb2019-11-15 11:59:57590//
River Riddlea8866252019-02-07 16:26:31591// IMPORTANT NOTE: the operations in this test are exactly those produced by
River Riddle4268e4f2020-01-13 21:12:37592// lowering affine.apply affine_map<(i) -> (i mod 42)> to standard operations. Please only
River Riddlea8866252019-02-07 16:26:31593// change these operations together with the affine lowering pass tests.
Stephan Herhut9c7bceb2019-11-15 11:59:57594//
River Riddlea8866252019-02-07 16:26:31595// CHECK-LABEL: @lowered_affine_mod
River Riddlecda6aa72022-04-20 23:22:21596func.func @lowered_affine_mod() -> (index, index) {
Mogballa54f4ea2021-10-12 23:14:57597// CHECK-DAG: {{.*}} = arith.constant 1 : index
598// CHECK-DAG: {{.*}} = arith.constant 41 : index
599 %c-43 = arith.constant -43 : index
600 %c42 = arith.constant 42 : index
601 %0 = arith.remsi %c-43, %c42 : index
602 %c0 = arith.constant 0 : index
603 %1 = arith.cmpi slt, %0, %c0 : index
604 %2 = arith.addi %0, %c42 : index
River Riddledec8af72022-01-31 20:44:35605 %3 = arith.select %1, %2, %0 : index
Mogballa54f4ea2021-10-12 23:14:57606 %c43 = arith.constant 43 : index
607 %c42_0 = arith.constant 42 : index
608 %4 = arith.remsi %c43, %c42_0 : index
609 %c0_1 = arith.constant 0 : index
610 %5 = arith.cmpi slt, %4, %c0_1 : index
611 %6 = arith.addi %4, %c42_0 : index
River Riddledec8af72022-01-31 20:44:35612 %7 = arith.select %5, %6, %4 : index
River Riddlea8866252019-02-07 16:26:31613 return %3, %7 : index, index
614}
615
Stephan Herhut9c7bceb2019-11-15 11:59:57616//
River Riddlea8866252019-02-07 16:26:31617// IMPORTANT NOTE: the operations in this test are exactly those produced by
River Riddle4268e4f2020-01-13 21:12:37618// lowering affine.apply affine_map<(i) -> (i mod 42)> to standard operations. Please only
River Riddlea8866252019-02-07 16:26:31619// change these operations together with the affine lowering pass tests.
Stephan Herhut9c7bceb2019-11-15 11:59:57620//
River Riddlea8866252019-02-07 16:26:31621// CHECK-LABEL: func @lowered_affine_floordiv
River Riddlecda6aa72022-04-20 23:22:21622func.func @lowered_affine_floordiv() -> (index, index) {
Mogballa54f4ea2021-10-12 23:14:57623// CHECK-DAG: %c1 = arith.constant 1 : index
624// CHECK-DAG: %c-2 = arith.constant -2 : index
625 %c-43 = arith.constant -43 : index
626 %c42 = arith.constant 42 : index
627 %c0 = arith.constant 0 : index
628 %c-1 = arith.constant -1 : index
629 %0 = arith.cmpi slt, %c-43, %c0 : index
630 %1 = arith.subi %c-1, %c-43 : index
River Riddledec8af72022-01-31 20:44:35631 %2 = arith.select %0, %1, %c-43 : index
Mogballa54f4ea2021-10-12 23:14:57632 %3 = arith.divsi %2, %c42 : index
633 %4 = arith.subi %c-1, %3 : index
River Riddledec8af72022-01-31 20:44:35634 %5 = arith.select %0, %4, %3 : index
Mogballa54f4ea2021-10-12 23:14:57635 %c43 = arith.constant 43 : index
636 %c42_0 = arith.constant 42 : index
637 %c0_1 = arith.constant 0 : index
638 %c-1_2 = arith.constant -1 : index
639 %6 = arith.cmpi slt, %c43, %c0_1 : index
640 %7 = arith.subi %c-1_2, %c43 : index
River Riddledec8af72022-01-31 20:44:35641 %8 = arith.select %6, %7, %c43 : index
Mogballa54f4ea2021-10-12 23:14:57642 %9 = arith.divsi %8, %c42_0 : index
643 %10 = arith.subi %c-1_2, %9 : index
River Riddledec8af72022-01-31 20:44:35644 %11 = arith.select %6, %10, %9 : index
River Riddlea8866252019-02-07 16:26:31645 return %5, %11 : index, index
646}
647
Stephan Herhut9c7bceb2019-11-15 11:59:57648//
River Riddlea8866252019-02-07 16:26:31649// IMPORTANT NOTE: the operations in this test are exactly those produced by
River Riddle4268e4f2020-01-13 21:12:37650// lowering affine.apply affine_map<(i) -> (i mod 42)> to standard operations. Please only
River Riddlea8866252019-02-07 16:26:31651// change these operations together with the affine lowering pass tests.
Stephan Herhut9c7bceb2019-11-15 11:59:57652//
River Riddlea8866252019-02-07 16:26:31653// CHECK-LABEL: func @lowered_affine_ceildiv
River Riddlecda6aa72022-04-20 23:22:21654func.func @lowered_affine_ceildiv() -> (index, index) {
Mogballa54f4ea2021-10-12 23:14:57655// CHECK-DAG: %c-1 = arith.constant -1 : index
656 %c-43 = arith.constant -43 : index
657 %c42 = arith.constant 42 : index
658 %c0 = arith.constant 0 : index
659 %c1 = arith.constant 1 : index
660 %0 = arith.cmpi sle, %c-43, %c0 : index
661 %1 = arith.subi %c0, %c-43 : index
662 %2 = arith.subi %c-43, %c1 : index
River Riddledec8af72022-01-31 20:44:35663 %3 = arith.select %0, %1, %2 : index
Mogballa54f4ea2021-10-12 23:14:57664 %4 = arith.divsi %3, %c42 : index
665 %5 = arith.subi %c0, %4 : index
666 %6 = arith.addi %4, %c1 : index
River Riddledec8af72022-01-31 20:44:35667 %7 = arith.select %0, %5, %6 : index
Mogballa54f4ea2021-10-12 23:14:57668// CHECK-DAG: %c2 = arith.constant 2 : index
669 %c43 = arith.constant 43 : index
670 %c42_0 = arith.constant 42 : index
671 %c0_1 = arith.constant 0 : index
672 %c1_2 = arith.constant 1 : index
673 %8 = arith.cmpi sle, %c43, %c0_1 : index
674 %9 = arith.subi %c0_1, %c43 : index
675 %10 = arith.subi %c43, %c1_2 : index
River Riddledec8af72022-01-31 20:44:35676 %11 = arith.select %8, %9, %10 : index
Mogballa54f4ea2021-10-12 23:14:57677 %12 = arith.divsi %11, %c42_0 : index
678 %13 = arith.subi %c0_1, %12 : index
679 %14 = arith.addi %12, %c1_2 : index
River Riddledec8af72022-01-31 20:44:35680 %15 = arith.select %8, %13, %14 : index
Chris Lattnera004da02021-05-24 22:45:58681
682 // CHECK-NEXT: return %c-1, %c2
River Riddlea8866252019-02-07 16:26:31683 return %7, %15 : index, index
684}
Smit Hinsuc9b05402019-04-28 03:55:38685
686// Checks that NOP casts are removed.
687// CHECK-LABEL: cast_values
River Riddlecda6aa72022-04-20 23:22:21688func.func @cast_values(%arg0: memref<?xi32>) -> memref<2xi32> {
Sean Silva129d6e52020-12-16 00:47:19689 // NOP cast
Julian Grosse2310702021-02-10 12:53:11690 %1 = memref.cast %arg0 : memref<?xi32> to memref<?xi32>
691 // CHECK-NEXT: %[[RET:.*]] = memref.cast %arg0 : memref<?xi32> to memref<2xi32>
692 %3 = memref.cast %1 : memref<?xi32> to memref<2xi32>
Sean Silva129d6e52020-12-16 00:47:19693 // NOP cast
Julian Grosse2310702021-02-10 12:53:11694 %5 = memref.cast %3 : memref<2xi32> to memref<2xi32>
Sean Silva129d6e52020-12-16 00:47:19695 // CHECK-NEXT: return %[[RET]] : memref<2xi32>
696 return %5 : memref<2xi32>
Smit Hinsuc9b05402019-04-28 03:55:38697}
Andy Davis5fbdb672019-11-07 16:04:33698
Stephan Herhut9c7bceb2019-11-15 11:59:57699// -----
700
Andy Davis5fbdb672019-11-07 16:04:33701// CHECK-LABEL: func @view
River Riddlecda6aa72022-04-20 23:22:21702func.func @view(%arg0 : index) -> (f32, f32, f32, f32) {
Mogballa54f4ea2021-10-12 23:14:57703 // CHECK: %[[C15:.*]] = arith.constant 15 : index
Julian Grosse2310702021-02-10 12:53:11704 // CHECK: %[[ALLOC_MEM:.*]] = memref.alloc() : memref<2048xi8>
705 %0 = memref.alloc() : memref<2048xi8>
Mogballa54f4ea2021-10-12 23:14:57706 %c0 = arith.constant 0 : index
707 %c7 = arith.constant 7 : index
708 %c11 = arith.constant 11 : index
709 %c15 = arith.constant 15 : index
Andy Davis5fbdb672019-11-07 16:04:33710
Nicolas Vasilache6ed61a22020-05-11 16:09:18711 // Test: fold constant sizes.
Julian Grosse2310702021-02-10 12:53:11712 // CHECK: memref.view %[[ALLOC_MEM]][%[[C15]]][] : memref<2048xi8> to memref<7x11xf32>
713 %1 = memref.view %0[%c15][%c7, %c11] : memref<2048xi8> to memref<?x?xf32>
714 %r0 = memref.load %1[%c0, %c0] : memref<?x?xf32>
Andy Davis8f00b442019-11-07 18:19:54715
Nicolas Vasilache6ed61a22020-05-11 16:09:18716 // Test: fold one constant size.
Julian Grosse2310702021-02-10 12:53:11717 // CHECK: memref.view %[[ALLOC_MEM]][%[[C15]]][%arg0, %arg0] : memref<2048xi8> to memref<?x?x7xf32>
718 %2 = memref.view %0[%c15][%arg0, %arg0, %c7] : memref<2048xi8> to memref<?x?x?xf32>
719 %r1 = memref.load %2[%c0, %c0, %c0] : memref<?x?x?xf32>
Andy Davis8f00b442019-11-07 18:19:54720
Nicolas Vasilache6ed61a22020-05-11 16:09:18721 // Test: preserve an existing static size.
Julian Grosse2310702021-02-10 12:53:11722 // CHECK: memref.view %[[ALLOC_MEM]][%[[C15]]][] : memref<2048xi8> to memref<7x4xf32>
723 %3 = memref.view %0[%c15][%c7] : memref<2048xi8> to memref<?x4xf32>
724 %r2 = memref.load %3[%c0, %c0] : memref<?x4xf32>
Andy Davis5fbdb672019-11-07 16:04:33725
Julian Grosse2310702021-02-10 12:53:11726 // Test: folding static alloc and memref.cast into a view.
klensya5985ca2024-06-14 15:16:02727 // CHECK: memref.view %[[ALLOC_MEM]][%[[C15]]][] : memref<2048xi8> to memref<15x7xf32>
Julian Grosse2310702021-02-10 12:53:11728 %4 = memref.cast %0 : memref<2048xi8> to memref<?xi8>
729 %5 = memref.view %4[%c15][%c15, %c7] : memref<?xi8> to memref<?x?xf32>
730 %r3 = memref.load %5[%c0, %c0] : memref<?x?xf32>
Nicolas Vasilache6ed61a22020-05-11 16:09:18731 return %r0, %r1, %r2, %r3 : f32, f32, f32, f32
Andy Davis5fbdb672019-11-07 16:04:33732}
Andy Davisa4669cd2019-11-14 20:22:28733
Stephan Herhut9c7bceb2019-11-15 11:59:57734// -----
735
Andy Davisa4669cd2019-11-14 20:22:28736// CHECK-LABEL: func @subview
Mahesh Ravishankar6db85302019-11-22 19:41:29737// CHECK-SAME: %[[ARG0:.*]]: index, %[[ARG1:.*]]: index
River Riddlecda6aa72022-04-20 23:22:21738func.func @subview(%arg0 : index, %arg1 : index) -> (index, index) {
Chris Lattnerb2f232b2021-03-20 00:57:47739 // Folded but reappears after subview folding into dim.
Mogballa54f4ea2021-10-12 23:14:57740 // CHECK-DAG: %[[C0:.*]] = arith.constant 0 : index
741 // CHECK-DAG: %[[C7:.*]] = arith.constant 7 : index
742 // CHECK-DAG: %[[C11:.*]] = arith.constant 11 : index
743 %c0 = arith.constant 0 : index
744 // CHECK-NOT: arith.constant 1 : index
745 %c1 = arith.constant 1 : index
746 // CHECK-NOT: arith.constant 2 : index
747 %c2 = arith.constant 2 : index
Nicolas Vasilache63c0e722020-05-12 21:17:34748 // Folded but reappears after subview folding into dim.
Mogballa54f4ea2021-10-12 23:14:57749 %c7 = arith.constant 7 : index
750 %c11 = arith.constant 11 : index
751 // CHECK-NOT: arith.constant 15 : index
752 %c15 = arith.constant 15 : index
Andy Davisa4669cd2019-11-14 20:22:28753
Julian Grosse2310702021-02-10 12:53:11754 // CHECK: %[[ALLOC0:.*]] = memref.alloc()
Matthias Springer5edf1272025-03-31 17:28:55755 %0 = memref.alloc() : memref<128x96x64xf32, strided<[6144, 64, 1], offset: 0>>
Andy Davisa4669cd2019-11-14 20:22:28756
757 // Test: subview with constant base memref and constant operands is folded.
Andy Davisa6a28732019-11-18 23:00:34758 // Note that the subview uses the base memrefs layout map because it used
759 // zero offset and unit stride arguments.
Julian Grosse2310702021-02-10 12:53:11760 // CHECK: memref.subview %[[ALLOC0]][0, 0, 0] [7, 11, 2] [1, 1, 1] :
Matthias Springer5edf1272025-03-31 17:28:55761 // CHECK-SAME: memref<128x96x64xf32, strided<[6144, 64, 1]>>
762 // CHECK-SAME: to memref<7x11x2xf32, strided<[6144, 64, 1]>>
Julian Grosse2310702021-02-10 12:53:11763 %1 = memref.subview %0[%c0, %c0, %c0] [%c7, %c11, %c2] [%c1, %c1, %c1]
Matthias Springer5edf1272025-03-31 17:28:55764 : memref<128x96x64xf32, strided<[6144, 64, 1], offset: 0>> to
Alex Zinenko519847f2022-08-30 08:23:57765 memref<?x?x?xf32, strided<[?, ?, ?], offset: ?>>
766 %v0 = memref.load %1[%c0, %c0, %c0] : memref<?x?x?xf32, strided<[?, ?, ?], offset: ?>>
Andy Davisa4669cd2019-11-14 20:22:28767
Nicolas Vasilache63c0e722020-05-12 21:17:34768 // Test: subview with one dynamic operand can also be folded.
Julian Grosse2310702021-02-10 12:53:11769 // CHECK: memref.subview %[[ALLOC0]][0, %[[ARG0]], 0] [7, 11, 15] [1, 1, 1] :
Matthias Springer5edf1272025-03-31 17:28:55770 // CHECK-SAME: memref<128x96x64xf32, strided<[6144, 64, 1]>>
771 // CHECK-SAME: to memref<7x11x15xf32, strided<[6144, 64, 1], offset: ?>>
Julian Grosse2310702021-02-10 12:53:11772 %2 = memref.subview %0[%c0, %arg0, %c0] [%c7, %c11, %c15] [%c1, %c1, %c1]
Matthias Springer5edf1272025-03-31 17:28:55773 : memref<128x96x64xf32, strided<[6144, 64, 1], offset: 0>> to
Alex Zinenko519847f2022-08-30 08:23:57774 memref<?x?x?xf32, strided<[?, ?, ?], offset: ?>>
775 memref.store %v0, %2[%c0, %c0, %c0] : memref<?x?x?xf32, strided<[?, ?, ?], offset: ?>>
Andy Davisa4669cd2019-11-14 20:22:28776
Julian Grosse2310702021-02-10 12:53:11777 // CHECK: %[[ALLOC1:.*]] = memref.alloc(%[[ARG0]])
Alex Zinenko519847f2022-08-30 08:23:57778 %3 = memref.alloc(%arg0) : memref<?x16x4xf32, strided<[64, 4, 1], offset: 0>>
Mahesh Ravishankar6db85302019-11-22 19:41:29779 // Test: subview with constant operands but dynamic base memref is folded as long as the strides and offset of the base memref are static.
Matthias Springer5edf1272025-03-31 17:28:55780 // CHECK: memref.subview %[[ALLOC1]][0, 0, 0] [7, 11, 2] [1, 1, 1] :
Alex Zinenko519847f2022-08-30 08:23:57781 // CHECK-SAME: memref<?x16x4xf32, strided<[64, 4, 1]>>
Matthias Springer5edf1272025-03-31 17:28:55782 // CHECK-SAME: to memref<7x11x2xf32, strided<[64, 4, 1]>>
783 %4 = memref.subview %3[%c0, %c0, %c0] [%c7, %c11, %c2] [%c1, %c1, %c1]
Alex Zinenko519847f2022-08-30 08:23:57784 : memref<?x16x4xf32, strided<[64, 4, 1], offset: 0>> to
785 memref<?x?x?xf32, strided<[?, ?, ?], offset: ?>>
786 memref.store %v0, %4[%c0, %c0, %c0] : memref<?x?x?xf32, strided<[?, ?, ?], offset: ?>>
Andy Davisa4669cd2019-11-14 20:22:28787
Uday Bondhugula3ade6a72019-12-06 13:59:06788 // Test: subview offset operands are folded correctly w.r.t. base strides.
Julian Grosse2310702021-02-10 12:53:11789 // CHECK: memref.subview %[[ALLOC0]][1, 2, 7] [7, 11, 2] [1, 1, 1] :
Matthias Springer5edf1272025-03-31 17:28:55790 // CHECK-SAME: memref<128x96x64xf32, strided<[6144, 64, 1]>> to
791 // CHECK-SAME: memref<7x11x2xf32, strided<[6144, 64, 1], offset: 6279>>
Julian Grosse2310702021-02-10 12:53:11792 %5 = memref.subview %0[%c1, %c2, %c7] [%c7, %c11, %c2] [%c1, %c1, %c1]
Matthias Springer5edf1272025-03-31 17:28:55793 : memref<128x96x64xf32, strided<[6144, 64, 1], offset: 0>> to
Alex Zinenko519847f2022-08-30 08:23:57794 memref<?x?x?xf32, strided<[?, ?, ?], offset: ?>>
795 memref.store %v0, %5[%c0, %c0, %c0] : memref<?x?x?xf32, strided<[?, ?, ?], offset: ?>>
Andy Davisa6a28732019-11-18 23:00:34796
797 // Test: subview stride operands are folded correctly w.r.t. base strides.
Julian Grosse2310702021-02-10 12:53:11798 // CHECK: memref.subview %[[ALLOC0]][0, 0, 0] [7, 11, 2] [2, 7, 11] :
Matthias Springer5edf1272025-03-31 17:28:55799 // CHECK-SAME: memref<128x96x64xf32, strided<[6144, 64, 1]>>
800 // CHECK-SAME: to memref<7x11x2xf32, strided<[12288, 448, 11]>>
Julian Grosse2310702021-02-10 12:53:11801 %6 = memref.subview %0[%c0, %c0, %c0] [%c7, %c11, %c2] [%c2, %c7, %c11]
Matthias Springer5edf1272025-03-31 17:28:55802 : memref<128x96x64xf32, strided<[6144, 64, 1], offset: 0>> to
Alex Zinenko519847f2022-08-30 08:23:57803 memref<?x?x?xf32, strided<[?, ?, ?], offset: ?>>
804 memref.store %v0, %6[%c0, %c0, %c0] : memref<?x?x?xf32, strided<[?, ?, ?], offset: ?>>
Andy Davisa6a28732019-11-18 23:00:34805
Mahesh Ravishankar6db85302019-11-22 19:41:29806 // Test: subview shape are folded, but offsets and strides are not even if base memref is static
Julian Grosse2310702021-02-10 12:53:11807 // CHECK: memref.subview %[[ALLOC0]][%[[ARG0]], %[[ARG0]], %[[ARG0]]] [7, 11, 2] [%[[ARG1]], %[[ARG1]], %[[ARG1]]] :
Matthias Springer5edf1272025-03-31 17:28:55808 // CHECK-SAME: memref<128x96x64xf32, strided<[6144, 64, 1]>> to
Alex Zinenko2791162b2022-09-15 16:29:14809 // CHECK-SAME: memref<7x11x2xf32, strided<[?, ?, ?], offset: ?>>
Julian Grosse2310702021-02-10 12:53:11810 %10 = memref.subview %0[%arg0, %arg0, %arg0] [%c7, %c11, %c2] [%arg1, %arg1, %arg1] :
Matthias Springer5edf1272025-03-31 17:28:55811 memref<128x96x64xf32, strided<[6144, 64, 1], offset: 0>> to
Alex Zinenko519847f2022-08-30 08:23:57812 memref<?x?x?xf32, strided<[?, ?, ?], offset: ?>>
Julian Grosse2310702021-02-10 12:53:11813 memref.store %v0, %10[%arg1, %arg1, %arg1] :
Alex Zinenko519847f2022-08-30 08:23:57814 memref<?x?x?xf32, strided<[?, ?, ?], offset: ?>>
Mahesh Ravishankar6db85302019-11-22 19:41:29815
816 // Test: subview strides are folded, but offsets and shape are not even if base memref is static
Julian Grosse2310702021-02-10 12:53:11817 // CHECK: memref.subview %[[ALLOC0]][%[[ARG0]], %[[ARG0]], %[[ARG0]]] [%[[ARG1]], %[[ARG1]], %[[ARG1]]] [2, 7, 11] :
Matthias Springer5edf1272025-03-31 17:28:55818 // CHECK-SAME: memref<128x96x64xf32, strided<[6144, 64, 1]>> to
819 // CHECK-SAME: memref<?x?x?xf32, strided<[12288, 448, 11], offset: ?>>
Julian Grosse2310702021-02-10 12:53:11820 %11 = memref.subview %0[%arg0, %arg0, %arg0] [%arg1, %arg1, %arg1] [%c2, %c7, %c11] :
Matthias Springer5edf1272025-03-31 17:28:55821 memref<128x96x64xf32, strided<[6144, 64, 1], offset: 0>> to
Alex Zinenko519847f2022-08-30 08:23:57822 memref<?x?x?xf32, strided<[?, ?, ?], offset: ?>>
Julian Grosse2310702021-02-10 12:53:11823 memref.store %v0, %11[%arg0, %arg0, %arg0] :
Alex Zinenko519847f2022-08-30 08:23:57824 memref<?x?x?xf32, strided<[?, ?, ?], offset: ?>>
Mahesh Ravishankar6db85302019-11-22 19:41:29825
826 // Test: subview offsets are folded, but strides and shape are not even if base memref is static
Julian Grosse2310702021-02-10 12:53:11827 // CHECK: memref.subview %[[ALLOC0]][1, 2, 7] [%[[ARG1]], %[[ARG1]], %[[ARG1]]] [%[[ARG0]], %[[ARG0]], %[[ARG0]]] :
Matthias Springer5edf1272025-03-31 17:28:55828 // CHECK-SAME: memref<128x96x64xf32, strided<[6144, 64, 1]>> to
829 // CHECK-SAME: memref<?x?x?xf32, strided<[?, ?, ?], offset: 6279>>
Julian Grosse2310702021-02-10 12:53:11830 %13 = memref.subview %0[%c1, %c2, %c7] [%arg1, %arg1, %arg1] [%arg0, %arg0, %arg0] :
Matthias Springer5edf1272025-03-31 17:28:55831 memref<128x96x64xf32, strided<[6144, 64, 1], offset: 0>> to
Alex Zinenko519847f2022-08-30 08:23:57832 memref<?x?x?xf32, strided<[?, ?, ?], offset: ?>>
Julian Grosse2310702021-02-10 12:53:11833 memref.store %v0, %13[%arg1, %arg1, %arg1] :
Alex Zinenko519847f2022-08-30 08:23:57834 memref<?x?x?xf32, strided<[?, ?, ?], offset: ?>>
Mahesh Ravishankar6db85302019-11-22 19:41:29835
Julian Grosse2310702021-02-10 12:53:11836 // CHECK: %[[ALLOC2:.*]] = memref.alloc(%[[ARG0]], %[[ARG0]], %[[ARG1]])
837 %14 = memref.alloc(%arg0, %arg0, %arg1) : memref<?x?x?xf32>
Mahesh Ravishankar6db85302019-11-22 19:41:29838 // Test: subview shape are folded, even if base memref is not static
Julian Grosse2310702021-02-10 12:53:11839 // CHECK: memref.subview %[[ALLOC2]][%[[ARG0]], %[[ARG0]], %[[ARG0]]] [7, 11, 2] [%[[ARG1]], %[[ARG1]], %[[ARG1]]] :
Nicolas Vasilache63c0e722020-05-12 21:17:34840 // CHECK-SAME: memref<?x?x?xf32> to
Alex Zinenko2791162b2022-09-15 16:29:14841 // CHECK-SAME: memref<7x11x2xf32, strided<[?, ?, ?], offset: ?>>
Julian Grosse2310702021-02-10 12:53:11842 %15 = memref.subview %14[%arg0, %arg0, %arg0] [%c7, %c11, %c2] [%arg1, %arg1, %arg1] :
Nicolas Vasilache63c0e722020-05-12 21:17:34843 memref<?x?x?xf32> to
Alex Zinenko519847f2022-08-30 08:23:57844 memref<?x?x?xf32, strided<[?, ?, ?], offset: ?>>
845 memref.store %v0, %15[%arg1, %arg1, %arg1] : memref<?x?x?xf32, strided<[?, ?, ?], offset: ?>>
Mahesh Ravishankar6db85302019-11-22 19:41:29846
Nicolas Vasilache63c0e722020-05-12 21:17:34847 // TEST: subview strides are folded, in the type only the most minor stride is folded.
Julian Grosse2310702021-02-10 12:53:11848 // CHECK: memref.subview %[[ALLOC2]][%[[ARG0]], %[[ARG0]], %[[ARG0]]] [%[[ARG1]], %[[ARG1]], %[[ARG1]]] [2, 2, 2] :
Nicolas Vasilache63c0e722020-05-12 21:17:34849 // CHECK-SAME: memref<?x?x?xf32> to
Alex Zinenko2791162b2022-09-15 16:29:14850 // CHECK-SAME: memref<?x?x?xf32, strided<[?, ?, 2], offset: ?>>
Julian Grosse2310702021-02-10 12:53:11851 %16 = memref.subview %14[%arg0, %arg0, %arg0] [%arg1, %arg1, %arg1] [%c2, %c2, %c2] :
Nicolas Vasilache63c0e722020-05-12 21:17:34852 memref<?x?x?xf32> to
Alex Zinenko519847f2022-08-30 08:23:57853 memref<?x?x?xf32, strided<[?, ?, ?], offset: ?>>
854 memref.store %v0, %16[%arg0, %arg0, %arg0] : memref<?x?x?xf32, strided<[?, ?, ?], offset: ?>>
Mahesh Ravishankar6db85302019-11-22 19:41:29855
Nicolas Vasilache63c0e722020-05-12 21:17:34856 // TEST: subview offsets are folded but the type offset remains dynamic, when the base memref is not static
Julian Grosse2310702021-02-10 12:53:11857 // CHECK: memref.subview %[[ALLOC2]][1, 1, 1] [%[[ARG0]], %[[ARG0]], %[[ARG0]]] [%[[ARG1]], %[[ARG1]], %[[ARG1]]] :
Nicolas Vasilache63c0e722020-05-12 21:17:34858 // CHECK-SAME: memref<?x?x?xf32> to
Alex Zinenko2791162b2022-09-15 16:29:14859 // CHECK-SAME: memref<?x?x?xf32, strided<[?, ?, ?], offset: ?>>
Julian Grosse2310702021-02-10 12:53:11860 %17 = memref.subview %14[%c1, %c1, %c1] [%arg0, %arg0, %arg0] [%arg1, %arg1, %arg1] :
Nicolas Vasilache63c0e722020-05-12 21:17:34861 memref<?x?x?xf32> to
Alex Zinenko519847f2022-08-30 08:23:57862 memref<?x?x?xf32, strided<[?, ?, ?], offset: ?>>
863 memref.store %v0, %17[%arg0, %arg0, %arg0] : memref<?x?x?xf32, strided<[?, ?, ?], offset: ?>>
Mahesh Ravishankar6db85302019-11-22 19:41:29864
Matthias Springer5edf1272025-03-31 17:28:55865 // CHECK: %[[ALLOC3:.*]] = memref.alloc() : memref<128x64xf32>
866 %18 = memref.alloc() : memref<128x64xf32>
Mogballa54f4ea2021-10-12 23:14:57867 %c4 = arith.constant 4 : index
Mahesh Ravishankar6db85302019-11-22 19:41:29868
869 // TEST: subview strides are maintained when sizes are folded
Julian Grosse2310702021-02-10 12:53:11870 // CHECK: memref.subview %[[ALLOC3]][%arg1, %arg1] [2, 4] [1, 1] :
Matthias Springer5edf1272025-03-31 17:28:55871 // CHECK-SAME: memref<128x64xf32> to
872 // CHECK-SAME: memref<2x4xf32, strided<[64, 1], offset: ?>
Julian Grosse2310702021-02-10 12:53:11873 %19 = memref.subview %18[%arg1, %arg1] [%c2, %c4] [1, 1] :
Matthias Springer5edf1272025-03-31 17:28:55874 memref<128x64xf32> to
875 memref<?x?xf32, strided<[64, 1], offset: ?>>
876 memref.store %v0, %19[%arg1, %arg1] : memref<?x?xf32, strided<[64, 1], offset: ?>>
Mahesh Ravishankar6db85302019-11-22 19:41:29877
878 // TEST: subview strides and sizes are maintained when offsets are folded
Julian Grosse2310702021-02-10 12:53:11879 // CHECK: memref.subview %[[ALLOC3]][2, 4] [12, 4] [1, 1] :
Matthias Springer5edf1272025-03-31 17:28:55880 // CHECK-SAME: memref<128x64xf32> to
881 // CHECK-SAME: memref<12x4xf32, strided<[64, 1], offset: 132>>
Julian Grosse2310702021-02-10 12:53:11882 %20 = memref.subview %18[%c2, %c4] [12, 4] [1, 1] :
Matthias Springer5edf1272025-03-31 17:28:55883 memref<128x64xf32> to
884 memref<12x4xf32, strided<[64, 1], offset: ?>>
885 memref.store %v0, %20[%arg1, %arg1] : memref<12x4xf32, strided<[64, 1], offset: ?>>
Mahesh Ravishankar6db85302019-11-22 19:41:29886
Andy Davisa6a28732019-11-18 23:00:34887 // Test: dim on subview is rewritten to size operand.
Alex Zinenko519847f2022-08-30 08:23:57888 %7 = memref.dim %4, %c0 : memref<?x?x?xf32, strided<[?, ?, ?], offset: ?>>
889 %8 = memref.dim %4, %c1 : memref<?x?x?xf32, strided<[?, ?, ?], offset: ?>>
Stephan Herhutf0f3b712019-11-18 12:31:02890
891 // CHECK: return %[[C7]], %[[C11]]
Andy Davisa6a28732019-11-18 23:00:34892 return %7, %8 : index, index
Andy Davisa4669cd2019-11-14 20:22:28893}
Stephen Neuendorffer12df4272019-12-05 00:15:10894
895// CHECK-LABEL: func @index_cast
896// CHECK-SAME: %[[ARG_0:arg[0-9]+]]: i16
River Riddlecda6aa72022-04-20 23:22:21897func.func @index_cast(%arg0: i16) -> (i16) {
Mogballa54f4ea2021-10-12 23:14:57898 %11 = arith.index_cast %arg0 : i16 to index
899 %12 = arith.index_cast %11 : index to i16
Stephen Neuendorffer12df4272019-12-05 00:15:10900 // CHECK: return %[[ARG_0]] : i16
901 return %12 : i16
902}
Stephen Neuendorffered566332019-12-10 19:59:13903
904// CHECK-LABEL: func @index_cast_fold
River Riddlecda6aa72022-04-20 23:22:21905func.func @index_cast_fold() -> (i16, index) {
Mogballa54f4ea2021-10-12 23:14:57906 %c4 = arith.constant 4 : index
907 %1 = arith.index_cast %c4 : index to i16
908 %c4_i16 = arith.constant 4 : i16
909 %2 = arith.index_cast %c4_i16 : i16 to index
910 // CHECK-DAG: %[[C4:.*]] = arith.constant 4 : index
911 // CHECK-DAG: %[[C4_I16:.*]] = arith.constant 4 : i16
Stephen Neuendorffered566332019-12-10 19:59:13912 // CHECK: return %[[C4_I16]], %[[C4]] : i16, index
913 return %1, %2 : i16, index
914}
Uday Bondhugula3f9cdd42020-03-30 19:45:40915
916// CHECK-LABEL: func @remove_dead_else
River Riddlecda6aa72022-04-20 23:22:21917func.func @remove_dead_else(%M : memref<100 x i32>) {
Uday Bondhugula3f9cdd42020-03-30 19:45:40918 affine.for %i = 0 to 100 {
919 affine.load %M[%i] : memref<100xi32>
920 affine.if affine_set<(d0) : (d0 - 2 >= 0)>(%i) {
921 affine.for %j = 0 to 100 {
Tres Poppf66c8762020-04-27 14:40:00922 %1 = affine.load %M[%j] : memref<100xi32>
923 "prevent.dce"(%1) : (i32) -> ()
Uday Bondhugula3f9cdd42020-03-30 19:45:40924 }
925 } else {
926 // Nothing
927 }
928 affine.load %M[%i] : memref<100xi32>
929 }
930 return
931}
932// CHECK: affine.if
933// CHECK-NEXT: affine.for
934// CHECK-NEXT: affine.load
Tres Poppf66c8762020-04-27 14:40:00935// CHECK-NEXT: "prevent.dce"
Uday Bondhugula3f9cdd42020-03-30 19:45:40936// CHECK-NEXT: }
937// CHECK-NEXT: }
Phoenix Meadowlark622aac62020-04-27 19:59:16938
939// -----
940
941// CHECK-LABEL: func @divi_signed_by_one
942// CHECK-SAME: %[[ARG:[a-zA-Z0-9]+]]
River Riddlecda6aa72022-04-20 23:22:21943func.func @divi_signed_by_one(%arg0: i32) -> (i32) {
Mogballa54f4ea2021-10-12 23:14:57944 %c1 = arith.constant 1 : i32
945 %res = arith.divsi %arg0, %c1 : i32
Phoenix Meadowlark622aac62020-04-27 19:59:16946 // CHECK: return %[[ARG]]
947 return %res : i32
948}
949
950// CHECK-LABEL: func @divi_unsigned_by_one
951// CHECK-SAME: %[[ARG:[a-zA-Z0-9]+]]
River Riddlecda6aa72022-04-20 23:22:21952func.func @divi_unsigned_by_one(%arg0: i32) -> (i32) {
Mogballa54f4ea2021-10-12 23:14:57953 %c1 = arith.constant 1 : i32
954 %res = arith.divui %arg0, %c1 : i32
Phoenix Meadowlark622aac62020-04-27 19:59:16955 // CHECK: return %[[ARG]]
956 return %res : i32
957}
958
959// CHECK-LABEL: func @tensor_divi_signed_by_one
960// CHECK-SAME: %[[ARG:[a-zA-Z0-9]+]]
River Riddlecda6aa72022-04-20 23:22:21961func.func @tensor_divi_signed_by_one(%arg0: tensor<4x5xi32>) -> tensor<4x5xi32> {
Mogballa54f4ea2021-10-12 23:14:57962 %c1 = arith.constant dense<1> : tensor<4x5xi32>
963 %res = arith.divsi %arg0, %c1 : tensor<4x5xi32>
Phoenix Meadowlark622aac62020-04-27 19:59:16964 // CHECK: return %[[ARG]]
965 return %res : tensor<4x5xi32>
966}
967
968// CHECK-LABEL: func @tensor_divi_unsigned_by_one
969// CHECK-SAME: %[[ARG:[a-zA-Z0-9]+]]
River Riddlecda6aa72022-04-20 23:22:21970func.func @tensor_divi_unsigned_by_one(%arg0: tensor<4x5xi32>) -> tensor<4x5xi32> {
Mogballa54f4ea2021-10-12 23:14:57971 %c1 = arith.constant dense<1> : tensor<4x5xi32>
972 %res = arith.divui %arg0, %c1 : tensor<4x5xi32>
Phoenix Meadowlark622aac62020-04-27 19:59:16973 // CHECK: return %[[ARG]]
974 return %res : tensor<4x5xi32>
975}
Sam McCall691e8262020-05-12 13:18:50976
977// -----
978
Mogballa54f4ea2021-10-12 23:14:57979// CHECK-LABEL: func @arith.floordivsi_by_one
Alexandre Eichenberger07957152020-11-04 18:51:10980// CHECK-SAME: %[[ARG:[a-zA-Z0-9]+]]
River Riddlecda6aa72022-04-20 23:22:21981func.func @arith.floordivsi_by_one(%arg0: i32) -> (i32) {
Mogballa54f4ea2021-10-12 23:14:57982 %c1 = arith.constant 1 : i32
983 %res = arith.floordivsi %arg0, %c1 : i32
Alexandre Eichenberger07957152020-11-04 18:51:10984 // CHECK: return %[[ARG]]
985 return %res : i32
986}
987
Mogballa54f4ea2021-10-12 23:14:57988// CHECK-LABEL: func @tensor_arith.floordivsi_by_one
Alexandre Eichenberger07957152020-11-04 18:51:10989// CHECK-SAME: %[[ARG:[a-zA-Z0-9]+]]
River Riddlecda6aa72022-04-20 23:22:21990func.func @tensor_arith.floordivsi_by_one(%arg0: tensor<4x5xi32>) -> tensor<4x5xi32> {
Mogballa54f4ea2021-10-12 23:14:57991 %c1 = arith.constant dense<1> : tensor<4x5xi32>
992 %res = arith.floordivsi %arg0, %c1 : tensor<4x5xi32>
Alexandre Eichenberger07957152020-11-04 18:51:10993 // CHECK: return %[[ARG]]
994 return %res : tensor<4x5xi32>
995}
996
long.chen631e54a2024-03-22 15:52:47997// CHECK-LABEL: func @arith.floordivsi_by_one_overflow
998func.func @arith.floordivsi_by_one_overflow() -> i64 {
999 %neg_one = arith.constant -1 : i64
1000 %min_int = arith.constant -9223372036854775808 : i64
1001 // CHECK: arith.floordivsi
1002 %poision = arith.floordivsi %min_int, %neg_one : i64
1003 return %poision : i64
1004}
1005
Alexandre Eichenberger07957152020-11-04 18:51:101006// -----
1007
Mogballa54f4ea2021-10-12 23:14:571008// CHECK-LABEL: func @arith.ceildivsi_by_one
Alexandre Eichenberger07957152020-11-04 18:51:101009// CHECK-SAME: %[[ARG:[a-zA-Z0-9]+]]
River Riddlecda6aa72022-04-20 23:22:211010func.func @arith.ceildivsi_by_one(%arg0: i32) -> (i32) {
Mogballa54f4ea2021-10-12 23:14:571011 %c1 = arith.constant 1 : i32
1012 %res = arith.ceildivsi %arg0, %c1 : i32
Alexandre Eichenberger07957152020-11-04 18:51:101013 // CHECK: return %[[ARG]]
1014 return %res : i32
1015}
1016
Mogballa54f4ea2021-10-12 23:14:571017// CHECK-LABEL: func @tensor_arith.ceildivsi_by_one
Alexandre Eichenberger07957152020-11-04 18:51:101018// CHECK-SAME: %[[ARG:[a-zA-Z0-9]+]]
River Riddlecda6aa72022-04-20 23:22:211019func.func @tensor_arith.ceildivsi_by_one(%arg0: tensor<4x5xi32>) -> tensor<4x5xi32> {
Mogballa54f4ea2021-10-12 23:14:571020 %c1 = arith.constant dense<1> : tensor<4x5xi32>
1021 %res = arith.ceildivsi %arg0, %c1 : tensor<4x5xi32>
Alexandre Eichenberger07957152020-11-04 18:51:101022 // CHECK: return %[[ARG]]
1023 return %res : tensor<4x5xi32>
1024}
1025
1026// -----
1027
lipracer8165eaa2021-11-11 01:47:471028// CHECK-LABEL: func @arith.ceildivui_by_one
1029// CHECK-SAME: %[[ARG:[a-zA-Z0-9]+]]
River Riddlecda6aa72022-04-20 23:22:211030func.func @arith.ceildivui_by_one(%arg0: i32) -> (i32) {
lipracer8165eaa2021-11-11 01:47:471031 %c1 = arith.constant 1 : i32
1032 %res = arith.ceildivui %arg0, %c1 : i32
1033 // CHECK: return %[[ARG]]
1034 return %res : i32
1035}
1036
1037// CHECK-LABEL: func @tensor_arith.ceildivui_by_one
1038// CHECK-SAME: %[[ARG:[a-zA-Z0-9]+]]
River Riddlecda6aa72022-04-20 23:22:211039func.func @tensor_arith.ceildivui_by_one(%arg0: tensor<4x5xi32>) -> tensor<4x5xi32> {
lipracer8165eaa2021-11-11 01:47:471040 %c1 = arith.constant dense<1> : tensor<4x5xi32>
1041 %res = arith.ceildivui %arg0, %c1 : tensor<4x5xi32>
1042 // CHECK: return %[[ARG]]
1043 return %res : tensor<4x5xi32>
1044}
1045
1046// -----
1047
Sam McCall691e8262020-05-12 13:18:501048// CHECK-LABEL: func @memref_cast_folding_subview
Alex Zinenko519847f2022-08-30 08:23:571049func.func @memref_cast_folding_subview(%arg0: memref<4x5xf32>, %i: index) -> (memref<?x?xf32, strided<[?, ?], offset: ?>>) {
Julian Grosse2310702021-02-10 12:53:111050 %0 = memref.cast %arg0 : memref<4x5xf32> to memref<?x?xf32>
1051 // CHECK-NEXT: memref.subview %{{.*}}: memref<4x5xf32>
Alex Zinenko519847f2022-08-30 08:23:571052 %1 = memref.subview %0[%i, %i][%i, %i][%i, %i]: memref<?x?xf32> to memref<?x?xf32, strided<[?, ?], offset: ?>>
Alex Zinenko519847f2022-08-30 08:23:571053 return %1: memref<?x?xf32, strided<[?, ?], offset: ?>>
Sam McCall691e8262020-05-12 13:18:501054}
1055
Nicolas Vasilachee0b99a52020-05-13 02:21:361056// -----
1057
Nicolas Vasilachee0b99a52020-05-13 02:21:361058// CHECK-LABEL: func @memref_cast_folding_subview_static(
River Riddlecda6aa72022-04-20 23:22:211059func.func @memref_cast_folding_subview_static(%V: memref<16x16xf32>, %a: index, %b: index)
Matthias Springer6dc8de72024-06-23 17:05:001060 -> memref<3x4xf32, strided<[?, 1]>>
Nicolas Vasilachee0b99a52020-05-13 02:21:361061{
Julian Grosse2310702021-02-10 12:53:111062 %0 = memref.cast %V : memref<16x16xf32> to memref<?x?xf32>
Matthias Springer6dc8de72024-06-23 17:05:001063 %1 = memref.subview %0[0, 0][3, 4][1, 1] : memref<?x?xf32> to memref<3x4xf32, strided<[?, 1]>>
Nicolas Vasilachee0b99a52020-05-13 02:21:361064
Alex Zinenko2791162b2022-09-15 16:29:141065 // CHECK: memref.subview{{.*}}: memref<16x16xf32> to memref<3x4xf32, strided<[16, 1]>>
Matthias Springer6dc8de72024-06-23 17:05:001066 return %1: memref<3x4xf32, strided<[?, 1]>>
Nicolas Vasilachee0b99a52020-05-13 02:21:361067}
Alexander Belyaevc3098e42020-05-28 11:36:401068
1069// -----
1070
Matthias Springer060208b2021-06-22 07:49:081071// CHECK-LABEL: func @slice
Nicolas Vasilache787bf5e2020-10-02 09:40:521072// CHECK-SAME: %[[ARG0:[0-9a-z]*]]: index, %[[ARG1:[0-9a-z]*]]: index
River Riddlecda6aa72022-04-20 23:22:211073func.func @slice(%t: tensor<8x16x4xf32>, %arg0 : index, %arg1 : index)
Nicolas Vasilacheb3c227a2021-02-18 22:03:021074 -> tensor<?x?x?xf32>
Nicolas Vasilache787bf5e2020-10-02 09:40:521075{
Mogballa54f4ea2021-10-12 23:14:571076 %c0 = arith.constant 0 : index
1077 %c1 = arith.constant 1 : index
1078 %c2 = arith.constant 2 : index
1079 %c7 = arith.constant 7 : index
1080 %c11 = arith.constant 11 : index
Nicolas Vasilache787bf5e2020-10-02 09:40:521081
Matthias Springer060208b2021-06-22 07:49:081082 // CHECK: tensor.extract_slice %{{.*}}[0, 0, 0] [7, 11, 2] [1, 1, 1] :
Nicolas Vasilache787bf5e2020-10-02 09:40:521083 // CHECK-SAME: tensor<8x16x4xf32> to tensor<7x11x2xf32>
Nicolas Vasilacheb3c227a2021-02-18 22:03:021084 // tensor.cast gets folded away in consumer.
1085 // CHECK-NOT: tensor.cast
Matthias Springer060208b2021-06-22 07:49:081086 %1 = tensor.extract_slice %t[%c0, %c0, %c0] [%c7, %c11, %c2] [%c1, %c1, %c1]
Nicolas Vasilache787bf5e2020-10-02 09:40:521087 : tensor<8x16x4xf32> to tensor<?x?x?xf32>
1088
Matthias Springer060208b2021-06-22 07:49:081089 // Test: slice with one dynamic operand can also be folded.
1090 // CHECK: tensor.extract_slice %{{.*}}[0, 0, 0] [2, %[[ARG0]], 2] [1, 1, 1] :
Nicolas Vasilacheb3c227a2021-02-18 22:03:021091 // CHECK-SAME: tensor<7x11x2xf32> to tensor<2x?x2xf32>
Sean Silva129d6e52020-12-16 00:47:191092 // CHECK: tensor.cast %{{.*}} : tensor<2x?x2xf32> to tensor<?x?x?xf32>
Matthias Springer060208b2021-06-22 07:49:081093 %2 = tensor.extract_slice %1[%c0, %c0, %c0] [%c2, %arg0, %c2] [%c1, %c1, %c1]
Nicolas Vasilache787bf5e2020-10-02 09:40:521094 : tensor<?x?x?xf32> to tensor<?x?x?xf32>
1095
1096 return %2 : tensor<?x?x?xf32>
1097}
Nicolas Vasilacheb3c227a2021-02-18 22:03:021098
KareemErgawy-TomTome5f28982021-03-27 18:40:101099// -----
1100
1101// CHECK-LABEL: func @fold_trunci
1102// CHECK-SAME: (%[[ARG0:[0-9a-z]*]]: i1)
River Riddlecda6aa72022-04-20 23:22:211103func.func @fold_trunci(%arg0: i1) -> i1 attributes {} {
KareemErgawy-TomTome5f28982021-03-27 18:40:101104 // CHECK-NEXT: return %[[ARG0]] : i1
Mogballa54f4ea2021-10-12 23:14:571105 %0 = arith.extui %arg0 : i1 to i8
1106 %1 = arith.trunci %0 : i8 to i1
KareemErgawy-TomTome5f28982021-03-27 18:40:101107 return %1 : i1
1108}
1109
1110// -----
1111
1112// CHECK-LABEL: func @fold_trunci_vector
1113// CHECK-SAME: (%[[ARG0:[0-9a-z]*]]: vector<4xi1>)
River Riddlecda6aa72022-04-20 23:22:211114func.func @fold_trunci_vector(%arg0: vector<4xi1>) -> vector<4xi1> attributes {} {
KareemErgawy-TomTome5f28982021-03-27 18:40:101115 // CHECK-NEXT: return %[[ARG0]] : vector<4xi1>
Mogballa54f4ea2021-10-12 23:14:571116 %0 = arith.extui %arg0 : vector<4xi1> to vector<4xi8>
1117 %1 = arith.trunci %0 : vector<4xi8> to vector<4xi1>
KareemErgawy-TomTome5f28982021-03-27 18:40:101118 return %1 : vector<4xi1>
1119}
1120
1121// -----
1122
Jakub Kuderskiab85aec2023-04-27 15:13:461123// CHECK-LABEL: func @fold_trunci
KareemErgawy-TomTome5f28982021-03-27 18:40:101124// CHECK-SAME: (%[[ARG0:[0-9a-z]*]]: i1)
Jakub Kuderskiab85aec2023-04-27 15:13:461125func.func @fold_trunci(%arg0: i1) -> i2 attributes {} {
1126 // CHECK-NEXT: %[[RES:[0-9a-z]*]] = arith.extui %[[ARG0]] : i1 to i2
KareemErgawy-TomTome5f28982021-03-27 18:40:101127 // CHECK-NEXT: return %[[RES]] : i2
Mogballa54f4ea2021-10-12 23:14:571128 %0 = arith.extui %arg0 : i1 to i8
1129 %1 = arith.trunci %0 : i8 to i2
KareemErgawy-TomTome5f28982021-03-27 18:40:101130 return %1 : i2
1131}
1132
1133// -----
1134
Jakub Kuderskiab85aec2023-04-27 15:13:461135// CHECK-LABEL: func @fold_trunci_vector
KareemErgawy-TomTome5f28982021-03-27 18:40:101136// CHECK-SAME: (%[[ARG0:[0-9a-z]*]]: vector<4xi1>)
Jakub Kuderskiab85aec2023-04-27 15:13:461137func.func @fold_trunci_vector(%arg0: vector<4xi1>) -> vector<4xi2> attributes {} {
1138 // CHECK-NEXT: %[[RES:[0-9a-z]*]] = arith.extui %[[ARG0]] : vector<4xi1> to vector<4xi2>
KareemErgawy-TomTome5f28982021-03-27 18:40:101139 // CHECK-NEXT: return %[[RES]] : vector<4xi2>
Mogballa54f4ea2021-10-12 23:14:571140 %0 = arith.extui %arg0 : vector<4xi1> to vector<4xi8>
1141 %1 = arith.trunci %0 : vector<4xi8> to vector<4xi2>
KareemErgawy-TomTome5f28982021-03-27 18:40:101142 return %1 : vector<4xi2>
1143}
KareemErgawy-TomTomc52a5f22021-03-29 06:33:561144
1145// -----
1146
1147// CHECK-LABEL: func @fold_trunci_sexti
1148// CHECK-SAME: (%[[ARG0:[0-9a-z]*]]: i1)
River Riddlecda6aa72022-04-20 23:22:211149func.func @fold_trunci_sexti(%arg0: i1) -> i1 attributes {} {
KareemErgawy-TomTomc52a5f22021-03-29 06:33:561150 // CHECK-NEXT: return %[[ARG0]] : i1
Mogballa54f4ea2021-10-12 23:14:571151 %0 = arith.extsi %arg0 : i1 to i8
1152 %1 = arith.trunci %0 : i8 to i1
KareemErgawy-TomTomc52a5f22021-03-29 06:33:561153 return %1 : i1
1154}
Alexander Belyaev465b9a42021-03-31 07:34:031155
1156// CHECK-LABEL: func @simple_clone_elimination
River Riddlecda6aa72022-04-20 23:22:211157func.func @simple_clone_elimination() -> memref<5xf32> {
Alexander Belyaev465b9a42021-03-31 07:34:031158 %ret = memref.alloc() : memref<5xf32>
Alexander Belyaev57470ab2021-11-25 10:42:161159 %temp = bufferization.clone %ret : memref<5xf32> to memref<5xf32>
Alexander Belyaev465b9a42021-03-31 07:34:031160 memref.dealloc %temp : memref<5xf32>
1161 return %ret : memref<5xf32>
1162}
1163// CHECK-NEXT: %[[ret:.*]] = memref.alloc()
Alexander Belyaev57470ab2021-11-25 10:42:161164// CHECK-NOT: %{{.*}} = bufferization.clone
Thomas Preud'homme17f4f232021-03-28 00:03:371165// CHECK-NOT: memref.dealloc %{{.*}}
Alexander Belyaev465b9a42021-03-31 07:34:031166// CHECK: return %[[ret]]
1167
1168// -----
1169
1170// CHECK-LABEL: func @clone_loop_alloc
River Riddlecda6aa72022-04-20 23:22:211171func.func @clone_loop_alloc(%arg0: index, %arg1: index, %arg2: index, %arg3: memref<2xf32>, %arg4: memref<2xf32>) {
Alexander Belyaev465b9a42021-03-31 07:34:031172 %0 = memref.alloc() : memref<2xf32>
1173 memref.dealloc %0 : memref<2xf32>
Alexander Belyaev57470ab2021-11-25 10:42:161174 %1 = bufferization.clone %arg3 : memref<2xf32> to memref<2xf32>
Alexander Belyaev465b9a42021-03-31 07:34:031175 %2 = scf.for %arg5 = %arg0 to %arg1 step %arg2 iter_args(%arg6 = %1) -> (memref<2xf32>) {
Mogballa54f4ea2021-10-12 23:14:571176 %3 = arith.cmpi eq, %arg5, %arg1 : index
Alexander Belyaev465b9a42021-03-31 07:34:031177 memref.dealloc %arg6 : memref<2xf32>
1178 %4 = memref.alloc() : memref<2xf32>
Alexander Belyaev57470ab2021-11-25 10:42:161179 %5 = bufferization.clone %4 : memref<2xf32> to memref<2xf32>
Alexander Belyaev465b9a42021-03-31 07:34:031180 memref.dealloc %4 : memref<2xf32>
Alexander Belyaev57470ab2021-11-25 10:42:161181 %6 = bufferization.clone %5 : memref<2xf32> to memref<2xf32>
Alexander Belyaev465b9a42021-03-31 07:34:031182 memref.dealloc %5 : memref<2xf32>
1183 scf.yield %6 : memref<2xf32>
1184 }
Alexander Belyaevebc81532022-02-01 17:07:331185 memref.copy %2, %arg4 : memref<2xf32> to memref<2xf32>
Alexander Belyaev465b9a42021-03-31 07:34:031186 memref.dealloc %2 : memref<2xf32>
1187 return
1188}
1189
Alexander Belyaev57470ab2021-11-25 10:42:161190// CHECK-NEXT: %[[ALLOC0:.*]] = bufferization.clone
Alexander Belyaev465b9a42021-03-31 07:34:031191// CHECK-NEXT: %[[ALLOC1:.*]] = scf.for
1192// CHECK-NEXT: memref.dealloc
1193// CHECK-NEXT: %[[ALLOC2:.*]] = memref.alloc
1194// CHECK-NEXT: scf.yield %[[ALLOC2]]
Alexander Belyaevebc81532022-02-01 17:07:331195// CHECK: memref.copy %[[ALLOC1]]
Alexander Belyaev465b9a42021-03-31 07:34:031196// CHECK-NEXT: memref.dealloc %[[ALLOC1]]
1197
1198// -----
1199
1200// CHECK-LABEL: func @clone_nested_region
River Riddlecda6aa72022-04-20 23:22:211201func.func @clone_nested_region(%arg0: index, %arg1: index, %arg2: index) -> memref<?x?xf32> {
Mogballa54f4ea2021-10-12 23:14:571202 %cmp = arith.cmpi eq, %arg0, %arg1 : index
1203 %0 = arith.cmpi eq, %arg0, %arg1 : index
Alexander Belyaev465b9a42021-03-31 07:34:031204 %1 = memref.alloc(%arg0, %arg0) : memref<?x?xf32>
1205 %2 = scf.if %0 -> (memref<?x?xf32>) {
William S. Mosesca272602021-04-22 02:26:101206 %3 = scf.if %cmp -> (memref<?x?xf32>) {
Alexander Belyaev57470ab2021-11-25 10:42:161207 %9 = bufferization.clone %1 : memref<?x?xf32> to memref<?x?xf32>
Alexander Belyaev465b9a42021-03-31 07:34:031208 scf.yield %9 : memref<?x?xf32>
1209 } else {
1210 %7 = memref.alloc(%arg0, %arg1) : memref<?x?xf32>
Alexander Belyaev57470ab2021-11-25 10:42:161211 %10 = bufferization.clone %7 : memref<?x?xf32> to memref<?x?xf32>
Alexander Belyaev465b9a42021-03-31 07:34:031212 memref.dealloc %7 : memref<?x?xf32>
1213 scf.yield %10 : memref<?x?xf32>
1214 }
Alexander Belyaev57470ab2021-11-25 10:42:161215 %6 = bufferization.clone %3 : memref<?x?xf32> to memref<?x?xf32>
Alexander Belyaev465b9a42021-03-31 07:34:031216 memref.dealloc %3 : memref<?x?xf32>
1217 scf.yield %6 : memref<?x?xf32>
1218 } else {
1219 %3 = memref.alloc(%arg1, %arg1) : memref<?x?xf32>
Alexander Belyaev57470ab2021-11-25 10:42:161220 %6 = bufferization.clone %3 : memref<?x?xf32> to memref<?x?xf32>
Alexander Belyaev465b9a42021-03-31 07:34:031221 memref.dealloc %3 : memref<?x?xf32>
1222 scf.yield %6 : memref<?x?xf32>
1223 }
1224 memref.dealloc %1 : memref<?x?xf32>
1225 return %2 : memref<?x?xf32>
1226}
1227
1228// CHECK: %[[ALLOC1:.*]] = memref.alloc
1229// CHECK-NEXT: %[[ALLOC2:.*]] = scf.if
1230// CHECK-NEXT: %[[ALLOC3_1:.*]] = scf.if
Alexander Belyaev57470ab2021-11-25 10:42:161231// CHECK-NEXT: %[[ALLOC4_1:.*]] = bufferization.clone %[[ALLOC1]]
Alexander Belyaev465b9a42021-03-31 07:34:031232// CHECK-NEXT: scf.yield %[[ALLOC4_1]]
1233// CHECK: %[[ALLOC4_2:.*]] = memref.alloc
1234// CHECK-NEXT: scf.yield %[[ALLOC4_2]]
1235// CHECK: scf.yield %[[ALLOC3_1]]
1236// CHECK: %[[ALLOC3_2:.*]] = memref.alloc
1237// CHECK-NEXT: scf.yield %[[ALLOC3_2]]
1238// CHECK: memref.dealloc %[[ALLOC1]]
1239// CHECK-NEXT: return %[[ALLOC2]]
Billy Zhueb428682024-01-08 18:29:321240
1241// -----
1242
1243// CHECK-LABEL: func @test_materialize_failure
1244func.func @test_materialize_failure() -> i64 {
1245 %const = index.constant 1234
1246 // Cannot materialize this castu's output constant.
1247 // CHECK: index.castu
1248 %u = index.castu %const : index to i64
1249 return %u: i64
1250}