1 /*========================== begin_copyright_notice ============================
2 
3 Copyright (C) 2017-2021 Intel Corporation
4 
5 SPDX-License-Identifier: MIT
6 
7 ============================= end_copyright_notice ===========================*/
8 
9 #ifndef _COMMON_H_
10 #define _COMMON_H_
11 #include <sstream>
12 #include <cassert>
13 #include <iostream>
14 
15 #include "visa_igc_common_header.h"
16 #include "VISADefines.h"
17 
18 #ifndef _WIN32
19 #include <errno.h>
20 #include "common/secure_string.h"
21 
22 typedef int errno_t;
memcpy_s(void * dst,size_t numberOfElements,const void * src,size_t count)23 static errno_t memcpy_s(void* dst, size_t numberOfElements, const void* src, size_t count)
24 {
25     if ((dst == NULL) || (src == NULL)) {
26         return EINVAL;
27     }
28     if (numberOfElements < count) {
29         return ERANGE;
30     }
31 
32     for (auto c = 0; c != count; c++)
33     {
34         *(((char*)dst) + c) = *(((char*)src) + c);
35     }
36 
37     return 0;
38 }
39 #endif
40 
41 /* stdio.h portability code end */
42 
43 /* stdint.h portability code start */
44 #ifndef _WIN32
45 // FIXME: do all of our configs support inttypes.h?
46 #include <stdint.h>
47 #else
48 #include <inttypes.h>
49 #endif /* _WIN32 */
50 /* stdint.h portability code end */
51 
52 /* Windows types for non-Windows start */
53 #if !defined(_WIN32) && !defined(_WIN64)
54 #define SUCCEED 1
55 #define ERROR 0
56 
57 typedef char CHAR;
58 typedef int INT;
59 typedef short SHORT;
60 typedef int32_t LONG;
61 typedef int64_t LONGLONG;
62 
63 typedef uint32_t UINT32;
64 typedef uint32_t DWORD;
65 typedef uint16_t WORD;
66 
67 typedef double DOUBLE;
68 typedef float FLOAT;
69 
70 #ifndef __LARGE_INTEGER_STRUCT_DEFINED__
71 union LARGE_INTEGER {
72     struct dummy {
73         DWORD LowPart;
74         LONG HighPart;
75     };
76 
77     struct u {
78         DWORD LowPart;
79         LONG HighPart;
80     };
81 
82     LONGLONG QuadPart;
83 };
84 #define __LARGE_INTEGER_STRUCT_DEFINED__
85 #endif // __LARGE_INTEGER_STRUCT_DEFINED__
86 
87 #endif /* Windows types for non-Windows end */
88 
89 //Common code for exception handling, debug messages, platform checks, etc.
90 
91 #if defined(DLL_MODE) && defined(_RELEASE)
92 #define IS_RELEASE_DLL
93 #endif
94 
95 #define VISA_SUCCESS                0
96 #define VISA_FAILURE               (-1)
97 #define VISA_SPILL                 (-3)
98 
99 // stream for error messages
100 extern std::stringstream errorMsgs;
101 
102 #define COUT_ERROR      std::cout
103 
104 #if defined(_DEBUG) && !defined(DLL_MODE)
105 
106 //#define DEBUG_VERBOSE_ON
107 
108 #ifdef DEBUG_VERBOSE_ON
109 #define DEBUG_VERBOSE(msg) { \
110     COUT_ERROR << msg; \
111 }
112 #else
113 #define DEBUG_VERBOSE(msg)
114 #endif
115 
116 #define DEBUG_MSG(msg) { \
117     COUT_ERROR << msg; \
118 }
119 
120 // call the obj's emit function
121 #define DEBUG_EMIT(obj) { \
122     (obj)->emit(COUT_ERROR); \
123 }
124 
125 #else
126 
127 #define DEBUG_VERBOSE(msg)
128 #define DEBUG_MSG(msg)
129 #define DEBUG_EMIT(obj)
130 
131 #endif  // #ifdef _DEBUG
132 
133 // disable asserts only for release DLL
134 #if defined(_DEBUG) || !defined(DLL_MODE) || !defined(NDEBUG)
135 #define ASSERT_USER(x, errormsg)\
136     do {\
137         if (!(x))   \
138         {           \
139             errorMsgs << "Error in Common ISA file:" << errormsg << std::endl; \
140             assert(false); \
141         } \
142     } while (0)
143 
144 #define ASSERT_USER_LOC(x, errormsg, line, file ) \
145     do { \
146         if (!(x))   \
147         { \
148             errorMsgs << "Error in Common ISA file(" << file << ":" << line << "): " << errormsg << std::endl; \
149             assert(false); \
150         }    \
151     } while(0)
152 
153 
154 #define MUST_BE_TRUE2(x, errormsg, inst) \
155     do {\
156         if (!(x))   \
157         { \
158             std::cerr <<errormsg << std::endl;  \
159             inst->emit(errorMsgs, true); \
160             std::cerr << std::endl; \
161             assert(false); \
162         } \
163     } while (0)
164 
165 #define MUST_BE_TRUE(x,errormsg) \
166     do { \
167         if (!(x)) \
168         { \
169             std::cerr << __FILE__ << ":" << __LINE__ << " " << errormsg << std::endl; \
170             assert(false); \
171         } \
172     } while (0)
173 
174 #define MUST_BE_TRUE1(x, lineno, errormsg) \
175     do { \
176         if (!(x)) \
177         { \
178             std::cerr << "(Source Line " << lineno << ") " << errormsg << std::endl;  \
179             assert(false); \
180         } \
181     } while (0)
182 
183 #else
184 #define ASSERT_USER(x, errormsg)
185 #define ASSERT_USER_LOC(x, errormsg, line, file )
186 #define MUST_BE_TRUE(x, errormsg)
187 #define MUST_BE_TRUE1(x, lineno, errormsg)
188 #define MUST_BE_TRUE2(x, errormsg, inst)
189 #endif
190 
191 
192 #define MAX_OPTION_STR_LENGTH 256
193 
194 // sets the platform by enum value
195 int SetVisaPlatform(TARGET_PLATFORM vPlatform);
196 
197 // currently the supported arguments are listed in the common.cpp table
198 // generally the symbol suffix is the preferred name:
199 //   e.g. GENX_SKL  means "SKL"
200 //             ^^^
201 int SetVisaPlatform(const char *platformName);
202 
203 /*************** internal jitter functions ********************/
204 // returns the HW platform for this jitter invocation
205 TARGET_PLATFORM getGenxPlatform();
206 
207 // returns an array of all supported platforms
208 const TARGET_PLATFORM *getGenxAllPlatforms(int *num);
209 
210 const char *getGenxPlatformString(TARGET_PLATFORM);
211 
212 // returns nullptr terminated array of strings that can be parsed
213 // for the platform name; these will be accepted by SetPlatform()
214 const char * const* getGenxPlatformStrings(TARGET_PLATFORM);
215 
216 enum class PlatformGen
217 {
218     GEN_UNKNOWN = 0,
219     GEN8 = 8,
220     GEN9 = 9,
221     GEN10 = 10,
222     GEN11 = 11,
223     XE = 12,
224 };
225 
226 unsigned char getGRFSize();
227 
228 // return the platform generation that can be used for comparison
229 PlatformGen getPlatformGeneration(TARGET_PLATFORM platform);
230 
231 // returns the vISA encoding bits for encoding
232 // NOTE: encoding values are not necessarily in order
233 int getGenxPlatformEncoding();
234 
235 // Error types
236 #define ERROR_UNKNOWN               "ERROR: Unknown fatal internal error"
237 #define ERROR_INTERNAL_ARGUMENT     "ERROR: Invalid argument in an internal function"
238 
239 #define ERROR_MEM_ALLOC             "ERROR: Fail to allocate memory or create object"
240 #define ERROR_FLOWGRAPH             "ERROR: Unknown error in Flow Graph"  // for all unknown errors related to flow graph
241 #define ERROR_SPILLCODE             "ERROR: Unknown error related to spill code"
242 #define ERROR_SCHEDULER             "ERROR: Unknown error in local scheduler"
243 #define ERROR_GRAPHCOLOR            "ERROR: Unknown error in Graph Coloring"
244 #define ERROR_REGALLOC              "ERROR: Unknown error in Register Allocation"
245 
246 #define ERROR_FILE_READ(x)          "ERROR: Invalid or non-existent file " << (x)
247 #define ERROR_OPTION                "ERROR: Invalid input option or option combination"
248 #define ERROR_INVALID_VISA_NAME(x)  "ERROR: Invalid name " << (x)
249 #define ERROR_SYNTAX(x)             "ERROR: Syntax error -- " << (x)
250 #define ERROR_DATA_RANGE(x)         "ERROR: Out of boundary or invalid data value in " << (x)
251 // end of Error Message
252 
253 // Target should be specified as follows
254 // - Kernel target attribute in VISA binarary or ISAASM
255 typedef enum {
256     VISA_CM = 0,
257     VISA_3D = 1
258 } VISATarget;
259 
260 #endif //_COMMON_H_
261