Disallow non-index operands and results in affine.apply
authorAlex Zinenko <zinenko@google.com>
Thu, 13 Jun 2019 14:08:46 +0000 (07:08 -0700)
committerMehdi Amini <joker.eph@gmail.com>
Thu, 20 Jun 2019 06:00:53 +0000 (23:00 -0700)
`affine.apply` is supposed to operate on values of index types in context of
affine loops.  It is possible to programmatically constuct an `affine.apply`
that takes values of other types as operands or returns them, but it would not
be parseable.  Disallow such cases in the verifier.

PiperOrigin-RevId: 253021704

mlir/lib/AffineOps/AffineOps.cpp
mlir/test/AffineOps/invalid.mlir

index fc70807..26a35ad 100644 (file)
@@ -174,6 +174,15 @@ LogicalResult AffineApplyOp::verify() {
     return emitOpError(
         "operand count and affine map dimension and symbol count must match");
 
+  // Verify that all operands are of `index` type.
+  for (Type t : getOperandTypes()) {
+    if (!t.isIndex())
+      return emitOpError("operands must be of type 'index'");
+  }
+
+  if (!getResult()->getType().isIndex())
+    return emitOpError("result must be of type 'index'");
+
   // Verify that the operands are valid dimension and symbol identifiers.
   if (failed(verifyDimAndSymbolIdentifiers(*this, getOperands(),
                                            map.getNumDims())))
index 69260a7..748e0fe 100644 (file)
@@ -28,6 +28,26 @@ func @affine_apply_invalid_sym() {
 
 // -----
 
+func @affine_apply_operand_non_index(%arg0 : i32) {
+  // Custom parser automatically assigns all arguments the `index` so we must
+  // use the generic syntax here to exercise the verifier.
+  // expected-error@+1 {{operands must be of type 'index'}}
+  %0 = "affine.apply"(%arg0) {map: (d0) -> (d0)} : (i32) -> (index)
+  return
+}
+
+// -----
+
+func @affine_apply_resul_non_index(%arg0 : index) {
+  // Custom parser automatically assigns `index` as the result type so we must
+  // use the generic syntax here to exercise the verifier.
+  // expected-error@+1 {{result must be of type 'index'}}
+  %0 = "affine.apply"(%arg0) {map: (d0) -> (d0)} : (index) -> (i32)
+  return
+}
+
+// -----
+
 #map = (d0)[s0] -> (d0 + s0)
 
 func @affine_for_lower_bound_invalid_dim(%arg : index) {