1 ================================================================================
2 Branching written with cf.br, cf.cond_br
3 ================================================================================
4 func.func @simple(i64, i1) -> i64 {
5 ^bb0(%a: i64, %cond: i1): // Code dominated by ^bb0 may refer to %a
6 cf.cond_br %cond, ^bb1, ^bb2
9 cf.br ^bb3(%a: i64) // Branch passes %a as the argument
12 %b = arith.addi %a, %a : i64
13 cf.br ^bb3(%b: i64) // Branch passes %b as the argument
15 // ^bb3 receives an argument, named %c, from predecessors
16 // and passes it on to bb4 along with %a. %a is referenced
17 // directly from its defining operation and is not passed through
18 // an argument of ^bb3.
20 cf.br ^bb4(%c, %a : i64, i64)
22 ^bb4(%d : i64, %e : i64):
23 %0 = arith.addi %d, %e : i64
24 return %0 : i64 // Return is also a terminator.
26 --------------------------------------------------------------------------------
41 (type_list_attr_parens
101 (integer_type))))))))
127 (integer_type))))))))
159 ================================================================================
160 Switch statement written with cf.switch
161 ================================================================================
162 func.func @switch(%flag : i32, %caseOperand : i32) {
163 cf.switch %flag : i32, [
164 default: ^bb1(%caseOperand : i32),
165 42: ^bb2(%caseOperand : i32),
166 43: ^bb3(%caseOperand : i32)
176 --------------------------------------------------------------------------------
223 (integer_type))))))))
256 (func_dialect)))))))))
258 ================================================================================
259 Simple for-loop written with scf.for
260 ================================================================================
261 func.func @for_loop_with_increasing_arg() -> i1 {
262 %c0 = arith.constant 0 : index
263 %c1 = arith.constant 1 : index
264 %c4 = arith.constant 4 : index
265 %c16 = arith.constant 16 : index
266 %0 = scf.for %arg0 = %c0 to %c4 step %c1 iter_args(%arg1 = %c0) -> index {
267 %10 = arith.addi %arg0, %arg1 : index
268 scf.yield %10 : index
270 %1 = arith.cmpi ule, %0, %c16 : index
273 --------------------------------------------------------------------------------
282 (type_list_attr_parens
356 (index_type)))))))))))
373 (integer_type))))))))))))
375 ================================================================================
376 Nested for-loop written using scf.for
377 ================================================================================
378 func.func @std_for(%arg0 : index, %arg1 : index, %arg2 : index) {
379 scf.for %i0 = %arg0 to %arg1 step %arg2 {
380 scf.for %i1 = %arg0 to %arg1 step %arg2 {
381 %min_cmp = arith.cmpi slt, %i0, %i1 : index
382 %min = arith.select %min_cmp, %i0, %i1 : index
383 %max_cmp = arith.cmpi sge, %i0, %i1 : index
384 %max = arith.select %max_cmp, %i0, %i1 : index
385 scf.for %i2 = %min to %max step %i1 {
391 --------------------------------------------------------------------------------
480 (region))))))))))))))
483 (func_dialect)))))))))
485 ================================================================================
486 Conditional written with scf.if and scf.yield
487 ================================================================================
488 func.func @std_if_yield(%arg0: i1, %arg1: f32)
490 %x, %y = scf.if %arg0 -> (f32, f32) {
491 %0 = arith.addf %arg1, %arg1 : f32
492 %1 = arith.subf %arg1, %arg1 : f32
493 scf.yield %0, %1 : f32, f32
495 %0 = arith.subf %arg1, %arg1 : f32
496 %1 = arith.addf %arg1, %arg1 : f32
497 scf.yield %0, %1 : f32, f32
501 --------------------------------------------------------------------------------
598 (float_type)))))))))))
601 (func_dialect)))))))))
603 ================================================================================
604 Switch statement using scf.index_switch
605 ================================================================================
606 func.func @switch(%arg0: index) -> i32 {
607 %0 = scf.index_switch %arg0 -> i32
609 %c10_i32 = arith.constant 10 : i32
610 scf.yield %c10_i32 : i32
613 %c20_i32 = arith.constant 20 : i32
614 scf.yield %c20_i32 : i32
617 %c30_i32 = arith.constant 30 : i32
618 scf.yield %c30_i32 : i32
621 scf.index_switch %arg0
628 --------------------------------------------------------------------------------
641 (type_list_attr_parens
675 (integer_type))))))))
695 (integer_type))))))))
714 (integer_type)))))))))))
731 (integer_type))))))))))))
733 ================================================================================
734 Parallel for-loop and reduction using scf.parallel and scf.reduce
735 ================================================================================
736 func.func @single_iteration_reduce(%A: index, %B: index) -> (index, index) {
737 %c0 = arith.constant 0 : index
738 %c1 = arith.constant 1 : index
739 %c2 = arith.constant 2 : index
740 %c3 = arith.constant 3 : index
741 %c6 = arith.constant 6 : index
742 %0:2 = scf.parallel (%i0, %i1) = (%c1, %c3) to (%c2, %c6) step (%c1, %c3) init(%A, %B) -> (index, index) {
743 scf.reduce(%i0) : index {
744 ^bb0(%lhs: index, %rhs: index):
745 %1 = arith.addi %lhs, %rhs : index
746 scf.reduce.return %1 : index
748 scf.reduce(%i1) : index {
749 ^bb0(%lhs: index, %rhs: index):
750 %2 = arith.muli %lhs, %rhs : index
751 scf.reduce.return %2 : index
755 return %0#0, %0#1 : index, index
758 func.func @single_iteration_some(%A: memref<?x?x?xi32>) {
759 %c0 = arith.constant 0 : index
760 %c1 = arith.constant 1 : index
761 %c2 = arith.constant 2 : index
762 %c3 = arith.constant 3 : index
763 %c6 = arith.constant 6 : index
764 %c7 = arith.constant 7 : index
765 %c10 = arith.constant 10 : index
766 scf.parallel (%i0, %i1, %i2) = (%c0, %c3, %c7) to (%c1, %c6, %c10) step (%c1, %c2, %c3) {
767 %c42 = arith.constant 42 : i32
768 memref.store %c42, %A[%i0, %i1, %i2] : memref<?x?x?xi32>
773 --------------------------------------------------------------------------------
790 (type_list_attr_parens
903 (index_type)))))))))))
940 (index_type)))))))))))
954 (index_type)))))))))))
1069 (integer_type))))))))
1072 (scf_dialect))))))))
1075 (func_dialect)))))))))
1077 ================================================================================
1078 While loop written using scf.while and scf.condition
1079 ================================================================================
1080 func.func @while_cond_true() -> i1 {
1081 %0 = scf.while () : () -> i1 {
1082 %condition = "test.condition"() : () -> i1
1083 scf.condition(%condition) %condition : i1
1086 "test.use"(%arg0) : (i1) -> ()
1092 func.func @while_unused_arg(%x : i32, %y : f64) -> i32 {
1093 %0 = scf.while (%arg1 = %x, %arg2 = %y) : (i32, f64) -> (i32) {
1094 %condition = "test.condition"(%arg1) : (i32) -> i1
1095 scf.condition(%condition) %arg1 : i32
1098 %next = "test.use"(%arg1) : (i32) -> (i32)
1099 scf.yield %next, %y : i32, f64
1104 func.func @infinite_while() {
1105 %true = arith.constant true
1106 scf.while : () -> () {
1107 scf.condition(%true)
1113 --------------------------------------------------------------------------------
1122 (type_list_attr_parens
1155 (integer_type))))))))
1175 (scf_dialect))))))))
1182 (integer_type)))))))))))
1197 (type_list_attr_parens
1244 (integer_type))))))))
1277 (float_type)))))))))))
1284 (integer_type)))))))))))
1312 (scf_dialect))))))))
1315 (func_dialect)))))))))
1317 ================================================================================
1318 affine.for with attribute aliseses
1319 ================================================================================
1320 #map_simple0 = affine_map<()[] -> (10)>
1321 #map_simple1 = affine_map<()[s0] -> (s0)>
1322 #map_non_simple0 = affine_map<(d0)[] -> (d0)>
1323 #map_non_simple1 = affine_map<(d0)[s0] -> (d0 + s0)>
1324 #map_non_simple2 = affine_map<()[s0, s1] -> (s0 + s1)>
1325 #map_non_simple3 = affine_map<()[s0] -> (s0 + 3)>
1326 func.func @funcsimplemap(%arg0: index, %arg1: index) -> () {
1327 affine.for %i0 = 0 to #map_simple0()[] {
1328 affine.for %i1 = 0 to #map_simple1()[%arg1] {
1329 affine.for %i2 = 0 to #map_non_simple0(%i0)[] {
1330 affine.for %i3 = 0 to #map_non_simple1(%i0)[%arg1] {
1331 affine.for %i4 = 0 to #map_non_simple2()[%arg1, %arg0] {
1332 affine.for %i5 = 0 to #map_non_simple3()[%arg0] {
1333 %c42_i32 = arith.constant 42 : i32
1342 --------------------------------------------------------------------------------
1345 (attribute_alias_def
1349 (integer_literal)))))
1350 (attribute_alias_def
1356 (attribute_alias_def
1362 (attribute_alias_def
1370 (attribute_alias_def
1378 (attribute_alias_def
1384 (integer_literal)))))
1399 (type_list_attr_parens))
1471 (integer_type))))))))))))))))))))))))))))))))))))
1474 (func_dialect)))))))))
1476 ================================================================================
1477 affine.if and affine.yield
1478 ================================================================================
1479 func.func @affine_if() -> f32 {
1480 %zero = arith.constant 0.0 : f32
1481 %0 = affine.if affine_set<() : ()> () -> f32 {
1482 affine.yield %zero : f32
1484 affine.yield %zero : f32
1488 --------------------------------------------------------------------------------
1497 (type_list_attr_parens
1540 (float_type)))))))))))
1547 (float_type))))))))))))
1549 ================================================================================
1550 Parallel for loop written using affine.parallel
1551 ================================================================================
1552 func.func @parallel(%A : memref<100x100xf32>, %N : index) {
1553 affine.parallel (%i0, %j0) = (0, 0) to (symbol(%N), 100) step (10, 10) {
1554 %0:2 = affine.parallel (%i1, %j1) = (%i0, %j0) to (%i0 + 10, %j0 + 10)
1555 reduce ("minf", "maxf") -> (f32, f32) {
1556 %2 = affine.load %A[%i0 + %i0, %j0 + %j1] : memref<100x100xf32>
1557 affine.yield %2, %2 : f32, f32
1563 func.func @parallel_min_max(%a: index, %b: index, %c: index, %d: index) {
1564 affine.parallel (%i, %j, %k) = (max(%a, %b), %b, max(%a, %c))
1565 to (%c, min(%c, %d), %b) {
1570 --------------------------------------------------------------------------------
1651 (float_type))))))))))))))))
1654 (func_dialect))))))))
1697 (affine_dialect))))))))
1700 (func_dialect)))))))))
1702 ================================================================================
1703 Execution of a region using scf.execute_region
1704 ================================================================================
1705 func.func @execute_region() -> i64 {
1706 %res = scf.execute_region -> i64 {
1707 %c1 = arith.constant 1 : i64
1711 %res2:2 = scf.execute_region -> (i64, i64) {
1712 %c1 = arith.constant 1 : i64
1713 scf.yield %c1, %c1 : i64, i64
1717 --------------------------------------------------------------------------------
1726 (type_list_attr_parens
1757 (integer_type)))))))))))
1790 (integer_type)))))))))))
1797 (integer_type))))))))))))
1799 ================================================================================
1800 Parallel loop written using scf.forall and scf.forall.in_parallel
1801 ================================================================================
1802 func.func @normalized_forall(%in: tensor<100xf32>, %out: tensor<100xf32>) {
1803 %c1 = arith.constant 1 : index
1804 %num_threads = arith.constant 100 : index
1805 %result = scf.forall (%thread_idx) in (%num_threads) shared_outs(%o = %out) -> tensor<100xf32> {
1806 %1 = tensor.extract_slice %in[%thread_idx][1][1] : tensor<100xf32> to tensor<1xf32>
1807 scf.forall.in_parallel {
1808 tensor.parallel_insert_slice %1 into %o[%thread_idx][1][1] :
1809 tensor<1xf32> into tensor<100xf32>
1815 func.func @explicit_loop_bounds_forall(%in: tensor<100xf32>,
1816 %out: tensor<100xf32>) {
1817 %c0 = arith.constant 0 : index
1818 %c1 = arith.constant 1 : index
1819 %num_threads = arith.constant 100 : index
1820 %result = scf.forall (%thread_idx) = (%c0) to (%num_threads) step (%c1) shared_outs(%o = %out) -> tensor<100xf32> {
1821 %1 = tensor.extract_slice %in[%thread_idx][1][1] : tensor<100xf32> to tensor<1xf32>
1822 scf.forall.in_parallel {
1823 tensor.parallel_insert_slice %1 into %o[%thread_idx][1][1] :
1824 tensor<1xf32> into tensor<100xf32>
1825 } {mapping = [#gpu.thread<x>]}
1830 func.func @normalized_forall_elide_terminator() -> () {
1831 %num_threads = arith.constant 100 : index
1832 scf.forall (%thread_idx) in (%num_threads) {
1833 scf.forall.in_parallel {
1835 } {mapping = [#gpu.thread<x>]}
1839 --------------------------------------------------------------------------------
1936 (float_type))))))))))))))))))
1939 (func_dialect))))))))
2045 (float_type))))))))))
2047 (dictionary_attribute
2052 (pretty_dialect_item
2055 (pretty_dialect_item_body)))))))))))))))
2058 (func_dialect))))))))
2065 (type_list_attr_parens))
2089 (dictionary_attribute
2094 (pretty_dialect_item
2097 (pretty_dialect_item_body))))))))))
2100 (func_dialect)))))))))