1 //===- TestGPUMemoryPromotionPass.cpp - Test pass for GPU promotion -------===//
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 the pass testing the utilities for moving data across
10 // different levels of the GPU memory hierarchy.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "mlir/Dialect/Affine/IR/AffineOps.h"
15 #include "mlir/Dialect/GPU/IR/GPUDialect.h"
16 #include "mlir/Dialect/GPU/Transforms/MemoryPromotion.h"
17 #include "mlir/Dialect/MemRef/IR/MemRef.h"
18 #include "mlir/Dialect/SCF/IR/SCF.h"
19 #include "mlir/Dialect/SPIRV/IR/SPIRVDialect.h"
20 #include "mlir/IR/Attributes.h"
21 #include "mlir/Pass/Pass.h"
22 
23 using namespace mlir;
24 
25 namespace {
26 /// Simple pass for testing the promotion to workgroup memory in GPU functions.
27 /// Promotes all arguments with "gpu.test_promote_workgroup" attribute. This
28 /// does not check whether the promotion is legal (e.g., amount of memory used)
29 /// or beneficial (e.g., makes previously uncoalesced loads coalesced).
30 struct TestGpuMemoryPromotionPass
31     : public PassWrapper<TestGpuMemoryPromotionPass,
32                          OperationPass<gpu::GPUFuncOp>> {
MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID__anonc04e43140111::TestGpuMemoryPromotionPass33   MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestGpuMemoryPromotionPass)
34 
35   void getDependentDialects(DialectRegistry &registry) const override {
36     registry.insert<AffineDialect, memref::MemRefDialect, scf::SCFDialect>();
37   }
getArgument__anonc04e43140111::TestGpuMemoryPromotionPass38   StringRef getArgument() const final { return "test-gpu-memory-promotion"; }
getDescription__anonc04e43140111::TestGpuMemoryPromotionPass39   StringRef getDescription() const final {
40     return "Promotes the annotated arguments of gpu.func to workgroup memory.";
41   }
42 
runOnOperation__anonc04e43140111::TestGpuMemoryPromotionPass43   void runOnOperation() override {
44     gpu::GPUFuncOp op = getOperation();
45     for (unsigned i = 0, e = op.getNumArguments(); i < e; ++i) {
46       if (op.getArgAttrOfType<UnitAttr>(i, "gpu.test_promote_workgroup"))
47         promoteToWorkgroupMemory(op, i);
48     }
49   }
50 };
51 } // namespace
52 
53 namespace mlir {
registerTestGpuMemoryPromotionPass()54 void registerTestGpuMemoryPromotionPass() {
55   PassRegistration<TestGpuMemoryPromotionPass>();
56 }
57 } // namespace mlir
58