File: index-to-spirv.mlir

package info (click to toggle)
llvm-toolchain-19 1%3A19.1.7-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,998,520 kB
  • sloc: cpp: 6,951,680; ansic: 1,486,157; asm: 913,598; python: 232,024; f90: 80,126; objc: 75,281; lisp: 37,276; pascal: 16,990; sh: 10,009; ml: 5,058; perl: 4,724; awk: 3,523; makefile: 3,167; javascript: 2,504; xml: 892; fortran: 664; cs: 573
file content (222 lines) | stat: -rw-r--r-- 7,987 bytes parent folder | download | duplicates (9)
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// RUN: mlir-opt %s -convert-index-to-spirv | FileCheck %s
// RUN: mlir-opt %s -convert-index-to-spirv=use-64bit-index=false | FileCheck %s --check-prefix=INDEX32
// RUN: mlir-opt %s -convert-index-to-spirv=use-64bit-index=true | FileCheck %s --check-prefix=INDEX64

module attributes {
  spirv.target_env = #spirv.target_env<#spirv.vce<v1.0, [Int64], []>, #spirv.resource_limits<>>
} {
// CHECK-LABEL: @trivial_ops
func.func @trivial_ops(%a: index, %b: index) {
  // CHECK: spirv.IAdd
  %0 = index.add %a, %b
  // CHECK: spirv.ISub
  %1 = index.sub %a, %b
  // CHECK: spirv.IMul
  %2 = index.mul %a, %b
  // CHECK: spirv.SDiv
  %3 = index.divs %a, %b
  // CHECK: spirv.UDiv
  %4 = index.divu %a, %b
  // CHECK: spirv.SRem
  %5 = index.rems %a, %b
  // CHECK: spirv.UMod
  %6 = index.remu %a, %b
  // CHECK: spirv.GL.SMax
  %7 = index.maxs %a, %b
  // CHECK: spirv.GL.UMax
  %8 = index.maxu %a, %b
  // CHECK: spirv.GL.SMin
  %9 = index.mins %a, %b
  // CHECK: spirv.GL.UMin
  %10 = index.minu %a, %b
  // CHECK: spirv.ShiftLeftLogical
  %11 = index.shl %a, %b
  // CHECK: spirv.ShiftRightArithmetic
  %12 = index.shrs %a, %b
  // CHECK: spirv.ShiftRightLogical
  %13 = index.shru %a, %b
  return
}

// CHECK-LABEL: @bitwise_ops
func.func @bitwise_ops(%a: index, %b: index) {
  // CHECK: spirv.BitwiseAnd
  %0 = index.and %a, %b
  // CHECK: spirv.BitwiseOr
  %1 = index.or %a, %b
  // CHECK: spirv.BitwiseXor
  %2 = index.xor %a, %b
  return
}

// INDEX32-LABEL: @constant_ops
// INDEX64-LABEL: @constant_ops
func.func @constant_ops() {
  // INDEX32: spirv.Constant 42 : i32
  // INDEX64: spirv.Constant 42 : i64
  %0 = index.constant 42
  // INDEX32: spirv.Constant true
  // INDEX64: spirv.Constant true
  %1 = index.bool.constant true
  // INDEX32: spirv.Constant false
  // INDEX64: spirv.Constant false
  %2 = index.bool.constant false
  return
}

// CHECK-LABEL: @ceildivs
// CHECK-SAME: %[[NI:.*]]: index, %[[MI:.*]]: index
func.func @ceildivs(%n: index, %m: index) -> index {
  // CHECK-DAG: %[[N:.*]] = builtin.unrealized_conversion_cast %[[NI]]
  // CHECK-DAG: %[[M:.*]] = builtin.unrealized_conversion_cast %[[MI]]
  // CHECK: %[[ZERO:.*]] = spirv.Constant 0
  // CHECK: %[[POS_ONE:.*]] = spirv.Constant 1
  // CHECK: %[[NEG_ONE:.*]] = spirv.Constant -1

  // CHECK: %[[M_POS:.*]] = spirv.SGreaterThan %[[M]], %[[ZERO]]
  // CHECK: %[[X:.*]] = spirv.Select %[[M_POS]], %[[NEG_ONE]], %[[POS_ONE]]

  // CHECK: %[[N_PLUS_X:.*]] = spirv.IAdd %[[N]], %[[X]]
  // CHECK: %[[N_PLUS_X_DIV_M:.*]] = spirv.SDiv %[[N_PLUS_X]], %[[M]]
  // CHECK: %[[POS_RES:.*]] = spirv.IAdd %[[N_PLUS_X_DIV_M]], %[[POS_ONE]]

  // CHECK: %[[NEG_N:.*]] = spirv.ISub %[[ZERO]], %[[N]]
  // CHECK: %[[NEG_N_DIV_M:.*]] = spirv.SDiv %[[NEG_N]], %[[M]]
  // CHECK: %[[NEG_RES:.*]] = spirv.ISub %[[ZERO]], %[[NEG_N_DIV_M]]

  // CHECK: %[[N_POS:.*]] = spirv.SGreaterThan %[[N]], %[[ZERO]]
  // CHECK: %[[SAME_SIGN:.*]] = spirv.LogicalEqual %[[N_POS]], %[[M_POS]]
  // CHECK: %[[N_NON_ZERO:.*]] = spirv.INotEqual %[[N]], %[[ZERO]]
  // CHECK: %[[CMP:.*]] = spirv.LogicalAnd %[[SAME_SIGN]], %[[N_NON_ZERO]]
  // CHECK: %[[RESULT:.*]] = spirv.Select %[[CMP]], %[[POS_RES]], %[[NEG_RES]]
  %result = index.ceildivs %n, %m

  // %[[RESULTI:.*] = builtin.unrealized_conversion_cast %[[RESULT]]
  // return %[[RESULTI]]
  return %result : index
}

// CHECK-LABEL: @ceildivu
// CHECK-SAME: %[[NI:.*]]: index, %[[MI:.*]]: index
func.func @ceildivu(%n: index, %m: index) -> index {
  // CHECK-DAG: %[[N:.*]] = builtin.unrealized_conversion_cast %[[NI]]
  // CHECK-DAG: %[[M:.*]] = builtin.unrealized_conversion_cast %[[MI]]
  // CHECK: %[[ZERO:.*]] = spirv.Constant 0
  // CHECK: %[[ONE:.*]] = spirv.Constant 1

  // CHECK: %[[N_MINUS_ONE:.*]] = spirv.ISub %[[N]], %[[ONE]]
  // CHECK: %[[N_MINUS_ONE_DIV_M:.*]] = spirv.UDiv %[[N_MINUS_ONE]], %[[M]]
  // CHECK: %[[N_MINUS_ONE_DIV_M_PLUS_ONE:.*]] = spirv.IAdd %[[N_MINUS_ONE_DIV_M]], %[[ONE]]

  // CHECK: %[[CMP:.*]] = spirv.IEqual %[[N]], %[[ZERO]]
  // CHECK: %[[RESULT:.*]] = spirv.Select %[[CMP]], %[[ZERO]], %[[N_MINUS_ONE_DIV_M_PLUS_ONE]]
  %result = index.ceildivu %n, %m

  // %[[RESULTI:.*] = builtin.unrealized_conversion_cast %[[RESULT]]
  // return %[[RESULTI]]
  return %result : index
}

// CHECK-LABEL: @floordivs
// CHECK-SAME: %[[NI:.*]]: index, %[[MI:.*]]: index
func.func @floordivs(%n: index, %m: index) -> index {
  // CHECK-DAG: %[[N:.*]] = builtin.unrealized_conversion_cast %[[NI]]
  // CHECK-DAG: %[[M:.*]] = builtin.unrealized_conversion_cast %[[MI]]
  // CHECK: %[[ZERO:.*]] = spirv.Constant 0
  // CHECK: %[[POS_ONE:.*]] = spirv.Constant 1
  // CHECK: %[[NEG_ONE:.*]] = spirv.Constant -1

  // CHECK: %[[M_NEG:.*]] = spirv.SLessThan %[[M]], %[[ZERO]]
  // CHECK: %[[X:.*]] = spirv.Select %[[M_NEG]], %[[POS_ONE]], %[[NEG_ONE]]

  // CHECK: %[[X_MINUS_N:.*]] = spirv.ISub %[[X]], %[[N]]
  // CHECK: %[[X_MINUS_N_DIV_M:.*]] = spirv.SDiv %[[X_MINUS_N]], %[[M]]
  // CHECK: %[[NEG_RES:.*]] = spirv.ISub %[[NEG_ONE]], %[[X_MINUS_N_DIV_M]]

  // CHECK: %[[POS_RES:.*]] = spirv.SDiv %[[N]], %[[M]]

  // CHECK: %[[N_NEG:.*]] = spirv.SLessThan %[[N]], %[[ZERO]]
  // CHECK: %[[DIFF_SIGN:.*]] = spirv.LogicalNotEqual %[[N_NEG]], %[[M_NEG]]
  // CHECK: %[[N_NON_ZERO:.*]] = spirv.INotEqual %[[N]], %[[ZERO]]

  // CHECK: %[[CMP:.*]] = spirv.LogicalAnd %[[DIFF_SIGN]], %[[N_NON_ZERO]]
  // CHECK: %[[RESULT:.*]] = spirv.Select %[[CMP]], %[[POS_RES]], %[[NEG_RES]]
  %result = index.floordivs %n, %m

  // %[[RESULTI:.*] = builtin.unrealized_conversion_cast %[[RESULT]]
  // return %[[RESULTI]]
  return %result : index
}

// CHECK-LABEL: @index_cmp
func.func @index_cmp(%a : index, %b : index) {
  // CHECK: spirv.IEqual
  %0 = index.cmp eq(%a, %b)
  // CHECK: spirv.INotEqual
  %1 = index.cmp ne(%a, %b)

  // CHECK: spirv.SLessThan
  %2 = index.cmp slt(%a, %b)
  // CHECK: spirv.SLessThanEqual
  %3 = index.cmp sle(%a, %b)
  // CHECK: spirv.SGreaterThan
  %4 = index.cmp sgt(%a, %b)
  // CHECK: spirv.SGreaterThanEqual
  %5 = index.cmp sge(%a, %b)

  // CHECK: spirv.ULessThan
  %6 = index.cmp ult(%a, %b)
  // CHECK: spirv.ULessThanEqual
  %7 = index.cmp ule(%a, %b)
  // CHECK: spirv.UGreaterThan
  %8 = index.cmp ugt(%a, %b)
  // CHECK: spirv.UGreaterThanEqual
  %9 = index.cmp uge(%a, %b)
  return
}

// CHECK-LABEL: @index_sizeof
func.func @index_sizeof() {
  // CHECK: spirv.Constant 32 : i32
  %0 = index.sizeof
  return
}

// INDEX32-LABEL: @index_cast_from
// INDEX64-LABEL: @index_cast_from
// INDEX32-SAME: %[[AI:.*]]: index
// INDEX64-SAME: %[[AI:.*]]: index
func.func @index_cast_from(%a: index) -> (i64, i32, i64, i32) {
  // INDEX32: %[[A:.*]] = builtin.unrealized_conversion_cast %[[AI]] : index to i32
  // INDEX64: %[[A:.*]] = builtin.unrealized_conversion_cast %[[AI]] : index to i64

  // INDEX32: %[[V0:.*]] = spirv.SConvert %[[A]] : i32 to i64
  %0 = index.casts %a : index to i64
  // INDEX64: %[[V1:.*]] = spirv.SConvert %[[A]] : i64 to i32
  %1 = index.casts %a : index to i32
  // INDEX32: %[[V2:.*]] = spirv.UConvert %[[A]] : i32 to i64
  %2 = index.castu %a : index to i64
  // INDEX64: %[[V3:.*]] = spirv.UConvert %[[A]] : i64 to i32
  %3 = index.castu %a : index to i32

  // INDEX32: return %[[V0]], %[[A]], %[[V2]], %[[A]]
  // INDEX64: return %[[A]], %[[V1]], %[[A]], %[[V3]]
  return %0, %1, %2, %3 : i64, i32, i64, i32
}

// INDEX32-LABEL: @index_cast_to
// INDEX64-LABEL: @index_cast_to
// INDEX32-SAME: %[[A:.*]]: i32, %[[B:.*]]: i64
// INDEX64-SAME: %[[A:.*]]: i32, %[[B:.*]]: i64
func.func @index_cast_to(%a: i32, %b: i64) -> (index, index, index, index) {
  // INDEX64: %[[V0:.*]] = spirv.SConvert %[[A]] : i32 to i64
  %0 = index.casts %a : i32 to index
  // INDEX32: %[[V1:.*]] = spirv.SConvert %[[B]] : i64 to i32
  %1 = index.casts %b : i64 to index
  // INDEX64: %[[V2:.*]] = spirv.UConvert %[[A]] : i32 to i64
  %2 = index.castu %a : i32 to index
  // INDEX32: %[[V3:.*]] = spirv.UConvert %[[B]] : i64 to i32
  %3 = index.castu %b : i64 to index
  return %0, %1, %2, %3 : index, index, index, index
}
}