//===- AffineExprVisitor.h - MLIR AffineExpr Visitor Class ------*- 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 defines the AffineExpr visitor class. // //===----------------------------------------------------------------------===// #ifndef MLIR_IR_AFFINEEXPRVISITOR_H #define MLIR_IR_AFFINEEXPRVISITOR_H #include "mlir/IR/AffineExpr.h" #include "mlir/Support/LLVM.h" #include "llvm/ADT/ArrayRef.h" namespace mlir { /// Base class for AffineExpr visitors/walkers. /// /// AffineExpr visitors are used when you want to perform different actions /// for different kinds of AffineExprs without having to use lots of casts /// and a big switch instruction. /// /// To define your own visitor, inherit from this class, specifying your /// new type for the 'SubClass' template parameter, and "override" visitXXX /// functions in your class. This class is defined in terms of statically /// resolved overloading, not virtual functions. /// /// The visitor is templated on its return type (`RetTy`). With a WalkResult /// return type, the visitor supports interrupting walks. /// /// For example, here is a visitor that counts the number of for AffineDimExprs /// in an AffineExpr. /// /// /// Declare the class. Note that we derive from AffineExprVisitor /// /// instantiated with our new subclasses_ type. /// /// struct DimExprCounter : public AffineExprVisitor<DimExprCounter> { /// unsigned numDimExprs; /// DimExprCounter() : numDimExprs(0) {} /// void visitDimExpr(AffineDimExpr expr) { ++numDimExprs; } /// }; /// /// And this class would be used like this: /// DimExprCounter dec; /// dec.visit(affineExpr); /// numDimExprs = dec.numDimExprs; /// /// AffineExprVisitor provides visit methods for the following binary affine /// op expressions: /// AffineBinaryAddOpExpr, AffineBinaryMulOpExpr, /// AffineBinaryModOpExpr, AffineBinaryFloorDivOpExpr, /// AffineBinaryCeilDivOpExpr. Note that default implementations of these /// methods will call the general AffineBinaryOpExpr method. /// /// In addition, visit methods are provided for the following affine // expressions: AffineConstantExpr, AffineDimExpr, and // AffineSymbolExpr. /// /// Note that if you don't implement visitXXX for some affine expression type, /// the visitXXX method for Instruction superclass will be invoked. /// /// Note that this class is specifically designed as a template to avoid /// virtual function call overhead. Defining and using a AffineExprVisitor is /// just as efficient as having your own switch instruction over the instruction /// opcode. template <typename SubClass, typename RetTy> class AffineExprVisitorBase { … }; /// See documentation for AffineExprVisitorBase. This visitor supports /// interrupting walks when a `WalkResult` is used for `RetTy`. template <typename SubClass, typename RetTy = void> class AffineExprVisitor : public AffineExprVisitorBase<SubClass, RetTy> { … }; AffineExprVisitor<SubClass, LogicalResult>; // This class is used to flatten a pure affine expression (AffineExpr, // which is in a tree form) into a sum of products (w.r.t constants) when // possible, and in that process simplifying the expression. For a modulo, // floordiv, or a ceildiv expression, an additional identifier, called a local // identifier, is introduced to rewrite the expression as a sum of product // affine expression. Each local identifier is always and by construction a // floordiv of a pure add/mul affine function of dimensional, symbolic, and // other local identifiers, in a non-mutually recursive way. Hence, every local // identifier can ultimately always be recovered as an affine function of // dimensional and symbolic identifiers (involving floordiv's); note however // that by AffineExpr construction, some floordiv combinations are converted to // mod's. The result of the flattening is a flattened expression and a set of // constraints involving just the local variables. // // d2 + (d0 + d1) floordiv 4 is flattened to d2 + q where 'q' is the local // variable introduced, with localVarCst containing 4*q <= d0 + d1 <= 4*q + 3. // // The simplification performed includes the accumulation of contributions for // each dimensional and symbolic identifier together, the simplification of // floordiv/ceildiv/mod expressions and other simplifications that in turn // happen as a result. A simplification that this flattening naturally performs // is of simplifying the numerator and denominator of floordiv/ceildiv, and // folding a modulo expression to a zero, if possible. Three examples are below: // // (d0 + 3 * d1) + d0) - 2 * d1) - d0 simplified to d0 + d1 // (d0 - d0 mod 4 + 4) mod 4 simplified to 0 // (3*d0 + 2*d1 + d0) floordiv 2 + d1 simplified to 2*d0 + 2*d1 // // The way the flattening works for the second example is as follows: d0 % 4 is // replaced by d0 - 4*q with q being introduced: the expression then simplifies // to: (d0 - (d0 - 4q) + 4) = 4q + 4, modulo of which w.r.t 4 simplifies to // zero. Note that an affine expression may not always be expressible purely as // a sum of products involving just the original dimensional and symbolic // identifiers due to the presence of modulo/floordiv/ceildiv expressions that // may not be eliminated after simplification; in such cases, the final // expression can be reconstructed by replacing the local identifiers with their // corresponding explicit form stored in 'localExprs' (note that each of the // explicit forms itself would have been simplified). // // The expression walk method here performs a linear time post order walk that // performs the above simplifications through visit methods, with partial // results being stored in 'operandExprStack'. When a parent expr is visited, // the flattened expressions corresponding to its two operands would already be // on the stack - the parent expression looks at the two flattened expressions // and combines the two. It pops off the operand expressions and pushes the // combined result (although this is done in-place on its LHS operand expr). // When the walk is completed, the flattened form of the top-level expression // would be left on the stack. // // A flattener can be repeatedly used for multiple affine expressions that bind // to the same operands, for example, for all result expressions of an // AffineMap or AffineValueMap. In such cases, using it for multiple expressions // is more efficient than creating a new flattener for each expression since // common identical div and mod expressions appearing across different // expressions are mapped to the same local identifier (same column position in // 'localVarCst'). class SimpleAffineExprFlattener : public AffineExprVisitor<SimpleAffineExprFlattener, LogicalResult> { … }; } // namespace mlir #endif // MLIR_IR_AFFINEEXPRVISITOR_H