[MLIR][EmitC] Add empty emitc.constant check
authorKiung Jung <answeqr@gmail.com>
Thu, 27 Apr 2023 15:52:59 +0000 (15:52 +0000)
committerMarius Brehler <marius.brehler@iml.fraunhofer.de>
Thu, 27 Apr 2023 16:03:53 +0000 (16:03 +0000)
Implementing logic to check if the emitc dialect constant Op is empty.

Reviewed By: marbre

Differential Revision: https://reviews.llvm.org/D147907

mlir/include/mlir/Dialect/EmitC/IR/EmitC.td
mlir/lib/Dialect/EmitC/IR/EmitC.cpp
mlir/test/Dialect/EmitC/invalid_ops.mlir

index b3765b0..4851d96 100644 (file)
@@ -142,7 +142,6 @@ def EmitC_ConstantOp : EmitC_Op<"constant", [ConstantLike]> {
   let results = (outs AnyType);
 
   let hasFolder = 1;
-  // TODO: Disallow empty constants.
   let hasVerifier = 1;
 }
 
index fa6919f..7190476 100644 (file)
@@ -125,6 +125,11 @@ LogicalResult emitc::ConstantOp::verify() {
   if (getValueAttr().isa<emitc::OpaqueAttr>())
     return success();
 
+  // Value must not be empty
+  StringAttr strAttr = getValueAttr().dyn_cast<StringAttr>();
+  if (strAttr && strAttr.getValue().empty())
+    return emitOpError() << "value must not be empty";
+
   auto value = cast<TypedAttr>(getValueAttr());
   Type type = getType();
   if (!value.getType().isa<NoneType>() && type != value.getType())
index 421a5fb..377c84e 100644 (file)
@@ -16,6 +16,14 @@ func.func @const_attribute_return_type_2() {
 
 // -----
 
+func.func @empty_constant() {
+    // expected-error @+1 {{'emitc.constant' op value must not be empty}}
+    %c0 = "emitc.constant"(){value = ""} : () -> i32
+    return
+}
+
+// -----
+
 func.func @index_args_out_of_range_1() {
     // expected-error @+1 {{'emitc.call' op index argument is out of range}}
     emitc.call "test" () {args = [0 : index]} : () -> ()