From 1b430f1d325df9f7b32a3b2a465e7b3fcffcd919 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Thu, 27 Dec 2018 15:53:49 -0800 Subject: [PATCH] Delicately re-layer Operation, Statement, and OperationStmt, reworking #includes so Statements.h includes Operation.h but nothing else does. This is in preparation to eliminate the Operation class and the complexity it brings with it. I split this patch off because it is just moving stuff around, the next patch will be more complex. This is step 14/n towards merging instructions and statements, NFC. PiperOrigin-RevId: 227071777 --- mlir/include/mlir/Analysis/AffineStructures.h | 4 +- mlir/include/mlir/IR/Function.h | 2 +- mlir/include/mlir/IR/Matchers.h | 1 - mlir/include/mlir/IR/OpDefinition.h | 3 +- mlir/include/mlir/IR/Operation.h | 62 +--------- mlir/include/mlir/IR/Statement.h | 116 +++++++++++++++--- mlir/include/mlir/IR/Statements.h | 1 + mlir/include/mlir/IR/StmtBlock.h | 9 +- mlir/lib/IR/AttributeListStorage.h | 2 +- 9 files changed, 108 insertions(+), 92 deletions(-) diff --git a/mlir/include/mlir/Analysis/AffineStructures.h b/mlir/include/mlir/Analysis/AffineStructures.h index a954dacc3f33..70786a14444a 100644 --- a/mlir/include/mlir/Analysis/AffineStructures.h +++ b/mlir/include/mlir/Analysis/AffineStructures.h @@ -23,9 +23,6 @@ #define MLIR_ANALYSIS_AFFINE_STRUCTURES_H #include "mlir/IR/AffineExpr.h" -#include "mlir/IR/Operation.h" -#include "mlir/Support/LLVM.h" -#include "llvm/ADT/SmallVector.h" namespace mlir { @@ -38,6 +35,7 @@ class IntegerSet; class MLIRContext; class Value; class HyperRectangularSet; +class MemRefType; /// A mutable affine map. Its affine expressions are however unique. struct MutableAffineMap { diff --git a/mlir/include/mlir/IR/Function.h b/mlir/include/mlir/IR/Function.h index 1a4e64fbc434..ece70f3ef929 100644 --- a/mlir/include/mlir/IR/Function.h +++ b/mlir/include/mlir/IR/Function.h @@ -27,7 +27,6 @@ #include "mlir/IR/Attributes.h" #include "mlir/IR/Identifier.h" #include "mlir/IR/Location.h" -#include "mlir/IR/Operation.h" #include "mlir/IR/StmtBlock.h" #include "mlir/IR/Types.h" #include "mlir/Support/LLVM.h" @@ -39,6 +38,7 @@ class FunctionType; class MLIRContext; class Module; template class ArgumentIterator; +using BasicBlock = StmtBlock; /// NamedAttribute is used for function attribute lists, it holds an /// identifier for the name and a value for the attribute. The attribute diff --git a/mlir/include/mlir/IR/Matchers.h b/mlir/include/mlir/IR/Matchers.h index 9ebe226600fb..b8e947fbf99b 100644 --- a/mlir/include/mlir/IR/Matchers.h +++ b/mlir/include/mlir/IR/Matchers.h @@ -26,7 +26,6 @@ #include "mlir/IR/Attributes.h" #include "mlir/IR/BuiltinOps.h" -#include "mlir/IR/Operation.h" #include "mlir/IR/Types.h" #include "mlir/IR/Value.h" #include diff --git a/mlir/include/mlir/IR/OpDefinition.h b/mlir/include/mlir/IR/OpDefinition.h index 5262ba5975bf..aa3be30d7d4a 100644 --- a/mlir/include/mlir/IR/OpDefinition.h +++ b/mlir/include/mlir/IR/OpDefinition.h @@ -28,8 +28,7 @@ #ifndef MLIR_IR_OPDEFINITION_H #define MLIR_IR_OPDEFINITION_H -#include "mlir/IR/Operation.h" -#include "mlir/IR/Value.h" +#include "mlir/IR/Statements.h" #include namespace mlir { diff --git a/mlir/include/mlir/IR/Operation.h b/mlir/include/mlir/IR/Operation.h index eeee62cb647e..d4ab877c473b 100644 --- a/mlir/include/mlir/IR/Operation.h +++ b/mlir/include/mlir/IR/Operation.h @@ -19,6 +19,7 @@ #define MLIR_IR_OPERATION_H #include "mlir/IR/OperationSupport.h" +#include "mlir/IR/Statement.h" #include "llvm/ADT/PointerIntPair.h" #include "llvm/ADT/Twine.h" @@ -315,67 +316,6 @@ private: AttributeListStorage *attrs; }; -/// This is a helper template used to implement an iterator that contains a -/// pointer to some object and an index into it. The iterator moves the -/// index but keeps the object constant. -template -class IndexedAccessorIterator - : public llvm::iterator_facade_base< - ConcreteType, std::random_access_iterator_tag, ElementType *, - std::ptrdiff_t, ElementType *, ElementType *> { -public: - ptrdiff_t operator-(const IndexedAccessorIterator &rhs) const { - assert(object == rhs.object && "incompatible iterators"); - return index - rhs.index; - } - bool operator==(const IndexedAccessorIterator &rhs) const { - return object == rhs.object && index == rhs.index; - } - bool operator<(const IndexedAccessorIterator &rhs) const { - assert(object == rhs.object && "incompatible iterators"); - return index < rhs.index; - } - - ConcreteType &operator+=(ptrdiff_t offset) { - this->index += offset; - return static_cast(*this); - } - ConcreteType &operator-=(ptrdiff_t offset) { - this->index -= offset; - return static_cast(*this); - } - -protected: - IndexedAccessorIterator(ObjectType *object, unsigned index) - : object(object), index(index) {} - ObjectType *object; - unsigned index; -}; - -/// This template implements the operand iterators for the various IR classes -/// in terms of getOperand(idx). -template -class OperandIterator final - : public IndexedAccessorIterator, - ObjectType, ElementType> { -public: - /// Initializes the operand iterator to the specified operand index. - OperandIterator(ObjectType *object, unsigned index) - : IndexedAccessorIterator, - ObjectType, ElementType>(object, index) {} - - /// Support converting to the const variant. This will be a no-op for const - /// variant. - operator OperandIterator() const { - return OperandIterator(this->object, - this->index); - } - - ElementType *operator*() const { - return this->object->getOperand(this->index); - } -}; - /// This template implements the result iterators for the various IR classes /// in terms of getResult(idx). template diff --git a/mlir/include/mlir/IR/Statement.h b/mlir/include/mlir/IR/Statement.h index 2871b274de1a..97d35cf79bc3 100644 --- a/mlir/include/mlir/IR/Statement.h +++ b/mlir/include/mlir/IR/Statement.h @@ -22,7 +22,6 @@ #ifndef MLIR_IR_STATEMENT_H #define MLIR_IR_STATEMENT_H -#include "mlir/IR/Operation.h" #include "mlir/IR/Value.h" #include "mlir/Support/LLVM.h" #include "llvm/ADT/ilist.h" @@ -63,6 +62,7 @@ private: } // end namespace llvm namespace mlir { +template class OperandIterator; /// Statement is a basic unit of execution within an ML function. /// Statements can be nested within for and if statements effectively @@ -143,32 +143,22 @@ public: // Support non-const operand iteration. using operand_iterator = OperandIterator; - operand_iterator operand_begin() { return operand_iterator(this, 0); } + operand_iterator operand_begin(); - operand_iterator operand_end() { - return operand_iterator(this, getNumOperands()); - } + operand_iterator operand_end(); - /// Returns an iterator on the underlying Value's (Value *). - llvm::iterator_range getOperands() { - return {operand_begin(), operand_end()}; - } + /// Returns an iterator on the underlying Values. + llvm::iterator_range getOperands(); // Support const operand iteration. using const_operand_iterator = OperandIterator; - const_operand_iterator operand_begin() const { - return const_operand_iterator(this, 0); - } + const_operand_iterator operand_begin() const; - const_operand_iterator operand_end() const { - return const_operand_iterator(this, getNumOperands()); - } + const_operand_iterator operand_end() const; - /// Returns a const iterator on the underlying Value's (Value *). - llvm::iterator_range getOperands() const { - return {operand_begin(), operand_end()}; - } + /// Returns a const iterator on the underlying Values. + llvm::iterator_range getOperands() const; MutableArrayRef getStmtOperands(); ArrayRef getStmtOperands() const { @@ -219,6 +209,94 @@ inline raw_ostream &operator<<(raw_ostream &os, const Statement &stmt) { stmt.print(os); return os; } + +/// This is a helper template used to implement an iterator that contains a +/// pointer to some object and an index into it. The iterator moves the +/// index but keeps the object constant. +template +class IndexedAccessorIterator + : public llvm::iterator_facade_base< + ConcreteType, std::random_access_iterator_tag, ElementType *, + std::ptrdiff_t, ElementType *, ElementType *> { +public: + ptrdiff_t operator-(const IndexedAccessorIterator &rhs) const { + assert(object == rhs.object && "incompatible iterators"); + return index - rhs.index; + } + bool operator==(const IndexedAccessorIterator &rhs) const { + return object == rhs.object && index == rhs.index; + } + bool operator<(const IndexedAccessorIterator &rhs) const { + assert(object == rhs.object && "incompatible iterators"); + return index < rhs.index; + } + + ConcreteType &operator+=(ptrdiff_t offset) { + this->index += offset; + return static_cast(*this); + } + ConcreteType &operator-=(ptrdiff_t offset) { + this->index -= offset; + return static_cast(*this); + } + +protected: + IndexedAccessorIterator(ObjectType *object, unsigned index) + : object(object), index(index) {} + ObjectType *object; + unsigned index; +}; + +/// This template implements the const/non-const operand iterators for the +/// Instruction class in terms of getOperand(idx). +template +class OperandIterator final + : public IndexedAccessorIterator, + ObjectType, ElementType> { +public: + /// Initializes the operand iterator to the specified operand index. + OperandIterator(ObjectType *object, unsigned index) + : IndexedAccessorIterator, + ObjectType, ElementType>(object, index) {} + + /// Support converting to the const variant. This will be a no-op for const + /// variant. + operator OperandIterator() const { + return OperandIterator(this->object, + this->index); + } + + ElementType *operator*() const { + return this->object->getOperand(this->index); + } +}; + +// Implement the inline operand iterator methods. +inline auto Statement::operand_begin() -> operand_iterator { + return operand_iterator(this, 0); +} + +inline auto Statement::operand_end() -> operand_iterator { + return operand_iterator(this, getNumOperands()); +} + +inline auto Statement::getOperands() -> llvm::iterator_range { + return {operand_begin(), operand_end()}; +} + +inline auto Statement::operand_begin() const -> const_operand_iterator { + return const_operand_iterator(this, 0); +} + +inline auto Statement::operand_end() const -> const_operand_iterator { + return const_operand_iterator(this, getNumOperands()); +} + +inline auto Statement::getOperands() const + -> llvm::iterator_range { + return {operand_begin(), operand_end()}; +} + } // end namespace mlir #endif // MLIR_IR_STATEMENT_H diff --git a/mlir/include/mlir/IR/Statements.h b/mlir/include/mlir/IR/Statements.h index 1ca511c00fa1..b224ab0b1f5d 100644 --- a/mlir/include/mlir/IR/Statements.h +++ b/mlir/include/mlir/IR/Statements.h @@ -24,6 +24,7 @@ #include "mlir/IR/AffineMap.h" #include "mlir/IR/IntegerSet.h" +#include "mlir/IR/Operation.h" #include "mlir/IR/StmtBlock.h" #include "llvm/Support/TrailingObjects.h" diff --git a/mlir/include/mlir/IR/StmtBlock.h b/mlir/include/mlir/IR/StmtBlock.h index e2885430c6c0..a5487319da48 100644 --- a/mlir/include/mlir/IR/StmtBlock.h +++ b/mlir/include/mlir/IR/StmtBlock.h @@ -23,6 +23,7 @@ #define MLIR_IR_STMTBLOCK_H #include "mlir/IR/Statement.h" +#include "llvm/ADT/PointerUnion.h" namespace mlir { class IfStmt; @@ -218,7 +219,7 @@ public: // Other //===--------------------------------------------------------------------===// - /// Unlink this BasicBlock from its CFGFunction and delete it. + /// Unlink this Block from its Function and delete it. void eraseFromFunction(); /// Split the basic block into two basic blocks before the specified @@ -228,11 +229,11 @@ public: /// the original basic block, an unconditional branch is added to the original /// block (going to the new block), and the rest of the instructions in the /// original block are moved to the new BB, including the old terminator. The - /// newly formed BasicBlock is returned. + /// newly formed Block is returned. /// /// This function invalidates the specified iterator. - BasicBlock *splitBasicBlock(iterator splitBefore); - BasicBlock *splitBasicBlock(Instruction *splitBeforeInst) { + StmtBlock *splitBasicBlock(iterator splitBefore); + StmtBlock *splitBasicBlock(Instruction *splitBeforeInst) { return splitBasicBlock(iterator(splitBeforeInst)); } diff --git a/mlir/lib/IR/AttributeListStorage.h b/mlir/lib/IR/AttributeListStorage.h index d4a6298f481a..de6e4a0557cf 100644 --- a/mlir/lib/IR/AttributeListStorage.h +++ b/mlir/lib/IR/AttributeListStorage.h @@ -18,7 +18,7 @@ #ifndef ATTRIBUTELISTSTORAGE_H #define ATTRIBUTELISTSTORAGE_H -#include "mlir/IR/Operation.h" +#include "mlir/IR/OperationSupport.h" #include "llvm/Support/TrailingObjects.h" namespace mlir {