Add support for operands to the return instructions, enhance verifier to report errors through the diagnostics system when invoked by the parser. It doesn't have perfect location info, but it is close enough to be testable.
PiperOrigin-RevId: 205534392
diff --git a/include/mlir/IR/Builders.h b/include/mlir/IR/Builders.h
index 1da2312..1a003b6 100644
--- a/include/mlir/IR/Builders.h
+++ b/include/mlir/IR/Builders.h
@@ -134,10 +134,12 @@
// Terminators.
- ReturnInst *createReturnInst() { return insertTerminator(new ReturnInst()); }
+ ReturnInst *createReturnInst(ArrayRef<CFGValue *> operands) {
+ return insertTerminator(ReturnInst::create(operands));
+ }
BranchInst *createBranchInst(BasicBlock *dest) {
- return insertTerminator(new BranchInst(dest));
+ return insertTerminator(BranchInst::create(dest));
}
private:
diff --git a/include/mlir/IR/Function.h b/include/mlir/IR/Function.h
index 7c00cd0..e413172 100644
--- a/include/mlir/IR/Function.h
+++ b/include/mlir/IR/Function.h
@@ -46,8 +46,9 @@
MLIRContext *getContext() const;
/// Perform (potentially expensive) checks of invariants, used to detect
- /// compiler bugs. This aborts on failure.
- void verify() const;
+ /// compiler bugs. On error, this fills in the string and return true,
+ /// or aborts if the string was not provided.
+ bool verify(std::string *errorResult = nullptr) const;
void print(raw_ostream &os) const;
void dump() const;
diff --git a/include/mlir/IR/Instructions.h b/include/mlir/IR/Instructions.h
index 52047d3..8d4068b 100644
--- a/include/mlir/IR/Instructions.h
+++ b/include/mlir/IR/Instructions.h
@@ -187,9 +187,7 @@
/// and may pass basic block arguments to the successor.
class BranchInst : public TerminatorInst {
public:
- explicit BranchInst(BasicBlock *dest)
- : TerminatorInst(Kind::Branch), dest(dest) {
- }
+ static BranchInst *create(BasicBlock *dest) { return new BranchInst(dest); }
~BranchInst() {}
/// Return the block this branch jumps to.
@@ -205,6 +203,9 @@
}
private:
+ explicit BranchInst(BasicBlock *dest)
+ : TerminatorInst(Kind::Branch), dest(dest) {}
+
BasicBlock *dest;
};
@@ -212,17 +213,53 @@
/// The 'return' instruction represents the end of control flow within the
/// current function, and can return zero or more results. The result list is
/// required to align with the result list of the containing function's type.
-class ReturnInst : public TerminatorInst {
+class ReturnInst final
+ : public TerminatorInst,
+ private llvm::TrailingObjects<ReturnInst, InstOperand> {
public:
- explicit ReturnInst() : TerminatorInst(Kind::Return) {}
- ~ReturnInst() {}
+ /// Create a new OperationInst with the specific fields.
+ static ReturnInst *create(ArrayRef<CFGValue *> operands);
- // TODO: Needs to take an operand list.
+ unsigned getNumOperands() const { return numOperands; }
+
+ // TODO: Add a getOperands() custom sequence that provides a value projection
+ // of the operand list.
+ CFGValue *getOperand(unsigned idx) { return getInstOperand(idx).get(); }
+ const CFGValue *getOperand(unsigned idx) const {
+ return getInstOperand(idx).get();
+ }
+
+ ArrayRef<InstOperand> getInstOperands() const {
+ return {getTrailingObjects<InstOperand>(), numOperands};
+ }
+ MutableArrayRef<InstOperand> getInstOperands() {
+ return {getTrailingObjects<InstOperand>(), numOperands};
+ }
+
+ InstOperand &getInstOperand(unsigned idx) { return getInstOperands()[idx]; }
+ const InstOperand &getInstOperand(unsigned idx) const {
+ return getInstOperands()[idx];
+ }
+
+ void destroy();
/// Methods for support type inquiry through isa, cast, and dyn_cast.
static bool classof(const Instruction *inst) {
return inst->getKind() == Kind::Return;
}
+
+private:
+ // This stuff is used by the TrailingObjects template.
+ friend llvm::TrailingObjects<ReturnInst, InstOperand>;
+ size_t numTrailingObjects(OverloadToken<InstOperand>) const {
+ return numOperands;
+ }
+
+ explicit ReturnInst(unsigned numOperands)
+ : TerminatorInst(Kind::Return), numOperands(numOperands) {}
+ ~ReturnInst();
+
+ unsigned numOperands;
};
} // end namespace mlir
diff --git a/include/mlir/IR/Module.h b/include/mlir/IR/Module.h
index ccc832a..8401252 100644
--- a/include/mlir/IR/Module.h
+++ b/include/mlir/IR/Module.h
@@ -40,8 +40,9 @@
std::vector<Function*> functionList;
/// Perform (potentially expensive) checks of invariants, used to detect
- /// compiler bugs. This aborts on failure.
- void verify() const;
+ /// compiler bugs. On error, this fills in the string and return true,
+ /// or aborts if the string was not provided.
+ bool verify(std::string *errorResult = nullptr) const;
void print(raw_ostream &os) const;
void dump() const;