1 /* Offload image generation tool for PTX.
2
3 Copyright (C) 2014-2022 Free Software Foundation, Inc.
4
5 Contributed by Nathan Sidwell <nathan@codesourcery.com> and
6 Bernd Schmidt <bernds@codesourcery.com>.
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 };
51
52 static id_map *func_ids, **funcs_tail = &func_ids;
53 static id_map *var_ids, **vars_tail = &var_ids;
54
55 /* Files to unlink. */
56 static const char *ptx_name;
57 static const char *ptx_cfile_name;
58 static const char *ptx_dumpbase;
59
60 enum offload_abi offload_abi = OFFLOAD_ABI_UNSET;
61
62 /* Delete tempfiles. */
63
64 void
tool_cleanup(bool from_signal ATTRIBUTE_UNUSED)65 tool_cleanup (bool from_signal ATTRIBUTE_UNUSED)
66 {
67 if (ptx_cfile_name)
68 maybe_unlink (ptx_cfile_name);
69 if (ptx_name)
70 maybe_unlink (ptx_name);
71 }
72
73 static void
mkoffload_cleanup(void)74 mkoffload_cleanup (void)
75 {
76 tool_cleanup (false);
77 }
78
79 /* Unlink FILE unless requested otherwise. */
80
81 void
maybe_unlink(const char * file)82 maybe_unlink (const char *file)
83 {
84 if (!save_temps)
85 {
86 if (unlink_if_ordinary (file)
87 && errno != ENOENT)
88 fatal_error (input_location, "deleting file %s: %m", file);
89 }
90 else if (verbose)
91 fprintf (stderr, "[Leaving %s]\n", file);
92 }
93
94 /* Add or change the value of an environment variable, outputting the
95 change to standard error if in verbose mode. */
96 static void
xputenv(const char * string)97 xputenv (const char *string)
98 {
99 if (verbose)
100 fprintf (stderr, "%s\n", string);
101 putenv (CONST_CAST (char *, string));
102 }
103
104
105 static void
record_id(const char * p1,id_map *** where)106 record_id (const char *p1, id_map ***where)
107 {
108 const char *end = strchr (p1, '\n');
109 if (!end)
110 fatal_error (input_location, "malformed ptx file");
111
112 id_map *v = XNEW (id_map);
113 size_t len = end - p1;
114 v->ptx_name = XNEWVEC (char, len + 1);
115 memcpy (v->ptx_name, p1, len);
116 v->ptx_name[len] = '\0';
117 v->next = NULL;
118 id_map **tail = *where;
119 *tail = v;
120 *where = &v->next;
121 }
122
123 /* Read the whole input file. It will be NUL terminated (but
124 remember, there could be a NUL in the file itself. */
125
126 static const char *
read_file(FILE * stream,size_t * plen)127 read_file (FILE *stream, size_t *plen)
128 {
129 size_t alloc = 16384;
130 size_t base = 0;
131 char *buffer;
132
133 if (!fseek (stream, 0, SEEK_END))
134 {
135 /* Get the file size. */
136 long s = ftell (stream);
137 if (s >= 0)
138 alloc = s + 100;
139 fseek (stream, 0, SEEK_SET);
140 }
141 buffer = XNEWVEC (char, alloc);
142
143 for (;;)
144 {
145 size_t n = fread (buffer + base, 1, alloc - base - 1, stream);
146
147 if (!n)
148 break;
149 base += n;
150 if (base + 1 == alloc)
151 {
152 alloc *= 2;
153 buffer = XRESIZEVEC (char, buffer, alloc);
154 }
155 }
156 buffer[base] = 0;
157 *plen = base;
158 return buffer;
159 }
160
161 /* Parse STR, saving found tokens into PVALUES and return their number.
162 Tokens are assumed to be delimited by ':'. */
163 static unsigned
parse_env_var(const char * str,char *** pvalues)164 parse_env_var (const char *str, char ***pvalues)
165 {
166 const char *curval, *nextval;
167 char **values;
168 unsigned num = 1, i;
169
170 curval = strchr (str, ':');
171 while (curval)
172 {
173 num++;
174 curval = strchr (curval + 1, ':');
175 }
176
177 values = (char **) xmalloc (num * sizeof (char *));
178 curval = str;
179 nextval = strchr (curval, ':');
180 if (nextval == NULL)
181 nextval = strchr (curval, '\0');
182
183 for (i = 0; i < num; i++)
184 {
185 int l = nextval - curval;
186 values[i] = (char *) xmalloc (l + 1);
187 memcpy (values[i], curval, l);
188 values[i][l] = 0;
189 curval = nextval + 1;
190 nextval = strchr (curval, ':');
191 if (nextval == NULL)
192 nextval = strchr (curval, '\0');
193 }
194 *pvalues = values;
195 return num;
196 }
197
198 /* Auxiliary function that frees elements of PTR and PTR itself.
199 N is number of elements to be freed. If PTR is NULL, nothing is freed.
200 If an element is NULL, subsequent elements are not freed. */
201 static void
free_array_of_ptrs(void ** ptr,unsigned n)202 free_array_of_ptrs (void **ptr, unsigned n)
203 {
204 unsigned i;
205 if (!ptr)
206 return;
207 for (i = 0; i < n; i++)
208 {
209 if (!ptr[i])
210 break;
211 free (ptr[i]);
212 }
213 free (ptr);
214 return;
215 }
216
217 /* Check whether NAME can be accessed in MODE. This is like access,
218 except that it never considers directories to be executable. */
219 static int
access_check(const char * name,int mode)220 access_check (const char *name, int mode)
221 {
222 if (mode == X_OK)
223 {
224 struct stat st;
225
226 if (stat (name, &st) < 0 || S_ISDIR (st.st_mode))
227 return -1;
228 }
229
230 return access (name, mode);
231 }
232
233 static void
process(FILE * in,FILE * out)234 process (FILE *in, FILE *out)
235 {
236 size_t len = 0;
237 const char *input = read_file (in, &len);
238 const char *comma;
239 id_map const *id;
240 unsigned obj_count = 0;
241 unsigned ix;
242
243 /* Dump out char arrays for each PTX object file. These are
244 terminated by a NUL. */
245 for (size_t i = 0; i != len;)
246 {
247 char c;
248
249 fprintf (out, "static const char ptx_code_%u[] =\n\t\"", obj_count++);
250 while ((c = input[i++]))
251 {
252 switch (c)
253 {
254 case '\r':
255 continue;
256 case '\n':
257 fprintf (out, "\\n\"\n\t\"");
258 /* Look for mappings on subsequent lines. */
259 while (startswith (input + i, "//:"))
260 {
261 i += 3;
262
263 if (startswith (input + i, "VAR_MAP "))
264 record_id (input + i + 8, &vars_tail);
265 else if (startswith (input + i, "FUNC_MAP "))
266 record_id (input + i + 9, &funcs_tail);
267 else
268 abort ();
269 /* Skip to next line. */
270 while (input[i++] != '\n')
271 continue;
272 }
273 continue;
274 case '"':
275 case '\\':
276 putc ('\\', out);
277 break;
278 default:
279 break;
280 }
281 putc (c, out);
282 }
283 fprintf (out, "\";\n\n");
284 }
285
286 /* Dump out array of pointers to ptx object strings. */
287 fprintf (out, "static const struct ptx_obj {\n"
288 " const char *code;\n"
289 " __SIZE_TYPE__ size;\n"
290 "} ptx_objs[] = {");
291 for (comma = "", ix = 0; ix != obj_count; comma = ",", ix++)
292 fprintf (out, "%s\n\t{ptx_code_%u, sizeof (ptx_code_%u)}", comma, ix, ix);
293 fprintf (out, "\n};\n\n");
294
295 /* Dump out variable idents. */
296 fprintf (out, "static const char *const var_mappings[] = {");
297 for (comma = "", id = var_ids; id; comma = ",", id = id->next)
298 fprintf (out, "%s\n\t%s", comma, id->ptx_name);
299 fprintf (out, "\n};\n\n");
300
301 /* Dump out function idents. */
302 fprintf (out, "static const struct nvptx_fn {\n"
303 " const char *name;\n"
304 " unsigned short dim[%d];\n"
305 "} func_mappings[] = {\n", GOMP_DIM_MAX);
306 for (comma = "", id = func_ids; id; comma = ",", id = id->next)
307 fprintf (out, "%s\n\t{%s}", comma, id->ptx_name);
308 fprintf (out, "\n};\n\n");
309
310 fprintf (out,
311 "static const struct nvptx_tdata {\n"
312 " const struct ptx_obj *ptx_objs;\n"
313 " unsigned ptx_num;\n"
314 " const char *const *var_names;\n"
315 " unsigned var_num;\n"
316 " const struct nvptx_fn *fn_names;\n"
317 " unsigned fn_num;\n"
318 "} target_data = {\n"
319 " ptx_objs, sizeof (ptx_objs) / sizeof (ptx_objs[0]),\n"
320 " var_mappings,"
321 " sizeof (var_mappings) / sizeof (var_mappings[0]),\n"
322 " func_mappings,"
323 " sizeof (func_mappings) / sizeof (func_mappings[0])\n"
324 "};\n\n");
325
326 fprintf (out, "#ifdef __cplusplus\n"
327 "extern \"C\" {\n"
328 "#endif\n");
329
330 fprintf (out, "extern void GOMP_offload_register_ver"
331 " (unsigned, const void *, int, const void *);\n");
332 fprintf (out, "extern void GOMP_offload_unregister_ver"
333 " (unsigned, const void *, int, const void *);\n");
334
335 fprintf (out, "#ifdef __cplusplus\n"
336 "}\n"
337 "#endif\n");
338
339 fprintf (out, "extern const void *const __OFFLOAD_TABLE__[];\n\n");
340
341 fprintf (out, "static __attribute__((constructor)) void init (void)\n"
342 "{\n"
343 " GOMP_offload_register_ver (%#x, __OFFLOAD_TABLE__,"
344 " %d/*NVIDIA_PTX*/, &target_data);\n"
345 "};\n",
346 GOMP_VERSION_PACK (GOMP_VERSION, GOMP_VERSION_NVIDIA_PTX),
347 GOMP_DEVICE_NVIDIA_PTX);
348
349 fprintf (out, "static __attribute__((destructor)) void fini (void)\n"
350 "{\n"
351 " GOMP_offload_unregister_ver (%#x, __OFFLOAD_TABLE__,"
352 " %d/*NVIDIA_PTX*/, &target_data);\n"
353 "};\n",
354 GOMP_VERSION_PACK (GOMP_VERSION, GOMP_VERSION_NVIDIA_PTX),
355 GOMP_DEVICE_NVIDIA_PTX);
356 }
357
358 static void
compile_native(const char * infile,const char * outfile,const char * compiler,bool fPIC,bool fpic)359 compile_native (const char *infile, const char *outfile, const char *compiler,
360 bool fPIC, bool fpic)
361 {
362 const char *collect_gcc_options = getenv ("COLLECT_GCC_OPTIONS");
363 if (!collect_gcc_options)
364 fatal_error (input_location,
365 "environment variable COLLECT_GCC_OPTIONS must be set");
366
367 struct obstack argv_obstack;
368 obstack_init (&argv_obstack);
369 obstack_ptr_grow (&argv_obstack, compiler);
370 if (fPIC)
371 obstack_ptr_grow (&argv_obstack, "-fPIC");
372 if (fpic)
373 obstack_ptr_grow (&argv_obstack, "-fpic");
374 if (save_temps)
375 obstack_ptr_grow (&argv_obstack, "-save-temps");
376 if (verbose)
377 obstack_ptr_grow (&argv_obstack, "-v");
378 obstack_ptr_grow (&argv_obstack, "-dumpdir");
379 obstack_ptr_grow (&argv_obstack, "");
380 obstack_ptr_grow (&argv_obstack, "-dumpbase");
381 obstack_ptr_grow (&argv_obstack, ptx_dumpbase);
382 obstack_ptr_grow (&argv_obstack, "-dumpbase-ext");
383 obstack_ptr_grow (&argv_obstack, ".c");
384 switch (offload_abi)
385 {
386 case OFFLOAD_ABI_LP64:
387 obstack_ptr_grow (&argv_obstack, "-m64");
388 break;
389 case OFFLOAD_ABI_ILP32:
390 obstack_ptr_grow (&argv_obstack, "-m32");
391 break;
392 default:
393 gcc_unreachable ();
394 }
395 obstack_ptr_grow (&argv_obstack, infile);
396 obstack_ptr_grow (&argv_obstack, "-c");
397 obstack_ptr_grow (&argv_obstack, "-o");
398 obstack_ptr_grow (&argv_obstack, outfile);
399 obstack_ptr_grow (&argv_obstack, NULL);
400
401 const char **new_argv = XOBFINISH (&argv_obstack, const char **);
402 fork_execute (new_argv[0], CONST_CAST (char **, new_argv), true,
403 ".gccnative_args");
404 obstack_free (&argv_obstack, NULL);
405 }
406
407 int
main(int argc,char ** argv)408 main (int argc, char **argv)
409 {
410 FILE *in = stdin;
411 FILE *out = stdout;
412 const char *outname = 0;
413
414 progname = "mkoffload";
415 diagnostic_initialize (global_dc, 0);
416
417 if (atexit (mkoffload_cleanup) != 0)
418 fatal_error (input_location, "atexit failed");
419
420 char *collect_gcc = getenv ("COLLECT_GCC");
421 if (collect_gcc == NULL)
422 fatal_error (input_location, "COLLECT_GCC must be set.");
423 const char *gcc_path = dirname (ASTRDUP (collect_gcc));
424 const char *gcc_exec = basename (ASTRDUP (collect_gcc));
425
426 size_t len = (strlen (gcc_path) + 1
427 + strlen (GCC_INSTALL_NAME)
428 + 1);
429 char *driver = XALLOCAVEC (char, len);
430
431 if (strcmp (gcc_exec, collect_gcc) == 0)
432 /* collect_gcc has no path, so it was found in PATH. Make sure we also
433 find accel-gcc in PATH. */
434 gcc_path = NULL;
435
436 int driver_used = 0;
437 if (gcc_path != NULL)
438 driver_used = sprintf (driver, "%s/", gcc_path);
439 sprintf (driver + driver_used, "%s", GCC_INSTALL_NAME);
440
441 bool found = false;
442 if (gcc_path == NULL)
443 found = true;
444 else if (access_check (driver, X_OK) == 0)
445 found = true;
446 else
447 {
448 /* Don't use alloca pointer with XRESIZEVEC. */
449 driver = NULL;
450 /* Look in all COMPILER_PATHs for GCC_INSTALL_NAME. */
451 char **paths = NULL;
452 unsigned n_paths;
453 n_paths = parse_env_var (getenv ("COMPILER_PATH"), &paths);
454 for (unsigned i = 0; i < n_paths; i++)
455 {
456 len = strlen (paths[i]) + 1 + strlen (GCC_INSTALL_NAME) + 1;
457 driver = XRESIZEVEC (char, driver, len);
458 sprintf (driver, "%s/%s", paths[i], GCC_INSTALL_NAME);
459 if (access_check (driver, X_OK) == 0)
460 {
461 found = true;
462 break;
463 }
464 }
465 free_array_of_ptrs ((void **) paths, n_paths);
466 }
467
468 if (!found)
469 fatal_error (input_location,
470 "offload compiler %s not found (consider using %<-B%>)",
471 GCC_INSTALL_NAME);
472
473 /* We may be called with all the arguments stored in some file and
474 passed with @file. Expand them into argv before processing. */
475 expandargv (&argc, &argv);
476
477 /* Scan the argument vector. */
478 bool fopenmp = false;
479 bool fopenacc = false;
480 bool fPIC = false;
481 bool fpic = false;
482 for (int i = 1; i < argc; i++)
483 {
484 #define STR "-foffload-abi="
485 if (startswith (argv[i], STR))
486 {
487 if (strcmp (argv[i] + strlen (STR), "lp64") == 0)
488 offload_abi = OFFLOAD_ABI_LP64;
489 else if (strcmp (argv[i] + strlen (STR), "ilp32") == 0)
490 offload_abi = OFFLOAD_ABI_ILP32;
491 else
492 fatal_error (input_location,
493 "unrecognizable argument of option " STR);
494 }
495 #undef STR
496 else if (strcmp (argv[i], "-fopenmp") == 0)
497 fopenmp = true;
498 else if (strcmp (argv[i], "-fopenacc") == 0)
499 fopenacc = true;
500 else if (strcmp (argv[i], "-fPIC") == 0)
501 fPIC = true;
502 else if (strcmp (argv[i], "-fpic") == 0)
503 fpic = true;
504 else if (strcmp (argv[i], "-save-temps") == 0)
505 save_temps = true;
506 else if (strcmp (argv[i], "-v") == 0)
507 verbose = true;
508 else if (strcmp (argv[i], "-dumpbase") == 0
509 && i + 1 < argc)
510 dumppfx = argv[++i];
511 }
512 if (!(fopenacc ^ fopenmp))
513 fatal_error (input_location, "either %<-fopenacc%> or %<-fopenmp%> "
514 "must be set");
515
516 struct obstack argv_obstack;
517 obstack_init (&argv_obstack);
518 obstack_ptr_grow (&argv_obstack, driver);
519 if (save_temps)
520 obstack_ptr_grow (&argv_obstack, "-save-temps");
521 if (verbose)
522 obstack_ptr_grow (&argv_obstack, "-v");
523 obstack_ptr_grow (&argv_obstack, "-xlto");
524 switch (offload_abi)
525 {
526 case OFFLOAD_ABI_LP64:
527 obstack_ptr_grow (&argv_obstack, "-m64");
528 break;
529 case OFFLOAD_ABI_ILP32:
530 obstack_ptr_grow (&argv_obstack, "-m32");
531 break;
532 default:
533 gcc_unreachable ();
534 }
535 if (fopenmp)
536 obstack_ptr_grow (&argv_obstack, "-mgomp");
537
538 for (int ix = 1; ix != argc; ix++)
539 {
540 if (!strcmp (argv[ix], "-o") && ix + 1 != argc)
541 outname = argv[++ix];
542 else
543 obstack_ptr_grow (&argv_obstack, argv[ix]);
544 }
545
546 if (!dumppfx)
547 dumppfx = outname;
548
549 ptx_dumpbase = concat (dumppfx, ".c", NULL);
550 if (save_temps)
551 ptx_cfile_name = ptx_dumpbase;
552 else
553 ptx_cfile_name = make_temp_file (".c");
554
555 out = fopen (ptx_cfile_name, "w");
556 if (!out)
557 fatal_error (input_location, "cannot open '%s'", ptx_cfile_name);
558
559 /* PR libgomp/65099: Currently, we only support offloading in 64-bit
560 configurations. */
561 if (offload_abi == OFFLOAD_ABI_LP64)
562 {
563 char *mko_dumpbase = concat (dumppfx, ".mkoffload", NULL);
564 if (save_temps)
565 ptx_name = mko_dumpbase;
566 else
567 ptx_name = make_temp_file (".mkoffload");
568 obstack_ptr_grow (&argv_obstack, "-dumpdir");
569 obstack_ptr_grow (&argv_obstack, "");
570 obstack_ptr_grow (&argv_obstack, "-dumpbase");
571 obstack_ptr_grow (&argv_obstack, mko_dumpbase);
572 obstack_ptr_grow (&argv_obstack, "-dumpbase-ext");
573 obstack_ptr_grow (&argv_obstack, "");
574 obstack_ptr_grow (&argv_obstack, "-o");
575 obstack_ptr_grow (&argv_obstack, ptx_name);
576 obstack_ptr_grow (&argv_obstack, NULL);
577 const char **new_argv = XOBFINISH (&argv_obstack, const char **);
578
579 char *execpath = getenv ("GCC_EXEC_PREFIX");
580 char *cpath = getenv ("COMPILER_PATH");
581 char *lpath = getenv ("LIBRARY_PATH");
582 unsetenv ("GCC_EXEC_PREFIX");
583 unsetenv ("COMPILER_PATH");
584 unsetenv ("LIBRARY_PATH");
585
586 fork_execute (new_argv[0], CONST_CAST (char **, new_argv), true,
587 ".gcc_args");
588 obstack_free (&argv_obstack, NULL);
589
590 xputenv (concat ("GCC_EXEC_PREFIX=", execpath, NULL));
591 xputenv (concat ("COMPILER_PATH=", cpath, NULL));
592 xputenv (concat ("LIBRARY_PATH=", lpath, NULL));
593
594 in = fopen (ptx_name, "r");
595 if (!in)
596 fatal_error (input_location, "cannot open intermediate ptx file");
597
598 process (in, out);
599 fclose (in);
600 }
601
602 fclose (out);
603
604 compile_native (ptx_cfile_name, outname, collect_gcc, fPIC, fpic);
605
606 return 0;
607 }
608