-
Notifications
You must be signed in to change notification settings - Fork 13.3k
/
Copy pathImageOps.cpp
317 lines (259 loc) · 12.4 KB
/
ImageOps.cpp
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
//===- ImageOps.cpp - MLIR SPIR-V Image Ops ------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// Defines the image operations in the SPIR-V dialect.
//
//===----------------------------------------------------------------------===//
#include "mlir/Dialect/SPIRV/IR/SPIRVOps.h"
using namespace mlir;
//===----------------------------------------------------------------------===//
// Common utility functions
//===----------------------------------------------------------------------===//
// TODO: In the future we should model image operands better, so we can move
// some verification into ODS.
static LogicalResult verifyImageOperands(Operation *imageOp,
spirv::ImageOperandsAttr attr,
Operation::operand_range operands) {
if (!attr) {
if (operands.empty())
return success();
return imageOp->emitError("the Image Operands should encode what operands "
"follow, as per Image Operands");
}
if (spirv::bitEnumContainsAll(attr.getValue(),
spirv::ImageOperands::Lod |
spirv::ImageOperands::Grad))
return imageOp->emitError(
"it is invalid to set both the Lod and Grad bits");
size_t index = 0;
// The order we process operands is important. In case of multiple argument
// taking operands, the arguments are ordered starting with operands having
// smaller-numbered bits first.
if (spirv::bitEnumContainsAny(attr.getValue(), spirv::ImageOperands::Bias)) {
if (!isa<spirv::ImplicitLodOpInterface>(imageOp))
return imageOp->emitError(
"Bias is only valid with implicit-lod instructions");
if (index + 1 > operands.size())
return imageOp->emitError("Bias operand requires 1 argument");
if (!isa<FloatType>(operands[index].getType()))
return imageOp->emitError("Bias must be a floating-point type scalar");
auto samplingOp = cast<spirv::SamplingOpInterface>(imageOp);
auto sampledImageType =
cast<spirv::SampledImageType>(samplingOp.getSampledImage().getType());
auto imageType = cast<spirv::ImageType>(sampledImageType.getImageType());
if (!llvm::is_contained({spirv::Dim::Dim1D, spirv::Dim::Dim2D,
spirv::Dim::Dim3D, spirv::Dim::Cube},
imageType.getDim()))
return imageOp->emitError(
"Bias must only be used with an image type that has "
"a dim operand of 1D, 2D, 3D, or Cube");
if (imageType.getSamplingInfo() != spirv::ImageSamplingInfo::SingleSampled)
return imageOp->emitError("Bias must only be used with an image type "
"that has a MS operand of 0");
++index;
}
if (spirv::bitEnumContainsAny(attr.getValue(), spirv::ImageOperands::Lod)) {
if (!isa<spirv::ExplicitLodOpInterface>(imageOp) &&
!isa<spirv::FetchOpInterface>(imageOp))
return imageOp->emitError(
"Lod is only valid with explicit-lod and fetch instructions");
if (index + 1 > operands.size())
return imageOp->emitError("Lod operand requires 1 argument");
spirv::ImageType imageType;
if (isa<spirv::SamplingOpInterface>(imageOp)) {
if (!isa<mlir::FloatType>(operands[index].getType()))
return imageOp->emitError("for sampling operations, Lod must be a "
"floating-point type scalar");
auto samplingOp = cast<spirv::SamplingOpInterface>(imageOp);
auto sampledImageType = llvm::cast<spirv::SampledImageType>(
samplingOp.getSampledImage().getType());
imageType = cast<spirv::ImageType>(sampledImageType.getImageType());
} else {
if (!isa<mlir::IntegerType>(operands[index].getType()))
return imageOp->emitError(
"for fetch operations, Lod must be an integer type scalar");
auto fetchOp = cast<spirv::FetchOpInterface>(imageOp);
imageType = cast<spirv::ImageType>(fetchOp.getImage().getType());
}
if (!llvm::is_contained({spirv::Dim::Dim1D, spirv::Dim::Dim2D,
spirv::Dim::Dim3D, spirv::Dim::Cube},
imageType.getDim()))
return imageOp->emitError(
"Lod must only be used with an image type that has "
"a dim operand of 1D, 2D, 3D, or Cube");
if (imageType.getSamplingInfo() != spirv::ImageSamplingInfo::SingleSampled)
return imageOp->emitError("Lod must only be used with an image type that "
"has a MS operand of 0");
++index;
}
if (spirv::bitEnumContainsAny(attr.getValue(), spirv::ImageOperands::Grad)) {
if (!isa<spirv::ExplicitLodOpInterface>(imageOp))
return imageOp->emitError(
"Grad is only valid with explicit-lod instructions");
if (index + 2 > operands.size())
return imageOp->emitError(
"Grad operand requires 2 arguments (scalars or vectors)");
auto samplingOp = cast<spirv::SamplingOpInterface>(imageOp);
auto sampledImageType =
cast<spirv::SampledImageType>(samplingOp.getSampledImage().getType());
auto imageType = cast<spirv::ImageType>(sampledImageType.getImageType());
if (imageType.getSamplingInfo() != spirv::ImageSamplingInfo::SingleSampled)
return imageOp->emitError("Grad must only be used with an image type "
"that has a MS operand of 0");
int64_t numberOfComponents = 0;
auto coordVector =
dyn_cast<mlir::VectorType>(samplingOp.getCoordinate().getType());
if (coordVector) {
numberOfComponents = coordVector.getNumElements();
if (imageType.getArrayedInfo() == spirv::ImageArrayedInfo::Arrayed)
numberOfComponents -= 1;
} else {
numberOfComponents = 1;
}
assert(numberOfComponents > 0);
auto dXVector = dyn_cast<mlir::VectorType>(operands[index].getType());
auto dYVector = dyn_cast<mlir::VectorType>(operands[index + 1].getType());
if (dXVector && dYVector) {
if (dXVector.getNumElements() != dYVector.getNumElements() ||
dXVector.getNumElements() != numberOfComponents)
return imageOp->emitError(
"number of components of each Grad argument must equal the number "
"of components in coordinate, minus the array layer component, if "
"present");
if (!isa<mlir::FloatType>(dXVector.getElementType()) ||
!isa<mlir::FloatType>(dYVector.getElementType()))
return imageOp->emitError(
"Grad arguments must be a vector of floating-point type");
} else if (isa<mlir::FloatType>(operands[index].getType()) &&
isa<mlir::FloatType>(operands[index + 1].getType())) {
if (numberOfComponents != 1)
return imageOp->emitError(
"number of components of each Grad argument must equal the number "
"of components in coordinate, minus the array layer component, if "
"present");
} else {
return imageOp->emitError(
"Grad arguments must be a scalar or vector of floating-point type");
}
index += 2;
}
// TODO: Add the validation rules for the following Image Operands.
spirv::ImageOperands noSupportOperands =
spirv::ImageOperands::ConstOffset | spirv::ImageOperands::Offset |
spirv::ImageOperands::ConstOffsets | spirv::ImageOperands::Sample |
spirv::ImageOperands::MinLod | spirv::ImageOperands::MakeTexelAvailable |
spirv::ImageOperands::MakeTexelVisible |
spirv::ImageOperands::SignExtend | spirv::ImageOperands::ZeroExtend;
assert(!spirv::bitEnumContainsAny(attr.getValue(), noSupportOperands) &&
"unimplemented operands of Image Operands");
(void)noSupportOperands;
if (index < operands.size())
return imageOp->emitError(
"too many image operand arguments have been provided");
return success();
}
//===----------------------------------------------------------------------===//
// spirv.ImageDrefGather
//===----------------------------------------------------------------------===//
LogicalResult spirv::ImageDrefGatherOp::verify() {
return verifyImageOperands(getOperation(), getImageOperandsAttr(),
getOperandArguments());
}
//===----------------------------------------------------------------------===//
// spirv.ImageWriteOp
//===----------------------------------------------------------------------===//
LogicalResult spirv::ImageWriteOp::verify() {
// TODO: Do we need check for: "If the Arrayed operand is 1, then additional
// capabilities may be required; e.g., ImageCubeArray, or ImageMSArray."?
// TODO: Ideally it should be somewhere verified that "The Image Format must
// not be Unknown, unless the StorageImageWriteWithoutFormat Capability was
// declared." This function however may not be the suitable place for such
// verification.
return verifyImageOperands(getOperation(), getImageOperandsAttr(),
getOperandArguments());
}
//===----------------------------------------------------------------------===//
// spirv.ImageQuerySize
//===----------------------------------------------------------------------===//
LogicalResult spirv::ImageQuerySizeOp::verify() {
spirv::ImageType imageType =
llvm::cast<spirv::ImageType>(getImage().getType());
Type resultType = getResult().getType();
spirv::Dim dim = imageType.getDim();
spirv::ImageSamplingInfo samplingInfo = imageType.getSamplingInfo();
spirv::ImageSamplerUseInfo samplerInfo = imageType.getSamplerUseInfo();
switch (dim) {
case spirv::Dim::Dim1D:
case spirv::Dim::Dim2D:
case spirv::Dim::Dim3D:
case spirv::Dim::Cube:
if (samplingInfo != spirv::ImageSamplingInfo::MultiSampled &&
samplerInfo != spirv::ImageSamplerUseInfo::SamplerUnknown &&
samplerInfo != spirv::ImageSamplerUseInfo::NoSampler)
return emitError(
"if Dim is 1D, 2D, 3D, or Cube, "
"it must also have either an MS of 1 or a Sampled of 0 or 2");
break;
case spirv::Dim::Buffer:
case spirv::Dim::Rect:
break;
default:
return emitError("the Dim operand of the image type must "
"be 1D, 2D, 3D, Buffer, Cube, or Rect");
}
unsigned componentNumber = 0;
switch (dim) {
case spirv::Dim::Dim1D:
case spirv::Dim::Buffer:
componentNumber = 1;
break;
case spirv::Dim::Dim2D:
case spirv::Dim::Cube:
case spirv::Dim::Rect:
componentNumber = 2;
break;
case spirv::Dim::Dim3D:
componentNumber = 3;
break;
default:
break;
}
if (imageType.getArrayedInfo() == spirv::ImageArrayedInfo::Arrayed)
componentNumber += 1;
unsigned resultComponentNumber = 1;
if (auto resultVectorType = llvm::dyn_cast<VectorType>(resultType))
resultComponentNumber = resultVectorType.getNumElements();
if (componentNumber != resultComponentNumber)
return emitError("expected the result to have ")
<< componentNumber << " component(s), but found "
<< resultComponentNumber << " component(s)";
return success();
}
//===----------------------------------------------------------------------===//
// spirv.ImageSampleImplicitLod
//===----------------------------------------------------------------------===//
LogicalResult spirv::ImageSampleImplicitLodOp::verify() {
return verifyImageOperands(getOperation(), getImageOperandsAttr(),
getOperandArguments());
}
//===----------------------------------------------------------------------===//
// spirv.ImageSampleExplicitLod
//===----------------------------------------------------------------------===//
LogicalResult spirv::ImageSampleExplicitLodOp::verify() {
// TODO: It should be verified somewhere that: "Unless the Kernel capability
// is declared, it [Coordinate] must be floating point."
return verifyImageOperands(getOperation(), getImageOperandsAttr(),
getOperandArguments());
}
//===----------------------------------------------------------------------===//
// spirv.ImageSampleProjDrefImplicitLod
//===----------------------------------------------------------------------===//
LogicalResult spirv::ImageSampleProjDrefImplicitLodOp::verify() {
return verifyImageOperands(getOperation(), getImageOperandsAttr(),
getOperandArguments());
}