1 //===- TestAffineDataCopy.cpp - Test affine data copy utility -------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file implements a pass to test affine data copy utility functions and 10 // options. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "mlir/Analysis/Utils.h" 15 #include "mlir/Dialect/Affine/IR/AffineOps.h" 16 #include "mlir/Dialect/MemRef/IR/MemRef.h" 17 #include "mlir/Pass/Pass.h" 18 #include "mlir/Transforms/GreedyPatternRewriteDriver.h" 19 #include "mlir/Transforms/LoopUtils.h" 20 #include "mlir/Transforms/Passes.h" 21 22 #define PASS_NAME "test-affine-data-copy" 23 24 using namespace mlir; 25 26 static llvm::cl::OptionCategory clOptionsCategory(PASS_NAME " options"); 27 28 namespace { 29 30 struct TestAffineDataCopy 31 : public PassWrapper<TestAffineDataCopy, FunctionPass> { 32 TestAffineDataCopy() = default; 33 TestAffineDataCopy(const TestAffineDataCopy &pass){}; 34 35 void getDependentDialects(DialectRegistry ®istry) const override { 36 registry.insert<memref::MemRefDialect>(); 37 } 38 void runOnFunction() override; 39 40 private: 41 Option<bool> clMemRefFilter{ 42 *this, "memref-filter", 43 llvm::cl::desc( 44 "Enable memref filter testing in affine data copy optimization"), 45 llvm::cl::init(false)}; 46 Option<bool> clTestGenerateCopyForMemRegion{ 47 *this, "for-memref-region", 48 llvm::cl::desc("Test copy generation for a single memref region"), 49 llvm::cl::init(false)}; 50 }; 51 52 } // end anonymous namespace 53 54 void TestAffineDataCopy::runOnFunction() { 55 // Gather all AffineForOps by loop depth. 56 std::vector<SmallVector<AffineForOp, 2>> depthToLoops; 57 gatherLoops(getFunction(), depthToLoops); 58 assert(depthToLoops.size() && "Loop nest not found"); 59 60 // Only support tests with a single loop nest and a single innermost loop 61 // for now. 62 unsigned innermostLoopIdx = depthToLoops.size() - 1; 63 if (depthToLoops[0].size() != 1 || depthToLoops[innermostLoopIdx].size() != 1) 64 return; 65 66 auto loopNest = depthToLoops[0][0]; 67 auto innermostLoop = depthToLoops[innermostLoopIdx][0]; 68 AffineLoadOp load; 69 if (clMemRefFilter || clTestGenerateCopyForMemRegion) { 70 // Gather MemRef filter. For simplicity, we use the first loaded memref 71 // found in the innermost loop. 72 for (auto &op : *innermostLoop.getBody()) { 73 if (auto ld = dyn_cast<AffineLoadOp>(op)) { 74 load = ld; 75 break; 76 } 77 } 78 } 79 80 AffineCopyOptions copyOptions = {/*generateDma=*/false, 81 /*slowMemorySpace=*/0, 82 /*fastMemorySpace=*/0, 83 /*tagMemorySpace=*/0, 84 /*fastMemCapacityBytes=*/32 * 1024 * 1024UL}; 85 DenseSet<Operation *> copyNests; 86 if (clMemRefFilter) { 87 affineDataCopyGenerate(loopNest, copyOptions, load.getMemRef(), copyNests); 88 } else if (clTestGenerateCopyForMemRegion) { 89 CopyGenerateResult result; 90 MemRefRegion region(loopNest.getLoc()); 91 (void)region.compute(load, /*loopDepth=*/0); 92 (void)generateCopyForMemRegion(region, loopNest, copyOptions, result); 93 } 94 95 // Promote any single iteration loops in the copy nests and simplify 96 // load/stores. 97 SmallVector<Operation *, 4> copyOps; 98 for (auto nest : copyNests) 99 // With a post order walk, the erasure of loops does not affect 100 // continuation of the walk or the collection of load/store ops. 101 nest->walk([&](Operation *op) { 102 if (auto forOp = dyn_cast<AffineForOp>(op)) 103 (void)promoteIfSingleIteration(forOp); 104 else if (auto loadOp = dyn_cast<AffineLoadOp>(op)) 105 copyOps.push_back(loadOp); 106 else if (auto storeOp = dyn_cast<AffineStoreOp>(op)) 107 copyOps.push_back(storeOp); 108 }); 109 110 // Promoting single iteration loops could lead to simplification of 111 // generated load's/store's, and the latter could anyway also be 112 // canonicalized. 113 RewritePatternSet patterns(&getContext()); 114 for (auto op : copyOps) { 115 patterns.clear(); 116 if (isa<AffineLoadOp>(op)) { 117 AffineLoadOp::getCanonicalizationPatterns(patterns, &getContext()); 118 } else { 119 assert(isa<AffineStoreOp>(op) && "expected affine store op"); 120 AffineStoreOp::getCanonicalizationPatterns(patterns, &getContext()); 121 } 122 (void)applyOpPatternsAndFold(op, std::move(patterns)); 123 } 124 } 125 126 namespace mlir { 127 void registerTestAffineDataCopyPass() { 128 PassRegistration<TestAffineDataCopy>( 129 PASS_NAME, "Tests affine data copy utility functions."); 130 } 131 } // namespace mlir 132