Skip to content

[mlir][spirv] Add verification for Bias operand #134231

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 40 additions & 10 deletions mlir/lib/Dialect/SPIRV/IR/ImageOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,36 @@ static LogicalResult verifyImageOperands(Operation *imageOp,
// 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))
Expand Down Expand Up @@ -74,12 +104,13 @@ static LogicalResult verifyImageOperands(Operation *imageOp,
if (!llvm::is_contained({spirv::Dim::Dim1D, spirv::Dim::Dim2D,
spirv::Dim::Dim3D, spirv::Dim::Cube},
imageType.getDim()))
return imageOp->emitError("Lod only be used with an image type that has "
"a dim operand of 1D, 2D, 3D, or Cube");
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 only be used with an image type that has a MS operand of 0");
return imageOp->emitError("Lod must only be used with an image type that "
"has a MS operand of 0");

++index;
}
Expand All @@ -99,8 +130,8 @@ static LogicalResult verifyImageOperands(Operation *imageOp,
auto imageType = cast<spirv::ImageType>(sampledImageType.getImageType());

if (imageType.getSamplingInfo() != spirv::ImageSamplingInfo::SingleSampled)
return imageOp->emitError(
"Grad only be used with an image type that has a MS operand of 0");
return imageOp->emitError("Grad must only be used with an image type "
"that has a MS operand of 0");

int64_t numberOfComponents = 0;

Expand Down Expand Up @@ -147,10 +178,9 @@ static LogicalResult verifyImageOperands(Operation *imageOp,

// TODO: Add the validation rules for the following Image Operands.
spirv::ImageOperands noSupportOperands =
spirv::ImageOperands::Bias | spirv::ImageOperands::ConstOffset |
spirv::ImageOperands::Offset | spirv::ImageOperands::ConstOffsets |
spirv::ImageOperands::Sample | spirv::ImageOperands::MinLod |
spirv::ImageOperands::MakeTexelAvailable |
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;

Expand Down
32 changes: 31 additions & 1 deletion mlir/test/Dialect/SPIRV/IR/image-ops.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,36 @@ func.func @sample_implicit_proj_dref(%arg0 : !spirv.sampled_image<!spirv.image<f

// -----

//===----------------------------------------------------------------------===//
// spirv.ImageOperands: Bias
//===----------------------------------------------------------------------===//

func.func @bias_too_many_arguments(%arg0 : !spirv.sampled_image<!spirv.image<f32, Dim1D, NoDepth, NonArrayed, SingleSampled, NeedSampler, Rgba8>>, %arg1 : f32, %arg2 : f32) -> () {
// expected-error @+1 {{too many image operand arguments have been provided}}
%0 = spirv.ImageSampleImplicitLod %arg0, %arg1 ["Bias"], %arg2, %arg2 : !spirv.sampled_image<!spirv.image<f32, Dim1D, NoDepth, NonArrayed, SingleSampled, NeedSampler, Rgba8>>, f32, f32, f32 -> vector<4xf32>
spirv.Return
}

// -----

func.func @bias_too_many_arguments(%arg0 : !spirv.sampled_image<!spirv.image<f32, Dim1D, NoDepth, NonArrayed, SingleSampled, NeedSampler, Rgba8>>, %arg1 : f32, %arg2 : i32) -> () {
// expected-error @+1 {{Bias must be a floating-point type scalar}}
%0 = spirv.ImageSampleImplicitLod %arg0, %arg1 ["Bias"], %arg2 : !spirv.sampled_image<!spirv.image<f32, Dim1D, NoDepth, NonArrayed, SingleSampled, NeedSampler, Rgba8>>, f32, i32 -> vector<4xf32>
spirv.Return
}

// -----

func.func @bias_with_rect(%arg0 : !spirv.sampled_image<!spirv.image<f32, Rect, NoDepth, NonArrayed, SingleSampled, NeedSampler, Rgba8>>, %arg1 : f32, %arg2 : f32) -> () {
// expected-error @+1 {{Bias must only be used with an image type that has a dim operand of 1D, 2D, 3D, or Cube}}
%0 = spirv.ImageSampleImplicitLod %arg0, %arg1 ["Bias"], %arg2 : !spirv.sampled_image<!spirv.image<f32, Rect, NoDepth, NonArrayed, SingleSampled, NeedSampler, Rgba8>>, f32, f32 -> vector<4xf32>
spirv.Return
}

// TODO: We cannot currently test Bias with MS != 0 as all implemented implicit operations already check for that.

// -----

//===----------------------------------------------------------------------===//
// spirv.ImageOperands: Lod
//===----------------------------------------------------------------------===//
Expand Down Expand Up @@ -305,7 +335,7 @@ func.func @lod_too_many_arguments(%arg0 : !spirv.sampled_image<!spirv.image<f32,
// -----

func.func @lod_with_rect(%arg0 : !spirv.sampled_image<!spirv.image<f32, Rect, NoDepth, NonArrayed, SingleSampled, NeedSampler, Rgba8>>, %arg1 : vector<2xf32>, %arg2 : f32) -> () {
// expected-error @+1 {{Lod only be used with an image type that has a dim operand of 1D, 2D, 3D, or Cube}}
// expected-error @+1 {{Lod must only be used with an image type that has a dim operand of 1D, 2D, 3D, or Cube}}
%0 = spirv.ImageSampleExplicitLod %arg0, %arg1 ["Lod"], %arg2 : !spirv.sampled_image<!spirv.image<f32, Rect, NoDepth, NonArrayed, SingleSampled, NeedSampler, Rgba8>>, vector<2xf32>, f32 -> vector<4xf32>
spirv.Return
}
Expand Down