14ead2cf7SAlex Zinenko //===- VectorToSCF.cpp - Conversion from Vector to mix of SCF and Std -----===// 24ead2cf7SAlex Zinenko // 34ead2cf7SAlex Zinenko // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 44ead2cf7SAlex Zinenko // See https://llvm.org/LICENSE.txt for license information. 54ead2cf7SAlex Zinenko // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 64ead2cf7SAlex Zinenko // 74ead2cf7SAlex Zinenko //===----------------------------------------------------------------------===// 84ead2cf7SAlex Zinenko // 94ead2cf7SAlex Zinenko // This file implements target-dependent lowering of vector transfer operations. 104ead2cf7SAlex Zinenko // 114ead2cf7SAlex Zinenko //===----------------------------------------------------------------------===// 124ead2cf7SAlex Zinenko 134ead2cf7SAlex Zinenko #include <type_traits> 144ead2cf7SAlex Zinenko 154ead2cf7SAlex Zinenko #include "mlir/Conversion/VectorToSCF/VectorToSCF.h" 164ead2cf7SAlex Zinenko #include "mlir/Dialect/Affine/EDSC/Intrinsics.h" 174ead2cf7SAlex Zinenko #include "mlir/Dialect/SCF/EDSC/Builders.h" 184ead2cf7SAlex Zinenko #include "mlir/Dialect/SCF/EDSC/Intrinsics.h" 194ead2cf7SAlex Zinenko #include "mlir/Dialect/StandardOps/EDSC/Intrinsics.h" 204ead2cf7SAlex Zinenko #include "mlir/Dialect/Vector/EDSC/Intrinsics.h" 214ead2cf7SAlex Zinenko #include "mlir/Dialect/Vector/VectorOps.h" 227c3c5b11SNicolas Vasilache #include "mlir/Dialect/Vector/VectorUtils.h" 234ead2cf7SAlex Zinenko #include "mlir/IR/AffineExpr.h" 244ead2cf7SAlex Zinenko #include "mlir/IR/AffineMap.h" 254ead2cf7SAlex Zinenko #include "mlir/IR/Attributes.h" 264ead2cf7SAlex Zinenko #include "mlir/IR/Builders.h" 274ead2cf7SAlex Zinenko #include "mlir/IR/Location.h" 284ead2cf7SAlex Zinenko #include "mlir/IR/Matchers.h" 294ead2cf7SAlex Zinenko #include "mlir/IR/OperationSupport.h" 304ead2cf7SAlex Zinenko #include "mlir/IR/PatternMatch.h" 314ead2cf7SAlex Zinenko #include "mlir/IR/Types.h" 324ead2cf7SAlex Zinenko 334ead2cf7SAlex Zinenko using namespace mlir; 344ead2cf7SAlex Zinenko using namespace mlir::edsc; 354ead2cf7SAlex Zinenko using namespace mlir::edsc::intrinsics; 364ead2cf7SAlex Zinenko using vector::TransferReadOp; 374ead2cf7SAlex Zinenko using vector::TransferWriteOp; 384ead2cf7SAlex Zinenko 39350dadaaSBenjamin Kramer namespace { 404ead2cf7SAlex Zinenko /// Helper class captures the common information needed to lower N>1-D vector 414ead2cf7SAlex Zinenko /// transfer operations (read and write). 424ead2cf7SAlex Zinenko /// On construction, this class opens an edsc::ScopedContext for simpler IR 434ead2cf7SAlex Zinenko /// manipulation. 444ead2cf7SAlex Zinenko /// In pseudo-IR, for an n-D vector_transfer_read such as: 454ead2cf7SAlex Zinenko /// 464ead2cf7SAlex Zinenko /// ``` 474ead2cf7SAlex Zinenko /// vector_transfer_read(%m, %offsets, identity_map, %fill) : 484ead2cf7SAlex Zinenko /// memref<(leading_dims) x (major_dims) x (minor_dims) x type>, 494ead2cf7SAlex Zinenko /// vector<(major_dims) x (minor_dims) x type> 504ead2cf7SAlex Zinenko /// ``` 514ead2cf7SAlex Zinenko /// 524ead2cf7SAlex Zinenko /// where rank(minor_dims) is the lower-level vector rank (e.g. 1 for LLVM or 534ead2cf7SAlex Zinenko /// higher). 544ead2cf7SAlex Zinenko /// 554ead2cf7SAlex Zinenko /// This is the entry point to emitting pseudo-IR resembling: 564ead2cf7SAlex Zinenko /// 574ead2cf7SAlex Zinenko /// ``` 584ead2cf7SAlex Zinenko /// %tmp = alloc(): memref<(major_dims) x vector<minor_dim x type>> 594ead2cf7SAlex Zinenko /// for (%ivs_major, {0}, {vector_shape}, {1}) { // (N-1)-D loop nest 604ead2cf7SAlex Zinenko /// if (any_of(%ivs_major + %offsets, <, major_dims)) { 614ead2cf7SAlex Zinenko /// %v = vector_transfer_read( 624ead2cf7SAlex Zinenko /// {%offsets_leading, %ivs_major + %offsets_major, %offsets_minor}, 634ead2cf7SAlex Zinenko /// %ivs_minor): 644ead2cf7SAlex Zinenko /// memref<(leading_dims) x (major_dims) x (minor_dims) x type>, 654ead2cf7SAlex Zinenko /// vector<(minor_dims) x type>; 664ead2cf7SAlex Zinenko /// store(%v, %tmp); 674ead2cf7SAlex Zinenko /// } else { 684ead2cf7SAlex Zinenko /// %v = splat(vector<(minor_dims) x type>, %fill) 694ead2cf7SAlex Zinenko /// store(%v, %tmp, %ivs_major); 704ead2cf7SAlex Zinenko /// } 714ead2cf7SAlex Zinenko /// } 724ead2cf7SAlex Zinenko /// %res = load(%tmp, %0): memref<(major_dims) x vector<minor_dim x type>>): 734ead2cf7SAlex Zinenko // vector<(major_dims) x (minor_dims) x type> 744ead2cf7SAlex Zinenko /// ``` 754ead2cf7SAlex Zinenko /// 764ead2cf7SAlex Zinenko template <typename ConcreteOp> 774ead2cf7SAlex Zinenko class NDTransferOpHelper { 784ead2cf7SAlex Zinenko public: 797c3c5b11SNicolas Vasilache NDTransferOpHelper(PatternRewriter &rewriter, ConcreteOp xferOp, 807c3c5b11SNicolas Vasilache const VectorTransferToSCFOptions &options) 817c3c5b11SNicolas Vasilache : rewriter(rewriter), options(options), loc(xferOp.getLoc()), 824ead2cf7SAlex Zinenko scope(std::make_unique<ScopedContext>(rewriter, loc)), xferOp(xferOp), 834ead2cf7SAlex Zinenko op(xferOp.getOperation()) { 844ead2cf7SAlex Zinenko vectorType = xferOp.getVectorType(); 854ead2cf7SAlex Zinenko // TODO(ntv, ajcbik): when we go to k > 1-D vectors adapt minorRank. 864ead2cf7SAlex Zinenko minorRank = 1; 874ead2cf7SAlex Zinenko majorRank = vectorType.getRank() - minorRank; 884ead2cf7SAlex Zinenko leadingRank = xferOp.getMemRefType().getRank() - (majorRank + minorRank); 894ead2cf7SAlex Zinenko majorVectorType = 904ead2cf7SAlex Zinenko VectorType::get(vectorType.getShape().take_front(majorRank), 914ead2cf7SAlex Zinenko vectorType.getElementType()); 924ead2cf7SAlex Zinenko minorVectorType = 934ead2cf7SAlex Zinenko VectorType::get(vectorType.getShape().take_back(minorRank), 944ead2cf7SAlex Zinenko vectorType.getElementType()); 954ead2cf7SAlex Zinenko /// Memref of minor vector type is used for individual transfers. 964ead2cf7SAlex Zinenko memRefMinorVectorType = 974ead2cf7SAlex Zinenko MemRefType::get(majorVectorType.getShape(), minorVectorType, {}, 984ead2cf7SAlex Zinenko xferOp.getMemRefType().getMemorySpace()); 994ead2cf7SAlex Zinenko } 1004ead2cf7SAlex Zinenko 1014ead2cf7SAlex Zinenko LogicalResult doReplace(); 1024ead2cf7SAlex Zinenko 1034ead2cf7SAlex Zinenko private: 1044ead2cf7SAlex Zinenko /// Creates the loop nest on the "major" dimensions and calls the 1054ead2cf7SAlex Zinenko /// `loopBodyBuilder` lambda in the context of the loop nest. 1064ead2cf7SAlex Zinenko template <typename Lambda> 1074ead2cf7SAlex Zinenko void emitLoops(Lambda loopBodyBuilder); 1084ead2cf7SAlex Zinenko 1094ead2cf7SAlex Zinenko /// Operate within the body of `emitLoops` to: 1107c3c5b11SNicolas Vasilache /// 1. Compute the indexings `majorIvs + majorOffsets` and save them in 1117c3c5b11SNicolas Vasilache /// `majorIvsPlusOffsets`. 1127c3c5b11SNicolas Vasilache /// 2. Return a boolean that determines whether the first `majorIvs.rank()` 1134ead2cf7SAlex Zinenko /// dimensions `majorIvs + majorOffsets` are all within `memrefBounds`. 1147c3c5b11SNicolas Vasilache Value emitInBoundsCondition(ValueRange majorIvs, ValueRange majorOffsets, 1154ead2cf7SAlex Zinenko MemRefBoundsCapture &memrefBounds, 1167c3c5b11SNicolas Vasilache SmallVectorImpl<Value> &majorIvsPlusOffsets); 1174ead2cf7SAlex Zinenko 1184ead2cf7SAlex Zinenko /// Common state to lower vector transfer ops. 1194ead2cf7SAlex Zinenko PatternRewriter &rewriter; 1207c3c5b11SNicolas Vasilache const VectorTransferToSCFOptions &options; 1214ead2cf7SAlex Zinenko Location loc; 1224ead2cf7SAlex Zinenko std::unique_ptr<ScopedContext> scope; 1234ead2cf7SAlex Zinenko ConcreteOp xferOp; 1244ead2cf7SAlex Zinenko Operation *op; 1254ead2cf7SAlex Zinenko // A vector transfer copies data between: 1264ead2cf7SAlex Zinenko // - memref<(leading_dims) x (major_dims) x (minor_dims) x type> 1274ead2cf7SAlex Zinenko // - vector<(major_dims) x (minor_dims) x type> 1284ead2cf7SAlex Zinenko unsigned minorRank; // for now always 1 1294ead2cf7SAlex Zinenko unsigned majorRank; // vector rank - minorRank 1304ead2cf7SAlex Zinenko unsigned leadingRank; // memref rank - vector rank 1314ead2cf7SAlex Zinenko VectorType vectorType; // vector<(major_dims) x (minor_dims) x type> 1324ead2cf7SAlex Zinenko VectorType majorVectorType; // vector<(major_dims) x type> 1334ead2cf7SAlex Zinenko VectorType minorVectorType; // vector<(minor_dims) x type> 1344ead2cf7SAlex Zinenko MemRefType memRefMinorVectorType; // memref<vector<(minor_dims) x type>> 1354ead2cf7SAlex Zinenko }; 1364ead2cf7SAlex Zinenko 1374ead2cf7SAlex Zinenko template <typename ConcreteOp> 1384ead2cf7SAlex Zinenko template <typename Lambda> 1394ead2cf7SAlex Zinenko void NDTransferOpHelper<ConcreteOp>::emitLoops(Lambda loopBodyBuilder) { 1404ead2cf7SAlex Zinenko /// Loop nest operates on the major dimensions 1414ead2cf7SAlex Zinenko MemRefBoundsCapture memrefBoundsCapture(xferOp.memref()); 1427c3c5b11SNicolas Vasilache 1437c3c5b11SNicolas Vasilache if (options.unroll) { 1447c3c5b11SNicolas Vasilache auto shape = majorVectorType.getShape(); 1457c3c5b11SNicolas Vasilache auto strides = computeStrides(shape); 1467c3c5b11SNicolas Vasilache unsigned numUnrolledInstances = computeMaxLinearIndex(shape); 1477c3c5b11SNicolas Vasilache ValueRange indices(xferOp.indices()); 1487c3c5b11SNicolas Vasilache for (unsigned idx = 0; idx < numUnrolledInstances; ++idx) { 1497c3c5b11SNicolas Vasilache SmallVector<int64_t, 4> offsets = delinearize(strides, idx); 1507c3c5b11SNicolas Vasilache SmallVector<Value, 4> offsetValues = 1517c3c5b11SNicolas Vasilache llvm::to_vector<4>(llvm::map_range(offsets, [](int64_t off) -> Value { 1527c3c5b11SNicolas Vasilache return std_constant_index(off); 1537c3c5b11SNicolas Vasilache })); 1547c3c5b11SNicolas Vasilache loopBodyBuilder(offsetValues, indices.take_front(leadingRank), 1557c3c5b11SNicolas Vasilache indices.drop_front(leadingRank).take_front(majorRank), 1567c3c5b11SNicolas Vasilache indices.take_back(minorRank), memrefBoundsCapture); 1577c3c5b11SNicolas Vasilache } 1587c3c5b11SNicolas Vasilache } else { 1594ead2cf7SAlex Zinenko VectorBoundsCapture vectorBoundsCapture(majorVectorType); 1604ead2cf7SAlex Zinenko auto majorLbs = vectorBoundsCapture.getLbs(); 1614ead2cf7SAlex Zinenko auto majorUbs = vectorBoundsCapture.getUbs(); 1624ead2cf7SAlex Zinenko auto majorSteps = vectorBoundsCapture.getSteps(); 1634ead2cf7SAlex Zinenko SmallVector<Value, 8> majorIvs(vectorBoundsCapture.rank()); 1644ead2cf7SAlex Zinenko AffineLoopNestBuilder(majorIvs, majorLbs, majorUbs, majorSteps)([&] { 1654ead2cf7SAlex Zinenko ValueRange indices(xferOp.indices()); 1664ead2cf7SAlex Zinenko loopBodyBuilder(majorIvs, indices.take_front(leadingRank), 1674ead2cf7SAlex Zinenko indices.drop_front(leadingRank).take_front(majorRank), 1684ead2cf7SAlex Zinenko indices.take_back(minorRank), memrefBoundsCapture); 1694ead2cf7SAlex Zinenko }); 1704ead2cf7SAlex Zinenko } 1717c3c5b11SNicolas Vasilache } 1724ead2cf7SAlex Zinenko 1734ead2cf7SAlex Zinenko template <typename ConcreteOp> 1747c3c5b11SNicolas Vasilache Value NDTransferOpHelper<ConcreteOp>::emitInBoundsCondition( 1754ead2cf7SAlex Zinenko ValueRange majorIvs, ValueRange majorOffsets, 1767c3c5b11SNicolas Vasilache MemRefBoundsCapture &memrefBounds, 1777c3c5b11SNicolas Vasilache SmallVectorImpl<Value> &majorIvsPlusOffsets) { 1787c3c5b11SNicolas Vasilache Value inBoundsCondition; 1794ead2cf7SAlex Zinenko majorIvsPlusOffsets.reserve(majorIvs.size()); 1801870e787SNicolas Vasilache unsigned idx = 0; 1814ead2cf7SAlex Zinenko for (auto it : llvm::zip(majorIvs, majorOffsets, memrefBounds.getUbs())) { 1824ead2cf7SAlex Zinenko Value iv = std::get<0>(it), off = std::get<1>(it), ub = std::get<2>(it); 1834ead2cf7SAlex Zinenko using namespace mlir::edsc::op; 1844ead2cf7SAlex Zinenko majorIvsPlusOffsets.push_back(iv + off); 1851870e787SNicolas Vasilache if (xferOp.isMaskedDim(leadingRank + idx)) { 1867c3c5b11SNicolas Vasilache Value inBounds = majorIvsPlusOffsets.back() < ub; 1877c3c5b11SNicolas Vasilache inBoundsCondition = 1887c3c5b11SNicolas Vasilache (inBoundsCondition) ? (inBoundsCondition && inBounds) : inBounds; 1891870e787SNicolas Vasilache } 1901870e787SNicolas Vasilache ++idx; 1914ead2cf7SAlex Zinenko } 1927c3c5b11SNicolas Vasilache return inBoundsCondition; 1934ead2cf7SAlex Zinenko } 1944ead2cf7SAlex Zinenko 1954ead2cf7SAlex Zinenko template <> 1964ead2cf7SAlex Zinenko LogicalResult NDTransferOpHelper<TransferReadOp>::doReplace() { 1977c3c5b11SNicolas Vasilache Value alloc, result; 1987c3c5b11SNicolas Vasilache if (options.unroll) 1997c3c5b11SNicolas Vasilache result = std_splat(vectorType, xferOp.padding()); 2007c3c5b11SNicolas Vasilache else 2017c3c5b11SNicolas Vasilache alloc = std_alloc(memRefMinorVectorType); 2024ead2cf7SAlex Zinenko 2034ead2cf7SAlex Zinenko emitLoops([&](ValueRange majorIvs, ValueRange leadingOffsets, 2044ead2cf7SAlex Zinenko ValueRange majorOffsets, ValueRange minorOffsets, 2054ead2cf7SAlex Zinenko MemRefBoundsCapture &memrefBounds) { 2067c3c5b11SNicolas Vasilache /// Lambda to load 1-D vector in the current loop ivs + offset context. 2077c3c5b11SNicolas Vasilache auto load1DVector = [&](ValueRange majorIvsPlusOffsets) -> Value { 2084ead2cf7SAlex Zinenko SmallVector<Value, 8> indexing; 2094ead2cf7SAlex Zinenko indexing.reserve(leadingRank + majorRank + minorRank); 2104ead2cf7SAlex Zinenko indexing.append(leadingOffsets.begin(), leadingOffsets.end()); 2114ead2cf7SAlex Zinenko indexing.append(majorIvsPlusOffsets.begin(), majorIvsPlusOffsets.end()); 2124ead2cf7SAlex Zinenko indexing.append(minorOffsets.begin(), minorOffsets.end()); 21336cdc17fSNicolas Vasilache Value memref = xferOp.memref(); 21436cdc17fSNicolas Vasilache auto map = TransferReadOp::getTransferMinorIdentityMap( 21536cdc17fSNicolas Vasilache xferOp.getMemRefType(), minorVectorType); 2161870e787SNicolas Vasilache ArrayAttr masked; 2171870e787SNicolas Vasilache if (xferOp.isMaskedDim(xferOp.getVectorType().getRank() - 1)) { 2181870e787SNicolas Vasilache OpBuilder &b = ScopedContext::getBuilderRef(); 2191870e787SNicolas Vasilache masked = b.getBoolArrayAttr({true}); 2201870e787SNicolas Vasilache } 2217c3c5b11SNicolas Vasilache return vector_transfer_read(minorVectorType, memref, indexing, 2227c3c5b11SNicolas Vasilache AffineMapAttr::get(map), xferOp.padding(), 2237c3c5b11SNicolas Vasilache masked); 2244ead2cf7SAlex Zinenko }; 2257c3c5b11SNicolas Vasilache 2267c3c5b11SNicolas Vasilache // 1. Compute the inBoundsCondition in the current loops ivs + offset 2277c3c5b11SNicolas Vasilache // context. 2287c3c5b11SNicolas Vasilache SmallVector<Value, 4> majorIvsPlusOffsets; 2297c3c5b11SNicolas Vasilache Value inBoundsCondition = emitInBoundsCondition( 2307c3c5b11SNicolas Vasilache majorIvs, majorOffsets, memrefBounds, majorIvsPlusOffsets); 2317c3c5b11SNicolas Vasilache 2327c3c5b11SNicolas Vasilache if (inBoundsCondition) { 2337c3c5b11SNicolas Vasilache // 2. If the condition is not null, we need an IfOp, which may yield 2347c3c5b11SNicolas Vasilache // if `options.unroll` is true. 2357c3c5b11SNicolas Vasilache SmallVector<Type, 1> resultType; 2367c3c5b11SNicolas Vasilache if (options.unroll) 2377c3c5b11SNicolas Vasilache resultType.push_back(vectorType); 2387c3c5b11SNicolas Vasilache auto ifOp = ScopedContext::getBuilderRef().create<scf::IfOp>( 2397c3c5b11SNicolas Vasilache ScopedContext::getLocation(), resultType, inBoundsCondition, 2407c3c5b11SNicolas Vasilache /*withElseRegion=*/true); 2417c3c5b11SNicolas Vasilache 2427c3c5b11SNicolas Vasilache // 3.a. If in-bounds, progressively lower to a 1-D transfer read. 2437c3c5b11SNicolas Vasilache BlockBuilder(&ifOp.thenRegion().front(), Append())([&] { 2447c3c5b11SNicolas Vasilache Value vector = load1DVector(majorIvsPlusOffsets); 2457c3c5b11SNicolas Vasilache // 3.a.i. If `options.unroll` is true, insert the 1-D vector in the 2467c3c5b11SNicolas Vasilache // aggregate. We must yield and merge with the `else` branch. 2477c3c5b11SNicolas Vasilache if (options.unroll) { 2487c3c5b11SNicolas Vasilache vector = vector_insert(vector, result, majorIvs); 2497c3c5b11SNicolas Vasilache (loop_yield(vector)); 2507c3c5b11SNicolas Vasilache return; 2517c3c5b11SNicolas Vasilache } 2527c3c5b11SNicolas Vasilache // 3.a.ii. Otherwise, just go through the temporary `alloc`. 2534ead2cf7SAlex Zinenko std_store(vector, alloc, majorIvs); 2544ead2cf7SAlex Zinenko }); 2554ead2cf7SAlex Zinenko 2567c3c5b11SNicolas Vasilache // 3.b. If not in-bounds, splat a 1-D vector. 2577c3c5b11SNicolas Vasilache BlockBuilder(&ifOp.elseRegion().front(), Append())([&] { 2587c3c5b11SNicolas Vasilache Value vector = std_splat(minorVectorType, xferOp.padding()); 2597c3c5b11SNicolas Vasilache // 3.a.i. If `options.unroll` is true, insert the 1-D vector in the 2607c3c5b11SNicolas Vasilache // aggregate. We must yield and merge with the `then` branch. 2617c3c5b11SNicolas Vasilache if (options.unroll) { 2627c3c5b11SNicolas Vasilache vector = vector_insert(vector, result, majorIvs); 2637c3c5b11SNicolas Vasilache (loop_yield(vector)); 2647c3c5b11SNicolas Vasilache return; 2657c3c5b11SNicolas Vasilache } 2667c3c5b11SNicolas Vasilache // 3.b.ii. Otherwise, just go through the temporary `alloc`. 2677c3c5b11SNicolas Vasilache std_store(vector, alloc, majorIvs); 2687c3c5b11SNicolas Vasilache }); 2697c3c5b11SNicolas Vasilache if (!resultType.empty()) 2707c3c5b11SNicolas Vasilache result = *ifOp.results().begin(); 2717c3c5b11SNicolas Vasilache } else { 2727c3c5b11SNicolas Vasilache // 4. Guaranteed in-bounds, progressively lower to a 1-D transfer read. 2737c3c5b11SNicolas Vasilache Value loaded1D = load1DVector(majorIvsPlusOffsets); 2747c3c5b11SNicolas Vasilache // 5.a. If `options.unroll` is true, insert the 1-D vector in the 2757c3c5b11SNicolas Vasilache // aggregate. 2767c3c5b11SNicolas Vasilache if (options.unroll) 2777c3c5b11SNicolas Vasilache result = vector_insert(loaded1D, result, majorIvs); 2787c3c5b11SNicolas Vasilache // 5.b. Otherwise, just go through the temporary `alloc`. 2797c3c5b11SNicolas Vasilache else 2807c3c5b11SNicolas Vasilache std_store(loaded1D, alloc, majorIvs); 2817c3c5b11SNicolas Vasilache } 2827c3c5b11SNicolas Vasilache }); 2837c3c5b11SNicolas Vasilache 284*a9b5edc5SBenjamin Kramer assert((!options.unroll ^ (bool)result) && 285*a9b5edc5SBenjamin Kramer "Expected resulting Value iff unroll"); 2867c3c5b11SNicolas Vasilache if (!result) 2877c3c5b11SNicolas Vasilache result = std_load(vector_type_cast(MemRefType::get({}, vectorType), alloc)); 2887c3c5b11SNicolas Vasilache rewriter.replaceOp(op, result); 2894ead2cf7SAlex Zinenko 2904ead2cf7SAlex Zinenko return success(); 2914ead2cf7SAlex Zinenko } 2924ead2cf7SAlex Zinenko 2934ead2cf7SAlex Zinenko template <> 2944ead2cf7SAlex Zinenko LogicalResult NDTransferOpHelper<TransferWriteOp>::doReplace() { 2957c3c5b11SNicolas Vasilache Value alloc; 2967c3c5b11SNicolas Vasilache if (!options.unroll) { 2977c3c5b11SNicolas Vasilache alloc = std_alloc(memRefMinorVectorType); 2984ead2cf7SAlex Zinenko std_store(xferOp.vector(), 2994ead2cf7SAlex Zinenko vector_type_cast(MemRefType::get({}, vectorType), alloc)); 3007c3c5b11SNicolas Vasilache } 3014ead2cf7SAlex Zinenko 3024ead2cf7SAlex Zinenko emitLoops([&](ValueRange majorIvs, ValueRange leadingOffsets, 3034ead2cf7SAlex Zinenko ValueRange majorOffsets, ValueRange minorOffsets, 3044ead2cf7SAlex Zinenko MemRefBoundsCapture &memrefBounds) { 3057c3c5b11SNicolas Vasilache // Lower to 1-D vector_transfer_write and let recursion handle it. 3067c3c5b11SNicolas Vasilache auto emitTransferWrite = [&](ValueRange majorIvsPlusOffsets) { 3074ead2cf7SAlex Zinenko SmallVector<Value, 8> indexing; 3084ead2cf7SAlex Zinenko indexing.reserve(leadingRank + majorRank + minorRank); 3094ead2cf7SAlex Zinenko indexing.append(leadingOffsets.begin(), leadingOffsets.end()); 3104ead2cf7SAlex Zinenko indexing.append(majorIvsPlusOffsets.begin(), majorIvsPlusOffsets.end()); 3114ead2cf7SAlex Zinenko indexing.append(minorOffsets.begin(), minorOffsets.end()); 3127c3c5b11SNicolas Vasilache Value result; 3137c3c5b11SNicolas Vasilache // If `options.unroll` is true, extract the 1-D vector from the 3147c3c5b11SNicolas Vasilache // aggregate. 3157c3c5b11SNicolas Vasilache if (options.unroll) 3167c3c5b11SNicolas Vasilache result = vector_extract(xferOp.vector(), majorIvs); 3177c3c5b11SNicolas Vasilache else 3187c3c5b11SNicolas Vasilache result = std_load(alloc, majorIvs); 31936cdc17fSNicolas Vasilache auto map = TransferWriteOp::getTransferMinorIdentityMap( 32036cdc17fSNicolas Vasilache xferOp.getMemRefType(), minorVectorType); 3211870e787SNicolas Vasilache ArrayAttr masked; 3221870e787SNicolas Vasilache if (xferOp.isMaskedDim(xferOp.getVectorType().getRank() - 1)) { 3231870e787SNicolas Vasilache OpBuilder &b = ScopedContext::getBuilderRef(); 3241870e787SNicolas Vasilache masked = b.getBoolArrayAttr({true}); 3251870e787SNicolas Vasilache } 3267c3c5b11SNicolas Vasilache vector_transfer_write(result, xferOp.memref(), indexing, 3271870e787SNicolas Vasilache AffineMapAttr::get(map), masked); 3284ead2cf7SAlex Zinenko }; 3297c3c5b11SNicolas Vasilache 3307c3c5b11SNicolas Vasilache // 1. Compute the inBoundsCondition in the current loops ivs + offset 3317c3c5b11SNicolas Vasilache // context. 3327c3c5b11SNicolas Vasilache SmallVector<Value, 4> majorIvsPlusOffsets; 3337c3c5b11SNicolas Vasilache Value inBoundsCondition = emitInBoundsCondition( 3347c3c5b11SNicolas Vasilache majorIvs, majorOffsets, memrefBounds, majorIvsPlusOffsets); 3357c3c5b11SNicolas Vasilache 3367c3c5b11SNicolas Vasilache if (inBoundsCondition) { 3377c3c5b11SNicolas Vasilache // 2.a. If the condition is not null, we need an IfOp, to write 3387c3c5b11SNicolas Vasilache // conditionally. Progressively lower to a 1-D transfer write. 3397c3c5b11SNicolas Vasilache auto ifOp = ScopedContext::getBuilderRef().create<scf::IfOp>( 3407c3c5b11SNicolas Vasilache ScopedContext::getLocation(), TypeRange{}, inBoundsCondition, 3417c3c5b11SNicolas Vasilache /*withElseRegion=*/false); 3427c3c5b11SNicolas Vasilache BlockBuilder(&ifOp.thenRegion().front(), 3437c3c5b11SNicolas Vasilache Append())([&] { emitTransferWrite(majorIvsPlusOffsets); }); 3447c3c5b11SNicolas Vasilache } else { 3457c3c5b11SNicolas Vasilache // 2.b. Guaranteed in-bounds. Progressively lower to a 1-D transfer write. 3467c3c5b11SNicolas Vasilache emitTransferWrite(majorIvsPlusOffsets); 3477c3c5b11SNicolas Vasilache } 3484ead2cf7SAlex Zinenko }); 3494ead2cf7SAlex Zinenko 3504ead2cf7SAlex Zinenko rewriter.eraseOp(op); 3514ead2cf7SAlex Zinenko 3524ead2cf7SAlex Zinenko return success(); 3534ead2cf7SAlex Zinenko } 3544ead2cf7SAlex Zinenko 355da95a0d8SNicolas Vasilache } // namespace 356da95a0d8SNicolas Vasilache 3574ead2cf7SAlex Zinenko /// Analyzes the `transfer` to find an access dimension along the fastest remote 3584ead2cf7SAlex Zinenko /// MemRef dimension. If such a dimension with coalescing properties is found, 3594ead2cf7SAlex Zinenko /// `pivs` and `vectorBoundsCapture` are swapped so that the invocation of 3604ead2cf7SAlex Zinenko /// LoopNestBuilder captures it in the innermost loop. 3614ead2cf7SAlex Zinenko template <typename TransferOpTy> 3624ead2cf7SAlex Zinenko static int computeCoalescedIndex(TransferOpTy transfer) { 3634ead2cf7SAlex Zinenko // rank of the remote memory access, coalescing behavior occurs on the 3644ead2cf7SAlex Zinenko // innermost memory dimension. 3654ead2cf7SAlex Zinenko auto remoteRank = transfer.getMemRefType().getRank(); 3664ead2cf7SAlex Zinenko // Iterate over the results expressions of the permutation map to determine 3674ead2cf7SAlex Zinenko // the loop order for creating pointwise copies between remote and local 3684ead2cf7SAlex Zinenko // memories. 3694ead2cf7SAlex Zinenko int coalescedIdx = -1; 3704ead2cf7SAlex Zinenko auto exprs = transfer.permutation_map().getResults(); 3714ead2cf7SAlex Zinenko for (auto en : llvm::enumerate(exprs)) { 3724ead2cf7SAlex Zinenko auto dim = en.value().template dyn_cast<AffineDimExpr>(); 3734ead2cf7SAlex Zinenko if (!dim) { 3744ead2cf7SAlex Zinenko continue; 3754ead2cf7SAlex Zinenko } 3764ead2cf7SAlex Zinenko auto memRefDim = dim.getPosition(); 3774ead2cf7SAlex Zinenko if (memRefDim == remoteRank - 1) { 3784ead2cf7SAlex Zinenko // memRefDim has coalescing properties, it should be swapped in the last 3794ead2cf7SAlex Zinenko // position. 3804ead2cf7SAlex Zinenko assert(coalescedIdx == -1 && "Unexpected > 1 coalesced indices"); 3814ead2cf7SAlex Zinenko coalescedIdx = en.index(); 3824ead2cf7SAlex Zinenko } 3834ead2cf7SAlex Zinenko } 3844ead2cf7SAlex Zinenko return coalescedIdx; 3854ead2cf7SAlex Zinenko } 3864ead2cf7SAlex Zinenko 3874ead2cf7SAlex Zinenko /// Emits remote memory accesses that are clipped to the boundaries of the 3884ead2cf7SAlex Zinenko /// MemRef. 3894ead2cf7SAlex Zinenko template <typename TransferOpTy> 3904ead2cf7SAlex Zinenko static SmallVector<Value, 8> 3914ead2cf7SAlex Zinenko clip(TransferOpTy transfer, MemRefBoundsCapture &bounds, ArrayRef<Value> ivs) { 3924ead2cf7SAlex Zinenko using namespace mlir::edsc; 3934ead2cf7SAlex Zinenko 3944ead2cf7SAlex Zinenko Value zero(std_constant_index(0)), one(std_constant_index(1)); 3954ead2cf7SAlex Zinenko SmallVector<Value, 8> memRefAccess(transfer.indices()); 3964ead2cf7SAlex Zinenko SmallVector<Value, 8> clippedScalarAccessExprs(memRefAccess.size()); 3974ead2cf7SAlex Zinenko // Indices accessing to remote memory are clipped and their expressions are 3984ead2cf7SAlex Zinenko // returned in clippedScalarAccessExprs. 3994ead2cf7SAlex Zinenko for (unsigned memRefDim = 0; memRefDim < clippedScalarAccessExprs.size(); 4004ead2cf7SAlex Zinenko ++memRefDim) { 4014ead2cf7SAlex Zinenko // Linear search on a small number of entries. 4024ead2cf7SAlex Zinenko int loopIndex = -1; 4034ead2cf7SAlex Zinenko auto exprs = transfer.permutation_map().getResults(); 4044ead2cf7SAlex Zinenko for (auto en : llvm::enumerate(exprs)) { 4054ead2cf7SAlex Zinenko auto expr = en.value(); 4064ead2cf7SAlex Zinenko auto dim = expr.template dyn_cast<AffineDimExpr>(); 4074ead2cf7SAlex Zinenko // Sanity check. 4084ead2cf7SAlex Zinenko assert( 4094ead2cf7SAlex Zinenko (dim || expr.template cast<AffineConstantExpr>().getValue() == 0) && 4104ead2cf7SAlex Zinenko "Expected dim or 0 in permutationMap"); 4114ead2cf7SAlex Zinenko if (dim && memRefDim == dim.getPosition()) { 4124ead2cf7SAlex Zinenko loopIndex = en.index(); 4134ead2cf7SAlex Zinenko break; 4144ead2cf7SAlex Zinenko } 4154ead2cf7SAlex Zinenko } 4164ead2cf7SAlex Zinenko 4174ead2cf7SAlex Zinenko // We cannot distinguish atm between unrolled dimensions that implement 4184ead2cf7SAlex Zinenko // the "always full" tile abstraction and need clipping from the other 4194ead2cf7SAlex Zinenko // ones. So we conservatively clip everything. 4204ead2cf7SAlex Zinenko using namespace edsc::op; 4214ead2cf7SAlex Zinenko auto N = bounds.ub(memRefDim); 4224ead2cf7SAlex Zinenko auto i = memRefAccess[memRefDim]; 4234ead2cf7SAlex Zinenko if (loopIndex < 0) { 4244ead2cf7SAlex Zinenko auto N_minus_1 = N - one; 4254ead2cf7SAlex Zinenko auto select_1 = std_select(i < N, i, N_minus_1); 4264ead2cf7SAlex Zinenko clippedScalarAccessExprs[memRefDim] = 4274ead2cf7SAlex Zinenko std_select(i < zero, zero, select_1); 4284ead2cf7SAlex Zinenko } else { 4294ead2cf7SAlex Zinenko auto ii = ivs[loopIndex]; 4304ead2cf7SAlex Zinenko auto i_plus_ii = i + ii; 4314ead2cf7SAlex Zinenko auto N_minus_1 = N - one; 4324ead2cf7SAlex Zinenko auto select_1 = std_select(i_plus_ii < N, i_plus_ii, N_minus_1); 4334ead2cf7SAlex Zinenko clippedScalarAccessExprs[memRefDim] = 4344ead2cf7SAlex Zinenko std_select(i_plus_ii < zero, zero, select_1); 4354ead2cf7SAlex Zinenko } 4364ead2cf7SAlex Zinenko } 4374ead2cf7SAlex Zinenko 4384ead2cf7SAlex Zinenko return clippedScalarAccessExprs; 4394ead2cf7SAlex Zinenko } 4404ead2cf7SAlex Zinenko 4413393cc4cSNicolas Vasilache namespace mlir { 4423393cc4cSNicolas Vasilache 4434ead2cf7SAlex Zinenko template <typename TransferOpTy> 4443393cc4cSNicolas Vasilache VectorTransferRewriter<TransferOpTy>::VectorTransferRewriter( 4457c3c5b11SNicolas Vasilache VectorTransferToSCFOptions options, MLIRContext *context) 4467c3c5b11SNicolas Vasilache : RewritePattern(TransferOpTy::getOperationName(), 1, context), 4477c3c5b11SNicolas Vasilache options(options) {} 4484ead2cf7SAlex Zinenko 4497c3c5b11SNicolas Vasilache /// Used for staging the transfer in a local buffer. 4507c3c5b11SNicolas Vasilache template <typename TransferOpTy> 4513393cc4cSNicolas Vasilache MemRefType VectorTransferRewriter<TransferOpTy>::tmpMemRefType( 4527c3c5b11SNicolas Vasilache TransferOpTy transfer) const { 4534ead2cf7SAlex Zinenko auto vectorType = transfer.getVectorType(); 4547c3c5b11SNicolas Vasilache return MemRefType::get(vectorType.getShape(), vectorType.getElementType(), {}, 4557c3c5b11SNicolas Vasilache 0); 4564ead2cf7SAlex Zinenko } 4574ead2cf7SAlex Zinenko 4584ead2cf7SAlex Zinenko /// Lowers TransferReadOp into a combination of: 4594ead2cf7SAlex Zinenko /// 1. local memory allocation; 4604ead2cf7SAlex Zinenko /// 2. perfect loop nest over: 4614ead2cf7SAlex Zinenko /// a. scalar load from local buffers (viewed as a scalar memref); 4624ead2cf7SAlex Zinenko /// a. scalar store to original memref (with clipping). 4634ead2cf7SAlex Zinenko /// 3. vector_load from local buffer (viewed as a memref<1 x vector>); 4644ead2cf7SAlex Zinenko /// 4. local memory deallocation. 4654ead2cf7SAlex Zinenko /// 4664ead2cf7SAlex Zinenko /// Lowers the data transfer part of a TransferReadOp while ensuring no 4674ead2cf7SAlex Zinenko /// out-of-bounds accesses are possible. Out-of-bounds behavior is handled by 4684ead2cf7SAlex Zinenko /// clipping. This means that a given value in memory can be read multiple 4694ead2cf7SAlex Zinenko /// times and concurrently. 4704ead2cf7SAlex Zinenko /// 4714ead2cf7SAlex Zinenko /// Important notes about clipping and "full-tiles only" abstraction: 4724ead2cf7SAlex Zinenko /// ================================================================= 4734ead2cf7SAlex Zinenko /// When using clipping for dealing with boundary conditions, the same edge 4744ead2cf7SAlex Zinenko /// value will appear multiple times (a.k.a edge padding). This is fine if the 4754ead2cf7SAlex Zinenko /// subsequent vector operations are all data-parallel but **is generally 4764ead2cf7SAlex Zinenko /// incorrect** in the presence of reductions or extract operations. 4774ead2cf7SAlex Zinenko /// 4784ead2cf7SAlex Zinenko /// More generally, clipping is a scalar abstraction that is expected to work 4794ead2cf7SAlex Zinenko /// fine as a baseline for CPUs and GPUs but not for vector_load and DMAs. 4804ead2cf7SAlex Zinenko /// To deal with real vector_load and DMAs, a "padded allocation + view" 4814ead2cf7SAlex Zinenko /// abstraction with the ability to read out-of-memref-bounds (but still within 4824ead2cf7SAlex Zinenko /// the allocated region) is necessary. 4834ead2cf7SAlex Zinenko /// 4844ead2cf7SAlex Zinenko /// Whether using scalar loops or vector_load/DMAs to perform the transfer, 4854ead2cf7SAlex Zinenko /// junk values will be materialized in the vectors and generally need to be 4864ead2cf7SAlex Zinenko /// filtered out and replaced by the "neutral element". This neutral element is 4874ead2cf7SAlex Zinenko /// op-dependent so, in the future, we expect to create a vector filter and 4884ead2cf7SAlex Zinenko /// apply it to a splatted constant vector with the proper neutral element at 4894ead2cf7SAlex Zinenko /// each ssa-use. This filtering is not necessary for pure data-parallel 4904ead2cf7SAlex Zinenko /// operations. 4914ead2cf7SAlex Zinenko /// 4924ead2cf7SAlex Zinenko /// In the case of vector_store/DMAs, Read-Modify-Write will be required, which 4934ead2cf7SAlex Zinenko /// also have concurrency implications. Note that by using clipped scalar stores 4944ead2cf7SAlex Zinenko /// in the presence of data-parallel only operations, we generate code that 4954ead2cf7SAlex Zinenko /// writes the same value multiple time on the edge locations. 4964ead2cf7SAlex Zinenko /// 4974ead2cf7SAlex Zinenko /// TODO(ntv): implement alternatives to clipping. 4984ead2cf7SAlex Zinenko /// TODO(ntv): support non-data-parallel operations. 4994ead2cf7SAlex Zinenko 5004ead2cf7SAlex Zinenko /// Performs the rewrite. 5014ead2cf7SAlex Zinenko template <> 5023393cc4cSNicolas Vasilache LogicalResult VectorTransferRewriter<TransferReadOp>::matchAndRewrite( 5034ead2cf7SAlex Zinenko Operation *op, PatternRewriter &rewriter) const { 5044ead2cf7SAlex Zinenko using namespace mlir::edsc::op; 5054ead2cf7SAlex Zinenko 5064ead2cf7SAlex Zinenko TransferReadOp transfer = cast<TransferReadOp>(op); 5074ead2cf7SAlex Zinenko if (AffineMap::isMinorIdentity(transfer.permutation_map())) { 5084ead2cf7SAlex Zinenko // If > 1D, emit a bunch of loops around 1-D vector transfers. 5094ead2cf7SAlex Zinenko if (transfer.getVectorType().getRank() > 1) 5107c3c5b11SNicolas Vasilache return NDTransferOpHelper<TransferReadOp>(rewriter, transfer, options) 5117c3c5b11SNicolas Vasilache .doReplace(); 5124ead2cf7SAlex Zinenko // If 1-D this is now handled by the target-specific lowering. 5134ead2cf7SAlex Zinenko if (transfer.getVectorType().getRank() == 1) 5144ead2cf7SAlex Zinenko return failure(); 5154ead2cf7SAlex Zinenko } 5164ead2cf7SAlex Zinenko 5174ead2cf7SAlex Zinenko // Conservative lowering to scalar load / stores. 5184ead2cf7SAlex Zinenko // 1. Setup all the captures. 5194ead2cf7SAlex Zinenko ScopedContext scope(rewriter, transfer.getLoc()); 5204ead2cf7SAlex Zinenko StdIndexedValue remote(transfer.memref()); 5214ead2cf7SAlex Zinenko MemRefBoundsCapture memRefBoundsCapture(transfer.memref()); 5224ead2cf7SAlex Zinenko VectorBoundsCapture vectorBoundsCapture(transfer.vector()); 5234ead2cf7SAlex Zinenko int coalescedIdx = computeCoalescedIndex(transfer); 5244ead2cf7SAlex Zinenko // Swap the vectorBoundsCapture which will reorder loop bounds. 5254ead2cf7SAlex Zinenko if (coalescedIdx >= 0) 5264ead2cf7SAlex Zinenko vectorBoundsCapture.swapRanges(vectorBoundsCapture.rank() - 1, 5274ead2cf7SAlex Zinenko coalescedIdx); 5284ead2cf7SAlex Zinenko 5294ead2cf7SAlex Zinenko auto lbs = vectorBoundsCapture.getLbs(); 5304ead2cf7SAlex Zinenko auto ubs = vectorBoundsCapture.getUbs(); 5314ead2cf7SAlex Zinenko SmallVector<Value, 8> steps; 5324ead2cf7SAlex Zinenko steps.reserve(vectorBoundsCapture.getSteps().size()); 5334ead2cf7SAlex Zinenko for (auto step : vectorBoundsCapture.getSteps()) 5344ead2cf7SAlex Zinenko steps.push_back(std_constant_index(step)); 5354ead2cf7SAlex Zinenko 5364ead2cf7SAlex Zinenko // 2. Emit alloc-copy-load-dealloc. 5374ead2cf7SAlex Zinenko Value tmp = std_alloc(tmpMemRefType(transfer)); 5384ead2cf7SAlex Zinenko StdIndexedValue local(tmp); 5394ead2cf7SAlex Zinenko Value vec = vector_type_cast(tmp); 540d1560f39SAlex Zinenko loopNestBuilder(lbs, ubs, steps, [&](ValueRange loopIvs) { 541d1560f39SAlex Zinenko auto ivs = llvm::to_vector<8>(loopIvs); 5424ead2cf7SAlex Zinenko // Swap the ivs which will reorder memory accesses. 5434ead2cf7SAlex Zinenko if (coalescedIdx >= 0) 5444ead2cf7SAlex Zinenko std::swap(ivs.back(), ivs[coalescedIdx]); 5454ead2cf7SAlex Zinenko // Computes clippedScalarAccessExprs in the loop nest scope (ivs exist). 5464ead2cf7SAlex Zinenko local(ivs) = remote(clip(transfer, memRefBoundsCapture, ivs)); 5474ead2cf7SAlex Zinenko }); 5484ead2cf7SAlex Zinenko Value vectorValue = std_load(vec); 5494ead2cf7SAlex Zinenko (std_dealloc(tmp)); // vexing parse 5504ead2cf7SAlex Zinenko 5514ead2cf7SAlex Zinenko // 3. Propagate. 5524ead2cf7SAlex Zinenko rewriter.replaceOp(op, vectorValue); 5534ead2cf7SAlex Zinenko return success(); 5544ead2cf7SAlex Zinenko } 5554ead2cf7SAlex Zinenko 5564ead2cf7SAlex Zinenko /// Lowers TransferWriteOp into a combination of: 5574ead2cf7SAlex Zinenko /// 1. local memory allocation; 5584ead2cf7SAlex Zinenko /// 2. vector_store to local buffer (viewed as a memref<1 x vector>); 5594ead2cf7SAlex Zinenko /// 3. perfect loop nest over: 5604ead2cf7SAlex Zinenko /// a. scalar load from local buffers (viewed as a scalar memref); 5614ead2cf7SAlex Zinenko /// a. scalar store to original memref (with clipping). 5624ead2cf7SAlex Zinenko /// 4. local memory deallocation. 5634ead2cf7SAlex Zinenko /// 5644ead2cf7SAlex Zinenko /// More specifically, lowers the data transfer part while ensuring no 5654ead2cf7SAlex Zinenko /// out-of-bounds accesses are possible. Out-of-bounds behavior is handled by 5664ead2cf7SAlex Zinenko /// clipping. This means that a given value in memory can be written to multiple 5674ead2cf7SAlex Zinenko /// times and concurrently. 5684ead2cf7SAlex Zinenko /// 5694ead2cf7SAlex Zinenko /// See `Important notes about clipping and full-tiles only abstraction` in the 5704ead2cf7SAlex Zinenko /// description of `readClipped` above. 5714ead2cf7SAlex Zinenko /// 5724ead2cf7SAlex Zinenko /// TODO(ntv): implement alternatives to clipping. 5734ead2cf7SAlex Zinenko /// TODO(ntv): support non-data-parallel operations. 5744ead2cf7SAlex Zinenko template <> 5753393cc4cSNicolas Vasilache LogicalResult VectorTransferRewriter<TransferWriteOp>::matchAndRewrite( 5764ead2cf7SAlex Zinenko Operation *op, PatternRewriter &rewriter) const { 5774ead2cf7SAlex Zinenko using namespace edsc::op; 5784ead2cf7SAlex Zinenko 5794ead2cf7SAlex Zinenko TransferWriteOp transfer = cast<TransferWriteOp>(op); 5804ead2cf7SAlex Zinenko if (AffineMap::isMinorIdentity(transfer.permutation_map())) { 5814ead2cf7SAlex Zinenko // If > 1D, emit a bunch of loops around 1-D vector transfers. 5824ead2cf7SAlex Zinenko if (transfer.getVectorType().getRank() > 1) 5837c3c5b11SNicolas Vasilache return NDTransferOpHelper<TransferWriteOp>(rewriter, transfer, options) 5844ead2cf7SAlex Zinenko .doReplace(); 5854ead2cf7SAlex Zinenko // If 1-D this is now handled by the target-specific lowering. 5864ead2cf7SAlex Zinenko if (transfer.getVectorType().getRank() == 1) 5874ead2cf7SAlex Zinenko return failure(); 5884ead2cf7SAlex Zinenko } 5894ead2cf7SAlex Zinenko 5904ead2cf7SAlex Zinenko // 1. Setup all the captures. 5914ead2cf7SAlex Zinenko ScopedContext scope(rewriter, transfer.getLoc()); 5924ead2cf7SAlex Zinenko StdIndexedValue remote(transfer.memref()); 5934ead2cf7SAlex Zinenko MemRefBoundsCapture memRefBoundsCapture(transfer.memref()); 5944ead2cf7SAlex Zinenko Value vectorValue(transfer.vector()); 5954ead2cf7SAlex Zinenko VectorBoundsCapture vectorBoundsCapture(transfer.vector()); 5964ead2cf7SAlex Zinenko int coalescedIdx = computeCoalescedIndex(transfer); 5974ead2cf7SAlex Zinenko // Swap the vectorBoundsCapture which will reorder loop bounds. 5984ead2cf7SAlex Zinenko if (coalescedIdx >= 0) 5994ead2cf7SAlex Zinenko vectorBoundsCapture.swapRanges(vectorBoundsCapture.rank() - 1, 6004ead2cf7SAlex Zinenko coalescedIdx); 6014ead2cf7SAlex Zinenko 6024ead2cf7SAlex Zinenko auto lbs = vectorBoundsCapture.getLbs(); 6034ead2cf7SAlex Zinenko auto ubs = vectorBoundsCapture.getUbs(); 6044ead2cf7SAlex Zinenko SmallVector<Value, 8> steps; 6054ead2cf7SAlex Zinenko steps.reserve(vectorBoundsCapture.getSteps().size()); 6064ead2cf7SAlex Zinenko for (auto step : vectorBoundsCapture.getSteps()) 6074ead2cf7SAlex Zinenko steps.push_back(std_constant_index(step)); 6084ead2cf7SAlex Zinenko 6094ead2cf7SAlex Zinenko // 2. Emit alloc-store-copy-dealloc. 6104ead2cf7SAlex Zinenko Value tmp = std_alloc(tmpMemRefType(transfer)); 6114ead2cf7SAlex Zinenko StdIndexedValue local(tmp); 6124ead2cf7SAlex Zinenko Value vec = vector_type_cast(tmp); 6134ead2cf7SAlex Zinenko std_store(vectorValue, vec); 614d1560f39SAlex Zinenko loopNestBuilder(lbs, ubs, steps, [&](ValueRange loopIvs) { 615d1560f39SAlex Zinenko auto ivs = llvm::to_vector<8>(loopIvs); 6164ead2cf7SAlex Zinenko // Swap the ivs which will reorder memory accesses. 6174ead2cf7SAlex Zinenko if (coalescedIdx >= 0) 6184ead2cf7SAlex Zinenko std::swap(ivs.back(), ivs[coalescedIdx]); 6194ead2cf7SAlex Zinenko // Computes clippedScalarAccessExprs in the loop nest scope (ivs exist). 6204ead2cf7SAlex Zinenko remote(clip(transfer, memRefBoundsCapture, ivs)) = local(ivs); 6214ead2cf7SAlex Zinenko }); 6224ead2cf7SAlex Zinenko (std_dealloc(tmp)); // vexing parse... 6234ead2cf7SAlex Zinenko 6244ead2cf7SAlex Zinenko rewriter.eraseOp(op); 6254ead2cf7SAlex Zinenko return success(); 6264ead2cf7SAlex Zinenko } 6274ead2cf7SAlex Zinenko 6283393cc4cSNicolas Vasilache void populateVectorToSCFConversionPatterns( 6297c3c5b11SNicolas Vasilache OwningRewritePatternList &patterns, MLIRContext *context, 6307c3c5b11SNicolas Vasilache const VectorTransferToSCFOptions &options) { 6314ead2cf7SAlex Zinenko patterns.insert<VectorTransferRewriter<vector::TransferReadOp>, 6327c3c5b11SNicolas Vasilache VectorTransferRewriter<vector::TransferWriteOp>>(options, 6337c3c5b11SNicolas Vasilache context); 6344ead2cf7SAlex Zinenko } 6353393cc4cSNicolas Vasilache 6363393cc4cSNicolas Vasilache } // namespace mlir 6373393cc4cSNicolas Vasilache 638