1 //===- TestAvailability.cpp - Test pass for setting Entry point ABI info --===//
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 a pass that sets the spv.entry_point_abi attribute on
10 // functions that are to be lowered as entry point functions.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "mlir/Dialect/GPU/GPUDialect.h"
15 #include "mlir/Dialect/SPIRV/IR/SPIRVDialect.h"
16 #include "mlir/Dialect/SPIRV/IR/TargetAndABI.h"
17 #include "mlir/Pass/Pass.h"
18 
19 using namespace mlir;
20 
21 namespace {
22 /// Pass to set the spv.entry_point_abi
23 class TestSpirvEntryPointABIPass
24     : public PassWrapper<TestSpirvEntryPointABIPass,
25                          OperationPass<gpu::GPUModuleOp>> {
26 public:
27   StringRef getArgument() const final { return "test-spirv-entry-point-abi"; }
28   StringRef getDescription() const final {
29     return "Set the spv.entry_point_abi attribute on GPU kernel function "
30            "within the "
31            "module, intended for testing only";
32   }
33   TestSpirvEntryPointABIPass() = default;
34   TestSpirvEntryPointABIPass(const TestSpirvEntryPointABIPass &) {}
35   void runOnOperation() override;
36 
37 private:
38   Pass::ListOption<int32_t> workgroupSize{
39       *this, "workgroup-size",
40       llvm::cl::desc(
41           "Workgroup size to use for all gpu.func kernels in the module, "
42           "specified with x-dimension first, y-dimension next and z-dimension "
43           "last. Unspecified dimensions will be set to 1"),
44       llvm::cl::ZeroOrMore, llvm::cl::MiscFlags::CommaSeparated};
45 };
46 } // namespace
47 
48 void TestSpirvEntryPointABIPass::runOnOperation() {
49   gpu::GPUModuleOp gpuModule = getOperation();
50   MLIRContext *context = &getContext();
51   StringRef attrName = spirv::getEntryPointABIAttrName();
52   for (gpu::GPUFuncOp gpuFunc : gpuModule.getOps<gpu::GPUFuncOp>()) {
53     if (!gpu::GPUDialect::isKernel(gpuFunc) || gpuFunc->getAttr(attrName))
54       continue;
55     SmallVector<int32_t, 3> workgroupSizeVec(workgroupSize.begin(),
56                                              workgroupSize.end());
57     workgroupSizeVec.resize(3, 1);
58     gpuFunc->setAttr(attrName,
59                      spirv::getEntryPointABIAttr(workgroupSizeVec, context));
60   }
61 }
62 
63 namespace mlir {
64 void registerTestSpirvEntryPointABIPass() {
65   PassRegistration<TestSpirvEntryPointABIPass>();
66 }
67 } // namespace mlir
68