[asan] Add a new AddressDescription structure, which can describe any type of address.
[lldb.git] / parallel-libs / streamexecutor / unittests / CoreTests / KernelSpecTest.cpp
1 //===-- KernelSpecTest.cpp - Tests for KernelSpec -------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 ///
10 /// \file
11 /// This file contains the unit tests for the code in KernelSpec.
12 ///
13 //===----------------------------------------------------------------------===//
14
15 #include "streamexecutor/KernelSpec.h"
16
17 #include "gtest/gtest.h"
18
19 namespace {
20
21 namespace se = ::streamexecutor;
22
23 TEST(CUDAPTXInMemorySpec, NoCode) {
24   se::CUDAPTXInMemorySpec Spec("KernelName", {});
25   EXPECT_EQ("KernelName", Spec.getKernelName());
26   EXPECT_EQ(nullptr, Spec.getCode(1, 0));
27 }
28
29 TEST(CUDAPTXInMemorySpec, SingleComputeCapability) {
30   const char *PTXCodeString = "Dummy PTX code";
31   se::CUDAPTXInMemorySpec Spec("KernelName", {{{1, 0}, PTXCodeString}});
32   EXPECT_EQ("KernelName", Spec.getKernelName());
33   EXPECT_EQ(PTXCodeString, Spec.getCode(1, 0));
34   EXPECT_EQ(nullptr, Spec.getCode(2, 0));
35 }
36
37 TEST(CUDAPTXInMemorySpec, TwoComputeCapabilities) {
38   const char *PTXCodeString10 = "Dummy PTX code 10";
39   const char *PTXCodeString30 = "Dummy PTX code 30";
40   se::CUDAPTXInMemorySpec Spec(
41       "KernelName", {{{1, 0}, PTXCodeString10}, {{3, 0}, PTXCodeString30}});
42   EXPECT_EQ("KernelName", Spec.getKernelName());
43   EXPECT_EQ(PTXCodeString10, Spec.getCode(1, 0));
44   EXPECT_EQ(PTXCodeString30, Spec.getCode(3, 0));
45   EXPECT_EQ(nullptr, Spec.getCode(2, 0));
46 }
47
48 TEST(CUDAFatbinInMemorySpec, BasicUsage) {
49   const char *FatbinBytes = "Dummy fatbin bytes";
50   se::CUDAFatbinInMemorySpec Spec("KernelName", FatbinBytes);
51   EXPECT_EQ("KernelName", Spec.getKernelName());
52   EXPECT_EQ(FatbinBytes, Spec.getBytes());
53 }
54
55 TEST(OpenCLTextInMemorySpec, BasicUsage) {
56   const char *OpenCLText = "Dummy OpenCL text";
57   se::OpenCLTextInMemorySpec Spec("KernelName", OpenCLText);
58   EXPECT_EQ("KernelName", Spec.getKernelName());
59   EXPECT_EQ(OpenCLText, Spec.getText());
60 }
61
62 TEST(MultiKernelLoaderSpec, NoCode) {
63   se::MultiKernelLoaderSpec MultiSpec;
64   EXPECT_FALSE(MultiSpec.hasCUDAPTXInMemory());
65   EXPECT_FALSE(MultiSpec.hasCUDAFatbinInMemory());
66   EXPECT_FALSE(MultiSpec.hasOpenCLTextInMemory());
67
68   EXPECT_DEBUG_DEATH(MultiSpec.getCUDAPTXInMemory(),
69                      "getting spec that is not present");
70   EXPECT_DEBUG_DEATH(MultiSpec.getCUDAFatbinInMemory(),
71                      "getting spec that is not present");
72   EXPECT_DEBUG_DEATH(MultiSpec.getOpenCLTextInMemory(),
73                      "getting spec that is not present");
74 }
75
76 TEST(MultiKernelLoaderSpec, Registration) {
77   se::MultiKernelLoaderSpec MultiSpec;
78   const char *KernelName = "KernelName";
79   const char *PTXCodeString = "Dummy PTX code";
80   const char *FatbinBytes = "Dummy fatbin bytes";
81   const char *OpenCLText = "Dummy OpenCL text";
82
83   MultiSpec.addCUDAPTXInMemory(KernelName, {{{1, 0}, PTXCodeString}})
84       .addCUDAFatbinInMemory(KernelName, FatbinBytes)
85       .addOpenCLTextInMemory(KernelName, OpenCLText);
86
87   EXPECT_TRUE(MultiSpec.hasCUDAPTXInMemory());
88   EXPECT_TRUE(MultiSpec.hasCUDAFatbinInMemory());
89   EXPECT_TRUE(MultiSpec.hasOpenCLTextInMemory());
90
91   EXPECT_EQ(KernelName, MultiSpec.getCUDAPTXInMemory().getKernelName());
92   EXPECT_EQ(PTXCodeString, MultiSpec.getCUDAPTXInMemory().getCode(1, 0));
93   EXPECT_EQ(nullptr, MultiSpec.getCUDAPTXInMemory().getCode(2, 0));
94
95   EXPECT_EQ(KernelName, MultiSpec.getCUDAFatbinInMemory().getKernelName());
96   EXPECT_EQ(FatbinBytes, MultiSpec.getCUDAFatbinInMemory().getBytes());
97
98   EXPECT_EQ(KernelName, MultiSpec.getOpenCLTextInMemory().getKernelName());
99   EXPECT_EQ(OpenCLText, MultiSpec.getOpenCLTextInMemory().getText());
100 }
101
102 TEST(MultiKernelLoaderSpec, RegisterTwice) {
103   se::MultiKernelLoaderSpec MultiSpec;
104   const char *KernelName = "KernelName";
105   const char *FatbinBytes = "Dummy fatbin bytes";
106
107   MultiSpec.addCUDAFatbinInMemory(KernelName, FatbinBytes);
108
109   EXPECT_DEBUG_DEATH(MultiSpec.addCUDAFatbinInMemory(KernelName, FatbinBytes),
110                      "illegal loader spec overwrite");
111 }
112
113 TEST(MultiKernelLoaderSpec, ConflictingKernelNames) {
114   se::MultiKernelLoaderSpec MultiSpec;
115   const char *KernelNameA = "KernelName";
116   std::string KernelNameB = KernelNameA;
117   const char *PTXCodeString = "Dummy PTX code";
118   const char *FatbinBytes = "Dummy fatbin bytes";
119
120   // Check that names don't conflict if they are equivalent strings in different
121   // locations.
122   MultiSpec.addCUDAPTXInMemory(KernelNameA, {{{1, 0}, PTXCodeString}})
123       .addCUDAFatbinInMemory(KernelNameB, FatbinBytes);
124
125   const char *OtherKernelName = "OtherKernelName";
126   const char *OpenCLText = "Dummy OpenCL text";
127   EXPECT_DEBUG_DEATH(
128       MultiSpec.addOpenCLTextInMemory(OtherKernelName, OpenCLText),
129       "different kernel names in one MultiKernelLoaderSpec");
130 }
131
132 } // namespace