Skip to content

[mlir][memref]: Fix Bug in GlobalOp Verifier #144900

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 1 commit into from
Jun 25, 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
22 changes: 17 additions & 5 deletions mlir/lib/Dialect/MemRef/IR/MemRefOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1567,11 +1567,23 @@ LogicalResult GlobalOp::verify() {
// Check that the type of the initial value is compatible with the type of
// the global variable.
if (auto elementsAttr = llvm::dyn_cast<ElementsAttr>(initValue)) {
Type initType = elementsAttr.getType();
Type tensorType = getTensorTypeFromMemRefType(memrefType);
if (initType != tensorType)
return emitOpError("initial value expected to be of type ")
<< tensorType << ", but was of type " << initType;
// Check the element types match.
auto initElementType =
cast<TensorType>(elementsAttr.getType()).getElementType();
auto memrefElementType = memrefType.getElementType();

if (initElementType != memrefElementType)
return emitOpError("initial value element expected to be of type ")
<< memrefElementType << ", but was of type " << initElementType;

// Check the shapes match, given that memref globals can only produce
// statically shaped memrefs and elements literal type must have a static
// shape we can assume both types are shaped.
auto initShape = elementsAttr.getShapedType().getShape();
auto memrefShape = memrefType.getShape();
if (initShape != memrefShape)
return emitOpError("initial value shape expected to be ")
<< memrefShape << " but was " << initShape;
}
}

Expand Down
10 changes: 10 additions & 0 deletions mlir/test/Dialect/MemRef/invalid.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,16 @@ memref.global "priate" constant @memref5 : memref<2xf32> = uninitialized

// -----

// expected-error @+1 {{op initial value element expected to be of type 'f16', but was of type 'f32'}}
"memref.global"() <{constant, initial_value = dense<1.000000e+00> : tensor<1xf32>, sym_name = "memref6", sym_visibility = "private", type = memref<1xf16>}> : () -> ()

// -----

// expected-error @+1 {{op initial value shape expected to be 1, 2 but was 2, 2}}
"memref.global"() <{constant, initial_value = dense<1.000000e+00> : tensor<2x2xf16>, sym_name = "memref7", sym_visibility = "private", type = memref<1x2xf16>}> : () -> ()

// -----

func.func @nonexistent_global_memref() {
// expected-error @+1 {{'gv' does not reference a valid global memref}}
%0 = memref.get_global @gv : memref<3xf32>
Expand Down
3 changes: 3 additions & 0 deletions mlir/test/Dialect/MemRef/ops.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ memref.global "private" @memref3 : memref<2xf32> = uninitialized
// CHECK-LABEL: memref.global "private" constant @memref4 : memref<2xf32> = uninitialized
memref.global "private" constant @memref4 : memref<2xf32> = uninitialized

// CHECK-LABEL: memref.global "private" constant @memref5 : memref<1xf16, 42 : i32> = dense<1.000000e+00>
"memref.global"() <{constant, initial_value = dense<1.000000e+00> : tensor<1xf16>, sym_name = "memref5", sym_visibility = "private", type = memref<1xf16, 42 : i32>}> : () -> ()

// CHECK-LABEL: func @read_global_memref
func.func @read_global_memref() {
%0 = memref.get_global @memref0 : memref<2xf32>
Expand Down