|
| 1 | +//===-- GpuLoopTiling.cpp - DESC --------------------------------*- C++ -*-===// |
| 2 | +// |
| 3 | +// This file is licensed 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 | +#include "mlir/Conversion/LLVMCommon/ConversionTarget.h" |
| 10 | +#include "mlir/Conversion/LLVMCommon/Pattern.h" |
| 11 | +#include "mlir/Conversion/Passes.h" |
| 12 | +#include "mlir/Dialect/Affine/IR/AffineOps.h" |
| 13 | +#include "mlir/Dialect/Affine/Utils.h" |
| 14 | +#include "mlir/Dialect/Func/IR/FuncOps.h" |
| 15 | +#include "mlir/Dialect/GPU/Transforms/Passes.h" |
| 16 | +#include "mlir/Dialect/Linalg/IR/Linalg.h" |
| 17 | +#include "mlir/Dialect/Linalg/Transforms/Transforms.h" |
| 18 | +#include "mlir/Dialect/SCF/Transforms/Transforms.h" |
| 19 | +#include "mlir/Transforms/RegionUtils.h" |
| 20 | + |
| 21 | +#include "./GpuUtils.h" |
| 22 | +#include "gc/Utils/Log.h" |
| 23 | + |
| 24 | +using namespace mlir; |
| 25 | +// using namespace mlir::gc::gpu; |
| 26 | + |
| 27 | +namespace mlir::gc { |
| 28 | +#define GEN_PASS_DECL_GPULOOPTILING |
| 29 | +#define GEN_PASS_DEF_GPULOOPTILING |
| 30 | +#include "gc/Transforms/Passes.h.inc" |
| 31 | +} // namespace mlir::gc |
| 32 | + |
| 33 | +namespace { |
| 34 | + |
| 35 | +struct GpuLoopTiling final : GpuPass<GpuLoopTiling>, |
| 36 | + gc::impl::GpuLoopTilingBase<GpuLoopTiling> { |
| 37 | + friend GpuPass; |
| 38 | + explicit GpuLoopTiling() : GpuLoopTiling(gc::GpuLoopTilingOptions{}) {} |
| 39 | + explicit GpuLoopTiling(const gc::GpuLoopTilingOptions &opts) |
| 40 | + : GpuPass(), GpuLoopTilingBase(opts) {} |
| 41 | + |
| 42 | + void runOnOperation() override { |
| 43 | + IRRewriter rewriter(&getContext()); |
| 44 | + auto euThreads = static_cast<double>(getEuThreads(rewriter)); |
| 45 | + getOperation().walk<WalkOrder::PreOrder>([&](scf::ParallelOp loop) { |
| 46 | + if (!loop->getParentOfType<scf::ParallelOp>()) { |
| 47 | + tile(loop, euThreads); |
| 48 | + } |
| 49 | + return WalkResult::skip(); |
| 50 | + }); |
| 51 | + if (failed(simplifyRegions(rewriter, getOperation()->getRegions()))) { |
| 52 | + gcLogD("Failed to simplify regions"); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | +private: |
| 57 | + static void tile(scf::ParallelOp loop, double euThreads) { |
| 58 | + SmallVector<int64_t> tileSizes; |
| 59 | + auto steps = loop.getStep(); |
| 60 | + tileSizes.reserve(steps.size()); |
| 61 | + |
| 62 | + for (auto step : steps) { |
| 63 | + if (auto v = getConstIdxValue(step)) { |
| 64 | + tileSizes.push_back(static_cast<int64_t>( |
| 65 | + std::ceil(static_cast<double>(v) / euThreads))); |
| 66 | + } else { |
| 67 | + tileSizes.push_back(32); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + tileParallelLoop(loop, tileSizes, false); |
| 72 | + } |
| 73 | +}; |
| 74 | +} // namespace |
0 commit comments