| 1 | //===- Utils.cpp - Utilities to support the ArmSME dialect ----------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements utilities for the ArmSME dialect. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "mlir/Dialect/ArmSME/Utils/Utils.h" |
| 14 | |
| 15 | namespace mlir::arm_sme { |
| 16 | |
| 17 | unsigned getSizeInBytes(TypeSize type) { |
| 18 | switch (type) { |
| 19 | case arm_sme::TypeSize::Byte: |
| 20 | return 1; |
| 21 | case arm_sme::TypeSize::Half: |
| 22 | return 2; |
| 23 | case arm_sme::TypeSize::Word: |
| 24 | return 4; |
| 25 | case arm_sme::TypeSize::Double: |
| 26 | return 8; |
| 27 | } |
| 28 | llvm_unreachable("unknown type size" ); |
| 29 | return 0; |
| 30 | } |
| 31 | |
| 32 | unsigned getSMETileSliceMinNumElts(Type type) { |
| 33 | assert(isValidSMETileElementType(type) && "invalid tile type!" ); |
| 34 | return MinStreamingVectorLengthInBits / type.getIntOrFloatBitWidth(); |
| 35 | } |
| 36 | |
| 37 | bool isValidSMETileElementType(Type type) { |
| 38 | return type.isInteger(width: 8) || type.isInteger(width: 16) || type.isInteger(width: 32) || |
| 39 | type.isInteger(width: 64) || type.isInteger(width: 128) || type.isF16() || |
| 40 | type.isBF16() || type.isF32() || type.isF64() || type.isF128(); |
| 41 | } |
| 42 | |
| 43 | bool isValidSMETileVectorType(VectorType vType) { |
| 44 | if ((vType.getRank() != 2) || !vType.allDimsScalable()) |
| 45 | return false; |
| 46 | |
| 47 | auto elemType = vType.getElementType(); |
| 48 | if (!isValidSMETileElementType(type: elemType)) |
| 49 | return false; |
| 50 | |
| 51 | unsigned minNumElts = getSMETileSliceMinNumElts(type: elemType); |
| 52 | return vType.getShape() == ArrayRef<int64_t>({minNumElts, minNumElts}); |
| 53 | } |
| 54 | |
| 55 | std::optional<ArmSMETileType> getSMETileType(VectorType type) { |
| 56 | if (!isValidSMETileVectorType(vType: type)) |
| 57 | return {}; |
| 58 | switch (type.getElementTypeBitWidth()) { |
| 59 | case 8: |
| 60 | return ArmSMETileType::ZAB; |
| 61 | case 16: |
| 62 | return ArmSMETileType::ZAH; |
| 63 | case 32: |
| 64 | return ArmSMETileType::ZAS; |
| 65 | case 64: |
| 66 | return ArmSMETileType::ZAD; |
| 67 | case 128: |
| 68 | return ArmSMETileType::ZAQ; |
| 69 | default: |
| 70 | llvm_unreachable("unknown SME tile type" ); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | LogicalResult verifyOperationHasValidTileId(Operation *op) { |
| 75 | auto tileOp = llvm::dyn_cast<ArmSMETileOpInterface>(Val: op); |
| 76 | if (!tileOp) |
| 77 | return success(); // Not a tile op (no need to check). |
| 78 | auto tileId = tileOp.getTileId(); |
| 79 | if (!tileId) |
| 80 | return success(); // Not having a tile ID (yet) is okay. |
| 81 | if (!tileId.getType().isSignlessInteger(width: 32)) |
| 82 | return tileOp.emitOpError(message: "tile ID should be a 32-bit signless integer" ); |
| 83 | return success(); |
| 84 | } |
| 85 | |
| 86 | scf::ForOp createLoopOverTileSlices( |
| 87 | PatternRewriter &rewriter, Location loc, Value initTile, |
| 88 | std::function<Value(OpBuilder &, Location, Value, Value)> makeLoopBody) { |
| 89 | OpBuilder::InsertionGuard g(rewriter); |
| 90 | auto step = arith::ConstantIndexOp::create(builder&: rewriter, location: loc, value: 1); |
| 91 | auto minTileSlices = arith::ConstantIndexOp::create( |
| 92 | builder&: rewriter, location: loc, value: llvm::cast<VectorType>(Val: initTile.getType()).getDimSize(idx: 0)); |
| 93 | auto vscale = |
| 94 | vector::VectorScaleOp::create(builder&: rewriter, location: loc, result: rewriter.getIndexType()); |
| 95 | auto lowerBound = arith::ConstantIndexOp::create(builder&: rewriter, location: loc, value: 0); |
| 96 | auto numTileSlices = |
| 97 | arith::MulIOp::create(builder&: rewriter, location: loc, lhs: minTileSlices, rhs: vscale); |
| 98 | auto forOp = scf::ForOp::create(builder&: rewriter, location: loc, lowerBound, upperBound: numTileSlices, |
| 99 | step, initArgs: ValueRange{initTile}); |
| 100 | rewriter.setInsertionPointToStart(forOp.getBody()); |
| 101 | Value nextTile = |
| 102 | makeLoopBody(rewriter, loc, /*tileSliceIndex=*/forOp.getInductionVar(), |
| 103 | /*currentTile=*/forOp.getRegionIterArg(index: 0)); |
| 104 | scf::YieldOp::create(builder&: rewriter, location: loc, results: nextTile); |
| 105 | return forOp; |
| 106 | } |
| 107 | |
| 108 | bool isMultipleOfSMETileVectorType(VectorType vType) { |
| 109 | if (vType.getRank() != 2 || !vType.allDimsScalable()) |
| 110 | return false; |
| 111 | |
| 112 | auto elementType = vType.getElementType(); |
| 113 | if (!isValidSMETileElementType(type: elementType)) |
| 114 | return false; |
| 115 | |
| 116 | unsigned minNumElts = getSMETileSliceMinNumElts(type: elementType); |
| 117 | |
| 118 | int64_t vectorRows = vType.getDimSize(idx: 0); |
| 119 | int64_t vectorCols = vType.getDimSize(idx: 1); |
| 120 | |
| 121 | return (vectorRows > minNumElts || vectorCols > minNumElts) && |
| 122 | vectorRows % minNumElts == 0 && vectorCols % minNumElts == 0; |
| 123 | } |
| 124 | |
| 125 | VectorType getSMETileTypeForElement(Type elementType) { |
| 126 | unsigned minNumElts = getSMETileSliceMinNumElts(type: elementType); |
| 127 | return VectorType::get(shape: {minNumElts, minNumElts}, elementType, scalableDims: {true, true}); |
| 128 | } |
| 129 | |
| 130 | void eraseTriviallyDeadTileOps(IRRewriter &rewriter, |
| 131 | FunctionOpInterface function) { |
| 132 | SmallVector<Operation *> worklist; |
| 133 | function->walk(callback: [&](Operation *op) { |
| 134 | auto armSMEOp = dyn_cast<arm_sme::ArmSMETileOpInterface>(Val: op); |
| 135 | if (armSMEOp && isOpTriviallyDead(op: armSMEOp)) |
| 136 | worklist.push_back(Elt: armSMEOp); |
| 137 | }); |
| 138 | while (!worklist.empty()) { |
| 139 | Operation *op = worklist.pop_back_val(); |
| 140 | if (!isOpTriviallyDead(op)) |
| 141 | continue; |
| 142 | for (Value value : op->getOperands()) { |
| 143 | if (auto armSMEOp = value.getDefiningOp<arm_sme::ArmSMETileOpInterface>()) |
| 144 | worklist.push_back(Elt: armSMEOp); |
| 145 | } |
| 146 | rewriter.eraseOp(op); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | bool isTriviallyCloneableTileOp(arm_sme::ArmSMETileOpInterface tileOp) { |
| 151 | return tileOp && tileOp->getNumResults() == 1 && |
| 152 | tileOp->getNumOperands() == 0 && isPure(op: tileOp); |
| 153 | } |
| 154 | |
| 155 | bool hasTileResult(arm_sme::ArmSMETileOpInterface tileOp) { |
| 156 | for (Value result : tileOp->getResults()) { |
| 157 | if (arm_sme::isValidSMETileVectorType(type: result.getType())) |
| 158 | return true; |
| 159 | } |
| 160 | return false; |
| 161 | } |
| 162 | |
| 163 | OpOperand *getTileOpOperand(arm_sme::ArmSMETileOpInterface tileOp) { |
| 164 | if (!tileOp) |
| 165 | return nullptr; |
| 166 | auto isTileOperandType = [](OpOperand &operand) { |
| 167 | return arm_sme::isValidSMETileVectorType(type: operand.get().getType()); |
| 168 | }; |
| 169 | assert(llvm::count_if(tileOp->getOpOperands(), isTileOperandType) <= 1 && |
| 170 | "expected at most one tile operand" ); |
| 171 | OpOperand *tileOperand = |
| 172 | llvm::find_if(Range: tileOp->getOpOperands(), P: isTileOperandType); |
| 173 | if (tileOperand == tileOp->getOpOperands().end()) |
| 174 | return nullptr; |
| 175 | return tileOperand; |
| 176 | } |
| 177 | |
| 178 | bool isTileTypeGreaterOrEqual(ArmSMETileType typeA, ArmSMETileType typeB) { |
| 179 | // Note: This is <= due to how tile types are numbered in ArmSMEOps.td. |
| 180 | return static_cast<unsigned>(typeA) <= static_cast<unsigned>(typeB); |
| 181 | } |
| 182 | |
| 183 | } // namespace mlir::arm_sme |
| 184 | |