From 276fae1b0d57666b991bf7e72aaa7c5d66bd5e15 Mon Sep 17 00:00:00 2001 From: Alex Zinenko Date: Thu, 14 Mar 2019 10:38:44 -0700 Subject: [PATCH] Rename BlockList into Region NFC. This is step 1/n to specifying regions as parts of any operation. PiperOrigin-RevId: 238472370 --- mlir/include/mlir/AffineOps/AffineOps.h | 24 +++--- mlir/include/mlir/Analysis/Dominance.h | 12 +-- mlir/include/mlir/IR/Block.h | 68 +++++++++-------- mlir/include/mlir/IR/Function.h | 54 +++++++------- mlir/include/mlir/IR/FunctionGraphTraits.h | 16 ++-- mlir/include/mlir/IR/Instruction.h | 50 ++++++------- mlir/include/mlir/IR/OpImplementation.h | 17 ++--- mlir/include/mlir/IR/OperationSupport.h | 10 +-- mlir/lib/AffineOps/AffineOps.cpp | 82 ++++++++++----------- mlir/lib/Analysis/Dominance.cpp | 32 ++++---- mlir/lib/Analysis/LoopAnalysis.cpp | 2 +- mlir/lib/Analysis/Utils.cpp | 4 +- mlir/lib/Analysis/Verifier.cpp | 8 +- mlir/lib/IR/AsmPrinter.cpp | 27 +++---- mlir/lib/IR/Block.cpp | 45 ++++++------ mlir/lib/IR/Builders.cpp | 7 +- mlir/lib/IR/Function.cpp | 8 +- mlir/lib/IR/Instruction.cpp | 61 +++++++-------- mlir/lib/Parser/Parser.cpp | 114 ++++++++++++++--------------- mlir/lib/Transforms/CSE.cpp | 29 ++++---- mlir/lib/Transforms/DialectConversion.cpp | 2 +- mlir/lib/Transforms/LoopFusion.cpp | 4 +- mlir/lib/Transforms/LoopUnroll.cpp | 4 +- mlir/lib/Transforms/LoopUnrollAndJam.cpp | 4 +- mlir/lib/Transforms/MaterializeVectors.cpp | 4 +- mlir/lib/Transforms/Vectorize.cpp | 4 +- mlir/test/IR/invalid.mlir | 2 +- mlir/test/IR/parser.mlir | 2 +- 28 files changed, 339 insertions(+), 357 deletions(-) diff --git a/mlir/include/mlir/AffineOps/AffineOps.h b/mlir/include/mlir/AffineOps/AffineOps.h index 487b0a3..a70d810 100644 --- a/mlir/include/mlir/AffineOps/AffineOps.h +++ b/mlir/include/mlir/AffineOps/AffineOps.h @@ -142,14 +142,12 @@ public: Block *createBody(); /// Get the body of the AffineForOp. - Block *getBody() { return &getBlockList().front(); } - const Block *getBody() const { return &getBlockList().front(); } + Block *getBody() { return &getRegion().front(); } + const Block *getBody() const { return &getRegion().front(); } - /// Get the blocklist containing the body. - BlockList &getBlockList() { return getInstruction()->getBlockList(0); } - const BlockList &getBlockList() const { - return getInstruction()->getBlockList(0); - } + /// Get the body region of the AffineForOp. + Region &getRegion() { return getInstruction()->getRegion(0); } + const Region &getRegion() const { return getInstruction()->getRegion(0); } /// Returns the induction variable for this loop. Value *getInductionVar(); @@ -332,15 +330,15 @@ public: IntegerSet getIntegerSet() const; void setIntegerSet(IntegerSet newSet); - /// Returns the list of 'then' blocks. - BlockList &getThenBlocks(); - const BlockList &getThenBlocks() const { + /// Returns the 'then' region. + Region &getThenBlocks(); + const Region &getThenBlocks() const { return const_cast(this)->getThenBlocks(); } - /// Returns the list of 'else' blocks. - BlockList &getElseBlocks(); - const BlockList &getElseBlocks() const { + /// Returns the 'else' blocks. + Region &getElseBlocks(); + const Region &getElseBlocks() const { return const_cast(this)->getElseBlocks(); } diff --git a/mlir/include/mlir/Analysis/Dominance.h b/mlir/include/mlir/Analysis/Dominance.h index edf5a36..6ab1b87 100644 --- a/mlir/include/mlir/Analysis/Dominance.h +++ b/mlir/include/mlir/Analysis/Dominance.h @@ -43,10 +43,10 @@ public: /// Recalculate the dominance info for the provided function. void recalculate(Function *function); - /// Get the root dominance node of the given block list. - DominanceInfoNode *getRootNode(const BlockList *blockList) { - assert(dominanceInfos.count(blockList) != 0); - return dominanceInfos[blockList]->getRootNode(); + /// Get the root dominance node of the given region. + DominanceInfoNode *getRootNode(const Region *region) { + assert(dominanceInfos.count(region) != 0); + return dominanceInfos[region]->getRootNode(); } protected: @@ -55,8 +55,8 @@ protected: /// Return true if the specified block A properly dominates block B. bool properlyDominates(const Block *a, const Block *b); - /// A mapping of block lists to their base dominator tree. - DenseMap> dominanceInfos; + /// A mapping of regions to their base dominator tree. + DenseMap> dominanceInfos; }; } // end namespace detail diff --git a/mlir/include/mlir/IR/Block.h b/mlir/include/mlir/IR/Block.h index da02db8..babc150 100644 --- a/mlir/include/mlir/IR/Block.h +++ b/mlir/include/mlir/IR/Block.h @@ -1,4 +1,4 @@ -//===- Block.h - MLIR Block and BlockList Classes ---------------*- C++ -*-===// +//===- Block.h - MLIR Block and Region Classes ------------------*- C++ -*-===// // // Copyright 2019 The MLIR Authors. // @@ -15,7 +15,7 @@ // limitations under the License. // ============================================================================= // -// This file defines Block and BlockList classes. +// This file defines Block and Region classes. // //===----------------------------------------------------------------------===// @@ -35,7 +35,7 @@ namespace llvm { namespace ilist_detail { // Explicitly define the node access for the instruction list so that we can // break the dependence on the Instruction class in this header. This allows for -// instructions to have trailing BlockLists without a circular include +// instructions to have trailing Regions without a circular include // dependence. template <> struct SpecificNodeAccess< @@ -71,7 +71,7 @@ private: namespace mlir { class BlockAndValueMapping; -class BlockList; +class Region; class Function; using BlockOperand = IROperandImpl; @@ -81,7 +81,7 @@ template class SuccessorIterator; /// `Block` represents an ordered list of `Instruction`s. class Block : public IRObjectWithUseList, - public llvm::ilist_node_with_parent { + public llvm::ilist_node_with_parent { public: explicit Block() {} ~Block(); @@ -96,8 +96,8 @@ public: instructions.pop_back(); } - /// Blocks are maintained in a list of BlockList type. - BlockList *getParent() const { return parentValidInstOrderPair.getPointer(); } + /// Blocks are maintained in a Region. + Region *getParent() const { return parentValidInstOrderPair.getPointer(); } /// Returns the closest surrounding instruction that contains this block or /// nullptr if this is a top-level block. @@ -339,8 +339,7 @@ public: private: /// Pair of the parent object that owns this block and a bit that signifies if /// the instructions within this block have a valid ordering. - llvm::PointerIntPair - parentValidInstOrderPair; + llvm::PointerIntPair parentValidInstOrderPair; /// This is the list of instructions in the block. InstListType instructions; @@ -373,7 +372,7 @@ struct ilist_traits<::mlir::Block> : public ilist_alloc_traits<::mlir::Block> { block_iterator first, block_iterator last); private: - mlir::BlockList *getContainingBlockList(); + mlir::Region *getContainingRegion(); }; } // end namespace llvm @@ -381,20 +380,20 @@ namespace mlir { /// This class contains a list of basic blocks and has a notion of the object it /// is part of - a Function or an operation region. -class BlockList { +class Region { public: - explicit BlockList(Function *container); - explicit BlockList(Instruction *container); + explicit Region(Function *container); + explicit Region(Instruction *container); - using BlockListType = llvm::iplist; - BlockListType &getBlocks() { return blocks; } - const BlockListType &getBlocks() const { return blocks; } + using RegionType = llvm::iplist; + RegionType &getBlocks() { return blocks; } + const RegionType &getBlocks() const { return blocks; } // Iteration over the block in the function. - using iterator = BlockListType::iterator; - using const_iterator = BlockListType::const_iterator; - using reverse_iterator = BlockListType::reverse_iterator; - using const_reverse_iterator = BlockListType::const_reverse_iterator; + using iterator = RegionType::iterator; + using const_iterator = RegionType::const_iterator; + using reverse_iterator = RegionType::reverse_iterator; + using const_reverse_iterator = RegionType::const_reverse_iterator; iterator begin() { return blocks.begin(); } iterator end() { return blocks.end(); } @@ -410,40 +409,39 @@ public: void push_front(Block *block) { blocks.push_front(block); } Block &back() { return blocks.back(); } - const Block &back() const { return const_cast(this)->back(); } + const Block &back() const { return const_cast(this)->back(); } Block &front() { return blocks.front(); } - const Block &front() const { return const_cast(this)->front(); } + const Block &front() const { return const_cast(this)->front(); } - /// getSublistAccess() - Returns pointer to member of block list. - static BlockListType BlockList::*getSublistAccess(Block *) { - return &BlockList::blocks; + /// getSublistAccess() - Returns pointer to member of region. + static RegionType Region::*getSublistAccess(Block *) { + return &Region::blocks; } - /// A BlockList is part of a function or an operation region. If it is - /// part of an operation region, then return the operation, otherwise return - /// null. + /// A Region is either a function body or a part of an operation. If it is + /// part of an operation, then return the operation, otherwise return null. Instruction *getContainingInst(); const Instruction *getContainingInst() const { - return const_cast(this)->getContainingInst(); + return const_cast(this)->getContainingInst(); } - /// A BlockList is part of a function or an operation region. If it is part - /// of a Function, then return it, otherwise return null. + /// A Region is either a function body or a part of an operation. If it is + /// a Function body, then return this function, otherwise return null. Function *getContainingFunction(); const Function *getContainingFunction() const { - return const_cast(this)->getContainingFunction(); + return const_cast(this)->getContainingFunction(); } - /// Clone the internal blocks from this block list into dest. Any + /// Clone the internal blocks from this region into dest. Any /// cloned blocks are appended to the back of dest. If the mapper /// contains entries for block arguments, these arguments are not included /// in the respective cloned block. - void cloneInto(BlockList *dest, BlockAndValueMapping &mapper, + void cloneInto(Region *dest, BlockAndValueMapping &mapper, MLIRContext *context) const; private: - BlockListType blocks; + RegionType blocks; /// This is the object we are part of. llvm::PointerUnion container; diff --git a/mlir/include/mlir/IR/Function.h b/mlir/include/mlir/IR/Function.h index 4a86640..221c5c4 100644 --- a/mlir/include/mlir/IR/Function.h +++ b/mlir/include/mlir/IR/Function.h @@ -80,37 +80,37 @@ public: // Body Handling //===--------------------------------------------------------------------===// - BlockList &getBlockList() { return blocks; } - const BlockList &getBlockList() const { return blocks; } + Region &getBody() { return body; } + const Region &getBody() const { return body; } /// This is the list of blocks in the function. - using BlockListType = llvm::iplist; - BlockListType &getBlocks() { return blocks.getBlocks(); } - const BlockListType &getBlocks() const { return blocks.getBlocks(); } + using RegionType = llvm::iplist; + RegionType &getBlocks() { return body.getBlocks(); } + const RegionType &getBlocks() const { return body.getBlocks(); } // Iteration over the block in the function. - using iterator = BlockListType::iterator; - using const_iterator = BlockListType::const_iterator; - using reverse_iterator = BlockListType::reverse_iterator; - using const_reverse_iterator = BlockListType::const_reverse_iterator; - - iterator begin() { return blocks.begin(); } - iterator end() { return blocks.end(); } - const_iterator begin() const { return blocks.begin(); } - const_iterator end() const { return blocks.end(); } - reverse_iterator rbegin() { return blocks.rbegin(); } - reverse_iterator rend() { return blocks.rend(); } - const_reverse_iterator rbegin() const { return blocks.rbegin(); } - const_reverse_iterator rend() const { return blocks.rend(); } - - bool empty() const { return blocks.empty(); } - void push_back(Block *block) { blocks.push_back(block); } - void push_front(Block *block) { blocks.push_front(block); } - - Block &back() { return blocks.back(); } + using iterator = RegionType::iterator; + using const_iterator = RegionType::const_iterator; + using reverse_iterator = RegionType::reverse_iterator; + using const_reverse_iterator = RegionType::const_reverse_iterator; + + iterator begin() { return body.begin(); } + iterator end() { return body.end(); } + const_iterator begin() const { return body.begin(); } + const_iterator end() const { return body.end(); } + reverse_iterator rbegin() { return body.rbegin(); } + reverse_iterator rend() { return body.rend(); } + const_reverse_iterator rbegin() const { return body.rbegin(); } + const_reverse_iterator rend() const { return body.rend(); } + + bool empty() const { return body.empty(); } + void push_back(Block *block) { body.push_back(block); } + void push_front(Block *block) { body.push_front(block); } + + Block &back() { return body.back(); } const Block &back() const { return const_cast(this)->back(); } - Block &front() { return blocks.front(); } + Block &front() { return body.front(); } const Block &front() const { return const_cast(this)->front(); } //===--------------------------------------------------------------------===// @@ -329,8 +329,8 @@ private: /// The attributes lists for each of the function arguments. std::vector argAttrs; - /// The contents of the body. - BlockList blocks; + /// The body of the function. + Region body; void operator=(const Function &) = delete; friend struct llvm::ilist_traits; diff --git a/mlir/include/mlir/IR/FunctionGraphTraits.h b/mlir/include/mlir/IR/FunctionGraphTraits.h index 6ba50e7..b8a0d7e 100644 --- a/mlir/include/mlir/IR/FunctionGraphTraits.h +++ b/mlir/include/mlir/IR/FunctionGraphTraits.h @@ -153,8 +153,8 @@ struct GraphTraits> }; template <> -struct GraphTraits : public GraphTraits { - using GraphType = mlir::BlockList *; +struct GraphTraits : public GraphTraits { + using GraphType = mlir::Region *; using NodeRef = mlir::Block *; static NodeRef getEntryNode(GraphType fn) { return &fn->front(); } @@ -169,9 +169,9 @@ struct GraphTraits : public GraphTraits { }; template <> -struct GraphTraits +struct GraphTraits : public GraphTraits { - using GraphType = const mlir::BlockList *; + using GraphType = const mlir::Region *; using NodeRef = const mlir::Block *; static NodeRef getEntryNode(GraphType fn) { return &fn->front(); } @@ -186,9 +186,9 @@ struct GraphTraits }; template <> -struct GraphTraits> +struct GraphTraits> : public GraphTraits> { - using GraphType = Inverse; + using GraphType = Inverse; using NodeRef = NodeRef; static NodeRef getEntryNode(GraphType fn) { return &fn.Graph->front(); } @@ -203,9 +203,9 @@ struct GraphTraits> }; template <> -struct GraphTraits> +struct GraphTraits> : public GraphTraits> { - using GraphType = Inverse; + using GraphType = Inverse; using NodeRef = NodeRef; static NodeRef getEntryNode(GraphType fn) { return &fn.Graph->front(); } diff --git a/mlir/include/mlir/IR/Instruction.h b/mlir/include/mlir/IR/Instruction.h index f9a3ac0..2bcbd8a 100644 --- a/mlir/include/mlir/IR/Instruction.h +++ b/mlir/include/mlir/IR/Instruction.h @@ -49,15 +49,15 @@ using BlockOperand = IROperandImpl; class Instruction final : public llvm::ilist_node_with_parent, private llvm::TrailingObjects { + unsigned, Region, detail::OperandStorage> { public: /// Create a new Instruction with the specific fields. - static Instruction * - create(Location location, OperationName name, ArrayRef operands, - ArrayRef resultTypes, ArrayRef attributes, - ArrayRef successors, unsigned numBlockLists, - bool resizableOperandList, MLIRContext *context); + static Instruction *create(Location location, OperationName name, + ArrayRef operands, + ArrayRef resultTypes, + ArrayRef attributes, + ArrayRef successors, unsigned numRegions, + bool resizableOperandList, MLIRContext *context); /// The name of an operation is the key identifier for it. OperationName getName() const { return name; } @@ -279,24 +279,24 @@ public: // Blocks //===--------------------------------------------------------------------===// - /// Returns the number of block lists held by this operation. - unsigned getNumBlockLists() const { return numBlockLists; } + /// Returns the number of regions held by this operation. + unsigned getNumRegions() const { return numRegions; } - /// Returns the block lists held by this operation. - MutableArrayRef getBlockLists() { - return {getTrailingObjects(), numBlockLists}; + /// Returns the regions held by this operation. + MutableArrayRef getRegions() { + return {getTrailingObjects(), numRegions}; } - ArrayRef getBlockLists() const { - return const_cast(this)->getBlockLists(); + ArrayRef getRegions() const { + return const_cast(this)->getRegions(); } - /// Returns the block list held by this operation at position 'index'. - BlockList &getBlockList(unsigned index) { - assert(index < numBlockLists && "invalid block list index"); - return getBlockLists()[index]; + /// Returns the region held by this operation at position 'index'. + Region &getRegion(unsigned index) { + assert(index < numRegions && "invalid region index"); + return getRegions()[index]; } - const BlockList &getBlockList(unsigned index) const { - return const_cast(this)->getBlockList(index); + const Region &getRegion(unsigned index) const { + return const_cast(this)->getRegion(index); } //===--------------------------------------------------------------------===// @@ -528,7 +528,7 @@ public: protected: Instruction(Location location, OperationName name, unsigned numResults, - unsigned numSuccessors, unsigned numBlockLists, + unsigned numSuccessors, unsigned numRegions, ArrayRef attributes, MLIRContext *context); // Instructions are deleted through the destroy() member because they are @@ -558,7 +558,7 @@ private: /// O(1) local dominance checks between instructions. mutable unsigned orderIndex = 0; - const unsigned numResults, numSuccs, numBlockLists; + const unsigned numResults, numSuccs, numRegions; /// This holds the name of the operation. OperationName name; @@ -577,16 +577,14 @@ private: // This stuff is used by the TrailingObjects template. friend llvm::TrailingObjects; + Region, detail::OperandStorage>; size_t numTrailingObjects(OverloadToken) const { return numResults; } size_t numTrailingObjects(OverloadToken) const { return numSuccs; } - size_t numTrailingObjects(OverloadToken) const { - return numBlockLists; - } + size_t numTrailingObjects(OverloadToken) const { return numRegions; } size_t numTrailingObjects(OverloadToken) const { return numSuccs; } }; diff --git a/mlir/include/mlir/IR/OpImplementation.h b/mlir/include/mlir/IR/OpImplementation.h index c507832..fcdf2b6 100644 --- a/mlir/include/mlir/IR/OpImplementation.h +++ b/mlir/include/mlir/IR/OpImplementation.h @@ -89,9 +89,9 @@ public: /// Print the entire operation with the default generic assembly form. virtual void printGenericOp(const Instruction *op) = 0; - /// Prints a block list. - virtual void printBlockList(const BlockList &blocks, - bool printEntryBlockArgs = true) = 0; + /// Prints a region. + virtual void printRegion(const Region &blocks, + bool printEntryBlockArgs = true) = 0; private: OpAsmPrinter(const OpAsmPrinter &) = delete; @@ -314,13 +314,12 @@ public: int requiredOperandCount = -1, Delimiter delimiter = Delimiter::None) = 0; - /// Parses a block list. Any parsed blocks are filled in to the - /// operation's block lists after the operation is created. - virtual bool parseBlockList() = 0; + /// Parses a region. Any parsed blocks are filled in to the operation's + /// regions after the operation is created. + virtual bool parseRegion() = 0; - /// Parses an argument for the entry block of the next block list to be - /// parsed. - virtual bool parseBlockListEntryBlockArgument(Type argType) = 0; + /// Parses an argument for the entry block of the next region to be parsed. + virtual bool parseRegionEntryBlockArgument(Type argType) = 0; //===--------------------------------------------------------------------===// // Methods for interacting with the parser diff --git a/mlir/include/mlir/IR/OperationSupport.h b/mlir/include/mlir/IR/OperationSupport.h index a3e2911..5c45bd6 100644 --- a/mlir/include/mlir/IR/OperationSupport.h +++ b/mlir/include/mlir/IR/OperationSupport.h @@ -229,7 +229,7 @@ struct OperationState { SmallVector attributes; /// Successors of this operation and their respective operands. SmallVector successors; - unsigned numBlockLists = 0; + unsigned numRegions = 0; /// If the operation has a resizable operand list. bool resizableOperandList = false; @@ -243,14 +243,14 @@ public: OperationState(MLIRContext *context, Location location, StringRef name, ArrayRef operands, ArrayRef types, ArrayRef attributes, - ArrayRef successors = {}, unsigned numBlockLists = 0, + ArrayRef successors = {}, unsigned numRegions = 0, bool resizableOperandList = false) : context(context), location(location), name(name, context), operands(operands.begin(), operands.end()), types(types.begin(), types.end()), attributes(attributes.begin(), attributes.end()), successors(successors.begin(), successors.end()), - numBlockLists(numBlockLists) {} + numRegions(numRegions) {} void addOperands(ArrayRef newOperands) { assert(successors.empty() && @@ -279,8 +279,8 @@ public: operands.append(succOperands.begin(), succOperands.end()); } - /// Add a new block list with the specified blocks. - void reserveBlockLists(unsigned numReserved) { numBlockLists += numReserved; } + /// Reserve space for new regions. + void reserveRegions(unsigned numReserved) { numRegions += numReserved; } /// Sets the operand list of the operation as resizable. void setOperandListToResizable(bool isResizable = true) { diff --git a/mlir/lib/AffineOps/AffineOps.cpp b/mlir/lib/AffineOps/AffineOps.cpp index a9de42e..16fddb4 100644 --- a/mlir/lib/AffineOps/AffineOps.cpp +++ b/mlir/lib/AffineOps/AffineOps.cpp @@ -553,8 +553,8 @@ void AffineForOp::build(Builder *builder, OperationState *result, builder->getAffineMapAttr(ubMap)); result->addOperands(ubOperands); - // Reserve a block list for the body. - result->reserveBlockLists(/*numReserved=*/1); + // Reserve a region for the body. + result->reserveRegions(/*numReserved=*/1); // Set the operands list as resizable so that we can freely modify the bounds. result->setOperandListToResizable(); @@ -568,12 +568,11 @@ void AffineForOp::build(Builder *builder, OperationState *result, int64_t lb, } bool AffineForOp::verify() const { - const auto &bodyBlockList = getInstruction()->getBlockList(0); + const auto &bodyRegion = getInstruction()->getRegion(0); - // The body block list must contain a single basic block. - if (bodyBlockList.empty() || - std::next(bodyBlockList.begin()) != bodyBlockList.end()) - return emitOpError("expected body block list to have a single block"); + // The body region must contain a single basic block. + if (bodyRegion.empty() || std::next(bodyRegion.begin()) != bodyRegion.end()) + return emitOpError("expected body region to have a single block"); // Check that the body defines as single block argument for the induction // variable. @@ -701,7 +700,7 @@ static bool parseBound(bool isLower, OperationState *result, OpAsmParser *p) { bool AffineForOp::parse(OpAsmParser *parser, OperationState *result) { auto &builder = parser->getBuilder(); // Parse the induction variable followed by '='. - if (parser->parseBlockListEntryBlockArgument(builder.getIndexType()) || + if (parser->parseRegionEntryBlockArgument(builder.getIndexType()) || parser->parseEqual()) return true; @@ -730,9 +729,9 @@ bool AffineForOp::parse(OpAsmParser *parser, OperationState *result) { "expected step to be representable as a positive signed integer"); } - // Parse the body block list. - result->reserveBlockLists(/*numReserved=*/1); - if (parser->parseBlockList()) + // Parse the body region. + result->reserveRegions(/*numReserved=*/1); + if (parser->parseRegion()) return true; // Parse the optional attribute list. @@ -793,8 +792,8 @@ void AffineForOp::print(OpAsmPrinter *p) const { if (getStep() != 1) *p << " step " << getStep(); - p->printBlockList(getInstruction()->getBlockList(0), - /*printEntryBlockArgs=*/false); + p->printRegion(getInstruction()->getRegion(0), + /*printEntryBlockArgs=*/false); p->printOptionalAttrDict(getAttrs(), /*elidedAttrs=*/{getLowerBoundAttrName(), getUpperBoundAttrName(), @@ -872,14 +871,14 @@ void AffineForOp::getCanonicalizationPatterns(OwningRewritePatternList &results, } Block *AffineForOp::createBody() { - auto &bodyBlockList = getBlockList(); - assert(bodyBlockList.empty() && "expected no existing body blocks"); + auto &bodyRegion = getRegion(); + assert(bodyRegion.empty() && "expected no existing body blocks"); // Create a new block for the body, and add an argument for the induction // variable. Block *body = new Block(); body->addArgument(IndexType::get(getInstruction()->getContext())); - bodyBlockList.push_back(body); + bodyRegion.push_back(body); return body; } @@ -1040,8 +1039,8 @@ void AffineIfOp::build(Builder *builder, OperationState *result, result->addAttribute(getConditionAttrName(), IntegerSetAttr::get(condition)); result->addOperands(conditionOperands); - // Reserve 2 block lists, one for the 'then' and one for the 'else' regions. - result->reserveBlockLists(2); + // Reserve 2 regions, one for the 'then' and one for the 'else' regions. + result->reserveRegions(2); } bool AffineIfOp::verify() const { @@ -1061,20 +1060,19 @@ bool AffineIfOp::verify() const { condition.getNumDims())) return true; - // Verify that the entry of each child blocklist does not have arguments. - for (const auto &blockList : getInstruction()->getBlockLists()) { - if (blockList.empty()) + // Verify that the entry of each child region does not have arguments. + for (const auto ®ion : getInstruction()->getRegions()) { + if (region.empty()) continue; // TODO(riverriddle) We currently do not allow multiple blocks in child - // block lists. - if (std::next(blockList.begin()) != blockList.end()) - return emitOpError( - "expects only one block per 'then' or 'else' block list"); - if (blockList.front().back().isKnownTerminator()) + // regions. + if (std::next(region.begin()) != region.end()) + return emitOpError("expects only one block per 'then' or 'else' regions"); + if (region.front().back().isKnownTerminator()) return emitOpError("expects region block to not have a terminator"); - for (const auto &b : blockList) + for (const auto &b : region) if (b.getNumArguments() != 0) return emitOpError( "requires that child entry blocks have no arguments"); @@ -1102,13 +1100,13 @@ bool AffineIfOp::parse(OpAsmParser *parser, OperationState *result) { parser->getNameLoc(), "symbol operand count and integer set symbol count must match"); - // Parse the 'then' block list. - if (parser->parseBlockList()) + // Parse the 'then' region. + if (parser->parseRegion()) return true; - // If we find an 'else' keyword then parse the else block list. + // If we find an 'else' keyword then parse the 'else' region. if (!parser->parseOptionalKeyword("else")) { - if (parser->parseBlockList()) + if (parser->parseRegion()) return true; } @@ -1116,8 +1114,8 @@ bool AffineIfOp::parse(OpAsmParser *parser, OperationState *result) { if (parser->parseOptionalAttributeDict(result->attributes)) return true; - // Reserve 2 block lists, one for the 'then' and one for the 'else' regions. - result->reserveBlockLists(2); + // Reserve 2 regions, one for the 'then' and one for the 'else' regions. + result->reserveRegions(2); return false; } @@ -1126,13 +1124,13 @@ void AffineIfOp::print(OpAsmPrinter *p) const { *p << "if " << conditionAttr; printDimAndSymbolList(operand_begin(), operand_end(), conditionAttr.getValue().getNumDims(), p); - p->printBlockList(getInstruction()->getBlockList(0)); + p->printRegion(getInstruction()->getRegion(0)); - // Print the 'else' block list if it has any blocks. - const auto &elseBlockList = getInstruction()->getBlockList(1); - if (!elseBlockList.empty()) { + // Print the 'else' regions if it has any blocks. + const auto &elseRegion = getInstruction()->getRegion(1); + if (!elseRegion.empty()) { *p << " else"; - p->printBlockList(elseBlockList); + p->printRegion(elseRegion); } // Print the attribute list. @@ -1148,11 +1146,7 @@ void AffineIfOp::setIntegerSet(IntegerSet newSet) { } /// Returns the list of 'then' blocks. -BlockList &AffineIfOp::getThenBlocks() { - return getInstruction()->getBlockList(0); -} +Region &AffineIfOp::getThenBlocks() { return getInstruction()->getRegion(0); } /// Returns the list of 'else' blocks. -BlockList &AffineIfOp::getElseBlocks() { - return getInstruction()->getBlockList(1); -} +Region &AffineIfOp::getElseBlocks() { return getInstruction()->getRegion(1); } diff --git a/mlir/lib/Analysis/Dominance.cpp b/mlir/lib/Analysis/Dominance.cpp index b3c9d82..a6f6845 100644 --- a/mlir/lib/Analysis/Dominance.cpp +++ b/mlir/lib/Analysis/Dominance.cpp @@ -41,19 +41,19 @@ void DominanceInfoBase::recalculate(Function *function) { // Build the top level function dominance. auto functionDominance = std::make_unique(); - functionDominance->recalculate(function->getBlockList()); - dominanceInfos.try_emplace(&function->getBlockList(), + functionDominance->recalculate(function->getBody()); + dominanceInfos.try_emplace(&function->getBody(), std::move(functionDominance)); - /// Build the dominance for each of the internal region block lists. + /// Build the dominance for each of the operation regions. function->walk([&](Instruction *inst) { - for (auto &blockList : inst->getBlockLists()) { + for (auto ®ion : inst->getRegions()) { // Don't compute dominance if the region is empty. - if (blockList.empty()) + if (region.empty()) continue; auto opDominance = std::make_unique(); - opDominance->recalculate(blockList); - dominanceInfos.try_emplace(&blockList, std::move(opDominance)); + opDominance->recalculate(region); + dominanceInfos.try_emplace(®ion, std::move(opDominance)); } }); } @@ -66,21 +66,21 @@ bool DominanceInfoBase::properlyDominates(const Block *a, if (a == b) return false; - // If both blocks are not in the same block list, 'a' properly dominates 'b' - // if 'b' is defined in an instruction region that (recursively) ends up being + // If both blocks are not in the same region, 'a' properly dominates 'b' if + // 'b' is defined in an instruction region that (recursively) ends up being // dominated by 'a'. Walk up the list of containers enclosing B. - auto *blockListA = a->getParent(), *blockListB = b->getParent(); - if (blockListA != blockListB) { + auto *regionA = a->getParent(), *regionB = b->getParent(); + if (regionA != regionB) { Instruction *bAncestor; do { - bAncestor = blockListB->getContainingInst(); + bAncestor = regionB->getContainingInst(); // If 'bAncestor' is the top level function, then 'a' is a block // that post dominates 'b'. if (!bAncestor) return IsPostDom; - blockListB = bAncestor->getBlock()->getParent(); - } while (blockListA != blockListB); + regionB = bAncestor->getBlock()->getParent(); + } while (regionA != regionB); // Check to see if the ancestor of 'b' is the same block as 'a'. b = bAncestor->getBlock(); @@ -89,8 +89,8 @@ bool DominanceInfoBase::properlyDominates(const Block *a, } // Otherwise, use the standard dominance functionality. - auto baseInfoIt = dominanceInfos.find(blockListA); - assert(baseInfoIt != dominanceInfos.end() && "block list info not found"); + auto baseInfoIt = dominanceInfos.find(regionA); + assert(baseInfoIt != dominanceInfos.end() && "region info not found"); return baseInfoIt->second->properlyDominates(a, b); } diff --git a/mlir/lib/Analysis/LoopAnalysis.cpp b/mlir/lib/Analysis/LoopAnalysis.cpp index 727ce18..0df51eb 100644 --- a/mlir/lib/Analysis/LoopAnalysis.cpp +++ b/mlir/lib/Analysis/LoopAnalysis.cpp @@ -303,7 +303,7 @@ static bool isVectorizableLoopWithCond(ConstOpPointer loop, // No vectorization across unknown regions. auto regions = matcher::Op([](const Instruction &inst) -> bool { - return inst.getNumBlockLists() != 0 && + return inst.getNumRegions() != 0 && !(inst.isa() || inst.isa()); }); SmallVector regionsMatched; diff --git a/mlir/lib/Analysis/Utils.cpp b/mlir/lib/Analysis/Utils.cpp index e330a01..3399c7d 100644 --- a/mlir/lib/Analysis/Utils.cpp +++ b/mlir/lib/Analysis/Utils.cpp @@ -462,8 +462,8 @@ static Instruction *getInstAtPosition(ArrayRef positions, return getInstAtPosition(positions, level + 1, childAffineForOp->getBody()); - for (auto &blockList : inst.getBlockLists()) { - for (auto &b : blockList) + for (auto ®ion : inst.getRegions()) { + for (auto &b : region) if (auto *ret = getInstAtPosition(positions, level + 1, &b)) return ret; } diff --git a/mlir/lib/Analysis/Verifier.cpp b/mlir/lib/Analysis/Verifier.cpp index 0f9365a..cab6758 100644 --- a/mlir/lib/Analysis/Verifier.cpp +++ b/mlir/lib/Analysis/Verifier.cpp @@ -306,8 +306,8 @@ bool FuncVerifier::verifyOperation(const Instruction &op) { } // Verify that all child blocks are ok. - for (auto &blockList : op.getBlockLists()) - for (auto &b : blockList) + for (auto ®ion : op.getRegions()) + for (auto &b : region) if (verifyBlock(b, /*isTopLevel=*/false)) return true; @@ -338,8 +338,8 @@ bool FuncVerifier::verifyInstDominance(const Instruction &inst) { } // Verify the dominance of each of the nested blocks within this instruction. - for (auto &blockList : inst.getBlockLists()) - for (auto &block : blockList) + for (auto ®ion : inst.getRegions()) + for (auto &block : region) if (verifyDominance(block)) return true; diff --git a/mlir/lib/IR/AsmPrinter.cpp b/mlir/lib/IR/AsmPrinter.cpp index b88de85..4454f69b 100644 --- a/mlir/lib/IR/AsmPrinter.cpp +++ b/mlir/lib/IR/AsmPrinter.cpp @@ -1103,9 +1103,8 @@ public: void printSuccessorAndUseList(const Instruction *term, unsigned index) override; - /// Print a block list. - void printBlockList(const BlockList &blocks, - bool printEntryBlockArgs) override { + /// Print a region. + void printRegion(const Region &blocks, bool printEntryBlockArgs) override { os << " {\n"; if (!blocks.empty()) { auto *entryBlock = &blocks.front(); @@ -1164,7 +1163,9 @@ FunctionPrinter::FunctionPrinter(const Function *function, numberValuesInBlock(block); } -/// Number all of the SSA values in the specified block list. +/// Number all of the SSA values in the specified block. Values get numbered +/// continuously throughout regions. In particular, we traverse the regions +/// held by operations and number values in depth-first pre-order. void FunctionPrinter::numberValuesInBlock(const Block &block) { // Each block gets a unique ID, and all of the instructions within it get // numbered as well. @@ -1178,8 +1179,8 @@ void FunctionPrinter::numberValuesInBlock(const Block &block) { // result. if (inst.getNumResults() != 0) numberValueID(inst.getResult(0)); - for (auto &blockList : inst.getBlockLists()) - for (const auto &block : blockList) + for (auto ®ion : inst.getRegions()) + for (const auto &block : region) numberValuesInBlock(block); } } @@ -1219,9 +1220,9 @@ void FunctionPrinter::numberValueID(const Value *value) { // argument is to an entry block of an operation region, give it an 'i' // name. if (auto *block = cast(value)->getOwner()) { - auto *parentBlockList = block->getParent(); - if (parentBlockList && block == &parentBlockList->front()) { - if (parentBlockList->getContainingFunction()) + auto *parentRegion = block->getParent(); + if (parentRegion && block == &parentRegion->front()) { + if (parentRegion->getContainingFunction()) specialName << "arg" << nextArgumentID++; else specialName << "i" << nextRegionArgumentID++; @@ -1279,7 +1280,7 @@ void FunctionPrinter::print() { printTrailingLocation(function->getLoc()); if (!function->empty()) { - printBlockList(function->getBlockList(), /*printEntryBlockArgs=*/false); + printRegion(function->getBody(), /*printEntryBlockArgs=*/false); os << "\n"; } os << '\n'; @@ -1496,9 +1497,9 @@ void FunctionPrinter::printGenericOp(const Instruction *op) { os << ')'; } - // Print any trailing block lists. - for (auto &blockList : op->getBlockLists()) - printBlockList(blockList, /*printEntryBlockArgs=*/true); + // Print any trailing regions. + for (auto ®ion : op->getRegions()) + printRegion(region, /*printEntryBlockArgs=*/true); } void FunctionPrinter::printSuccessorAndUseList(const Instruction *term, diff --git a/mlir/lib/IR/Block.cpp b/mlir/lib/IR/Block.cpp index 60c382f..eddf124 100644 --- a/mlir/lib/IR/Block.cpp +++ b/mlir/lib/IR/Block.cpp @@ -1,4 +1,4 @@ -//===- Block.cpp - MLIR Block and BlockList Classes -----------------------===// +//===- Block.cpp - MLIR Block and Region Classes --------------------------===// // // Copyright 2019 The MLIR Authors. // @@ -66,7 +66,7 @@ Function *Block::getFunction() { void Block::insertBefore(Block *block) { assert(!getParent() && "already inserted into a block!"); assert(block->getParent() && "cannot insert before a block without a parent"); - block->getParent()->getBlocks().insert(BlockList::iterator(block), this); + block->getParent()->getBlocks().insert(Region::iterator(block), this); } /// Unlink this Block from its Function and delete it. @@ -262,26 +262,26 @@ Block *Block::splitBlock(iterator splitBefore) { } //===----------------------------------------------------------------------===// -// BlockList +// Region //===----------------------------------------------------------------------===// -BlockList::BlockList(Function *container) : container(container) {} +Region::Region(Function *container) : container(container) {} -BlockList::BlockList(Instruction *container) : container(container) {} +Region::Region(Instruction *container) : container(container) {} -Instruction *BlockList::getContainingInst() { +Instruction *Region::getContainingInst() { return container.dyn_cast(); } -Function *BlockList::getContainingFunction() { +Function *Region::getContainingFunction() { return container.dyn_cast(); } -/// Clone the internal blocks from this block list into dest. Any +/// Clone the internal blocks from this region into `dest`. Any /// cloned blocks are appended to the back of dest. -void BlockList::cloneInto(BlockList *dest, BlockAndValueMapping &mapper, - MLIRContext *context) const { - assert(dest && "expected valid block list to clone into"); +void Region::cloneInto(Region *dest, BlockAndValueMapping &mapper, + MLIRContext *context) const { + assert(dest && "expected valid region to clone into"); // If the list is empty there is nothing to clone. if (empty()) @@ -321,25 +321,24 @@ void BlockList::cloneInto(BlockList *dest, BlockAndValueMapping &mapper, it->walk(remapOperands); } -BlockList *llvm::ilist_traits<::mlir::Block>::getContainingBlockList() { +Region *llvm::ilist_traits<::mlir::Block>::getContainingRegion() { size_t Offset( - size_t(&((BlockList *)nullptr->*BlockList::getSublistAccess(nullptr)))); + size_t(&((Region *)nullptr->*Region::getSublistAccess(nullptr)))); iplist *Anchor(static_cast *>(this)); - return reinterpret_cast(reinterpret_cast(Anchor) - - Offset); + return reinterpret_cast(reinterpret_cast(Anchor) - Offset); } -/// This is a trait method invoked when a basic block is added to a function. -/// We keep the function pointer up to date. +/// This is a trait method invoked when a basic block is added to a region. +/// We keep the region pointer up to date. void llvm::ilist_traits<::mlir::Block>::addNodeToList(Block *block) { - assert(!block->getParent() && "already in a function!"); - block->parentValidInstOrderPair.setPointer(getContainingBlockList()); + assert(!block->getParent() && "already in a region!"); + block->parentValidInstOrderPair.setPointer(getContainingRegion()); } /// This is a trait method invoked when an instruction is removed from a -/// function. We keep the function pointer up to date. +/// region. We keep the region pointer up to date. void llvm::ilist_traits<::mlir::Block>::removeNodeFromList(Block *block) { - assert(block->getParent() && "not already in a function!"); + assert(block->getParent() && "not already in a region!"); block->parentValidInstOrderPair.setPointer(nullptr); } @@ -349,8 +348,8 @@ void llvm::ilist_traits<::mlir::Block>::transferNodesFromList( ilist_traits &otherList, block_iterator first, block_iterator last) { // If we are transferring instructions within the same function, the parent // pointer doesn't need to be updated. - auto *curParent = getContainingBlockList(); - if (curParent == otherList.getContainingBlockList()) + auto *curParent = getContainingRegion(); + if (curParent == otherList.getContainingRegion()) return; // Update the 'parent' member of each Block. diff --git a/mlir/lib/IR/Builders.cpp b/mlir/lib/IR/Builders.cpp index 1100a2b..56d0ad0 100644 --- a/mlir/lib/IR/Builders.cpp +++ b/mlir/lib/IR/Builders.cpp @@ -311,10 +311,9 @@ Block *FuncBuilder::createBlock(Block *insertBefore) { /// Create an operation given the fields represented as an OperationState. Instruction *FuncBuilder::createOperation(const OperationState &state) { assert(block && "createOperation() called without setting builder's block"); - auto *op = Instruction::create(state.location, state.name, state.operands, - state.types, state.attributes, - state.successors, state.numBlockLists, - state.resizableOperandList, context); + auto *op = Instruction::create( + state.location, state.name, state.operands, state.types, state.attributes, + state.successors, state.numRegions, state.resizableOperandList, context); block->getInstructions().insert(insertPoint, op); return op; } diff --git a/mlir/lib/IR/Function.cpp b/mlir/lib/IR/Function.cpp index 8ad8139..107f1b5 100644 --- a/mlir/lib/IR/Function.cpp +++ b/mlir/lib/IR/Function.cpp @@ -30,14 +30,14 @@ Function::Function(Location location, StringRef name, FunctionType type, ArrayRef attrs) : name(Identifier::get(name, type.getContext())), location(location), type(type), attrs(type.getContext(), attrs), - argAttrs(type.getNumInputs()), blocks(this) {} + argAttrs(type.getNumInputs()), body(this) {} Function::Function(Location location, StringRef name, FunctionType type, ArrayRef attrs, ArrayRef argAttrs) : name(Identifier::get(name, type.getContext())), location(location), type(type), attrs(type.getContext(), attrs), argAttrs(argAttrs), - blocks(this) {} + body(this) {} Function::~Function() { // Instructions may have cyclic references, which need to be dropped before we @@ -160,8 +160,8 @@ void Function::cloneInto(Function *dest, BlockAndValueMapping &mapper) const { } dest->setAttrs(newAttrs.takeVector()); - // Clone the block list. - blocks.cloneInto(&dest->blocks, mapper, dest->getContext()); + // Clone the body. + body.cloneInto(&dest->body, mapper, dest->getContext()); } /// Create a deep copy of this function and all of its blocks, remapping diff --git a/mlir/lib/IR/Instruction.cpp b/mlir/lib/IR/Instruction.cpp index 36a0449..b38ea6a 100644 --- a/mlir/lib/IR/Instruction.cpp +++ b/mlir/lib/IR/Instruction.cpp @@ -134,12 +134,13 @@ void detail::OperandStorage::grow(ResizableStorage &resizeUtil, //===----------------------------------------------------------------------===// /// Create a new Instruction with the specific fields. -Instruction * -Instruction::create(Location location, OperationName name, - ArrayRef operands, ArrayRef resultTypes, - ArrayRef attributes, - ArrayRef successors, unsigned numBlockLists, - bool resizableOperandList, MLIRContext *context) { +Instruction *Instruction::create(Location location, OperationName name, + ArrayRef operands, + ArrayRef resultTypes, + ArrayRef attributes, + ArrayRef successors, + unsigned numRegions, bool resizableOperandList, + MLIRContext *context) { unsigned numSuccessors = successors.size(); // Input operands are nullptr-separated for each successor, the null operands @@ -147,9 +148,9 @@ Instruction::create(Location location, OperationName name, unsigned numOperands = operands.size() - numSuccessors; // Compute the byte size for the instruction and the operand storage. - auto byteSize = totalSizeToAlloc( - resultTypes.size(), numSuccessors, numSuccessors, numBlockLists, + auto byteSize = totalSizeToAlloc( + resultTypes.size(), numSuccessors, numSuccessors, numRegions, /*detail::OperandStorage*/ 1); byteSize += llvm::alignTo(detail::OperandStorage::additionalAllocSize( numOperands, resizableOperandList), @@ -158,15 +159,15 @@ Instruction::create(Location location, OperationName name, // Create the new Instruction. auto inst = ::new (rawMem) - Instruction(location, name, resultTypes.size(), numSuccessors, - numBlockLists, attributes, context); + Instruction(location, name, resultTypes.size(), numSuccessors, numRegions, + attributes, context); assert((numSuccessors == 0 || !inst->isKnownNonTerminator()) && "unexpected successors in a non-terminator operation"); - // Initialize the block lists. - for (unsigned i = 0; i != numBlockLists; ++i) - new (&inst->getBlockList(i)) BlockList(inst); + // Initialize the regions. + for (unsigned i = 0; i != numRegions; ++i) + new (&inst->getRegion(i)) Region(inst); // Initialize the results and operands. new (&inst->getOperandStorage()) @@ -238,11 +239,11 @@ Instruction::create(Location location, OperationName name, Instruction::Instruction(Location location, OperationName name, unsigned numResults, unsigned numSuccessors, - unsigned numBlockLists, + unsigned numRegions, ArrayRef attributes, MLIRContext *context) : location(location), numResults(numResults), numSuccs(numSuccessors), - numBlockLists(numBlockLists), name(name), attrs(context, attributes) {} + numRegions(numRegions), name(name), attrs(context, attributes) {} // Instructions are deleted through the destroy() member because they are // allocated via malloc. @@ -259,9 +260,9 @@ Instruction::~Instruction() { for (auto &successor : getBlockOperands()) successor.~BlockOperand(); - // Explicitly destroy the block list. - for (auto &blockList : getBlockLists()) - blockList.~BlockList(); + // Explicitly destroy the regions. + for (auto ®ion : getRegions()) + region.~Region(); } /// Destroy this instruction or one of its subclasses. @@ -301,16 +302,16 @@ void Instruction::walk(const std::function &callback) { callback(this); // Visit any internal instructions. - for (auto &blockList : getBlockLists()) - for (auto &block : blockList) + for (auto ®ion : getRegions()) + for (auto &block : region) block.walk(callback); } void Instruction::walkPostOrder( const std::function &callback) { // Visit any internal instructions. - for (auto &blockList : llvm::reverse(getBlockLists())) - for (auto &block : llvm::reverse(blockList)) + for (auto ®ion : llvm::reverse(getRegions())) + for (auto &block : llvm::reverse(region)) block.walkPostOrder(callback); // Visit the current instruction. @@ -465,8 +466,8 @@ void Instruction::dropAllReferences() { for (auto &op : getInstOperands()) op.drop(); - for (auto &blockList : getBlockLists()) - for (Block &block : blockList) + for (auto ®ion : getRegions()) + for (Block &block : region) block.dropAllReferences(); for (auto &dest : getBlockOperands()) @@ -603,14 +604,14 @@ Instruction *Instruction::clone(BlockAndValueMapping &mapper, for (auto *result : getResults()) resultTypes.push_back(result->getType()); - unsigned numBlockLists = getNumBlockLists(); + unsigned numRegions = getNumRegions(); auto *newOp = Instruction::create(getLoc(), getName(), operands, resultTypes, - getAttrs(), successors, numBlockLists, + getAttrs(), successors, numRegions, hasResizableOperandsList(), context); - // Clone the block lists. - for (unsigned i = 0; i != numBlockLists; ++i) - getBlockList(i).cloneInto(&newOp->getBlockList(i), mapper, context); + // Clone the regions. + for (unsigned i = 0; i != numRegions; ++i) + getRegion(i).cloneInto(&newOp->getRegion(i), mapper, context); // Remember the mapping of any results. for (unsigned i = 0, e = getNumResults(); i != e; ++i) diff --git a/mlir/lib/Parser/Parser.cpp b/mlir/lib/Parser/Parser.cpp index 7d977ae..7b5050b 100644 --- a/mlir/lib/Parser/Parser.cpp +++ b/mlir/lib/Parser/Parser.cpp @@ -2196,9 +2196,9 @@ public: // Block references. ParseResult - parseOperationBlockList(SmallVectorImpl &results, - ArrayRef> entryArguments); - ParseResult parseBlockListBody(SmallVectorImpl &results); + parseOperationRegion(SmallVectorImpl &results, + ArrayRef> entryArguments); + ParseResult parseRegionBody(SmallVectorImpl &results); ParseResult parseBlock(Block *&block); ParseResult parseBlockBody(Block *block); @@ -2279,7 +2279,7 @@ ParseResult FunctionParser::parseFunctionBody(bool hadNamedArguments) { // Parse the remaining list of blocks. SmallVector blocks; - if (parseBlockListBody(blocks)) + if (parseRegionBody(blocks)) return ParseFailure; function->getBlocks().insert(function->end(), blocks.begin(), blocks.end()); @@ -2307,14 +2307,14 @@ ParseResult FunctionParser::parseFunctionBody(bool hadNamedArguments) { /// /// block-list ::= '{' block-list-body /// -ParseResult FunctionParser::parseOperationBlockList( +ParseResult FunctionParser::parseOperationRegion( SmallVectorImpl &results, ArrayRef> entryArguments) { // Parse the '{'. - if (parseToken(Token::l_brace, "expected '{' to begin block list")) + if (parseToken(Token::l_brace, "expected '{' to begin a region")) return ParseFailure; - // Check for an empty block list. + // Check for an empty region. if (entryArguments.empty() && consumeIf(Token::r_brace)) return ParseSuccess; Block *currentBlock = builder.getInsertionBlock(); @@ -2342,9 +2342,9 @@ ParseResult FunctionParser::parseOperationBlockList( return emitError("entry block arguments were already defined"); } - // Parse the rest of the block list. + // Parse the rest of the region. results.push_back(block); - if (parseBlockListBody(results)) + if (parseRegionBody(results)) return ParseFailure; // Reset insertion point to the current block. @@ -2352,13 +2352,12 @@ ParseResult FunctionParser::parseOperationBlockList( return ParseSuccess; } -/// Block list. +/// Region. /// -/// block-list-body ::= block* '}' +/// region-body ::= block* '}' /// -ParseResult -FunctionParser::parseBlockListBody(SmallVectorImpl &results) { - // Parse the block list. +ParseResult FunctionParser::parseRegionBody(SmallVectorImpl &results) { + // Parse the list of blocks. while (!consumeIf(Token::r_brace)) { Block *newBlock = nullptr; if (parseBlock(newBlock)) { @@ -2440,7 +2439,7 @@ Value *FunctionParser::createForwardReferencePlaceholder(SMLoc loc, Type type) { auto name = OperationName("placeholder", getContext()); auto *inst = Instruction::create( getEncodedSourceLocation(loc), name, /*operands=*/{}, type, - /*attributes=*/{}, /*successors=*/{}, /*numBlockLists=*/0, + /*attributes=*/{}, /*successors=*/{}, /*numRegions=*/0, /*resizableOperandList=*/false, getContext()); forwardReferencePlaceholders[inst->getResult(0)] = loc; return inst->getResult(0); @@ -2888,25 +2887,25 @@ Instruction *FunctionParser::parseGenericOperation() { result.addSuccessor(successor, operands); } - // Parse the optional block lists for this operation. + // Parse the optional regions for this operation. std::vector> blocks; while (getToken().is(Token::l_brace)) { SmallVector newBlocks; - if (parseOperationBlockList(newBlocks, /*entryArguments=*/llvm::None)) { - for (auto &blockList : blocks) - cleanupInvalidBlocks(blockList); + if (parseOperationRegion(newBlocks, /*entryArguments=*/llvm::None)) { + for (auto ®ion : blocks) + cleanupInvalidBlocks(region); return nullptr; } blocks.emplace_back(newBlocks); } - result.reserveBlockLists(blocks.size()); + result.reserveRegions(blocks.size()); auto *opInst = builder.createOperation(result); - // Initialize the parsed block lists. + // Initialize the parsed regions. for (unsigned i = 0, e = blocks.size(); i != e; ++i) { - auto &blockList = opInst->getBlockList(i).getBlocks(); - blockList.insert(blockList.end(), blocks[i].begin(), blocks[i].end()); + auto ®ion = opInst->getRegion(i).getBlocks(); + region.insert(region.end(), blocks[i].begin(), blocks[i].end()); } return opInst; } @@ -2922,23 +2921,22 @@ public: if (opDefinition->parseAssembly(this, opState)) return true; - // Check that enough block lists were reserved for those that were parsed. - if (parsedBlockLists.size() > opState->numBlockLists) { + // Check that enough regions were reserved for those that were parsed. + if (parsedRegions.size() > opState->numRegions) { return emitError( nameLoc, - "parsed more block lists than those reserved in the operation state"); + "parsed more regions than those reserved in the operation state"); } // Check there were no dangling entry block arguments. - if (!parsedBlockListEntryArguments.empty()) { + if (!parsedRegionEntryArguments.empty()) { return emitError( - nameLoc, - "no block list was attached to parsed entry block arguments"); + nameLoc, "no region was attached to parsed entry block arguments"); } // Check that none of the operands of the current operation reference an - // entry block argument for any of the block lists. - for (auto *entryArg : parsedBlockListEntryArgumentPlaceholders) + // entry block argument for any of the region. + for (auto *entryArg : parsedRegionEntryArgumentPlaceholders) if (llvm::is_contained(opState->operands, entryArg)) return emitError(nameLoc, "operand use before it's defined"); @@ -3144,21 +3142,19 @@ public: return result == nullptr; } - /// Parses a list of blocks. - bool parseBlockList() override { - // Parse the block list. + /// Parses a region. + bool parseRegion() override { SmallVector results; - if (parser.parseOperationBlockList(results, parsedBlockListEntryArguments)) + if (parser.parseOperationRegion(results, parsedRegionEntryArguments)) return true; - parsedBlockListEntryArguments.clear(); - parsedBlockLists.emplace_back(results); + parsedRegionEntryArguments.clear(); + parsedRegions.emplace_back(results); return false; } - /// Parses an argument for the entry block of the next block list to be - /// parsed. - bool parseBlockListEntryBlockArgument(Type argType) override { + /// Parses an argument for the entry block of the next region to be parsed. + bool parseRegionEntryBlockArgument(Type argType) override { SmallVector argValues; OperandType operand; if (parseOperand(operand)) @@ -3168,10 +3164,10 @@ public: FunctionParser::SSAUseInfo operandInfo = {operand.name, operand.number, operand.location}; if (auto *value = parser.resolveSSAUse(operandInfo, argType)) { - parsedBlockListEntryArguments.emplace_back(operandInfo, argType); + parsedRegionEntryArguments.emplace_back(operandInfo, argType); // Track each of the placeholders so that we can detect invalid references - // to block list arguments. - parsedBlockListEntryArgumentPlaceholders.emplace_back(value); + // to region arguments. + parsedRegionEntryArgumentPlaceholders.emplace_back(value); return false; } @@ -3199,10 +3195,10 @@ public: /// Emit a diagnostic at the specified location and return true. bool emitError(llvm::SMLoc loc, const Twine &message) override { - // If we emit an error, then cleanup any parsed block lists. - for (auto &blockList : parsedBlockLists) - parser.cleanupInvalidBlocks(blockList); - parsedBlockLists.clear(); + // If we emit an error, then cleanup any parsed regions. + for (auto ®ion : parsedRegions) + parser.cleanupInvalidBlocks(region); + parsedRegions.clear(); parser.emitError(loc, "custom op '" + Twine(opName) + "' " + message); emittedError = true; @@ -3211,16 +3207,16 @@ public: bool didEmitError() const { return emittedError; } - /// Returns the block lists that were parsed. - MutableArrayRef> getParsedBlockLists() { - return parsedBlockLists; + /// Returns the regions that were parsed. + MutableArrayRef> getParsedRegions() { + return parsedRegions; } private: - std::vector> parsedBlockLists; + std::vector> parsedRegions; SmallVector, 2> - parsedBlockListEntryArguments; - SmallVector parsedBlockListEntryArgumentPlaceholders; + parsedRegionEntryArguments; + SmallVector parsedRegionEntryArgumentPlaceholders; SMLoc nameLoc; StringRef opName; FunctionParser &parser; @@ -3271,12 +3267,12 @@ Instruction *FunctionParser::parseCustomOperation() { // Otherwise, we succeeded. Use the state it parsed as our op information. auto *opInst = builder.createOperation(opState); - // Resolve any parsed block lists. - auto parsedBlockLists = opAsmParser.getParsedBlockLists(); - for (unsigned i = 0, e = parsedBlockLists.size(); i != e; ++i) { - auto &opBlockList = opInst->getBlockList(i).getBlocks(); - opBlockList.insert(opBlockList.end(), parsedBlockLists[i].begin(), - parsedBlockLists[i].end()); + // Resolve any parsed regions. + auto parsedRegions = opAsmParser.getParsedRegions(); + for (unsigned i = 0, e = parsedRegions.size(); i != e; ++i) { + auto &opRegion = opInst->getRegion(i).getBlocks(); + opRegion.insert(opRegion.end(), parsedRegions[i].begin(), + parsedRegions[i].end()); } return opInst; } diff --git a/mlir/lib/Transforms/CSE.cpp b/mlir/lib/Transforms/CSE.cpp index 37dfce1..02fbda0 100644 --- a/mlir/lib/Transforms/CSE.cpp +++ b/mlir/lib/Transforms/CSE.cpp @@ -109,7 +109,7 @@ struct CSE : public FunctionPass { bool simplifyOperation(Instruction *op); void simplifyBlock(DominanceInfo &domInfo, Block *bb); - void simplifyBlockList(DominanceInfo &domInfo, BlockList &blockList); + void simplifyRegion(DominanceInfo &domInfo, Region ®ion); void runOnFunction() override; @@ -127,7 +127,7 @@ bool CSE::simplifyOperation(Instruction *op) { // Don't simplify operations with nested blocks. We don't currently model // equality comparisons correctly among other things. It is also unclear // whether we would want to CSE such operations. - if (op->getNumBlockLists() != 0) + if (op->getNumRegions() != 0) return false; // TODO(riverriddle) We currently only eliminate non side-effecting @@ -166,25 +166,25 @@ bool CSE::simplifyOperation(Instruction *op) { void CSE::simplifyBlock(DominanceInfo &domInfo, Block *bb) { for (auto &i : *bb) { - // If the operation is simplified, we don't process any held block lists. + // If the operation is simplified, we don't process any held regions. if (simplifyOperation(&i)) continue; // Simplify any held blocks. - for (auto &blockList : i.getBlockLists()) - simplifyBlockList(domInfo, blockList); + for (auto ®ion : i.getRegions()) + simplifyRegion(domInfo, region); } } -void CSE::simplifyBlockList(DominanceInfo &domInfo, BlockList &blockList) { - // If the block list is empty there is nothing to do. - if (blockList.empty()) +void CSE::simplifyRegion(DominanceInfo &domInfo, Region ®ion) { + // If the region is empty there is nothing to do. + if (region.empty()) return; - // If the block list only contains one block, then simplify it directly. - if (std::next(blockList.begin()) == blockList.end()) { + // If the region only contains one block, then simplify it directly. + if (std::next(region.begin()) == region.end()) { ScopedMapTy::ScopeTy scope(knownValues); - simplifyBlock(domInfo, &blockList.front()); + simplifyBlock(domInfo, ®ion.front()); return; } @@ -196,9 +196,9 @@ void CSE::simplifyBlockList(DominanceInfo &domInfo, BlockList &blockList) { // http://lists.llvm.org/pipermail/llvm-commits/Week-of-Mon-20120116/135228.html std::deque> stack; - // Process the nodes of the dom tree for this blocklist. + // Process the nodes of the dom tree for this region. stack.emplace_back(std::make_unique( - knownValues, domInfo.getRootNode(&blockList))); + knownValues, domInfo.getRootNode(®ion))); while (!stack.empty()) { auto ¤tNode = stack.back(); @@ -223,8 +223,7 @@ void CSE::simplifyBlockList(DominanceInfo &domInfo, BlockList &blockList) { } void CSE::runOnFunction() { - simplifyBlockList(getAnalysis(), - getFunction()->getBlockList()); + simplifyRegion(getAnalysis(), getFunction()->getBody()); // If no operations were erased, then we mark all analyses as preserved. if (opsToErase.empty()) { diff --git a/mlir/lib/Transforms/DialectConversion.cpp b/mlir/lib/Transforms/DialectConversion.cpp index a621db4..72a378b 100644 --- a/mlir/lib/Transforms/DialectConversion.cpp +++ b/mlir/lib/Transforms/DialectConversion.cpp @@ -172,7 +172,7 @@ impl::FunctionConversion::convertBlock(Block *block, FuncBuilder &builder, // Iterate over ops and convert them. for (Instruction &inst : *block) { - if (inst.getNumBlockLists() != 0) { + if (inst.getNumRegions() != 0) { inst.emitError("unsupported region instruction"); return failure(); } diff --git a/mlir/lib/Transforms/LoopFusion.cpp b/mlir/lib/Transforms/LoopFusion.cpp index e05af79..12e52ea 100644 --- a/mlir/lib/Transforms/LoopFusion.cpp +++ b/mlir/lib/Transforms/LoopFusion.cpp @@ -130,7 +130,7 @@ struct LoopNestStateCollector { instToWalk->walk([&](Instruction *opInst) { if (opInst->isa()) forOps.push_back(opInst->cast()); - else if (opInst->getNumBlockLists() != 0) + else if (opInst->getNumRegions() != 0) hasNonForRegion = true; else if (opInst->isa()) loadOpInsts.push_back(opInst); @@ -670,7 +670,7 @@ bool MemRefDependenceGraph::init(Function *f) { auto *memref = inst.cast()->getMemRef(); memrefAccesses[memref].insert(node.id); nodes.insert({node.id, node}); - } else if (inst.getNumBlockLists() != 0) { + } else if (inst.getNumRegions() != 0) { // Return false if another region is found (not currently supported). return false; } else if (inst.getNumResults() > 0 && !inst.use_empty()) { diff --git a/mlir/lib/Transforms/LoopUnroll.cpp b/mlir/lib/Transforms/LoopUnroll.cpp index 4682923..b1a306d 100644 --- a/mlir/lib/Transforms/LoopUnroll.cpp +++ b/mlir/lib/Transforms/LoopUnroll.cpp @@ -109,8 +109,8 @@ void LoopUnroll::runOnFunction() { } bool walkPostOrder(Instruction *opInst) { bool hasInnerLoops = false; - for (auto &blockList : opInst->getBlockLists()) - for (auto &block : blockList) + for (auto ®ion : opInst->getRegions()) + for (auto &block : region) hasInnerLoops |= walkPostOrder(block.begin(), block.end()); if (opInst->isa()) { if (!hasInnerLoops) diff --git a/mlir/lib/Transforms/LoopUnrollAndJam.cpp b/mlir/lib/Transforms/LoopUnrollAndJam.cpp index f1cc7c6..2b92b2a 100644 --- a/mlir/lib/Transforms/LoopUnrollAndJam.cpp +++ b/mlir/lib/Transforms/LoopUnrollAndJam.cpp @@ -132,8 +132,8 @@ LogicalResult mlir::loopUnrollJamByFactor(OpPointer forOp, // This is a linear time walk. void walk(Instruction *inst) { - for (auto &blockList : inst->getBlockLists()) - for (auto &block : blockList) + for (auto ®ion : inst->getRegions()) + for (auto &block : region) walk(block); } void walk(Block &block) { diff --git a/mlir/lib/Transforms/MaterializeVectors.cpp b/mlir/lib/Transforms/MaterializeVectors.cpp index 0d54ead..804991a 100644 --- a/mlir/lib/Transforms/MaterializeVectors.cpp +++ b/mlir/lib/Transforms/MaterializeVectors.cpp @@ -411,7 +411,7 @@ instantiate(FuncBuilder *b, Instruction *opInst, VectorType hwVectorType, "Should call the function specialized for VectorTransferReadOp"); assert(!opInst->isa() && "Should call the function specialized for VectorTransferWriteOp"); - if (opInst->getNumBlockLists() != 0) + if (opInst->getNumRegions() != 0) return nullptr; bool fail = false; @@ -553,7 +553,7 @@ static bool instantiateMaterialization(Instruction *inst, if (inst->isa()) { return false; } - if (inst->getNumBlockLists() != 0) + if (inst->getNumRegions() != 0) return inst->emitError("NYI path Op with region"); if (auto write = inst->dyn_cast()) { diff --git a/mlir/lib/Transforms/Vectorize.cpp b/mlir/lib/Transforms/Vectorize.cpp index b084b01..e7e72fd 100644 --- a/mlir/lib/Transforms/Vectorize.cpp +++ b/mlir/lib/Transforms/Vectorize.cpp @@ -1087,7 +1087,7 @@ static Instruction *vectorizeOneInstruction(FuncBuilder *b, Instruction *opInst, opInst->erase(); return res; } - if (opInst->getNumBlockLists() != 0) + if (opInst->getNumRegions() != 0) return nullptr; auto types = map([state](Value *v) { return getVectorType(v, *state); }, @@ -1112,7 +1112,7 @@ static Instruction *vectorizeOneInstruction(FuncBuilder *b, Instruction *opInst, OperationState newOp(b->getContext(), opInst->getLoc(), opInst->getName().getStringRef(), operands, types, opInst->getAttrs(), /*successors=*/{}, - /*numBlockLists=*/0, opInst->hasResizableOperandsList()); + /*numRegions=*/0, opInst->hasResizableOperandsList()); return b->createOperation(newOp); } diff --git a/mlir/test/IR/invalid.mlir b/mlir/test/IR/invalid.mlir index b96eaaa..cebcdf6 100644 --- a/mlir/test/IR/invalid.mlir +++ b/mlir/test/IR/invalid.mlir @@ -222,7 +222,7 @@ func @malformed_for_to() { func @incomplete_for() { for %i = 1 to 10 step 2 -} // expected-error {{expected '{' to begin block list}} +} // expected-error {{expected '{' to begin a region}} // ----- diff --git a/mlir/test/IR/parser.mlir b/mlir/test/IR/parser.mlir index 34b7462..a9aa477 100644 --- a/mlir/test/IR/parser.mlir +++ b/mlir/test/IR/parser.mlir @@ -787,7 +787,7 @@ func @verbose_if(%N: index) { // CHECK-NEXT: "add" %y = "add"(%c, %N) : (index, index) -> index // CHECK-NEXT: } else { - } { // The else block list. + } { // The else region. // CHECK-NEXT: "add" %z = "add"(%c, %c) : (index, index) -> index } -- 2.7.4