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   MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestSerializeToCubinPass)
24 
25   StringRef getArgument() const final { return "test-gpu-to-cubin"; }
26   StringRef getDescription() const final {
27     return "Lower GPU kernel function to CUBIN binary annotations";
28   }
29   TestSerializeToCubinPass();
30 
31 private:
32   void getDependentDialects(DialectRegistry &registry) const override;
33 
34   // Serializes PTX to CUBIN.
35   std::unique_ptr<std::vector<char>>
36   serializeISA(const std::string &isa) override;
37 };
38 } // namespace
39 
40 TestSerializeToCubinPass::TestSerializeToCubinPass() {
41   this->triple = "nvptx64-nvidia-cuda";
42   this->chip = "sm_35";
43   this->features = "+ptx60";
44 }
45 
46 void TestSerializeToCubinPass::getDependentDialects(
47     DialectRegistry &registry) const {
48   registerNVVMDialectTranslation(registry);
49   gpu::SerializeToBlobPass::getDependentDialects(registry);
50 }
51 
52 std::unique_ptr<std::vector<char>>
53 TestSerializeToCubinPass::serializeISA(const std::string &) {
54   std::string data = "CUBIN";
55   return std::make_unique<std::vector<char>>(data.begin(), data.end());
56 }
57 
58 namespace mlir {
59 namespace test {
60 // Register test pass to serialize GPU module to a CUBIN binary annotation.
61 void registerTestGpuSerializeToCubinPass() {
62   PassRegistration<TestSerializeToCubinPass>([] {
63     // Initialize LLVM NVPTX backend.
64     LLVMInitializeNVPTXTarget();
65     LLVMInitializeNVPTXTargetInfo();
66     LLVMInitializeNVPTXTargetMC();
67     LLVMInitializeNVPTXAsmPrinter();
68 
69     return std::make_unique<TestSerializeToCubinPass>();
70   });
71 }
72 } // namespace test
73 } // namespace mlir
74 #endif // MLIR_CUDA_CONVERSIONS_ENABLED
75