1 //===- TestConvertGPUKernelToCubin.cpp - Test gpu kernel cubin 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/NVVM/NVVMToLLVMIRTranslation.h"
13 #include "mlir/Target/LLVMIR/Export.h"
14 #include "llvm/Support/TargetSelect.h"
15 
16 using namespace mlir;
17 
18 #if MLIR_CUDA_CONVERSIONS_ENABLED
19 namespace {
20 class TestSerializeToCubinPass
21     : public PassWrapper<TestSerializeToCubinPass, gpu::SerializeToBlobPass> {
22 public:
23   StringRef getArgument() const final { return "test-gpu-to-cubin"; }
24   StringRef getDescription() const final {
25     return "Lower GPU kernel function to CUBIN binary annotations";
26   }
27   TestSerializeToCubinPass();
28 
29 private:
30   void getDependentDialects(DialectRegistry &registry) const override;
31 
32   // Serializes PTX to CUBIN.
33   std::unique_ptr<std::vector<char>>
34   serializeISA(const std::string &isa) override;
35 };
36 } // namespace
37 
38 TestSerializeToCubinPass::TestSerializeToCubinPass() {
39   this->triple = "nvptx64-nvidia-cuda";
40   this->chip = "sm_35";
41   this->features = "+ptx60";
42 }
43 
44 void TestSerializeToCubinPass::getDependentDialects(
45     DialectRegistry &registry) const {
46   registerNVVMDialectTranslation(registry);
47   gpu::SerializeToBlobPass::getDependentDialects(registry);
48 }
49 
50 std::unique_ptr<std::vector<char>>
51 TestSerializeToCubinPass::serializeISA(const std::string &) {
52   std::string data = "CUBIN";
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 CUBIN binary annotation.
59 void registerTestGpuSerializeToCubinPass() {
60   PassRegistration<TestSerializeToCubinPass>([] {
61     // Initialize LLVM NVPTX backend.
62     LLVMInitializeNVPTXTarget();
63     LLVMInitializeNVPTXTargetInfo();
64     LLVMInitializeNVPTXTargetMC();
65     LLVMInitializeNVPTXAsmPrinter();
66 
67     return std::make_unique<TestSerializeToCubinPass>();
68   });
69 }
70 } // namespace test
71 } // namespace mlir
72 #endif // MLIR_CUDA_CONVERSIONS_ENABLED
73