-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathinterface-libnvrtc.cc
More file actions
169 lines (133 loc) · 4.21 KB
/
Copy pathinterface-libnvrtc.cc
File metadata and controls
169 lines (133 loc) · 4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/* Interface to the NVIDIA/CUDA NVRTC library.
Copyright (C) 2026 BayLibre
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 3, or (at
your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with nvptx-tools; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>. */
#include "interface-libnvrtc.h"
#include <stdlib.h>
#ifndef NVPTX_AS_INCLUDE_SYSTEM_NVRTC_H
# include "cuda/nvrtc.h"
#else
# include <nvrtc.h>
#endif
#define DO_PRAGMA(x) _Pragma (#x)
#ifndef NVPTX_AS_LINK_LIBNVRTC
static struct libnvrtc_s {
# define LIBNVRTC_SYMBOL(name) \
__typeof (::name) *name;
# define LIBNVRTC_SYMBOL_MAYBE_NULL(name) \
LIBNVRTC_SYMBOL (name)
# include "interface-libnvrtc-symbols.def"
# undef LIBNVRTC_SYMBOL
# undef LIBNVRTC_SYMBOL_MAYBE_NULL
} libnvrtc_s;
# define LIBNVRTC_SYMBOL_PREFIX libnvrtc_s.
# ifdef HAVE_DLFCN_H
# include <dlfcn.h>
/* -1 if libnvrtc_init has not been called yet, false
if it has been and failed, true if it has been and succeeded. */
static signed char libnvrtc_inited = -1;
/* Dynamically load the NVIDIA/CUDA NVRTC library and initialize function
pointers, return false if unsuccessful, true if successful. */
static bool
libnvrtc_init (std::ostream &error_stream)
{
if (libnvrtc_inited != -1)
return libnvrtc_inited;
const char *libnvrtc = "libnvrtc.so";
void *h = dlopen (libnvrtc, RTLD_LAZY);
libnvrtc_inited = false;
if (h == NULL)
{
error_stream << "couldn't dlopen " << libnvrtc;
return false;
}
# define LIBNVRTC_SYMBOL(name) LIBNVRTC_SYMBOL_1 (name, false)
# define LIBNVRTC_SYMBOL_MAYBE_NULL(name) LIBNVRTC_SYMBOL_1 (name, true)
# define LIBNVRTC_SYMBOL_1(name, allow_null) \
libnvrtc_s.name = (__typeof (name) *) dlsym (h, #name); \
if (!allow_null && libnvrtc_s.name == NULL) \
{ \
error_stream << "couldn't find " << #name << " in " << libnvrtc; \
return false; \
}
# include "interface-libnvrtc-symbols.def"
# undef LIBNVRTC_SYMBOL
# undef LIBNVRTC_SYMBOL_1
# undef LIBNVRTC_SYMBOL_MAYBE_NULL
libnvrtc_inited = true;
return true;
}
# else /* !HAVE_DLFCN_H */
static bool
libnvrtc_init (std::ostream &error_stream)
{
error_stream << "Don't know how to load dynamic shared objects (NVRTC library)";
return false;
}
# endif /* HAVE_DLFCN_H */
#else /* NVPTX_AS_LINK_LIBNVRTC */
# define LIBNVRTC_SYMBOL_PREFIX
# define LIBNVRTC_SYMBOL(name)
# define LIBNVRTC_SYMBOL_MAYBE_NULL(name) DO_PRAGMA (weak name)
# include "interface-libnvrtc-symbols.def"
# undef LIBNVRTC_SYMBOL_MAYBE_NULL
# undef LIBNVRTC_SYMBOL
static bool
libnvrtc_init (std::ostream &error_stream)
{
(void) error_stream;
return true;
}
#endif /* NVPTX_AS_LINK_LIBNVRTC */
#define LIBNVRTC_SYMBOL_CALL_NOCHECK(FN, ...) \
LIBNVRTC_SYMBOL_PREFIX FN (__VA_ARGS__)
#define LIBNVRTC_SYMBOL_EXISTS(FN) \
LIBNVRTC_SYMBOL_PREFIX FN
bool
interface_libnvrtc_init (std::ostream &error_stream)
{
if (!libnvrtc_init (error_stream))
return false;
return true;
}
bool
interface_libnvrtc_version (std::ostream &error_stream, int *major, int *minor)
{
nvrtcResult r;
r = LIBNVRTC_SYMBOL_CALL_NOCHECK (nvrtcVersion, major, minor);
if (r != NVRTC_SUCCESS)
{
error_stream << "nvrtcVersion failed: " << (int) r;
return false;
}
return true;
}
bool
interface_libnvrtc_supported_archs (std::ostream &error_stream, int *n, int **archs)
{
nvrtcResult r;
r = LIBNVRTC_SYMBOL_CALL_NOCHECK (nvrtcGetNumSupportedArchs, n);
if (r != NVRTC_SUCCESS)
{
error_stream << "nvrtcGetNumSupportedArchs failed: " << (int) r;
return false;
}
*archs = new int[*n];
r = LIBNVRTC_SYMBOL_CALL_NOCHECK (nvrtcGetSupportedArchs, *archs);
if (r != NVRTC_SUCCESS)
{
error_stream << "nvrtcGetSupportedArchs failed: " << (int) r;
delete[] *archs;
return false;
}
return true;
}