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" 165f9e0466SNicolas Vasilache 175f9e0466SNicolas Vasilache #include "../PassDetail.h" 184ead2cf7SAlex Zinenko #include "mlir/Dialect/Affine/EDSC/Intrinsics.h" 194ead2cf7SAlex Zinenko #include "mlir/Dialect/SCF/EDSC/Builders.h" 204ead2cf7SAlex Zinenko #include "mlir/Dialect/SCF/EDSC/Intrinsics.h" 214ead2cf7SAlex Zinenko #include "mlir/Dialect/StandardOps/EDSC/Intrinsics.h" 224ead2cf7SAlex Zinenko #include "mlir/Dialect/Vector/EDSC/Intrinsics.h" 234ead2cf7SAlex Zinenko #include "mlir/Dialect/Vector/VectorOps.h" 247c3c5b11SNicolas Vasilache #include "mlir/Dialect/Vector/VectorUtils.h" 254ead2cf7SAlex Zinenko #include "mlir/IR/AffineExpr.h" 264ead2cf7SAlex Zinenko #include "mlir/IR/AffineMap.h" 274ead2cf7SAlex Zinenko #include "mlir/IR/Attributes.h" 284ead2cf7SAlex Zinenko #include "mlir/IR/Builders.h" 294ead2cf7SAlex Zinenko #include "mlir/IR/Location.h" 304ead2cf7SAlex Zinenko #include "mlir/IR/Matchers.h" 314ead2cf7SAlex Zinenko #include "mlir/IR/OperationSupport.h" 324ead2cf7SAlex Zinenko #include "mlir/IR/PatternMatch.h" 334ead2cf7SAlex Zinenko #include "mlir/IR/Types.h" 345f9e0466SNicolas Vasilache #include "mlir/Pass/Pass.h" 355f9e0466SNicolas Vasilache #include "mlir/Transforms/Passes.h" 364ead2cf7SAlex Zinenko 374ead2cf7SAlex Zinenko using namespace mlir; 384ead2cf7SAlex Zinenko using namespace mlir::edsc; 394ead2cf7SAlex Zinenko using namespace mlir::edsc::intrinsics; 404ead2cf7SAlex Zinenko using vector::TransferReadOp; 414ead2cf7SAlex Zinenko using vector::TransferWriteOp; 424ead2cf7SAlex Zinenko 43350dadaaSBenjamin Kramer namespace { 444ead2cf7SAlex Zinenko /// Helper class captures the common information needed to lower N>1-D vector 454ead2cf7SAlex Zinenko /// transfer operations (read and write). 464ead2cf7SAlex Zinenko /// On construction, this class opens an edsc::ScopedContext for simpler IR 474ead2cf7SAlex Zinenko /// manipulation. 484ead2cf7SAlex Zinenko /// In pseudo-IR, for an n-D vector_transfer_read such as: 494ead2cf7SAlex Zinenko /// 504ead2cf7SAlex Zinenko /// ``` 514ead2cf7SAlex Zinenko /// vector_transfer_read(%m, %offsets, identity_map, %fill) : 524ead2cf7SAlex Zinenko /// memref<(leading_dims) x (major_dims) x (minor_dims) x type>, 534ead2cf7SAlex Zinenko /// vector<(major_dims) x (minor_dims) x type> 544ead2cf7SAlex Zinenko /// ``` 554ead2cf7SAlex Zinenko /// 564ead2cf7SAlex Zinenko /// where rank(minor_dims) is the lower-level vector rank (e.g. 1 for LLVM or 574ead2cf7SAlex Zinenko /// higher). 584ead2cf7SAlex Zinenko /// 594ead2cf7SAlex Zinenko /// This is the entry point to emitting pseudo-IR resembling: 604ead2cf7SAlex Zinenko /// 614ead2cf7SAlex Zinenko /// ``` 624ead2cf7SAlex Zinenko /// %tmp = alloc(): memref<(major_dims) x vector<minor_dim x type>> 634ead2cf7SAlex Zinenko /// for (%ivs_major, {0}, {vector_shape}, {1}) { // (N-1)-D loop nest 644ead2cf7SAlex Zinenko /// if (any_of(%ivs_major + %offsets, <, major_dims)) { 654ead2cf7SAlex Zinenko /// %v = vector_transfer_read( 664ead2cf7SAlex Zinenko /// {%offsets_leading, %ivs_major + %offsets_major, %offsets_minor}, 674ead2cf7SAlex Zinenko /// %ivs_minor): 684ead2cf7SAlex Zinenko /// memref<(leading_dims) x (major_dims) x (minor_dims) x type>, 694ead2cf7SAlex Zinenko /// vector<(minor_dims) x type>; 704ead2cf7SAlex Zinenko /// store(%v, %tmp); 714ead2cf7SAlex Zinenko /// } else { 724ead2cf7SAlex Zinenko /// %v = splat(vector<(minor_dims) x type>, %fill) 734ead2cf7SAlex Zinenko /// store(%v, %tmp, %ivs_major); 744ead2cf7SAlex Zinenko /// } 754ead2cf7SAlex Zinenko /// } 764ead2cf7SAlex Zinenko /// %res = load(%tmp, %0): memref<(major_dims) x vector<minor_dim x type>>): 774ead2cf7SAlex Zinenko // vector<(major_dims) x (minor_dims) x type> 784ead2cf7SAlex Zinenko /// ``` 794ead2cf7SAlex Zinenko /// 804ead2cf7SAlex Zinenko template <typename ConcreteOp> 814ead2cf7SAlex Zinenko class NDTransferOpHelper { 824ead2cf7SAlex Zinenko public: 837c3c5b11SNicolas Vasilache NDTransferOpHelper(PatternRewriter &rewriter, ConcreteOp xferOp, 847c3c5b11SNicolas Vasilache const VectorTransferToSCFOptions &options) 857c3c5b11SNicolas Vasilache : rewriter(rewriter), options(options), loc(xferOp.getLoc()), 864ead2cf7SAlex Zinenko scope(std::make_unique<ScopedContext>(rewriter, loc)), xferOp(xferOp), 874ead2cf7SAlex Zinenko op(xferOp.getOperation()) { 884ead2cf7SAlex Zinenko vectorType = xferOp.getVectorType(); 894ead2cf7SAlex Zinenko // TODO(ntv, ajcbik): when we go to k > 1-D vectors adapt minorRank. 904ead2cf7SAlex Zinenko minorRank = 1; 914ead2cf7SAlex Zinenko majorRank = vectorType.getRank() - minorRank; 924ead2cf7SAlex Zinenko leadingRank = xferOp.getMemRefType().getRank() - (majorRank + minorRank); 934ead2cf7SAlex Zinenko majorVectorType = 944ead2cf7SAlex Zinenko VectorType::get(vectorType.getShape().take_front(majorRank), 954ead2cf7SAlex Zinenko vectorType.getElementType()); 964ead2cf7SAlex Zinenko minorVectorType = 974ead2cf7SAlex Zinenko VectorType::get(vectorType.getShape().take_back(minorRank), 984ead2cf7SAlex Zinenko vectorType.getElementType()); 994ead2cf7SAlex Zinenko /// Memref of minor vector type is used for individual transfers. 1004ead2cf7SAlex Zinenko memRefMinorVectorType = 1014ead2cf7SAlex Zinenko MemRefType::get(majorVectorType.getShape(), minorVectorType, {}, 1024ead2cf7SAlex Zinenko xferOp.getMemRefType().getMemorySpace()); 1034ead2cf7SAlex Zinenko } 1044ead2cf7SAlex Zinenko 1054ead2cf7SAlex Zinenko LogicalResult doReplace(); 1064ead2cf7SAlex Zinenko 1074ead2cf7SAlex Zinenko private: 1084ead2cf7SAlex Zinenko /// Creates the loop nest on the "major" dimensions and calls the 1094ead2cf7SAlex Zinenko /// `loopBodyBuilder` lambda in the context of the loop nest. 1104ead2cf7SAlex Zinenko template <typename Lambda> 1114ead2cf7SAlex Zinenko void emitLoops(Lambda loopBodyBuilder); 1124ead2cf7SAlex Zinenko 1134ead2cf7SAlex Zinenko /// Operate within the body of `emitLoops` to: 1147c3c5b11SNicolas Vasilache /// 1. Compute the indexings `majorIvs + majorOffsets` and save them in 1157c3c5b11SNicolas Vasilache /// `majorIvsPlusOffsets`. 1167c3c5b11SNicolas Vasilache /// 2. Return a boolean that determines whether the first `majorIvs.rank()` 1174ead2cf7SAlex Zinenko /// dimensions `majorIvs + majorOffsets` are all within `memrefBounds`. 1187c3c5b11SNicolas Vasilache Value emitInBoundsCondition(ValueRange majorIvs, ValueRange majorOffsets, 1194ead2cf7SAlex Zinenko MemRefBoundsCapture &memrefBounds, 1207c3c5b11SNicolas Vasilache SmallVectorImpl<Value> &majorIvsPlusOffsets); 1214ead2cf7SAlex Zinenko 1224ead2cf7SAlex Zinenko /// Common state to lower vector transfer ops. 1234ead2cf7SAlex Zinenko PatternRewriter &rewriter; 1247c3c5b11SNicolas Vasilache const VectorTransferToSCFOptions &options; 1254ead2cf7SAlex Zinenko Location loc; 1264ead2cf7SAlex Zinenko std::unique_ptr<ScopedContext> scope; 1274ead2cf7SAlex Zinenko ConcreteOp xferOp; 1284ead2cf7SAlex Zinenko Operation *op; 1294ead2cf7SAlex Zinenko // A vector transfer copies data between: 1304ead2cf7SAlex Zinenko // - memref<(leading_dims) x (major_dims) x (minor_dims) x type> 1314ead2cf7SAlex Zinenko // - vector<(major_dims) x (minor_dims) x type> 1324ead2cf7SAlex Zinenko unsigned minorRank; // for now always 1 1334ead2cf7SAlex Zinenko unsigned majorRank; // vector rank - minorRank 1344ead2cf7SAlex Zinenko unsigned leadingRank; // memref rank - vector rank 1354ead2cf7SAlex Zinenko VectorType vectorType; // vector<(major_dims) x (minor_dims) x type> 1364ead2cf7SAlex Zinenko VectorType majorVectorType; // vector<(major_dims) x type> 1374ead2cf7SAlex Zinenko VectorType minorVectorType; // vector<(minor_dims) x type> 1384ead2cf7SAlex Zinenko MemRefType memRefMinorVectorType; // memref<vector<(minor_dims) x type>> 1394ead2cf7SAlex Zinenko }; 1404ead2cf7SAlex Zinenko 1414ead2cf7SAlex Zinenko template <typename ConcreteOp> 1424ead2cf7SAlex Zinenko template <typename Lambda> 1434ead2cf7SAlex Zinenko void NDTransferOpHelper<ConcreteOp>::emitLoops(Lambda loopBodyBuilder) { 1444ead2cf7SAlex Zinenko /// Loop nest operates on the major dimensions 1454ead2cf7SAlex Zinenko MemRefBoundsCapture memrefBoundsCapture(xferOp.memref()); 1467c3c5b11SNicolas Vasilache 1477c3c5b11SNicolas Vasilache if (options.unroll) { 1487c3c5b11SNicolas Vasilache auto shape = majorVectorType.getShape(); 1497c3c5b11SNicolas Vasilache auto strides = computeStrides(shape); 1507c3c5b11SNicolas Vasilache unsigned numUnrolledInstances = computeMaxLinearIndex(shape); 1517c3c5b11SNicolas Vasilache ValueRange indices(xferOp.indices()); 1527c3c5b11SNicolas Vasilache for (unsigned idx = 0; idx < numUnrolledInstances; ++idx) { 1537c3c5b11SNicolas Vasilache SmallVector<int64_t, 4> offsets = delinearize(strides, idx); 1547c3c5b11SNicolas Vasilache SmallVector<Value, 4> offsetValues = 1557c3c5b11SNicolas Vasilache llvm::to_vector<4>(llvm::map_range(offsets, [](int64_t off) -> Value { 1567c3c5b11SNicolas Vasilache return std_constant_index(off); 1577c3c5b11SNicolas Vasilache })); 1587c3c5b11SNicolas Vasilache loopBodyBuilder(offsetValues, indices.take_front(leadingRank), 1597c3c5b11SNicolas Vasilache indices.drop_front(leadingRank).take_front(majorRank), 1607c3c5b11SNicolas Vasilache indices.take_back(minorRank), memrefBoundsCapture); 1617c3c5b11SNicolas Vasilache } 1627c3c5b11SNicolas Vasilache } else { 1634ead2cf7SAlex Zinenko VectorBoundsCapture vectorBoundsCapture(majorVectorType); 1644ead2cf7SAlex Zinenko auto majorLbs = vectorBoundsCapture.getLbs(); 1654ead2cf7SAlex Zinenko auto majorUbs = vectorBoundsCapture.getUbs(); 1664ead2cf7SAlex Zinenko auto majorSteps = vectorBoundsCapture.getSteps(); 167*3f5bd53eSAlex Zinenko affineLoopNestBuilder( 168*3f5bd53eSAlex Zinenko majorLbs, majorUbs, majorSteps, [&](ValueRange majorIvs) { 1694ead2cf7SAlex Zinenko ValueRange indices(xferOp.indices()); 1704ead2cf7SAlex Zinenko loopBodyBuilder(majorIvs, indices.take_front(leadingRank), 1714ead2cf7SAlex Zinenko indices.drop_front(leadingRank).take_front(majorRank), 1724ead2cf7SAlex Zinenko indices.take_back(minorRank), memrefBoundsCapture); 1734ead2cf7SAlex Zinenko }); 1744ead2cf7SAlex Zinenko } 1757c3c5b11SNicolas Vasilache } 1764ead2cf7SAlex Zinenko 1774ead2cf7SAlex Zinenko template <typename ConcreteOp> 1787c3c5b11SNicolas Vasilache Value NDTransferOpHelper<ConcreteOp>::emitInBoundsCondition( 1794ead2cf7SAlex Zinenko ValueRange majorIvs, ValueRange majorOffsets, 1807c3c5b11SNicolas Vasilache MemRefBoundsCapture &memrefBounds, 1817c3c5b11SNicolas Vasilache SmallVectorImpl<Value> &majorIvsPlusOffsets) { 1827c3c5b11SNicolas Vasilache Value inBoundsCondition; 1834ead2cf7SAlex Zinenko majorIvsPlusOffsets.reserve(majorIvs.size()); 1841870e787SNicolas Vasilache unsigned idx = 0; 1854ead2cf7SAlex Zinenko for (auto it : llvm::zip(majorIvs, majorOffsets, memrefBounds.getUbs())) { 1864ead2cf7SAlex Zinenko Value iv = std::get<0>(it), off = std::get<1>(it), ub = std::get<2>(it); 1874ead2cf7SAlex Zinenko using namespace mlir::edsc::op; 1884ead2cf7SAlex Zinenko majorIvsPlusOffsets.push_back(iv + off); 1891870e787SNicolas Vasilache if (xferOp.isMaskedDim(leadingRank + idx)) { 1907c3c5b11SNicolas Vasilache Value inBounds = majorIvsPlusOffsets.back() < ub; 1917c3c5b11SNicolas Vasilache inBoundsCondition = 1927c3c5b11SNicolas Vasilache (inBoundsCondition) ? (inBoundsCondition && inBounds) : inBounds; 1931870e787SNicolas Vasilache } 1941870e787SNicolas Vasilache ++idx; 1954ead2cf7SAlex Zinenko } 1967c3c5b11SNicolas Vasilache return inBoundsCondition; 1974ead2cf7SAlex Zinenko } 1984ead2cf7SAlex Zinenko 199247e185dSNicolas Vasilache // TODO: Parallelism and threadlocal considerations. 200247e185dSNicolas Vasilache static Value setAllocAtFunctionEntry(MemRefType memRefMinorVectorType, 201247e185dSNicolas Vasilache Operation *op) { 202247e185dSNicolas Vasilache auto &b = ScopedContext::getBuilderRef(); 203247e185dSNicolas Vasilache OpBuilder::InsertionGuard guard(b); 204247e185dSNicolas Vasilache b.setInsertionPointToStart(&op->getParentOfType<FuncOp>().front()); 205247e185dSNicolas Vasilache Value res = 206247e185dSNicolas Vasilache std_alloca(memRefMinorVectorType, ValueRange{}, b.getI64IntegerAttr(128)); 207247e185dSNicolas Vasilache return res; 208247e185dSNicolas Vasilache } 209247e185dSNicolas Vasilache 2104ead2cf7SAlex Zinenko template <> 2114ead2cf7SAlex Zinenko LogicalResult NDTransferOpHelper<TransferReadOp>::doReplace() { 2127c3c5b11SNicolas Vasilache Value alloc, result; 2137c3c5b11SNicolas Vasilache if (options.unroll) 2147c3c5b11SNicolas Vasilache result = std_splat(vectorType, xferOp.padding()); 2157c3c5b11SNicolas Vasilache else 216247e185dSNicolas Vasilache alloc = setAllocAtFunctionEntry(memRefMinorVectorType, op); 2174ead2cf7SAlex Zinenko 2184ead2cf7SAlex Zinenko emitLoops([&](ValueRange majorIvs, ValueRange leadingOffsets, 2194ead2cf7SAlex Zinenko ValueRange majorOffsets, ValueRange minorOffsets, 2204ead2cf7SAlex Zinenko MemRefBoundsCapture &memrefBounds) { 2217c3c5b11SNicolas Vasilache /// Lambda to load 1-D vector in the current loop ivs + offset context. 2227c3c5b11SNicolas Vasilache auto load1DVector = [&](ValueRange majorIvsPlusOffsets) -> Value { 2234ead2cf7SAlex Zinenko SmallVector<Value, 8> indexing; 2244ead2cf7SAlex Zinenko indexing.reserve(leadingRank + majorRank + minorRank); 2254ead2cf7SAlex Zinenko indexing.append(leadingOffsets.begin(), leadingOffsets.end()); 2264ead2cf7SAlex Zinenko indexing.append(majorIvsPlusOffsets.begin(), majorIvsPlusOffsets.end()); 2274ead2cf7SAlex Zinenko indexing.append(minorOffsets.begin(), minorOffsets.end()); 22836cdc17fSNicolas Vasilache Value memref = xferOp.memref(); 22936cdc17fSNicolas Vasilache auto map = TransferReadOp::getTransferMinorIdentityMap( 23036cdc17fSNicolas Vasilache xferOp.getMemRefType(), minorVectorType); 2311870e787SNicolas Vasilache ArrayAttr masked; 2321870e787SNicolas Vasilache if (xferOp.isMaskedDim(xferOp.getVectorType().getRank() - 1)) { 2331870e787SNicolas Vasilache OpBuilder &b = ScopedContext::getBuilderRef(); 2341870e787SNicolas Vasilache masked = b.getBoolArrayAttr({true}); 2351870e787SNicolas Vasilache } 2367c3c5b11SNicolas Vasilache return vector_transfer_read(minorVectorType, memref, indexing, 2377c3c5b11SNicolas Vasilache AffineMapAttr::get(map), xferOp.padding(), 2387c3c5b11SNicolas Vasilache masked); 2394ead2cf7SAlex Zinenko }; 2407c3c5b11SNicolas Vasilache 2417c3c5b11SNicolas Vasilache // 1. Compute the inBoundsCondition in the current loops ivs + offset 2427c3c5b11SNicolas Vasilache // context. 2437c3c5b11SNicolas Vasilache SmallVector<Value, 4> majorIvsPlusOffsets; 2447c3c5b11SNicolas Vasilache Value inBoundsCondition = emitInBoundsCondition( 2457c3c5b11SNicolas Vasilache majorIvs, majorOffsets, memrefBounds, majorIvsPlusOffsets); 2467c3c5b11SNicolas Vasilache 2477c3c5b11SNicolas Vasilache if (inBoundsCondition) { 2487c3c5b11SNicolas Vasilache // 2. If the condition is not null, we need an IfOp, which may yield 2497c3c5b11SNicolas Vasilache // if `options.unroll` is true. 2507c3c5b11SNicolas Vasilache SmallVector<Type, 1> resultType; 2517c3c5b11SNicolas Vasilache if (options.unroll) 2527c3c5b11SNicolas Vasilache resultType.push_back(vectorType); 2537c3c5b11SNicolas Vasilache 254cadb7ccfSAlex Zinenko // 3. If in-bounds, progressively lower to a 1-D transfer read, otherwise 255cadb7ccfSAlex Zinenko // splat a 1-D vector. 256cadb7ccfSAlex Zinenko ValueRange ifResults = conditionBuilder( 257cadb7ccfSAlex Zinenko resultType, inBoundsCondition, 258cadb7ccfSAlex Zinenko [&]() -> scf::ValueVector { 2597c3c5b11SNicolas Vasilache Value vector = load1DVector(majorIvsPlusOffsets); 260cadb7ccfSAlex Zinenko // 3.a. If `options.unroll` is true, insert the 1-D vector in the 2617c3c5b11SNicolas Vasilache // aggregate. We must yield and merge with the `else` branch. 2627c3c5b11SNicolas Vasilache if (options.unroll) { 2637c3c5b11SNicolas Vasilache vector = vector_insert(vector, result, majorIvs); 264cadb7ccfSAlex Zinenko return {vector}; 2657c3c5b11SNicolas Vasilache } 266cadb7ccfSAlex Zinenko // 3.b. Otherwise, just go through the temporary `alloc`. 2674ead2cf7SAlex Zinenko std_store(vector, alloc, majorIvs); 268cadb7ccfSAlex Zinenko return {}; 269cadb7ccfSAlex Zinenko }, 270cadb7ccfSAlex Zinenko [&]() -> scf::ValueVector { 2717c3c5b11SNicolas Vasilache Value vector = std_splat(minorVectorType, xferOp.padding()); 272cadb7ccfSAlex Zinenko // 3.c. If `options.unroll` is true, insert the 1-D vector in the 2737c3c5b11SNicolas Vasilache // aggregate. We must yield and merge with the `then` branch. 2747c3c5b11SNicolas Vasilache if (options.unroll) { 2757c3c5b11SNicolas Vasilache vector = vector_insert(vector, result, majorIvs); 276cadb7ccfSAlex Zinenko return {vector}; 2777c3c5b11SNicolas Vasilache } 278cadb7ccfSAlex Zinenko // 3.d. Otherwise, just go through the temporary `alloc`. 2797c3c5b11SNicolas Vasilache std_store(vector, alloc, majorIvs); 280cadb7ccfSAlex Zinenko return {}; 2817c3c5b11SNicolas Vasilache }); 282cadb7ccfSAlex Zinenko 2837c3c5b11SNicolas Vasilache if (!resultType.empty()) 284cadb7ccfSAlex Zinenko result = *ifResults.begin(); 2857c3c5b11SNicolas Vasilache } else { 2867c3c5b11SNicolas Vasilache // 4. Guaranteed in-bounds, progressively lower to a 1-D transfer read. 2877c3c5b11SNicolas Vasilache Value loaded1D = load1DVector(majorIvsPlusOffsets); 2887c3c5b11SNicolas Vasilache // 5.a. If `options.unroll` is true, insert the 1-D vector in the 2897c3c5b11SNicolas Vasilache // aggregate. 2907c3c5b11SNicolas Vasilache if (options.unroll) 2917c3c5b11SNicolas Vasilache result = vector_insert(loaded1D, result, majorIvs); 2927c3c5b11SNicolas Vasilache // 5.b. Otherwise, just go through the temporary `alloc`. 2937c3c5b11SNicolas Vasilache else 2947c3c5b11SNicolas Vasilache std_store(loaded1D, alloc, majorIvs); 2957c3c5b11SNicolas Vasilache } 2967c3c5b11SNicolas Vasilache }); 2977c3c5b11SNicolas Vasilache 298a9b5edc5SBenjamin Kramer assert((!options.unroll ^ (bool)result) && 299a9b5edc5SBenjamin Kramer "Expected resulting Value iff unroll"); 3007c3c5b11SNicolas Vasilache if (!result) 3017c3c5b11SNicolas Vasilache result = std_load(vector_type_cast(MemRefType::get({}, vectorType), alloc)); 3027c3c5b11SNicolas Vasilache rewriter.replaceOp(op, result); 3034ead2cf7SAlex Zinenko 3044ead2cf7SAlex Zinenko return success(); 3054ead2cf7SAlex Zinenko } 3064ead2cf7SAlex Zinenko 3074ead2cf7SAlex Zinenko template <> 3084ead2cf7SAlex Zinenko LogicalResult NDTransferOpHelper<TransferWriteOp>::doReplace() { 3097c3c5b11SNicolas Vasilache Value alloc; 3107c3c5b11SNicolas Vasilache if (!options.unroll) { 311247e185dSNicolas Vasilache alloc = setAllocAtFunctionEntry(memRefMinorVectorType, op); 3124ead2cf7SAlex Zinenko std_store(xferOp.vector(), 3134ead2cf7SAlex Zinenko vector_type_cast(MemRefType::get({}, vectorType), alloc)); 3147c3c5b11SNicolas Vasilache } 3154ead2cf7SAlex Zinenko 3164ead2cf7SAlex Zinenko emitLoops([&](ValueRange majorIvs, ValueRange leadingOffsets, 3174ead2cf7SAlex Zinenko ValueRange majorOffsets, ValueRange minorOffsets, 3184ead2cf7SAlex Zinenko MemRefBoundsCapture &memrefBounds) { 3197c3c5b11SNicolas Vasilache // Lower to 1-D vector_transfer_write and let recursion handle it. 3207c3c5b11SNicolas Vasilache auto emitTransferWrite = [&](ValueRange majorIvsPlusOffsets) { 3214ead2cf7SAlex Zinenko SmallVector<Value, 8> indexing; 3224ead2cf7SAlex Zinenko indexing.reserve(leadingRank + majorRank + minorRank); 3234ead2cf7SAlex Zinenko indexing.append(leadingOffsets.begin(), leadingOffsets.end()); 3244ead2cf7SAlex Zinenko indexing.append(majorIvsPlusOffsets.begin(), majorIvsPlusOffsets.end()); 3254ead2cf7SAlex Zinenko indexing.append(minorOffsets.begin(), minorOffsets.end()); 3267c3c5b11SNicolas Vasilache Value result; 3277c3c5b11SNicolas Vasilache // If `options.unroll` is true, extract the 1-D vector from the 3287c3c5b11SNicolas Vasilache // aggregate. 3297c3c5b11SNicolas Vasilache if (options.unroll) 3307c3c5b11SNicolas Vasilache result = vector_extract(xferOp.vector(), majorIvs); 3317c3c5b11SNicolas Vasilache else 3327c3c5b11SNicolas Vasilache result = std_load(alloc, majorIvs); 33336cdc17fSNicolas Vasilache auto map = TransferWriteOp::getTransferMinorIdentityMap( 33436cdc17fSNicolas Vasilache xferOp.getMemRefType(), minorVectorType); 3351870e787SNicolas Vasilache ArrayAttr masked; 3361870e787SNicolas Vasilache if (xferOp.isMaskedDim(xferOp.getVectorType().getRank() - 1)) { 3371870e787SNicolas Vasilache OpBuilder &b = ScopedContext::getBuilderRef(); 3381870e787SNicolas Vasilache masked = b.getBoolArrayAttr({true}); 3391870e787SNicolas Vasilache } 3407c3c5b11SNicolas Vasilache vector_transfer_write(result, xferOp.memref(), indexing, 3411870e787SNicolas Vasilache AffineMapAttr::get(map), masked); 3424ead2cf7SAlex Zinenko }; 3437c3c5b11SNicolas Vasilache 3447c3c5b11SNicolas Vasilache // 1. Compute the inBoundsCondition in the current loops ivs + offset 3457c3c5b11SNicolas Vasilache // context. 3467c3c5b11SNicolas Vasilache SmallVector<Value, 4> majorIvsPlusOffsets; 3477c3c5b11SNicolas Vasilache Value inBoundsCondition = emitInBoundsCondition( 3487c3c5b11SNicolas Vasilache majorIvs, majorOffsets, memrefBounds, majorIvsPlusOffsets); 3497c3c5b11SNicolas Vasilache 3507c3c5b11SNicolas Vasilache if (inBoundsCondition) { 3517c3c5b11SNicolas Vasilache // 2.a. If the condition is not null, we need an IfOp, to write 3527c3c5b11SNicolas Vasilache // conditionally. Progressively lower to a 1-D transfer write. 353cadb7ccfSAlex Zinenko conditionBuilder(inBoundsCondition, 354cadb7ccfSAlex Zinenko [&] { emitTransferWrite(majorIvsPlusOffsets); }); 3557c3c5b11SNicolas Vasilache } else { 3567c3c5b11SNicolas Vasilache // 2.b. Guaranteed in-bounds. Progressively lower to a 1-D transfer write. 3577c3c5b11SNicolas Vasilache emitTransferWrite(majorIvsPlusOffsets); 3587c3c5b11SNicolas Vasilache } 3594ead2cf7SAlex Zinenko }); 3604ead2cf7SAlex Zinenko 3614ead2cf7SAlex Zinenko rewriter.eraseOp(op); 3624ead2cf7SAlex Zinenko 3634ead2cf7SAlex Zinenko return success(); 3644ead2cf7SAlex Zinenko } 3654ead2cf7SAlex Zinenko 366da95a0d8SNicolas Vasilache } // namespace 367da95a0d8SNicolas Vasilache 3684ead2cf7SAlex Zinenko /// Analyzes the `transfer` to find an access dimension along the fastest remote 3694ead2cf7SAlex Zinenko /// MemRef dimension. If such a dimension with coalescing properties is found, 3704ead2cf7SAlex Zinenko /// `pivs` and `vectorBoundsCapture` are swapped so that the invocation of 3714ead2cf7SAlex Zinenko /// LoopNestBuilder captures it in the innermost loop. 3724ead2cf7SAlex Zinenko template <typename TransferOpTy> 3734ead2cf7SAlex Zinenko static int computeCoalescedIndex(TransferOpTy transfer) { 3744ead2cf7SAlex Zinenko // rank of the remote memory access, coalescing behavior occurs on the 3754ead2cf7SAlex Zinenko // innermost memory dimension. 3764ead2cf7SAlex Zinenko auto remoteRank = transfer.getMemRefType().getRank(); 3774ead2cf7SAlex Zinenko // Iterate over the results expressions of the permutation map to determine 3784ead2cf7SAlex Zinenko // the loop order for creating pointwise copies between remote and local 3794ead2cf7SAlex Zinenko // memories. 3804ead2cf7SAlex Zinenko int coalescedIdx = -1; 3814ead2cf7SAlex Zinenko auto exprs = transfer.permutation_map().getResults(); 3824ead2cf7SAlex Zinenko for (auto en : llvm::enumerate(exprs)) { 3834ead2cf7SAlex Zinenko auto dim = en.value().template dyn_cast<AffineDimExpr>(); 3844ead2cf7SAlex Zinenko if (!dim) { 3854ead2cf7SAlex Zinenko continue; 3864ead2cf7SAlex Zinenko } 3874ead2cf7SAlex Zinenko auto memRefDim = dim.getPosition(); 3884ead2cf7SAlex Zinenko if (memRefDim == remoteRank - 1) { 3894ead2cf7SAlex Zinenko // memRefDim has coalescing properties, it should be swapped in the last 3904ead2cf7SAlex Zinenko // position. 3914ead2cf7SAlex Zinenko assert(coalescedIdx == -1 && "Unexpected > 1 coalesced indices"); 3924ead2cf7SAlex Zinenko coalescedIdx = en.index(); 3934ead2cf7SAlex Zinenko } 3944ead2cf7SAlex Zinenko } 3954ead2cf7SAlex Zinenko return coalescedIdx; 3964ead2cf7SAlex Zinenko } 3974ead2cf7SAlex Zinenko 3984ead2cf7SAlex Zinenko /// Emits remote memory accesses that are clipped to the boundaries of the 3994ead2cf7SAlex Zinenko /// MemRef. 4004ead2cf7SAlex Zinenko template <typename TransferOpTy> 4014ead2cf7SAlex Zinenko static SmallVector<Value, 8> 4024ead2cf7SAlex Zinenko clip(TransferOpTy transfer, MemRefBoundsCapture &bounds, ArrayRef<Value> ivs) { 4034ead2cf7SAlex Zinenko using namespace mlir::edsc; 4044ead2cf7SAlex Zinenko 4054ead2cf7SAlex Zinenko Value zero(std_constant_index(0)), one(std_constant_index(1)); 4064ead2cf7SAlex Zinenko SmallVector<Value, 8> memRefAccess(transfer.indices()); 4074ead2cf7SAlex Zinenko SmallVector<Value, 8> clippedScalarAccessExprs(memRefAccess.size()); 4084ead2cf7SAlex Zinenko // Indices accessing to remote memory are clipped and their expressions are 4094ead2cf7SAlex Zinenko // returned in clippedScalarAccessExprs. 4104ead2cf7SAlex Zinenko for (unsigned memRefDim = 0; memRefDim < clippedScalarAccessExprs.size(); 4114ead2cf7SAlex Zinenko ++memRefDim) { 4124ead2cf7SAlex Zinenko // Linear search on a small number of entries. 4134ead2cf7SAlex Zinenko int loopIndex = -1; 4144ead2cf7SAlex Zinenko auto exprs = transfer.permutation_map().getResults(); 4154ead2cf7SAlex Zinenko for (auto en : llvm::enumerate(exprs)) { 4164ead2cf7SAlex Zinenko auto expr = en.value(); 4174ead2cf7SAlex Zinenko auto dim = expr.template dyn_cast<AffineDimExpr>(); 4184ead2cf7SAlex Zinenko // Sanity check. 4194ead2cf7SAlex Zinenko assert( 4204ead2cf7SAlex Zinenko (dim || expr.template cast<AffineConstantExpr>().getValue() == 0) && 4214ead2cf7SAlex Zinenko "Expected dim or 0 in permutationMap"); 4224ead2cf7SAlex Zinenko if (dim && memRefDim == dim.getPosition()) { 4234ead2cf7SAlex Zinenko loopIndex = en.index(); 4244ead2cf7SAlex Zinenko break; 4254ead2cf7SAlex Zinenko } 4264ead2cf7SAlex Zinenko } 4274ead2cf7SAlex Zinenko 4284ead2cf7SAlex Zinenko // We cannot distinguish atm between unrolled dimensions that implement 4294ead2cf7SAlex Zinenko // the "always full" tile abstraction and need clipping from the other 4304ead2cf7SAlex Zinenko // ones. So we conservatively clip everything. 4314ead2cf7SAlex Zinenko using namespace edsc::op; 4324ead2cf7SAlex Zinenko auto N = bounds.ub(memRefDim); 4334ead2cf7SAlex Zinenko auto i = memRefAccess[memRefDim]; 4344ead2cf7SAlex Zinenko if (loopIndex < 0) { 4354ead2cf7SAlex Zinenko auto N_minus_1 = N - one; 4364ead2cf7SAlex Zinenko auto select_1 = std_select(i < N, i, N_minus_1); 4374ead2cf7SAlex Zinenko clippedScalarAccessExprs[memRefDim] = 4384ead2cf7SAlex Zinenko std_select(i < zero, zero, select_1); 4394ead2cf7SAlex Zinenko } else { 4404ead2cf7SAlex Zinenko auto ii = ivs[loopIndex]; 4414ead2cf7SAlex Zinenko auto i_plus_ii = i + ii; 4424ead2cf7SAlex Zinenko auto N_minus_1 = N - one; 4434ead2cf7SAlex Zinenko auto select_1 = std_select(i_plus_ii < N, i_plus_ii, N_minus_1); 4444ead2cf7SAlex Zinenko clippedScalarAccessExprs[memRefDim] = 4454ead2cf7SAlex Zinenko std_select(i_plus_ii < zero, zero, select_1); 4464ead2cf7SAlex Zinenko } 4474ead2cf7SAlex Zinenko } 4484ead2cf7SAlex Zinenko 4494ead2cf7SAlex Zinenko return clippedScalarAccessExprs; 4504ead2cf7SAlex Zinenko } 4514ead2cf7SAlex Zinenko 4523393cc4cSNicolas Vasilache namespace mlir { 4533393cc4cSNicolas Vasilache 4544ead2cf7SAlex Zinenko template <typename TransferOpTy> 4553393cc4cSNicolas Vasilache VectorTransferRewriter<TransferOpTy>::VectorTransferRewriter( 4567c3c5b11SNicolas Vasilache VectorTransferToSCFOptions options, MLIRContext *context) 4577c3c5b11SNicolas Vasilache : RewritePattern(TransferOpTy::getOperationName(), 1, context), 4587c3c5b11SNicolas Vasilache options(options) {} 4594ead2cf7SAlex Zinenko 4607c3c5b11SNicolas Vasilache /// Used for staging the transfer in a local buffer. 4617c3c5b11SNicolas Vasilache template <typename TransferOpTy> 4623393cc4cSNicolas Vasilache MemRefType VectorTransferRewriter<TransferOpTy>::tmpMemRefType( 4637c3c5b11SNicolas Vasilache TransferOpTy transfer) const { 4644ead2cf7SAlex Zinenko auto vectorType = transfer.getVectorType(); 4657c3c5b11SNicolas Vasilache return MemRefType::get(vectorType.getShape(), vectorType.getElementType(), {}, 4667c3c5b11SNicolas Vasilache 0); 4674ead2cf7SAlex Zinenko } 4684ead2cf7SAlex Zinenko 4694ead2cf7SAlex Zinenko /// Lowers TransferReadOp into a combination of: 4704ead2cf7SAlex Zinenko /// 1. local memory allocation; 4714ead2cf7SAlex Zinenko /// 2. perfect loop nest over: 4724ead2cf7SAlex Zinenko /// a. scalar load from local buffers (viewed as a scalar memref); 4734ead2cf7SAlex Zinenko /// a. scalar store to original memref (with clipping). 4744ead2cf7SAlex Zinenko /// 3. vector_load from local buffer (viewed as a memref<1 x vector>); 4754ead2cf7SAlex Zinenko /// 4. local memory deallocation. 4764ead2cf7SAlex Zinenko /// 4774ead2cf7SAlex Zinenko /// Lowers the data transfer part of a TransferReadOp while ensuring no 4784ead2cf7SAlex Zinenko /// out-of-bounds accesses are possible. Out-of-bounds behavior is handled by 4794ead2cf7SAlex Zinenko /// clipping. This means that a given value in memory can be read multiple 4804ead2cf7SAlex Zinenko /// times and concurrently. 4814ead2cf7SAlex Zinenko /// 4824ead2cf7SAlex Zinenko /// Important notes about clipping and "full-tiles only" abstraction: 4834ead2cf7SAlex Zinenko /// ================================================================= 4844ead2cf7SAlex Zinenko /// When using clipping for dealing with boundary conditions, the same edge 4854ead2cf7SAlex Zinenko /// value will appear multiple times (a.k.a edge padding). This is fine if the 4864ead2cf7SAlex Zinenko /// subsequent vector operations are all data-parallel but **is generally 4874ead2cf7SAlex Zinenko /// incorrect** in the presence of reductions or extract operations. 4884ead2cf7SAlex Zinenko /// 4894ead2cf7SAlex Zinenko /// More generally, clipping is a scalar abstraction that is expected to work 4904ead2cf7SAlex Zinenko /// fine as a baseline for CPUs and GPUs but not for vector_load and DMAs. 4914ead2cf7SAlex Zinenko /// To deal with real vector_load and DMAs, a "padded allocation + view" 4924ead2cf7SAlex Zinenko /// abstraction with the ability to read out-of-memref-bounds (but still within 4934ead2cf7SAlex Zinenko /// the allocated region) is necessary. 4944ead2cf7SAlex Zinenko /// 4954ead2cf7SAlex Zinenko /// Whether using scalar loops or vector_load/DMAs to perform the transfer, 4964ead2cf7SAlex Zinenko /// junk values will be materialized in the vectors and generally need to be 4974ead2cf7SAlex Zinenko /// filtered out and replaced by the "neutral element". This neutral element is 4984ead2cf7SAlex Zinenko /// op-dependent so, in the future, we expect to create a vector filter and 4994ead2cf7SAlex Zinenko /// apply it to a splatted constant vector with the proper neutral element at 5004ead2cf7SAlex Zinenko /// each ssa-use. This filtering is not necessary for pure data-parallel 5014ead2cf7SAlex Zinenko /// operations. 5024ead2cf7SAlex Zinenko /// 5034ead2cf7SAlex Zinenko /// In the case of vector_store/DMAs, Read-Modify-Write will be required, which 5044ead2cf7SAlex Zinenko /// also have concurrency implications. Note that by using clipped scalar stores 5054ead2cf7SAlex Zinenko /// in the presence of data-parallel only operations, we generate code that 5064ead2cf7SAlex Zinenko /// writes the same value multiple time on the edge locations. 5074ead2cf7SAlex Zinenko /// 5084ead2cf7SAlex Zinenko /// TODO(ntv): implement alternatives to clipping. 5094ead2cf7SAlex Zinenko /// TODO(ntv): support non-data-parallel operations. 5104ead2cf7SAlex Zinenko 5114ead2cf7SAlex Zinenko /// Performs the rewrite. 5124ead2cf7SAlex Zinenko template <> 5133393cc4cSNicolas Vasilache LogicalResult VectorTransferRewriter<TransferReadOp>::matchAndRewrite( 5144ead2cf7SAlex Zinenko Operation *op, PatternRewriter &rewriter) const { 5154ead2cf7SAlex Zinenko using namespace mlir::edsc::op; 5164ead2cf7SAlex Zinenko 5174ead2cf7SAlex Zinenko TransferReadOp transfer = cast<TransferReadOp>(op); 5184ead2cf7SAlex Zinenko if (AffineMap::isMinorIdentity(transfer.permutation_map())) { 5194ead2cf7SAlex Zinenko // If > 1D, emit a bunch of loops around 1-D vector transfers. 5204ead2cf7SAlex Zinenko if (transfer.getVectorType().getRank() > 1) 5217c3c5b11SNicolas Vasilache return NDTransferOpHelper<TransferReadOp>(rewriter, transfer, options) 5227c3c5b11SNicolas Vasilache .doReplace(); 5234ead2cf7SAlex Zinenko // If 1-D this is now handled by the target-specific lowering. 5244ead2cf7SAlex Zinenko if (transfer.getVectorType().getRank() == 1) 5254ead2cf7SAlex Zinenko return failure(); 5264ead2cf7SAlex Zinenko } 5274ead2cf7SAlex Zinenko 5284ead2cf7SAlex Zinenko // Conservative lowering to scalar load / stores. 5294ead2cf7SAlex Zinenko // 1. Setup all the captures. 5304ead2cf7SAlex Zinenko ScopedContext scope(rewriter, transfer.getLoc()); 5314ead2cf7SAlex Zinenko StdIndexedValue remote(transfer.memref()); 5324ead2cf7SAlex Zinenko MemRefBoundsCapture memRefBoundsCapture(transfer.memref()); 5334ead2cf7SAlex Zinenko VectorBoundsCapture vectorBoundsCapture(transfer.vector()); 5344ead2cf7SAlex Zinenko int coalescedIdx = computeCoalescedIndex(transfer); 5354ead2cf7SAlex Zinenko // Swap the vectorBoundsCapture which will reorder loop bounds. 5364ead2cf7SAlex Zinenko if (coalescedIdx >= 0) 5374ead2cf7SAlex Zinenko vectorBoundsCapture.swapRanges(vectorBoundsCapture.rank() - 1, 5384ead2cf7SAlex Zinenko coalescedIdx); 5394ead2cf7SAlex Zinenko 5404ead2cf7SAlex Zinenko auto lbs = vectorBoundsCapture.getLbs(); 5414ead2cf7SAlex Zinenko auto ubs = vectorBoundsCapture.getUbs(); 5424ead2cf7SAlex Zinenko SmallVector<Value, 8> steps; 5434ead2cf7SAlex Zinenko steps.reserve(vectorBoundsCapture.getSteps().size()); 5444ead2cf7SAlex Zinenko for (auto step : vectorBoundsCapture.getSteps()) 5454ead2cf7SAlex Zinenko steps.push_back(std_constant_index(step)); 5464ead2cf7SAlex Zinenko 5474ead2cf7SAlex Zinenko // 2. Emit alloc-copy-load-dealloc. 5484ead2cf7SAlex Zinenko Value tmp = std_alloc(tmpMemRefType(transfer)); 5494ead2cf7SAlex Zinenko StdIndexedValue local(tmp); 5504ead2cf7SAlex Zinenko Value vec = vector_type_cast(tmp); 551d1560f39SAlex Zinenko loopNestBuilder(lbs, ubs, steps, [&](ValueRange loopIvs) { 552d1560f39SAlex Zinenko auto ivs = llvm::to_vector<8>(loopIvs); 5534ead2cf7SAlex Zinenko // Swap the ivs which will reorder memory accesses. 5544ead2cf7SAlex Zinenko if (coalescedIdx >= 0) 5554ead2cf7SAlex Zinenko std::swap(ivs.back(), ivs[coalescedIdx]); 5564ead2cf7SAlex Zinenko // Computes clippedScalarAccessExprs in the loop nest scope (ivs exist). 5574ead2cf7SAlex Zinenko local(ivs) = remote(clip(transfer, memRefBoundsCapture, ivs)); 5584ead2cf7SAlex Zinenko }); 5594ead2cf7SAlex Zinenko Value vectorValue = std_load(vec); 5604ead2cf7SAlex Zinenko (std_dealloc(tmp)); // vexing parse 5614ead2cf7SAlex Zinenko 5624ead2cf7SAlex Zinenko // 3. Propagate. 5634ead2cf7SAlex Zinenko rewriter.replaceOp(op, vectorValue); 5644ead2cf7SAlex Zinenko return success(); 5654ead2cf7SAlex Zinenko } 5664ead2cf7SAlex Zinenko 5674ead2cf7SAlex Zinenko /// Lowers TransferWriteOp into a combination of: 5684ead2cf7SAlex Zinenko /// 1. local memory allocation; 5694ead2cf7SAlex Zinenko /// 2. vector_store to local buffer (viewed as a memref<1 x vector>); 5704ead2cf7SAlex Zinenko /// 3. perfect loop nest over: 5714ead2cf7SAlex Zinenko /// a. scalar load from local buffers (viewed as a scalar memref); 5724ead2cf7SAlex Zinenko /// a. scalar store to original memref (with clipping). 5734ead2cf7SAlex Zinenko /// 4. local memory deallocation. 5744ead2cf7SAlex Zinenko /// 5754ead2cf7SAlex Zinenko /// More specifically, lowers the data transfer part while ensuring no 5764ead2cf7SAlex Zinenko /// out-of-bounds accesses are possible. Out-of-bounds behavior is handled by 5774ead2cf7SAlex Zinenko /// clipping. This means that a given value in memory can be written to multiple 5784ead2cf7SAlex Zinenko /// times and concurrently. 5794ead2cf7SAlex Zinenko /// 5804ead2cf7SAlex Zinenko /// See `Important notes about clipping and full-tiles only abstraction` in the 5814ead2cf7SAlex Zinenko /// description of `readClipped` above. 5824ead2cf7SAlex Zinenko /// 5834ead2cf7SAlex Zinenko /// TODO(ntv): implement alternatives to clipping. 5844ead2cf7SAlex Zinenko /// TODO(ntv): support non-data-parallel operations. 5854ead2cf7SAlex Zinenko template <> 5863393cc4cSNicolas Vasilache LogicalResult VectorTransferRewriter<TransferWriteOp>::matchAndRewrite( 5874ead2cf7SAlex Zinenko Operation *op, PatternRewriter &rewriter) const { 5884ead2cf7SAlex Zinenko using namespace edsc::op; 5894ead2cf7SAlex Zinenko 5904ead2cf7SAlex Zinenko TransferWriteOp transfer = cast<TransferWriteOp>(op); 5914ead2cf7SAlex Zinenko if (AffineMap::isMinorIdentity(transfer.permutation_map())) { 5924ead2cf7SAlex Zinenko // If > 1D, emit a bunch of loops around 1-D vector transfers. 5934ead2cf7SAlex Zinenko if (transfer.getVectorType().getRank() > 1) 5947c3c5b11SNicolas Vasilache return NDTransferOpHelper<TransferWriteOp>(rewriter, transfer, options) 5954ead2cf7SAlex Zinenko .doReplace(); 5964ead2cf7SAlex Zinenko // If 1-D this is now handled by the target-specific lowering. 5974ead2cf7SAlex Zinenko if (transfer.getVectorType().getRank() == 1) 5984ead2cf7SAlex Zinenko return failure(); 5994ead2cf7SAlex Zinenko } 6004ead2cf7SAlex Zinenko 6014ead2cf7SAlex Zinenko // 1. Setup all the captures. 6024ead2cf7SAlex Zinenko ScopedContext scope(rewriter, transfer.getLoc()); 6034ead2cf7SAlex Zinenko StdIndexedValue remote(transfer.memref()); 6044ead2cf7SAlex Zinenko MemRefBoundsCapture memRefBoundsCapture(transfer.memref()); 6054ead2cf7SAlex Zinenko Value vectorValue(transfer.vector()); 6064ead2cf7SAlex Zinenko VectorBoundsCapture vectorBoundsCapture(transfer.vector()); 6074ead2cf7SAlex Zinenko int coalescedIdx = computeCoalescedIndex(transfer); 6084ead2cf7SAlex Zinenko // Swap the vectorBoundsCapture which will reorder loop bounds. 6094ead2cf7SAlex Zinenko if (coalescedIdx >= 0) 6104ead2cf7SAlex Zinenko vectorBoundsCapture.swapRanges(vectorBoundsCapture.rank() - 1, 6114ead2cf7SAlex Zinenko coalescedIdx); 6124ead2cf7SAlex Zinenko 6134ead2cf7SAlex Zinenko auto lbs = vectorBoundsCapture.getLbs(); 6144ead2cf7SAlex Zinenko auto ubs = vectorBoundsCapture.getUbs(); 6154ead2cf7SAlex Zinenko SmallVector<Value, 8> steps; 6164ead2cf7SAlex Zinenko steps.reserve(vectorBoundsCapture.getSteps().size()); 6174ead2cf7SAlex Zinenko for (auto step : vectorBoundsCapture.getSteps()) 6184ead2cf7SAlex Zinenko steps.push_back(std_constant_index(step)); 6194ead2cf7SAlex Zinenko 6204ead2cf7SAlex Zinenko // 2. Emit alloc-store-copy-dealloc. 6214ead2cf7SAlex Zinenko Value tmp = std_alloc(tmpMemRefType(transfer)); 6224ead2cf7SAlex Zinenko StdIndexedValue local(tmp); 6234ead2cf7SAlex Zinenko Value vec = vector_type_cast(tmp); 6244ead2cf7SAlex Zinenko std_store(vectorValue, vec); 625d1560f39SAlex Zinenko loopNestBuilder(lbs, ubs, steps, [&](ValueRange loopIvs) { 626d1560f39SAlex Zinenko auto ivs = llvm::to_vector<8>(loopIvs); 6274ead2cf7SAlex Zinenko // Swap the ivs which will reorder memory accesses. 6284ead2cf7SAlex Zinenko if (coalescedIdx >= 0) 6294ead2cf7SAlex Zinenko std::swap(ivs.back(), ivs[coalescedIdx]); 6304ead2cf7SAlex Zinenko // Computes clippedScalarAccessExprs in the loop nest scope (ivs exist). 6314ead2cf7SAlex Zinenko remote(clip(transfer, memRefBoundsCapture, ivs)) = local(ivs); 6324ead2cf7SAlex Zinenko }); 6334ead2cf7SAlex Zinenko (std_dealloc(tmp)); // vexing parse... 6344ead2cf7SAlex Zinenko 6354ead2cf7SAlex Zinenko rewriter.eraseOp(op); 6364ead2cf7SAlex Zinenko return success(); 6374ead2cf7SAlex Zinenko } 6384ead2cf7SAlex Zinenko 6393393cc4cSNicolas Vasilache void populateVectorToSCFConversionPatterns( 6407c3c5b11SNicolas Vasilache OwningRewritePatternList &patterns, MLIRContext *context, 6417c3c5b11SNicolas Vasilache const VectorTransferToSCFOptions &options) { 6424ead2cf7SAlex Zinenko patterns.insert<VectorTransferRewriter<vector::TransferReadOp>, 6437c3c5b11SNicolas Vasilache VectorTransferRewriter<vector::TransferWriteOp>>(options, 6447c3c5b11SNicolas Vasilache context); 6454ead2cf7SAlex Zinenko } 6463393cc4cSNicolas Vasilache 6473393cc4cSNicolas Vasilache } // namespace mlir 6483393cc4cSNicolas Vasilache 6495f9e0466SNicolas Vasilache namespace { 6505f9e0466SNicolas Vasilache 6515f9e0466SNicolas Vasilache struct ConvertVectorToSCFPass 6525f9e0466SNicolas Vasilache : public ConvertVectorToSCFBase<ConvertVectorToSCFPass> { 6535f9e0466SNicolas Vasilache ConvertVectorToSCFPass() = default; 6545f9e0466SNicolas Vasilache ConvertVectorToSCFPass(const VectorTransferToSCFOptions &options) { 6555f9e0466SNicolas Vasilache this->fullUnroll = options.unroll; 6565f9e0466SNicolas Vasilache } 6575f9e0466SNicolas Vasilache 6585f9e0466SNicolas Vasilache void runOnFunction() override { 6595f9e0466SNicolas Vasilache OwningRewritePatternList patterns; 6605f9e0466SNicolas Vasilache auto *context = getFunction().getContext(); 6615f9e0466SNicolas Vasilache populateVectorToSCFConversionPatterns( 6625f9e0466SNicolas Vasilache patterns, context, VectorTransferToSCFOptions().setUnroll(fullUnroll)); 6635f9e0466SNicolas Vasilache applyPatternsAndFoldGreedily(getFunction(), patterns); 6645f9e0466SNicolas Vasilache } 6655f9e0466SNicolas Vasilache }; 6665f9e0466SNicolas Vasilache 6675f9e0466SNicolas Vasilache } // namespace 6685f9e0466SNicolas Vasilache 6695f9e0466SNicolas Vasilache std::unique_ptr<Pass> 6705f9e0466SNicolas Vasilache mlir::createConvertVectorToSCFPass(const VectorTransferToSCFOptions &options) { 6715f9e0466SNicolas Vasilache return std::make_unique<ConvertVectorToSCFPass>(options); 6725f9e0466SNicolas Vasilache } 673