Uday Bondhugula | 83a41c9 | 2018-08-30 17:35:15 -0700 | [diff] [blame] | 1 | //===- SimplifyAffineExpr.cpp - MLIR Affine Structures Class-----*- C++ -*-===// |
| 2 | // |
| 3 | // Copyright 2019 The MLIR Authors. |
| 4 | // |
| 5 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | // you may not use this file except in compliance with the License. |
| 7 | // You may obtain a copy of the License at |
| 8 | // |
| 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | // |
| 11 | // Unless required by applicable law or agreed to in writing, software |
| 12 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | // See the License for the specific language governing permissions and |
| 15 | // limitations under the License. |
| 16 | // ============================================================================= |
| 17 | // |
| 18 | // This file implements a pass to simplify affine expressions. |
| 19 | // |
| 20 | //===----------------------------------------------------------------------===// |
| 21 | |
| 22 | #include "mlir/Analysis/AffineStructures.h" |
Uday Bondhugula | 83a41c9 | 2018-08-30 17:35:15 -0700 | [diff] [blame] | 23 | #include "mlir/IR/AffineMap.h" |
| 24 | #include "mlir/IR/Attributes.h" |
| 25 | #include "mlir/IR/StmtVisitor.h" |
| 26 | |
| 27 | #include "mlir/Transforms/Pass.h" |
| 28 | #include "mlir/Transforms/Passes.h" |
| 29 | |
| 30 | using namespace mlir; |
| 31 | using llvm::report_fatal_error; |
| 32 | |
| 33 | namespace { |
| 34 | |
Uday Bondhugula | 128c7aa | 2018-09-04 15:55:38 -0700 | [diff] [blame] | 35 | /// Simplifies all affine expressions appearing in the operation statements of |
| 36 | /// the MLFunction. This is mainly to test the simplifyAffineExpr method. |
Uday Bondhugula | 83a41c9 | 2018-08-30 17:35:15 -0700 | [diff] [blame] | 37 | // TODO(someone): Gradually, extend this to all affine map references found in |
| 38 | // ML functions and CFG functions. |
| 39 | struct SimplifyAffineExpr : public FunctionPass { |
| 40 | explicit SimplifyAffineExpr() {} |
| 41 | |
| 42 | void runOnMLFunction(MLFunction *f); |
| 43 | // Does nothing on CFG functions for now. No reusable walkers/visitors exist |
| 44 | // for this yet? TODO(someone). |
| 45 | void runOnCFGFunction(CFGFunction *f) {} |
| 46 | }; |
| 47 | |
Uday Bondhugula | 83a41c9 | 2018-08-30 17:35:15 -0700 | [diff] [blame] | 48 | } // end anonymous namespace |
| 49 | |
| 50 | FunctionPass *mlir::createSimplifyAffineExprPass() { |
| 51 | return new SimplifyAffineExpr(); |
| 52 | } |
| 53 | |
| 54 | AffineMap *MutableAffineMap::getAffineMap() { |
| 55 | return AffineMap::get(numDims, numSymbols, results, rangeSizes, context); |
| 56 | } |
| 57 | |
| 58 | void SimplifyAffineExpr::runOnMLFunction(MLFunction *f) { |
| 59 | struct MapSimplifier : public StmtWalker<MapSimplifier> { |
| 60 | MLIRContext *context; |
| 61 | MapSimplifier(MLIRContext *context) : context(context) {} |
| 62 | |
| 63 | void visitOperationStmt(OperationStmt *opStmt) { |
| 64 | for (auto attr : opStmt->getAttrs()) { |
| 65 | if (auto *mapAttr = dyn_cast<AffineMapAttr>(attr.second)) { |
| 66 | MutableAffineMap mMap(mapAttr->getValue(), context); |
| 67 | mMap.simplify(); |
| 68 | auto *map = mMap.getAffineMap(); |
| 69 | opStmt->setAttr(attr.first, AffineMapAttr::get(map, context)); |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | }; |
| 74 | |
| 75 | MapSimplifier v(f->getContext()); |
| 76 | v.walkPostOrder(f); |
| 77 | } |