Debian Codegraph @

gcc/config/gcn/mkoffload.cc

raw
1 /* Offload image generation tool for AMD GCN.
2
3 Copyright (C) 2014-2025 Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published
9 by the Free Software Foundation; either version 3, or (at your
10 option) any later version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
15 License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21 /* Munges GCN assembly into a C source file defining the GCN code as a
22 string.
23
24 This is not a complete assembler. We presume the source is well
25 formed from the compiler and can die horribly if it is not. */
26
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "obstack.h"
31 #include "diagnostic.h"
32 #include "intl.h"
33 #include <libgen.h>
34 #include "collect-utils.h"
35 #include "gomp-constants.h"
36 #include "simple-object.h"
37 #include "elf.h"
38 #include "configargs.h" /* For configure_default_options. */
39 #include "multilib.h" /* For multilib_options. */
40
41 /* These probably won't (all) be in elf.h for a while. */
42 #undef EM_AMDGPU
43 #define EM_AMDGPU 0xe0;
44
45 #undef ELFOSABI_AMDGPU_HSA
46 #define ELFOSABI_AMDGPU_HSA 64
47 #undef ELFABIVERSION_AMDGPU_HSA_V3
48 #define ELFABIVERSION_AMDGPU_HSA_V3 1
49 #undef ELFABIVERSION_AMDGPU_HSA_V4
50 #define ELFABIVERSION_AMDGPU_HSA_V4 2
51 #undef ELFABIVERSION_AMDGPU_HSA_V6
52 #define ELFABIVERSION_AMDGPU_HSA_V6 4
53
54 /* Extract the EF_AMDGPU_MACH_AMDGCN_GFXnnn from the def file. */
55 enum elf_arch_code {
56 EF_AMDGPU_MACH_AMDGCN_NONE = -1, /* For generic handling. */
57 #define GCN_DEVICE(name, NAME, ELF_ARCH, ...) \
58 EF_AMDGPU_MACH_AMDGCN_ ## NAME = ELF_ARCH,
59 #include "gcn-devices.def"
60 #undef GCN_DEVICE
61 };
62
63 #define EF_AMDGPU_FEATURE_XNACK_V4 0x300 /* Mask. */
64 #define EF_AMDGPU_FEATURE_XNACK_UNSUPPORTED_V4 0x000
65 #define EF_AMDGPU_FEATURE_XNACK_ANY_V4 0x100
66 #define EF_AMDGPU_FEATURE_XNACK_OFF_V4 0x200
67 #define EF_AMDGPU_FEATURE_XNACK_ON_V4 0x300
68
69 #define EF_AMDGPU_FEATURE_SRAMECC_V4 0xc00 /* Mask. */
70 #define EF_AMDGPU_FEATURE_SRAMECC_UNSUPPORTED_V4 0x000
71 #define EF_AMDGPU_FEATURE_SRAMECC_ANY_V4 0x400
72 #define EF_AMDGPU_FEATURE_SRAMECC_OFF_V4 0x800
73 #define EF_AMDGPU_FEATURE_SRAMECC_ON_V4 0xc00
74
75 #define EF_AMDGPU_GENERIC_VERSION_V 0xff000000 /* Mask. */
76 #define EF_AMDGPU_GENERIC_VERSION_OFFSET 24
77
78 #define SET_XNACK_ON(VAR) VAR = ((VAR & ~EF_AMDGPU_FEATURE_XNACK_V4) \
79 | EF_AMDGPU_FEATURE_XNACK_ON_V4)
80 #define SET_XNACK_ANY(VAR) VAR = ((VAR & ~EF_AMDGPU_FEATURE_XNACK_V4) \
81 | EF_AMDGPU_FEATURE_XNACK_ANY_V4)
82 #define SET_XNACK_OFF(VAR) VAR = ((VAR & ~EF_AMDGPU_FEATURE_XNACK_V4) \
83 | EF_AMDGPU_FEATURE_XNACK_OFF_V4)
84 #define SET_XNACK_UNSET(VAR) VAR = ((VAR & ~EF_AMDGPU_FEATURE_XNACK_V4) \
85 | EF_AMDGPU_FEATURE_SRAMECC_UNSUPPORTED_V4)
86 #define TEST_XNACK_ANY(VAR) ((VAR & EF_AMDGPU_FEATURE_XNACK_V4) \
87 == EF_AMDGPU_FEATURE_XNACK_ANY_V4)
88 #define TEST_XNACK_ON(VAR) ((VAR & EF_AMDGPU_FEATURE_XNACK_V4) \
89 == EF_AMDGPU_FEATURE_XNACK_ON_V4)
90 #define TEST_XNACK_OFF(VAR) ((VAR & EF_AMDGPU_FEATURE_XNACK_V4) \
91 == EF_AMDGPU_FEATURE_XNACK_OFF_V4)
92 #define TEST_XNACK_UNSET(VAR) ((VAR & EF_AMDGPU_FEATURE_XNACK_V4) == 0)
93
94 #define SET_SRAM_ECC_ON(VAR) VAR = ((VAR & ~EF_AMDGPU_FEATURE_SRAMECC_V4) \
95 | EF_AMDGPU_FEATURE_SRAMECC_ON_V4)
96 #define SET_SRAM_ECC_ANY(VAR) VAR = ((VAR & ~EF_AMDGPU_FEATURE_SRAMECC_V4) \
97 | EF_AMDGPU_FEATURE_SRAMECC_ANY_V4)
98 #define SET_SRAM_ECC_OFF(VAR) VAR = ((VAR & ~EF_AMDGPU_FEATURE_SRAMECC_V4) \
99 | EF_AMDGPU_FEATURE_SRAMECC_OFF_V4)
100 #define SET_SRAM_ECC_UNSET(VAR) \
101 VAR = ((VAR & ~EF_AMDGPU_FEATURE_SRAMECC_V4) \
102 | EF_AMDGPU_FEATURE_SRAMECC_UNSUPPORTED_V4)
103 #define TEST_SRAM_ECC_ANY(VAR) ((VAR & EF_AMDGPU_FEATURE_SRAMECC_V4) \
104 == EF_AMDGPU_FEATURE_SRAMECC_ANY_V4)
105 #define TEST_SRAM_ECC_ON(VAR) ((VAR & EF_AMDGPU_FEATURE_SRAMECC_V4) \
106 == EF_AMDGPU_FEATURE_SRAMECC_ON_V4)
107 #define TEST_SRAM_ECC_UNSET(VAR) ((VAR & EF_AMDGPU_FEATURE_SRAMECC_V4) == 0)
108
109 #define GET_GENERIC_VERSION(VAR) ((VAR & EF_AMDGPU_GENERIC_VERSION_V) \
110 >> EF_AMDGPU_GENERIC_VERSION_OFFSET)
111 #define SET_GENERIC_VERSION(VAR,GEN_VER) \
112 VAR = ((VAR & ~EF_AMDGPU_GENERIC_VERSION_V) \
113 | (GEN_VER << EF_AMDGPU_GENERIC_VERSION_OFFSET))
114
115 #ifndef R_AMDGPU_NONE
116 #define R_AMDGPU_NONE 0
117 #define R_AMDGPU_ABS32_LO 1 /* (S + A) & 0xFFFFFFFF */
118 #define R_AMDGPU_ABS32_HI 2 /* (S + A) >> 32 */
119 #define R_AMDGPU_ABS64 3 /* S + A */
120 #define R_AMDGPU_REL32 4 /* S + A - P */
121 #define R_AMDGPU_REL64 5 /* S + A - P */
122 #define R_AMDGPU_ABS32 6 /* S + A */
123 #define R_AMDGPU_GOTPCREL 7 /* G + GOT + A - P */
124 #define R_AMDGPU_GOTPCREL32_LO 8 /* (G + GOT + A - P) & 0xFFFFFFFF */
125 #define R_AMDGPU_GOTPCREL32_HI 9 /* (G + GOT + A - P) >> 32 */
126 #define R_AMDGPU_REL32_LO 10 /* (S + A - P) & 0xFFFFFFFF */
127 #define R_AMDGPU_REL32_HI 11 /* (S + A - P) >> 32 */
128 #define R_AMDGPU_RELATIVE64 13 /* B + A */
129 #endif
130
131 const char tool_name[] = "gcn mkoffload";
132
133 static const char *gcn_dumpbase;
134 static struct obstack files_to_cleanup;
135
136 enum offload_abi offload_abi = OFFLOAD_ABI_UNSET;
137 const char *offload_abi_host_opts = NULL;
138
139 enum elf_arch_code elf_arch = EF_AMDGPU_MACH_AMDGCN_GFX900; // Default GPU architecture.
140 uint32_t elf_flags = EF_AMDGPU_FEATURE_SRAMECC_UNSUPPORTED_V4;
141 static int gcn_stack_size = 0; /* Zero means use default. */
142
143 /* Delete tempfiles. */
144
145 void
146 tool_cleanup (bool from_signal ATTRIBUTE_UNUSED)
147 {
148 obstack_ptr_grow (&files_to_cleanup, NULL);
149 const char **files = XOBFINISH (&files_to_cleanup, const char **);
150 for (int i = 0; files[i]; i++)
151 maybe_unlink (files[i]);
152 }
153
154 static void
155 mkoffload_cleanup (void)
156 {
157 tool_cleanup (false);
158 }
159
160 /* Unlink FILE unless requested otherwise. */
161
162 void
163 maybe_unlink (const char *file)
164 {
165 if (!save_temps)
166 {
167 if (unlink_if_ordinary (file) && errno != ENOENT)
168 fatal_error (input_location, "deleting file %qs: %m", file);
169 }
170 else if (verbose)
171 fprintf (stderr, "[Leaving %s]\n", file);
172 }
173
174 /* Add or change the value of an environment variable, outputting the
175 change to standard error if in verbose mode. */
176
177 static void
178 xputenv (const char *string)
179 {
180 if (verbose)
181 fprintf (stderr, "%s\n", string);
182 putenv (CONST_CAST (char *, string));
183 }
184
185 /* Parse STR, saving found tokens into PVALUES and return their number.
186 Tokens are assumed to be delimited by ':'. */
187
188 static unsigned
189 parse_env_var (const char *str, char ***pvalues)
190 {
191 const char *curval, *nextval;
192 char **values;
193 unsigned num = 1, i;
194
195 curval = strchr (str, ':');
196 while (curval)
197 {
198 num++;
199 curval = strchr (curval + 1, ':');
200 }
201
202 values = (char **) xmalloc (num * sizeof (char *));
203 curval = str;
204 nextval = strchr (curval, ':');
205 if (nextval == NULL)
206 nextval = strchr (curval, '\0');
207
208 for (i = 0; i < num; i++)
209 {
210 int l = nextval - curval;
211 values[i] = (char *) xmalloc (l + 1);
212 memcpy (values[i], curval, l);
213 values[i][l] = 0;
214 curval = nextval + 1;
215 nextval = strchr (curval, ':');
216 if (nextval == NULL)
217 nextval = strchr (curval, '\0');
218 }
219 *pvalues = values;
220 return num;
221 }
222
223 /* Auxiliary function that frees elements of PTR and PTR itself.
224 N is number of elements to be freed. If PTR is NULL, nothing is freed.
225 If an element is NULL, subsequent elements are not freed. */
226
227 static void
228 free_array_of_ptrs (void **ptr, unsigned n)
229 {
230 unsigned i;
231 if (!ptr)
232 return;
233 for (i = 0; i < n; i++)
234 {
235 if (!ptr[i])
236 break;
237 free (ptr[i]);
238 }
239 free (ptr);
240 return;
241 }
242
243 /* Check whether NAME can be accessed in MODE. This is like access,
244 except that it never considers directories to be executable. */
245
246 static int
247 access_check (const char *name, int mode)
248 {
249 if (mode == X_OK)
250 {
251 struct stat st;
252
253 if (stat (name, &st) < 0 || S_ISDIR (st.st_mode))
254 return -1;
255 }
256
257 return access (name, mode);
258 }
259
260 /* Copy the early-debug-info from the incoming LTO object to a new object
261 that will be linked into the output HSACO file. The host relocations
262 must be translated into GCN relocations, and any global undefined symbols
263 must be weakened (so as not to have the debug info try to pull in host
264 junk).
265
266 Returns true if the file was created, false otherwise. */
267
268 static bool
269 copy_early_debug_info (const char *infile, const char *outfile)
270 {
271 const char *errmsg;
272 int err;
273
274 /* The simple_object code can handle extracting the debug sections.
275 This code is based on that in lto-wrapper.cc. */
276 int infd = open (infile, O_RDONLY | O_BINARY);
277 if (infd == -1)
278 return false;
279 simple_object_read *inobj = simple_object_start_read (infd, 0,
280 "__GNU_LTO",
281 &errmsg, &err);
282 if (!inobj)
283 return false;
284
285 off_t off, len;
286 if (simple_object_find_section (inobj, ".gnu.debuglto_.debug_info",
287 &off, &len, &errmsg, &err) != 1)
288 {
289 simple_object_release_read (inobj);
290 close (infd);
291 return false;
292 }
293
294 errmsg = simple_object_copy_lto_debug_sections (inobj, outfile, &err, true);
295 if (errmsg)
296 return false;
297
298 simple_object_release_read (inobj);
299 close (infd);
300
301 /* Open the file we just created for some adjustments.
302 The simple_object code can't do this, so we do it manually. */
303 FILE *outfd = fopen (outfile, "r+b");
304 if (!outfd)
305 return false;
306
307 Elf64_Ehdr ehdr;
308 if (fread (&ehdr, sizeof (ehdr), 1, outfd) != 1)
309 {
310 fclose (outfd);
311 return true;
312 }
313
314 /* We only support host relocations of x86_64, for now. */
315 gcc_assert (ehdr.e_machine == EM_X86_64);
316
317 /* Patch the correct elf architecture flag into the file. */
318 ehdr.e_ident[7] = ELFOSABI_AMDGPU_HSA;
319 ehdr.e_ident[8] = (GET_GENERIC_VERSION (elf_flags)
320 ? ELFABIVERSION_AMDGPU_HSA_V6
321 : ELFABIVERSION_AMDGPU_HSA_V4);
322 ehdr.e_type = ET_REL;
323 ehdr.e_machine = EM_AMDGPU;
324 ehdr.e_flags = elf_arch | elf_flags;
325
326 /* Load the section headers so we can walk them later. */
327 Elf64_Shdr *sections = (Elf64_Shdr *)xmalloc (sizeof (Elf64_Shdr)
328 * ehdr.e_shnum);
329 if (fseek (outfd, ehdr.e_shoff, SEEK_SET) == -1
330 || fread (sections, sizeof (Elf64_Shdr), ehdr.e_shnum,
331 outfd) != ehdr.e_shnum)
332 {
333 free (sections);
334 fclose (outfd);
335 return true;
336 }
337
338 /* Convert the host relocations to target relocations. */
339 for (int i = 0; i < ehdr.e_shnum; i++)
340 {
341 if (sections[i].sh_type != SHT_RELA)
342 continue;
343
344 char *data = (char *)xmalloc (sections[i].sh_size);
345 if (fseek (outfd, sections[i].sh_offset, SEEK_SET) == -1
346 || fread (data, sections[i].sh_size, 1, outfd) != 1)
347 {
348 free (data);
349 continue;
350 }
351
352 for (size_t offset = 0;
353 offset < sections[i].sh_size;
354 offset += sections[i].sh_entsize)
355 {
356 Elf64_Rela *reloc = (Elf64_Rela *) (data + offset);
357
358 /* Map the host relocations to GCN relocations.
359 Only relocations that can appear in DWARF need be handled. */
360 switch (ELF64_R_TYPE (reloc->r_info))
361 {
362 case R_X86_64_32:
363 case R_X86_64_32S:
364 reloc->r_info = ELF32_R_INFO(ELF32_R_SYM(reloc->r_info),
365 R_AMDGPU_ABS32);
366 break;
367 case R_X86_64_PC32:
368 reloc->r_info = ELF32_R_INFO(ELF32_R_SYM(reloc->r_info),
369 R_AMDGPU_REL32);
370 break;
371 case R_X86_64_PC64:
372 reloc->r_info = ELF32_R_INFO(ELF32_R_SYM(reloc->r_info),
373 R_AMDGPU_REL64);
374 break;
375 case R_X86_64_64:
376 reloc->r_info = ELF32_R_INFO(ELF32_R_SYM(reloc->r_info),
377 R_AMDGPU_ABS64);
378 break;
379 case R_X86_64_RELATIVE:
380 reloc->r_info = ELF32_R_INFO(ELF32_R_SYM(reloc->r_info),
381 R_AMDGPU_RELATIVE64);
382 break;
383 default:
384 gcc_unreachable ();
385 }
386 }
387
388 /* Write back our relocation changes. */
389 if (fseek (outfd, sections[i].sh_offset, SEEK_SET) != -1)
390 fwrite (data, sections[i].sh_size, 1, outfd);
391
392 free (data);
393 }
394
395 /* Weaken any global undefined symbols that would pull in unwanted
396 objects. */
397 for (int i = 0; i < ehdr.e_shnum; i++)
398 {
399 if (sections[i].sh_type != SHT_SYMTAB)
400 continue;
401
402 char *data = (char *)xmalloc (sections[i].sh_size);
403 if (fseek (outfd, sections[i].sh_offset, SEEK_SET) == -1
404 || fread (data, sections[i].sh_size, 1, outfd) != 1)
405 {
406 free (data);
407 continue;
408 }
409
410 for (size_t offset = 0;
411 offset < sections[i].sh_size;
412 offset += sections[i].sh_entsize)
413 {
414 Elf64_Sym *sym = (Elf64_Sym *) (data + offset);
415 int type = ELF64_ST_TYPE (sym->st_info);
416 int bind = ELF64_ST_BIND (sym->st_info);
417
418 if (bind == STB_GLOBAL && sym->st_shndx == 0)
419 sym->st_info = ELF64_ST_INFO (STB_WEAK, type);
420 }
421
422 /* Write back our symbol changes. */
423 if (fseek (outfd, sections[i].sh_offset, SEEK_SET) != -1)
424 fwrite (data, sections[i].sh_size, 1, outfd);
425
426 free (data);
427 }
428 free (sections);
429
430 /* Write back our header changes. */
431 rewind (outfd);
432 fwrite (&ehdr, sizeof (ehdr), 1, outfd);
433
434 fclose (outfd);
435 return true;
436 }
437
438 /* Parse an input assembler file, extract the offload tables etc.,
439 and output (1) the assembler code, minus the tables (which can contain
440 problematic relocations), and (2) a C file with the offload tables
441 encoded as structured data. */
442
443 static void
444 process_asm (FILE *in, FILE *out, FILE *cfile)
445 {
446 int fn_count = 0, var_count = 0, ind_fn_count = 0;
447 int dims_count = 0, regcount_count = 0;
448 struct obstack fns_os, dims_os, regcounts_os;
449 obstack_init (&fns_os);
450 obstack_init (&dims_os);
451 obstack_init (&regcounts_os);
452
453 struct oaccdims
454 {
455 int d[3];
456 char *name;
457 } dim;
458
459 struct regcount
460 {
461 int sgpr_count;
462 int vgpr_count;
463 char *kernel_name;
464 } regcount = { -1, -1, NULL };
465
466 /* Always add _init_array and _fini_array as kernels. */
467 obstack_ptr_grow (&fns_os, xstrdup ("_init_array"));
468 obstack_ptr_grow (&fns_os, xstrdup ("_fini_array"));
469 fn_count += 2;
470
471 char buf[1000];
472 enum
473 { IN_CODE,
474 IN_METADATA,
475 IN_VARS,
476 IN_FUNCS,
477 IN_IND_FUNCS,
478 } state = IN_CODE;
479 while (fgets (buf, sizeof (buf), in))
480 {
481 switch (state)
482 {
483 case IN_CODE:
484 {
485 if (sscanf (buf, " ;; OPENACC-DIMS: %d, %d, %d : %ms\n",
486 &dim.d[0], &dim.d[1], &dim.d[2], &dim.name) == 4)
487 {
488 obstack_grow (&dims_os, &dim, sizeof (dim));
489 dims_count++;
490 }
491
492 break;
493 }
494 case IN_METADATA:
495 {
496 if (sscanf (buf, " - .name: %ms\n", &regcount.kernel_name) == 1)
497 break;
498 else if (sscanf (buf, " .sgpr_count: %d\n",
499 &regcount.sgpr_count) == 1)
500 {
501 gcc_assert (regcount.kernel_name);
502 break;
503 }
504 else if (sscanf (buf, " .vgpr_count: %d\n",
505 &regcount.vgpr_count) == 1)
506 {
507 gcc_assert (regcount.kernel_name);
508 break;
509 }
510
511 break;
512 }
513 case IN_VARS:
514 {
515 char *varname;
516 unsigned varsize;
517 if (sscanf (buf, " .8byte %ms\n", &varname))
518 {
519 fputs (buf, out);
520 fgets (buf, sizeof (buf), in);
521 if (!sscanf (buf, " .8byte %u\n", &varsize))
522 abort ();
523 var_count++;
524 }
525 break;
526 }
527 case IN_FUNCS:
528 {
529 char *funcname;
530 if (sscanf (buf, "\t.8byte\t%ms\n", &funcname))
531 {
532 fputs (buf, out);
533 obstack_ptr_grow (&fns_os, funcname);
534 fn_count++;
535 continue;
536 }
537 break;
538 }
539 case IN_IND_FUNCS:
540 {
541 char *funcname;
542 if (sscanf (buf, "\t.8byte\t%ms\n", &funcname))
543 {
544 fputs (buf, out);
545 ind_fn_count++;
546 continue;
547 }
548 break;
549 }
550 }
551
552 char dummy;
553 if (sscanf (buf, " .section .gnu.offload_vars%c", &dummy) > 0)
554 {
555 state = IN_VARS;
556
557 /* Add a global symbol to allow plugin-gcn.c to locate the table
558 at runtime. It can't use the "offload_var_table.N" emitted by
559 the compiler because a) they're not global, and b) there's one
560 for each input file combined into the binary. */
561 fputs (buf, out);
562 fputs ("\t.global .offload_var_table\n"
563 "\t.type .offload_var_table, @object\n"
564 ".offload_var_table:\n",
565 out);
566 }
567 else if (sscanf (buf, " .section .gnu.offload_funcs%c", &dummy) > 0)
568 {
569 state = IN_FUNCS;
570 /* Likewise for .gnu.offload_vars; used for reverse offload. */
571 fputs (buf, out);
572 fputs ("\t.global .offload_func_table\n"
573 "\t.type .offload_func_table, @object\n"
574 ".offload_func_table:\n",
575 out);
576 }
577 else if (sscanf (buf, " .section .gnu.offload_ind_funcs%c", &dummy) > 0)
578 {
579 state = IN_IND_FUNCS;
580 fputs (buf, out);
581 fputs ("\t.global .offload_ind_func_table\n"
582 "\t.type .offload_ind_func_table, @object\n"
583 ".offload_ind_func_table:\n",
584 out);
585 }
586 else if (sscanf (buf, " .amdgpu_metadata%c", &dummy) > 0)
587 {
588 state = IN_METADATA;
589 regcount.kernel_name = NULL;
590 regcount.sgpr_count = regcount.vgpr_count = -1;
591 }
592 else if (sscanf (buf, " .section %c", &dummy) > 0
593 || sscanf (buf, " .text%c", &dummy) > 0
594 || sscanf (buf, " .bss%c", &dummy) > 0
595 || sscanf (buf, " .data%c", &dummy) > 0
596 || sscanf (buf, " .ident %c", &dummy) > 0)
597 state = IN_CODE;
598 else if (sscanf (buf, " .end_amdgpu_metadata%c", &dummy) > 0)
599 {
600 state = IN_CODE;
601 gcc_assert (regcount.kernel_name != NULL
602 && regcount.sgpr_count >= 0
603 && regcount.vgpr_count >= 0);
604 obstack_grow (&regcounts_os, &regcount, sizeof (regcount));
605 regcount_count++;
606 regcount.kernel_name = NULL;
607 regcount.sgpr_count = regcount.vgpr_count = -1;
608 }
609
610 if (state == IN_CODE || state == IN_METADATA || state == IN_VARS)
611 fputs (buf, out);
612 }
613
614 char **fns = XOBFINISH (&fns_os, char **);
615 struct oaccdims *dims = XOBFINISH (&dims_os, struct oaccdims *);
616 struct regcount *regcounts = XOBFINISH (&regcounts_os, struct regcount *);
617
618 if (gcn_stack_size)
619 {
620 fprintf (cfile, "#include <stdlib.h>\n");
621 fprintf (cfile, "#include <stdbool.h>\n\n");
622 }
623
624 fprintf (cfile, "static const int gcn_num_vars = %d;\n\n", var_count);
625 fprintf (cfile, "static const int gcn_num_ind_funcs = %d;\n\n", ind_fn_count);
626
627 /* Dump out function idents. */
628 fprintf (cfile, "static const struct hsa_kernel_description {\n"
629 " const char *name;\n"
630 " int oacc_dims[3];\n"
631 " int sgpr_count;\n"
632 " int vgpr_count;\n"
633 "} gcn_kernels[] = {\n ");
634 dim.d[0] = dim.d[1] = dim.d[2] = 0;
635 const char *comma;
636 int i;
637 for (comma = "", i = 0; i < fn_count; comma = ",\n ", i++)
638 {
639 /* Find if we recorded dimensions for this function. */
640 int *d = dim.d; /* Previously zeroed. */
641 int sgpr_count = 0;
642 int vgpr_count = 0;
643 for (int j = 0; j < dims_count; j++)
644 if (strcmp (fns[i], dims[j].name) == 0)
645 {
646 d = dims[j].d;
647 break;
648 }
649 for (int j = 0; j < regcount_count; j++)
650 if (strcmp (fns[i], regcounts[j].kernel_name) == 0)
651 {
652 sgpr_count = regcounts[j].sgpr_count;
653 vgpr_count = regcounts[j].vgpr_count;
654 break;
655 }
656
657 fprintf (cfile, "%s{\"%s\", {%d, %d, %d}, %d, %d}", comma,
658 fns[i], d[0], d[1], d[2], sgpr_count, vgpr_count);
659
660 free (fns[i]);
661 }
662 fprintf (cfile, "\n};\n\n");
663
664 /* Set the stack size if the user configured a value. */
665 if (gcn_stack_size)
666 fprintf (cfile,
667 "static __attribute__((constructor))\n"
668 "void configure_stack_size (void)\n"
669 "{\n"
670 " const char *val = getenv (\"GCN_STACK_SIZE\");\n"
671 " if (!val || val[0] == '\\0')\n"
672 " setenv (\"GCN_STACK_SIZE\", \"%d\", true);\n"
673 "}\n\n",
674 gcn_stack_size);
675
676 obstack_free (&fns_os, NULL);
677 for (i = 0; i < dims_count; i++)
678 free (dims[i].name);
679 for (i = 0; i < regcount_count; i++)
680 free (regcounts[i].kernel_name);
681 obstack_free (&dims_os, NULL);
682 obstack_free (&regcounts_os, NULL);
683 }
684
685 /* Embed an object file into a C source file. */
686
687 static void
688 process_obj (const char *fname_in, FILE *cfile, uint32_t omp_requires)
689 {
690 /* Dump out an array containing the binary.
691 If the file is empty, a parse error is shown as the argument to is_empty
692 is an undeclared identifier. */
693 fprintf (cfile,
694 "static unsigned char gcn_code[] = {\n"
695 "#embed \"%s\" if_empty (error_file_is_empty)\n"
696 "};\n\n", fname_in);
697
698 fprintf (cfile,
699 "static const struct gcn_image {\n"
700 " __SIZE_TYPE__ size;\n"
701 " void *image;\n"
702 "} gcn_image = {\n"
703 " sizeof(gcn_code),\n"
704 " gcn_code\n"
705 "};\n\n");
706
707 fprintf (cfile,
708 "static const struct gcn_data {\n"
709 " __UINTPTR_TYPE__ omp_requires_mask;\n"
710 " const struct gcn_image *gcn_image;\n"
711 " unsigned kernel_count;\n"
712 " const struct hsa_kernel_description *kernel_infos;\n"
713 " unsigned ind_func_count;\n"
714 " unsigned global_variable_count;\n"
715 "} gcn_data = {\n"
716 " %d,\n"
717 " &gcn_image,\n"
718 " sizeof (gcn_kernels) / sizeof (gcn_kernels[0]),\n"
719 " gcn_kernels,\n"
720 " gcn_num_ind_funcs,\n"
721 " gcn_num_vars\n"
722 "};\n\n", omp_requires);
723
724 fprintf (cfile,
725 "#ifdef __cplusplus\n"
726 "extern \"C\" {\n"
727 "#endif\n"
728 "extern void GOMP_offload_register_ver"
729 " (unsigned, const void *, int, const void *);\n"
730 "extern void GOMP_offload_unregister_ver"
731 " (unsigned, const void *, int, const void *);\n"
732 "#ifdef __cplusplus\n"
733 "}\n"
734 "#endif\n\n");
735
736 fprintf (cfile, "extern const void *const __OFFLOAD_TABLE__[];\n\n");
737
738 fprintf (cfile, "static __attribute__((constructor)) void init (void)\n"
739 "{\n"
740 " GOMP_offload_register_ver (%#x, __OFFLOAD_TABLE__,"
741 " %d/*GCN*/, &gcn_data);\n"
742 "};\n",
743 GOMP_VERSION_PACK (GOMP_VERSION, GOMP_VERSION_GCN),
744 GOMP_DEVICE_GCN);
745
746 fprintf (cfile, "static __attribute__((destructor)) void fini (void)\n"
747 "{\n"
748 " GOMP_offload_unregister_ver (%#x, __OFFLOAD_TABLE__,"
749 " %d/*GCN*/, &gcn_data);\n"
750 "};\n",
751 GOMP_VERSION_PACK (GOMP_VERSION, GOMP_VERSION_GCN),
752 GOMP_DEVICE_GCN);
753 }
754
755 /* Compile a C file using the host compiler. */
756
757 static void
758 compile_native (const char *infile, const char *outfile, const char *compiler,
759 bool fPIC, bool fpic)
760 {
761 const char *collect_gcc_options = getenv ("COLLECT_GCC_OPTIONS");
762 if (!collect_gcc_options)
763 fatal_error (input_location,
764 "environment variable %<COLLECT_GCC_OPTIONS%> must be set");
765
766 struct obstack argv_obstack;
767 obstack_init (&argv_obstack);
768 obstack_ptr_grow (&argv_obstack, compiler);
769 if (fPIC)
770 obstack_ptr_grow (&argv_obstack, "-fPIC");
771 if (fpic)
772 obstack_ptr_grow (&argv_obstack, "-fpic");
773 if (save_temps)
774 obstack_ptr_grow (&argv_obstack, "-save-temps");
775 if (verbose)
776 obstack_ptr_grow (&argv_obstack, "-v");
777 obstack_ptr_grow (&argv_obstack, "-dumpdir");
778 obstack_ptr_grow (&argv_obstack, "");
779 obstack_ptr_grow (&argv_obstack, "-dumpbase");
780 obstack_ptr_grow (&argv_obstack, gcn_dumpbase);
781 obstack_ptr_grow (&argv_obstack, "-dumpbase-ext");
782 obstack_ptr_grow (&argv_obstack, ".c");
783 if (!offload_abi_host_opts)
784 fatal_error (input_location,
785 "%<-foffload-abi-host-opts%> not specified");
786 obstack_ptr_grow (&argv_obstack, offload_abi_host_opts);
787 obstack_ptr_grow (&argv_obstack, infile);
788 obstack_ptr_grow (&argv_obstack, "-c");
789 obstack_ptr_grow (&argv_obstack, "-o");
790 obstack_ptr_grow (&argv_obstack, outfile);
791 obstack_ptr_grow (&argv_obstack, NULL);
792
793 const char **new_argv = XOBFINISH (&argv_obstack, const char **);
794 fork_execute (new_argv[0], CONST_CAST (char **, new_argv), true,
795 ".gccnative_args");
796 obstack_free (&argv_obstack, NULL);
797 }
798
799 static enum elf_arch_code
800 get_arch (const char *str, const char *with_arch_str)
801 {
802 /* Use the def file to map the name to the elf_arch_code. */
803 if (!str) ;
804 #define GCN_DEVICE(name, NAME, ELF, ...) \
805 else if (strcmp (str, #name) == 0) \
806 return (enum elf_arch_code) ELF;
807 #include "gcn-devices.def"
808
809 /* else */
810 error ("unrecognized argument in option %<-march=%s%>", str);
811
812 /* The suggestions are based on the configured multilib support; the compiler
813 itself might support more. */
814 if (multilib_options[0] != '\0')
815 {
816 /* Example: "march=gfx900/march=gfx906" */
817 char *args = (char *) alloca (strlen (multilib_options));
818 const char *p = multilib_options, *q = NULL;
819 args[0] = '\0';
820 while (true)
821 {
822 p = strchr (p, '=');
823 if (!p)
824 break;
825 if (q)
826 strcat (args, ", ");
827 ++p;
828 q = strchr (p, '/');
829 if (q)
830 strncat (args, p, q-p);
831 else
832 strcat (args, p);
833 }
834 inform (UNKNOWN_LOCATION, "valid arguments to %<-march=%> are: %s", args);
835 }
836 else if (with_arch_str)
837 inform (UNKNOWN_LOCATION, "valid argument to %<-march=%> is %qs", with_arch_str);
838
839 exit (FATAL_EXIT_CODE);
840
841 return EF_AMDGPU_MACH_AMDGCN_NONE;
842 }
843
844 static const char*
845 get_arch_name (enum elf_arch_code arch_code)
846 {
847 switch (arch_code)
848 {
849 #define GCN_DEVICE(name, NAME, ELF, ...) \
850 case EF_AMDGPU_MACH_AMDGCN_ ## NAME: \
851 return #name;
852 #include "../../gcc/config/gcn/gcn-devices.def"
853 default: return NULL;
854 }
855 }
856
857 /* If an generic arch exists and for the chosen arch no (multi)lib is
858 available, print a fatal error - and suggest to compile for the generic
859 version instead. */
860
861 static void
862 check_for_missing_lib (enum elf_arch_code elf_arch,
863 enum elf_arch_code default_arch)
864 {
865 enum elf_arch_code generic_arch;
866 switch (elf_arch)
867 {
868 #define GCN_DEVICE(name, NAME, ELF, ISA, XNACK, SRAM, WAVE64, CU, \
869 MAX_ISA_VGPRS, GEN_VER, ARCH_FAM, GEN_MACH, ...) \
870 case EF_AMDGPU_MACH_AMDGCN_ ## NAME: \
871 generic_arch = EF_AMDGPU_MACH_AMDGCN_ ## GEN_MACH; break;
872 #include "../../gcc/config/gcn/gcn-devices.def"
873 default: generic_arch = EF_AMDGPU_MACH_AMDGCN_NONE;
874 }
875
876 /* If not generic or the default arch, the library version exists. */
877 if (generic_arch == EF_AMDGPU_MACH_AMDGCN_NONE || elf_arch == default_arch)
878 return;
879
880 /* Search gcn_arch in the multilib config, which might look like
881 "march=gfx900/march=gfx906". */
882 const char *p = multilib_options;
883 const char *q = NULL;
884 const char *isa_name = get_arch_name (elf_arch);
885 while ((q = strstr (p, isa_name)) != NULL)
886 {
887 if (multilib_options + strlen ("march=") <= q
888 && startswith (&q[-strlen ("march=")], "march="))
889 {
890 const char r = q[strlen (isa_name)];
891 if (r != '\0' && r != '/')
892 continue;
893 break;
894 }
895 p++;
896 }
897
898 /* Specified -march= exists in the multilib. */
899 if (q != NULL)
900 return;
901
902 /* If no lib, try to find one for the generic arch. */
903 const char *gen_name = get_arch_name (generic_arch);
904 if (generic_arch != default_arch)
905 {
906 p = multilib_options;
907 while ((q = strstr (p, gen_name)) != NULL)
908 {
909 if (multilib_options + strlen ("march=") <= q
910 && startswith (&q[-strlen ("march=")], "march="))
911 {
912 const char r = q[strlen (gen_name)];
913 if (r != '\0' && r != '/')
914 continue;
915 break;
916 }
917 p++;
918 }
919 if (q == NULL)
920 return;
921 }
922 fatal_error (UNKNOWN_LOCATION,
923 "GCC was built without library support for %<-march=%s%>; "
924 "consider compiling for the associated generic architecture "
925 "%<-march=%s%> instead", isa_name, gen_name);
926 }
927
928 int
929 main (int argc, char **argv)
930 {
931 FILE *in = stdin;
932 FILE *out = stdout;
933 FILE *cfile = stdout;
934 const char *outname = 0;
935 const char *with_arch_str = NULL;
936
937 progname = tool_name;
938 gcc_init_libintl ();
939 diagnostic_initialize (global_dc, 0);
940 diagnostic_color_init (global_dc);
941
942 for (size_t i = 0; i < ARRAY_SIZE (configure_default_options); i++)
943 if (configure_default_options[i].name != NULL
944 && strcmp (configure_default_options[i].name, "arch") == 0)
945 {
946 with_arch_str = configure_default_options[0].value;
947 elf_arch = get_arch (configure_default_options[0].value, NULL);
948 break;
949 }
950 enum elf_arch_code default_arch = elf_arch;
951
952 obstack_init (&files_to_cleanup);
953 if (atexit (mkoffload_cleanup) != 0)
954 fatal_error (input_location, "%<atexit%> failed");
955
956 char *collect_gcc = getenv ("COLLECT_GCC");
957 if (collect_gcc == NULL)
958 fatal_error (input_location, "%<COLLECT_GCC%> must be set");
959 const char *gcc_path = dirname (ASTRDUP (collect_gcc));
960 const char *gcc_exec = basename (ASTRDUP (collect_gcc));
961
962 size_t len = (strlen (gcc_path) + 1 + strlen (GCC_INSTALL_NAME) + 1);
963 char *driver = XALLOCAVEC (char, len);
964
965 if (strcmp (gcc_exec, collect_gcc) == 0)
966 /* collect_gcc has no path, so it was found in PATH. Make sure we also
967 find accel-gcc in PATH. */
968 gcc_path = NULL;
969
970 int driver_used = 0;
971 if (gcc_path != NULL)
972 driver_used = sprintf (driver, "%s/", gcc_path);
973 sprintf (driver + driver_used, "%s", GCC_INSTALL_NAME);
974
975 bool found = false;
976 if (gcc_path == NULL)
977 found = true;
978 else if (access_check (driver, X_OK) == 0)
979 found = true;
980 else
981 {
982 /* Don't use alloca pointer with XRESIZEVEC. */
983 driver = NULL;
984 /* Look in all COMPILER_PATHs for GCC_INSTALL_NAME. */
985 char **paths = NULL;
986 unsigned n_paths;
987 n_paths = parse_env_var (getenv ("COMPILER_PATH"), &paths);
988 for (unsigned i = 0; i < n_paths; i++)
989 {
990 len = strlen (paths[i]) + 1 + strlen (GCC_INSTALL_NAME) + 1;
991 driver = XRESIZEVEC (char, driver, len);
992 sprintf (driver, "%s/%s", paths[i], GCC_INSTALL_NAME);
993 if (access_check (driver, X_OK) == 0)
994 {
995 found = true;
996 break;
997 }
998 }
999 free_array_of_ptrs ((void **) paths, n_paths);
1000 }
1001
1002 if (!found)
1003 fatal_error (input_location,
1004 "offload compiler %qs not found", GCC_INSTALL_NAME);
1005
1006 /* We may be called with all the arguments stored in some file and
1007 passed with @file. Expand them into argv before processing. */
1008 expandargv (&argc, &argv);
1009
1010 /* Scan the argument vector. */
1011 bool fopenmp = false;
1012 bool fopenacc = false;
1013 bool fPIC = false;
1014 bool fpic = false;
1015 for (int i = 1; i < argc; i++)
1016 {
1017 #define STR "-foffload-abi="
1018 if (startswith (argv[i], STR))
1019 {
1020 if (strcmp (argv[i] + strlen (STR), "lp64") == 0)
1021 offload_abi = OFFLOAD_ABI_LP64;
1022 else if (strcmp (argv[i] + strlen (STR), "ilp32") == 0)
1023 offload_abi = OFFLOAD_ABI_ILP32;
1024 else
1025 fatal_error (input_location,
1026 "unrecognizable argument of option %<" STR "%>");
1027 }
1028 #undef STR
1029 else if (startswith (argv[i], "-foffload-abi-host-opts="))
1030 {
1031 if (offload_abi_host_opts)
1032 fatal_error (input_location,
1033 "%<-foffload-abi-host-opts%> specified "
1034 "multiple times");
1035 offload_abi_host_opts
1036 = argv[i] + strlen ("-foffload-abi-host-opts=");
1037 }
1038 else if (strcmp (argv[i], "-fopenmp") == 0)
1039 fopenmp = true;
1040 else if (strcmp (argv[i], "-fopenacc") == 0)
1041 fopenacc = true;
1042 else if (strcmp (argv[i], "-fPIC") == 0)
1043 fPIC = true;
1044 else if (strcmp (argv[i], "-fpic") == 0)
1045 fpic = true;
1046 else if (strcmp (argv[i], "-mxnack=on") == 0)
1047 SET_XNACK_ON (elf_flags);
1048 else if (strcmp (argv[i], "-mxnack=any") == 0)
1049 SET_XNACK_ANY (elf_flags);
1050 else if (strcmp (argv[i], "-mxnack=off") == 0)
1051 SET_XNACK_OFF (elf_flags);
1052 else if (strcmp (argv[i], "-msram-ecc=on") == 0)
1053 SET_SRAM_ECC_ON (elf_flags);
1054 else if (strcmp (argv[i], "-msram-ecc=any") == 0)
1055 SET_SRAM_ECC_ANY (elf_flags);
1056 else if (strcmp (argv[i], "-msram-ecc=off") == 0)
1057 SET_SRAM_ECC_OFF (elf_flags);
1058 else if (strcmp (argv[i], "-save-temps") == 0)
1059 save_temps = true;
1060 else if (strcmp (argv[i], "-v") == 0)
1061 verbose = true;
1062 else if (strcmp (argv[i], "-dumpbase") == 0
1063 && i + 1 < argc)
1064 dumppfx = argv[++i];
1065 else if (startswith (argv[i], "-march="))
1066 elf_arch = get_arch (argv[i] + strlen ("-march="), with_arch_str);
1067 #define STR "-mstack-size="
1068 else if (startswith (argv[i], STR))
1069 gcn_stack_size = atoi (argv[i] + strlen (STR));
1070 #undef STR
1071 /* Translate host into offloading libraries. */
1072 else if (strcmp (argv[i], "-l_GCC_gfortran") == 0
1073 || strcmp (argv[i], "-l_GCC_m") == 0
1074 || strcmp (argv[i], "-l_GCC_stdc++") == 0)
1075 {
1076 /* Elide '_GCC_'. */
1077 size_t i_dst = strlen ("-l");
1078 size_t i_src = strlen ("-l_GCC_");
1079 char c;
1080 do
1081 c = argv[i][i_dst++] = argv[i][i_src++];
1082 while (c != '\0');
1083 }
1084 }
1085
1086 check_for_missing_lib (elf_arch, default_arch);
1087
1088 if (!(fopenacc ^ fopenmp))
1089 fatal_error (input_location,
1090 "either %<-fopenacc%> or %<-fopenmp%> must be set");
1091
1092 const char *abi;
1093 switch (offload_abi)
1094 {
1095 case OFFLOAD_ABI_LP64:
1096 abi = "-m64";
1097 break;
1098 case OFFLOAD_ABI_ILP32:
1099 abi = "-m32";
1100 break;
1101 default:
1102 gcc_unreachable ();
1103 }
1104
1105 /* Set the default ELF flags for XNACK. */
1106 switch (elf_arch)
1107 {
1108 #define GCN_DEVICE(name, NAME, ELF, ISA, XNACK, SRAM, ...) \
1109 case ELF: XNACK; break;
1110 #define HSACO_ATTR_UNSUPPORTED SET_XNACK_UNSET (elf_flags)
1111 #define HSACO_ATTR_OFF SET_XNACK_OFF (elf_flags)
1112 #define HSACO_ATTR_ANY \
1113 if (TEST_XNACK_UNSET (elf_flags)) SET_XNACK_ANY (elf_flags)
1114 #include "gcn-devices.def"
1115 #undef HSACO_ATTR_UNSUPPORTED
1116 #undef HSACO_ATTR_OFF
1117 #undef HSACO_ATTR_ANY
1118 default:
1119 fatal_error (input_location, "unhandled architecture");
1120 }
1121
1122 /* Set the default ELF flags for SRAM_ECC. */
1123 switch (elf_arch)
1124 {
1125 #define GCN_DEVICE(name, NAME, ELF, ISA, XNACK, SRAM, ...) \
1126 case ELF: SRAM; break;
1127 #define HSACO_ATTR_UNSUPPORTED SET_SRAM_ECC_UNSET (elf_flags)
1128 #define HSACO_ATTR_OFF SET_SRAM_ECC_OFF (elf_flags)
1129 #define HSACO_ATTR_ANY \
1130 if (TEST_SRAM_ECC_UNSET (elf_flags)) SET_SRAM_ECC_ANY (elf_flags)
1131 #include "gcn-devices.def"
1132 #undef HSACO_ATTR_UNSUPPORTED
1133 #undef HSACO_ATTR_OFF
1134 #undef HSACO_ATTR_ANY
1135 default:
1136 fatal_error (input_location, "unhandled architecture");
1137 }
1138
1139 /* Set the generic version. */
1140 switch (elf_arch)
1141 {
1142 #define GCN_DEVICE(name, NAME, ELF, ISA, XNACK, SRAMECC, WAVE64, CU, VGPRS, GEN_VER, ...) \
1143 case ELF: if (GEN_VER) SET_GENERIC_VERSION (elf_flags, GEN_VER); break;
1144 #include "gcn-devices.def"
1145 #undef GCN_DEVICE
1146 case EF_AMDGPU_MACH_AMDGCN_NONE: gcc_unreachable ();
1147 }
1148
1149 /* Build arguments for compiler pass. */
1150 struct obstack cc_argv_obstack;
1151 obstack_init (&cc_argv_obstack);
1152 obstack_ptr_grow (&cc_argv_obstack, driver);
1153 obstack_ptr_grow (&cc_argv_obstack, "-S");
1154
1155 if (save_temps)
1156 obstack_ptr_grow (&cc_argv_obstack, "-save-temps");
1157 if (verbose)
1158 obstack_ptr_grow (&cc_argv_obstack, "-v");
1159 obstack_ptr_grow (&cc_argv_obstack, abi);
1160 obstack_ptr_grow (&cc_argv_obstack, "-xlto");
1161 if (fopenmp)
1162 obstack_ptr_grow (&cc_argv_obstack, "-mgomp");
1163 /* The host code may contain exception handling constructs.
1164 Handle these as good as we can. */
1165 obstack_ptr_grow (&cc_argv_obstack, "-mfake-exceptions");
1166
1167 for (int ix = 1; ix != argc; ix++)
1168 {
1169 if (!strcmp (argv[ix], "-o") && ix + 1 != argc)
1170 outname = argv[++ix];
1171 else
1172 obstack_ptr_grow (&cc_argv_obstack, argv[ix]);
1173 }
1174
1175 if (!dumppfx)
1176 dumppfx = outname;
1177
1178 gcn_dumpbase = concat (dumppfx, ".c", NULL);
1179
1180 const char *gcn_cfile_name;
1181 if (save_temps)
1182 gcn_cfile_name = gcn_dumpbase;
1183 else
1184 gcn_cfile_name = make_temp_file (".c");
1185 obstack_ptr_grow (&files_to_cleanup, gcn_cfile_name);
1186
1187 cfile = fopen (gcn_cfile_name, "w");
1188 if (!cfile)
1189 fatal_error (input_location, "cannot open %qs", gcn_cfile_name);
1190
1191 /* Currently, we only support offloading in 64-bit configurations. */
1192 if (offload_abi == OFFLOAD_ABI_LP64)
1193 {
1194 const char *mko_dumpbase = concat (dumppfx, ".mkoffload", NULL);
1195 const char *hsaco_dumpbase = concat (dumppfx, ".mkoffload.hsaco", NULL);
1196
1197 const char *gcn_s1_name;
1198 const char *gcn_s2_name;
1199 const char *gcn_o_name;
1200 if (save_temps)
1201 {
1202 gcn_s1_name = concat (mko_dumpbase, ".1.s", NULL);
1203 gcn_s2_name = concat (mko_dumpbase, ".2.s", NULL);
1204 gcn_o_name = hsaco_dumpbase;
1205 }
1206 else
1207 {
1208 gcn_s1_name = make_temp_file (".mkoffload.1.s");
1209 gcn_s2_name = make_temp_file (".mkoffload.2.s");
1210 gcn_o_name = make_temp_file (".mkoffload.hsaco");
1211 }
1212 obstack_ptr_grow (&files_to_cleanup, gcn_s1_name);
1213 obstack_ptr_grow (&files_to_cleanup, gcn_s2_name);
1214 obstack_ptr_grow (&files_to_cleanup, gcn_o_name);
1215
1216 obstack_ptr_grow (&cc_argv_obstack, "-dumpdir");
1217 obstack_ptr_grow (&cc_argv_obstack, "");
1218 obstack_ptr_grow (&cc_argv_obstack, "-dumpbase");
1219 obstack_ptr_grow (&cc_argv_obstack, mko_dumpbase);
1220 obstack_ptr_grow (&cc_argv_obstack, "-dumpbase-ext");
1221 obstack_ptr_grow (&cc_argv_obstack, "");
1222
1223 obstack_ptr_grow (&cc_argv_obstack, "-o");
1224 obstack_ptr_grow (&cc_argv_obstack, gcn_s1_name);
1225 obstack_ptr_grow (&cc_argv_obstack, NULL);
1226 const char **cc_argv = XOBFINISH (&cc_argv_obstack, const char **);
1227
1228 /* Build arguments for assemble/link pass. */
1229 struct obstack ld_argv_obstack;
1230 obstack_init (&ld_argv_obstack);
1231 obstack_ptr_grow (&ld_argv_obstack, driver);
1232
1233 /* Extract early-debug information from the input objects.
1234 This loop finds all the inputs that end ".o" and aren't the output. */
1235 int dbgcount = 0;
1236 for (int ix = 1; ix != argc; ix++)
1237 {
1238 if (!strcmp (argv[ix], "-o") && ix + 1 != argc)
1239 ++ix;
1240 else
1241 {
1242 if (strcmp (argv[ix] + strlen(argv[ix]) - 2, ".o") == 0)
1243 {
1244 char *dbgobj;
1245 if (save_temps)
1246 {
1247 char buf[10];
1248 sprintf (buf, "%d", dbgcount++);
1249 dbgobj = concat (dumppfx, ".mkoffload.dbg", buf, ".o", NULL);
1250 }
1251 else
1252 dbgobj = make_temp_file (".mkoffload.dbg.o");
1253
1254 /* If the copy fails then just ignore it. */
1255 if (copy_early_debug_info (argv[ix], dbgobj))
1256 {
1257 obstack_ptr_grow (&ld_argv_obstack, dbgobj);
1258 obstack_ptr_grow (&files_to_cleanup, dbgobj);
1259 }
1260 else
1261 {
1262 maybe_unlink (dbgobj);
1263 free (dbgobj);
1264 }
1265 }
1266 }
1267 }
1268 obstack_ptr_grow (&ld_argv_obstack, gcn_s2_name);
1269 obstack_ptr_grow (&ld_argv_obstack, "-lgomp");
1270 if (!TEST_XNACK_UNSET (elf_flags))
1271 obstack_ptr_grow (&ld_argv_obstack,
1272 (TEST_XNACK_ON (elf_flags) ? "-mxnack=on"
1273 : TEST_XNACK_ANY (elf_flags) ? "-mxnack=any"
1274 : "-mxnack=off"));
1275 if (!TEST_SRAM_ECC_UNSET (elf_flags))
1276 obstack_ptr_grow (&ld_argv_obstack,
1277 (TEST_SRAM_ECC_ON (elf_flags) ? "-msram-ecc=on"
1278 : TEST_SRAM_ECC_ANY (elf_flags) ? "-msram-ecc=any"
1279 : "-msram-ecc=off"));
1280 if (verbose)
1281 obstack_ptr_grow (&ld_argv_obstack, "-v");
1282
1283 if (save_temps)
1284 obstack_ptr_grow (&ld_argv_obstack, "-save-temps");
1285
1286 for (int i = 1; i < argc; i++)
1287 if (startswith (argv[i], "-l")
1288 || startswith (argv[i], "-Wl")
1289 || startswith (argv[i], "-march"))
1290 obstack_ptr_grow (&ld_argv_obstack, argv[i]);
1291
1292 obstack_ptr_grow (&cc_argv_obstack, "-dumpdir");
1293 obstack_ptr_grow (&cc_argv_obstack, "");
1294 obstack_ptr_grow (&cc_argv_obstack, "-dumpbase");
1295 obstack_ptr_grow (&cc_argv_obstack, hsaco_dumpbase);
1296 obstack_ptr_grow (&cc_argv_obstack, "-dumpbase-ext");
1297 obstack_ptr_grow (&cc_argv_obstack, "");
1298
1299 obstack_ptr_grow (&ld_argv_obstack, "-o");
1300 obstack_ptr_grow (&ld_argv_obstack, gcn_o_name);
1301 obstack_ptr_grow (&ld_argv_obstack, NULL);
1302 const char **ld_argv = XOBFINISH (&ld_argv_obstack, const char **);
1303
1304 /* Clean up unhelpful environment variables. */
1305 char *execpath = getenv ("GCC_EXEC_PREFIX");
1306 char *cpath = getenv ("COMPILER_PATH");
1307 char *lpath = getenv ("LIBRARY_PATH");
1308 unsetenv ("GCC_EXEC_PREFIX");
1309 unsetenv ("COMPILER_PATH");
1310 unsetenv ("LIBRARY_PATH");
1311
1312 char *omp_requires_file;
1313 if (save_temps)
1314 omp_requires_file = concat (dumppfx, ".mkoffload.omp_requires", NULL);
1315 else
1316 omp_requires_file = make_temp_file (".mkoffload.omp_requires");
1317 obstack_ptr_grow (&files_to_cleanup, omp_requires_file);
1318
1319 /* Run the compiler pass. */
1320 xputenv (concat ("GCC_OFFLOAD_OMP_REQUIRES_FILE=", omp_requires_file, NULL));
1321 fork_execute (cc_argv[0], CONST_CAST (char **, cc_argv), true, ".gcc_args");
1322 obstack_free (&cc_argv_obstack, NULL);
1323 unsetenv("GCC_OFFLOAD_OMP_REQUIRES_FILE");
1324
1325 in = fopen (omp_requires_file, "rb");
1326 if (!in)
1327 fatal_error (input_location, "cannot open omp_requires file %qs",
1328 omp_requires_file);
1329 uint32_t omp_requires;
1330 if (fread (&omp_requires, sizeof (omp_requires), 1, in) != 1)
1331 fatal_error (input_location, "cannot read omp_requires file %qs",
1332 omp_requires_file);
1333 fclose (in);
1334
1335 in = fopen (gcn_s1_name, "r");
1336 if (!in)
1337 fatal_error (input_location, "cannot open intermediate gcn asm file");
1338
1339 out = fopen (gcn_s2_name, "w");
1340 if (!out)
1341 fatal_error (input_location, "cannot open %qs", gcn_s2_name);
1342
1343 process_asm (in, out, cfile);
1344
1345 fclose (in);
1346 fclose (out);
1347
1348 /* Run the assemble/link pass. */
1349 fork_execute (ld_argv[0], CONST_CAST (char **, ld_argv), true, ".ld_args");
1350 obstack_free (&ld_argv_obstack, NULL);
1351
1352 process_obj (gcn_o_name, cfile, omp_requires);
1353
1354 xputenv (concat ("GCC_EXEC_PREFIX=", execpath, NULL));
1355 xputenv (concat ("COMPILER_PATH=", cpath, NULL));
1356 xputenv (concat ("LIBRARY_PATH=", lpath, NULL));
1357 }
1358
1359 fclose (cfile);
1360
1361 compile_native (gcn_cfile_name, outname, collect_gcc, fPIC, fpic);
1362
1363 return 0;
1364 }