|
| 1 | +//===- FindFirstGemmIndex.cpp -----------------===// |
| 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 | +// Fusions can generate multiple linalg.generic ops and firstGemmIdx might not |
| 10 | +// match between block inputs and linalg.generic op after linalg.generic ops |
| 11 | +// have been fused. This pass traces the fused linalg.generic op back to the |
| 12 | +// firstGemmIdx of the block inputs. |
| 13 | +// |
| 14 | +//===----------------------------------------------------------------------===// |
| 15 | + |
| 16 | +#include "mlir/Dialect/Func/IR/FuncOps.h" |
| 17 | +#include "mlir/Dialect/Linalg/IR/Linalg.h" |
| 18 | +#include "mlir/Dialect/Rock/IR/Rock.h" |
| 19 | +#include "mlir/Dialect/Rock/IR/RockGemmGemmWrapperInterface.h" |
| 20 | +#include "mlir/Dialect/Rock/Passes.h" |
| 21 | +#include "mlir/Dialect/Rock/utility/loweringUtils.h" |
| 22 | +#include "mlir/Dialect/Rock/utility/transformMapUtils.h" |
| 23 | +#include "mlir/IR/BuiltinAttributes.h" |
| 24 | +#include "mlir/IR/Value.h" |
| 25 | +#include "mlir/Support/LogicalResult.h" |
| 26 | +#include "llvm/ADT/STLExtras.h" |
| 27 | +#include <cstdint> |
| 28 | + |
| 29 | +namespace mlir { |
| 30 | +namespace rock { |
| 31 | +#define GEN_PASS_DEF_ROCKFINDFIRSTGEMMINDEXPASS |
| 32 | +#include "mlir/Dialect/Rock/Passes.h.inc" |
| 33 | +} // namespace rock |
| 34 | +} // namespace mlir |
| 35 | + |
| 36 | +#define DEBUG_TYPE "rock-find-first-gemm-index" |
| 37 | + |
| 38 | +using namespace mlir; |
| 39 | + |
| 40 | +namespace { |
| 41 | +struct RockFindFirstGemmIndexPass |
| 42 | + : public rock::impl::RockFindFirstGemmIndexPassBase< |
| 43 | + RockFindFirstGemmIndexPass> { |
| 44 | + void runOnOperation() override; |
| 45 | +}; |
| 46 | +} // end anonymous namespace |
| 47 | + |
| 48 | +static bool canTraceToFirstGemmArg(Value input, Value firstGemmArg) { |
| 49 | + // trace back to block arguments (through ViewLike ops) |
| 50 | + FailureOr<Value> maybeBlockArg = rock::findBlockArgument(input); |
| 51 | + if (failed(maybeBlockArg)) |
| 52 | + return false; |
| 53 | + |
| 54 | + // If the input is a block argument, check if it matches the first gemm |
| 55 | + // argument. |
| 56 | + return maybeBlockArg.value() == firstGemmArg; |
| 57 | +} |
| 58 | + |
| 59 | +static LogicalResult |
| 60 | +reassignFirstGemmIndex(func::FuncOp &func, |
| 61 | + rock::RockGemmGemmWrapperInterface gemmGemmOp) { |
| 62 | + // Get the first gemm index from the gemmGemmOp. |
| 63 | + uint32_t firstGemmIndex = gemmGemmOp.getFirstGemmIndex(); |
| 64 | + |
| 65 | + // get linalg.generic ops in the preSecondGemmRegion |
| 66 | + SmallVector<linalg::GenericOp> genOps; |
| 67 | + gemmGemmOp.getPreSecondGemmRegion().walk( |
| 68 | + [&genOps](linalg::GenericOp genOp) { genOps.push_back(genOp); }); |
| 69 | + |
| 70 | + // no fusion, nothing to do |
| 71 | + if (genOps.empty()) |
| 72 | + return success(); |
| 73 | + |
| 74 | + if (genOps.size() != 1) |
| 75 | + return gemmGemmOp.emitError( |
| 76 | + "More than one linalg.generic operation found, expected only one."); |
| 77 | + |
| 78 | + linalg::GenericOp genOp = genOps[0]; |
| 79 | + assert(firstGemmIndex < |
| 80 | + gemmGemmOp.getPreSecondGemmRegion().getNumArguments()); |
| 81 | + Value firstGemmArg = |
| 82 | + gemmGemmOp.getPreSecondGemmRegion().getArgument(firstGemmIndex); |
| 83 | + |
| 84 | + // try to trace args of linalg.generic op back to the first gemm argument |
| 85 | + int64_t newFirstGemmIndex = -1; |
| 86 | + for (auto [index, input] : llvm::enumerate(genOp.getInputs())) { |
| 87 | + if (canTraceToFirstGemmArg(input, firstGemmArg)) { |
| 88 | + // If the input can be traced back to the first gemm argument, we found |
| 89 | + // the new index. |
| 90 | + newFirstGemmIndex = index; |
| 91 | + break; |
| 92 | + } |
| 93 | + } |
| 94 | + if (newFirstGemmIndex == -1) { |
| 95 | + return gemmGemmOp.emitError( |
| 96 | + "Could not find a matching input for the first gemm index."); |
| 97 | + } |
| 98 | + |
| 99 | + // Set the new first gemm index in the gemmGemmOp. |
| 100 | + gemmGemmOp.setFirstGemmIndex(static_cast<uint32_t>(newFirstGemmIndex)); |
| 101 | + return success(); |
| 102 | +} |
| 103 | + |
| 104 | +void RockFindFirstGemmIndexPass::runOnOperation() { |
| 105 | + auto func = getOperation(); |
| 106 | + // Only run this pass on GPU kernel functions. |
| 107 | + if (!func->hasAttr("kernel")) |
| 108 | + return; |
| 109 | + |
| 110 | + // find gemm+gemm like operations with fusion |
| 111 | + SmallVector<rock::RockGemmGemmWrapperInterface> gemmGemmOps; |
| 112 | + func.walk([&gemmGemmOps](rock::RockGemmGemmWrapperInterface gemmGemmOp) { |
| 113 | + gemmGemmOps.push_back(gemmGemmOp); |
| 114 | + }); |
| 115 | + |
| 116 | + // no gemm+gemm like operations found, nothing to do |
| 117 | + if (gemmGemmOps.empty()) |
| 118 | + return; |
| 119 | + |
| 120 | + if (gemmGemmOps.size() != 1) { |
| 121 | + func.emitError( |
| 122 | + "More than one gemm+gemm like operation found, expected only one."); |
| 123 | + return signalPassFailure(); |
| 124 | + } |
| 125 | + |
| 126 | + if (failed(reassignFirstGemmIndex(func, gemmGemmOps[0]))) { |
| 127 | + return signalPassFailure(); |
| 128 | + } |
| 129 | +} |
0 commit comments