diff --git a/mlir/include/mlir/Dialect/Affine/IR/AffineOps.h b/mlir/include/mlir/Dialect/Affine/IR/AffineOps.h index a614ec61c0b3..164e8b3b83ee 100644 --- a/mlir/include/mlir/Dialect/Affine/IR/AffineOps.h +++ b/mlir/include/mlir/Dialect/Affine/IR/AffineOps.h @@ -356,7 +356,10 @@ void canonicalizeSetAndOperands(IntegerSet *set, /// other AffineApplyOps supplying those operands. The operands of the resulting /// AffineApplyOp do not change the length of AffineApplyOp chains. AffineApplyOp makeComposedAffineApply(OpBuilder &b, Location loc, AffineMap map, - ArrayRef operands); + ValueRange operands); +/// Variant of `makeComposedAffineApply` which infers the AffineMap from `e`. +AffineApplyOp makeComposedAffineApply(OpBuilder &b, Location loc, AffineExpr e, + ValueRange values); /// Given an affine map `map` and its input `operands`, this method composes /// into `map`, maps of AffineApplyOps whose results are the values in diff --git a/mlir/include/mlir/Dialect/Vector/EDSC/Builders.h b/mlir/include/mlir/Dialect/Vector/EDSC/Builders.h deleted file mode 100644 index 65767af61b2d..000000000000 --- a/mlir/include/mlir/Dialect/Vector/EDSC/Builders.h +++ /dev/null @@ -1,52 +0,0 @@ -//===- Builders.h - MLIR Declarative Vector Builders ------------*- 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 -// -//===----------------------------------------------------------------------===// -// -// Provides intuitive composable interfaces for building structured MLIR -// snippets in a declarative fashion. -// -//===----------------------------------------------------------------------===// -#ifndef MLIR_DIALECT_VECTOR_EDSC_BUILDERS_H_ -#define MLIR_DIALECT_VECTOR_EDSC_BUILDERS_H_ - -#include "mlir/Dialect/Utils/StructuredOpsUtils.h" -#include "mlir/Dialect/Vector/VectorOps.h" -#include "mlir/EDSC/Builders.h" -#include "mlir/IR/AffineExpr.h" -#include "mlir/IR/Builders.h" - -namespace mlir { -namespace edsc { -namespace ops { - -/// Build a generic vector contraction, that is a `vector.contract` op with -/// specified `iteratorTypes`. The client is responsible for specifying proper -/// indexings when creating the StructuredIndexed. -/// The computation represents a notional (A * B + C) where indexings specify -/// which dimensions are reduced and reordered. -/// Return the result of the `vector.contract` op -/// -/// Prerequisites: -/// A, B and C capture values of proper vector types, and indexing expressions -/// that match semantics of the `vector.contract` op. -Value vector_contraction(StructuredIndexed A, StructuredIndexed B, - StructuredIndexed C, - ArrayRef iteratorTypes); - -/// Build a generic vector contraction that computes a matmul on vectors. -/// Return the result of C(i, j) + sum_k {A(i, k) * B(k, j)} on vectors. -/// -/// Prerequisites: -/// A, B and C capture values of proper vector types. For instance -/// `A: vector<4x8xf32>`, `B: vector<8x16f32>` and `C: vector<4x16xf32>`. -Value vector_contraction_matmul(Value A, Value B, Value C); - -} // namespace ops -} // namespace edsc -} // namespace mlir - -#endif // MLIR_DIALECT_VECTOR_EDSC_BUILDERS_H_ diff --git a/mlir/include/mlir/Dialect/Vector/EDSC/Intrinsics.h b/mlir/include/mlir/Dialect/Vector/EDSC/Intrinsics.h deleted file mode 100644 index 269d9c1b27af..000000000000 --- a/mlir/include/mlir/Dialect/Vector/EDSC/Intrinsics.h +++ /dev/null @@ -1,41 +0,0 @@ -//===- Intrinsics.h - MLIR EDSC Intrinsics for Vector -----------*- 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 -// -//===----------------------------------------------------------------------===// -#ifndef MLIR_DIALECT_VECTOR_EDSC_INTRINSICS_H_ -#define MLIR_DIALECT_VECTOR_EDSC_INTRINSICS_H_ - -#include "mlir/Dialect/Vector/EDSC/Builders.h" - -namespace mlir { -namespace edsc { -namespace intrinsics { - -using vector_broadcast = ValueBuilder; -using vector_contract = ValueBuilder; -using vector_extract = ValueBuilder; -using vector_extract_element = ValueBuilder; -using vector_extract_slices = ValueBuilder; -using vector_extract_strided_slice = - ValueBuilder; -using vector_fma = ValueBuilder; -using vector_insert = ValueBuilder; -using vector_insert_element = ValueBuilder; -using vector_insert_slices = ValueBuilder; -using vector_insert_strided_slice = ValueBuilder; -using vector_matmul = ValueBuilder; -using vector_outerproduct = ValueBuilder; -using vector_print = OperationBuilder; -using vector_transfer_read = ValueBuilder; -using vector_transfer_write = OperationBuilder; -using vector_transpose = ValueBuilder; -using vector_type_cast = ValueBuilder; - -} // namespace intrinsics -} // namespace edsc -} // namespace mlir - -#endif // MLIR_DIALECT_VECTOR_EDSC_INTRINSICS_H_ diff --git a/mlir/lib/Conversion/VectorToSCF/VectorToSCF.cpp b/mlir/lib/Conversion/VectorToSCF/VectorToSCF.cpp index 54783a7a3992..bef847c1ffac 100644 --- a/mlir/lib/Conversion/VectorToSCF/VectorToSCF.cpp +++ b/mlir/lib/Conversion/VectorToSCF/VectorToSCF.cpp @@ -15,21 +15,18 @@ #include "mlir/Conversion/VectorToSCF/VectorToSCF.h" #include "../PassDetail.h" -#include "mlir/Dialect/Affine/EDSC/Intrinsics.h" -#include "mlir/Dialect/MemRef/EDSC/Intrinsics.h" -#include "mlir/Dialect/SCF/EDSC/Intrinsics.h" -#include "mlir/Dialect/StandardOps/EDSC/Intrinsics.h" -#include "mlir/Dialect/Vector/EDSC/Intrinsics.h" +#include "mlir/Dialect/Affine/IR/AffineOps.h" +#include "mlir/Dialect/Affine/Utils.h" +#include "mlir/Dialect/SCF/SCF.h" #include "mlir/Dialect/Vector/VectorOps.h" #include "mlir/Dialect/Vector/VectorUtils.h" #include "mlir/IR/Builders.h" +#include "mlir/IR/ImplicitLocOpBuilder.h" #include "mlir/Pass/Pass.h" #include "mlir/Transforms/GreedyPatternRewriteDriver.h" #include "mlir/Transforms/Passes.h" using namespace mlir; -using namespace mlir::edsc; -using namespace mlir::edsc::intrinsics; using vector::TransferReadOp; using vector::TransferWriteOp; @@ -67,10 +64,10 @@ static Optional unpackedDim(OpTy xferOp) { /// map is identical to the current permutation map, but the first result is /// omitted. template -static AffineMap unpackedPermutationMap(OpTy xferOp, OpBuilder &builder) { +static AffineMap unpackedPermutationMap(OpBuilder &b, OpTy xferOp) { auto map = xferOp.permutation_map(); return AffineMap::get(map.getNumDims(), 0, map.getResults().drop_front(), - builder.getContext()); + b.getContext()); } /// Calculate the indices for the new vector transfer op. @@ -80,7 +77,7 @@ static AffineMap unpackedPermutationMap(OpTy xferOp, OpBuilder &builder) { /// ^^^^^^ /// `iv` is the iteration variable of the (new) surrounding loop. template -static void getXferIndices(OpTy xferOp, Value iv, +static void getXferIndices(OpBuilder &b, OpTy xferOp, Value iv, SmallVector &indices) { typename OpTy::Adaptor adaptor(xferOp); // Corresponding memref dim of the vector dim that is unpacked. @@ -88,19 +85,23 @@ static void getXferIndices(OpTy xferOp, Value iv, auto prevIndices = adaptor.indices(); indices.append(prevIndices.begin(), prevIndices.end()); + Location loc = xferOp.getLoc(); bool isBroadcast = !dim.hasValue(); if (!isBroadcast) { - using edsc::op::operator+; - indices[dim.getValue()] = adaptor.indices()[dim.getValue()] + iv; + AffineExpr d0, d1; + bindDims(xferOp.getContext(), d0, d1); + Value offset = adaptor.indices()[dim.getValue()]; + indices[dim.getValue()] = + makeComposedAffineApply(b, loc, d0 + d1, {offset, iv}); } } -static void maybeYieldValue(bool hasRetVal, OpBuilder builder, Location loc, +static void maybeYieldValue(OpBuilder &b, Location loc, bool hasRetVal, Value value) { if (hasRetVal) { - builder.create(loc, value); + b.create(loc, value); } else { - builder.create(loc); + b.create(loc); } } @@ -111,7 +112,7 @@ static void maybeYieldValue(bool hasRetVal, OpBuilder builder, Location loc, /// computed and attached to the new transfer op in the pattern.) /// * The to-be-unpacked dim of xferOp is a broadcast. template -static Value generateMaskCheck(OpBuilder &builder, OpTy xferOp, Value iv) { +static Value generateMaskCheck(OpBuilder &b, OpTy xferOp, Value iv) { if (!xferOp.mask()) return Value(); if (xferOp.getMaskType().getRank() != 1) @@ -119,8 +120,10 @@ static Value generateMaskCheck(OpBuilder &builder, OpTy xferOp, Value iv) { if (xferOp.isBroadcastDim(0)) return Value(); - auto ivI32 = std_index_cast(IntegerType::get(builder.getContext(), 32), iv); - return vector_extract_element(xferOp.mask(), ivI32).value; + Location loc = xferOp.getLoc(); + Value ivI32 = + b.create(loc, IntegerType::get(b.getContext(), 32), iv); + return b.create(loc, xferOp.mask(), ivI32); } /// Helper function TransferOpConversion and TransferOp1dConversion. @@ -149,7 +152,7 @@ static Value generateMaskCheck(OpBuilder &builder, OpTy xferOp, Value iv) { /// `resultTypes`. template static Value generateInBoundsCheck( - OpTy xferOp, Value iv, OpBuilder &builder, Optional dim, + OpBuilder &b, OpTy xferOp, Value iv, Optional dim, TypeRange resultTypes, function_ref inBoundsCase, function_ref outOfBoundsCase = nullptr) { @@ -158,38 +161,39 @@ static Value generateInBoundsCheck( // Condition check 1: Access in-bounds? bool isBroadcast = !dim.hasValue(); // No in-bounds check for broadcasts. + Location loc = xferOp.getLoc(); + ImplicitLocOpBuilder lb(xferOp.getLoc(), b); if (!xferOp.isDimInBounds(0) && !isBroadcast) { - auto memrefDim = - memref_dim(xferOp.source(), std_constant_index(dim.getValue())); - using edsc::op::operator+; - auto memrefIdx = xferOp.indices()[dim.getValue()] + iv; - cond = std_cmpi_sgt(memrefDim.value, memrefIdx); + Value memrefDim = lb.create(xferOp.source(), *dim); + AffineExpr d0, d1; + bindDims(xferOp.getContext(), d0, d1); + Value base = xferOp.indices()[dim.getValue()]; + Value memrefIdx = makeComposedAffineApply(b, loc, d0 + d1, {base, iv}); + cond = lb.create(CmpIPredicate::sgt, memrefDim, memrefIdx); } // Condition check 2: Masked in? - if (auto maskCond = generateMaskCheck(builder, xferOp, iv)) { - if (cond) { - cond = builder.create(xferOp.getLoc(), cond, maskCond); - } else { + if (auto maskCond = generateMaskCheck(b, xferOp, iv)) { + if (cond) + cond = lb.create(cond, maskCond); + else cond = maskCond; - } } // If the condition is non-empty, generate an SCF::IfOp. if (cond) { - auto check = builder.create( - xferOp.getLoc(), resultTypes, cond, + auto check = lb.create( + resultTypes, cond, /*thenBuilder=*/ - [&](OpBuilder &builder, Location loc) { - maybeYieldValue(hasRetVal, builder, loc, inBoundsCase(builder, loc)); + [&](OpBuilder &b, Location loc) { + maybeYieldValue(b, loc, hasRetVal, inBoundsCase(b, loc)); }, /*elseBuilder=*/ - [&](OpBuilder &builder, Location loc) { + [&](OpBuilder &b, Location loc) { if (outOfBoundsCase) { - maybeYieldValue(hasRetVal, builder, loc, - outOfBoundsCase(builder, loc)); + maybeYieldValue(b, loc, hasRetVal, outOfBoundsCase(b, loc)); } else { - builder.create(loc); + b.create(loc); } }); @@ -197,45 +201,45 @@ static Value generateInBoundsCheck( } // Condition is empty, no need for an SCF::IfOp. - return inBoundsCase(builder, xferOp.getLoc()); + return inBoundsCase(b, loc); } /// In this function variant, `inBoundsCase` and `outOfBoundsCase` do not have /// a return value. Consequently, this function does not have a return value. template static void generateInBoundsCheck( - OpTy xferOp, Value iv, OpBuilder &builder, Optional dim, + OpBuilder &b, OpTy xferOp, Value iv, Optional dim, function_ref inBoundsCase, function_ref outOfBoundsCase = nullptr) { generateInBoundsCheck( - xferOp, iv, builder, dim, /*resultTypes=*/TypeRange(), + b, xferOp, iv, dim, /*resultTypes=*/TypeRange(), /*inBoundsCase=*/ - [&](OpBuilder &builder, Location loc) { - inBoundsCase(builder, loc); + [&](OpBuilder &b, Location loc) { + inBoundsCase(b, loc); return Value(); }, /*outOfBoundsCase=*/ - [&](OpBuilder &builder, Location loc) { + [&](OpBuilder &b, Location loc) { if (outOfBoundsCase) - outOfBoundsCase(builder, loc); + outOfBoundsCase(b, loc); return Value(); }); } /// Given an ArrayAttr, return a copy where the first element is dropped. -static ArrayAttr dropFirstElem(OpBuilder &builder, ArrayAttr attr) { +static ArrayAttr dropFirstElem(OpBuilder &b, ArrayAttr attr) { if (!attr) return attr; - return ArrayAttr::get(builder.getContext(), attr.getValue().drop_front()); + return ArrayAttr::get(b.getContext(), attr.getValue().drop_front()); } /// Add the pass label to a vector transfer op if its rank is not the target /// rank. template -static void maybeApplyPassLabel(OpBuilder &builder, OpTy newXferOp, +static void maybeApplyPassLabel(OpBuilder &b, OpTy newXferOp, unsigned targetRank) { if (newXferOp.getVectorType().getRank() > targetRank) - newXferOp->setAttr(kPassLabel, builder.getUnitAttr()); + newXferOp->setAttr(kPassLabel, b.getUnitAttr()); } namespace lowering_n_d { @@ -249,8 +253,8 @@ struct BufferAllocs { /// Allocate temporary buffers for data (vector) and mask (if present). /// TODO: Parallelism and threadlocal considerations. template -static BufferAllocs allocBuffers(OpTy xferOp) { - auto &b = ScopedContext::getBuilderRef(); +static BufferAllocs allocBuffers(OpBuilder &b, OpTy xferOp) { + Location loc = xferOp.getLoc(); OpBuilder::InsertionGuard guard(b); Operation *scope = xferOp->template getParentWithTrait(); @@ -259,14 +263,14 @@ static BufferAllocs allocBuffers(OpTy xferOp) { BufferAllocs result; auto bufferType = MemRefType::get({}, xferOp.getVectorType()); - result.dataBuffer = memref_alloca(bufferType).value; + result.dataBuffer = b.create(loc, bufferType); if (xferOp.mask()) { auto maskType = MemRefType::get({}, xferOp.mask().getType()); - auto maskBuffer = memref_alloca(maskType).value; + auto maskBuffer = b.create(loc, maskType); b.setInsertionPoint(xferOp); - memref_store(xferOp.mask(), maskBuffer); - result.maskBuffer = memref_load(maskBuffer); + b.create(loc, xferOp.mask(), maskBuffer); + result.maskBuffer = b.create(loc, maskBuffer); } return result; @@ -359,7 +363,7 @@ struct Strategy { /// Note: The loop and type cast are generated in TransferOpConversion. /// The original TransferReadOp and store op are deleted in `cleanup`. /// Note: The `mask` operand is set in TransferOpConversion. - static TransferReadOp rewriteOp(OpBuilder &builder, + static TransferReadOp rewriteOp(OpBuilder &b, VectorTransferToSCFOptions options, TransferReadOp xferOp, Value buffer, Value iv) { @@ -368,39 +372,36 @@ struct Strategy { storeIndices.push_back(iv); SmallVector xferIndices; - getXferIndices(xferOp, iv, xferIndices); + getXferIndices(b, xferOp, iv, xferIndices); + Location loc = xferOp.getLoc(); auto bufferType = buffer.getType().dyn_cast(); auto vecType = bufferType.getElementType().dyn_cast(); - auto inBoundsAttr = dropFirstElem(builder, xferOp.in_boundsAttr()); - auto newXfer = - vector_transfer_read( - vecType, xferOp.source(), xferIndices, - AffineMapAttr::get(unpackedPermutationMap(xferOp, builder)), - xferOp.padding(), Value(), inBoundsAttr) - .value; + auto inBoundsAttr = dropFirstElem(b, xferOp.in_boundsAttr()); + auto newXferOp = b.create( + loc, vecType, xferOp.source(), xferIndices, + AffineMapAttr::get(unpackedPermutationMap(b, xferOp)), xferOp.padding(), + Value(), inBoundsAttr); - maybeApplyPassLabel(builder, - dyn_cast(newXfer.getDefiningOp()), - options.targetRank); + maybeApplyPassLabel(b, newXferOp, options.targetRank); - memref_store(newXfer, buffer, storeIndices); - return newXfer.getDefiningOp(); + b.create(loc, newXferOp.vector(), buffer, storeIndices); + return newXferOp; } /// Handle out-of-bounds accesses on the to-be-unpacked dimension: Write /// padding value to the temporary buffer. - static void handleOutOfBoundsDim(OpBuilder & /*builder*/, - TransferReadOp xferOp, Value buffer, - Value iv) { + static void handleOutOfBoundsDim(OpBuilder &b, TransferReadOp xferOp, + Value buffer, Value iv) { SmallVector storeIndices; getBufferIndices(xferOp, storeIndices); storeIndices.push_back(iv); + Location loc = xferOp.getLoc(); auto bufferType = buffer.getType().dyn_cast(); auto vecType = bufferType.getElementType().dyn_cast(); - auto vec = std_splat(vecType, xferOp.padding()); - memref_store(vec, buffer, storeIndices); + auto vec = b.create(loc, vecType, xferOp.padding()); + b.create(loc, vec, buffer, storeIndices); } /// Cleanup after rewriting the op. @@ -443,7 +444,7 @@ struct Strategy { /// to memory. /// /// Note: For more details, see comments on Strategy. - static TransferWriteOp rewriteOp(OpBuilder &builder, + static TransferWriteOp rewriteOp(OpBuilder &b, VectorTransferToSCFOptions options, TransferWriteOp xferOp, Value buffer, Value iv) { @@ -452,22 +453,23 @@ struct Strategy { loadIndices.push_back(iv); SmallVector xferIndices; - getXferIndices(xferOp, iv, xferIndices); + getXferIndices(b, xferOp, iv, xferIndices); - auto vec = memref_load(buffer, loadIndices); - auto inBoundsAttr = dropFirstElem(builder, xferOp.in_boundsAttr()); - auto newXfer = vector_transfer_write( - Type(), vec, xferOp.source(), xferIndices, - AffineMapAttr::get(unpackedPermutationMap(xferOp, builder)), Value(), + Location loc = xferOp.getLoc(); + auto vec = b.create(loc, buffer, loadIndices); + auto inBoundsAttr = dropFirstElem(b, xferOp.in_boundsAttr()); + auto newXferOp = b.create( + loc, Type(), vec, xferOp.source(), xferIndices, + AffineMapAttr::get(unpackedPermutationMap(b, xferOp)), Value(), inBoundsAttr); - maybeApplyPassLabel(builder, newXfer.op, options.targetRank); + maybeApplyPassLabel(b, newXferOp, options.targetRank); - return newXfer; + return newXferOp; } /// Handle out-of-bounds accesses on the to-be-unpacked dimension. - static void handleOutOfBoundsDim(OpBuilder &builder, TransferWriteOp xferOp, + static void handleOutOfBoundsDim(OpBuilder &b, TransferWriteOp xferOp, Value buffer, Value iv) {} /// Cleanup after rewriting the op. @@ -518,8 +520,7 @@ struct PrepareTransferReadConversion if (checkPrepareXferOp(xferOp, options).failed()) return failure(); - ScopedContext scope(rewriter, xferOp.getLoc()); - auto buffers = allocBuffers(xferOp); + auto buffers = allocBuffers(rewriter, xferOp); auto *newXfer = rewriter.clone(*xferOp.getOperation()); newXfer->setAttr(kPassLabel, rewriter.getUnitAttr()); if (xferOp.mask()) { @@ -527,7 +528,9 @@ struct PrepareTransferReadConversion buffers.maskBuffer); } - memref_store(newXfer->getResult(0), buffers.dataBuffer); + Location loc = xferOp.getLoc(); + rewriter.create(loc, newXfer->getResult(0), + buffers.dataBuffer); rewriter.replaceOpWithNewOp(xferOp, buffers.dataBuffer); return success(); @@ -566,10 +569,10 @@ struct PrepareTransferWriteConversion if (checkPrepareXferOp(xferOp, options).failed()) return failure(); - ScopedContext scope(rewriter, xferOp.getLoc()); - auto buffers = allocBuffers(xferOp); - memref_store(xferOp.vector(), buffers.dataBuffer); - auto loadedVec = memref_load(buffers.dataBuffer); + Location loc = xferOp.getLoc(); + auto buffers = allocBuffers(rewriter, xferOp); + rewriter.create(loc, xferOp.vector(), buffers.dataBuffer); + auto loadedVec = rewriter.create(loc, buffers.dataBuffer); rewriter.updateRootInPlace(xferOp, [&]() { xferOp.vectorMutable().assign(loadedVec); xferOp->setAttr(kPassLabel, rewriter.getUnitAttr()); @@ -610,13 +613,13 @@ struct TransferOpConversion : public VectorToSCFPattern { if (!xferOp->hasAttr(kPassLabel)) return failure(); - ScopedContext scope(rewriter, xferOp.getLoc()); - // Find and cast data buffer. How the buffer can be found depends on OpTy. + ImplicitLocOpBuilder locB(xferOp.getLoc(), rewriter); auto dataBuffer = Strategy::getBuffer(xferOp); auto dataBufferType = dataBuffer.getType().template dyn_cast(); auto castedDataType = unpackOneDim(dataBufferType); - auto castedDataBuffer = vector_type_cast(castedDataType, dataBuffer); + auto castedDataBuffer = + locB.create(castedDataType, dataBuffer); // If the xferOp has a mask: Find and cast mask buffer. Value castedMaskBuffer; @@ -633,26 +636,25 @@ struct TransferOpConversion : public VectorToSCFPattern { castedMaskBuffer = maskBuffer; } else { auto castedMaskType = unpackOneDim(maskBufferType); - castedMaskBuffer = vector_type_cast(castedMaskType, maskBuffer); + castedMaskBuffer = + locB.create(castedMaskType, maskBuffer); } } // Loop bounds and step. - auto lb = std_constant_index(0).value; - auto ub = std_constant_index( - castedDataType.getDimSize(castedDataType.getRank() - 1)) - .value; - auto step = std_constant_index(1).value; + auto lb = locB.create(0); + auto ub = locB.create( + castedDataType.getDimSize(castedDataType.getRank() - 1)); + auto step = locB.create(1); // Generate for loop. - rewriter.create( - xferOp.getLoc(), lb, ub, step, ValueRange(), + locB.create( + lb, ub, step, ValueRange(), [&](OpBuilder &b, Location loc, Value iv, ValueRange /*loopState*/) { - ScopedContext scope(b, loc); generateInBoundsCheck( - xferOp, iv, b, unpackedDim(xferOp), + b, xferOp, iv, unpackedDim(xferOp), /*inBoundsCase=*/ - [&](OpBuilder &b, Location /*loc*/) { + [&](OpBuilder &b, Location loc) { // Create new transfer op. OpTy newXfer = Strategy::rewriteOp( b, this->options, xferOp, castedDataBuffer, iv); @@ -674,7 +676,8 @@ struct TransferOpConversion : public VectorToSCFPattern { if (!xferOp.isBroadcastDim(0)) loadIndices.push_back(iv); - auto mask = memref_load(castedMaskBuffer, loadIndices); + auto mask = b.create(loc, castedMaskBuffer, + loadIndices); rewriter.updateRootInPlace( newXfer, [&]() { newXfer.maskMutable().assign(mask); }); } @@ -699,7 +702,7 @@ namespace lowering_n_d_unrolled { /// If the original transfer op has a mask, compute the mask of the new transfer /// op (for the current iteration `i`) and assign it. template -static void maybeAssignMask(OpBuilder &builder, OpTy xferOp, OpTy newXferOp, +static void maybeAssignMask(OpBuilder &b, OpTy xferOp, OpTy newXferOp, int64_t i) { if (!xferOp.mask()) return; @@ -713,11 +716,12 @@ static void maybeAssignMask(OpBuilder &builder, OpTy xferOp, OpTy newXferOp, if (xferOp.getMaskType().getRank() > 1) { // Unpack one dimension of the mask. - OpBuilder::InsertionGuard guard(builder); - builder.setInsertionPoint(newXferOp); // Insert load before newXfer. + OpBuilder::InsertionGuard guard(b); + b.setInsertionPoint(newXferOp); // Insert load before newXfer. llvm::SmallVector indices({i}); - auto newMask = vector_extract(xferOp.mask(), indices).value; + Location loc = xferOp.getLoc(); + auto newMask = b.create(loc, xferOp.mask(), indices); newXferOp.maskMutable().assign(newMask); } @@ -764,7 +768,9 @@ struct UnrollTransferReadConversion PatternRewriter &rewriter) const { if (auto insertOp = getInsertOp(xferOp)) return insertOp.dest(); - return std_splat(xferOp.getVectorType(), xferOp.padding()).value; + Location loc = xferOp.getLoc(); + return rewriter.create(loc, xferOp.getVectorType(), + xferOp.padding()); } /// If the result of the TransferReadOp has exactly one user, which is a @@ -797,7 +803,6 @@ struct UnrollTransferReadConversion if (xferOp.getVectorType().getRank() <= options.targetRank) return failure(); - ScopedContext scope(rewriter, xferOp.getLoc()); auto insertOp = getInsertOp(xferOp); auto vec = getResultVector(xferOp, rewriter); auto vecType = vec.getType().dyn_cast(); @@ -807,18 +812,17 @@ struct UnrollTransferReadConversion int64_t dimSize = xferVecType.getShape()[0]; // Generate fully unrolled loop of transfer ops. + Location loc = xferOp.getLoc(); for (int64_t i = 0; i < dimSize; ++i) { - Value iv = std_constant_index(i); + Value iv = rewriter.create(loc, i); vec = generateInBoundsCheck( - xferOp, iv, rewriter, unpackedDim(xferOp), TypeRange(vecType), + rewriter, xferOp, iv, unpackedDim(xferOp), TypeRange(vecType), /*inBoundsCase=*/ [&](OpBuilder &b, Location loc) { - ScopedContext scope(b, loc); - // Indices for the new transfer op. SmallVector xferIndices; - getXferIndices(xferOp, iv, xferIndices); + getXferIndices(b, xferOp, iv, xferIndices); // Indices for the new vector.insert op. SmallVector insertionIndices; @@ -826,18 +830,13 @@ struct UnrollTransferReadConversion insertionIndices.push_back(i); auto inBoundsAttr = dropFirstElem(b, xferOp.in_boundsAttr()); - auto newXferOpVal = - vector_transfer_read( - newXferVecType, xferOp.source(), xferIndices, - AffineMapAttr::get(unpackedPermutationMap(xferOp, b)), - xferOp.padding(), Value(), inBoundsAttr) - .value; - auto newXferOp = - dyn_cast(newXferOpVal.getDefiningOp()); - + auto newXferOp = b.create( + loc, newXferVecType, xferOp.source(), xferIndices, + AffineMapAttr::get(unpackedPermutationMap(b, xferOp)), + xferOp.padding(), Value(), inBoundsAttr); maybeAssignMask(b, xferOp, newXferOp, i); - - return vector_insert(newXferOp, vec, insertionIndices).value; + return b.create(loc, newXferOp, vec, + insertionIndices); }, /*outOfBoundsCase=*/ [&](OpBuilder &b, Location loc) { @@ -920,38 +919,35 @@ struct UnrollTransferWriteConversion if (xferOp.getVectorType().getRank() <= options.targetRank) return failure(); - ScopedContext scope(rewriter, xferOp.getLoc()); auto vec = getDataVector(xferOp); auto xferVecType = xferOp.getVectorType(); int64_t dimSize = xferVecType.getShape()[0]; // Generate fully unrolled loop of transfer ops. + Location loc = xferOp.getLoc(); for (int64_t i = 0; i < dimSize; ++i) { - Value iv = std_constant_index(i); + Value iv = rewriter.create(loc, i); generateInBoundsCheck( - xferOp, iv, rewriter, unpackedDim(xferOp), + rewriter, xferOp, iv, unpackedDim(xferOp), /*inBoundsCase=*/[&](OpBuilder &b, Location loc) { - ScopedContext scope(b, loc); - // Indices for the new transfer op. SmallVector xferIndices; - getXferIndices(xferOp, iv, xferIndices); + getXferIndices(b, xferOp, iv, xferIndices); // Indices for the new vector.extract op. SmallVector extractionIndices; getExtractionIndices(xferOp, extractionIndices); extractionIndices.push_back(i); - auto extracted = vector_extract(vec, extractionIndices).value; + auto extracted = + b.create(loc, vec, extractionIndices); auto inBoundsAttr = dropFirstElem(b, xferOp.in_boundsAttr()); - auto newXferOp = - vector_transfer_write( - Type(), extracted, xferOp.source(), xferIndices, - AffineMapAttr::get(unpackedPermutationMap(xferOp, b)), - Value(), inBoundsAttr) - .op; + auto newXferOp = b.create( + loc, Type(), extracted, xferOp.source(), xferIndices, + AffineMapAttr::get(unpackedPermutationMap(b, xferOp)), Value(), + inBoundsAttr); maybeAssignMask(b, xferOp, newXferOp, i); }); @@ -971,7 +967,7 @@ namespace lowering_1_d { /// the transfer is operating. A return value of None indicates a broadcast. template static Optional -get1dMemrefIndices(OpTy xferOp, Value iv, +get1dMemrefIndices(OpBuilder &b, OpTy xferOp, Value iv, SmallVector &memrefIndices) { auto indices = xferOp.indices(); auto map = xferOp.permutation_map(); @@ -980,9 +976,12 @@ get1dMemrefIndices(OpTy xferOp, Value iv, assert(map.getNumResults() == 1 && "Expected 1 permutation map result for 1D transfer"); if (auto expr = map.getResult(0).template dyn_cast()) { + Location loc = xferOp.getLoc(); auto dim = expr.getPosition(); - using edsc::op::operator+; - memrefIndices[dim] = memrefIndices[dim] + iv; + AffineExpr d0, d1; + bindDims(xferOp.getContext(), d0, d1); + Value offset = memrefIndices[dim]; + memrefIndices[dim] = makeComposedAffineApply(b, loc, d0 + d1, {offset, iv}); return dim; } @@ -999,55 +998,61 @@ struct Strategy1d; /// Codegen strategy for TransferReadOp. template <> struct Strategy1d { - static void generateForLoopBody(OpBuilder &builder, Location loc, + static void generateForLoopBody(OpBuilder &b, Location loc, TransferReadOp xferOp, Value iv, ValueRange loopState) { SmallVector indices; - auto dim = get1dMemrefIndices(xferOp, iv, indices); - auto ivI32 = std_index_cast(IntegerType::get(builder.getContext(), 32), iv); + auto dim = get1dMemrefIndices(b, xferOp, iv, indices); + Value ivI32 = + b.create(loc, IntegerType::get(b.getContext(), 32), iv); auto vec = loopState[0]; // In case of out-of-bounds access, leave `vec` as is (was initialized with // padding value). auto nextVec = generateInBoundsCheck( - xferOp, iv, builder, dim, TypeRange(xferOp.getVectorType()), + b, xferOp, iv, dim, TypeRange(xferOp.getVectorType()), /*inBoundsCase=*/ - [&](OpBuilder & /*b*/, Location loc) { - auto val = memref_load(xferOp.source(), indices); - return vector_insert_element(val, vec, ivI32.value).value; + [&](OpBuilder &b, Location loc) { + Value val = b.create(loc, xferOp.source(), indices); + return b.create(loc, val, vec, ivI32); }, /*outOfBoundsCase=*/ [&](OpBuilder & /*b*/, Location loc) { return vec; }); - builder.create(loc, nextVec); + b.create(loc, nextVec); } - static Value initialLoopState(TransferReadOp xferOp) { + static Value initialLoopState(OpBuilder &b, TransferReadOp xferOp) { // Inititalize vector with padding value. - return std_splat(xferOp.getVectorType(), xferOp.padding()).value; + Location loc = xferOp.getLoc(); + return b.create(loc, xferOp.getVectorType(), xferOp.padding()); } }; /// Codegen strategy for TransferWriteOp. template <> struct Strategy1d { - static void generateForLoopBody(OpBuilder &builder, Location loc, + static void generateForLoopBody(OpBuilder &b, Location loc, TransferWriteOp xferOp, Value iv, ValueRange /*loopState*/) { SmallVector indices; - auto dim = get1dMemrefIndices(xferOp, iv, indices); - auto ivI32 = std_index_cast(IntegerType::get(builder.getContext(), 32), iv); + auto dim = get1dMemrefIndices(b, xferOp, iv, indices); + Value ivI32 = + b.create(loc, IntegerType::get(b.getContext(), 32), iv); // Nothing to do in case of out-of-bounds access. generateInBoundsCheck( - xferOp, iv, builder, dim, - /*inBoundsCase=*/[&](OpBuilder & /*b*/, Location loc) { - auto val = vector_extract_element(xferOp.vector(), ivI32.value); - memref_store(val, xferOp.source(), indices); + b, xferOp, iv, dim, + /*inBoundsCase=*/[&](OpBuilder &b, Location loc) { + auto val = + b.create(loc, xferOp.vector(), ivI32); + b.create(loc, val, xferOp.source(), indices); }); - builder.create(loc); + b.create(loc); } - static Value initialLoopState(TransferWriteOp xferOp) { return Value(); } + static Value initialLoopState(OpBuilder &b, TransferWriteOp xferOp) { + return Value(); + } }; /// Return true if the last dimension of the MemRefType has unit stride. @@ -1095,7 +1100,6 @@ struct TransferOp1dConversion : public VectorToSCFPattern { LogicalResult matchAndRewrite(OpTy xferOp, PatternRewriter &rewriter) const override { - ScopedContext scope(rewriter, xferOp.getLoc()); auto map = xferOp.permutation_map(); auto memRefType = xferOp.getShapedType().template dyn_cast(); @@ -1107,19 +1111,18 @@ struct TransferOp1dConversion : public VectorToSCFPattern { return failure(); // Handled by ConvertVectorToLLVM // Loop bounds, step, state... + Location loc = xferOp.getLoc(); auto vecType = xferOp.getVectorType(); - auto lb = std_constant_index(0); - auto ub = std_constant_index(vecType.getDimSize(0)); - auto step = std_constant_index(1); - auto loopState = Strategy1d::initialLoopState(xferOp); + auto lb = rewriter.create(loc, 0); + auto ub = rewriter.create(loc, vecType.getDimSize(0)); + auto step = rewriter.create(loc, 1); + auto loopState = Strategy1d::initialLoopState(rewriter, xferOp); // Generate for loop. rewriter.replaceOpWithNewOp( xferOp, lb, ub, step, loopState ? ValueRange(loopState) : ValueRange(), - [&](OpBuilder &builder, Location loc, Value iv, ValueRange loopState) { - ScopedContext nestedScope(builder, loc); - Strategy1d::generateForLoopBody(builder, loc, xferOp, iv, - loopState); + [&](OpBuilder &b, Location loc, Value iv, ValueRange loopState) { + Strategy1d::generateForLoopBody(b, loc, xferOp, iv, loopState); }); return success(); diff --git a/mlir/lib/Dialect/Affine/IR/AffineOps.cpp b/mlir/lib/Dialect/Affine/IR/AffineOps.cpp index 6063bb5d835d..b267223b423e 100644 --- a/mlir/lib/Dialect/Affine/IR/AffineOps.cpp +++ b/mlir/lib/Dialect/Affine/IR/AffineOps.cpp @@ -698,7 +698,7 @@ void mlir::fullyComposeAffineMapAndOperands(AffineMap *map, AffineApplyOp mlir::makeComposedAffineApply(OpBuilder &b, Location loc, AffineMap map, - ArrayRef operands) { + ValueRange operands) { AffineMap normalizedMap = map; SmallVector normalizedOperands(operands.begin(), operands.end()); composeAffineMapAndOperands(&normalizedMap, &normalizedOperands); @@ -706,6 +706,13 @@ AffineApplyOp mlir::makeComposedAffineApply(OpBuilder &b, Location loc, return b.create(loc, normalizedMap, normalizedOperands); } +AffineApplyOp mlir::makeComposedAffineApply(OpBuilder &b, Location loc, + AffineExpr e, ValueRange values) { + return makeComposedAffineApply( + b, loc, AffineMap::inferFromExprList(ArrayRef{e}).front(), + values); +} + // A symbol may appear as a dim in affine.apply operations. This function // canonicalizes dims that are valid symbols into actual symbols. template diff --git a/mlir/lib/Dialect/Linalg/Transforms/Interchange.cpp b/mlir/lib/Dialect/Linalg/Transforms/Interchange.cpp index e03d8cb01bc1..842fccfda218 100644 --- a/mlir/lib/Dialect/Linalg/Transforms/Interchange.cpp +++ b/mlir/lib/Dialect/Linalg/Transforms/Interchange.cpp @@ -16,7 +16,6 @@ #include "mlir/Dialect/Linalg/Utils/Utils.h" #include "mlir/Dialect/StandardOps/EDSC/Intrinsics.h" #include "mlir/Dialect/Utils/StructuredOpsUtils.h" -#include "mlir/Dialect/Vector/EDSC/Intrinsics.h" #include "mlir/Dialect/Vector/VectorOps.h" #include "mlir/IR/AffineExpr.h" #include "mlir/IR/Matchers.h" diff --git a/mlir/lib/Dialect/Linalg/Transforms/Transforms.cpp b/mlir/lib/Dialect/Linalg/Transforms/Transforms.cpp index 736d298a713a..f0ea4e8423a5 100644 --- a/mlir/lib/Dialect/Linalg/Transforms/Transforms.cpp +++ b/mlir/lib/Dialect/Linalg/Transforms/Transforms.cpp @@ -18,7 +18,6 @@ #include "mlir/Dialect/Linalg/Utils/Utils.h" #include "mlir/Dialect/StandardOps/EDSC/Intrinsics.h" #include "mlir/Dialect/Utils/StructuredOpsUtils.h" -#include "mlir/Dialect/Vector/EDSC/Intrinsics.h" #include "mlir/Dialect/Vector/VectorOps.h" #include "mlir/IR/AffineExpr.h" #include "mlir/IR/Matchers.h" diff --git a/mlir/lib/Dialect/Linalg/Transforms/Vectorization.cpp b/mlir/lib/Dialect/Linalg/Transforms/Vectorization.cpp index aee5233f4930..5ceb56868a17 100644 --- a/mlir/lib/Dialect/Linalg/Transforms/Vectorization.cpp +++ b/mlir/lib/Dialect/Linalg/Transforms/Vectorization.cpp @@ -17,7 +17,6 @@ #include "mlir/Dialect/Linalg/Utils/Utils.h" #include "mlir/Dialect/StandardOps/EDSC/Intrinsics.h" #include "mlir/Dialect/Utils/StructuredOpsUtils.h" -#include "mlir/Dialect/Vector/EDSC/Intrinsics.h" #include "mlir/Dialect/Vector/VectorOps.h" #include "mlir/IR/AffineExpr.h" #include "mlir/IR/Matchers.h" @@ -158,7 +157,7 @@ static Operation *getSingleBinaryOpAssumedReduction(OpOperand &outputOperand) { /// Otherwise, just return `value`. // TODO: this is best effort atm and there is currently no guarantee of // correctness for the broadcast semantics. -static Value broadcastIfNeeded(OpBuilder &builder, Value value, +static Value broadcastIfNeeded(OpBuilder &b, Value value, ArrayRef shape) { unsigned numDimsGtOne = std::count_if(shape.begin(), shape.end(), [](int64_t val) { return val > 1; }); @@ -169,8 +168,8 @@ static Value broadcastIfNeeded(OpBuilder &builder, Value value, return value; auto newVecType = VectorType::get(shape, vecType ? vecType.getElementType() : value.getType()); - return builder.create( - builder.getInsertionPoint()->getLoc(), newVecType, value); + return b.create(b.getInsertionPoint()->getLoc(), + newVecType, value); } static llvm::Optional @@ -189,7 +188,7 @@ getKindForOp(Operation *reductionOp) { /// If value of assumed VectorType has a shape different than `shape`, build and /// return a new vector.broadcast to `shape`. /// Otherwise, just return value. -static Value reduceIfNeeded(OpBuilder &builder, VectorType targetVectorType, +static Value reduceIfNeeded(OpBuilder &b, VectorType targetVectorType, Value value, OpOperand &outputOperand) { assert(targetVectorType.getShape() == outputOperand.get().getType().cast().getShape()); @@ -201,7 +200,7 @@ static Value reduceIfNeeded(OpBuilder &builder, VectorType targetVectorType, Operation *reductionOp = getSingleBinaryOpAssumedReduction(outputOperand); auto linalgOp = cast(outputOperand.getOwner()); unsigned pos = 0; - MLIRContext *ctx = builder.getContext(); + MLIRContext *ctx = b.getContext(); SmallVector exprs; for (auto s : linalgOp.iterator_types()) if (isParallelIterator(s)) @@ -217,41 +216,45 @@ static Value reduceIfNeeded(OpBuilder &builder, VectorType targetVectorType, reductionMask[idx] = true; ++idx; } - return builder.create(loc, value, reductionMask, - *maybeKind); + return b.create(loc, value, reductionMask, + *maybeKind); } /// Build a vector.transfer_read from `source` at indices set to all `0`. /// If source has rank zero, build an memref.load. /// Return the produced value. -static Value buildVectorRead(OpBuilder &builder, Value source, - VectorType vectorType, AffineMap map) { - edsc::ScopedContext scope(builder); +static Value buildVectorRead(OpBuilder &b, Value source, VectorType vectorType, + AffineMap map) { + Location loc = source.getLoc(); auto shapedType = source.getType().cast(); - SmallVector indices(shapedType.getRank(), std_constant_index(0)); - return vector_transfer_read(vectorType, source, indices, map); + SmallVector indices(shapedType.getRank(), + b.create(loc, 0)); + return b.create(loc, vectorType, source, indices, + map); } /// Build a vector.transfer_write of `value` into `outputOperand` at indices set /// to all `0`; where `outputOperand` is an output operand of the LinalgOp /// currently being vectorized. If `dest` has null rank, build an memref.store. /// Return the produced value or null if no value is produced. -static Value buildVectorWrite(OpBuilder &builder, Value value, +static Value buildVectorWrite(OpBuilder &b, Value value, OpOperand &outputOperand) { - edsc::ScopedContext scope(builder); Operation *write; + Location loc = value.getLoc(); auto shapedType = outputOperand.get().getType().cast(); if (VectorType vectorType = extractVectorTypeFromShapedValue(outputOperand.get())) { auto linalgOp = cast(outputOperand.getOwner()); AffineMap map = reindexIndexingMap( linalgOp.getIndexingMap(outputOperand.getOperandNumber())); - SmallVector indices(shapedType.getRank(), std_constant_index(0)); - value = broadcastIfNeeded(builder, value, vectorType.getShape()); - value = reduceIfNeeded(builder, vectorType, value, outputOperand); - write = vector_transfer_write(value, outputOperand.get(), indices, map); + SmallVector indices(shapedType.getRank(), + b.create(loc, 0)); + value = broadcastIfNeeded(b, value, vectorType.getShape()); + value = reduceIfNeeded(b, vectorType, value, outputOperand); + write = b.create(loc, value, outputOperand.get(), + indices, map); } else { - write = memref_store(value, outputOperand.get()); + write = b.create(loc, value, outputOperand.get()); } LLVM_DEBUG(dbgs() << "\n[" DEBUG_TYPE "]: vectorized op: " << *write); if (!write->getResults().empty()) @@ -273,7 +276,7 @@ using CustomVectorizationHook = std::function &newResults) { auto yieldOp = dyn_cast(op); @@ -284,7 +287,7 @@ vectorizeLinalgYield(OpBuilder &builder, Operation *op, // TODO: use a map. Value vectorValue = bvm.lookup(outputs.value()); Value newResult = buildVectorWrite( - builder, vectorValue, linalgOp.getOutputOpOperands()[outputs.index()]); + b, vectorValue, linalgOp.getOutputOpOperands()[outputs.index()]); if (newResult) newResults.push_back(newResult); } @@ -295,8 +298,8 @@ vectorizeLinalgYield(OpBuilder &builder, Operation *op, /// VectorizationStatus::NewOp to signal the vectorization algorithm that it /// should map the produced operations. This function is meant to be used as a /// CustomVectorizationHook. -static VectorizationResult -vectorizeLinalgIndex(OpBuilder &builder, Operation *op, LinalgOp linalgOp) { +static VectorizationResult vectorizeLinalgIndex(OpBuilder &b, Operation *op, + LinalgOp linalgOp) { IndexOp indexOp = dyn_cast(op); if (!indexOp) return VectorizationResult{VectorizationStatus::Failure, nullptr}; @@ -307,7 +310,7 @@ vectorizeLinalgIndex(OpBuilder &builder, Operation *op, LinalgOp linalgOp) { SmallVector constantSeq( llvm::seq(0, targetShape[indexOp.dim()])); ConstantOp constantOp = - builder.create(loc, builder.getIndexVectorAttr(constantSeq)); + b.create(loc, b.getIndexVectorAttr(constantSeq)); // Return the one-dimensional index vector if it lives in the trailing // dimension of the iteration space since the vectorization algorithm in this // case can handle the broadcast. @@ -317,13 +320,13 @@ vectorizeLinalgIndex(OpBuilder &builder, Operation *op, LinalgOp linalgOp) { // broadcast the one-dimensional index vector to the permuted shape, and // finally transpose the broadcasted index vector to undo the permutation. std::swap(targetShape[indexOp.dim()], targetShape.back()); - auto broadCastOp = builder.create( - loc, VectorType::get(targetShape, builder.getIndexType()), constantOp); + auto broadCastOp = b.create( + loc, VectorType::get(targetShape, b.getIndexType()), constantOp); SmallVector transposition( llvm::seq(0, linalgOp.getNumLoops())); std::swap(transposition.back(), transposition[indexOp.dim()]); auto transposeOp = - builder.create(loc, broadCastOp, transposition); + b.create(loc, broadCastOp, transposition); return VectorizationResult{VectorizationStatus::NewOp, transposeOp}; } @@ -347,8 +350,7 @@ vectorizeLinalgIndex(OpBuilder &builder, Operation *op, LinalgOp linalgOp) { /// This function does not update `bvm` but returns a VectorizationStatus that /// instructs the caller what `bvm` update needs to occur. static VectorizationResult -vectorizeOneOp(OpBuilder &builder, Operation *op, - const BlockAndValueMapping &bvm, +vectorizeOneOp(OpBuilder &b, Operation *op, const BlockAndValueMapping &bvm, ArrayRef customVectorizationHooks) { LLVM_DEBUG(dbgs() << "\n[" DEBUG_TYPE "]: vectorize op " << *op); @@ -365,7 +367,7 @@ vectorizeOneOp(OpBuilder &builder, Operation *op, // 2. Constant ops don't get vectorized but rather broadcasted at their users. // Clone so that the constant is not confined to the linalgOp block . if (isa(op)) - return VectorizationResult{VectorizationStatus::NewOp, builder.clone(*op)}; + return VectorizationResult{VectorizationStatus::NewOp, b.clone(*op)}; // 3. Only ElementwiseMappable are allowed in the generic vectorization. if (!OpTrait::hasElementwiseMappableTraits(op)) @@ -383,7 +385,7 @@ vectorizeOneOp(OpBuilder &builder, Operation *op, auto vectorizedOperands = llvm::map_range(op->getOperands(), [&](Value v) { return firstMaxRankedShape.empty() ? bvm.lookup(v) - : broadcastIfNeeded(builder, bvm.lookup(v), firstMaxRankedShape); + : broadcastIfNeeded(b, bvm.lookup(v), firstMaxRankedShape); }); // c. for elementwise, the result is the vector with the firstMaxRankedShape auto returnTypes = llvm::map_range(op->getResultTypes(), [&](Type t) { @@ -398,7 +400,7 @@ vectorizeOneOp(OpBuilder &builder, Operation *op, state.addOperands(llvm::to_vector<4>(vectorizedOperands)); state.addTypes(llvm::to_vector<4>(returnTypes)); return VectorizationResult{VectorizationStatus::NewOp, - builder.createOperation(state)}; + b.createOperation(state)}; } /// Detect whether `r` has only ConstantOp, ElementwiseMappable and YieldOp. @@ -455,7 +457,7 @@ static bool isElementwise(Operation *op) { /// This is not deemed a problem as we expect canonicalizations and foldings to /// aggressively clean up the useless work. LogicalResult vectorizeAsLinalgGeneric( - OpBuilder &builder, LinalgOp linalgOp, SmallVectorImpl &newResults, + OpBuilder &b, LinalgOp linalgOp, SmallVectorImpl &newResults, bool broadcastToMaximalCommonShape = false, ArrayRef customVectorizationHooks = {}) { // 1. Fail to vectorize if the operation does not have one non-empty region. @@ -485,8 +487,7 @@ LogicalResult vectorizeAsLinalgGeneric( ShapedType shapedType = shapedArg.getType().cast(); // TODO: 0-d vectors. if (shapedType.getShape().empty()) { - Value loaded = - builder.create(linalgOp.getLoc(), shapedArg); + Value loaded = b.create(linalgOp.getLoc(), shapedArg); LLVM_DEBUG(dbgs() << "\n[" DEBUG_TYPE "]: new vectorized bbarg(" << bbarg.getArgNumber() << "): " << loaded); bvm.map(bbarg, loaded); @@ -506,7 +507,7 @@ LogicalResult vectorizeAsLinalgGeneric( vectorType = VectorType::get(map.compose(shapedType.getShape()), shapedType.getElementType()); } - Value vectorRead = buildVectorRead(builder, shapedArg, vectorType, map); + Value vectorRead = buildVectorRead(b, shapedArg, vectorType, map); LLVM_DEBUG(dbgs() << "\n[" DEBUG_TYPE "]: new vectorized bbarg(" << bbarg.getArgNumber() << "): " << vectorRead); bvm.map(bbarg, vectorRead); @@ -518,7 +519,7 @@ LogicalResult vectorizeAsLinalgGeneric( CustomVectorizationHook vectorizeYield = [&](Operation *op, const BlockAndValueMapping &bvm) -> VectorizationResult { - return vectorizeLinalgYield(builder, op, bvm, linalgOp, newResults); + return vectorizeLinalgYield(b, op, bvm, linalgOp, newResults); }; hooks.push_back(vectorizeYield); @@ -526,13 +527,13 @@ LogicalResult vectorizeAsLinalgGeneric( CustomVectorizationHook vectorizeIndex = [&](Operation *op, const BlockAndValueMapping &bvm) -> VectorizationResult { - return vectorizeLinalgIndex(builder, op, linalgOp); + return vectorizeLinalgIndex(b, op, linalgOp); }; hooks.push_back(vectorizeIndex); // 5. Iteratively call `vectorizeOneOp` to each op in the slice. for (Operation &op : block.getOperations()) { - VectorizationResult result = vectorizeOneOp(builder, &op, bvm, hooks); + VectorizationResult result = vectorizeOneOp(b, &op, bvm, hooks); if (result.status == VectorizationStatus::Failure) { LLVM_DEBUG(dbgs() << "\n[" DEBUG_TYPE "]: failed to vectorize: " << op); return failure(); @@ -547,7 +548,7 @@ LogicalResult vectorizeAsLinalgGeneric( return success(); } -static LogicalResult vectorizeContraction(OpBuilder &builder, LinalgOp linalgOp, +static LogicalResult vectorizeContraction(OpBuilder &b, LinalgOp linalgOp, SmallVectorImpl &newResults) { assert(isaContractionOpInterface(linalgOp) && "expected vectorizeContraction preconditions to be met"); @@ -568,8 +569,7 @@ static LogicalResult vectorizeContraction(OpBuilder &builder, LinalgOp linalgOp, auto vType = outShape.empty() ? op->getResult(0).getType() : VectorType::get(outShape, op->getResult(0).getType()); - auto zero = - builder.create(loc, vType, builder.getZeroAttr(vType)); + auto zero = b.create(loc, vType, b.getZeroAttr(vType)); // Indexing maps at the time of vector.transfer_read are adjusted to order // vector dimensions in the same order as the canonical linalg op iteration // space order. @@ -584,12 +584,12 @@ static LogicalResult vectorizeContraction(OpBuilder &builder, LinalgOp linalgOp, .compose(linalgOp.getIndexingMap(1)), inversePermutation(reindexIndexingMap(linalgOp.getIndexingMap(2))) .compose(linalgOp.getIndexingMap(2))}; - Operation *contract = builder.create( + Operation *contract = b.create( loc, bvm.lookup(op->getOperand(0)), bvm.lookup(op->getOperand(1)), zero, - builder.getAffineMapArrayAttr(indexingMaps), linalgOp.iterator_types()); + b.getAffineMapArrayAttr(indexingMaps), linalgOp.iterator_types()); return VectorizationResult{VectorizationStatus::NewOp, contract}; }; - return vectorizeAsLinalgGeneric(builder, linalgOp, newResults, + return vectorizeAsLinalgGeneric(b, linalgOp, newResults, /*broadcastToMaximalCommonShape=*/false, {vectorizeContraction}); } @@ -635,22 +635,22 @@ LogicalResult mlir::linalg::vectorizeLinalgOpPrecondition(Operation *op) { } LogicalResult -mlir::linalg::vectorizeLinalgOp(OpBuilder &builder, Operation *op, +mlir::linalg::vectorizeLinalgOp(OpBuilder &b, Operation *op, SmallVectorImpl &newResults) { if (failed(vectorizeLinalgOpPrecondition(op))) return failure(); - edsc::ScopedContext scope(builder, op->getLoc()); + edsc::ScopedContext scope(b, op->getLoc()); auto linalgOp = cast(op); if (isaContractionOpInterface(linalgOp)) - return vectorizeContraction(builder, linalgOp, newResults); + return vectorizeContraction(b, linalgOp, newResults); LLVM_DEBUG(dbgs() << "\n[" DEBUG_TYPE "]: " << "Vectorize linalg op as a generic by broadcasting to " "maximal common shape: " << *op); - return vectorizeAsLinalgGeneric(builder, linalgOp, newResults, + return vectorizeAsLinalgGeneric(b, linalgOp, newResults, /*broadcastToMaximalCommonShape=*/true); } @@ -762,13 +762,16 @@ LogicalResult ConvOpVectorization::matchAndRewrite( Type elemType = inShapeType.getElementType(); auto map = AffineMap::get(rank, 0, mapping, context); - SmallVector zeros(rank, std_constant_index(0)); + SmallVector zeros(rank, rewriter.create(loc, 0)); auto vecType = VectorType::get(vectorDims, elemType); - auto inputVec = vector_transfer_read(vecType, input, zeros, map); - auto kernelVec = vector_transfer_read(vecType, kernel, zeros, map); + auto inputVec = + rewriter.create(loc, vecType, input, zeros, map); + auto kernelVec = + rewriter.create(loc, vecType, kernel, zeros, map); - auto acc = std_constant(elemType, rewriter.getZeroAttr(elemType)); + auto acc = rewriter.create(loc, elemType, + rewriter.getZeroAttr(elemType)); std::array indexingMaps{ AffineMap::getMultiDimIdentityMap(numDims, context), diff --git a/mlir/lib/Dialect/Vector/CMakeLists.txt b/mlir/lib/Dialect/Vector/CMakeLists.txt index b74c9a5a823a..9ea8aabb698d 100644 --- a/mlir/lib/Dialect/Vector/CMakeLists.txt +++ b/mlir/lib/Dialect/Vector/CMakeLists.txt @@ -3,7 +3,6 @@ add_mlir_dialect_library(MLIRVector VectorTransferOpTransforms.cpp VectorTransforms.cpp VectorUtils.cpp - EDSC/Builders.cpp ADDITIONAL_HEADER_DIRS ${MLIR_MAIN_INCLUDE_DIR}/mlir/Dialect/Vector @@ -13,12 +12,11 @@ add_mlir_dialect_library(MLIRVector MLIRVectorOpsEnumsIncGen LINK_LIBS PUBLIC - MLIRAffineEDSC - MLIREDSC MLIRDialectUtils MLIRIR MLIRStandard MLIRAffine + MLIRAffineUtils MLIRLinalg MLIRMemRef MLIRSCF diff --git a/mlir/lib/Dialect/Vector/EDSC/Builders.cpp b/mlir/lib/Dialect/Vector/EDSC/Builders.cpp deleted file mode 100644 index 7d6d2b9432d7..000000000000 --- a/mlir/lib/Dialect/Vector/EDSC/Builders.cpp +++ /dev/null @@ -1,40 +0,0 @@ -//===- Builders.cpp - MLIR Declarative Linalg Builders --------------------===// -// -// 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 -// -//===----------------------------------------------------------------------===// - -#include "mlir/Dialect/Vector/EDSC/Builders.h" -#include "mlir/Dialect/Vector/EDSC/Intrinsics.h" -#include "mlir/Dialect/Vector/VectorOps.h" -#include "mlir/EDSC/Builders.h" -#include "mlir/IR/AffineExpr.h" -#include "mlir/IR/Builders.h" - -using namespace mlir; -using namespace mlir::edsc; -using namespace mlir::edsc::intrinsics; -using namespace mlir::edsc::ops; - -Value mlir::edsc::ops::vector_contraction( - StructuredIndexed A, StructuredIndexed B, StructuredIndexed C, - ArrayRef iteratorTypes) { - using IndexingExprs = ArrayRef>; - return vector_contract( - A.getValue(), B.getValue(), C.getValue(), - IndexingExprs{A.getExprs(), B.getExprs(), C.getExprs()}, - ArrayRef{ - llvm::to_vector<8>(llvm::map_range(iteratorTypes, toString))}); -} - -Value mlir::edsc::ops::vector_contraction_matmul(Value A, Value B, Value C) { - AffineExpr m, n, k; - bindDims(ScopedContext::getContext(), m, n, k); - return vector_contraction(StructuredIndexed(A, {m, k}), - StructuredIndexed(B, {k, n}), - StructuredIndexed(C, {m, n}), - {IteratorType::Parallel, IteratorType::Parallel, - IteratorType::Reduction}); -} diff --git a/mlir/lib/Dialect/Vector/VectorTransforms.cpp b/mlir/lib/Dialect/Vector/VectorTransforms.cpp index f529167a2526..8aa4153e9481 100644 --- a/mlir/lib/Dialect/Vector/VectorTransforms.cpp +++ b/mlir/lib/Dialect/Vector/VectorTransforms.cpp @@ -12,17 +12,14 @@ #include -#include "mlir/Dialect/Affine/EDSC/Builders.h" -#include "mlir/Dialect/Affine/EDSC/Intrinsics.h" #include "mlir/Dialect/Affine/IR/AffineOps.h" -#include "mlir/Dialect/Linalg/EDSC/Intrinsics.h" -#include "mlir/Dialect/MemRef/EDSC/Intrinsics.h" +#include "mlir/Dialect/Affine/Utils.h" +#include "mlir/Dialect/Linalg/IR/LinalgOps.h" #include "mlir/Dialect/MemRef/IR/MemRef.h" -#include "mlir/Dialect/SCF/EDSC/Intrinsics.h" -#include "mlir/Dialect/StandardOps/EDSC/Intrinsics.h" +#include "mlir/Dialect/SCF/SCF.h" #include "mlir/Dialect/StandardOps/IR/Ops.h" #include "mlir/Dialect/Utils/StructuredOpsUtils.h" -#include "mlir/Dialect/Vector/EDSC/Intrinsics.h" + #include "mlir/Dialect/Vector/VectorOps.h" #include "mlir/Dialect/Vector/VectorTransforms.h" #include "mlir/Dialect/Vector/VectorUtils.h" @@ -31,6 +28,7 @@ #include "mlir/IR/Attributes.h" #include "mlir/IR/Builders.h" #include "mlir/IR/BuiltinOps.h" +#include "mlir/IR/ImplicitLocOpBuilder.h" #include "mlir/IR/Location.h" #include "mlir/IR/Matchers.h" #include "mlir/IR/OperationSupport.h" @@ -2274,18 +2272,18 @@ static Optional extractConstantIndex(Value v) { // Missing foldings of scf.if make it necessary to perform poor man's folding // eagerly, especially in the case of unrolling. In the future, this should go // away once scf.if folds properly. -static Value createScopedFoldedSLE(Value v, Value ub) { - using namespace edsc::op; +static Value createFoldedSLE(OpBuilder &b, Value v, Value ub) { auto maybeCstV = extractConstantIndex(v); auto maybeCstUb = extractConstantIndex(ub); if (maybeCstV && maybeCstUb && *maybeCstV < *maybeCstUb) return Value(); - return sle(v, ub); + return b.create(v.getLoc(), CmpIPredicate::sle, v, ub); } // Operates under a scoped context to build the condition to ensure that a // particular VectorTransferOpInterface is in-bounds. -static Value createScopedInBoundsCond(VectorTransferOpInterface xferOp) { +static Value createInBoundsCond(OpBuilder &b, + VectorTransferOpInterface xferOp) { assert(xferOp.permutation_map().isMinorIdentity() && "Expected minor identity map"); Value inBoundsCond; @@ -2295,17 +2293,23 @@ static Value createScopedInBoundsCond(VectorTransferOpInterface xferOp) { // the construction of `inBoundsCond`. if (xferOp.isDimInBounds(resultIdx)) return; - int64_t vectorSize = xferOp.getVectorType().getDimSize(resultIdx); - using namespace edsc::op; - using namespace edsc::intrinsics; // Fold or create the check that `index + vector_size` <= `memref_size`. - Value sum = xferOp.indices()[indicesIdx] + std_constant_index(vectorSize); - Value cond = - createScopedFoldedSLE(sum, memref_dim(xferOp.source(), indicesIdx)); + Location loc = xferOp.getLoc(); + ImplicitLocOpBuilder lb(loc, b); + int64_t vectorSize = xferOp.getVectorType().getDimSize(resultIdx); + auto d0 = getAffineDimExpr(0, xferOp.getContext()); + auto vs = getAffineConstantExpr(vectorSize, xferOp.getContext()); + Value sum = + makeComposedAffineApply(b, loc, d0 + vs, xferOp.indices()[indicesIdx]); + Value cond = createFoldedSLE( + b, sum, lb.create(xferOp.source(), indicesIdx)); if (!cond) return; // Conjunction over all dims for which we are in-bounds. - inBoundsCond = inBoundsCond ? inBoundsCond && cond : cond; + if (inBoundsCond) + inBoundsCond = lb.create(inBoundsCond, cond); + else + inBoundsCond = cond; }); return inBoundsCond; } @@ -2368,9 +2372,10 @@ static MemRefType getCastCompatibleMemRefType(MemRefType aT, MemRefType bT) { /// Operates under a scoped context to build the intersection between the /// view `xferOp.source()` @ `xferOp.indices()` and the view `alloc`. // TODO: view intersection/union/differences should be a proper std op. -static Value createScopedSubViewIntersection(VectorTransferOpInterface xferOp, - Value alloc) { - using namespace edsc::intrinsics; +static Value createSubViewIntersection(OpBuilder &b, + VectorTransferOpInterface xferOp, + Value alloc) { + ImplicitLocOpBuilder lb(xferOp.getLoc(), b); int64_t memrefRank = xferOp.getShapedType().getRank(); // TODO: relax this precondition, will require rank-reducing subviews. assert(memrefRank == alloc.getType().cast().getRank() && @@ -2382,22 +2387,22 @@ static Value createScopedSubViewIntersection(VectorTransferOpInterface xferOp, auto isaWrite = isa(xferOp); xferOp.zipResultAndIndexing([&](int64_t resultIdx, int64_t indicesIdx) { using MapList = ArrayRef>; - Value dimMemRef = memref_dim(xferOp.source(), indicesIdx); - Value dimAlloc = memref_dim(alloc, resultIdx); + Value dimMemRef = lb.create(xferOp.source(), indicesIdx); + Value dimAlloc = lb.create(alloc, resultIdx); Value index = xferOp.indices()[indicesIdx]; AffineExpr i, j, k; bindDims(xferOp.getContext(), i, j, k); SmallVector maps = AffineMap::inferFromExprList(MapList{{i - j, k}}); // affine_min(%dimMemRef - %index, %dimAlloc) - Value affineMin = affine_min(index.getType(), maps[0], - ValueRange{dimMemRef, index, dimAlloc}); + Value affineMin = lb.create( + index.getType(), maps[0], ValueRange{dimMemRef, index, dimAlloc}); sizes.push_back(affineMin); }); SmallVector indices = llvm::to_vector<4>(llvm::map_range( xferOp.indices(), [](Value idx) -> OpFoldResult { return idx; })); - return memref_sub_view( + return lb.create( isaWrite ? alloc : xferOp.source(), indices, sizes, SmallVector(memrefRank, OpBuilder(xferOp).getIndexAttr(1))); } @@ -2419,40 +2424,38 @@ static Value createScopedSubViewIntersection(VectorTransferOpInterface xferOp, /// } /// ``` /// Return the produced scf::IfOp. -static scf::IfOp createScopedFullPartialLinalgCopy( - vector::TransferReadOp xferOp, TypeRange returnTypes, Value inBoundsCond, - MemRefType compatibleMemRefType, Value alloc) { - using namespace edsc; - using namespace edsc::intrinsics; - scf::IfOp fullPartialIfOp; - Value zero = std_constant_index(0); +static scf::IfOp +createFullPartialLinalgCopy(OpBuilder &b, vector::TransferReadOp xferOp, + TypeRange returnTypes, Value inBoundsCond, + MemRefType compatibleMemRefType, Value alloc) { + Location loc = xferOp.getLoc(); + Value zero = b.create(loc, 0); Value memref = xferOp.source(); - conditionBuilder( - returnTypes, inBoundsCond, - [&]() -> scf::ValueVector { + return b.create( + loc, returnTypes, inBoundsCond, + [&](OpBuilder &b, Location loc) { Value res = memref; if (compatibleMemRefType != xferOp.getShapedType()) - res = memref_cast(memref, compatibleMemRefType); + res = b.create(loc, memref, compatibleMemRefType); scf::ValueVector viewAndIndices{res}; viewAndIndices.insert(viewAndIndices.end(), xferOp.indices().begin(), xferOp.indices().end()); - return viewAndIndices; + b.create(loc, viewAndIndices); }, - [&]() -> scf::ValueVector { - linalg_fill(alloc, xferOp.padding()); + [&](OpBuilder &b, Location loc) { + b.create(loc, alloc, xferOp.padding()); // Take partial subview of memref which guarantees no dimension // overflows. - Value memRefSubView = createScopedSubViewIntersection( - cast(xferOp.getOperation()), alloc); - linalg_copy(memRefSubView, alloc); - Value casted = memref_cast(alloc, compatibleMemRefType); + Value memRefSubView = createSubViewIntersection( + b, cast(xferOp.getOperation()), alloc); + b.create(loc, memRefSubView, alloc); + Value casted = + b.create(loc, alloc, compatibleMemRefType); scf::ValueVector viewAndIndices{casted}; viewAndIndices.insert(viewAndIndices.end(), xferOp.getTransferRank(), zero); - return viewAndIndices; - }, - &fullPartialIfOp); - return fullPartialIfOp; + b.create(loc, viewAndIndices); + }); } /// Given an `xferOp` for which: @@ -2473,41 +2476,39 @@ static scf::IfOp createScopedFullPartialLinalgCopy( /// } /// ``` /// Return the produced scf::IfOp. -static scf::IfOp createScopedFullPartialVectorTransferRead( - vector::TransferReadOp xferOp, TypeRange returnTypes, Value inBoundsCond, - MemRefType compatibleMemRefType, Value alloc) { - using namespace edsc; - using namespace edsc::intrinsics; +static scf::IfOp createFullPartialVectorTransferRead( + OpBuilder &b, vector::TransferReadOp xferOp, TypeRange returnTypes, + Value inBoundsCond, MemRefType compatibleMemRefType, Value alloc) { + Location loc = xferOp.getLoc(); scf::IfOp fullPartialIfOp; - Value zero = std_constant_index(0); + Value zero = b.create(loc, 0); Value memref = xferOp.source(); - conditionBuilder( - returnTypes, inBoundsCond, - [&]() -> scf::ValueVector { + return b.create( + loc, returnTypes, inBoundsCond, + [&](OpBuilder &b, Location loc) { Value res = memref; if (compatibleMemRefType != xferOp.getShapedType()) - res = memref_cast(memref, compatibleMemRefType); + res = b.create(loc, memref, compatibleMemRefType); scf::ValueVector viewAndIndices{res}; viewAndIndices.insert(viewAndIndices.end(), xferOp.indices().begin(), xferOp.indices().end()); - return viewAndIndices; + b.create(loc, viewAndIndices); }, - [&]() -> scf::ValueVector { - Operation *newXfer = - ScopedContext::getBuilderRef().clone(*xferOp.getOperation()); + [&](OpBuilder &b, Location loc) { + Operation *newXfer = b.clone(*xferOp.getOperation()); Value vector = cast(newXfer).vector(); - memref_store(vector, vector_type_cast( - MemRefType::get({}, vector.getType()), alloc)); + b.create( + loc, vector, + b.create( + loc, MemRefType::get({}, vector.getType()), alloc)); - Value casted = memref_cast(alloc, compatibleMemRefType); + Value casted = + b.create(loc, alloc, compatibleMemRefType); scf::ValueVector viewAndIndices{casted}; viewAndIndices.insert(viewAndIndices.end(), xferOp.getTransferRank(), zero); - - return viewAndIndices; - }, - &fullPartialIfOp); - return fullPartialIfOp; + b.create(loc, viewAndIndices); + }); } /// Given an `xferOp` for which: @@ -2525,33 +2526,35 @@ static scf::IfOp createScopedFullPartialVectorTransferRead( /// scf.yield %4, ... : compatibleMemRefType, index, index /// } /// ``` -static ValueRange getLocationToWriteFullVec(vector::TransferWriteOp xferOp, - TypeRange returnTypes, - Value inBoundsCond, - MemRefType compatibleMemRefType, - Value alloc) { - using namespace edsc; - using namespace edsc::intrinsics; - Value zero = std_constant_index(0); +static ValueRange +getLocationToWriteFullVec(OpBuilder &b, vector::TransferWriteOp xferOp, + TypeRange returnTypes, Value inBoundsCond, + MemRefType compatibleMemRefType, Value alloc) { + Location loc = xferOp.getLoc(); + Value zero = b.create(loc, 0); Value memref = xferOp.source(); - return conditionBuilder( - returnTypes, inBoundsCond, - [&]() -> scf::ValueVector { - Value res = memref; - if (compatibleMemRefType != xferOp.getShapedType()) - res = memref_cast(memref, compatibleMemRefType); - scf::ValueVector viewAndIndices{res}; - viewAndIndices.insert(viewAndIndices.end(), xferOp.indices().begin(), - xferOp.indices().end()); - return viewAndIndices; - }, - [&]() -> scf::ValueVector { - Value casted = memref_cast(alloc, compatibleMemRefType); - scf::ValueVector viewAndIndices{casted}; - viewAndIndices.insert(viewAndIndices.end(), xferOp.getTransferRank(), - zero); - return viewAndIndices; - }); + return b + .create( + loc, returnTypes, inBoundsCond, + [&](OpBuilder &b, Location loc) { + Value res = memref; + if (compatibleMemRefType != xferOp.getShapedType()) + res = b.create(loc, memref, compatibleMemRefType); + scf::ValueVector viewAndIndices{res}; + viewAndIndices.insert(viewAndIndices.end(), + xferOp.indices().begin(), + xferOp.indices().end()); + b.create(loc, viewAndIndices); + }, + [&](OpBuilder &b, Location loc) { + Value casted = + b.create(loc, alloc, compatibleMemRefType); + scf::ValueVector viewAndIndices{casted}; + viewAndIndices.insert(viewAndIndices.end(), + xferOp.getTransferRank(), zero); + b.create(loc, viewAndIndices); + }) + ->getResults(); } /// Given an `xferOp` for which: @@ -2566,19 +2569,17 @@ static ValueRange getLocationToWriteFullVec(vector::TransferWriteOp xferOp, /// linalg.copy(%3, %view) /// } /// ``` -static void createScopedFullPartialLinalgCopy(vector::TransferWriteOp xferOp, - Value inBoundsCond, Value alloc) { - using namespace edsc; - using namespace edsc::intrinsics; - auto &b = ScopedContext::getBuilderRef(); - auto notInBounds = b.create( - xferOp->getLoc(), inBoundsCond, - b.create<::mlir::ConstantIntOp>(xferOp.getLoc(), true, 1)); - - conditionBuilder(notInBounds, [&]() { - Value memRefSubView = createScopedSubViewIntersection( - cast(xferOp.getOperation()), alloc); - linalg_copy(memRefSubView, xferOp.source()); +static void createFullPartialLinalgCopy(OpBuilder &b, + vector::TransferWriteOp xferOp, + Value inBoundsCond, Value alloc) { + ImplicitLocOpBuilder lb(xferOp.getLoc(), b); + auto notInBounds = + lb.create(inBoundsCond, lb.create(true, 1)); + lb.create(notInBounds, [&](OpBuilder &b, Location loc) { + Value memRefSubView = createSubViewIntersection( + b, cast(xferOp.getOperation()), alloc); + b.create(loc, memRefSubView, xferOp.source()); + b.create(loc, ValueRange{}); }); } @@ -2594,23 +2595,21 @@ static void createScopedFullPartialLinalgCopy(vector::TransferWriteOp xferOp, /// vector.transfer_write %2, %view[...] : memref, vector<...> /// } /// ``` -static void -createScopedFullPartialVectorTransferWrite(vector::TransferWriteOp xferOp, - Value inBoundsCond, Value alloc) { - using namespace edsc; - using namespace edsc::intrinsics; - auto &b = ScopedContext::getBuilderRef(); - auto notInBounds = b.create( - xferOp->getLoc(), inBoundsCond, - b.create<::mlir::ConstantIntOp>(xferOp.getLoc(), true, 1)); - conditionBuilder(notInBounds, [&]() { +static void createFullPartialVectorTransferWrite(OpBuilder &b, + vector::TransferWriteOp xferOp, + Value inBoundsCond, + Value alloc) { + ImplicitLocOpBuilder lb(xferOp.getLoc(), b); + auto notInBounds = + lb.create(inBoundsCond, lb.create(true, 1)); + lb.create(notInBounds, [&](OpBuilder &b, Location loc) { BlockAndValueMapping mapping; - - Value load = memref_load(vector_type_cast( - MemRefType::get({}, xferOp.vector().getType()), alloc)); - + Value load = b.create( + loc, b.create( + loc, MemRefType::get({}, xferOp.vector().getType()), alloc)); mapping.map(xferOp.vector(), load); b.clone(*xferOp.getOperation(), mapping); + b.create(loc, ValueRange{}); }); } @@ -2677,9 +2676,6 @@ createScopedFullPartialVectorTransferWrite(vector::TransferWriteOp xferOp, LogicalResult mlir::vector::splitFullAndPartialTransfer( OpBuilder &b, VectorTransferOpInterface xferOp, VectorTransformsOptions options, scf::IfOp *ifOp) { - using namespace edsc; - using namespace edsc::intrinsics; - if (options.vectorTransferSplit == VectorTransferSplit::None) return failure(); @@ -2709,9 +2705,8 @@ LogicalResult mlir::vector::splitFullAndPartialTransfer( OpBuilder::InsertionGuard guard(b); b.setInsertionPoint(xferOp); - ScopedContext scope(b, xferOp.getLoc()); - Value inBoundsCond = createScopedInBoundsCond( - cast(xferOp.getOperation())); + Value inBoundsCond = createInBoundsCond( + b, cast(xferOp.getOperation())); if (!inBoundsCond) return failure(); @@ -2723,8 +2718,9 @@ LogicalResult mlir::vector::splitFullAndPartialTransfer( b.setInsertionPointToStart(&funcOp.getRegion().front()); auto shape = xferOp.getVectorType().getShape(); Type elementType = xferOp.getVectorType().getElementType(); - alloc = memref_alloca(MemRefType::get(shape, elementType), ValueRange{}, - b.getI64IntegerAttr(32)); + alloc = b.create(funcOp.getLoc(), + MemRefType::get(shape, elementType), + ValueRange{}, b.getI64IntegerAttr(32)); } MemRefType compatibleMemRefType = @@ -2739,12 +2735,12 @@ LogicalResult mlir::vector::splitFullAndPartialTransfer( // Read case: full fill + partial copy -> in-bounds vector.xfer_read. scf::IfOp fullPartialIfOp = options.vectorTransferSplit == VectorTransferSplit::VectorTransfer - ? createScopedFullPartialVectorTransferRead( - xferReadOp, returnTypes, inBoundsCond, compatibleMemRefType, - alloc) - : createScopedFullPartialLinalgCopy(xferReadOp, returnTypes, - inBoundsCond, - compatibleMemRefType, alloc); + ? createFullPartialVectorTransferRead(b, xferReadOp, returnTypes, + inBoundsCond, + compatibleMemRefType, alloc) + : createFullPartialLinalgCopy(b, xferReadOp, returnTypes, + inBoundsCond, compatibleMemRefType, + alloc); if (ifOp) *ifOp = fullPartialIfOp; @@ -2761,7 +2757,7 @@ LogicalResult mlir::vector::splitFullAndPartialTransfer( // Decide which location to write the entire vector to. auto memrefAndIndices = getLocationToWriteFullVec( - xferWriteOp, returnTypes, inBoundsCond, compatibleMemRefType, alloc); + b, xferWriteOp, returnTypes, inBoundsCond, compatibleMemRefType, alloc); // Do an in bounds write to either the output or the extra allocated buffer. // The operation is cloned to prevent deleting information needed for the @@ -2775,10 +2771,9 @@ LogicalResult mlir::vector::splitFullAndPartialTransfer( // Create a potential copy from the allocated buffer to the final output in // the slow path case. if (options.vectorTransferSplit == VectorTransferSplit::VectorTransfer) - createScopedFullPartialVectorTransferWrite(xferWriteOp, inBoundsCond, - alloc); + createFullPartialVectorTransferWrite(b, xferWriteOp, inBoundsCond, alloc); else - createScopedFullPartialLinalgCopy(xferWriteOp, inBoundsCond, alloc); + createFullPartialLinalgCopy(b, xferWriteOp, inBoundsCond, alloc); xferOp->erase(); @@ -2864,27 +2859,27 @@ struct TransferReadExtractPattern return failure(); if (read.mask()) return failure(); - edsc::ScopedContext scope(rewriter, read.getLoc()); - using mlir::edsc::op::operator+; - using mlir::edsc::op::operator*; - using namespace mlir::edsc::intrinsics; + SmallVector indices(read.indices().begin(), read.indices().end()); AffineMap map = extract.map(); unsigned idCount = 0; + ImplicitLocOpBuilder lb(read.getLoc(), rewriter); for (auto expr : map.getResults()) { + AffineExpr d0, d1; + bindDims(read.getContext(), d0, d1); unsigned pos = expr.cast().getPosition(); + auto scale = getAffineConstantExpr( + extract.getResultType().getDimSize(pos), read.getContext()); indices[pos] = - indices[pos] + - extract.ids()[idCount++] * - std_constant_index(extract.getResultType().getDimSize(pos)); + makeComposedAffineApply(rewriter, read.getLoc(), d0 + scale * d1, + {indices[pos], extract.ids()[idCount++]}); } - Value newRead = vector_transfer_read(extract.getType(), read.source(), - indices, read.permutation_map(), - read.padding(), read.in_boundsAttr()); - Value dest = rewriter.create( - read.getLoc(), read.getType(), rewriter.getZeroAttr(read.getType())); - newRead = rewriter.create(read.getLoc(), newRead, dest, - extract.ids()); + Value newRead = lb.create( + extract.getType(), read.source(), indices, read.permutation_map(), + read.padding(), read.in_boundsAttr()); + Value dest = lb.create(read.getType(), + rewriter.getZeroAttr(read.getType())); + newRead = lb.create(newRead, dest, extract.ids()); rewriter.replaceOp(read, newRead); return success(); } @@ -2901,23 +2896,24 @@ struct TransferWriteInsertPattern return failure(); if (write.mask()) return failure(); - edsc::ScopedContext scope(rewriter, write.getLoc()); - using mlir::edsc::op::operator+; - using mlir::edsc::op::operator*; - using namespace mlir::edsc::intrinsics; SmallVector indices(write.indices().begin(), write.indices().end()); AffineMap map = insert.map(); unsigned idCount = 0; + Location loc = write.getLoc(); for (auto expr : map.getResults()) { + AffineExpr d0, d1; + bindDims(write.getContext(), d0, d1); unsigned pos = expr.cast().getPosition(); + auto scale = getAffineConstantExpr( + insert.getSourceVectorType().getDimSize(pos), write.getContext()); indices[pos] = - indices[pos] + - insert.ids()[idCount++] * - std_constant_index(insert.getSourceVectorType().getDimSize(pos)); + makeComposedAffineApply(rewriter, loc, d0 + scale * d1, + {indices[pos], insert.ids()[idCount++]}); } - vector_transfer_write(insert.vector(), write.source(), indices, - write.permutation_map(), write.in_boundsAttr()); + rewriter.create( + loc, insert.vector(), write.source(), indices, write.permutation_map(), + write.in_boundsAttr()); rewriter.eraseOp(write); return success(); } @@ -3175,23 +3171,23 @@ struct TransferWritePermutationLowering SmallVector indices; llvm::transform(comp.getResults(), std::back_inserter(indices), [](AffineExpr expr) { - return expr.dyn_cast().getPosition(); - }); + return expr.dyn_cast().getPosition(); + }); // Transpose mask operand. - Value newMask = op.mask() - ? rewriter.create(op.getLoc(), op.mask(), indices) - : Value(); + Value newMask = op.mask() ? rewriter.create( + op.getLoc(), op.mask(), indices) + : Value(); // Transpose in_bounds attribute. - ArrayAttr newInBounds = op.in_bounds() - ? transposeInBoundsAttr(rewriter, op.in_bounds().getValue(), - permutation) - : ArrayAttr(); + ArrayAttr newInBounds = + op.in_bounds() ? transposeInBoundsAttr( + rewriter, op.in_bounds().getValue(), permutation) + : ArrayAttr(); // Generate new transfer_write operation. - Value newVec = rewriter.create( - op.getLoc(), op.vector(), indices); + Value newVec = + rewriter.create(op.getLoc(), op.vector(), indices); auto newMap = AffineMap::getMinorIdentityMap( map.getNumDims(), map.getNumResults(), rewriter.getContext()); rewriter.replaceOpWithNewOp( diff --git a/mlir/test/CMakeLists.txt b/mlir/test/CMakeLists.txt index f8e349b5b606..614a990bace0 100644 --- a/mlir/test/CMakeLists.txt +++ b/mlir/test/CMakeLists.txt @@ -1,5 +1,4 @@ add_subdirectory(CAPI) -add_subdirectory(EDSC) add_subdirectory(SDBM) add_subdirectory(lib) @@ -63,7 +62,6 @@ set(MLIR_TEST_DEPENDS mlir-capi-pass-test mlir-capi-sparse-tensor-test mlir-cpu-runner - mlir-edsc-builder-api-test mlir-linalg-ods-gen mlir-lsp-server mlir-opt diff --git a/mlir/test/EDSC/CMakeLists.txt b/mlir/test/EDSC/CMakeLists.txt deleted file mode 100644 index d4380561854b..000000000000 --- a/mlir/test/EDSC/CMakeLists.txt +++ /dev/null @@ -1,26 +0,0 @@ -set(LLVM_LINK_COMPONENTS - Core - Support - ) -add_llvm_executable(mlir-edsc-builder-api-test - builder-api-test.cpp -) - -llvm_update_compile_flags(mlir-edsc-builder-api-test) - -target_link_libraries(mlir-edsc-builder-api-test - PRIVATE - MLIRAffine - MLIRAffineEDSC - MLIREDSC - MLIRIR - MLIRLinalg - MLIRLinalgEDSC - MLIRMemRef - MLIRSCF - MLIRStandard - MLIRTransforms - MLIRVector - ) - -target_include_directories(mlir-edsc-builder-api-test PRIVATE ..) diff --git a/mlir/test/EDSC/builder-api-test.cpp b/mlir/test/EDSC/builder-api-test.cpp deleted file mode 100644 index ed7fef160ff9..000000000000 --- a/mlir/test/EDSC/builder-api-test.cpp +++ /dev/null @@ -1,1254 +0,0 @@ -//===- builder-api-test.cpp - Tests for Declarative Builder APIs ----------===// -// -// 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 -// -//===----------------------------------------------------------------------===// - -// RUN: mlir-edsc-builder-api-test | FileCheck %s - -#include "mlir/Dialect/Affine/EDSC/Intrinsics.h" -#include "mlir/Dialect/Linalg/EDSC/Builders.h" -#include "mlir/Dialect/Linalg/EDSC/Intrinsics.h" -#include "mlir/Dialect/Math/IR/Math.h" -#include "mlir/Dialect/MemRef/EDSC/Intrinsics.h" -#include "mlir/Dialect/SCF/EDSC/Intrinsics.h" -#include "mlir/Dialect/StandardOps/EDSC/Intrinsics.h" -#include "mlir/Dialect/Vector/EDSC/Intrinsics.h" -#include "mlir/EDSC/Builders.h" -#include "mlir/IR/AffineExpr.h" -#include "mlir/IR/Builders.h" -#include "mlir/IR/BuiltinOps.h" -#include "mlir/IR/BuiltinTypes.h" -#include "mlir/IR/IntegerSet.h" -#include "mlir/IR/MLIRContext.h" -#include "mlir/IR/Types.h" -#include "mlir/Pass/Pass.h" -#include "mlir/Pass/PassManager.h" -#include "mlir/Transforms/LoopUtils.h" -#include "mlir/Transforms/Passes.h" - -#include "APITest.h" - -#include "llvm/Support/raw_ostream.h" - -using namespace mlir; -using namespace mlir::edsc; -using namespace mlir::edsc::intrinsics; - -static MLIRContext &globalContext() { - static thread_local MLIRContext context; - static thread_local bool initOnce = [&]() { - // clang-format off - context.loadDialect(); - // clang-format on - return true; - }(); - (void)initOnce; - context.allowUnregisteredDialects(); - return context; -} - -static FuncOp makeFunction(StringRef name, ArrayRef results = {}, - ArrayRef args = {}) { - auto &ctx = globalContext(); - auto function = FuncOp::create(UnknownLoc::get(&ctx), name, - FunctionType::get(&ctx, args, results)); - function.addEntryBlock(); - return function; -} - -TEST_FUNC(builder_dynamic_for_func_args) { - auto indexType = IndexType::get(&globalContext()); - auto f32Type = FloatType::getF32(&globalContext()); - auto f = - makeFunction("builder_dynamic_for_func_args", {}, {indexType, indexType}); - - OpBuilder builder(f.getBody()); - ScopedContext scope(builder, f.getLoc()); - Value lb(f.getArgument(0)), ub(f.getArgument(1)); - Value f7(std_constant_float(llvm::APFloat(7.0f), f32Type)); - Value f13(std_constant_float(llvm::APFloat(13.0f), f32Type)); - Value i7(std_constant_int(7, 32)); - Value i13(std_constant_int(13, 32)); - affineLoopBuilder(lb, ub, 3, [&](Value i) { - using namespace edsc::op; - lb *std_constant_index(3) + ub; - lb + std_constant_index(3); - affineLoopBuilder(lb, ub, 2, [&](Value j) { - ceilDiv(std_constant_index(31) * floorDiv(i + j * std_constant_index(3), - std_constant_index(32)), - std_constant_index(32)); - ((f7 + f13) / f7) % f13 - f7 *f13; - ((i7 + i13) / i7) % i13 - i7 *i13; - }); - }); - - // clang-format off - // CHECK-LABEL: func @builder_dynamic_for_func_args(%{{.*}}: index, %{{.*}}: index) { - // CHECK: affine.for %{{.*}} = affine_map<(d0) -> (d0)>(%{{.*}}) to affine_map<(d0) -> (d0)>(%{{.*}}) step 3 { - // CHECK: {{.*}} = affine.apply affine_map<()[s0] -> (s0 * 3)>()[%{{.*}}] - // CHECK: {{.*}} = affine.apply affine_map<()[s0, s1] -> (s0 * 3 + s1)>()[%{{.*}}, %{{.*}}] - // CHECK: {{.*}} = affine.apply affine_map<()[s0] -> (s0 + 3)>()[%{{.*}}] - // CHECK: affine.for %{{.*}} = affine_map<(d0) -> (d0)>(%{{.*}}) to affine_map<(d0) -> (d0)>(%{{.*}}) step 2 { - // CHECK: {{.*}} = affine.apply affine_map<(d0, d1) -> ((d0 + d1 * 3) floordiv 32)>(%{{.*}}, %{{.*}}) - // CHECK: {{.*}} = affine.apply affine_map<(d0, d1) -> (((d0 + d1 * 3) floordiv 32) * 31)>(%{{.*}}, %{{.*}}) - // CHECK: {{.*}} = affine.apply affine_map<(d0, d1) -> ((((d0 + d1 * 3) floordiv 32) * 31) ceildiv 32)>(%{{.*}}, %{{.*}}) - // CHECK-DAG: [[rf1:%[0-9]+]] = addf {{.*}}, {{.*}} : f32 - // CHECK-DAG: [[rf2:%[0-9]+]] = divf [[rf1]], {{.*}} : f32 - // CHECK-DAG: [[rf3:%[0-9]+]] = remf [[rf2]], {{.*}} : f32 - // CHECK-DAG: [[rf4:%[0-9]+]] = mulf {{.*}}, {{.*}} : f32 - // CHECK: {{.*}} = subf [[rf3]], [[rf4]] : f32 - // CHECK-DAG: [[ri1:%[0-9]+]] = addi {{.*}}, {{.*}} : i32 - // CHECK-DAG: [[ri2:%[0-9]+]] = divi_signed [[ri1]], {{.*}} : i32 - // CHECK-DAG: [[ri3:%[0-9]+]] = remi_signed [[ri2]], {{.*}} : i32 - // CHECK-DAG: [[ri4:%[0-9]+]] = muli {{.*}}, {{.*}} : i32 - // CHECK: {{.*}} = subi [[ri3]], [[ri4]] : i32 - // clang-format on - f.print(llvm::outs(), OpPrintingFlags().useLocalScope()); - f.erase(); -} - -TEST_FUNC(builder_dynamic_for) { - auto indexType = IndexType::get(&globalContext()); - auto f = makeFunction("builder_dynamic_for", {}, - {indexType, indexType, indexType, indexType}); - - OpBuilder builder(f.getBody()); - ScopedContext scope(builder, f.getLoc()); - Value i, a(f.getArgument(0)), b(f.getArgument(1)), c(f.getArgument(2)), - d(f.getArgument(3)); - using namespace edsc::op; - affineLoopBuilder(a - b, c + d, 2); - - // clang-format off - // CHECK-LABEL: func @builder_dynamic_for(%{{.*}}: index, %{{.*}}: index, %{{.*}}: index, %{{.*}}: index) { - // CHECK-DAG: [[r0:%[0-9]+]] = affine.apply affine_map<()[s0, s1] -> (s0 - s1)>()[%{{.*}}, %{{.*}}] - // CHECK-DAG: [[r1:%[0-9]+]] = affine.apply affine_map<()[s0, s1] -> (s0 + s1)>()[%{{.*}}, %{{.*}}] - // CHECK-NEXT: affine.for %{{.*}} = affine_map<(d0) -> (d0)>([[r0]]) to affine_map<(d0) -> (d0)>([[r1]]) step 2 { - // clang-format on - f.print(llvm::outs(), OpPrintingFlags().useLocalScope()); - f.erase(); -} - -TEST_FUNC(builder_loop_for) { - auto indexType = IndexType::get(&globalContext()); - auto f = makeFunction("builder_loop_for", {}, - {indexType, indexType, indexType, indexType}); - - OpBuilder builder(f.getBody()); - ScopedContext scope(builder, f.getLoc()); - Value a(f.getArgument(0)), b(f.getArgument(1)), c(f.getArgument(2)), - d(f.getArgument(3)); - using namespace edsc::op; - loopNestBuilder(a - b, c + d, a); - - // clang-format off - // CHECK-LABEL: func @builder_loop_for(%{{.*}}: index, %{{.*}}: index, %{{.*}}: index, %{{.*}}: index) { - // CHECK-DAG: [[r0:%[0-9]+]] = affine.apply affine_map<()[s0, s1] -> (s0 - s1)>()[%{{.*}}, %{{.*}}] - // CHECK-DAG: [[r1:%[0-9]+]] = affine.apply affine_map<()[s0, s1] -> (s0 + s1)>()[%{{.*}}, %{{.*}}] - // CHECK-NEXT: scf.for %{{.*}} = [[r0]] to [[r1]] step {{.*}} { - // clang-format on - f.print(llvm::outs(), OpPrintingFlags().useLocalScope()); - f.erase(); -} - -TEST_FUNC(builder_max_min_for) { - auto indexType = IndexType::get(&globalContext()); - auto f = makeFunction("builder_max_min_for", {}, - {indexType, indexType, indexType, indexType}); - - OpBuilder builder(f.getBody()); - ScopedContext scope(builder, f.getLoc()); - Value lb1(f.getArgument(0)), lb2(f.getArgument(1)), ub1(f.getArgument(2)), - ub2(f.getArgument(3)); - affineLoopBuilder({lb1, lb2}, {ub1, ub2}, 1); - std_ret(); - - // clang-format off - // CHECK-LABEL: func @builder_max_min_for(%{{.*}}: index, %{{.*}}: index, %{{.*}}: index, %{{.*}}: index) { - // CHECK: affine.for %{{.*}} = max affine_map<(d0, d1) -> (d0, d1)>(%{{.*}}, %{{.*}}) to min affine_map<(d0, d1) -> (d0, d1)>(%{{.*}}, %{{.*}}) { - // CHECK: return - // clang-format on - f.print(llvm::outs(), OpPrintingFlags().useLocalScope()); - f.erase(); -} - -TEST_FUNC(builder_affine_for_iter_args) { - auto indexType = IndexType::get(&globalContext()); - auto f = makeFunction("builder_affine_for_iter_args", {}, - {indexType, indexType, indexType}); - - OpBuilder builder(f.getBody()); - ScopedContext scope(builder, f.getLoc()); - Value i, lb_1(f.getArgument(0)), ub_1(f.getArgument(1)), - ub_2(f.getArgument(2)); - Value c32(std_constant_int(32, 32)); - Value c42(std_constant_int(42, 32)); - using namespace edsc::op; - affineLoopBuilder( - lb_1, {ub_1, ub_2}, 2, {c32, c42}, [&](Value iv, ValueRange args) { - Value sum(args[0] + args[1]); - builder.create(f.getLoc(), ValueRange({args[1], sum})); - }); - - // clang-format off - // CHECK-LABEL: func @builder_affine_for_iter_args - // CHECK: (%[[lb_1:.*]]: index, %[[ub_1:.*]]: index, %[[ub_2:.*]]: index) { - // CHECK-NEXT: %[[c32:.*]] = constant 32 : i32 - // CHECK-NEXT: %[[c42:.*]] = constant 42 : i32 - // CHECK-NEXT: %{{.*}} = affine.for %{{.*}} = affine_map<(d0) -> (d0)>(%{{.*}}) to min affine_map<(d0, d1) -> (d0, d1)>(%[[ub_1]], %[[ub_2]]) step 2 iter_args(%[[iarg_1:.*]] = %[[c32]], %[[iarg_2:.*]] = %[[c42]]) -> (i32, i32) { - // CHECK-NEXT: %[[sum:.*]] = addi %[[iarg_1]], %[[iarg_2]] : i32 - // CHECK-NEXT: affine.yield %[[iarg_2]], %[[sum]] : i32, i32 - // CHECK-NEXT: } - // clang-format on - f.print(llvm::outs(), OpPrintingFlags().useLocalScope()); - f.erase(); -} - -TEST_FUNC(builder_block_append) { - using namespace edsc::op; - auto f = makeFunction("builder_blocks"); - - OpBuilder builder(f.getBody()); - ScopedContext scope(builder, f.getLoc()); - - Block *b = - buildInNewBlock(TypeRange(), [&](ValueRange) { std_constant_index(0); }); - appendToBlock(b, [&](ValueRange) { std_constant_index(1); }); - appendToBlock(b, [&](ValueRange) { std_ret(); }); - // Get back to entry block and add a branch into "b". - appendToBlock(&f.front(), [&](ValueRange) { std_br(b, {}); }); - - // clang-format off - // CHECK-LABEL: @builder_blocks - // CHECK-NEXT: br ^bb1 - // CHECK-NEXT: ^bb1: // pred: ^bb0 - // CHECK-NEXT: constant 0 : index - // CHECK-NEXT: constant 1 : index - // CHECK-NEXT: return - // CHECK-NEXT: } - // clang-format on - f.print(llvm::outs(), OpPrintingFlags().useLocalScope()); - f.erase(); -} - -TEST_FUNC(builder_blocks) { - using namespace edsc::op; - auto f = makeFunction("builder_blocks"); - - OpBuilder builder(f.getBody()); - ScopedContext scope(builder, f.getLoc()); - Value c1(std_constant_int(42, 32)), c2(std_constant_int(1234, 32)); - ReturnOp ret = std_ret(); - - Block *b1 = createBlock({c1.getType(), c1.getType()}); - Block *b2 = buildInNewBlock({c1.getType(), c1.getType()}, - [&](ValueRange args) { std_br(b1, args); }); - // The insertion point within the toplevel function is now past b2, we will - // need to get back the entry block. - // This is what happens with unstructured control-flow. - appendToBlock(b1, [&](ValueRange args) { - Value r = args[0] + args[1]; - std_br(b2, {args[0], r}); - }); - // Get back to entry block and add a branch into b1. - appendToBlock(&f.front(), [&](ValueRange) { std_br(b1, {c1, c2}); }); - ret.erase(); - - // clang-format off - // CHECK-LABEL: @builder_blocks - // CHECK: %{{.*}} = constant 42 : i32 - // CHECK-NEXT: %{{.*}} = constant 1234 : i32 - // CHECK-NEXT: br ^bb1(%{{.*}}, %{{.*}} : i32, i32) - // CHECK-NEXT: ^bb1(%{{.*}}: i32, %{{.*}}: i32): // 2 preds: ^bb0, ^bb2 - // CHECK-NEXT: %{{.*}} = addi %{{.*}}, %{{.*}} : i32 - // CHECK-NEXT: br ^bb2(%{{.*}}, %{{.*}} : i32, i32) - // CHECK-NEXT: ^bb2(%{{.*}}: i32, %{{.*}}: i32): // pred: ^bb1 - // CHECK-NEXT: br ^bb1(%{{.*}}, %{{.*}} : i32, i32) - // CHECK-NEXT: } - // clang-format on - f.print(llvm::outs(), OpPrintingFlags().useLocalScope()); - f.erase(); -} - -TEST_FUNC(builder_cond_branch) { - auto f = makeFunction("builder_cond_branch", {}, - {IntegerType::get(&globalContext(), 1)}); - - OpBuilder builder(f.getBody()); - ScopedContext scope(builder, f.getLoc()); - Value c32(std_constant_int(32, 32)), c64(std_constant_int(64, 64)), - c42(std_constant_int(42, 32)); - ReturnOp ret = std_ret(); - - Block *b1 = buildInNewBlock(c32.getType(), [&](ValueRange) { std_ret(); }); - Block *b2 = buildInNewBlock({c64.getType(), c32.getType()}, - [&](ValueRange) { std_ret(); }); - // Get back to entry block and add a conditional branch. - appendToBlock(&f.front(), [&](ValueRange args) { - std_cond_br(args[0], b1, {c32}, b2, {c64, c42}); - }); - ret.erase(); - - // clang-format off - // CHECK-LABEL: @builder_cond_branch - // CHECK: %{{.*}} = constant 32 : i32 - // CHECK-NEXT: %{{.*}} = constant 64 : i64 - // CHECK-NEXT: %{{.*}} = constant 42 : i32 - // CHECK-NEXT: cond_br %{{.*}}, ^bb1(%{{.*}} : i32), ^bb2(%{{.*}}, %{{.*}} : i64, i32) - // CHECK-NEXT: ^bb1(%{{.*}}: i32): // pred: ^bb0 - // CHECK-NEXT: return - // CHECK-NEXT: ^bb2(%{{.*}}: i64, %{{.*}}: i32): // pred: ^bb0 - // CHECK-NEXT: return - // clang-format on - f.print(llvm::outs(), OpPrintingFlags().useLocalScope()); - f.erase(); -} - -TEST_FUNC(builder_helpers) { - using namespace edsc::op; - auto f32Type = FloatType::getF32(&globalContext()); - auto memrefType = - MemRefType::get({ShapedType::kDynamicSize, ShapedType::kDynamicSize, - ShapedType::kDynamicSize}, - f32Type, {}, 0); - auto f = - makeFunction("builder_helpers", {}, {memrefType, memrefType, memrefType}); - - OpBuilder builder(f.getBody()); - ScopedContext scope(builder, f.getLoc()); - // clang-format off - Value f7 = std_constant_float(llvm::APFloat(7.0f), f32Type); - MemRefBoundsCapture vA(f.getArgument(0)), vB(f.getArgument(1)), - vC(f.getArgument(2)); - AffineIndexedValue A(f.getArgument(0)), B(f.getArgument(1)), C(f.getArgument(2)); - Value lb0, lb1, lb2, ub0, ub1, ub2; - int64_t step0, step1, step2; - std::tie(lb0, ub0, step0) = vA.range(0); - std::tie(lb1, ub1, step1) = vA.range(1); - lb2 = vA.lb(2); - ub2 = vA.ub(2); - step2 = vA.step(2); - affineLoopNestBuilder({lb0, lb1}, {ub0, ub1}, {step0, step1}, [&](ValueRange ivs) { - Value i = ivs[0]; - Value j = ivs[1]; - affineLoopBuilder(lb2, ub2, step2, [&](Value k1){ - C(i, j, k1) = f7 + A(i, j, k1) + B(i, j, k1); - }); - affineLoopBuilder(lb2, ub2, step2, [&](Value k2){ - C(i, j, k2) += A(i, j, k2) + B(i, j, k2); - }); - }); - - // clang-format off - // CHECK-LABEL: @builder_helpers - // CHECK: affine.for %{{.*}} = affine_map<(d0) -> (d0)>({{.*}}) to affine_map<(d0) -> (d0)>({{.*}}) { - // CHECK-NEXT: affine.for %{{.*}} = affine_map<(d0) -> (d0)>({{.*}}) to affine_map<(d0) -> (d0)>({{.*}}) { - // CHECK-NEXT: affine.for %{{.*}} = affine_map<(d0) -> (d0)>({{.*}}) to affine_map<(d0) -> (d0)>({{.*}}) { - // CHECK-DAG: [[a:%.*]] = affine.load %arg0[%{{.*}}, %{{.*}}, %{{.*}}] : memref - // CHECK-DAG: [[b:%.*]] = addf {{.*}}, [[a]] : f32 - // CHECK-DAG: [[c:%.*]] = affine.load %arg1[%{{.*}}, %{{.*}}, %{{.*}}] : memref - // CHECK-DAG: [[d:%.*]] = addf [[b]], [[c]] : f32 - // CHECK-NEXT: affine.store [[d]], %{{.*}}[%{{.*}}, %{{.*}}, %{{.*}}] : memref - // CHECK-NEXT: } - // CHECK-NEXT: affine.for %{{.*}} = affine_map<(d0) -> (d0)>(%{{.*}}) to affine_map<(d0) -> (d0)>(%{{.*}}) { - // CHECK-DAG: [[a:%.*]] = affine.load %{{.*}}[%{{.*}}, %{{.*}}, %{{.*}}] : memref - // CHECK-DAG: [[b:%.*]] = affine.load %{{.*}}[%{{.*}}, %{{.*}}, %{{.*}}] : memref - // CHECK-DAG: [[c:%.*]] = addf [[b]], [[a]] : f32 - // CHECK-DAG: [[d:%.*]] = affine.load %{{.*}}[%{{.*}}, %{{.*}}, %{{.*}}] : memref - // CHECK-DAG: [[e:%.*]] = addf [[d]], [[c]] : f32 - // CHECK-NEXT: affine.store [[e]], %{{.*}}[%{{.*}}, %{{.*}}, %{{.*}}] : memref - // clang-format on - f.print(llvm::outs(), OpPrintingFlags().useLocalScope()); - f.erase(); -} - -TEST_FUNC(insertion_in_block) { - using namespace edsc::op; - auto indexType = IndexType::get(&globalContext()); - auto f = makeFunction("insertion_in_block", {}, {indexType, indexType}); - - OpBuilder builder(f.getBody()); - ScopedContext scope(builder, f.getLoc()); - std_constant_int(0, 32); - buildInNewBlock({}, [&](ValueRange) { std_constant_int(1, 32); }); - std_constant_int(2, 32); - // clang-format off - // CHECK-LABEL: @insertion_in_block - // CHECK: {{.*}} = constant 0 : i32 - // CHECK: {{.*}} = constant 2 : i32 - // CHECK: ^bb1: // no predecessors - // CHECK: {{.*}} = constant 1 : i32 - // clang-format on - f.print(llvm::outs(), OpPrintingFlags().useLocalScope()); - f.erase(); -} - -TEST_FUNC(zero_and_std_sign_extendi_op_i1_to_i8) { - using namespace edsc::op; - auto i1Type = IntegerType::get(&globalContext(), 1); - auto i8Type = IntegerType::get(&globalContext(), 8); - auto memrefType = MemRefType::get({}, i1Type, {}, 0); - auto f = makeFunction("zero_and_std_sign_extendi_op", {}, - {memrefType, memrefType}); - - OpBuilder builder(f.getBody()); - ScopedContext scope(builder, f.getLoc()); - AffineIndexedValue A(f.getArgument(0)); - AffineIndexedValue B(f.getArgument(1)); - // clang-format off - edsc::intrinsics::std_zero_extendi(A, i8Type); - edsc::intrinsics::std_sign_extendi(B, i8Type); - // CHECK-LABEL: @zero_and_std_sign_extendi_op - // CHECK: %[[SRC1:.*]] = affine.load - // CHECK: zexti %[[SRC1]] : i1 to i8 - // CHECK: %[[SRC2:.*]] = affine.load - // CHECK: sexti %[[SRC2]] : i1 to i8 - // clang-format on - f.print(llvm::outs(), OpPrintingFlags().useLocalScope()); - f.erase(); -} - -TEST_FUNC(operator_or) { - auto i1Type = IntegerType::get(&globalContext(), /*width=*/1); - auto f = makeFunction("operator_or", {}, {i1Type, i1Type}); - - OpBuilder builder(f.getBody()); - ScopedContext scope(builder, f.getLoc()); - - using op::operator||; - Value lhs(f.getArgument(0)); - Value rhs(f.getArgument(1)); - lhs || rhs; - - // clang-format off - // CHECK-LABEL: @operator_or - // CHECK: [[ARG0:%.*]]: i1, [[ARG1:%.*]]: i1 - // CHECK: or [[ARG0]], [[ARG1]] - // clang-format on - f.print(llvm::outs(), OpPrintingFlags().useLocalScope()); - f.erase(); -} - -TEST_FUNC(operator_and) { - auto i1Type = IntegerType::get(&globalContext(), /*width=*/1); - auto f = makeFunction("operator_and", {}, {i1Type, i1Type}); - - OpBuilder builder(f.getBody()); - ScopedContext scope(builder, f.getLoc()); - - using op::operator&&; - using op::negate; - Value lhs(f.getArgument(0)); - Value rhs(f.getArgument(1)); - negate(lhs && rhs); - - // clang-format off - // CHECK-LABEL: @operator_and - // CHECK: [[ARG0:%.*]]: i1, [[ARG1:%.*]]: i1 - // CHECK: [[AND:%.*]] = and [[ARG0]], [[ARG1]] - // CHECK: [[TRUE:%.*]] = constant true - // CHECK: subi [[TRUE]], [[AND]] : i1 - // clang-format on - f.print(llvm::outs(), OpPrintingFlags().useLocalScope()); - f.erase(); -} - -TEST_FUNC(divis_op_i32) { - using namespace edsc::op; - auto f = makeFunction("divis_op", {}, {}); - - OpBuilder builder(f.getBody()); - ScopedContext scope(builder, f.getLoc()); - auto i32Type = builder.getI32Type(); - std_divis(std_constant_int(10, i32Type), std_constant_int(2, i32Type)); - - // clang-format off - // CHECK-LABEL: @divis_op - // CHECK-DAG: {{.*}} = constant 10 - // CHECK-DAG: {{.*}} = constant 2 - // CHECK-NEXT: {{.*}} = divi_signed - // clang-format on - f.print(llvm::outs(), OpPrintingFlags().useLocalScope()); - f.erase(); -} - -TEST_FUNC(diviu_op_i32) { - using namespace edsc::op; - auto f = makeFunction("diviu_op", {}, {}); - - OpBuilder builder(f.getBody()); - ScopedContext scope(builder, f.getLoc()); - auto i32Type = builder.getI32Type(); - std_diviu(std_constant_int(10, i32Type), std_constant_int(2, i32Type)); - - // clang-format off - // CHECK-LABEL: @diviu_op - // CHECK-DAG: {{.*}} = constant 10 - // CHECK-DAG: {{.*}} = constant 2 - // CHECK-NEXT: {{.*}} = divi_unsigned - // clang-format on - f.print(llvm::outs(), OpPrintingFlags().useLocalScope()); - f.erase(); -} - -TEST_FUNC(fpext_f32_f64) { - using namespace edsc::op; - auto f = makeFunction("fpext", {}, {}); - - OpBuilder builder(f.getBody()); - ScopedContext scope(builder, f.getLoc()); - auto f32Type = builder.getF32Type(); - auto f64Type = builder.getF64Type(); - std_fpext(std_constant_float(llvm::APFloat(10.0f), f32Type), f64Type); - - // clang-format off - // CHECK-LABEL: @fpext - // CHECK: {{.*}} = constant 1.0 - // CHECK-NEXT: {{.*}} = fpext - // clang-format on - f.print(llvm::outs(), OpPrintingFlags().useLocalScope()); - f.erase(); -} - -TEST_FUNC(fptrunc_f32_bf16) { - using namespace edsc::op; - auto f = makeFunction("fptrunc", {}, {}); - - OpBuilder builder(f.getBody()); - ScopedContext scope(builder, f.getLoc()); - auto f32Type = builder.getF32Type(); - auto bf16Type = builder.getBF16Type(); - std_fptrunc(std_constant_float(llvm::APFloat(10.0f), f32Type), bf16Type); - - // clang-format off - // CHECK-LABEL: @fptrunc - // CHECK: {{.*}} = constant 1.0 - // CHECK-NEXT: {{.*}} = fptrunc - // clang-format on - f.print(llvm::outs(), OpPrintingFlags().useLocalScope()); - f.erase(); -} - -TEST_FUNC(select_op_i32) { - using namespace edsc::op; - auto i32Type = IntegerType::get(&globalContext(), 32); - auto memrefType = MemRefType::get( - {ShapedType::kDynamicSize, ShapedType::kDynamicSize}, i32Type, {}, 0); - auto f = makeFunction("select_op", {}, {memrefType}); - - OpBuilder builder(f.getBody()); - ScopedContext scope(builder, f.getLoc()); - Value zero = std_constant_index(0), one = std_constant_index(1); - MemRefBoundsCapture vA(f.getArgument(0)); - AffineIndexedValue A(f.getArgument(0)); - affineLoopNestBuilder({zero, zero}, {one, one}, {1, 1}, [&](ValueRange ivs) { - using namespace edsc::op; - Value i = ivs[0], j = ivs[1]; - std_select(eq(i, zero), A(zero, zero), A(i, j)); - std_select(ne(i, zero), A(zero, zero), A(i, j)); - std_select(slt(i, zero), A(zero, zero), A(i, j)); - std_select(sle(i, zero), A(zero, zero), A(i, j)); - std_select(sgt(i, zero), A(zero, zero), A(i, j)); - std_select(sge(i, zero), A(zero, zero), A(i, j)); - std_select(ult(i, zero), A(zero, zero), A(i, j)); - std_select(ule(i, zero), A(zero, zero), A(i, j)); - std_select(ugt(i, zero), A(zero, zero), A(i, j)); - std_select(uge(i, zero), A(zero, zero), A(i, j)); - }); - - // clang-format off - // CHECK-LABEL: @select_op - // CHECK: affine.for %{{.*}} = 0 to 1 { - // CHECK-NEXT: affine.for %{{.*}} = 0 to 1 { - // CHECK-DAG: {{.*}} = cmpi eq - // CHECK-DAG: {{.*}} = affine.load - // CHECK-DAG: {{.*}} = affine.load - // CHECK-NEXT: {{.*}} = select - // CHECK-DAG: {{.*}} = cmpi ne - // CHECK-DAG: {{.*}} = affine.load - // CHECK-DAG: {{.*}} = affine.load - // CHECK-NEXT: {{.*}} = select - // CHECK-DAG: {{.*}} = cmpi slt - // CHECK-DAG: {{.*}} = affine.load - // CHECK-DAG: {{.*}} = affine.load - // CHECK-NEXT: {{.*}} = select - // CHECK-DAG: {{.*}} = cmpi sle - // CHECK-DAG: {{.*}} = affine.load - // CHECK-DAG: {{.*}} = affine.load - // CHECK-NEXT: {{.*}} = select - // CHECK-DAG: {{.*}} = cmpi sgt - // CHECK-DAG: {{.*}} = affine.load - // CHECK-DAG: {{.*}} = affine.load - // CHECK-NEXT: {{.*}} = select - // CHECK-DAG: {{.*}} = cmpi sge - // CHECK-DAG: {{.*}} = affine.load - // CHECK-DAG: {{.*}} = affine.load - // CHECK-NEXT: {{.*}} = select - // CHECK-DAG: {{.*}} = cmpi ult - // CHECK-DAG: {{.*}} = affine.load - // CHECK-DAG: {{.*}} = affine.load - // CHECK-NEXT: {{.*}} = select - // CHECK-DAG: {{.*}} = cmpi ule - // CHECK-DAG: {{.*}} = affine.load - // CHECK-DAG: {{.*}} = affine.load - // CHECK-NEXT: {{.*}} = select - // CHECK-DAG: {{.*}} = cmpi ugt - // CHECK-DAG: {{.*}} = affine.load - // CHECK-DAG: {{.*}} = affine.load - // CHECK-NEXT: {{.*}} = select - // CHECK-DAG: {{.*}} = cmpi uge - // CHECK-DAG: {{.*}} = affine.load - // CHECK-DAG: {{.*}} = affine.load - // CHECK-NEXT: {{.*}} = select - // clang-format on - f.print(llvm::outs(), OpPrintingFlags().useLocalScope()); - f.erase(); -} - -TEST_FUNC(select_op_f32) { - auto f32Type = FloatType::getF32(&globalContext()); - auto memrefType = MemRefType::get( - {ShapedType::kDynamicSize, ShapedType::kDynamicSize}, f32Type, {}, 0); - auto f = makeFunction("select_op", {}, {memrefType, memrefType}); - - OpBuilder builder(f.getBody()); - ScopedContext scope(builder, f.getLoc()); - // clang-format off - Value zero = std_constant_index(0), one = std_constant_index(1); - MemRefBoundsCapture vA(f.getArgument(0)), vB(f.getArgument(1)); - AffineIndexedValue A(f.getArgument(0)), B(f.getArgument(1)); - affineLoopNestBuilder({zero, zero}, {one, one}, {1, 1}, [&](ValueRange ivs) { - using namespace edsc::op; - Value i = ivs[0], j = ivs[1]; - std_select(eq(B(i, j), B(i + one, j)), A(zero, zero), A(i, j)); - std_select(ne(B(i, j), B(i + one, j)), A(zero, zero), A(i, j)); - std_select(sge(B(i, j), B(i + one, j)), A(zero, zero), A(i, j)); - std_select(sle(B(i, j), B(i + one, j)), A(zero, zero), A(i, j)); - std_select(slt(B(i, j), B(i + one, j)), A(zero, zero), A(i, j)); - std_select(sgt(B(i, j), B(i + one, j)), A(zero, zero), A(i, j)); - std_select(uge(B(i, j), B(i + one, j)), A(zero, zero), A(i, j)); - std_select(ule(B(i, j), B(i + one, j)), A(zero, zero), A(i, j)); - std_select(ult(B(i, j), B(i + one, j)), A(zero, zero), A(i, j)); - std_select(ugt(B(i, j), B(i + one, j)), A(zero, zero), A(i, j)); - }); - - // clang-format off - // CHECK-LABEL: @select_op - // CHECK: affine.for %{{.*}} = 0 to 1 { - // CHECK-NEXT: affine.for %{{.*}} = 0 to 1 { - // CHECK-DAG: cmpf oeq - // CHECK-DAG: affine.load - // CHECK-DAG: affine.load - // CHECK-DAG: affine.load - // CHECK-DAG: affine.load - // CHECK-DAG: affine.apply - // CHECK-NEXT: select - // CHECK-DAG: cmpf one - // CHECK-DAG: affine.load - // CHECK-DAG: affine.load - // CHECK-DAG: affine.load - // CHECK-DAG: affine.load - // CHECK-DAG: affine.apply - // CHECK-NEXT: select - // CHECK-DAG: cmpf oge - // CHECK-DAG: affine.load - // CHECK-DAG: affine.load - // CHECK-DAG: affine.load - // CHECK-DAG: affine.load - // CHECK-DAG: affine.apply - // CHECK-NEXT: select - // CHECK-DAG: cmpf ole - // CHECK-DAG: affine.load - // CHECK-DAG: affine.load - // CHECK-DAG: affine.load - // CHECK-DAG: affine.load - // CHECK-DAG: affine.apply - // CHECK-NEXT: select - // CHECK-DAG: cmpf olt - // CHECK-DAG: affine.load - // CHECK-DAG: affine.load - // CHECK-DAG: affine.load - // CHECK-DAG: affine.load - // CHECK-DAG: affine.apply - // CHECK-NEXT: select - // CHECK-DAG: cmpf ogt - // CHECK-DAG: affine.load - // CHECK-DAG: affine.load - // CHECK-DAG: affine.load - // CHECK-DAG: affine.load - // CHECK-DAG: affine.apply - // CHECK-NEXT: select - // CHECK-DAG: cmpf oge - // CHECK-DAG: affine.load - // CHECK-DAG: affine.load - // CHECK-DAG: affine.load - // CHECK-DAG: affine.load - // CHECK-DAG: affine.apply - // CHECK-NEXT: select - // CHECK-DAG: cmpf ole - // CHECK-DAG: affine.load - // CHECK-DAG: affine.load - // CHECK-DAG: affine.load - // CHECK-DAG: affine.load - // CHECK-DAG: affine.apply - // CHECK-NEXT: select - // CHECK-DAG: cmpf olt - // CHECK-DAG: affine.load - // CHECK-DAG: affine.load - // CHECK-DAG: affine.load - // CHECK-DAG: affine.load - // CHECK-DAG: affine.apply - // CHECK-NEXT: select - // CHECK-DAG: cmpf ogt - // CHECK-DAG: affine.load - // CHECK-DAG: affine.load - // CHECK-DAG: affine.load - // CHECK-DAG: affine.load - // CHECK-DAG: affine.apply - // CHECK-NEXT: select - // clang-format on - f.print(llvm::outs(), OpPrintingFlags().useLocalScope()); - f.erase(); -} - -// Inject an EDSC-constructed computation to exercise imperfectly nested 2-d -// tiling. -TEST_FUNC(tile_2d) { - auto memrefType = - MemRefType::get({ShapedType::kDynamicSize, ShapedType::kDynamicSize, - ShapedType::kDynamicSize}, - FloatType::getF32(&globalContext()), {}, 0); - auto f = makeFunction("tile_2d", {}, {memrefType, memrefType, memrefType}); - - OpBuilder builder(f.getBody()); - ScopedContext scope(builder, f.getLoc()); - Value zero = std_constant_index(0); - MemRefBoundsCapture vA(f.getArgument(0)), vB(f.getArgument(1)), - vC(f.getArgument(2)); - AffineIndexedValue A(f.getArgument(0)), B(f.getArgument(1)), - C(f.getArgument(2)); - Value i, j, k1, k2; - Value M(vC.ub(0)), N(vC.ub(1)), O(vC.ub(2)); - - // clang-format off - using namespace edsc::op; - affineLoopNestBuilder({zero, zero}, {M, N}, {1, 1}, [&](ValueRange ivs) { - i = ivs[0]; - j = ivs[1]; - affineLoopBuilder(zero, O, 1, [&](Value k) { - k1 = k; - C(i, j, k1) = A(i, j, k1) + B(i, j, k1); - }); - affineLoopBuilder(zero, O, 1, [&](Value k) { - k2 = k; - C(i, j, k2) = A(i, j, k2) + B(i, j, k2); - }); - }); - // clang-format on - - auto li = getForInductionVarOwner(i), lj = getForInductionVarOwner(j), - lk1 = getForInductionVarOwner(k1), lk2 = getForInductionVarOwner(k2); - auto indicesL1 = mlir::tile({li, lj}, {512, 1024}, {lk1, lk2}); - auto lii1 = indicesL1[0][0], ljj1 = indicesL1[1][0]; - mlir::tile({ljj1, lii1}, {32, 16}, ljj1); - - // clang-format off - // CHECK-LABEL: func @tile_2d - // CHECK: %[[ZERO:.*]] = constant 0 : index - // CHECK: %[[M:[0-9]+]] = memref.dim %arg2, %c0{{[_0-9]*}} : memref - // CHECK: %[[N:[0-9]+]] = memref.dim %arg2, %c1{{[_0-9]*}} : memref - // CHECK: %[[P:[0-9]+]] = memref.dim %arg2, %c2{{[_0-9]*}} : memref - // CHECK: affine.for %{{.*}} = affine_map<(d0) -> (d0)>(%[[ZERO]]) to affine_map<(d0) -> (d0)>(%[[M]]) step 512 { - // CHECK-NEXT: affine.for %{{.*}} = affine_map<(d0) -> (d0)>(%[[ZERO]]) to affine_map<(d0) -> (d0)>(%[[N]]) step 1024 { - // CHECK-NEXT: affine.for %{{.*}} = affine_map<(d0) -> (d0)>(%[[ZERO]]) to affine_map<(d0) -> (d0)>(%[[P]]) { - // CHECK-NEXT: affine.for %{{.*}} = max affine_map<(d0) -> (0, d0)>(%{{.*}}) to min affine_map<(d0)[s0] -> (s0, d0 + 512)>(%{{.*}})[%[[M]]] step 16 { - // CHECK-NEXT: affine.for %{{.*}} = max affine_map<(d0) -> (0, d0)>(%{{.*}}) to min affine_map<(d0)[s0] -> (s0, d0 + 1024)>(%{{.*}})[%[[N]]] step 32 { - // CHECK-NEXT: affine.for %{{.*}} = max affine_map<(d0, d1) -> (0, d0, d1)>(%{{.*}}, %{{.*}}) to min affine_map<(d0, d1)[s0] -> (s0, d0 + 1024, d1 + 32)>(%{{.*}}, %{{.*}})[%[[N]]] { - // CHECK-NEXT: affine.for %{{.*}} = max affine_map<(d0, d1) -> (0, d0, d1)>(%{{.*}}, %{{.*}}) to min affine_map<(d0, d1)[s0] -> (s0, d0 + 512, d1 + 16)>(%{{.*}}, %{{.*}})[%[[M]]] { - // CHECK-NEXT: {{.*}} = affine.load {{.*}}[%{{.*}}, %{{.*}}, %{{.*}}] : memref - // CHECK-NEXT: {{.*}} = affine.load {{.*}}[%{{.*}}, %{{.*}}, %{{.*}}] : memref - // CHECK-NEXT: {{.*}} = addf {{.*}}, {{.*}} : f32 - // CHECK-NEXT: affine.store {{.*}}, {{.*}}[%{{.*}}, %{{.*}}, %{{.*}}] : memref - // CHECK: } - // CHECK-NEXT: } - // CHECK-NEXT: } - // CHECK-NEXT: } - // CHECK-NEXT: } - // CHECK-NEXT: affine.for %{{.*}} = affine_map<(d0) -> (d0)>(%[[ZERO]]) to affine_map<(d0) -> (d0)>(%[[P]]) { - // CHECK-NEXT: affine.for %{{.*}} = max affine_map<(d0) -> (0, d0)>(%{{.*}}) to min affine_map<(d0)[s0] -> (s0, d0 + 512)>(%{{.*}})[%[[M]]] { - // CHECK-NEXT: affine.for %{{.*}} = max affine_map<(d0) -> (0, d0)>(%{{.*}}) to min affine_map<(d0)[s0] -> (s0, d0 + 1024)>(%{{.*}})[%[[N]]] { - // CHECK-NEXT: {{.*}} = affine.load {{.*}}[%{{.*}}, %{{.*}}, %{{.*}}] : memref - // CHECK-NEXT: {{.*}} = affine.load {{.*}}[%{{.*}}, %{{.*}}, %{{.*}}] : memref - // CHECK-NEXT: {{.*}}= addf {{.*}}, {{.*}} : f32 - // CHECK-NEXT: affine.store {{.*}}, {{.*}}[%{{.*}}, %{{.*}}, %{{.*}}] : memref - // clang-format on - f.print(llvm::outs(), OpPrintingFlags().useLocalScope()); - f.erase(); -} - -// Exercise StdIndexedValue for loads and stores. -TEST_FUNC(indirect_access) { - using namespace edsc::op; - auto memrefType = MemRefType::get({ShapedType::kDynamicSize}, - FloatType::getF32(&globalContext()), {}, 0); - auto f = makeFunction("indirect_access", {}, - {memrefType, memrefType, memrefType, memrefType}); - - OpBuilder builder(f.getBody()); - ScopedContext scope(builder, f.getLoc()); - Value zero = std_constant_index(0); - MemRefBoundsCapture vC(f.getArgument(2)); - AffineIndexedValue B(f.getArgument(1)), D(f.getArgument(3)); - MemRefIndexedValue A(f.getArgument(0)), C(f.getArgument(2)); - Value N(vC.ub(0)); - - // clang-format off - affineLoopBuilder(zero, N, 1, [&](Value i) { - C((Value)D(i)) = A((Value)B(i)); - }); - // clang-format on - - // clang-format off - // CHECK-LABEL: func @indirect_access - // CHECK-SAME: (%[[ARG0:.*]]: memref, %[[ARG1:.*]]: memref, %[[ARG2:.*]]: memref, %[[ARG3:.*]]: memref) - // CHECK-DAG: [[B:%.*]] = affine.load %[[ARG1]] - // CHECK-DAG: [[D:%.*]] = affine.load %[[ARG3]] - // CHECK: load %{{.*}}{{\[}}[[B]]{{\]}} - // CHECK: store %{{.*}}, %{{.*}}{{\[}}[[D]]{{\]}} - // clang-format on - f.print(llvm::outs(), OpPrintingFlags().useLocalScope()); - f.erase(); -} - -// Exercise affine loads and stores build with empty maps. -TEST_FUNC(empty_map_load_store) { - using namespace edsc::op; - auto memrefType = - MemRefType::get({}, FloatType::getF32(&globalContext()), {}, 0); - auto f = makeFunction("empty_map_load_store", {}, - {memrefType, memrefType, memrefType, memrefType}); - - OpBuilder builder(f.getBody()); - ScopedContext scope(builder, f.getLoc()); - Value zero = std_constant_index(0); - Value one = std_constant_index(1); - AffineIndexedValue input(f.getArgument(0)), res(f.getArgument(1)); - - // clang-format off - affineLoopBuilder(zero, one, 1, [&](Value) { - res() = input(); - }); - // clang-format on - - // clang-format off - // CHECK-LABEL: func @empty_map_load_store( - // CHECK: [[A:%.*]] = affine.load %{{.*}}[] - // CHECK: affine.store [[A]], %{{.*}}[] - // clang-format on - f.print(llvm::outs(), OpPrintingFlags().useLocalScope()); - f.erase(); -} - -// clang-format off -// CHECK-LABEL: func @affine_if_op -// CHECK: affine.if affine_set<([[d0:.*]], [[d1:.*]]){{\[}}[[s0:.*]], [[s1:.*]]{{\]}} -// CHECK-NOT: else -// CHECK: affine.if affine_set<([[d0:.*]], [[d1:.*]]){{\[}}[[s0:.*]], [[s1:.*]]{{\]}} -// CHECK-NEXT: } else { -// clang-format on -TEST_FUNC(affine_if_op) { - using namespace edsc::op; - auto f32Type = FloatType::getF32(&globalContext()); - auto memrefType = MemRefType::get( - {ShapedType::kDynamicSize, ShapedType::kDynamicSize}, f32Type, {}, 0); - auto f = makeFunction("affine_if_op", {}, {memrefType}); - - OpBuilder builder(f.getBody()); - ScopedContext scope(builder, f.getLoc()); - - Value zero = std_constant_index(0), ten = std_constant_index(10); - - SmallVector isEq = {false, false, false, false}; - SmallVector affineExprs = { - builder.getAffineDimExpr(0), // d0 >= 0 - builder.getAffineDimExpr(1), // d1 >= 0 - builder.getAffineSymbolExpr(0), // s0 >= 0 - builder.getAffineSymbolExpr(1) // s1 >= 0 - }; - auto intSet = IntegerSet::get(2, 2, affineExprs, isEq); - - SmallVector affineIfArgs = {zero, zero, ten, ten}; - intrinsics::affine_if(intSet, affineIfArgs, /*withElseRegion=*/false); - intrinsics::affine_if(intSet, affineIfArgs, /*withElseRegion=*/true); - - f.print(llvm::outs(), OpPrintingFlags().useLocalScope()); - f.erase(); -} - -// clang-format off -// CHECK-LABEL: func @linalg_generic_pointwise -// CHECK: linalg.generic { -// CHECK-SAME: indexing_maps = [affine_map<(d0, d1) -> (d0, d1)>, affine_map<(d0, d1) -> (d0, d1)>, affine_map<(d0, d1) -> (d0, d1)>], -// CHECK-SAME: iterator_types = ["parallel", "parallel"]} -// CHECK-SAME: ins({{.*}}memref, memref) -// CHECK-SAME: outs({{.*}}memref) -// CHECK: addf -// CHECK: linalg.generic { -// CHECK-SAME: indexing_maps = [affine_map<(d0, d1) -> (d0, d1)>, affine_map<(d0, d1) -> (d0, d1)>, affine_map<(d0, d1) -> (d0, d1)>], -// CHECK-SAME: iterator_types = ["parallel", "parallel"]} -// CHECK-SAME: ins({{.*}}memref, memref) -// CHECK-SAME: outs({{.*}}memref) -// CHECK: cmpf ogt -// CHECK: select -// CHECK: linalg.generic { -// CHECK-SAME: indexing_maps = [affine_map<(d0, d1) -> (d0, d1)>, affine_map<(d0, d1) -> (d0, d1)>], -// CHECK-SAME: iterator_types = ["parallel", "parallel"]} -// CHECK-SAME: ins(%{{[a-z0-9]*}} : memref) -// CHECK-SAME: outs(%{{[a-z0-9]*}} : memref) -// CHECK: tanh -// clang-format on -TEST_FUNC(linalg_generic_pointwise_test) { - using namespace edsc; - using namespace edsc::ops; - - auto f32Type = FloatType::getF32(&globalContext()); - auto memrefType = MemRefType::get( - {ShapedType::kDynamicSize, ShapedType::kDynamicSize}, f32Type, {}, 0); - auto f = makeFunction("linalg_generic_pointwise", {}, - {memrefType, memrefType, memrefType}); - - OpBuilder builder(f.getBody()); - ScopedContext scope(builder, f.getLoc()); - Value A(f.getArgument(0)), B(f.getArgument(1)), C(f.getArgument(2)); - AffineExpr i, j; - bindDims(&globalContext(), i, j); - StructuredIndexed SA(A), SB(B), SC(C); - linalg_generic_pointwise_add(SA({i, j}), SB({i, j}), SC({i, j})); - linalg_generic_pointwise_max(SA({i, j}), SB({i, j}), SC({i, j})); - linalg_generic_pointwise_tanh(SA({i, j}), SC({i, j})); - - f.print(llvm::outs(), OpPrintingFlags().useLocalScope()); - f.erase(); -} - -// clang-format off -// CHECK-LABEL: func @linalg_generic_matmul -// CHECK: linalg.generic { -// CHECK-SAME: indexing_maps = [affine_map<(d0, d1, d2) -> (d0, d2)>, affine_map<(d0, d1, d2) -> (d2, d1)>, affine_map<(d0, d1, d2) -> (d0, d1)>], -// CHECK-SAME: iterator_types = ["parallel", "parallel", "reduction"]} -// CHECK-SAME: ins(%{{[a-z0-9]*}}, %{{[a-z0-9]*}} : memref, memref) -// CHECK-SAME: outs(%{{[a-z0-9]*}} : memref) -/// CHECK: ^bb0(%[[a0:.*]]: f32, %[[a1:.*]]: f32, %[[a2:.*]]: f32): -// CHECK: %[[a3:.*]] = mulf %[[a0]], %[[a1]] : f32 -// CHECK: %[[a4:.*]] = addf %[[a2]], %[[a3]] : f32 -// CHECK: linalg.yield %[[a4]] : f32 -// CHECK: } -// clang-format on -TEST_FUNC(linalg_generic_matmul_test) { - using namespace edsc; - using namespace edsc::ops; - - auto f32Type = FloatType::getF32(&globalContext()); - auto memrefType = MemRefType::get( - {ShapedType::kDynamicSize, ShapedType::kDynamicSize}, f32Type, {}, 0); - auto f = makeFunction("linalg_generic_matmul", {}, - {memrefType, memrefType, memrefType}); - - OpBuilder builder(f.getBody()); - ScopedContext scope(builder, f.getLoc()); - linalg_generic_matmul(f.getArguments()); - - f.print(llvm::outs(), OpPrintingFlags().useLocalScope()); - f.erase(); -} - -// clang-format off -// CHECK-LABEL: func @linalg_generic_conv_nhwc -// CHECK: linalg.generic { -// CHECK-SAME: indexing_maps = [affine_map<(d0, d1, d2, d3, d4, d5, d6) -> (d0, d2 * 3 + d4 * 5, d3 * 4 + d5 * 6, d6)>, -// CHECK-SAME: affine_map<(d0, d1, d2, d3, d4, d5, d6) -> (d4, d5, d6, d1)>, -// CHECK-SAME: affine_map<(d0, d1, d2, d3, d4, d5, d6) -> (d0, d2, d3, d1)>], -// CHECK-SAME: iterator_types = ["parallel", "parallel", "parallel", "parallel", "reduction", "reduction", "reduction"]} -// CHECK-SAME: ins(%{{[a-z0-9]*}}, %{{[a-z0-9]*}} : memref, memref) -// CHECK-SAME: outs(%{{[a-z0-9]*}} : memref) -/// CHECK: ^bb0(%[[a0:.*]]: f32, %[[a1:.*]]: f32, %[[a2:.*]]: f32): -// CHECK: %[[a3:.*]] = mulf %[[a0]], %[[a1]] : f32 -// CHECK: %[[a4:.*]] = addf %[[a2]], %[[a3]] : f32 -// CHECK: linalg.yield %[[a4]] : f32 -// CHECK: } -// clang-format on -TEST_FUNC(linalg_generic_conv_nhwc) { - using namespace edsc; - using namespace edsc::ops; - - auto f32Type = FloatType::getF32(&globalContext()); - auto memrefType = - MemRefType::get({ShapedType::kDynamicSize, ShapedType::kDynamicSize, - ShapedType::kDynamicSize, ShapedType::kDynamicSize}, - f32Type, {}, 0); - auto f = makeFunction("linalg_generic_conv_nhwc", {}, - {memrefType, memrefType, memrefType}); - - OpBuilder builder(f.getBody()); - ScopedContext scope(builder, f.getLoc()); - linalg_generic_conv_nhwc(f.getArguments(), - /*strides=*/{3, 4}, /*dilations=*/{5, 6}); - - f.print(llvm::outs(), OpPrintingFlags().useLocalScope()); - f.erase(); -} - -// clang-format off -// CHECK-LABEL: func @linalg_generic_dilated_conv_nhwc -// CHECK: linalg.generic { -// CHECK-SAME: indexing_maps = [affine_map<(d0, d1, d2, d3, d4, d5, d6) -> (d0, d3 * 3 + d5 * 5, d4 * 4 + d6 * 6, d2)>, -// CHECK-SAME: affine_map<(d0, d1, d2, d3, d4, d5, d6) -> (d5, d6, d2, d1)>, -// CHECK-SAME: affine_map<(d0, d1, d2, d3, d4, d5, d6) -> (d0, d3, d4, d1 + d2 * 7)>], -// CHECK-SAME: iterator_types = ["parallel", "parallel", "parallel", "parallel", "parallel", "reduction", "reduction"]} -// CHECK-SAME: ins(%{{[a-z0-9]*}}, %{{[a-z0-9]*}} : memref, memref) -// CHECK-SAME: outs(%{{[a-z0-9]*}} : memref) -// CHECK: ^bb0(%[[a0:.*]]: f32, %[[a1:.*]]: f32, %[[a2:.*]]: f32): -// CHECK: %[[a3:.*]] = mulf %[[a0]], %[[a1]] : f32 -// CHECK: %[[a4:.*]] = addf %[[a2]], %[[a3]] : f32 -// CHECK: linalg.yield %[[a4]] : f32 -// CHECK: } -// clang-format on -TEST_FUNC(linalg_generic_dilated_conv_nhwc) { - using namespace edsc; - using namespace edsc::ops; - - auto f32Type = FloatType::getF32(&globalContext()); - auto memrefType = - MemRefType::get({ShapedType::kDynamicSize, ShapedType::kDynamicSize, - ShapedType::kDynamicSize, ShapedType::kDynamicSize}, - f32Type, {}, 0); - auto f = makeFunction("linalg_generic_dilated_conv_nhwc", {}, - {memrefType, memrefType, memrefType}); - - OpBuilder builder(f.getBody()); - ScopedContext scope(builder, f.getLoc()); - linalg_generic_dilated_conv_nhwc(f.getArguments(), - /*depth_multiplier=*/7, - /*strides=*/{3, 4}, /*dilations=*/{5, 6}); - - f.print(llvm::outs(), OpPrintingFlags().useLocalScope()); - f.erase(); -} - -// clang-format off -// CHECK-LABEL: func @linalg_metadata_ops -// CHECK: linalg.reshape {{.*}} {{\[}}[0, 1], [2]] : memref<4x8x16xf32> into memref<32x16xf32> -// CHECK: linalg.reshape {{.*}} {{\[}}[0, 1], [2]] : memref<32x16xf32> into memref<4x8x16xf32> -// clang-format on -TEST_FUNC(linalg_metadata_ops) { - using linalg::ReassociationExprs; - - auto f32Type = FloatType::getF32(&globalContext()); - auto memrefType = MemRefType::get({4, 8, 16}, f32Type, {}, 0); - auto f = makeFunction("linalg_metadata_ops", {}, {memrefType}); - - OpBuilder builder(f.getBody()); - ScopedContext scope(builder, f.getLoc()); - AffineExpr i, j, k; - bindDims(&globalContext(), i, j, k); - Value v(f.getArgument(0)); - SmallVector maps = {ReassociationExprs({i, j}), - ReassociationExprs({k})}; - auto reshaped = linalg_reshape(v, maps); - linalg_reshape(memrefType, reshaped, maps); - - f.print(llvm::outs(), OpPrintingFlags().useLocalScope()); - f.erase(); -} - -// clang-format off -// CHECK-LABEL: func @linalg_tensors -// CHECK: linalg.generic { -// CHECK-SAME: indexing_maps = [affine_map<(d0, d1) -> (d0, d1)>, affine_map<(d0, d1) -> (d0, d1)>, affine_map<(d0, d1) -> (d0, d1)>], -// CHECK-SAME: iterator_types = ["parallel", "parallel"]} -// CHECK-SAME: ins(%{{[a-z0-9]*}}, %{{[a-z0-9]*}} : tensor, memref) -// CHECK: addf -// CHECK: } -> tensor -// CHECK: linalg.generic { -// CHECK-SAME: indexing_maps = [affine_map<(d0, d1) -> (d0, d1)>, affine_map<(d0, d1) -> (d0, d1)>, affine_map<(d0, d1) -> (d0, d1)>], -// CHECK-SAME: iterator_types = ["parallel", "parallel"]} -// CHECK-SAME: ins(%{{[a-z0-9]*}}, %{{[a-z0-9]*}} : tensor, tensor) -// CHECK: cmpf ogt -// CHECK: select -// CHECK: } -> tensor -// CHECK: linalg.generic { -// CHECK-SAME: indexing_maps = [affine_map<(d0, d1) -> (d0, d1)>, affine_map<(d0, d1) -> (d0, d1)>], -// CHECK-SAME: iterator_types = ["parallel", "parallel"]} -// CHECK-SAME: ins(%{{[a-z0-9]*}} : tensor) -// CHECK: tanh -// CHECK: } -> tensor -// CHECK: linalg.generic { -// CHECK-SAME: indexing_maps = [affine_map<(d0, d1, d2) -> (d0, d2)>, -// CHECK-SAME: affine_map<(d0, d1, d2) -> (d2, d1)>, -// CHECK-SAME: affine_map<(d0, d1, d2) -> (d0, d1)>], -// CHECK-SAME: iterator_types = ["parallel", "parallel", "reduction"]} -// CHECK-SAME: ins(%{{[a-z0-9]*}}, %{{[a-z0-9]*}} : tensor, memref) -// CHECK: mulf -// CHECK: } -> tensor -// CHECK: linalg.generic { -// CHECK-SAME: indexing_maps = [affine_map<(d0, d1, d2) -> (d0, d2)>, -// CHECK-SAME: affine_map<(d0, d1, d2) -> (d2, d1)>, -// CHECK-SAME: affine_map<(d0, d1, d2) -> (d0, d1)>], -// CHECK-SAME: iterator_types = ["parallel", "parallel", "reduction"] -// CHECK-SAME: ins(%{{[a-z0-9]*}}, %{{[a-z0-9]*}} : tensor, memref) -// CHECK-SAME: outs(%{{[a-z0-9]*}} : tensor) -// CHECK: mulf -// CHECK: addf -// CHECK: } -> tensor -// clang-format on -TEST_FUNC(linalg_tensors_test) { - using namespace edsc; - using namespace edsc::ops; - - auto f32Type = FloatType::getF32(&globalContext()); - auto memrefType = MemRefType::get( - {ShapedType::kDynamicSize, ShapedType::kDynamicSize}, f32Type, {}, 0); - auto tensorType = RankedTensorType::get( - {ShapedType::kDynamicSize, ShapedType::kDynamicSize}, f32Type); - auto f = - makeFunction("linalg_tensors", {}, {tensorType, memrefType, tensorType}); - - OpBuilder builder(f.getBody()); - ScopedContext scope(builder, f.getLoc()); - Value A(f.getArgument(0)), B(f.getArgument(1)), C(f.getArgument(2)); - AffineExpr i, j; - bindDims(&globalContext(), i, j); - StructuredIndexed SA(A), SB(B), SC(C); - Value added = linalg_generic_pointwise_add(SA({i, j}), SB({i, j}), SC({i, j})) - ->getResult(0); - Value maxed = linalg_generic_pointwise_max( - SA({i, j}), StructuredIndexed(added)({i, j}), SC({i, j})) - ->getResult(0); - Value tanhed = linalg_generic_pointwise_tanh(StructuredIndexed(maxed)({i, j}), - SC({i, j})) - ->getResult(0); - Value o1 = linalg_generic_matmul(A, B, tanhed, tensorType)->getResult(0); - linalg_generic_matmul(A, B, o1, tensorType); - - f.print(llvm::outs(), OpPrintingFlags().useLocalScope()); - f.erase(); -} - -TEST_FUNC(vector_extractelement_op_i32) { - using namespace edsc::op; - auto f = makeFunction("vector_extractelement_op", {}, {}); - - OpBuilder builder(f.getBody()); - ScopedContext scope(builder, f.getLoc()); - auto i32Type = builder.getI32Type(); - auto vectorType = VectorType::get(/*shape=*/{8}, i32Type); - vector_extract_element( - i32Type, std_constant(vectorType, builder.getI32VectorAttr({10})), - std_constant_int(0, i32Type)); - - // clang-format off - // CHECK-LABEL: @vector_extractelement_op - // CHECK-DAG: {{.*}} = constant dense<10> - // CHECK-DAG: {{.*}} = constant 0 - // CHECK-NEXT: {{.*}} = vector.extractelement - // clang-format on - f.print(llvm::outs(), OpPrintingFlags().useLocalScope()); - f.erase(); -} - -// clang-format off -// CHECK-LABEL: func @memref_vector_matmul_test( -// CHECK-SAME: %[[A:.*]]: memref>, -// CHECK-SAME: %[[B:.*]]: memref>, -// CHECK-SAME: %[[C:.*]]: memref>) -// CHECK: linalg.generic {{{.*}}} -// CHECK-SAME: ins(%[[A]], %[[B]] : memref>, memref>) -// CHECK-SAME: outs(%[[C]] : memref>) -// CHECK: vector.contract{{.*}}[affine_map<(d0, d1, d2) -> (d0, d2)>, -// CHECK-SAME: affine_map<(d0, d1, d2) -> (d2, d1)>, -// CHECK-SAME: affine_map<(d0, d1, d2) -> (d0, d1)>], -// CHECK-SAME: {{.*}}["parallel", "parallel", "reduction"] -// clang-format on -TEST_FUNC(memref_vector_matmul_test) { - using namespace edsc; - using namespace edsc::ops; - - int64_t M = 4, N = 8, K = 16; - auto f32Type = FloatType::getF32(&globalContext()); - auto mkVectorType = VectorType::get({M, K}, f32Type); - auto knVectorType = VectorType::get({K, N}, f32Type); - auto mnVectorType = VectorType::get({M, N}, f32Type); - auto typeA = - MemRefType::get({ShapedType::kDynamicSize, ShapedType::kDynamicSize}, - mkVectorType, {}, 0); - auto typeB = - MemRefType::get({ShapedType::kDynamicSize, ShapedType::kDynamicSize}, - knVectorType, {}, 0); - auto typeC = - MemRefType::get({ShapedType::kDynamicSize, ShapedType::kDynamicSize}, - mnVectorType, {}, 0); - auto f = makeFunction("memref_vector_matmul_test", {}, {typeA, typeB, typeC}); - - OpBuilder builder(f.getBody()); - ScopedContext scope(builder, f.getLoc()); - Value A(f.getArgument(0)), B(f.getArgument(1)), C(f.getArgument(2)); - auto contractionBuilder = [](ValueRange args) { - assert(args.size() == 3 && "expected 3 block arguments"); - (linalg_yield(vector_contraction_matmul(args[0], args[1], args[2]))); - }; - linalg_generic_matmul(A, B, C, contractionBuilder); - - f.print(llvm::outs(), OpPrintingFlags().useLocalScope()); - f.erase(); -} - -TEST_FUNC(builder_loop_for_yield) { - auto indexType = IndexType::get(&globalContext()); - auto f32Type = FloatType::getF32(&globalContext()); - auto f = makeFunction("builder_loop_for_yield", {}, - {indexType, indexType, indexType, indexType}); - - OpBuilder builder(f.getBody()); - ScopedContext scope(builder, f.getLoc()); - Value init0 = std_constant_float(llvm::APFloat(1.0f), f32Type); - Value init1 = std_constant_float(llvm::APFloat(2.0f), f32Type); - Value a(f.getArgument(0)), b(f.getArgument(1)), c(f.getArgument(2)), - d(f.getArgument(3)); - using namespace edsc::op; - auto results = loopNestBuilder(a - b, c + d, a, {init0, init1}, - [&](Value iv, ValueRange args) { - Value sum = args[0] + args[1]; - return scf::ValueVector{args[1], sum}; - }) - .getResults(); - results[0] + results[1]; - - // clang-format off - // CHECK-LABEL: func @builder_loop_for_yield(%{{.*}}: index, %{{.*}}: index, %{{.*}}: index, %{{.*}}: index) { - // CHECK: [[init0:%.*]] = constant - // CHECK: [[init1:%.*]] = constant - // CHECK-DAG: [[r0:%[0-9]+]] = affine.apply affine_map<()[s0, s1] -> (s0 - s1)>()[%{{.*}}, %{{.*}}] - // CHECK-DAG: [[r1:%[0-9]+]] = affine.apply affine_map<()[s0, s1] -> (s0 + s1)>()[%{{.*}}, %{{.*}}] - // CHECK-NEXT: [[res:%[0-9]+]]:2 = scf.for %{{.*}} = [[r0]] to [[r1]] step {{.*}} iter_args([[arg0:%.*]] = [[init0]], [[arg1:%.*]] = [[init1]]) -> (f32, f32) { - // CHECK: [[sum:%[0-9]+]] = addf [[arg0]], [[arg1]] : f32 - // CHECK: scf.yield [[arg1]], [[sum]] : f32, f32 - // CHECK: addf [[res]]#0, [[res]]#1 : f32 - // clang-format on - - f.print(llvm::outs(), OpPrintingFlags().useLocalScope()); - f.erase(); -} - -int main() { - RUN_TESTS(); - return 0; -} diff --git a/mlir/test/EDSC/lit.local.cfg b/mlir/test/EDSC/lit.local.cfg deleted file mode 100644 index 81261555b424..000000000000 --- a/mlir/test/EDSC/lit.local.cfg +++ /dev/null @@ -1 +0,0 @@ -config.suffixes.add('.cpp')