HIP: Heterogenous-computing Interface for Portability
hiprtc.h
1 /*
2 Copyright (c) 2021 - present Advanced Micro Devices, Inc. All rights reserved.
3 
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10 
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
13 
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 THE SOFTWARE.
21 */
22 #ifndef HIPRTC_H
23 #define HIPRTC_H
24 
25 #include <cuda.h>
26 #include <nvrtc.h>
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif /* __cplusplus */
31 
32 #include <stdlib.h>
33 
34 #if !defined(_WIN32)
35 #pragma GCC visibility push(default)
36 #endif
37 
38 typedef enum hiprtcResult {
39  HIPRTC_SUCCESS = 0,
40  HIPRTC_ERROR_OUT_OF_MEMORY = 1,
41  HIPRTC_ERROR_PROGRAM_CREATION_FAILURE = 2,
42  HIPRTC_ERROR_INVALID_INPUT = 3,
43  HIPRTC_ERROR_INVALID_PROGRAM = 4,
44  HIPRTC_ERROR_INVALID_OPTION = 5,
45  HIPRTC_ERROR_COMPILATION = 6,
46  HIPRTC_ERROR_BUILTIN_OPERATION_FAILURE = 7,
47  HIPRTC_ERROR_NO_NAME_EXPRESSIONS_AFTER_COMPILATION = 8,
48  HIPRTC_ERROR_NO_LOWERED_NAMES_BEFORE_COMPILATION = 9,
49  HIPRTC_ERROR_NAME_EXPRESSION_NOT_VALID = 10,
50  HIPRTC_ERROR_INTERNAL_ERROR = 11
51 } hiprtcResult;
52 
53 inline static nvrtcResult hiprtcResultTonvrtcResult(hiprtcResult result) {
54  switch (result) {
55  case HIPRTC_SUCCESS:
56  return NVRTC_SUCCESS;
57  case HIPRTC_ERROR_OUT_OF_MEMORY:
58  return NVRTC_ERROR_OUT_OF_MEMORY;
59  case HIPRTC_ERROR_PROGRAM_CREATION_FAILURE:
60  return NVRTC_ERROR_PROGRAM_CREATION_FAILURE;
61  case HIPRTC_ERROR_INVALID_INPUT:
62  return NVRTC_ERROR_INVALID_INPUT;
63  case HIPRTC_ERROR_INVALID_PROGRAM:
64  return NVRTC_ERROR_INVALID_PROGRAM;
65  case HIPRTC_ERROR_INVALID_OPTION:
66  return NVRTC_ERROR_INVALID_OPTION;
67  case HIPRTC_ERROR_COMPILATION:
68  return NVRTC_ERROR_COMPILATION;
69  case HIPRTC_ERROR_BUILTIN_OPERATION_FAILURE:
70  return NVRTC_ERROR_BUILTIN_OPERATION_FAILURE;
71  case HIPRTC_ERROR_NO_NAME_EXPRESSIONS_AFTER_COMPILATION:
72  return NVRTC_ERROR_NO_NAME_EXPRESSIONS_AFTER_COMPILATION;
73  case HIPRTC_ERROR_NO_LOWERED_NAMES_BEFORE_COMPILATION:
74  return NVRTC_ERROR_NO_LOWERED_NAMES_BEFORE_COMPILATION;
75  case HIPRTC_ERROR_NAME_EXPRESSION_NOT_VALID:
76  return NVRTC_ERROR_NAME_EXPRESSION_NOT_VALID;
77  case HIPRTC_ERROR_INTERNAL_ERROR:
78  return NVRTC_ERROR_INTERNAL_ERROR;
79  }
80 }
81 
82 inline static hiprtcResult nvrtcResultTohiprtcResult(nvrtcResult result) {
83  switch (result) {
84  case NVRTC_SUCCESS:
85  return HIPRTC_SUCCESS;
86  case NVRTC_ERROR_OUT_OF_MEMORY:
87  return HIPRTC_ERROR_OUT_OF_MEMORY;
88  case NVRTC_ERROR_PROGRAM_CREATION_FAILURE:
89  return HIPRTC_ERROR_PROGRAM_CREATION_FAILURE;
90  case NVRTC_ERROR_INVALID_INPUT:
91  return HIPRTC_ERROR_INVALID_INPUT;
92  case NVRTC_ERROR_INVALID_PROGRAM:
93  return HIPRTC_ERROR_INVALID_PROGRAM;
94  case NVRTC_ERROR_INVALID_OPTION:
95  return HIPRTC_ERROR_INVALID_OPTION;
96  case NVRTC_ERROR_COMPILATION:
97  return HIPRTC_ERROR_COMPILATION;
98  case NVRTC_ERROR_BUILTIN_OPERATION_FAILURE:
99  return HIPRTC_ERROR_BUILTIN_OPERATION_FAILURE;
100  case NVRTC_ERROR_NO_NAME_EXPRESSIONS_AFTER_COMPILATION:
101  return HIPRTC_ERROR_NO_NAME_EXPRESSIONS_AFTER_COMPILATION;
102  case NVRTC_ERROR_NO_LOWERED_NAMES_BEFORE_COMPILATION:
103  return HIPRTC_ERROR_NO_LOWERED_NAMES_BEFORE_COMPILATION;
104  case NVRTC_ERROR_NAME_EXPRESSION_NOT_VALID:
105  return HIPRTC_ERROR_NAME_EXPRESSION_NOT_VALID;
106  case NVRTC_ERROR_INTERNAL_ERROR:
107  return HIPRTC_ERROR_INTERNAL_ERROR;
108  }
109 }
110 
111 const char* hiprtcGetErrorString(hiprtcResult result) {
112  return nvrtcGetErrorString(hiprtcResultTonvrtcResult(result));
113 }
114 
115 hiprtcResult hiprtcVersion(int* major, int* minor) {
116  return nvrtcResultTohiprtcResult(nvrtcVersion(major, minor));
117 }
118 
119 typedef nvrtcProgram hiprtcProgram;
120 
121 hiprtcResult hiprtcAddNameExpression(hiprtcProgram prog, const char* name_expression) {
122  return nvrtcResultTohiprtcResult(nvrtcAddNameExpression(prog, name_expression));
123 }
124 
125 hiprtcResult hiprtcCompileProgram(hiprtcProgram prog, int numOptions, const char** options) {
126  return nvrtcResultTohiprtcResult(nvrtcCompileProgram(prog, numOptions, options));
127 }
128 
129 hiprtcResult hiprtcCreateProgram(hiprtcProgram* prog, const char* src, const char* name,
130  int numHeaders, const char** headers, const char** includeNames) {
131  return nvrtcResultTohiprtcResult(
132  nvrtcCreateProgram(prog, src, name, numHeaders, headers, includeNames));
133 }
134 
135 hiprtcResult hiprtcDestroyProgram(hiprtcProgram* prog) {
136  return nvrtcResultTohiprtcResult(nvrtcDestroyProgram(prog));
137 }
138 
139 hiprtcResult hiprtcGetLoweredName(hiprtcProgram prog, const char* name_expression,
140  const char** lowered_name) {
141  return nvrtcResultTohiprtcResult(nvrtcGetLoweredName(prog, name_expression, lowered_name));
142 }
143 
144 hiprtcResult hiprtcGetProgramLog(hiprtcProgram prog, char* log) {
145  return nvrtcResultTohiprtcResult(nvrtcGetProgramLog(prog, log));
146 }
147 
148 hiprtcResult hiprtcGetProgramLogSize(hiprtcProgram prog, size_t* logSizeRet) {
149  return nvrtcResultTohiprtcResult(nvrtcGetProgramLogSize(prog, logSizeRet));
150 }
151 
152 hiprtcResult hiprtcGetCode(hiprtcProgram prog, char* code) {
153  return nvrtcResultTohiprtcResult(nvrtcGetPTX(prog, code));
154 }
155 
156 hiprtcResult hiprtcGetCodeSize(hiprtcProgram prog, size_t* codeSizeRet) {
157  return nvrtcResultTohiprtcResult(nvrtcGetPTXSize(prog, codeSizeRet));
158 }
159 
160 #if !defined(_WIN32)
161 #pragma GCC visibility pop
162 #endif
163 
164 #ifdef __cplusplus
165 }
166 #endif /* __cplusplus */
167 
168 #endif // HIPRTC_H