| 1 |
/* Offload image generation tool for PTX. |
| 2 |
|
| 3 |
Copyright (C) 2014-2025 Free Software Foundation, Inc. |
| 4 |
|
| 5 |
Contributed by Nathan Sidwell <[email protected]> and |
| 6 |
Bernd Schmidt <[email protected]>. |
| 7 |
|
| 8 |
This file is part of GCC. |
| 9 |
|
| 10 |
GCC is free software; you can redistribute it and/or modify it |
| 11 |
under the terms of the GNU General Public License as published |
| 12 |
by the Free Software Foundation; either version 3, or (at your |
| 13 |
option) any later version. |
| 14 |
|
| 15 |
GCC is distributed in the hope that it will be useful, but WITHOUT |
| 16 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 17 |
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public |
| 18 |
License for more details. |
| 19 |
|
| 20 |
You should have received a copy of the GNU General Public License |
| 21 |
along with GCC; see the file COPYING3. If not see |
| 22 |
<http://www.gnu.org/licenses/>. */ |
| 23 |
|
| 24 |
/* Munges PTX assembly into a C source file defining the PTX code as a |
| 25 |
string. |
| 26 |
|
| 27 |
This is not a complete assembler. We presume the source is well |
| 28 |
formed from the compiler and can die horribly if it is not. */ |
| 29 |
|
| 30 |
#define IN_TARGET_CODE 1 |
| 31 |
|
| 32 |
#include "config.h" |
| 33 |
#include "system.h" |
| 34 |
#include "coretypes.h" |
| 35 |
#include "obstack.h" |
| 36 |
#include "diagnostic.h" |
| 37 |
#include "intl.h" |
| 38 |
#include <libgen.h> |
| 39 |
#include "collect-utils.h" |
| 40 |
#include "gomp-constants.h" |
| 41 |
|
| 42 |
const char tool_name[] = "nvptx mkoffload"; |
| 43 |
|
| 44 |
#define COMMENT_PREFIX "#" |
| 45 |
|
| 46 |
struct id_map |
| 47 |
{ |
| 48 |
id_map *next; |
| 49 |
char *ptx_name; |
| 50 |
char *dim; |
| 51 |
}; |
| 52 |
|
| 53 |
static id_map *func_ids, **funcs_tail = &func_ids; |
| 54 |
static id_map *ind_func_ids, **ind_funcs_tail = &ind_func_ids; |
| 55 |
static id_map *var_ids, **vars_tail = &var_ids; |
| 56 |
|
| 57 |
/* Files to unlink. */ |
| 58 |
static const char *ptx_name; |
| 59 |
static const char *ptx_cfile_name; |
| 60 |
static const char *omp_requires_file; |
| 61 |
static const char *ptx_dumpbase; |
| 62 |
|
| 63 |
enum offload_abi offload_abi = OFFLOAD_ABI_UNSET; |
| 64 |
const char *offload_abi_host_opts = NULL; |
| 65 |
|
| 66 |
/* Delete tempfiles. */ |
| 67 |
|
| 68 |
void |
| 69 |
tool_cleanup (bool from_signal ATTRIBUTE_UNUSED) |
| 70 |
{ |
| 71 |
if (ptx_cfile_name) |
| 72 |
maybe_unlink (ptx_cfile_name); |
| 73 |
if (ptx_name) |
| 74 |
maybe_unlink (ptx_name); |
| 75 |
if (omp_requires_file) |
| 76 |
maybe_unlink (omp_requires_file); |
| 77 |
} |
| 78 |
|
| 79 |
static void |
| 80 |
mkoffload_cleanup (void) |
| 81 |
{ |
| 82 |
tool_cleanup (false); |
| 83 |
} |
| 84 |
|
| 85 |
/* Unlink FILE unless requested otherwise. */ |
| 86 |
|
| 87 |
void |
| 88 |
maybe_unlink (const char *file) |
| 89 |
{ |
| 90 |
if (!save_temps) |
| 91 |
{ |
| 92 |
if (unlink_if_ordinary (file) |
| 93 |
&& errno != ENOENT) |
| 94 |
fatal_error (input_location, "deleting file %s: %m", file); |
| 95 |
} |
| 96 |
else if (verbose) |
| 97 |
fprintf (stderr, "[Leaving %s]\n", file); |
| 98 |
} |
| 99 |
|
| 100 |
/* Add or change the value of an environment variable, outputting the |
| 101 |
change to standard error if in verbose mode. */ |
| 102 |
static void |
| 103 |
xputenv (const char *string) |
| 104 |
{ |
| 105 |
if (verbose) |
| 106 |
fprintf (stderr, "%s\n", string); |
| 107 |
putenv (CONST_CAST (char *, string)); |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
static void |
| 112 |
record_id (const char *p1, id_map ***where) |
| 113 |
{ |
| 114 |
gcc_assert (p1[0] == '"'); |
| 115 |
p1++; |
| 116 |
const char *end = strchr (p1, '"'); |
| 117 |
const char *end2 = strchr (p1, '\n'); |
| 118 |
if (!end || !end2 || end >= end2) |
| 119 |
fatal_error (input_location, "malformed ptx file"); |
| 120 |
|
| 121 |
id_map *v = XNEW (id_map); |
| 122 |
size_t len = end - p1; |
| 123 |
v->ptx_name = XNEWVEC (char, len + 1); |
| 124 |
memcpy (v->ptx_name, p1, len); |
| 125 |
v->ptx_name[len] = '\0'; |
| 126 |
p1 = end + 1; |
| 127 |
if (*end != '\n') |
| 128 |
{ |
| 129 |
len = end2 - p1; |
| 130 |
v->dim = XNEWVEC (char, len + 1); |
| 131 |
memcpy (v->dim, p1, len); |
| 132 |
v->dim[len] = '\0'; |
| 133 |
} |
| 134 |
else |
| 135 |
v->dim = NULL; |
| 136 |
v->next = NULL; |
| 137 |
id_map **tail = *where; |
| 138 |
*tail = v; |
| 139 |
*where = &v->next; |
| 140 |
} |
| 141 |
|
| 142 |
/* Read the whole input file. It will be NUL terminated (but |
| 143 |
remember, there could be a NUL in the file itself. */ |
| 144 |
|
| 145 |
static const char * |
| 146 |
read_file (FILE *stream, size_t *plen) |
| 147 |
{ |
| 148 |
size_t alloc = 16384; |
| 149 |
size_t base = 0; |
| 150 |
char *buffer; |
| 151 |
|
| 152 |
if (!fseek (stream, 0, SEEK_END)) |
| 153 |
{ |
| 154 |
/* Get the file size. */ |
| 155 |
long s = ftell (stream); |
| 156 |
if (s >= 0) |
| 157 |
alloc = s + 100; |
| 158 |
fseek (stream, 0, SEEK_SET); |
| 159 |
} |
| 160 |
buffer = XNEWVEC (char, alloc); |
| 161 |
|
| 162 |
for (;;) |
| 163 |
{ |
| 164 |
size_t n = fread (buffer + base, 1, alloc - base - 1, stream); |
| 165 |
|
| 166 |
if (!n) |
| 167 |
break; |
| 168 |
base += n; |
| 169 |
if (base + 1 == alloc) |
| 170 |
{ |
| 171 |
alloc *= 2; |
| 172 |
buffer = XRESIZEVEC (char, buffer, alloc); |
| 173 |
} |
| 174 |
} |
| 175 |
buffer[base] = 0; |
| 176 |
*plen = base; |
| 177 |
return buffer; |
| 178 |
} |
| 179 |
|
| 180 |
/* Parse STR, saving found tokens into PVALUES and return their number. |
| 181 |
Tokens are assumed to be delimited by ':'. */ |
| 182 |
static unsigned |
| 183 |
parse_env_var (const char *str, char ***pvalues) |
| 184 |
{ |
| 185 |
const char *curval, *nextval; |
| 186 |
char **values; |
| 187 |
unsigned num = 1, i; |
| 188 |
|
| 189 |
curval = strchr (str, ':'); |
| 190 |
while (curval) |
| 191 |
{ |
| 192 |
num++; |
| 193 |
curval = strchr (curval + 1, ':'); |
| 194 |
} |
| 195 |
|
| 196 |
values = (char **) xmalloc (num * sizeof (char *)); |
| 197 |
curval = str; |
| 198 |
nextval = strchr (curval, ':'); |
| 199 |
if (nextval == NULL) |
| 200 |
nextval = strchr (curval, '\0'); |
| 201 |
|
| 202 |
for (i = 0; i < num; i++) |
| 203 |
{ |
| 204 |
int l = nextval - curval; |
| 205 |
values[i] = (char *) xmalloc (l + 1); |
| 206 |
memcpy (values[i], curval, l); |
| 207 |
values[i][l] = 0; |
| 208 |
curval = nextval + 1; |
| 209 |
nextval = strchr (curval, ':'); |
| 210 |
if (nextval == NULL) |
| 211 |
nextval = strchr (curval, '\0'); |
| 212 |
} |
| 213 |
*pvalues = values; |
| 214 |
return num; |
| 215 |
} |
| 216 |
|
| 217 |
/* Auxiliary function that frees elements of PTR and PTR itself. |
| 218 |
N is number of elements to be freed. If PTR is NULL, nothing is freed. |
| 219 |
If an element is NULL, subsequent elements are not freed. */ |
| 220 |
static void |
| 221 |
free_array_of_ptrs (void **ptr, unsigned n) |
| 222 |
{ |
| 223 |
unsigned i; |
| 224 |
if (!ptr) |
| 225 |
return; |
| 226 |
for (i = 0; i < n; i++) |
| 227 |
{ |
| 228 |
if (!ptr[i]) |
| 229 |
break; |
| 230 |
free (ptr[i]); |
| 231 |
} |
| 232 |
free (ptr); |
| 233 |
return; |
| 234 |
} |
| 235 |
|
| 236 |
/* Check whether NAME can be accessed in MODE. This is like access, |
| 237 |
except that it never considers directories to be executable. */ |
| 238 |
static int |
| 239 |
access_check (const char *name, int mode) |
| 240 |
{ |
| 241 |
if (mode == X_OK) |
| 242 |
{ |
| 243 |
struct stat st; |
| 244 |
|
| 245 |
if (stat (name, &st) < 0 || S_ISDIR (st.st_mode)) |
| 246 |
return -1; |
| 247 |
} |
| 248 |
|
| 249 |
return access (name, mode); |
| 250 |
} |
| 251 |
|
| 252 |
static void |
| 253 |
process (FILE *in, FILE *out, uint32_t omp_requires) |
| 254 |
{ |
| 255 |
size_t len = 0; |
| 256 |
const char *input = read_file (in, &len); |
| 257 |
const char *comma; |
| 258 |
id_map const *id; |
| 259 |
unsigned obj_count = 0; |
| 260 |
unsigned ix; |
| 261 |
const char *sm_ver = NULL, *version = NULL; |
| 262 |
const char *sm_ver2 = NULL, *version2 = NULL; |
| 263 |
/* To reduce the number of reallocations for 'file_idx', guess 'file_cnt' |
| 264 |
(very roughly...), based on 'len'. */ |
| 265 |
const size_t file_cnt_guessed = 13 + len / 27720; |
| 266 |
auto_vec<size_t> file_idx (file_cnt_guessed); |
| 267 |
|
| 268 |
fprintf (out, "#include <stdint.h>\n\n"); |
| 269 |
|
| 270 |
/* Dump out char arrays for each PTX object file. These are |
| 271 |
terminated by a NUL. */ |
| 272 |
for (size_t i = 0; i != len;) |
| 273 |
{ |
| 274 |
file_idx.safe_push (i); |
| 275 |
|
| 276 |
char c; |
| 277 |
bool output_fn_ptr = false; |
| 278 |
|
| 279 |
fprintf (out, "static const char ptx_code_%u[] =\n\t\"", obj_count++); |
| 280 |
while ((c = input[i++])) |
| 281 |
{ |
| 282 |
switch (c) |
| 283 |
{ |
| 284 |
case '\r': |
| 285 |
continue; |
| 286 |
case '\n': |
| 287 |
fprintf (out, "\\n\"\n\t\""); |
| 288 |
/* Look for mappings on subsequent lines. */ |
| 289 |
if (UNLIKELY (startswith (input + i, ".target sm_"))) |
| 290 |
{ |
| 291 |
sm_ver = input + i + strlen (".target sm_"); |
| 292 |
continue; |
| 293 |
} |
| 294 |
if (UNLIKELY (startswith (input + i, ".version "))) |
| 295 |
{ |
| 296 |
version = input + i + strlen (".version "); |
| 297 |
continue; |
| 298 |
} |
| 299 |
while (startswith (input + i, "//:")) |
| 300 |
{ |
| 301 |
i += 3; |
| 302 |
|
| 303 |
if (startswith (input + i, "VAR_MAP ")) |
| 304 |
record_id (input + i + 8, &vars_tail); |
| 305 |
else if (startswith (input + i, "FUNC_MAP ")) |
| 306 |
{ |
| 307 |
output_fn_ptr = true; |
| 308 |
record_id (input + i + 9, &funcs_tail); |
| 309 |
} |
| 310 |
else if (startswith (input + i, "IND_FUNC_MAP ")) |
| 311 |
{ |
| 312 |
output_fn_ptr = true; |
| 313 |
record_id (input + i + 13, &ind_funcs_tail); |
| 314 |
} |
| 315 |
else |
| 316 |
abort (); |
| 317 |
/* Skip to next line. */ |
| 318 |
while (input[i++] != '\n') |
| 319 |
continue; |
| 320 |
} |
| 321 |
continue; |
| 322 |
case '"': |
| 323 |
case '\\': |
| 324 |
putc ('\\', out); |
| 325 |
break; |
| 326 |
default: |
| 327 |
break; |
| 328 |
} |
| 329 |
putc (c, out); |
| 330 |
} |
| 331 |
fprintf (out, "\";\n\n"); |
| 332 |
if (output_fn_ptr |
| 333 |
&& (omp_requires & GOMP_REQUIRES_REVERSE_OFFLOAD) != 0) |
| 334 |
{ |
| 335 |
if (sm_ver && sm_ver[0] == '3' && sm_ver[1] == '0' |
| 336 |
&& sm_ver[2] == '\n') |
| 337 |
{ |
| 338 |
warning_at (input_location, 0, |
| 339 |
"%<omp requires reverse_offload%> requires at " |
| 340 |
"least %<sm_35%> for " |
| 341 |
"%<-foffload-options=nvptx-none=-march=%> - disabling" |
| 342 |
" offload-code generation for this device type"); |
| 343 |
/* As now an empty file is compiled and there is no call to |
| 344 |
GOMP_offload_register_ver, this device type is effectively |
| 345 |
disabled. */ |
| 346 |
fflush (out); |
| 347 |
ftruncate (fileno (out), 0); |
| 348 |
return; |
| 349 |
} |
| 350 |
sm_ver2 = sm_ver; |
| 351 |
version2 = version; |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
const size_t file_cnt = file_idx.length (); |
| 356 |
gcc_checking_assert (file_cnt == obj_count); |
| 357 |
|
| 358 |
/* Create function-pointer array, required for reverse |
| 359 |
offload function-pointer lookup. */ |
| 360 |
|
| 361 |
if (func_ids && (omp_requires & GOMP_REQUIRES_REVERSE_OFFLOAD) != 0) |
| 362 |
{ |
| 363 |
const char needle[] = "// BEGIN GLOBAL FUNCTION DECL: "; |
| 364 |
fprintf (out, "static const char ptx_code_%u[] =\n", obj_count++); |
| 365 |
fprintf (out, "\t\".version "); |
| 366 |
for (size_t i = 0; version2[i] != '\0' && version2[i] != '\n'; i++) |
| 367 |
fputc (version2[i], out); |
| 368 |
fprintf (out, "\"\n\t\".target sm_"); |
| 369 |
for (size_t i = 0; version2[i] != '\0' && sm_ver2[i] != '\n'; i++) |
| 370 |
fputc (sm_ver2[i], out); |
| 371 |
fprintf (out, "\"\n\t\".file 1 \\\"<dummy>\\\"\"\n"); |
| 372 |
|
| 373 |
/* WORKAROUND - see PR 108098 |
| 374 |
It seems as if older CUDA JIT compiler optimizes the function pointers |
| 375 |
in offload_func_table to NULL, which can be prevented by adding a |
| 376 |
dummy procedure. With CUDA 11.1, it seems to work fine without |
| 377 |
workaround while CUDA 10.2 as some ancient version have need the |
| 378 |
workaround. Assuming CUDA 11.0 fixes it, emitting it could be |
| 379 |
restricted to 'if (sm_ver2[0] < 8 && version2[0] < 7)' as sm_80 and |
| 380 |
PTX ISA 7.0 are new in CUDA 11.0; for 11.1 it would be sm_86 and |
| 381 |
PTX ISA 7.1. */ |
| 382 |
fprintf (out, "\n\t\".func __dummy$func ( );\"\n"); |
| 383 |
fprintf (out, "\t\".func __dummy$func ( )\"\n"); |
| 384 |
fprintf (out, "\t\"{\"\n"); |
| 385 |
fprintf (out, "\t\"}\"\n"); |
| 386 |
|
| 387 |
size_t fidx = 0; |
| 388 |
for (id = func_ids; id; id = id->next) |
| 389 |
{ |
| 390 |
/* Only 'nohost' functions are needed - use NULL for the rest. |
| 391 |
Alternatively, besides searching for 'BEGIN FUNCTION DECL', |
| 392 |
checking for '.visible .entry ' + id->ptx_name would be |
| 393 |
required. */ |
| 394 |
if (!endswith (id->ptx_name, "$nohost") |
| 395 |
&& !strstr (id->ptx_name, "$nohost$")) |
| 396 |
continue; |
| 397 |
fprintf (out, "\t\".extern "); |
| 398 |
const char *p = input + file_idx[fidx]; |
| 399 |
while (true) |
| 400 |
{ |
| 401 |
p = strstr (p, needle); |
| 402 |
if (!p) |
| 403 |
{ |
| 404 |
fidx++; |
| 405 |
if (fidx >= file_cnt) |
| 406 |
break; |
| 407 |
p = input + file_idx[fidx]; |
| 408 |
continue; |
| 409 |
} |
| 410 |
p += strlen (needle); |
| 411 |
if (!startswith (p, id->ptx_name)) |
| 412 |
continue; |
| 413 |
p += strlen (id->ptx_name); |
| 414 |
if (*p != '\n') |
| 415 |
continue; |
| 416 |
p++; |
| 417 |
gcc_assert (startswith (p, ".visible ")); |
| 418 |
p += strlen (".visible "); |
| 419 |
for (; *p != '\0' && *p != '\n'; p++) |
| 420 |
fputc (*p, out); |
| 421 |
break; |
| 422 |
} |
| 423 |
fprintf (out, "\"\n"); |
| 424 |
if (fidx == file_cnt) |
| 425 |
fatal_error (input_location, |
| 426 |
"Cannot find function declaration for %qs", |
| 427 |
id->ptx_name); |
| 428 |
} |
| 429 |
fprintf (out, "\t\".visible .global .align 8 .u64 " |
| 430 |
"$offload_func_table[] = {"); |
| 431 |
for (comma = "", id = func_ids; id; comma = ",", id = id->next) |
| 432 |
fprintf (out, "%s\"\n\t\t\"%s", comma, |
| 433 |
(endswith (id->ptx_name, "$nohost") |
| 434 |
|| strstr (id->ptx_name, "$nohost$")) ? id->ptx_name : "0"); |
| 435 |
fprintf (out, "};\\n\";\n\n"); |
| 436 |
} |
| 437 |
|
| 438 |
if (ind_func_ids) |
| 439 |
{ |
| 440 |
const char needle[] = "// BEGIN GLOBAL FUNCTION DECL: "; |
| 441 |
|
| 442 |
fprintf (out, "static const char ptx_code_%u[] =\n", obj_count++); |
| 443 |
fprintf (out, "\t\".version "); |
| 444 |
for (size_t i = 0; version[i] != '\0' && version[i] != '\n'; i++) |
| 445 |
fputc (version[i], out); |
| 446 |
fprintf (out, "\"\n\t\".target sm_"); |
| 447 |
for (size_t i = 0; sm_ver[i] != '\0' && sm_ver[i] != '\n'; i++) |
| 448 |
fputc (sm_ver[i], out); |
| 449 |
fprintf (out, "\"\n\t\".file 2 \\\"<dummy>\\\"\"\n"); |
| 450 |
|
| 451 |
/* WORKAROUND - see PR 108098 |
| 452 |
It seems as if older CUDA JIT compiler optimizes the function pointers |
| 453 |
in offload_func_table to NULL, which can be prevented by adding a |
| 454 |
dummy procedure. With CUDA 11.1, it seems to work fine without |
| 455 |
workaround while CUDA 10.2 as some ancient version have need the |
| 456 |
workaround. Assuming CUDA 11.0 fixes it, emitting it could be |
| 457 |
restricted to 'if (sm_ver2[0] < 8 && version2[0] < 7)' as sm_80 and |
| 458 |
PTX ISA 7.0 are new in CUDA 11.0; for 11.1 it would be sm_86 and |
| 459 |
PTX ISA 7.1. */ |
| 460 |
fprintf (out, "\n\t\".func __dummy$func2 ( );\"\n"); |
| 461 |
fprintf (out, "\t\".func __dummy$func2 ( )\"\n"); |
| 462 |
fprintf (out, "\t\"{\"\n"); |
| 463 |
fprintf (out, "\t\"}\"\n"); |
| 464 |
|
| 465 |
size_t fidx = 0; |
| 466 |
for (id = ind_func_ids; id; id = id->next) |
| 467 |
{ |
| 468 |
fprintf (out, "\t\".extern "); |
| 469 |
const char *p = input + file_idx[fidx]; |
| 470 |
while (true) |
| 471 |
{ |
| 472 |
p = strstr (p, needle); |
| 473 |
if (!p) |
| 474 |
{ |
| 475 |
fidx++; |
| 476 |
if (fidx >= file_cnt) |
| 477 |
break; |
| 478 |
p = input + file_idx[fidx]; |
| 479 |
continue; |
| 480 |
} |
| 481 |
p += strlen (needle); |
| 482 |
if (!startswith (p, id->ptx_name)) |
| 483 |
continue; |
| 484 |
p += strlen (id->ptx_name); |
| 485 |
if (*p != '\n') |
| 486 |
continue; |
| 487 |
p++; |
| 488 |
/* Skip over any directives. */ |
| 489 |
while (!startswith (p, ".func")) |
| 490 |
while (*p++ != ' '); |
| 491 |
for (; *p != '\0' && *p != '\n'; p++) |
| 492 |
fputc (*p, out); |
| 493 |
break; |
| 494 |
} |
| 495 |
fprintf (out, "\"\n"); |
| 496 |
if (fidx == file_cnt) |
| 497 |
fatal_error (input_location, |
| 498 |
"Cannot find function declaration for %qs", |
| 499 |
id->ptx_name); |
| 500 |
} |
| 501 |
|
| 502 |
fprintf (out, "\t\".visible .global .align 8 .u64 " |
| 503 |
"$offload_ind_func_table[] = {"); |
| 504 |
for (comma = "", id = ind_func_ids; id; comma = ",", id = id->next) |
| 505 |
fprintf (out, "%s\"\n\t\t\"%s", comma, id->ptx_name); |
| 506 |
fprintf (out, "};\\n\";\n\n"); |
| 507 |
} |
| 508 |
|
| 509 |
/* Dump out array of pointers to ptx object strings. */ |
| 510 |
fprintf (out, "static const struct ptx_obj {\n" |
| 511 |
" const char *code;\n" |
| 512 |
" __SIZE_TYPE__ size;\n" |
| 513 |
"} ptx_objs[] = {"); |
| 514 |
for (comma = "", ix = 0; ix != obj_count; comma = ",", ix++) |
| 515 |
fprintf (out, "%s\n\t{ptx_code_%u, sizeof (ptx_code_%u)}", comma, ix, ix); |
| 516 |
fprintf (out, "\n};\n\n"); |
| 517 |
|
| 518 |
/* Dump out variable idents. */ |
| 519 |
fprintf (out, "static const char *const var_mappings[] = {"); |
| 520 |
for (comma = "", id = var_ids; id; comma = ",", id = id->next) |
| 521 |
fprintf (out, "%s\n\t\"%s\"", comma, id->ptx_name); |
| 522 |
fprintf (out, "\n};\n\n"); |
| 523 |
|
| 524 |
/* Dump out function idents. */ |
| 525 |
fprintf (out, "static const struct nvptx_fn {\n" |
| 526 |
" const char *name;\n" |
| 527 |
" unsigned short dim[%d];\n" |
| 528 |
"} func_mappings[] = {\n", GOMP_DIM_MAX); |
| 529 |
for (comma = "", id = func_ids; id; comma = ",", id = id->next) |
| 530 |
fprintf (out, "%s\n\t{\"%s\"%s}", comma, id->ptx_name, |
| 531 |
id->dim ? id->dim : ""); |
| 532 |
fprintf (out, "\n};\n\n"); |
| 533 |
|
| 534 |
/* Dump out indirect function idents. */ |
| 535 |
fprintf (out, "static const char *const ind_func_mappings[] = {"); |
| 536 |
for (comma = "", id = ind_func_ids; id; comma = ",", id = id->next) |
| 537 |
fprintf (out, "%s\n\t\"%s\"", comma, id->ptx_name); |
| 538 |
fprintf (out, "\n};\n\n"); |
| 539 |
|
| 540 |
fprintf (out, |
| 541 |
"static const struct nvptx_data {\n" |
| 542 |
" uintptr_t omp_requires_mask;\n" |
| 543 |
" const struct ptx_obj *ptx_objs;\n" |
| 544 |
" unsigned ptx_num;\n" |
| 545 |
" const char *const *var_names;\n" |
| 546 |
" unsigned var_num;\n" |
| 547 |
" const struct nvptx_fn *fn_names;\n" |
| 548 |
" unsigned fn_num;\n" |
| 549 |
" unsigned ind_fn_num;\n" |
| 550 |
"} nvptx_data = {\n" |
| 551 |
" %d, ptx_objs, sizeof (ptx_objs) / sizeof (ptx_objs[0]),\n" |
| 552 |
" var_mappings," |
| 553 |
" sizeof (var_mappings) / sizeof (var_mappings[0]),\n" |
| 554 |
" func_mappings," |
| 555 |
" sizeof (func_mappings) / sizeof (func_mappings[0]),\n" |
| 556 |
" sizeof (ind_func_mappings) / sizeof (ind_func_mappings[0])\n" |
| 557 |
"};\n\n", omp_requires); |
| 558 |
|
| 559 |
fprintf (out, "#ifdef __cplusplus\n" |
| 560 |
"extern \"C\" {\n" |
| 561 |
"#endif\n"); |
| 562 |
|
| 563 |
fprintf (out, "extern void GOMP_offload_register_ver" |
| 564 |
" (unsigned, const void *, int, const void *);\n"); |
| 565 |
fprintf (out, "extern void GOMP_offload_unregister_ver" |
| 566 |
" (unsigned, const void *, int, const void *);\n"); |
| 567 |
|
| 568 |
fprintf (out, "#ifdef __cplusplus\n" |
| 569 |
"}\n" |
| 570 |
"#endif\n"); |
| 571 |
|
| 572 |
fprintf (out, "extern const void *const __OFFLOAD_TABLE__[];\n\n"); |
| 573 |
|
| 574 |
fprintf (out, "static __attribute__((constructor)) void init (void)\n" |
| 575 |
"{\n" |
| 576 |
" GOMP_offload_register_ver (%#x, __OFFLOAD_TABLE__," |
| 577 |
" %d/*NVIDIA_PTX*/, &nvptx_data);\n" |
| 578 |
"};\n", |
| 579 |
GOMP_VERSION_PACK (GOMP_VERSION, GOMP_VERSION_NVIDIA_PTX), |
| 580 |
GOMP_DEVICE_NVIDIA_PTX); |
| 581 |
|
| 582 |
fprintf (out, "static __attribute__((destructor)) void fini (void)\n" |
| 583 |
"{\n" |
| 584 |
" GOMP_offload_unregister_ver (%#x, __OFFLOAD_TABLE__," |
| 585 |
" %d/*NVIDIA_PTX*/, &nvptx_data);\n" |
| 586 |
"};\n", |
| 587 |
GOMP_VERSION_PACK (GOMP_VERSION, GOMP_VERSION_NVIDIA_PTX), |
| 588 |
GOMP_DEVICE_NVIDIA_PTX); |
| 589 |
} |
| 590 |
|
| 591 |
static void |
| 592 |
compile_native (const char *infile, const char *outfile, const char *compiler, |
| 593 |
bool fPIC, bool fpic) |
| 594 |
{ |
| 595 |
const char *collect_gcc_options = getenv ("COLLECT_GCC_OPTIONS"); |
| 596 |
if (!collect_gcc_options) |
| 597 |
fatal_error (input_location, |
| 598 |
"environment variable COLLECT_GCC_OPTIONS must be set"); |
| 599 |
|
| 600 |
struct obstack argv_obstack; |
| 601 |
obstack_init (&argv_obstack); |
| 602 |
obstack_ptr_grow (&argv_obstack, compiler); |
| 603 |
if (fPIC) |
| 604 |
obstack_ptr_grow (&argv_obstack, "-fPIC"); |
| 605 |
if (fpic) |
| 606 |
obstack_ptr_grow (&argv_obstack, "-fpic"); |
| 607 |
if (save_temps) |
| 608 |
obstack_ptr_grow (&argv_obstack, "-save-temps"); |
| 609 |
if (verbose) |
| 610 |
obstack_ptr_grow (&argv_obstack, "-v"); |
| 611 |
obstack_ptr_grow (&argv_obstack, "-dumpdir"); |
| 612 |
obstack_ptr_grow (&argv_obstack, ""); |
| 613 |
obstack_ptr_grow (&argv_obstack, "-dumpbase"); |
| 614 |
obstack_ptr_grow (&argv_obstack, ptx_dumpbase); |
| 615 |
obstack_ptr_grow (&argv_obstack, "-dumpbase-ext"); |
| 616 |
obstack_ptr_grow (&argv_obstack, ".c"); |
| 617 |
if (!offload_abi_host_opts) |
| 618 |
fatal_error (input_location, |
| 619 |
"%<-foffload-abi-host-opts%> not specified."); |
| 620 |
obstack_ptr_grow (&argv_obstack, offload_abi_host_opts); |
| 621 |
obstack_ptr_grow (&argv_obstack, infile); |
| 622 |
obstack_ptr_grow (&argv_obstack, "-c"); |
| 623 |
obstack_ptr_grow (&argv_obstack, "-o"); |
| 624 |
obstack_ptr_grow (&argv_obstack, outfile); |
| 625 |
obstack_ptr_grow (&argv_obstack, NULL); |
| 626 |
|
| 627 |
const char **new_argv = XOBFINISH (&argv_obstack, const char **); |
| 628 |
fork_execute (new_argv[0], CONST_CAST (char **, new_argv), true, |
| 629 |
".gccnative_args"); |
| 630 |
obstack_free (&argv_obstack, NULL); |
| 631 |
} |
| 632 |
|
| 633 |
int |
| 634 |
main (int argc, char **argv) |
| 635 |
{ |
| 636 |
FILE *in = stdin; |
| 637 |
FILE *out = stdout; |
| 638 |
const char *outname = 0; |
| 639 |
|
| 640 |
progname = tool_name; |
| 641 |
gcc_init_libintl (); |
| 642 |
diagnostic_initialize (global_dc, 0); |
| 643 |
diagnostic_color_init (global_dc); |
| 644 |
|
| 645 |
if (atexit (mkoffload_cleanup) != 0) |
| 646 |
fatal_error (input_location, "atexit failed"); |
| 647 |
|
| 648 |
char *collect_gcc = getenv ("COLLECT_GCC"); |
| 649 |
if (collect_gcc == NULL) |
| 650 |
fatal_error (input_location, "COLLECT_GCC must be set."); |
| 651 |
const char *gcc_path = dirname (ASTRDUP (collect_gcc)); |
| 652 |
const char *gcc_exec = basename (ASTRDUP (collect_gcc)); |
| 653 |
|
| 654 |
size_t len = (strlen (gcc_path) + 1 |
| 655 |
+ strlen (GCC_INSTALL_NAME) |
| 656 |
+ 1); |
| 657 |
char *driver = XALLOCAVEC (char, len); |
| 658 |
|
| 659 |
if (strcmp (gcc_exec, collect_gcc) == 0) |
| 660 |
/* collect_gcc has no path, so it was found in PATH. Make sure we also |
| 661 |
find accel-gcc in PATH. */ |
| 662 |
gcc_path = NULL; |
| 663 |
|
| 664 |
int driver_used = 0; |
| 665 |
if (gcc_path != NULL) |
| 666 |
driver_used = sprintf (driver, "%s/", gcc_path); |
| 667 |
sprintf (driver + driver_used, "%s", GCC_INSTALL_NAME); |
| 668 |
|
| 669 |
bool found = false; |
| 670 |
if (gcc_path == NULL) |
| 671 |
found = true; |
| 672 |
else if (access_check (driver, X_OK) == 0) |
| 673 |
found = true; |
| 674 |
else |
| 675 |
{ |
| 676 |
/* Don't use alloca pointer with XRESIZEVEC. */ |
| 677 |
driver = NULL; |
| 678 |
/* Look in all COMPILER_PATHs for GCC_INSTALL_NAME. */ |
| 679 |
char **paths = NULL; |
| 680 |
unsigned n_paths; |
| 681 |
n_paths = parse_env_var (getenv ("COMPILER_PATH"), &paths); |
| 682 |
for (unsigned i = 0; i < n_paths; i++) |
| 683 |
{ |
| 684 |
len = strlen (paths[i]) + 1 + strlen (GCC_INSTALL_NAME) + 1; |
| 685 |
driver = XRESIZEVEC (char, driver, len); |
| 686 |
sprintf (driver, "%s/%s", paths[i], GCC_INSTALL_NAME); |
| 687 |
if (access_check (driver, X_OK) == 0) |
| 688 |
{ |
| 689 |
found = true; |
| 690 |
break; |
| 691 |
} |
| 692 |
} |
| 693 |
free_array_of_ptrs ((void **) paths, n_paths); |
| 694 |
} |
| 695 |
|
| 696 |
if (!found) |
| 697 |
fatal_error (input_location, |
| 698 |
"offload compiler %s not found (consider using %<-B%>)", |
| 699 |
GCC_INSTALL_NAME); |
| 700 |
|
| 701 |
/* We may be called with all the arguments stored in some file and |
| 702 |
passed with @file. Expand them into argv before processing. */ |
| 703 |
expandargv (&argc, &argv); |
| 704 |
|
| 705 |
/* Scan the argument vector. */ |
| 706 |
bool fopenmp = false; |
| 707 |
bool fopenacc = false; |
| 708 |
bool fPIC = false; |
| 709 |
bool fpic = false; |
| 710 |
for (int i = 1; i < argc; i++) |
| 711 |
{ |
| 712 |
#define STR "-foffload-abi=" |
| 713 |
if (startswith (argv[i], STR)) |
| 714 |
{ |
| 715 |
if (strcmp (argv[i] + strlen (STR), "lp64") == 0) |
| 716 |
offload_abi = OFFLOAD_ABI_LP64; |
| 717 |
else if (strcmp (argv[i] + strlen (STR), "ilp32") == 0) |
| 718 |
offload_abi = OFFLOAD_ABI_ILP32; |
| 719 |
else |
| 720 |
fatal_error (input_location, |
| 721 |
"unrecognizable argument of option " STR); |
| 722 |
} |
| 723 |
#undef STR |
| 724 |
else if (startswith (argv[i], "-foffload-abi-host-opts=")) |
| 725 |
{ |
| 726 |
if (offload_abi_host_opts) |
| 727 |
fatal_error (input_location, |
| 728 |
"%<-foffload-abi-host-opts%> specified " |
| 729 |
"multiple times"); |
| 730 |
offload_abi_host_opts |
| 731 |
= argv[i] + strlen ("-foffload-abi-host-opts="); |
| 732 |
} |
| 733 |
else if (strcmp (argv[i], "-fopenmp") == 0) |
| 734 |
fopenmp = true; |
| 735 |
else if (strcmp (argv[i], "-fopenacc") == 0) |
| 736 |
fopenacc = true; |
| 737 |
else if (strcmp (argv[i], "-fPIC") == 0) |
| 738 |
fPIC = true; |
| 739 |
else if (strcmp (argv[i], "-fpic") == 0) |
| 740 |
fpic = true; |
| 741 |
else if (strcmp (argv[i], "-save-temps") == 0) |
| 742 |
save_temps = true; |
| 743 |
else if (strcmp (argv[i], "-v") == 0) |
| 744 |
verbose = true; |
| 745 |
else if (strcmp (argv[i], "-dumpbase") == 0 |
| 746 |
&& i + 1 < argc) |
| 747 |
dumppfx = argv[++i]; |
| 748 |
/* Translate host into offloading libraries. */ |
| 749 |
else if (strcmp (argv[i], "-l_GCC_gfortran") == 0 |
| 750 |
|| strcmp (argv[i], "-l_GCC_m") == 0 |
| 751 |
|| strcmp (argv[i], "-l_GCC_stdc++") == 0) |
| 752 |
{ |
| 753 |
/* Elide '_GCC_'. */ |
| 754 |
size_t i_dst = strlen ("-l"); |
| 755 |
size_t i_src = strlen ("-l_GCC_"); |
| 756 |
char c; |
| 757 |
do |
| 758 |
c = argv[i][i_dst++] = argv[i][i_src++]; |
| 759 |
while (c != '\0'); |
| 760 |
} |
| 761 |
} |
| 762 |
if (!(fopenacc ^ fopenmp)) |
| 763 |
fatal_error (input_location, "either %<-fopenacc%> or %<-fopenmp%> " |
| 764 |
"must be set"); |
| 765 |
|
| 766 |
struct obstack argv_obstack; |
| 767 |
obstack_init (&argv_obstack); |
| 768 |
obstack_ptr_grow (&argv_obstack, driver); |
| 769 |
if (save_temps) |
| 770 |
obstack_ptr_grow (&argv_obstack, "-save-temps"); |
| 771 |
if (verbose) |
| 772 |
obstack_ptr_grow (&argv_obstack, "-v"); |
| 773 |
obstack_ptr_grow (&argv_obstack, "-xlto"); |
| 774 |
switch (offload_abi) |
| 775 |
{ |
| 776 |
case OFFLOAD_ABI_LP64: |
| 777 |
obstack_ptr_grow (&argv_obstack, "-m64"); |
| 778 |
break; |
| 779 |
case OFFLOAD_ABI_ILP32: |
| 780 |
obstack_ptr_grow (&argv_obstack, "-m32"); |
| 781 |
break; |
| 782 |
default: |
| 783 |
gcc_unreachable (); |
| 784 |
} |
| 785 |
if (fopenmp) |
| 786 |
obstack_ptr_grow (&argv_obstack, "-mgomp"); |
| 787 |
/* The host code may contain exception handling constructs. |
| 788 |
Handle these as good as we can. */ |
| 789 |
obstack_ptr_grow (&argv_obstack, "-mfake-exceptions"); |
| 790 |
|
| 791 |
for (int ix = 1; ix != argc; ix++) |
| 792 |
{ |
| 793 |
if (!strcmp (argv[ix], "-o") && ix + 1 != argc) |
| 794 |
outname = argv[++ix]; |
| 795 |
else |
| 796 |
obstack_ptr_grow (&argv_obstack, argv[ix]); |
| 797 |
} |
| 798 |
|
| 799 |
if (!dumppfx) |
| 800 |
dumppfx = outname; |
| 801 |
|
| 802 |
ptx_dumpbase = concat (dumppfx, ".c", NULL); |
| 803 |
if (save_temps) |
| 804 |
ptx_cfile_name = ptx_dumpbase; |
| 805 |
else |
| 806 |
ptx_cfile_name = make_temp_file (".c"); |
| 807 |
|
| 808 |
out = fopen (ptx_cfile_name, "w"); |
| 809 |
if (!out) |
| 810 |
fatal_error (input_location, "cannot open '%s'", ptx_cfile_name); |
| 811 |
|
| 812 |
/* PR libgomp/65099: Currently, we only support offloading in 64-bit |
| 813 |
configurations. */ |
| 814 |
if (offload_abi == OFFLOAD_ABI_LP64) |
| 815 |
{ |
| 816 |
char *mko_dumpbase = concat (dumppfx, ".mkoffload", NULL); |
| 817 |
if (save_temps) |
| 818 |
ptx_name = mko_dumpbase; |
| 819 |
else |
| 820 |
ptx_name = make_temp_file (".mkoffload"); |
| 821 |
obstack_ptr_grow (&argv_obstack, "-dumpdir"); |
| 822 |
obstack_ptr_grow (&argv_obstack, ""); |
| 823 |
obstack_ptr_grow (&argv_obstack, "-dumpbase"); |
| 824 |
obstack_ptr_grow (&argv_obstack, mko_dumpbase); |
| 825 |
obstack_ptr_grow (&argv_obstack, "-dumpbase-ext"); |
| 826 |
obstack_ptr_grow (&argv_obstack, ""); |
| 827 |
obstack_ptr_grow (&argv_obstack, "-o"); |
| 828 |
obstack_ptr_grow (&argv_obstack, ptx_name); |
| 829 |
obstack_ptr_grow (&argv_obstack, NULL); |
| 830 |
const char **new_argv = XOBFINISH (&argv_obstack, const char **); |
| 831 |
|
| 832 |
char *execpath = getenv ("GCC_EXEC_PREFIX"); |
| 833 |
char *cpath = getenv ("COMPILER_PATH"); |
| 834 |
char *lpath = getenv ("LIBRARY_PATH"); |
| 835 |
unsetenv ("GCC_EXEC_PREFIX"); |
| 836 |
unsetenv ("COMPILER_PATH"); |
| 837 |
unsetenv ("LIBRARY_PATH"); |
| 838 |
|
| 839 |
if (save_temps) |
| 840 |
omp_requires_file = concat (dumppfx, ".mkoffload.omp_requires", NULL); |
| 841 |
else |
| 842 |
omp_requires_file = make_temp_file (".mkoffload.omp_requires"); |
| 843 |
|
| 844 |
xputenv (concat ("GCC_OFFLOAD_OMP_REQUIRES_FILE=", omp_requires_file, NULL)); |
| 845 |
fork_execute (new_argv[0], CONST_CAST (char **, new_argv), true, |
| 846 |
".gcc_args"); |
| 847 |
obstack_free (&argv_obstack, NULL); |
| 848 |
unsetenv("GCC_OFFLOAD_OMP_REQUIRES_FILE"); |
| 849 |
|
| 850 |
xputenv (concat ("GCC_EXEC_PREFIX=", execpath, NULL)); |
| 851 |
xputenv (concat ("COMPILER_PATH=", cpath, NULL)); |
| 852 |
xputenv (concat ("LIBRARY_PATH=", lpath, NULL)); |
| 853 |
|
| 854 |
in = fopen (omp_requires_file, "rb"); |
| 855 |
if (!in) |
| 856 |
fatal_error (input_location, "cannot open omp_requires file %qs", |
| 857 |
omp_requires_file); |
| 858 |
uint32_t omp_requires; |
| 859 |
if (fread (&omp_requires, sizeof (omp_requires), 1, in) != 1) |
| 860 |
fatal_error (input_location, "cannot read omp_requires file %qs", |
| 861 |
omp_requires_file); |
| 862 |
fclose (in); |
| 863 |
|
| 864 |
in = fopen (ptx_name, "r"); |
| 865 |
if (!in) |
| 866 |
fatal_error (input_location, "cannot open intermediate ptx file"); |
| 867 |
|
| 868 |
process (in, out, omp_requires); |
| 869 |
fclose (in); |
| 870 |
} |
| 871 |
|
| 872 |
fclose (out); |
| 873 |
|
| 874 |
compile_native (ptx_cfile_name, outname, collect_gcc, fPIC, fpic); |
| 875 |
|
| 876 |
return 0; |
| 877 |
} |