Skip to content

Commit fdbadbe

Browse files
committed
refactor: check for buffer validity and emit errors instead of match failures
1 parent 1e242db commit fdbadbe

File tree

2 files changed

+45
-5
lines changed

2 files changed

+45
-5
lines changed

mlir/lib/Conversion/ArithToSPIRV/ArithToSPIRV.cpp

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,10 @@ struct ConstantCompositeOpPattern final
230230
if (!srcType || srcType.getNumElements() == 1)
231231
return failure();
232232

233-
// arith.constant should only have vector or tensor types.
234-
assert((isa<VectorType, RankedTensorType>(srcType)));
233+
// arith.constant should only have vector or tensor types. This is a MLIR
234+
// wide problem at the moment.
235+
if (!isa<VectorType, RankedTensorType>(srcType))
236+
return rewriter.notifyMatchFailure(constOp, "unsupported ShapedType");
235237

236238
Type dstType = getTypeConverter()->convertType(srcType);
237239
if (!dstType)
@@ -246,12 +248,22 @@ struct ConstantCompositeOpPattern final
246248
} else if (auto resourceAttr =
247249
dyn_cast<DenseResourceElementsAttr>(constOp.getValue())) {
248250

249-
ArrayRef<char> ptr = resourceAttr.getRawHandle().getBlob()->getData();
251+
AsmResourceBlob *blob = resourceAttr.getRawHandle().getBlob();
252+
if (!blob)
253+
return constOp->emitError("could not find resource blob");
254+
255+
ArrayRef<char> ptr = blob->getData();
256+
257+
// Check that the buffer meets the requirements to get converted to a
258+
// DenseElementsAttr
259+
bool detectedSplat = false;
260+
if (!DenseElementsAttr::isValidRawBuffer(srcType, ptr, detectedSplat))
261+
return constOp->emitError("resource is not a valid buffer");
262+
250263
dstElementsAttr =
251264
DenseElementsAttr::getFromRawBuffer(resourceAttr.getType(), ptr);
252265
} else {
253-
return rewriter.notifyMatchFailure(constOp,
254-
"Could not decode ElementsAttr");
266+
return constOp->emitError("unsupported elements attribute");
255267
}
256268

257269
ShapedType dstAttrType = dstElementsAttr.getType();

mlir/test/Conversion/ArithToSPIRV/arith-to-spirv-unsupported.mlir

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,34 @@ func.func @unsupported_constant_tensor_2xf64_0() {
9595
return
9696
}
9797

98+
// -----
99+
100+
func.func @constant_dense_resource_non_existant() {
101+
// expected-error @+2 {{failed to legalize operation 'arith.constant'}}
102+
// expected-error @+1 {{could not find resource blob}}
103+
%0 = arith.constant dense_resource<non_existant> : tensor<5xf32>
104+
return
105+
}
106+
107+
// -----
108+
109+
module {
110+
func.func @constant_dense_resource_invalid_buffer() {
111+
// expected-error @+2 {{failed to legalize operation 'arith.constant'}}
112+
// expected-error @+1 {{resource is not a valid buffer}}
113+
%0 = arith.constant dense_resource<dense_resource_test_2xi32> : vector<2xi32>
114+
return
115+
}
116+
}
117+
// This is a buffer of wrong type and shape
118+
{-#
119+
dialect_resources: {
120+
builtin: {
121+
dense_resource_test_2xi32: "0x0800000054A3B53ED6C0B33E55D1A2BDE5D2BB3E"
122+
}
123+
}
124+
#-}
125+
98126
///===----------------------------------------------------------------------===//
99127
// Type emulation
100128
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)