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