1 //===- TestConvertGPUKernelToHsaco.cpp - Test gpu kernel hsaco lowering ---===// 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 #include "mlir/Dialect/GPU/Transforms/Passes.h" 10 #include "mlir/Pass/Pass.h" 11 #include "mlir/Target/LLVMIR/Dialect/ROCDL/ROCDLToLLVMIRTranslation.h" 12 #include "mlir/Target/LLVMIR/Export.h" 13 #include "llvm/Support/TargetSelect.h" 14 15 using namespace mlir; 16 17 #if MLIR_ROCM_CONVERSIONS_ENABLED 18 namespace { 19 class TestSerializeToHsacoPass 20 : public PassWrapper<TestSerializeToHsacoPass, gpu::SerializeToBlobPass> { 21 public: MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestSerializeToHsacoPass)22 MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestSerializeToHsacoPass) 23 24 StringRef getArgument() const final { return "test-gpu-to-hsaco"; } getDescription() const25 StringRef getDescription() const final { 26 return "Lower GPU kernel function to HSAco binary annotations"; 27 } 28 TestSerializeToHsacoPass(); 29 30 private: 31 void getDependentDialects(DialectRegistry ®istry) const override; 32 33 // Serializes ROCDL IR to HSACO. 34 std::unique_ptr<std::vector<char>> 35 serializeISA(const std::string &isa) override; 36 }; 37 } // namespace 38 TestSerializeToHsacoPass()39TestSerializeToHsacoPass::TestSerializeToHsacoPass() { 40 this->triple = "amdgcn-amd-amdhsa"; 41 this->chip = "gfx900"; 42 } 43 getDependentDialects(DialectRegistry & registry) const44void TestSerializeToHsacoPass::getDependentDialects( 45 DialectRegistry ®istry) const { 46 registerROCDLDialectTranslation(registry); 47 gpu::SerializeToBlobPass::getDependentDialects(registry); 48 } 49 50 std::unique_ptr<std::vector<char>> serializeISA(const std::string &)51TestSerializeToHsacoPass::serializeISA(const std::string &) { 52 std::string data = "HSACO"; 53 return std::make_unique<std::vector<char>>(data.begin(), data.end()); 54 } 55 56 namespace mlir { 57 namespace test { 58 // Register test pass to serialize GPU module to a HSAco binary annotation. registerTestGpuSerializeToHsacoPass()59void registerTestGpuSerializeToHsacoPass() { 60 PassRegistration<TestSerializeToHsacoPass>([] { 61 // Initialize LLVM AMDGPU backend. 62 LLVMInitializeAMDGPUTarget(); 63 LLVMInitializeAMDGPUTargetInfo(); 64 LLVMInitializeAMDGPUTargetMC(); 65 LLVMInitializeAMDGPUAsmPrinter(); 66 67 return std::make_unique<TestSerializeToHsacoPass>(); 68 }); 69 } 70 } // namespace test 71 } // namespace mlir 72 #endif // MLIR_ROCM_CONVERSIONS_ENABLED 73