Skip to content

Commit 74090de

Browse files
authored
[mlir][spirv] Verify SampledImageType Dim (#159397)
This patches adds check for: "It [ImageType] must not have a Dim of SubpassData. Additionally, starting with version 1.6, it must not have a Dim of Buffer." as defined in "3.3.6. Type-Declaration Instructions" of SPIR-V spec.
1 parent 77028d6 commit 74090de

File tree

4 files changed

+36
-26
lines changed

4 files changed

+36
-26
lines changed

mlir/lib/Dialect/SPIRV/IR/SPIRVDialect.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,13 +251,21 @@ static Type parseAndVerifySampledImageType(SPIRVDialect const &dialect,
251251
if (parser.parseType(type))
252252
return Type();
253253

254-
if (!llvm::isa<ImageType>(type)) {
254+
auto imageType = dyn_cast<ImageType>(type);
255+
if (!imageType) {
255256
parser.emitError(typeLoc,
256257
"sampled image must be composed using image type, got ")
257258
<< type;
258259
return Type();
259260
}
260261

262+
if (llvm::is_contained({Dim::SubpassData, Dim::Buffer}, imageType.getDim())) {
263+
parser.emitError(
264+
typeLoc, "sampled image Dim must not be SubpassData or Buffer, got ")
265+
<< stringifyDim(imageType.getDim());
266+
return Type();
267+
}
268+
261269
return type;
262270
}
263271

mlir/lib/Dialect/SPIRV/IR/SPIRVTypes.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -805,9 +805,16 @@ Type SampledImageType::getImageType() const { return getImpl()->imageType; }
805805
LogicalResult
806806
SampledImageType::verifyInvariants(function_ref<InFlightDiagnostic()> emitError,
807807
Type imageType) {
808-
if (!llvm::isa<ImageType>(imageType))
808+
auto image = dyn_cast<ImageType>(imageType);
809+
if (!image)
809810
return emitError() << "expected image type";
810811

812+
// As per SPIR-V spec: "It [ImageType] must not have a Dim of SubpassData.
813+
// Additionally, starting with version 1.6, it must not have a Dim of Buffer.
814+
// ("3.3.6. Type-Declaration Instructions")
815+
if (llvm::is_contained({Dim::SubpassData, Dim::Buffer}, image.getDim()))
816+
return emitError() << "Dim must not be SubpassData or Buffer";
817+
811818
return success();
812819
}
813820

mlir/test/Dialect/SPIRV/IR/image-ops.mlir

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,9 @@ func.func @image_write_texel_type_mismatch(%arg0 : !spirv.image<f32, Dim2D, NoDe
192192
// spirv.ImageSampleExplicitLod
193193
//===----------------------------------------------------------------------===//
194194

195+
// No need to have a negative test for Dim being Buffer as this is already handled
196+
// by SampledImageType logic.
197+
195198
func.func @sample_explicit_lod(%arg0 : !spirv.sampled_image<!spirv.image<f32, Dim2D, NoDepth, NonArrayed, SingleSampled, NeedSampler, Rgba8>>, %arg1 : vector<2xf32>, %arg2 : f32) -> () {
196199
// CHECK: {{%.*}} = spirv.ImageSampleExplicitLod {{%.*}}, {{%.*}} ["Lod"], {{%.*}} : !spirv.sampled_image<!spirv.image<f32, Dim2D, NoDepth, NonArrayed, SingleSampled, NeedSampler, Rgba8>>, vector<2xf32>, f32 -> vector<4xf32>
197200
%0 = spirv.ImageSampleExplicitLod %arg0, %arg1 ["Lod"], %arg2 : !spirv.sampled_image<!spirv.image<f32, Dim2D, NoDepth, NonArrayed, SingleSampled, NeedSampler, Rgba8>>, vector<2xf32>, f32 -> vector<4xf32>
@@ -200,14 +203,6 @@ func.func @sample_explicit_lod(%arg0 : !spirv.sampled_image<!spirv.image<f32, Di
200203

201204
// -----
202205

203-
func.func @sample_explicit_lod_buffer_dim(%arg0 : !spirv.sampled_image<!spirv.image<f32, Buffer, NoDepth, NonArrayed, SingleSampled, NeedSampler, Rgba8>>, %arg1 : vector<2xf32>, %arg2 : f32) -> () {
204-
// expected-error @+1 {{the Dim operand of the underlying image must not be Buffer}}
205-
%0 = spirv.ImageSampleExplicitLod %arg0, %arg1 ["Lod"], %arg2 : !spirv.sampled_image<!spirv.image<f32, Buffer, NoDepth, NonArrayed, SingleSampled, NeedSampler, Rgba8>>, vector<2xf32>, f32 -> vector<4xf32>
206-
spirv.Return
207-
}
208-
209-
// -----
210-
211206
func.func @sample_explicit_lod_multi_sampled(%arg0 : !spirv.sampled_image<!spirv.image<f32, Dim2D, NoDepth, NonArrayed, MultiSampled, NeedSampler, Rgba8>>, %arg1 : vector<2xf32>, %arg2 : f32) -> () {
212207
// expected-error @+1 {{the MS operand of the underlying image type must be SingleSampled}}
213208
%0 = spirv.ImageSampleExplicitLod %arg0, %arg1 ["Lod"], %arg2 : !spirv.sampled_image<!spirv.image<f32, Dim2D, NoDepth, NonArrayed, MultiSampled, NeedSampler, Rgba8>>, vector<2xf32>, f32 -> vector<4xf32>
@@ -236,6 +231,9 @@ func.func @sample_explicit_lod_no_lod(%arg0 : !spirv.sampled_image<!spirv.image<
236231
// spirv.ImageSampleImplicitLod
237232
//===----------------------------------------------------------------------===//
238233

234+
// No need to have a negative test for Dim being Buffer as this is already handled
235+
// by SampledImageType logic.
236+
239237
func.func @sample_implicit_lod(%arg0 : !spirv.sampled_image<!spirv.image<f32, Dim2D, NoDepth, NonArrayed, SingleSampled, NeedSampler, Rgba8>>, %arg1 : vector<2xf32>) -> () {
240238
// CHECK: {{%.*}} = spirv.ImageSampleImplicitLod {{%.*}}, {{%.*}} : !spirv.sampled_image<!spirv.image<f32, Dim2D, NoDepth, NonArrayed, SingleSampled, NeedSampler, Rgba8>>, vector<2xf32> -> vector<4xf32>
241239
%0 = spirv.ImageSampleImplicitLod %arg0, %arg1 : !spirv.sampled_image<!spirv.image<f32, Dim2D, NoDepth, NonArrayed, SingleSampled, NeedSampler, Rgba8>>, vector<2xf32> -> vector<4xf32>
@@ -244,14 +242,6 @@ func.func @sample_implicit_lod(%arg0 : !spirv.sampled_image<!spirv.image<f32, Di
244242

245243
// -----
246244

247-
func.func @sample_implicit_lod_buffer(%arg0 : !spirv.sampled_image<!spirv.image<f32, Buffer, NoDepth, NonArrayed, SingleSampled, NeedSampler, Rgba8>>, %arg1 : vector<2xf32>) -> () {
248-
// expected-error @+1 {{the Dim operand of the underlying image must not be Buffer}}
249-
%0 = spirv.ImageSampleImplicitLod %arg0, %arg1 : !spirv.sampled_image<!spirv.image<f32, Buffer, NoDepth, NonArrayed, SingleSampled, NeedSampler, Rgba8>>, vector<2xf32> -> vector<4xf32>
250-
spirv.Return
251-
}
252-
253-
// -----
254-
255245
func.func @sample_implicit_lod_multi_sampled(%arg0 : !spirv.sampled_image<!spirv.image<f32, Dim2D, NoDepth, NonArrayed, MultiSampled, NeedSampler, Rgba8>>, %arg1 : vector<2xf32>) -> () {
256246
// expected-error @+1 {{the MS operand of the underlying image type must be SingleSampled}}
257247
%0 = spirv.ImageSampleImplicitLod %arg0, %arg1 : !spirv.sampled_image<!spirv.image<f32, Dim2D, NoDepth, NonArrayed, MultiSampled, NeedSampler, Rgba8>>, vector<2xf32> -> vector<4xf32>
@@ -272,6 +262,9 @@ func.func @sample_implicit_lod_wrong_result(%arg0 : !spirv.sampled_image<!spirv.
272262
// spirv.ImageSampleProjDrefImplicitLod
273263
//===----------------------------------------------------------------------===//
274264

265+
// No need to have a negative test for Dim being Buffer as this is already handled
266+
// by SampledImageType logic.
267+
275268
func.func @sample_implicit_proj_dref(%arg0 : !spirv.sampled_image<!spirv.image<f32, Dim2D, IsDepth, NonArrayed, SingleSampled, NeedSampler, Rgba8>>, %arg1 : vector<4xf32>, %arg2 : f32) -> () {
276269
// CHECK: {{%.*}} = spirv.ImageSampleProjDrefImplicitLod {{%.*}}, {{%.*}}, {{%.*}} : !spirv.sampled_image<!spirv.image<f32, Dim2D, IsDepth, NonArrayed, SingleSampled, NeedSampler, Rgba8>>, vector<4xf32>, f32 -> f32
277270
%0 = spirv.ImageSampleProjDrefImplicitLod %arg0, %arg1, %arg2 : !spirv.sampled_image<!spirv.image<f32, Dim2D, IsDepth, NonArrayed, SingleSampled, NeedSampler, Rgba8>>, vector<4xf32>, f32 -> f32
@@ -280,14 +273,6 @@ func.func @sample_implicit_proj_dref(%arg0 : !spirv.sampled_image<!spirv.image<f
280273

281274
// -----
282275

283-
func.func @sample_implicit_proj_dref_buffer_dim(%arg0 : !spirv.sampled_image<!spirv.image<f32, Buffer, IsDepth, NonArrayed, SingleSampled, NeedSampler, Rgba8>>, %arg1 : vector<4xf32>, %arg2 : f32) -> () {
284-
// expected-error @+1 {{the Dim operand of the underlying image must not be Buffer}}
285-
%0 = spirv.ImageSampleProjDrefImplicitLod %arg0, %arg1, %arg2 : !spirv.sampled_image<!spirv.image<f32, Buffer, IsDepth, NonArrayed, SingleSampled, NeedSampler, Rgba8>>, vector<4xf32>, f32 -> f32
286-
spirv.Return
287-
}
288-
289-
// -----
290-
291276
func.func @sample_implicit_proj_dref_multi_sampled(%arg0 : !spirv.sampled_image<!spirv.image<f32, Dim2D, IsDepth, NonArrayed, MultiSampled, NeedSampler, Rgba8>>, %arg1 : vector<4xf32>, %arg2 : f32) -> () {
292277
// expected-error @+1 {{the MS operand of the underlying image type must be SingleSampled}}
293278
%0 = spirv.ImageSampleProjDrefImplicitLod %arg0, %arg1, %arg2 : !spirv.sampled_image<!spirv.image<f32, Dim2D, IsDepth, NonArrayed, MultiSampled, NeedSampler, Rgba8>>, vector<4xf32>, f32 -> f32

mlir/test/Dialect/SPIRV/IR/types.mlir

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,16 @@ func.func private @samped_image_type_invaid_type(!spirv.sampled_image<f32>) -> (
238238

239239
// -----
240240

241+
// expected-error @+1 {{sampled image Dim must not be SubpassData or Buffer, got Buffer}}
242+
func.func private @sampled_image_type(!spirv.sampled_image<!spirv.image<f32, Buffer, NoDepth, NonArrayed, SingleSampled, NoSampler, Unknown>>) -> ()
243+
244+
// -----
245+
246+
// expected-error @+1 {{sampled image Dim must not be SubpassData or Buffer, got SubpassData}}
247+
func.func private @sampled_image_type(!spirv.sampled_image<!spirv.image<f32, SubpassData, NoDepth, NonArrayed, SingleSampled, NoSampler, Unknown>>) -> ()
248+
249+
// -----
250+
241251
//===----------------------------------------------------------------------===//
242252
// StructType
243253
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)