diff --git a/mlir/include/mlir/Analysis/NumberOfExecutions.h b/mlir/include/mlir/Analysis/NumberOfExecutions.h new file mode 100644 --- /dev/null +++ b/mlir/include/mlir/Analysis/NumberOfExecutions.h @@ -0,0 +1,91 @@ +//===- NumberOfExecutions.h - Number of executions analysis -----*- C++ -*-===// +// +// 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 +// +//===----------------------------------------------------------------------===// +// +// This file contains an analysis for computing how many times a block within a +// region is executed *each time* that region is entered. The analysis +// iterates over all associated regions that are attached to the given top-level +// operation. +// +// It is possible to query number of executions information on block level. +// +//===----------------------------------------------------------------------===// + +#ifndef MLIR_ANALYSIS_NUMBER_OF_EXECUTIONS_H +#define MLIR_ANALYSIS_NUMBER_OF_EXECUTIONS_H + +#include "mlir/Support/LLVM.h" +#include "llvm/ADT/DenseMap.h" +#include "llvm/ADT/Optional.h" + +namespace mlir { + +class Block; +class BlockNumberOfExecutionsInfo; +class Operation; +class Region; + +/// Represents an analysis for computing how many times a block within a region +/// is executed *each time* that region is entered. The analysis iterates over +/// all associated regions that are attached to the given top-level operation. +class NumberOfExecutions { +public: + /// Creates a new NumberOfExecutions analysis that computes how many times a + /// block within a region is executed for all associated regions. + explicit NumberOfExecutions(Operation *op); + + /// Returns the number of times operations `op` is executed *each time* the + /// control flow enters the region `perEntryOfThisRegion`. Returns empty + /// optional if this is not known statically. + Optional getNumberOfExecutions(Operation *op, + Region *perEntryOfThisRegion) const; + + /// Returns the number of times block `block` is executed *each time* the + /// control flow enters the region `perEntryOfThisRegion`. Returns empty + /// optional if this is not known statically. + Optional getNumberOfExecutions(Block *block, + Region *perEntryOfThisRegion) const; + + /// Dumps the number of block executions *each time* the control flow enters + /// the region `perEntryOfThisRegion` to the given stream. + void printBlockExecutions(raw_ostream &os, + Region *perEntryOfThisRegion) const; + + /// Dumps the number of operation executions *each time* the control flow + /// enters the region `perEntryOfThisRegion` to the given stream. + void printOperationExecutions(raw_ostream &os, + Region *perEntryOfThisRegion) const; + +private: + /// The operation this analysis was constructed from. + Operation *operation; + + /// A mapping from blocks to number of executions information. + DenseMap blockNumbersOfExecution; +}; + +/// Represents number of block executions information. This class is not default +/// constructible to catch accidental construction inside map-like container. +class BlockNumberOfExecutionsInfo { +public: + /// Returns the number of times this block will be executed within a parent + /// operation *each time* the parent operation is executed. This does not take + /// into account how many times the parent operation itself will be executed. + Optional getNumberOfExecutions() const; + + static BlockNumberOfExecutionsInfo known(int64_t value); + static BlockNumberOfExecutionsInfo unknown(); + +private: + BlockNumberOfExecutionsInfo(Optional numberOfExecutions); + + Optional numberOfExecutions; +}; + +} // end namespace mlir + +#endif // MLIR_ANALYSIS_NUMBER_OF_EXECUTIONS_H diff --git a/mlir/include/mlir/Dialect/Async/IR/Async.h b/mlir/include/mlir/Dialect/Async/IR/Async.h --- a/mlir/include/mlir/Dialect/Async/IR/Async.h +++ b/mlir/include/mlir/Dialect/Async/IR/Async.h @@ -19,6 +19,7 @@ #include "mlir/IR/OpDefinition.h" #include "mlir/IR/OpImplementation.h" #include "mlir/IR/StandardTypes.h" +#include "mlir/Interfaces/ControlFlowInterfaces.h" #include "mlir/Interfaces/SideEffectInterfaces.h" namespace mlir { diff --git a/mlir/include/mlir/Dialect/Async/IR/AsyncOps.td b/mlir/include/mlir/Dialect/Async/IR/AsyncOps.td --- a/mlir/include/mlir/Dialect/Async/IR/AsyncOps.td +++ b/mlir/include/mlir/Dialect/Async/IR/AsyncOps.td @@ -14,6 +14,7 @@ #define ASYNC_OPS include "mlir/Dialect/Async/IR/AsyncBase.td" +include "mlir/Interfaces/ControlFlowInterfaces.td" include "mlir/Interfaces/SideEffectInterfaces.td" //===----------------------------------------------------------------------===// @@ -26,6 +27,8 @@ def Async_ExecuteOp : Async_Op<"execute", [SingleBlockImplicitTerminator<"YieldOp">, + DeclareOpInterfaceMethods, AttrSizedOperandSegments]> { let summary = "Asynchronous execute operation"; let description = [{ diff --git a/mlir/include/mlir/Dialect/SCF/SCFOps.td b/mlir/include/mlir/Dialect/SCF/SCFOps.td --- a/mlir/include/mlir/Dialect/SCF/SCFOps.td +++ b/mlir/include/mlir/Dialect/SCF/SCFOps.td @@ -196,6 +196,11 @@ /// induction variable. LoopOp only has one region, so 0 is the only valid /// value for `index`. OperandRange getSuccessorEntryOperands(unsigned index); + + /// Returns the number of invocations of the body block if the loop bounds + /// are constants. Returns `kUnknownNumRegionInvocations` otherwise. + void getNumRegionInvocations(ArrayRef operands, + SmallVectorImpl &countPerRegion); }]; let hasCanonicalizer = 1; diff --git a/mlir/include/mlir/Interfaces/ControlFlowInterfaces.h b/mlir/include/mlir/Interfaces/ControlFlowInterfaces.h --- a/mlir/include/mlir/Interfaces/ControlFlowInterfaces.h +++ b/mlir/include/mlir/Interfaces/ControlFlowInterfaces.h @@ -41,6 +41,9 @@ // RegionBranchOpInterface //===----------------------------------------------------------------------===// +// A constant value to represent unknown number of region invocations. +extern const int64_t kUnknownNumRegionInvocations; + namespace detail { /// Verify that types match along control flow edges described the given op. LogicalResult verifyTypesAlongControlFlowEdges(Operation *op); diff --git a/mlir/include/mlir/Interfaces/ControlFlowInterfaces.td b/mlir/include/mlir/Interfaces/ControlFlowInterfaces.td --- a/mlir/include/mlir/Interfaces/ControlFlowInterfaces.td +++ b/mlir/include/mlir/Interfaces/ControlFlowInterfaces.td @@ -130,6 +130,26 @@ "void", "getSuccessorRegions", (ins "Optional":$index, "ArrayRef":$operands, "SmallVectorImpl &":$regions) + >, + InterfaceMethod<[{ + Populates countPerRegion with the number of times this operation will + invoke the attached regions (assuming the regions yield normally, i.e. + do not abort or invoke an infinite loop). If the number of region + invocations is not known statically it will set the number of + invocations to `kUnknownNumRegionInvocations`. + + `operands` is a set of optional attributes that either correspond to a + constant values for each operand of this operation, or null if that + operand is not a constant. + }], + "void", "getNumRegionInvocations", + (ins "ArrayRef":$operands, + "SmallVectorImpl &":$countPerRegion), [{}], + /*defaultImplementation=*/[{ + unsigned numRegions = this->getOperation()->getNumRegions(); + assert(countPerRegion.empty()); + countPerRegion.resize(numRegions, kUnknownNumRegionInvocations); + }] > ]; diff --git a/mlir/include/mlir/Support/MathExtras.h b/mlir/include/mlir/Support/MathExtras.h --- a/mlir/include/mlir/Support/MathExtras.h +++ b/mlir/include/mlir/Support/MathExtras.h @@ -19,19 +19,21 @@ namespace mlir { /// Returns the result of MLIR's ceildiv operation on constants. The RHS is -/// expected to be positive. +/// expected to be non-zero. inline int64_t ceilDiv(int64_t lhs, int64_t rhs) { - assert(rhs >= 1); + assert(rhs != 0); // C/C++'s integer division rounds towards 0. - return lhs % rhs > 0 ? lhs / rhs + 1 : lhs / rhs; + int64_t x = (rhs > 0) ? -1 : 1; + return (lhs * rhs > 0) ? ((lhs + x) / rhs) + 1 : -(-lhs / rhs); } /// Returns the result of MLIR's floordiv operation on constants. The RHS is -/// expected to be positive. +/// expected to be non-zero. inline int64_t floorDiv(int64_t lhs, int64_t rhs) { - assert(rhs >= 1); + assert(rhs != 0); // C/C++'s integer division rounds towards 0. - return lhs % rhs < 0 ? lhs / rhs - 1 : lhs / rhs; + int64_t x = (rhs < 0) ? 1 : -1; + return (lhs * rhs < 0) ? -((-lhs + x) / rhs) - 1 : lhs / rhs; } /// Returns MLIR's mod operation on constants. MLIR's mod operation yields the diff --git a/mlir/lib/Analysis/CMakeLists.txt b/mlir/lib/Analysis/CMakeLists.txt --- a/mlir/lib/Analysis/CMakeLists.txt +++ b/mlir/lib/Analysis/CMakeLists.txt @@ -6,6 +6,7 @@ Liveness.cpp LoopAnalysis.cpp NestedMatcher.cpp + NumberOfExecutions.cpp PresburgerSet.cpp SliceAnalysis.cpp Utils.cpp @@ -15,6 +16,7 @@ BufferAliasAnalysis.cpp CallGraph.cpp Liveness.cpp + NumberOfExecutions.cpp SliceAnalysis.cpp ADDITIONAL_HEADER_DIRS @@ -53,5 +55,5 @@ MLIRPresburger MLIRSCF ) - + add_subdirectory(Presburger) diff --git a/mlir/lib/Analysis/NumberOfExecutions.cpp b/mlir/lib/Analysis/NumberOfExecutions.cpp new file mode 100644 --- /dev/null +++ b/mlir/lib/Analysis/NumberOfExecutions.cpp @@ -0,0 +1,239 @@ +//===- NumberOfExecutions.cpp - Number of executions analysis -------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// +// +// Implementation of the number of executions analysis. +// +//===----------------------------------------------------------------------===// + +#include "mlir/Analysis/NumberOfExecutions.h" +#include "mlir/Dialect/StandardOps/IR/Ops.h" +#include "mlir/IR/Function.h" +#include "mlir/IR/Matchers.h" +#include "mlir/IR/RegionKindInterface.h" +#include "mlir/Interfaces/ControlFlowInterfaces.h" + +#include "llvm/ADT/FunctionExtras.h" +#include "llvm/ADT/SmallSet.h" +#include "llvm/Support/raw_ostream.h" + +#define DEBUG_TYPE "number-of-executions-analysis" + +using namespace mlir; + +//===----------------------------------------------------------------------===// +// NumberOfExecutions +//===----------------------------------------------------------------------===// + +/// Computes blocks number of executions information for the given region. +static void computeRegionBlockNumberOfExecutions( + Region ®ion, DenseMap &blockInfo) { + Operation *parentOp = region.getParentOp(); + int regionId = region.getRegionNumber(); + + // CFG analysis does not make sense for Graph regions. + if (auto kindInterface = dyn_cast(parentOp)) { + auto kind = kindInterface.getRegionKind(regionId); + assert(kind == RegionKind::SSACFG && "only SSA CFG regions are supported"); + (void)kind; + } + + // Number of region invocations for all attached regions. + SmallVector numRegionsInvocations; + + // Query RegionBranchOpInterface interface if it is available. + if (auto regionInterface = dyn_cast(parentOp)) { + SmallVector operands; + operands.reserve(parentOp->getNumOperands()); + + for (Value operand : parentOp->getOperands()) { + Attribute attr; + if (matchPattern(operand, m_Constant(&attr))) + operands.push_back(attr); + else + operands.push_back(Attribute()); + } + + regionInterface.getNumRegionInvocations(operands, numRegionsInvocations); + } + + // FunctionLike operations be default execute attached region once. + if (numRegionsInvocations.empty() && + parentOp->hasTrait() && + parentOp->hasTrait()) { + numRegionsInvocations.push_back(1); + } + + // If we do not have region invocation information then we can't compute + // execution number for the blocks inside the region. + if (numRegionsInvocations.empty() || + numRegionsInvocations[regionId] == kUnknownNumRegionInvocations) { + for (Block &block : region) + blockInfo.insert({&block, BlockNumberOfExecutionsInfo::unknown()}); + return; + } + + int64_t numRegionInvocations = numRegionsInvocations[regionId]; + + // DFS traversal looking for loops in the CFG. + llvm::SmallSet loopStart; + + llvm::unique_function &)> dfs = + [&](Block *block, llvm::SmallSet &visited) { + // Found a loop in the CFG that starts at the `block`. + if (visited.contains(block)) { + loopStart.insert(block); + return; + } + + // Continue DFS traversal. + visited.insert(block); + for (Block *successor : block->getSuccessors()) + dfs(successor, visited); + visited.erase(block); + }; + + llvm::SmallSet visited; + dfs(®ion.front(), visited); + + // Start from the entry block and follow only blocks with single succesor. + Block *block = ®ion.front(); + while (block && !loopStart.contains(block)) { + // Block will be executed exactly once. + blockInfo.insert( + {block, BlockNumberOfExecutionsInfo::known(numRegionInvocations)}); + + // We reached the exit block or block with multiple successors. + if (block->getNumSuccessors() != 1) + break; + + // Continue traversal. + block = block->getSuccessor(0); + } + + // For all blocks that we did not visit set the executions number to unknown. + for (Block &block : region) + if (blockInfo.count(&block) == 0) + blockInfo.insert({&block, BlockNumberOfExecutionsInfo::unknown()}); +} + +/// Creates a new NumberOfExecutions analysis that computes how many times a +/// block within a region is executed for all associated regions. +NumberOfExecutions::NumberOfExecutions(Operation *op) : operation(op) { + operation->walk([&](Region *region) { + computeRegionBlockNumberOfExecutions(*region, blockNumbersOfExecution); + }); +} + +Optional +NumberOfExecutions::getNumberOfExecutions(Operation *op, + Region *perEntryOfThisRegion) const { + // Assuming that all operations complete in the finite amount of time (do not + // abort and do not go into the infinite loop), the number of operation + // executions is equal to the number of block executions that contains the + // operation. + return getNumberOfExecutions(op->getBlock(), perEntryOfThisRegion); +} + +Optional +NumberOfExecutions::getNumberOfExecutions(Block *block, + Region *perEntryOfThisRegion) const { + // Return None if the given `block` does not lie inside the + // `perEntryOfThisRegion` region. + if (!perEntryOfThisRegion->findAncestorBlockInRegion(*block)) + return None; + + // Find the block information for the given `block. + auto blockIt = blockNumbersOfExecution.find(block); + if (blockIt == blockNumbersOfExecution.end()) + return None; + const auto &blockInfo = blockIt->getSecond(); + + // Immediately return None if we do not know block number of executions + // within its parent operation. + if (!blockInfo.getNumberOfExecutions().hasValue()) + return None; + + // Follow parent operations until we reach the operations that owns the + // `perEntryOfThisRegion`. + int64_t numberOfExecutions = *blockInfo.getNumberOfExecutions(); + Operation *parentOp = block->getParentOp(); + + while (parentOp != perEntryOfThisRegion->getParentOp()) { + // Find how many times will be executed the block that owns the parent + // operation. + Block *parentBlock = parentOp->getBlock(); + + auto parentBlockIt = blockNumbersOfExecution.find(parentBlock); + if (parentBlockIt == blockNumbersOfExecution.end()) + return None; + const auto &parentBlockInfo = parentBlockIt->getSecond(); + + // We stumbled upon an operation with unknown number of executions. + if (!parentBlockInfo.getNumberOfExecutions().hasValue()) + return None; + + // Number of block executions is a product of all parent blocks executions. + numberOfExecutions *= *parentBlockInfo.getNumberOfExecutions(); + parentOp = parentOp->getParentOp(); + + assert(parentOp != nullptr); + } + + return numberOfExecutions; +} + +void NumberOfExecutions::printBlockExecutions( + raw_ostream &os, Region *perEntryOfThisRegion) const { + unsigned blockId = 0; + + operation->walk([&](Block *block) { + llvm::errs() << "Block: " << blockId++ << "\n"; + llvm::errs() << "Number of executions: "; + if (auto n = getNumberOfExecutions(block, perEntryOfThisRegion)) + llvm::errs() << *n << "\n"; + else + llvm::errs() << "\n"; + }); +} + +void NumberOfExecutions::printOperationExecutions( + raw_ostream &os, Region *perEntryOfThisRegion) const { + operation->walk([&](Block *block) { + block->walk([&](Operation *operation) { + // Skip the operation that was used to build the analysis. + if (operation == this->operation) + return; + + llvm::errs() << "Operation: " << operation->getName() << "\n"; + llvm::errs() << "Number of executions: "; + if (auto n = getNumberOfExecutions(operation, perEntryOfThisRegion)) + llvm::errs() << *n << "\n"; + else + llvm::errs() << "\n"; + }); + }); +} + +//===----------------------------------------------------------------------===// +// BlockNumberOfExecutionsInfo +//===----------------------------------------------------------------------===// + +BlockNumberOfExecutionsInfo::BlockNumberOfExecutionsInfo( + Optional numberOfExecutions) + : numberOfExecutions(numberOfExecutions) {} + +Optional BlockNumberOfExecutionsInfo::getNumberOfExecutions() const { + return numberOfExecutions; +} + +BlockNumberOfExecutionsInfo BlockNumberOfExecutionsInfo::known(int64_t value) { + return {value}; +} +BlockNumberOfExecutionsInfo BlockNumberOfExecutionsInfo::unknown() { + return {None}; +} diff --git a/mlir/lib/Dialect/Async/IR/Async.cpp b/mlir/lib/Dialect/Async/IR/Async.cpp --- a/mlir/lib/Dialect/Async/IR/Async.cpp +++ b/mlir/lib/Dialect/Async/IR/Async.cpp @@ -118,6 +118,27 @@ constexpr char kOperandSegmentSizesAttr[] = "operand_segment_sizes"; +void ExecuteOp::getNumRegionInvocations( + ArrayRef operands, SmallVectorImpl &countPerRegion) { + (void)operands; + assert(countPerRegion.empty()); + countPerRegion.push_back(1); +} + +void ExecuteOp::getSuccessorRegions(Optional index, + ArrayRef operands, + SmallVectorImpl ®ions) { + // The `body` region branch back to the parent operation. + if (index.hasValue()) { + assert(*index == 0); + regions.push_back(RegionSuccessor(getResults())); + return; + } + + // Otherwise the successor is the body region. + regions.push_back(RegionSuccessor(&body())); +} + static void print(OpAsmPrinter &p, ExecuteOp op) { p << op.getOperationName(); diff --git a/mlir/lib/Dialect/SCF/SCF.cpp b/mlir/lib/Dialect/SCF/SCF.cpp --- a/mlir/lib/Dialect/SCF/SCF.cpp +++ b/mlir/lib/Dialect/SCF/SCF.cpp @@ -10,6 +10,7 @@ #include "mlir/Dialect/StandardOps/IR/Ops.h" #include "mlir/IR/BlockAndValueMapping.h" #include "mlir/IR/PatternMatch.h" +#include "mlir/Support/MathExtras.h" #include "mlir/Transforms/InliningUtils.h" using namespace mlir; @@ -284,6 +285,26 @@ regions.push_back(RegionSuccessor(getResults())); } +void ForOp::getNumRegionInvocations(ArrayRef operands, + SmallVectorImpl &countPerRegion) { + assert(countPerRegion.empty()); + countPerRegion.resize(1); + + auto lb = operands[0].dyn_cast_or_null(); + auto ub = operands[1].dyn_cast_or_null(); + auto step = operands[2].dyn_cast_or_null(); + + // Loop bounds are not known statically. + if (!lb || !ub || !step || step.getValue().getSExtValue() == 0) { + countPerRegion[0] = -1; + return; + } + + countPerRegion[0] = + ceilDiv(ub.getValue().getSExtValue() - lb.getValue().getSExtValue(), + step.getValue().getSExtValue()); +} + ValueVector mlir::scf::buildLoopNest( OpBuilder &builder, Location loc, ValueRange lbs, ValueRange ubs, ValueRange steps, ValueRange iterArgs, diff --git a/mlir/lib/Interfaces/ControlFlowInterfaces.cpp b/mlir/lib/Interfaces/ControlFlowInterfaces.cpp --- a/mlir/lib/Interfaces/ControlFlowInterfaces.cpp +++ b/mlir/lib/Interfaces/ControlFlowInterfaces.cpp @@ -73,6 +73,9 @@ // RegionBranchOpInterface //===----------------------------------------------------------------------===// +// A constant value to represent unknown number of region invocations. +const int64_t mlir::kUnknownNumRegionInvocations = -1; + /// Verify that types match along all region control flow edges originating from /// `sourceNo` (region # if source is a region, llvm::None if source is parent /// op). `getInputsTypesForRegion` is a function that returns the types of the diff --git a/mlir/test/Analysis/test-number-of-block-executions.mlir b/mlir/test/Analysis/test-number-of-block-executions.mlir new file mode 100644 --- /dev/null +++ b/mlir/test/Analysis/test-number-of-block-executions.mlir @@ -0,0 +1,192 @@ +// RUN: mlir-opt %s \ +// RUN: -test-print-number-of-block-executions \ +// RUN: -split-input-file 2>&1 \ +// RUN: | FileCheck %s --dump-input=always + +// CHECK-LABEL: Number of executions: empty +func @empty() { + // CHECK: Block: 0 + // CHECK-NEXT: Number of executions: 1 + return +} + +// ----- + +// CHECK-LABEL: Number of executions: sequential +func @sequential() { + // CHECK: Block: 0 + // CHECK-NEXT: Number of executions: 1 + br ^bb1 +^bb1: + // CHECK: Block: 1 + // CHECK-NEXT: Number of executions: 1 + br ^bb2 +^bb2: + // CHECK: Block: 2 + // CHECK-NEXT: Number of executions: 1 + return +} + +// ----- + +// CHECK-LABEL: Number of executions: conditional +func @conditional(%cond : i1) { + // CHECK: Block: 0 + // CHECK-NEXT: Number of executions: 1 + br ^bb1 +^bb1: + // CHECK: Block: 1 + // CHECK-NEXT: Number of executions: 1 + cond_br %cond, ^bb2, ^bb3 +^bb2: + // CHECK: Block: 2 + // CHECK-NEXT: Number of executions: + br ^bb4 +^bb3: + // CHECK: Block: 3 + // CHECK-NEXT: Number of executions: + br ^bb4 +^bb4: + // CHECK: Block: 4 + // CHECK-NEXT: Number of executions: + return +} + +// ----- + +// CHECK-LABEL: Number of executions: loop +func @loop(%cond : i1) { + // CHECK: Block: 0 + // CHECK-NEXT: Number of executions: 1 + br ^bb1 +^bb1: + // CHECK: Block: 1 + // CHECK-NEXT: Number of executions: + br ^bb2 +^bb2: + // CHECK: Block: 2 + // CHECK-NEXT: Number of executions: + br ^bb3 +^bb3: + // CHECK: Block: 3 + // CHECK-NEXT: Number of executions: + cond_br %cond, ^bb1, ^bb4 +^bb4: + // CHECK: Block: 4 + // CHECK-NEXT: Number of executions: + return +} + +// ----- + +// CHECK-LABEL: Number of executions: scf_if_dynamic_branch +func @scf_if_dynamic_branch(%cond : i1) { + // CHECK: Block: 0 + // CHECK-NEXT: Number of executions: 1 + scf.if %cond { + // CHECK: Block: 1 + // CHECK-NEXT: Number of executions: + } else { + // CHECK: Block: 2 + // CHECK-NEXT: Number of executions: + } + return +} + +// ----- + +// CHECK-LABEL: Number of executions: async_execute +func @async_execute() { + // CHECK: Block: 0 + // CHECK-NEXT: Number of executions: 1 + async.execute { + // CHECK: Block: 1 + // CHECK-NEXT: Number of executions: 1 + async.yield + } + return +} + +// ----- + +// CHECK-LABEL: Number of executions: async_execute_with_scf_if +func @async_execute_with_scf_if(%cond : i1) { + // CHECK: Block: 0 + // CHECK-NEXT: Number of executions: 1 + async.execute { + // CHECK: Block: 1 + // CHECK-NEXT: Number of executions: 1 + scf.if %cond { + // CHECK: Block: 2 + // CHECK-NEXT: Number of executions: + } else { + // CHECK: Block: 3 + // CHECK-NEXT: Number of executions: + } + async.yield + } + return +} + +// ----- + +// CHECK-LABEL: Number of executions: scf_for_constant_bounds +func @scf_for_constant_bounds() { + // CHECK: Block: 0 + // CHECK-NEXT: Number of executions: 1 + %c0 = constant 0 : index + %c1 = constant 1 : index + %c2 = constant 2 : index + + scf.for %i = %c0 to %c2 step %c1 { + // CHECK: Block: 1 + // CHECK-NEXT: Number of executions: 2 + } + + return +} + +// ----- + +// CHECK-LABEL: Number of executions: propagate_parent_num_executions +func @propagate_parent_num_executions() { + // CHECK: Block: 0 + // CHECK-NEXT: Number of executions: 1 + %c0 = constant 0 : index + %c1 = constant 1 : index + %c2 = constant 2 : index + + scf.for %i = %c0 to %c2 step %c1 { + // CHECK: Block: 1 + // CHECK-NEXT: Number of executions: 2 + async.execute { + // CHECK: Block: 2 + // CHECK-NEXT: Number of executions: 2 + async.yield + } + } + + return +} + +// ----- + +// CHECK-LABEL: Number of executions: clear_num_executions +func @clear_num_executions(%step : index) { + // CHECK: Block: 0 + // CHECK-NEXT: Number of executions: 1 + %c0 = constant 0 : index + %c2 = constant 2 : index + + scf.for %i = %c0 to %c2 step %step { + // CHECK: Block: 1 + // CHECK-NEXT: Number of executions: + async.execute { + // CHECK: Block: 2 + // CHECK-NEXT: Number of executions: + async.yield + } + } + + return +} diff --git a/mlir/test/Analysis/test-number-of-operation-executions.mlir b/mlir/test/Analysis/test-number-of-operation-executions.mlir new file mode 100644 --- /dev/null +++ b/mlir/test/Analysis/test-number-of-operation-executions.mlir @@ -0,0 +1,66 @@ +// RUN: mlir-opt %s \ +// RUN: -test-print-number-of-operation-executions \ +// RUN: -split-input-file 2>&1 \ +// RUN: | FileCheck %s --dump-input=always + +// CHECK-LABEL: Number of executions: empty +func @empty() { + // CHECK: Operation: std.return + // CHECK-NEXT: Number of executions: 1 + return +} + +// ----- + +// CHECK-LABEL: Number of executions: propagate_parent_num_executions +func @propagate_parent_num_executions() { + // CHECK: Operation: std.constant + // CHECK-NEXT: Number of executions: 1 + %c0 = constant 0 : index + // CHECK: Operation: std.constant + // CHECK-NEXT: Number of executions: 1 + %c1 = constant 1 : index + // CHECK: Operation: std.constant + // CHECK-NEXT: Number of executions: 1 + %c2 = constant 2 : index + + // CHECK-DAG: Operation: scf.for + // CHECK-NEXT: Number of executions: 1 + scf.for %i = %c0 to %c2 step %c1 { + // CHECK-DAG: Operation: async.execute + // CHECK-NEXT: Number of executions: 2 + async.execute { + // CHECK-DAG: Operation: async.yield + // CHECK-NEXT: Number of executions: 2 + async.yield + } + } + + return +} + +// ----- + +// CHECK-LABEL: Number of executions: clear_num_executions +func @clear_num_executions(%step : index) { + // CHECK: Operation: std.constant + // CHECK-NEXT: Number of executions: 1 + %c0 = constant 0 : index + // CHECK: Operation: std.constant + // CHECK-NEXT: Number of executions: 1 + %c2 = constant 2 : index + + // CHECK: Operation: scf.for + // CHECK-NEXT: Number of executions: 1 + scf.for %i = %c0 to %c2 step %step { + // CHECK: Operation: async.execute + // CHECK-NEXT: Number of executions: + async.execute { + // CHECK: Operation: async.yield + // CHECK-NEXT: Number of executions: + async.yield + } + } + + return +} diff --git a/mlir/test/lib/Transforms/CMakeLists.txt b/mlir/test/lib/Transforms/CMakeLists.txt --- a/mlir/test/lib/Transforms/CMakeLists.txt +++ b/mlir/test/lib/Transforms/CMakeLists.txt @@ -25,6 +25,7 @@ TestLoopMapping.cpp TestLoopParametricTiling.cpp TestLoopUnrolling.cpp + TestNumberOfExecutions.cpp TestOpaqueLoc.cpp TestMemRefBoundCheck.cpp TestMemRefDependenceCheck.cpp diff --git a/mlir/test/lib/Transforms/TestNumberOfExecutions.cpp b/mlir/test/lib/Transforms/TestNumberOfExecutions.cpp new file mode 100644 --- /dev/null +++ b/mlir/test/lib/Transforms/TestNumberOfExecutions.cpp @@ -0,0 +1,55 @@ +//===- TestNumberOfExecutions.cpp - Test number of executions analysis ----===// +// +// 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 +// +//===----------------------------------------------------------------------===// +// +// This file contains test passes for constructing and resolving number of +// executions information. +// +//===----------------------------------------------------------------------===// + +#include "mlir/Analysis/NumberOfExecutions.h" +#include "mlir/Pass/Pass.h" + +using namespace mlir; + +namespace { + +struct TestNumberOfBlockExecutionsPass + : public PassWrapper { + void runOnFunction() override { + llvm::errs() << "Number of executions: " << getFunction().getName() << "\n"; + getAnalysis().printBlockExecutions( + llvm::errs(), &getFunction().getBody()); + } +}; + +struct TestNumberOfOperationExecutionsPass + : public PassWrapper { + void runOnFunction() override { + llvm::errs() << "Number of executions: " << getFunction().getName() << "\n"; + getAnalysis().printOperationExecutions( + llvm::errs(), &getFunction().getBody()); + } +}; + +} // end anonymous namespace + +namespace mlir { +void registerTestNumberOfBlockExecutionsPass() { + PassRegistration( + "test-print-number-of-block-executions", + "Print the contents of a constructed number of executions analysis for " + "all blocks."); +} + +void registerTestNumberOfOperationExecutionsPass() { + PassRegistration( + "test-print-number-of-operation-executions", + "Print the contents of a constructed number of executions analysis for " + "all operations."); +} +} // namespace mlir diff --git a/mlir/tools/mlir-opt/mlir-opt.cpp b/mlir/tools/mlir-opt/mlir-opt.cpp --- a/mlir/tools/mlir-opt/mlir-opt.cpp +++ b/mlir/tools/mlir-opt/mlir-opt.cpp @@ -72,6 +72,8 @@ void registerTestMatchers(); void registerTestMemRefDependenceCheck(); void registerTestMemRefStrideCalculation(); +void registerTestNumberOfBlockExecutionsPass(); +void registerTestNumberOfOperationExecutionsPass(); void registerTestOpaqueLoc(); void registerTestPreparationPassWithAllowedMemrefResults(); void registerTestPrintDefUsePass(); @@ -133,6 +135,8 @@ registerTestMatchers(); registerTestMemRefDependenceCheck(); registerTestMemRefStrideCalculation(); + registerTestNumberOfBlockExecutionsPass(); + registerTestNumberOfOperationExecutionsPass(); registerTestOpaqueLoc(); registerTestPreparationPassWithAllowedMemrefResults(); registerTestPrintDefUsePass();