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