|
| 1 | +//===- AllocsToSLM.cpp - A pass adding shared mem-space attr ----*- 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 "gc/Transforms/Passes.h" |
| 10 | + |
| 11 | +#include "mlir/Dialect/Func/IR/FuncOps.h" |
| 12 | +#include "mlir/Dialect/GPU/TransformOps/Utils.h" |
| 13 | +#include "mlir/Dialect/MemRef/IR/MemRef.h" |
| 14 | +#include "mlir/IR/Dialect.h" |
| 15 | +#include "mlir/Pass/Pass.h" |
| 16 | +#include "mlir/Pass/PassManager.h" |
| 17 | +#include "mlir/Transforms/GreedyPatternRewriteDriver.h" |
| 18 | + |
| 19 | +#include <numeric> |
| 20 | +#include <optional> |
| 21 | + |
| 22 | +using namespace mlir; |
| 23 | +using namespace mlir::gc; |
| 24 | + |
| 25 | +namespace mlir { |
| 26 | +namespace gc { |
| 27 | +#define GEN_PASS_DEF_ALLOCSTOSLM |
| 28 | +#include "gc/Transforms/Passes.h.inc" |
| 29 | +} // namespace gc |
| 30 | +} // namespace mlir |
| 31 | + |
| 32 | +namespace { |
| 33 | + |
| 34 | +bool isInGpuLaunch(Operation *op) { |
| 35 | + auto launchOp = op->getParentOfType<gpu::LaunchOp>(); |
| 36 | + return launchOp != nullptr; |
| 37 | +} |
| 38 | + |
| 39 | +bool hasAssignedMemSpace(Value value) { |
| 40 | + if (auto memrefType = dyn_cast<MemRefType>(value.getType())) { |
| 41 | + if (memrefType.getMemorySpace()) { |
| 42 | + return true; |
| 43 | + } |
| 44 | + } |
| 45 | + return false; |
| 46 | +} |
| 47 | + |
| 48 | +struct ConvertAlloc : public OpRewritePattern<memref::AllocOp> { |
| 49 | + using OpRewritePattern<memref::AllocOp>::OpRewritePattern; |
| 50 | + |
| 51 | + ConvertAlloc(MLIRContext *ctx) : OpRewritePattern<memref::AllocOp>(ctx) {} |
| 52 | + |
| 53 | + LogicalResult matchAndRewrite(memref::AllocOp allocOp, |
| 54 | + PatternRewriter &rewriter) const override { |
| 55 | + if (hasAssignedMemSpace(allocOp->getResult(0))) { |
| 56 | + return rewriter.notifyMatchFailure( |
| 57 | + allocOp, "Memref already has some memory space attribute"); |
| 58 | + } |
| 59 | + |
| 60 | + if (!isInGpuLaunch(allocOp)) { |
| 61 | + return rewriter.notifyMatchFailure(allocOp, |
| 62 | + "Only support allocs in GPU regions"); |
| 63 | + } |
| 64 | + |
| 65 | + Value memref = allocOp->getResult(0); |
| 66 | + MemRefType originalMemRefType = cast<MemRefType>(memref.getType()); |
| 67 | + |
| 68 | + IntegerAttr sharedAddressSpace = |
| 69 | + IntegerAttr::get(rewriter.getIntegerType(64), |
| 70 | + static_cast<int64_t>(gpu::AddressSpace::Private)); |
| 71 | + |
| 72 | + // Create a new MemRefType with the desired address space |
| 73 | + MemRefType newMemRefType = MemRefType::get( |
| 74 | + originalMemRefType.getShape(), originalMemRefType.getElementType(), |
| 75 | + originalMemRefType.getLayout(), sharedAddressSpace); |
| 76 | + |
| 77 | + Value newMemRef = rewriter.create<memref::AllocOp>( |
| 78 | + allocOp.getLoc(), newMemRefType, allocOp.getOperands()); |
| 79 | + |
| 80 | + memref.replaceAllUsesWith(newMemRef); |
| 81 | + |
| 82 | + return success(); |
| 83 | + } |
| 84 | +}; |
| 85 | + |
| 86 | +struct AllocsToSLM : public gc::impl::AllocsToSLMBase<AllocsToSLM> { |
| 87 | + void runOnOperation() override { |
| 88 | + const auto ctx = &getContext(); |
| 89 | + |
| 90 | + RewritePatternSet patterns(ctx); |
| 91 | + patterns.add<ConvertAlloc>(patterns.getContext()); |
| 92 | + (void)applyPatternsAndFoldGreedily(getOperation(), std::move(patterns)); |
| 93 | + } |
| 94 | +}; |
| 95 | + |
| 96 | +} // namespace |
0 commit comments