MLIR  21.0.0git
ImageOps.cpp
Go to the documentation of this file.
1 //===- ImageOps.cpp - MLIR SPIR-V Image Ops ------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Defines the image operations in the SPIR-V dialect.
10 //
11 //===----------------------------------------------------------------------===//
12 
14 
15 using namespace mlir;
16 
17 //===----------------------------------------------------------------------===//
18 // Common utility functions
19 //===----------------------------------------------------------------------===//
20 
21 static LogicalResult verifyImageOperands(Operation *imageOp,
22  spirv::ImageOperandsAttr attr,
23  Operation::operand_range operands) {
24  if (!attr) {
25  if (operands.empty())
26  return success();
27 
28  return imageOp->emitError("the Image Operands should encode what operands "
29  "follow, as per Image Operands");
30  }
31 
32  // TODO: Add the validation rules for the following Image Operands.
33  spirv::ImageOperands noSupportOperands =
34  spirv::ImageOperands::Bias | spirv::ImageOperands::Lod |
35  spirv::ImageOperands::Grad | spirv::ImageOperands::ConstOffset |
36  spirv::ImageOperands::Offset | spirv::ImageOperands::ConstOffsets |
37  spirv::ImageOperands::Sample | spirv::ImageOperands::MinLod |
38  spirv::ImageOperands::MakeTexelAvailable |
39  spirv::ImageOperands::MakeTexelVisible |
40  spirv::ImageOperands::SignExtend | spirv::ImageOperands::ZeroExtend;
41 
42  assert(!spirv::bitEnumContainsAny(attr.getValue(), noSupportOperands) &&
43  "unimplemented operands of Image Operands");
44  (void)noSupportOperands;
45 
46  return success();
47 }
48 
49 //===----------------------------------------------------------------------===//
50 // spirv.ImageDrefGather
51 //===----------------------------------------------------------------------===//
52 
53 LogicalResult spirv::ImageDrefGatherOp::verify() {
54  return verifyImageOperands(getOperation(), getImageOperandsAttr(),
55  getOperandArguments());
56 }
57 
58 //===----------------------------------------------------------------------===//
59 // spirv.ImageWriteOp
60 //===----------------------------------------------------------------------===//
61 
62 LogicalResult spirv::ImageWriteOp::verify() {
63  // TODO: Do we need check for: "If the Arrayed operand is 1, then additional
64  // capabilities may be required; e.g., ImageCubeArray, or ImageMSArray."?
65 
66  // TODO: Ideally it should be somewhere verified that "The Image Format must
67  // not be Unknown, unless the StorageImageWriteWithoutFormat Capability was
68  // declared." This function however may not be the suitable place for such
69  // verification.
70 
71  return verifyImageOperands(getOperation(), getImageOperandsAttr(),
72  getOperandArguments());
73 }
74 
75 //===----------------------------------------------------------------------===//
76 // spirv.ImageQuerySize
77 //===----------------------------------------------------------------------===//
78 
79 LogicalResult spirv::ImageQuerySizeOp::verify() {
80  spirv::ImageType imageType =
81  llvm::cast<spirv::ImageType>(getImage().getType());
82  Type resultType = getResult().getType();
83 
84  spirv::Dim dim = imageType.getDim();
85  spirv::ImageSamplingInfo samplingInfo = imageType.getSamplingInfo();
86  spirv::ImageSamplerUseInfo samplerInfo = imageType.getSamplerUseInfo();
87  switch (dim) {
88  case spirv::Dim::Dim1D:
89  case spirv::Dim::Dim2D:
90  case spirv::Dim::Dim3D:
91  case spirv::Dim::Cube:
92  if (samplingInfo != spirv::ImageSamplingInfo::MultiSampled &&
93  samplerInfo != spirv::ImageSamplerUseInfo::SamplerUnknown &&
94  samplerInfo != spirv::ImageSamplerUseInfo::NoSampler)
95  return emitError(
96  "if Dim is 1D, 2D, 3D, or Cube, "
97  "it must also have either an MS of 1 or a Sampled of 0 or 2");
98  break;
99  case spirv::Dim::Buffer:
100  case spirv::Dim::Rect:
101  break;
102  default:
103  return emitError("the Dim operand of the image type must "
104  "be 1D, 2D, 3D, Buffer, Cube, or Rect");
105  }
106 
107  unsigned componentNumber = 0;
108  switch (dim) {
109  case spirv::Dim::Dim1D:
110  case spirv::Dim::Buffer:
111  componentNumber = 1;
112  break;
113  case spirv::Dim::Dim2D:
114  case spirv::Dim::Cube:
115  case spirv::Dim::Rect:
116  componentNumber = 2;
117  break;
118  case spirv::Dim::Dim3D:
119  componentNumber = 3;
120  break;
121  default:
122  break;
123  }
124 
125  if (imageType.getArrayedInfo() == spirv::ImageArrayedInfo::Arrayed)
126  componentNumber += 1;
127 
128  unsigned resultComponentNumber = 1;
129  if (auto resultVectorType = llvm::dyn_cast<VectorType>(resultType))
130  resultComponentNumber = resultVectorType.getNumElements();
131 
132  if (componentNumber != resultComponentNumber)
133  return emitError("expected the result to have ")
134  << componentNumber << " component(s), but found "
135  << resultComponentNumber << " component(s)";
136 
137  return success();
138 }
static LogicalResult verifyImageOperands(Operation *imageOp, spirv::ImageOperandsAttr attr, Operation::operand_range operands)
Definition: ImageOps.cpp:21
This class implements the operand iterators for the Operation class.
Definition: ValueRange.h:42
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
InFlightDiagnostic emitError(const Twine &message={})
Emit an error about fatal conditions with this operation, reporting up to any diagnostic handlers tha...
Definition: Operation.cpp:268
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition: Types.h:74
ImageArrayedInfo getArrayedInfo() const
Definition: SPIRVTypes.cpp:351
ImageSamplerUseInfo getSamplerUseInfo() const
Definition: SPIRVTypes.cpp:359
ImageSamplingInfo getSamplingInfo() const
Definition: SPIRVTypes.cpp:355
Include the generated interface declarations.
Type getType(OpFoldResult ofr)
Returns the int type of the integer in ofr.
Definition: Utils.cpp:305
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
LogicalResult verify(Operation *op, bool verifyRecursively=true)
Perform (potentially expensive) checks of invariants, used to detect compiler bugs,...
Definition: Verifier.cpp:425