Skip to content

Commit 3a6403a

Browse files
committed
[mlir][memref]: Fix Bug in GlobalOp Verifier
When comparing the type of the initializer in a `memref::GlobalOp` against its result only consider the element type and the shape. Other attributes such as memory space should be ignored since comparing these between tensors and memrefs doesn't make sense and constructing a memref in a specific memory space with a tensor that has no such attribute should be valid. Signed-off-by: Jack Frankland <[email protected]>
1 parent 50a7511 commit 3a6403a

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

mlir/lib/Dialect/MemRef/IR/MemRefOps.cpp

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1567,11 +1567,23 @@ LogicalResult GlobalOp::verify() {
15671567
// Check that the type of the initial value is compatible with the type of
15681568
// the global variable.
15691569
if (auto elementsAttr = llvm::dyn_cast<ElementsAttr>(initValue)) {
1570-
Type initType = elementsAttr.getType();
1571-
Type tensorType = getTensorTypeFromMemRefType(memrefType);
1572-
if (initType != tensorType)
1573-
return emitOpError("initial value expected to be of type ")
1574-
<< tensorType << ", but was of type " << initType;
1570+
// Check the element types match.
1571+
auto initElementType =
1572+
cast<TensorType>(elementsAttr.getType()).getElementType();
1573+
auto memrefElementType = memrefType.getElementType();
1574+
1575+
if (initElementType != memrefElementType)
1576+
return emitOpError("initial value element expected to be of type ")
1577+
<< memrefElementType << ", but was of type " << initElementType;
1578+
1579+
// Check the shapes match, given that memref globals can only produce
1580+
// statically shaped memrefs and elements literal type must have a static
1581+
// shape we can assume both types are shaped.
1582+
auto initShape = elementsAttr.getShapedType().getShape();
1583+
auto memrefShape = memrefType.getShape();
1584+
if (initShape != memrefShape)
1585+
return emitOpError("initial value shape expected to be ")
1586+
<< memrefShape << " but was " << initShape;
15751587
}
15761588
}
15771589

mlir/test/Dialect/MemRef/invalid.mlir

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,16 @@ memref.global "priate" constant @memref5 : memref<2xf32> = uninitialized
342342

343343
// -----
344344

345+
// expected-error @+1 {{op initial value element expected to be of type 'f16', but was of type 'f32'}}
346+
"memref.global"() <{constant, initial_value = dense<1.000000e+00> : tensor<1xf32>, sym_name = "memref6", sym_visibility = "private", type = memref<1xf16>}> : () -> ()
347+
348+
// -----
349+
350+
// expected-error @+1 {{op initial value shape expected to be 1, 2 but was 2, 2}}
351+
"memref.global"() <{constant, initial_value = dense<1.000000e+00> : tensor<2x2xf16>, sym_name = "memref7", sym_visibility = "private", type = memref<1x2xf16>}> : () -> ()
352+
353+
// -----
354+
345355
func.func @nonexistent_global_memref() {
346356
// expected-error @+1 {{'gv' does not reference a valid global memref}}
347357
%0 = memref.get_global @gv : memref<3xf32>

mlir/test/Dialect/MemRef/ops.mlir

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,9 @@ memref.global "private" @memref3 : memref<2xf32> = uninitialized
174174
// CHECK-LABEL: memref.global "private" constant @memref4 : memref<2xf32> = uninitialized
175175
memref.global "private" constant @memref4 : memref<2xf32> = uninitialized
176176

177+
// CHECK-LABEL: memref.global "private" constant @memref5 : memref<1xf16, 42 : i32> = dense<1.000000e+00>
178+
"memref.global"() <{constant, initial_value = dense<1.000000e+00> : tensor<1xf16>, sym_name = "memref5", sym_visibility = "private", type = memref<1xf16, 42 : i32>}> : () -> ()
179+
177180
// CHECK-LABEL: func @read_global_memref
178181
func.func @read_global_memref() {
179182
%0 = memref.get_global @memref0 : memref<2xf32>

0 commit comments

Comments
 (0)