Add npcomp-verify-backend-contract pass.
This pass verifies that a given module satisfies the contract that we
have for backends. This is phrased as an "allowlist", because we want to
keep this interface tight. Also, this gives much better diagnostics than
a backend randomly crashing or failing to compile would (though they
could still be improved).
This was especially painful because if we had
`tensor<?x!numpy.any_dtype>` slip through, at some point RefBackend
would convert it to a memref type and trip the "verify type invariants"
assertion which gives no location or anything and crashed the process,
which was very unpleasant.
We implement this with the dialect conversion framework, which works
reasonably well and was quick to put together and familiar, but is still
very "op oriented". We probably want to make this hand-rolled
eventually, especially the error reporting (the most useful kind of
error for a dialect conversion user is not necessarily the best for this
use case). Also, in production, these error will go to users, and need
to be surfaced carefully such as "the compiler needs a type annotation
on this function parameter" which in general requires some special
analysis, wordsmithing, and overall awareness of the e2e use case (such
as how much we can lean into certain source locations) to provide a
meaningful user-level diagnostic.
Also, add `inline` to the current frontend lowering pass pipeline to
allow slightly more complicated programs that otherwise would fail on
shape inference.
2021-04-13 09:39:53 +08:00
|
|
|
// RUN: npcomp-opt -npcomp-verify-backend-contract -split-input-file -verify-diagnostics -allow-unregistered-dialect %s | FileCheck %s
|
|
|
|
|
|
|
|
// CHECK: func @mm
|
2021-06-24 01:03:29 +08:00
|
|
|
func @mm(%arg0: tensor<?x?xf32>, %arg1: tensor<?x?xf32>) -> tensor<?x?xf32> attributes {iree.module.export} {
|
|
|
|
%c0 = constant 0 : index
|
|
|
|
%c1 = constant 1 : index
|
|
|
|
%cst = constant 0.000000e+00 : f32
|
|
|
|
%0 = memref.dim %arg0, %c0 : tensor<?x?xf32>
|
|
|
|
%1 = memref.dim %arg0, %c1 : tensor<?x?xf32>
|
|
|
|
%2 = memref.dim %arg1, %c0 : tensor<?x?xf32>
|
|
|
|
%3 = memref.dim %arg1, %c1 : tensor<?x?xf32>
|
|
|
|
%4 = cmpi eq, %1, %2 : index
|
|
|
|
assert %4, "mismatching contracting dimension for aten.mm"
|
|
|
|
%5 = linalg.init_tensor [%0, %3] : tensor<?x?xf32>
|
|
|
|
%6 = linalg.fill(%cst, %5) : f32, tensor<?x?xf32> -> tensor<?x?xf32>
|
|
|
|
%7 = linalg.matmul ins(%arg0, %arg1 : tensor<?x?xf32>, tensor<?x?xf32>) outs(%6 : tensor<?x?xf32>) -> tensor<?x?xf32>
|
|
|
|
return %7 : tensor<?x?xf32>
|
|
|
|
}
|
Add npcomp-verify-backend-contract pass.
This pass verifies that a given module satisfies the contract that we
have for backends. This is phrased as an "allowlist", because we want to
keep this interface tight. Also, this gives much better diagnostics than
a backend randomly crashing or failing to compile would (though they
could still be improved).
This was especially painful because if we had
`tensor<?x!numpy.any_dtype>` slip through, at some point RefBackend
would convert it to a memref type and trip the "verify type invariants"
assertion which gives no location or anything and crashed the process,
which was very unpleasant.
We implement this with the dialect conversion framework, which works
reasonably well and was quick to put together and familiar, but is still
very "op oriented". We probably want to make this hand-rolled
eventually, especially the error reporting (the most useful kind of
error for a dialect conversion user is not necessarily the best for this
use case). Also, in production, these error will go to users, and need
to be surfaced carefully such as "the compiler needs a type annotation
on this function parameter" which in general requires some special
analysis, wordsmithing, and overall awareness of the e2e use case (such
as how much we can lean into certain source locations) to provide a
meaningful user-level diagnostic.
Also, add `inline` to the current frontend lowering pass pipeline to
allow slightly more complicated programs that otherwise would fail on
shape inference.
2021-04-13 09:39:53 +08:00
|
|
|
|
|
|
|
// -----
|
|
|
|
|
|
|
|
// Basic check of error reporting.
|
|
|
|
|
|
|
|
// expected-error@+1 {{Module does not conform to npcomp's backend contract.}}
|
|
|
|
module {
|
|
|
|
func @disallowed() {
|
|
|
|
// expected-error@+1 {{failed to legalize operation 'unknown_dialect.unknown_op'}}
|
|
|
|
"unknown_dialect.unknown_op"() : () -> ()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----
|
|
|
|
|
|
|
|
// TODO: Improve these errors to give more exact reporting.
|
|
|
|
//
|
|
|
|
// The reporting we inherit from dialect conversion is not precise.
|
|
|
|
// For example, here we want it to explicitly call out that
|
|
|
|
// `tensor<?x!numpy.any_dtype>` is the problem here, which suggests
|
|
|
|
// that type inference didn't succeed, or insufficient type information
|
|
|
|
// was available.
|
|
|
|
//
|
|
|
|
// Ultimately, the output of this pass needs to be conveyed to the user
|
|
|
|
// in an understandable way, such as suggesting a particular place where
|
|
|
|
// a shape annotation is needed.
|
|
|
|
|
|
|
|
// expected-error@+1 {{Module does not conform to npcomp's backend contract.}}
|
|
|
|
module {
|
|
|
|
func @disallowed(%arg0: tensor<?x!numpy.any_dtype>) -> tensor<?x!numpy.any_dtype> {
|
|
|
|
// expected-error@+1 {{failed to legalize operation 'std.return'}}
|
|
|
|
return %arg0 : tensor<?x!numpy.any_dtype>
|
|
|
|
}
|
|
|
|
}
|