File: if.mlir

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (170 lines) | stat: -rw-r--r-- 7,961 bytes parent folder | download | duplicates (14)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// RUN: mlir-opt -convert-scf-to-spirv %s -o - | FileCheck %s

module attributes {
  spirv.target_env = #spirv.target_env<
    #spirv.vce<v1.0, [Shader], [SPV_KHR_storage_buffer_storage_class]>, #spirv.resource_limits<>>
} {

// CHECK-LABEL: @kernel_simple_selection
func.func @kernel_simple_selection(%arg2 : memref<10xf32, #spirv.storage_class<StorageBuffer>>, %arg3 : i1) {
  %value = arith.constant 0.0 : f32
  %i = arith.constant 0 : index

  // CHECK:       spirv.mlir.selection {
  // CHECK-NEXT:    spirv.BranchConditional {{%.*}}, [[TRUE:\^.*]], [[MERGE:\^.*]]
  // CHECK-NEXT:  [[TRUE]]:
  // CHECK:         spirv.Branch [[MERGE]]
  // CHECK-NEXT:  [[MERGE]]:
  // CHECK-NEXT:    spirv.mlir.merge
  // CHECK-NEXT:  }
  // CHECK-NEXT:  spirv.Return

  scf.if %arg3 {
    memref.store %value, %arg2[%i] : memref<10xf32, #spirv.storage_class<StorageBuffer>>
  }
  return
}

// CHECK-LABEL: @kernel_nested_selection
func.func @kernel_nested_selection(%arg3 : memref<10xf32, #spirv.storage_class<StorageBuffer>>, %arg4 : memref<10xf32, #spirv.storage_class<StorageBuffer>>, %arg5 : i1, %arg6 : i1) {
  %i = arith.constant 0 : index
  %j = arith.constant 9 : index

  // CHECK:       spirv.mlir.selection {
  // CHECK-NEXT:    spirv.BranchConditional {{%.*}}, [[TRUE_TOP:\^.*]], [[FALSE_TOP:\^.*]]
  // CHECK-NEXT:  [[TRUE_TOP]]:
  // CHECK-NEXT:    spirv.mlir.selection {
  // CHECK-NEXT:      spirv.BranchConditional {{%.*}}, [[TRUE_NESTED_TRUE_PATH:\^.*]], [[FALSE_NESTED_TRUE_PATH:\^.*]]
  // CHECK-NEXT:    [[TRUE_NESTED_TRUE_PATH]]:
  // CHECK:           spirv.Branch [[MERGE_NESTED_TRUE_PATH:\^.*]]
  // CHECK-NEXT:    [[FALSE_NESTED_TRUE_PATH]]:
  // CHECK:           spirv.Branch [[MERGE_NESTED_TRUE_PATH]]
  // CHECK-NEXT:    [[MERGE_NESTED_TRUE_PATH]]:
  // CHECK-NEXT:      spirv.mlir.merge
  // CHECK-NEXT:    }
  // CHECK-NEXT:    spirv.Branch [[MERGE_TOP:\^.*]]
  // CHECK-NEXT:  [[FALSE_TOP]]:
  // CHECK-NEXT:    spirv.mlir.selection {
  // CHECK-NEXT:      spirv.BranchConditional {{%.*}}, [[TRUE_NESTED_FALSE_PATH:\^.*]], [[FALSE_NESTED_FALSE_PATH:\^.*]]
  // CHECK-NEXT:    [[TRUE_NESTED_FALSE_PATH]]:
  // CHECK:           spirv.Branch [[MERGE_NESTED_FALSE_PATH:\^.*]]
  // CHECK-NEXT:    [[FALSE_NESTED_FALSE_PATH]]:
  // CHECK:           spirv.Branch [[MERGE_NESTED_FALSE_PATH]]
  // CHECK:         [[MERGE_NESTED_FALSE_PATH]]:
  // CHECK-NEXT:      spirv.mlir.merge
  // CHECK-NEXT:    }
  // CHECK-NEXT:    spirv.Branch [[MERGE_TOP]]
  // CHECK-NEXT:  [[MERGE_TOP]]:
  // CHECK-NEXT:    spirv.mlir.merge
  // CHECK-NEXT:  }
  // CHECK-NEXT:  spirv.Return

  scf.if %arg5 {
    scf.if %arg6 {
      %value = memref.load %arg3[%i] : memref<10xf32, #spirv.storage_class<StorageBuffer>>
      memref.store %value, %arg4[%i] : memref<10xf32, #spirv.storage_class<StorageBuffer>>
    } else {
      %value = memref.load %arg4[%i] : memref<10xf32, #spirv.storage_class<StorageBuffer>>
      memref.store %value, %arg3[%i] : memref<10xf32, #spirv.storage_class<StorageBuffer>>
    }
  } else {
    scf.if %arg6 {
      %value = memref.load %arg3[%j] : memref<10xf32, #spirv.storage_class<StorageBuffer>>
      memref.store %value, %arg4[%j] : memref<10xf32, #spirv.storage_class<StorageBuffer>>
    } else {
      %value = memref.load %arg4[%j] : memref<10xf32, #spirv.storage_class<StorageBuffer>>
      memref.store %value, %arg3[%j] : memref<10xf32, #spirv.storage_class<StorageBuffer>>
    }
  }
  return
}

// CHECK-LABEL: @simple_if_yield
func.func @simple_if_yield(%arg2 : memref<10xf32, #spirv.storage_class<StorageBuffer>>, %arg3 : i1) {
  // CHECK: %[[VAR1:.*]] = spirv.Variable : !spirv.ptr<f32, Function>
  // CHECK: %[[VAR2:.*]] = spirv.Variable : !spirv.ptr<f32, Function>
  // CHECK:       spirv.mlir.selection {
  // CHECK-NEXT:    spirv.BranchConditional {{%.*}}, [[TRUE:\^.*]], [[FALSE:\^.*]]
  // CHECK-NEXT:  [[TRUE]]:
  // CHECK:         %[[RET1TRUE:.*]] = spirv.Constant 0.000000e+00 : f32
  // CHECK:         %[[RET2TRUE:.*]] = spirv.Constant 1.000000e+00 : f32
  // CHECK-DAG:     spirv.Store "Function" %[[VAR1]], %[[RET1TRUE]] : f32
  // CHECK-DAG:     spirv.Store "Function" %[[VAR2]], %[[RET2TRUE]] : f32
  // CHECK:         spirv.Branch ^[[MERGE:.*]]
  // CHECK-NEXT:  [[FALSE]]:
  // CHECK:         %[[RET2FALSE:.*]] = spirv.Constant 2.000000e+00 : f32
  // CHECK:         %[[RET1FALSE:.*]] = spirv.Constant 3.000000e+00 : f32
  // CHECK-DAG:     spirv.Store "Function" %[[VAR1]], %[[RET1FALSE]] : f32
  // CHECK-DAG:     spirv.Store "Function" %[[VAR2]], %[[RET2FALSE]] : f32
  // CHECK:         spirv.Branch ^[[MERGE]]
  // CHECK-NEXT:  ^[[MERGE]]:
  // CHECK:         spirv.mlir.merge
  // CHECK-NEXT:  }
  // CHECK-DAG:   %[[OUT1:.*]] = spirv.Load "Function" %[[VAR1]] : f32
  // CHECK-DAG:   %[[OUT2:.*]] = spirv.Load "Function" %[[VAR2]] : f32
  // CHECK:       spirv.Store "StorageBuffer" {{%.*}}, %[[OUT1]] : f32
  // CHECK:       spirv.Store "StorageBuffer" {{%.*}}, %[[OUT2]] : f32
  // CHECK:       spirv.Return
  %0:2 = scf.if %arg3 -> (f32, f32) {
    %c0 = arith.constant 0.0 : f32
    %c1 = arith.constant 1.0 : f32
    scf.yield %c0, %c1 : f32, f32
  } else {
    %c0 = arith.constant 2.0 : f32
    %c1 = arith.constant 3.0 : f32
    scf.yield %c1, %c0 : f32, f32
  }
  %i = arith.constant 0 : index
  %j = arith.constant 1 : index
  memref.store %0#0, %arg2[%i] : memref<10xf32, #spirv.storage_class<StorageBuffer>>
  memref.store %0#1, %arg2[%j] : memref<10xf32, #spirv.storage_class<StorageBuffer>>
  return
}

// TODO: The transformation should only be legal if VariablePointer capability
// is supported. This test is still useful to make sure we can handle scf op
// result with type change.
func.func @simple_if_yield_type_change(%arg2 : memref<10xf32, #spirv.storage_class<StorageBuffer>>, %arg3 : memref<10xf32, #spirv.storage_class<StorageBuffer>>, %arg4 : i1) {
  // CHECK-LABEL: @simple_if_yield_type_change
  // CHECK:       %[[VAR:.*]] = spirv.Variable : !spirv.ptr<!spirv.ptr<!spirv.struct<(!spirv.array<10 x f32, stride=4> [0])>, StorageBuffer>, Function>
  // CHECK:       spirv.mlir.selection {
  // CHECK-NEXT:    spirv.BranchConditional {{%.*}}, [[TRUE:\^.*]], [[FALSE:\^.*]]
  // CHECK-NEXT:  [[TRUE]]:
  // CHECK:         spirv.Store "Function" %[[VAR]], {{%.*}} : !spirv.ptr<!spirv.struct<(!spirv.array<10 x f32, stride=4> [0])>, StorageBuffer>
  // CHECK:         spirv.Branch ^[[MERGE:.*]]
  // CHECK-NEXT:  [[FALSE]]:
  // CHECK:         spirv.Store "Function" %[[VAR]], {{%.*}} : !spirv.ptr<!spirv.struct<(!spirv.array<10 x f32, stride=4> [0])>, StorageBuffer>
  // CHECK:         spirv.Branch ^[[MERGE]]
  // CHECK-NEXT:  ^[[MERGE]]:
  // CHECK:         spirv.mlir.merge
  // CHECK-NEXT:  }
  // CHECK:       %[[OUT:.*]] = spirv.Load "Function" %[[VAR]] : !spirv.ptr<!spirv.struct<(!spirv.array<10 x f32, stride=4> [0])>, StorageBuffer>
  // CHECK:       %[[ADD:.*]] = spirv.AccessChain %[[OUT]][{{%.*}}, {{%.*}}] : !spirv.ptr<!spirv.struct<(!spirv.array<10 x f32, stride=4> [0])>, StorageBuffer>
  // CHECK:       spirv.Store "StorageBuffer" %[[ADD]], {{%.*}} : f32
  // CHECK:       spirv.Return
  %i = arith.constant 0 : index
  %value = arith.constant 0.0 : f32
  %0 = scf.if %arg4 -> (memref<10xf32, #spirv.storage_class<StorageBuffer>>) {
    scf.yield %arg2 : memref<10xf32, #spirv.storage_class<StorageBuffer>>
  } else {
    scf.yield %arg3 : memref<10xf32, #spirv.storage_class<StorageBuffer>>
  }
  memref.store %value, %0[%i] : memref<10xf32, #spirv.storage_class<StorageBuffer>>
  return
}

// Memrefs without a spirv storage class are not supported. The conversion
// should preserve the `scf.if` and not crash.
func.func @unsupported_yield_type(%arg0 : memref<8xi32>, %arg1 : memref<8xi32>, %c : i1) {
// CHECK-LABEL: @unsupported_yield_type
// CHECK-NEXT:    scf.if
// CHECK:         spirv.Return
  %r = scf.if %c -> (memref<8xi32>) {
    scf.yield %arg0 : memref<8xi32>
  } else {
    scf.yield %arg1 : memref<8xi32>
  }
  return
}

} // end module