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   StringRef getArgument() const final { return "test-gpu-to-hsaco"; }
24   StringRef getDescription() const final {
25     return "Lower GPU kernel function to HSAco binary annotations";
26   }
27   TestSerializeToHsacoPass();
28 
29 private:
30   void getDependentDialects(DialectRegistry &registry) const override;
31 
32   // Serializes ROCDL IR to HSACO.
33   std::unique_ptr<std::vector<char>>
34   serializeISA(const std::string &isa) override;
35 };
36 } // namespace
37 
38 TestSerializeToHsacoPass::TestSerializeToHsacoPass() {
39   this->triple = "amdgcn-amd-amdhsa";
40   this->chip = "gfx900";
41 }
42 
43 void TestSerializeToHsacoPass::getDependentDialects(
44     DialectRegistry &registry) const {
45   registerROCDLDialectTranslation(registry);
46   gpu::SerializeToBlobPass::getDependentDialects(registry);
47 }
48 
49 std::unique_ptr<std::vector<char>>
50 TestSerializeToHsacoPass::serializeISA(const std::string &) {
51   std::string data = "HSACO";
52   return std::make_unique<std::vector<char>>(data.begin(), data.end());
53 }
54 
55 namespace mlir {
56 namespace test {
57 // Register test pass to serialize GPU module to a HSAco binary annotation.
58 void registerTestGpuSerializeToHsacoPass() {
59   PassRegistration<TestSerializeToHsacoPass>([] {
60     // Initialize LLVM AMDGPU backend.
61     LLVMInitializeAMDGPUTarget();
62     LLVMInitializeAMDGPUTargetInfo();
63     LLVMInitializeAMDGPUTargetMC();
64     LLVMInitializeAMDGPUAsmPrinter();
65 
66     return std::make_unique<TestSerializeToHsacoPass>();
67   });
68 }
69 } // namespace test
70 } // namespace mlir
71 #endif // MLIR_ROCM_CONVERSIONS_ENABLED
72