@@ -187,6 +187,54 @@ func @iadd_zero(%arg0: i32) -> (i32, i32) {
187
187
return %0 , %1: i32 , i32
188
188
}
189
189
190
+ // CHECK-LABEL: @const_fold_scalar_iadd_normal
191
+ func @const_fold_scalar_iadd_normal () -> (i32 , i32 , i32 ) {
192
+ %c5 = spv.constant 5 : i32
193
+ %cn8 = spv.constant -8 : i32
194
+
195
+ // CHECK: spv.constant 10
196
+ // CHECK: spv.constant -16
197
+ // CHECK: spv.constant -3
198
+ %0 = spv.IAdd %c5 , %c5 : i32
199
+ %1 = spv.IAdd %cn8 , %cn8 : i32
200
+ %2 = spv.IAdd %c5 , %cn8 : i32
201
+ return %0 , %1 , %2: i32 , i32 , i32
202
+ }
203
+
204
+ // CHECK-LABEL: @const_fold_scalar_iadd_flow
205
+ func @const_fold_scalar_iadd_flow () -> (i32 , i32 , i32 , i32 ) {
206
+ %c1 = spv.constant 1 : i32
207
+ %c2 = spv.constant 2 : i32
208
+ %c3 = spv.constant 4294967295 : i32 // 2^32 - 1: 0xffff ffff
209
+ %c4 = spv.constant -2147483648 : i32 // -2^31 : 0x8000 0000
210
+ %c5 = spv.constant -1 : i32 // : 0xffff ffff
211
+ %c6 = spv.constant -2 : i32 // : 0xffff fffe
212
+
213
+ // 0x0000 0001 + 0xffff ffff = 0x1 0000 0000 -> 0x0000 0000
214
+ // CHECK: spv.constant 0
215
+ %0 = spv.IAdd %c1 , %c3 : i32
216
+ // 0x0000 0002 + 0xffff ffff = 0x1 0000 0001 -> 0x0000 0001
217
+ // CHECK: spv.constant 1
218
+ %1 = spv.IAdd %c2 , %c3 : i32
219
+ // 0x8000 0000 + 0xffff ffff = 0x1 7fff ffff -> 0x7fff ffff
220
+ // CHECK: spv.constant 2147483647
221
+ %2 = spv.IAdd %c4 , %c5 : i32
222
+ // 0x8000 0000 + 0xffff fffe = 0x1 7fff fffe -> 0x7fff fffe
223
+ // CHECK: spv.constant 2147483646
224
+ %3 = spv.IAdd %c4 , %c6 : i32
225
+ return %0 , %1 , %2 , %3: i32 , i32 , i32 , i32
226
+ }
227
+
228
+ // CHECK-LABEL: @const_fold_vector_iadd
229
+ func @const_fold_vector_iadd () -> vector <3 xi32 > {
230
+ %vc1 = spv.constant dense <[42 , -55 , 127 ]> : vector <3 xi32 >
231
+ %vc2 = spv.constant dense <[-3 , -15 , 28 ]> : vector <3 xi32 >
232
+
233
+ // CHECK: spv.constant dense<[39, -70, 155]>
234
+ %0 = spv.IAdd %vc1 , %vc2 : vector <3 xi32 >
235
+ return %0: vector <3 xi32 >
236
+ }
237
+
190
238
// -----
191
239
192
240
//===----------------------------------------------------------------------===//
@@ -205,6 +253,113 @@ func @imul_zero_one(%arg0: i32) -> (i32, i32) {
205
253
return %0 , %1: i32 , i32
206
254
}
207
255
256
+ // CHECK-LABEL: @const_fold_scalar_imul_normal
257
+ func @const_fold_scalar_imul_normal () -> (i32 , i32 , i32 ) {
258
+ %c5 = spv.constant 5 : i32
259
+ %cn8 = spv.constant -8 : i32
260
+ %c7 = spv.constant 7 : i32
261
+
262
+ // CHECK: spv.constant 35
263
+ // CHECK: spv.constant -40
264
+ // CHECK: spv.constant -56
265
+ %0 = spv.IMul %c7 , %c5 : i32
266
+ %1 = spv.IMul %c5 , %cn8 : i32
267
+ %2 = spv.IMul %cn8 , %c7 : i32
268
+ return %0 , %1 , %2: i32 , i32 , i32
269
+ }
270
+
271
+ // CHECK-LABEL: @const_fold_scalar_imul_flow
272
+ func @const_fold_scalar_imul_flow () -> (i32 , i32 , i32 ) {
273
+ %c1 = spv.constant 2 : i32
274
+ %c2 = spv.constant 4 : i32
275
+ %c3 = spv.constant 4294967295 : i32 // 2^32 - 1 : 0xffff ffff
276
+ %c4 = spv.constant -2147483649 : i32 // -2^31 - 1: 0x7fff ffff
277
+
278
+ // (0xffff ffff << 1) = 0x1 ffff fffe -> 0xffff fffe
279
+ // CHECK: %[[CST2:.*]] = spv.constant -2
280
+ %0 = spv.IMul %c1 , %c3 : i32
281
+ // (0x7fff ffff << 1) = 0x0 ffff fffe -> 0xffff fffe
282
+ %1 = spv.IMul %c1 , %c4 : i32
283
+ // (0x7fff ffff << 2) = 0x1 ffff fffc -> 0xffff fffc
284
+ // CHECK: %[[CST4:.*]] = spv.constant -4
285
+ %2 = spv.IMul %c4 , %c2 : i32
286
+ // CHECK: return %[[CST2]], %[[CST2]], %[[CST4]]
287
+ return %0 , %1 , %2: i32 , i32 , i32
288
+ }
289
+
290
+
291
+ // CHECK-LABEL: @const_fold_vector_imul
292
+ func @const_fold_vector_imul () -> vector <3 xi32 > {
293
+ %vc1 = spv.constant dense <[42 , -55 , 127 ]> : vector <3 xi32 >
294
+ %vc2 = spv.constant dense <[-3 , -15 , 28 ]> : vector <3 xi32 >
295
+
296
+ // CHECK: spv.constant dense<[-126, 825, 3556]>
297
+ %0 = spv.IMul %vc1 , %vc2 : vector <3 xi32 >
298
+ return %0: vector <3 xi32 >
299
+ }
300
+
301
+ // -----
302
+
303
+ //===----------------------------------------------------------------------===//
304
+ // spv.ISub
305
+ //===----------------------------------------------------------------------===//
306
+
307
+ // CHECK-LABEL: @isub_x_x
308
+ func @isub_x_x (%arg0: i32 ) -> i32 {
309
+ // CHECK: spv.constant 0
310
+ %0 = spv.ISub %arg0 , %arg0: i32
311
+ return %0: i32
312
+ }
313
+
314
+ // CHECK-LABEL: @const_fold_scalar_isub_normal
315
+ func @const_fold_scalar_isub_normal () -> (i32 , i32 , i32 ) {
316
+ %c5 = spv.constant 5 : i32
317
+ %cn8 = spv.constant -8 : i32
318
+ %c7 = spv.constant 7 : i32
319
+
320
+ // CHECK: spv.constant 2
321
+ // CHECK: spv.constant 13
322
+ // CHECK: spv.constant -15
323
+ %0 = spv.ISub %c7 , %c5 : i32
324
+ %1 = spv.ISub %c5 , %cn8 : i32
325
+ %2 = spv.ISub %cn8 , %c7 : i32
326
+ return %0 , %1 , %2: i32 , i32 , i32
327
+ }
328
+
329
+ // CHECK-LABEL: @const_fold_scalar_isub_flow
330
+ func @const_fold_scalar_isub_flow () -> (i32 , i32 , i32 , i32 ) {
331
+ %c1 = spv.constant 0 : i32
332
+ %c2 = spv.constant 1 : i32
333
+ %c3 = spv.constant 4294967295 : i32 // 2^32 - 1 : 0xffff ffff
334
+ %c4 = spv.constant -2147483649 : i32 // -2^31 - 1: 0x7fff ffff
335
+ %c5 = spv.constant -1 : i32 // : 0xffff ffff
336
+ %c6 = spv.constant -2 : i32 // : 0xffff fffe
337
+
338
+ // 0x0000 0000 - 0xffff ffff -> 0x0000 0000 + 0x0000 0001 = 0x0000 0001
339
+ // CHECK: spv.constant 1
340
+ %0 = spv.ISub %c1 , %c3 : i32
341
+ // 0x0000 0001 - 0xffff ffff -> 0x0000 0001 + 0x0000 0001 = 0x0000 0002
342
+ // CHECK: spv.constant 2
343
+ %1 = spv.ISub %c2 , %c3 : i32
344
+ // 0xffff ffff - 0x7fff ffff -> 0xffff ffff + 0x8000 0001 = 0x1 8000 0000
345
+ // CHECK: spv.constant -2147483648
346
+ %2 = spv.ISub %c5 , %c4 : i32
347
+ // 0xffff fffe - 0x7fff ffff -> 0xffff fffe + 0x8000 0001 = 0x1 7fff ffff
348
+ // CHECK: spv.constant 2147483647
349
+ %3 = spv.ISub %c6 , %c4 : i32
350
+ return %0 , %1 , %2 , %3: i32 , i32 , i32 , i32
351
+ }
352
+
353
+ // CHECK-LABEL: @const_fold_vector_isub
354
+ func @const_fold_vector_isub () -> vector <3 xi32 > {
355
+ %vc1 = spv.constant dense <[42 , -55 , 127 ]> : vector <3 xi32 >
356
+ %vc2 = spv.constant dense <[-3 , -15 , 28 ]> : vector <3 xi32 >
357
+
358
+ // CHECK: spv.constant dense<[45, -40, 99]>
359
+ %0 = spv.ISub %vc1 , %vc2 : vector <3 xi32 >
360
+ return %0: vector <3 xi32 >
361
+ }
362
+
208
363
// -----
209
364
210
365
//===----------------------------------------------------------------------===//
0 commit comments