Static op verification cannot detect cases where an op is valid at compile time but may be invalid at runtime. An example of such an op is `memref::ExpandShapeOp`. Invalid at compile time: `memref.expand_shape %m [[0, 1]] : memref<11xf32> into memref<2x5xf32>` Valid at compile time (because we do not know any better): `memref.expand_shape %m [[0, 1]] : memref<?xf32> into memref<?x5xf32>`. This op may or may not be valid at runtime depending on the runtime shape of `%m`. Invalid runtime ops such as the one above are hard to debug because they can crash the program execution at a seemingly unrelated position or (even worse) compute an invalid result without crashing. This revision adds a new op interface `RuntimeVerifiableOpInterface` that can be implemented by ops that provide additional runtime verification. Such runtime verification can be computationally expensive, so it is only generated on an opt-in basis by running `-generate-runtime-verification`. A simple runtime verifier for `memref::ExpandShapeOp` is provided as an example. Differential Revision: https://reviews.llvm.org/D138576
41 lines
1.3 KiB
C++
41 lines
1.3 KiB
C++
//===- RuntimeOpVerification.cpp - Op Verification ------------------------===//
|
|
//
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "mlir/Transforms/Passes.h"
|
|
|
|
#include "mlir/IR/Builders.h"
|
|
#include "mlir/IR/Operation.h"
|
|
#include "mlir/Interfaces/RuntimeVerifiableOpInterface.h"
|
|
|
|
namespace mlir {
|
|
#define GEN_PASS_DEF_GENERATERUNTIMEVERIFICATION
|
|
#include "mlir/Transforms/Passes.h.inc"
|
|
} // namespace mlir
|
|
|
|
using namespace mlir;
|
|
|
|
namespace {
|
|
struct GenerateRuntimeVerificationPass
|
|
: public impl::GenerateRuntimeVerificationBase<
|
|
GenerateRuntimeVerificationPass> {
|
|
void runOnOperation() override;
|
|
};
|
|
} // namespace
|
|
|
|
void GenerateRuntimeVerificationPass::runOnOperation() {
|
|
getOperation()->walk([&](RuntimeVerifiableOpInterface verifiableOp) {
|
|
OpBuilder builder(getOperation()->getContext());
|
|
builder.setInsertionPoint(verifiableOp);
|
|
verifiableOp.generateRuntimeVerification(builder, verifiableOp.getLoc());
|
|
});
|
|
}
|
|
|
|
std::unique_ptr<Pass> mlir::createGenerateRuntimeVerificationPass() {
|
|
return std::make_unique<GenerateRuntimeVerificationPass>();
|
|
}
|