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/GPU/GPUDialect.h"
15 #include "mlir/Dialect/GPU/MemoryPromotion.h"
16 #include "mlir/Dialect/SCF/SCF.h"
17 #include "mlir/Dialect/SPIRV/IR/SPIRVDialect.h"
18 #include "mlir/Dialect/StandardOps/IR/Ops.h"
19 #include "mlir/IR/Attributes.h"
20 #include "mlir/Pass/Pass.h"
21 
22 using namespace mlir;
23 
24 namespace {
25 /// Simple pass for testing the promotion to workgroup memory in GPU functions.
26 /// Promotes all arguments with "gpu.test_promote_workgroup" attribute. This
27 /// does not check whether the promotion is legal (e.g., amount of memory used)
28 /// or beneficial (e.g., makes previously uncoalesced loads coalesced).
29 class TestGpuMemoryPromotionPass
30     : public PassWrapper<TestGpuMemoryPromotionPass,
31                          OperationPass<gpu::GPUFuncOp>> {
getDependentDialects(DialectRegistry & registry) const32   void getDependentDialects(DialectRegistry &registry) const override {
33     registry.insert<StandardOpsDialect, scf::SCFDialect>();
34   }
35 
runOnOperation()36   void runOnOperation() override {
37     gpu::GPUFuncOp op = getOperation();
38     for (unsigned i = 0, e = op.getNumArguments(); i < e; ++i) {
39       if (op.getArgAttrOfType<UnitAttr>(i, "gpu.test_promote_workgroup"))
40         promoteToWorkgroupMemory(op, i);
41     }
42   }
43 };
44 } // end namespace
45 
46 namespace mlir {
registerTestGpuMemoryPromotionPass()47 void registerTestGpuMemoryPromotionPass() {
48   PassRegistration<TestGpuMemoryPromotionPass>(
49       "test-gpu-memory-promotion",
50       "Promotes the annotated arguments of gpu.func to workgroup memory.");
51 }
52 } // namespace mlir
53