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 struct TestSpirvEntryPointABIPass 24 : public PassWrapper<TestSpirvEntryPointABIPass, 25 OperationPass<gpu::GPUModuleOp>> { 26 MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestSpirvEntryPointABIPass) 27 28 StringRef getArgument() const final { return "test-spirv-entry-point-abi"; } 29 StringRef getDescription() const final { 30 return "Set the spv.entry_point_abi attribute on GPU kernel function " 31 "within the " 32 "module, intended for testing only"; 33 } 34 TestSpirvEntryPointABIPass() = default; 35 TestSpirvEntryPointABIPass(const TestSpirvEntryPointABIPass &) {} 36 void runOnOperation() override; 37 38 private: 39 Pass::ListOption<int32_t> workgroupSize{ 40 *this, "workgroup-size", 41 llvm::cl::desc( 42 "Workgroup size to use for all gpu.func kernels in the module, " 43 "specified with x-dimension first, y-dimension next and z-dimension " 44 "last. Unspecified dimensions will be set to 1"), 45 llvm::cl::ZeroOrMore}; 46 }; 47 } // namespace 48 49 void TestSpirvEntryPointABIPass::runOnOperation() { 50 gpu::GPUModuleOp gpuModule = getOperation(); 51 MLIRContext *context = &getContext(); 52 StringRef attrName = spirv::getEntryPointABIAttrName(); 53 for (gpu::GPUFuncOp gpuFunc : gpuModule.getOps<gpu::GPUFuncOp>()) { 54 if (!gpu::GPUDialect::isKernel(gpuFunc) || gpuFunc->getAttr(attrName)) 55 continue; 56 SmallVector<int32_t, 3> workgroupSizeVec(workgroupSize.begin(), 57 workgroupSize.end()); 58 workgroupSizeVec.resize(3, 1); 59 gpuFunc->setAttr(attrName, 60 spirv::getEntryPointABIAttr(workgroupSizeVec, context)); 61 } 62 } 63 64 namespace mlir { 65 void registerTestSpirvEntryPointABIPass() { 66 PassRegistration<TestSpirvEntryPointABIPass>(); 67 } 68 } // namespace mlir 69