Finish support for function attributes, and improve lots of things:
- Have the parser rewrite forward references to their resolved values at the
end of parsing.
- Implement verifier support for detecting malformed function attrs.
- Add efficient query for (in general, recursive) attributes to tell if they
contain a function.
As part of this, improve other general infrastructure:
- Implement support for verifying OperationStmt's in ml functions, refactoring
and generalizing support for operations in the verifier.
- Refactor location handling code in mlir-opt to have the non-error expecting
form of mlir-opt invocations to report error locations precisely.
- Fix parser to detect verifier failures and report them through errorReporter
instead of printing the error and crashing.
This regresses the location info for verifier errors in the parser that were
previously ascribed to the function. This will get resolved in future patches
by adding support for function attributes, which we can use to manage location
information.
PiperOrigin-RevId: 209600980
diff --git a/lib/IR/Operation.cpp b/lib/IR/Operation.cpp
index af937bc..aed6fcc 100644
--- a/lib/IR/Operation.cpp
+++ b/lib/IR/Operation.cpp
@@ -17,7 +17,9 @@
#include "mlir/IR/Operation.h"
#include "AttributeListStorage.h"
+#include "mlir/IR/CFGFunction.h"
#include "mlir/IR/Instructions.h"
+#include "mlir/IR/MLFunction.h"
#include "mlir/IR/MLIRContext.h"
#include "mlir/IR/Statements.h"
using namespace mlir;
@@ -42,22 +44,26 @@
return cast<OperationStmt>(this)->getContext();
}
+/// Return the function this operation is defined in.
+Function *Operation::getOperationFunction() {
+ if (auto *inst = dyn_cast<OperationInst>(this))
+ return inst->getFunction();
+ return cast<OperationStmt>(this)->findFunction();
+}
+
/// Return the number of operands this operation has.
unsigned Operation::getNumOperands() const {
- if (auto *inst = dyn_cast<OperationInst>(this)) {
+ if (auto *inst = dyn_cast<OperationInst>(this))
return inst->getNumOperands();
- } else {
- return cast<OperationStmt>(this)->getNumOperands();
- }
+
+ return cast<OperationStmt>(this)->getNumOperands();
}
SSAValue *Operation::getOperand(unsigned idx) {
- if (auto *inst = dyn_cast<OperationInst>(this)) {
+ if (auto *inst = dyn_cast<OperationInst>(this))
return inst->getOperand(idx);
- } else {
- auto *stmt = cast<OperationStmt>(this);
- return stmt->getOperand(idx);
- }
+
+ return cast<OperationStmt>(this)->getOperand(idx);
}
void Operation::setOperand(unsigned idx, SSAValue *value) {
@@ -71,22 +77,18 @@
/// Return the number of results this operation has.
unsigned Operation::getNumResults() const {
- if (auto *inst = dyn_cast<OperationInst>(this)) {
+ if (auto *inst = dyn_cast<OperationInst>(this))
return inst->getNumResults();
- } else {
- auto *stmt = cast<OperationStmt>(this);
- return stmt->getNumResults();
- }
+
+ return cast<OperationStmt>(this)->getNumResults();
}
/// Return the indicated result.
SSAValue *Operation::getResult(unsigned idx) {
- if (auto *inst = dyn_cast<OperationInst>(this)) {
+ if (auto *inst = dyn_cast<OperationInst>(this))
return inst->getResult(idx);
- } else {
- auto *stmt = cast<OperationStmt>(this);
- return stmt->getResult(idx);
- }
+
+ return cast<OperationStmt>(this)->getResult(idx);
}
ArrayRef<NamedAttribute> Operation::getAttrs() const {