|
| 1 | +#include "intel/include/Dialect/Triton/Transforms/Passes.h" |
| 2 | +#include "intel/include/Utils/Utility.h" |
| 3 | +#include "mlir/Dialect/Arith/IR/Arith.h" |
| 4 | +#include "mlir/Dialect/SCF/IR/SCF.h" |
| 5 | +#include "mlir/Dialect/Utils/StaticValueUtils.h" |
| 6 | +#include "mlir/IR/BuiltinTypes.h" |
| 7 | +#include "mlir/IR/Verifier.h" |
| 8 | +#include "mlir/Interfaces/InferIntRangeInterface.h" |
| 9 | +#include "mlir/Support/WalkResult.h" |
| 10 | +#include "triton/Dialect/Triton/IR/Dialect.h" |
| 11 | +#include "llvm/ADT/APInt.h" |
| 12 | +#include "llvm/Support/Debug.h" |
| 13 | +#include "llvm/Support/raw_ostream.h" |
| 14 | +#include <cmath> |
| 15 | +#include <optional> |
| 16 | + |
| 17 | +#define DEBUG_TYPE "triton-intel-remove-boundary-checks" |
| 18 | + |
| 19 | +using namespace mlir; |
| 20 | +namespace tt = mlir::triton; |
| 21 | + |
| 22 | +namespace mlir::triton::intel { |
| 23 | +#define GEN_PASS_DEF_TRITONINTELREMOVEBOUNDARYCHECKS |
| 24 | +#include "intel/include/Dialect/Triton/Transforms/Passes.h.inc" |
| 25 | +} // namespace mlir::triton::intel |
| 26 | + |
| 27 | +namespace { |
| 28 | +class BoundaryChecksRemover { |
| 29 | +public: |
| 30 | + void run(ModuleOp moduleOp) { |
| 31 | + moduleOp.walk([&](tt::LoadOp loadOp) { |
| 32 | + if (!isCandidate(loadOp)) |
| 33 | + return WalkResult::skip(); |
| 34 | + |
| 35 | + tt::MakeTensorPtrOp makeTensorPtrOp = |
| 36 | + *tt::intel::findDefiningMakeTensorPtrOp(loadOp.getPtr()); |
| 37 | + LLVM_DEBUG(llvm::dbgs() |
| 38 | + << "Analyzing boundaryCheck for: " << loadOp << "\n"); |
| 39 | + |
| 40 | + SmallVector<int> newBoundaryCheck; |
| 41 | + for (int boundIdx : loadOp.getBoundaryCheck()) { |
| 42 | + ArrayRef<int> order = makeTensorPtrOp.getOrder(); |
| 43 | + int idx = order.size() - order[boundIdx] - 1; |
| 44 | + Value offset = makeTensorPtrOp.getOffsets()[idx]; |
| 45 | + Value shape = makeTensorPtrOp.getShape()[idx]; |
| 46 | + auto resType = cast<RankedTensorType>(loadOp.getResult().getType()); |
| 47 | + ArrayRef<int64_t> resShape = resType.getShape(); |
| 48 | + std::optional<int64_t> offsetVal = getConstantIntValue(offset), |
| 49 | + shapeVal = getConstantIntValue(shape); |
| 50 | + |
| 51 | + // If the shape is not known at compile time we cannot determine whether |
| 52 | + // the bound check is unnecessary. |
| 53 | + if (!shapeVal) { |
| 54 | + LLVM_DEBUG(llvm::dbgs().indent(2) |
| 55 | + << "Check at index " << boundIdx << " is necessary\n"); |
| 56 | + newBoundaryCheck.push_back(boundIdx); |
| 57 | + continue; |
| 58 | + } |
| 59 | + |
| 60 | + // Case 1: offset and shape are constant. |
| 61 | + if (offsetVal && ((*offsetVal + resShape[idx]) <= *shapeVal)) { |
| 62 | + LLVM_DEBUG(llvm::dbgs().indent(2) |
| 63 | + << "Check at index " << boundIdx << " is unnecessary\n"); |
| 64 | + continue; |
| 65 | + } |
| 66 | + |
| 67 | + // Case 2: analyze boundary check in loops. |
| 68 | + if (auto forOp = makeTensorPtrOp->getParentOfType<scf::ForOp>()) { |
| 69 | + assert(forOp.getSingleInductionVar() && "Single IV expected"); |
| 70 | + Value iv = *forOp.getSingleInductionVar(); |
| 71 | + if (offset != iv) { |
| 72 | + LLVM_DEBUG(llvm::dbgs().indent(2) |
| 73 | + << "Check at index " << boundIdx << " is necessary\n"); |
| 74 | + newBoundaryCheck.push_back(boundIdx); |
| 75 | + continue; |
| 76 | + } |
| 77 | + |
| 78 | + OpFoldResult lb = *forOp.getSingleLowerBound(); |
| 79 | + OpFoldResult ub = *forOp.getSingleUpperBound(); |
| 80 | + OpFoldResult step = *forOp.getSingleStep(); |
| 81 | + |
| 82 | + auto computeLoopIVRange = |
| 83 | + [&](OpFoldResult lb, OpFoldResult ub, |
| 84 | + OpFoldResult step) -> std::optional<ConstantIntRanges> { |
| 85 | + auto getBoundValue = |
| 86 | + [](OpFoldResult bound) -> std::optional<int64_t> { |
| 87 | + if (std::optional<int64_t> opVal = getConstantIntValue(bound)) |
| 88 | + return *opVal; |
| 89 | + |
| 90 | + Value val = tt::intel::getFinalValue(cast<Value>(bound)); |
| 91 | + if (auto cst = dyn_cast<arith::BitcastOp>(val.getDefiningOp())) |
| 92 | + val = cst.getIn(); |
| 93 | + |
| 94 | + return getConstantIntValue(getAsOpFoldResult(val)); |
| 95 | + }; |
| 96 | + |
| 97 | + auto areLoopBoundKnown = [&](OpFoldResult lb, OpFoldResult ub, |
| 98 | + OpFoldResult step) { |
| 99 | + return (getBoundValue(lb) && getBoundValue(ub) && |
| 100 | + getBoundValue(step)); |
| 101 | + }; |
| 102 | + |
| 103 | + if (!areLoopBoundKnown(lb, ub, step)) |
| 104 | + return std::nullopt; |
| 105 | + |
| 106 | + int64_t lbVal = *getBoundValue(lb); |
| 107 | + int64_t ubVal = *getBoundValue(ub); |
| 108 | + int64_t stepVal = *getBoundValue(step); |
| 109 | + int64_t lastIVVal = |
| 110 | + lbVal + ((ubVal - lbVal - 1) / stepVal) * stepVal; |
| 111 | + llvm::APInt start(64, lbVal, true); |
| 112 | + llvm::APInt end(64, lastIVVal, true); |
| 113 | + |
| 114 | + return ConstantIntRanges::range(start, end, true); |
| 115 | + }; |
| 116 | + |
| 117 | + std::optional<ConstantIntRanges> optRange = |
| 118 | + computeLoopIVRange(lb, ub, step); |
| 119 | + if (!optRange) { |
| 120 | + LLVM_DEBUG(llvm::dbgs().indent(2) |
| 121 | + << "Check at index " << boundIdx << " is necessary\n"); |
| 122 | + newBoundaryCheck.push_back(boundIdx); |
| 123 | + continue; |
| 124 | + } |
| 125 | + |
| 126 | + APInt maxIV = (*optRange).smax(); |
| 127 | + if (maxIV.getSExtValue() + resShape[idx] <= shapeVal) { |
| 128 | + LLVM_DEBUG(llvm::dbgs().indent(2) |
| 129 | + << "Check at index " << boundIdx << " is unnecessary\n"); |
| 130 | + continue; |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + LLVM_DEBUG(llvm::dbgs().indent(2) |
| 135 | + << "Check at index " << boundIdx << " is necessary\n"); |
| 136 | + newBoundaryCheck.push_back(boundIdx); |
| 137 | + } |
| 138 | + |
| 139 | + if (newBoundaryCheck.size() != loadOp.getBoundaryCheck().size()) { |
| 140 | + loadOp.setBoundaryCheck(newBoundaryCheck); |
| 141 | + LLVM_DEBUG(llvm::dbgs().indent(2) |
| 142 | + << "Rewritten load is: " << loadOp << "\n"); |
| 143 | + } |
| 144 | + |
| 145 | + return WalkResult::advance(); |
| 146 | + }); |
| 147 | + } |
| 148 | + |
| 149 | +private: |
| 150 | + // A candidate load operation is one that: |
| 151 | + // - has the boundary check attribute |
| 152 | + // - uses a block pointer defined by a `make_tensor_ptr` that is not |
| 153 | + // advanced |
| 154 | + bool isCandidate(tt::LoadOp loadOp) const { |
| 155 | + assert(loadOp && "Expecting a valid load operation"); |
| 156 | + |
| 157 | + ArrayRef<int> boundaryCheck = loadOp.getBoundaryCheck(); |
| 158 | + if (boundaryCheck.empty()) |
| 159 | + return false; |
| 160 | + |
| 161 | + Type ptrType = loadOp.getPtr().getType(); |
| 162 | + if (!tt::isTensorPointerType(ptrType)) |
| 163 | + return false; |
| 164 | + |
| 165 | + std::optional<tt::MakeTensorPtrOp> makeTensorPtrOp = |
| 166 | + tt::intel::findDefiningMakeTensorPtrOp(loadOp.getPtr()); |
| 167 | + if (!makeTensorPtrOp) |
| 168 | + return false; |
| 169 | + |
| 170 | + if (llvm::any_of((*makeTensorPtrOp)->getUsers(), |
| 171 | + [](Operation *user) { return isa<tt::AdvanceOp>(user); })) |
| 172 | + return false; |
| 173 | + |
| 174 | + return true; |
| 175 | + } |
| 176 | +}; |
| 177 | + |
| 178 | +} // namespace |
| 179 | + |
| 180 | +struct TritonIntelRemoveBoundaryChecks |
| 181 | + : tt::intel::impl::TritonIntelRemoveBoundaryChecksBase< |
| 182 | + TritonIntelRemoveBoundaryChecks> { |
| 183 | +public: |
| 184 | + void runOnOperation() final { |
| 185 | + ModuleOp moduleOp = getOperation(); |
| 186 | + BoundaryChecksRemover remover; |
| 187 | + remover.run(moduleOp); |
| 188 | + assert(succeeded(verify(moduleOp)) && "Module verification failed"); |
| 189 | + } |
| 190 | +}; |
0 commit comments