forked from riscvarchive/riscv-gcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhsa-brig.c
2612 lines (2209 loc) · 77.7 KB
/
hsa-brig.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Producing binary form of HSA BRIG from our internal representation.
Copyright (C) 2013-2020 Free Software Foundation, Inc.
Contributed by Martin Jambor <[email protected]> and
Martin Liska <[email protected]>.
This file is part of GCC.
GCC is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
GCC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>. */
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tm.h"
#include "target.h"
#include "memmodel.h"
#include "tm_p.h"
#include "is-a.h"
#include "vec.h"
#include "hash-table.h"
#include "hash-map.h"
#include "tree.h"
#include "tree-iterator.h"
#include "stor-layout.h"
#include "output.h"
#include "basic-block.h"
#include "function.h"
#include "cfg.h"
#include "fold-const.h"
#include "stringpool.h"
#include "gimple-pretty-print.h"
#include "diagnostic-core.h"
#include "cgraph.h"
#include "dumpfile.h"
#include "print-tree.h"
#include "alloc-pool.h"
#include "symbol-summary.h"
#include "hsa-common.h"
#include "gomp-constants.h"
/* Convert VAL to little endian form, if necessary. */
static uint16_t
lendian16 (uint16_t val)
{
#if GCC_VERSION >= 4008
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
return val;
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
return __builtin_bswap16 (val);
#else /* __ORDER_PDP_ENDIAN__ */
return val;
#endif
#else
// provide a safe slower default, with shifts and masking
#ifndef WORDS_BIGENDIAN
return val;
#else
return (val >> 8) | (val << 8);
#endif
#endif
}
/* Convert VAL to little endian form, if necessary. */
static uint32_t
lendian32 (uint32_t val)
{
#if GCC_VERSION >= 4006
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
return val;
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
return __builtin_bswap32 (val);
#else /* __ORDER_PDP_ENDIAN__ */
return (val >> 16) | (val << 16);
#endif
#else
// provide a safe slower default, with shifts and masking
#ifndef WORDS_BIGENDIAN
return val;
#else
val = ((val & 0xff00ff00) >> 8) | ((val & 0xff00ff) << 8);
return (val >> 16) | (val << 16);
#endif
#endif
}
/* Convert VAL to little endian form, if necessary. */
static uint64_t
lendian64 (uint64_t val)
{
#if GCC_VERSION >= 4006
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
return val;
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
return __builtin_bswap64 (val);
#else /* __ORDER_PDP_ENDIAN__ */
return (((val & 0xffffll) << 48)
| ((val & 0xffff0000ll) << 16)
| ((val & 0xffff00000000ll) >> 16)
| ((val & 0xffff000000000000ll) >> 48));
#endif
#else
// provide a safe slower default, with shifts and masking
#ifndef WORDS_BIGENDIAN
return val;
#else
val = (((val & 0xff00ff00ff00ff00ll) >> 8)
| ((val & 0x00ff00ff00ff00ffll) << 8));
val = ((( val & 0xffff0000ffff0000ll) >> 16)
| (( val & 0x0000ffff0000ffffll) << 16));
return (val >> 32) | (val << 32);
#endif
#endif
}
#define BRIG_ELF_SECTION_NAME ".brig"
#define BRIG_LABEL_STRING "hsa_brig"
#define BRIG_SECTION_DATA_NAME "hsa_data"
#define BRIG_SECTION_CODE_NAME "hsa_code"
#define BRIG_SECTION_OPERAND_NAME "hsa_operand"
#define BRIG_CHUNK_MAX_SIZE (64 * 1024)
/* Required HSA section alignment. */
#define HSA_SECTION_ALIGNMENT 16
/* Chunks of BRIG binary data. */
struct hsa_brig_data_chunk
{
/* Size of the data already stored into a chunk. */
unsigned size;
/* Pointer to the data. */
char *data;
};
/* Structure representing a BRIG section, holding and writing its data. */
struct hsa_brig_section
{
/* Section name that will be output to the BRIG. */
const char *section_name;
/* Size in bytes of all data stored in the section. */
unsigned total_size;
/* The size of the header of the section including padding. */
unsigned header_byte_count;
/* The size of the header of the section without any padding. */
unsigned header_byte_delta;
void init (const char *name);
void release ();
void output ();
unsigned add (const void *data, unsigned len, void **output = NULL);
void round_size_up (int factor);
void *get_ptr_by_offset (unsigned int offset);
private:
void allocate_new_chunk ();
/* Buffers of binary data, each containing BRIG_CHUNK_MAX_SIZE bytes. */
vec <struct hsa_brig_data_chunk> chunks;
/* More convenient access to the last chunk from the vector above. */
struct hsa_brig_data_chunk *cur_chunk;
};
static struct hsa_brig_section brig_data, brig_code, brig_operand;
static uint32_t brig_insn_count;
static bool brig_initialized = false;
/* Mapping between emitted HSA functions and their offset in code segment. */
static hash_map<tree, BrigCodeOffset32_t> *function_offsets;
/* Hash map of emitted function declarations. */
static hash_map <tree, BrigDirectiveExecutable *> *emitted_declarations;
/* Hash table of emitted internal function declaration offsets. */
hash_table <hsa_internal_fn_hasher> *hsa_emitted_internal_decls;
/* List of sbr instructions. */
static vec <hsa_insn_sbr *> *switch_instructions;
class function_linkage_pair
{
public:
function_linkage_pair (tree decl, unsigned int off)
: function_decl (decl), offset (off) {}
/* Declaration of called function. */
tree function_decl;
/* Offset in operand section. */
unsigned int offset;
};
/* Vector of function calls where we need to resolve function offsets. */
static auto_vec <function_linkage_pair> function_call_linkage;
/* Add a new chunk, allocate data for it and initialize it. */
void
hsa_brig_section::allocate_new_chunk ()
{
struct hsa_brig_data_chunk new_chunk;
new_chunk.data = XCNEWVEC (char, BRIG_CHUNK_MAX_SIZE);
new_chunk.size = 0;
cur_chunk = chunks.safe_push (new_chunk);
}
/* Initialize the brig section. */
void
hsa_brig_section::init (const char *name)
{
section_name = name;
/* While the following computation is basically wrong, because the intent
certainly wasn't to have the first character of name and padding, which
are a part of sizeof (BrigSectionHeader), included in the first addend,
this is what the disassembler expects. */
total_size = sizeof (BrigSectionHeader) + strlen (section_name);
chunks.create (1);
allocate_new_chunk ();
header_byte_delta = total_size;
round_size_up (4);
header_byte_count = total_size;
}
/* Free all data in the section. */
void
hsa_brig_section::release ()
{
for (unsigned i = 0; i < chunks.length (); i++)
free (chunks[i].data);
chunks.release ();
cur_chunk = NULL;
}
/* Write the section to the output file to a section with the name given at
initialization. Switches the output section and does not restore it. */
void
hsa_brig_section::output ()
{
struct BrigSectionHeader section_header;
char padding[8];
section_header.byteCount = lendian64 (total_size);
section_header.headerByteCount = lendian32 (header_byte_count);
section_header.nameLength = lendian32 (strlen (section_name));
assemble_string ((const char *) §ion_header, 16);
assemble_string (section_name, (section_header.nameLength));
memset (&padding, 0, sizeof (padding));
/* This is also a consequence of the wrong header size computation described
in a comment in hsa_brig_section::init. */
assemble_string (padding, 8);
for (unsigned i = 0; i < chunks.length (); i++)
assemble_string (chunks[i].data, chunks[i].size);
}
/* Add to the stream LEN bytes of opaque binary DATA. Return the offset at
which it was stored. If OUTPUT is not NULL, store into it the pointer to
the place where DATA was actually stored. */
unsigned
hsa_brig_section::add (const void *data, unsigned len, void **output)
{
unsigned offset = total_size;
gcc_assert (len <= BRIG_CHUNK_MAX_SIZE);
if (cur_chunk->size > (BRIG_CHUNK_MAX_SIZE - len))
allocate_new_chunk ();
char *dst = cur_chunk->data + cur_chunk->size;
memcpy (dst, data, len);
if (output)
*output = dst;
cur_chunk->size += len;
total_size += len;
return offset;
}
/* Add padding to section so that its size is divisible by FACTOR. */
void
hsa_brig_section::round_size_up (int factor)
{
unsigned padding, res = total_size % factor;
if (res == 0)
return;
padding = factor - res;
total_size += padding;
if (cur_chunk->size > (BRIG_CHUNK_MAX_SIZE - padding))
{
padding -= BRIG_CHUNK_MAX_SIZE - cur_chunk->size;
cur_chunk->size = BRIG_CHUNK_MAX_SIZE;
allocate_new_chunk ();
}
cur_chunk->size += padding;
}
/* Return pointer to data by global OFFSET in the section. */
void *
hsa_brig_section::get_ptr_by_offset (unsigned int offset)
{
gcc_assert (offset < total_size);
offset -= header_byte_delta;
unsigned i;
for (i = 0; offset >= chunks[i].size; i++)
offset -= chunks[i].size;
return chunks[i].data + offset;
}
/* BRIG string data hashing. */
struct brig_string_slot
{
const char *s;
char prefix;
int len;
uint32_t offset;
};
/* Hash table helpers. */
struct brig_string_slot_hasher : pointer_hash <brig_string_slot>
{
static inline hashval_t hash (const value_type);
static inline bool equal (const value_type, const compare_type);
static inline void remove (value_type);
};
/* Returns a hash code for DS. Adapted from libiberty's htab_hash_string
to support strings that may not end in '\0'. */
inline hashval_t
brig_string_slot_hasher::hash (const value_type ds)
{
hashval_t r = ds->len;
int i;
for (i = 0; i < ds->len; i++)
r = r * 67 + (unsigned) ds->s[i] - 113;
r = r * 67 + (unsigned) ds->prefix - 113;
return r;
}
/* Returns nonzero if DS1 and DS2 are equal. */
inline bool
brig_string_slot_hasher::equal (const value_type ds1, const compare_type ds2)
{
if (ds1->len == ds2->len)
return ds1->prefix == ds2->prefix
&& memcmp (ds1->s, ds2->s, ds1->len) == 0;
return 0;
}
/* Deallocate memory for DS upon its removal. */
inline void
brig_string_slot_hasher::remove (value_type ds)
{
free (const_cast<char *> (ds->s));
free (ds);
}
/* Hash for strings we output in order not to duplicate them needlessly. */
static hash_table<brig_string_slot_hasher> *brig_string_htab;
/* Emit a null terminated string STR to the data section and return its
offset in it. If PREFIX is non-zero, output it just before STR too.
Sanitize the string if SANITIZE option is set to true. */
static unsigned
brig_emit_string (const char *str, char prefix = 0, bool sanitize = true)
{
unsigned slen = strlen (str);
unsigned offset, len = slen + (prefix ? 1 : 0);
uint32_t hdr_len = lendian32 (len);
brig_string_slot s_slot;
brig_string_slot **slot;
char *str2;
str2 = xstrdup (str);
if (sanitize)
hsa_sanitize_name (str2);
s_slot.s = str2;
s_slot.len = slen;
s_slot.prefix = prefix;
s_slot.offset = 0;
slot = brig_string_htab->find_slot (&s_slot, INSERT);
if (*slot == NULL)
{
brig_string_slot *new_slot = XCNEW (brig_string_slot);
/* In theory we should fill in BrigData but that would mean copying
the string to a buffer for no reason, so we just emulate it. */
offset = brig_data.add (&hdr_len, sizeof (hdr_len));
if (prefix)
brig_data.add (&prefix, 1);
brig_data.add (str2, slen);
brig_data.round_size_up (4);
/* TODO: could use the string we just copied into
brig_string->cur_chunk */
new_slot->s = str2;
new_slot->len = slen;
new_slot->prefix = prefix;
new_slot->offset = offset;
*slot = new_slot;
}
else
{
offset = (*slot)->offset;
free (str2);
}
return offset;
}
/* Linked list of queued operands. */
static struct operand_queue
{
/* First from the chain of queued operands. */
hsa_op_base *first_op, *last_op;
/* The offset at which the next operand will be enqueued. */
unsigned projected_size;
} op_queue;
/* Unless already initialized, initialize infrastructure to produce BRIG. */
static void
brig_init (void)
{
brig_insn_count = 0;
if (brig_initialized)
return;
brig_string_htab = new hash_table<brig_string_slot_hasher> (37);
brig_data.init (BRIG_SECTION_DATA_NAME);
brig_code.init (BRIG_SECTION_CODE_NAME);
brig_operand.init (BRIG_SECTION_OPERAND_NAME);
brig_initialized = true;
struct BrigDirectiveModule moddir;
memset (&moddir, 0, sizeof (moddir));
moddir.base.byteCount = lendian16 (sizeof (moddir));
char *modname;
if (main_input_filename && *main_input_filename != '\0')
{
const char *part = strrchr (main_input_filename, '/');
if (!part)
part = main_input_filename;
else
part++;
modname = concat ("&__hsa_module_", part, NULL);
char *extension = strchr (modname, '.');
if (extension)
*extension = '\0';
/* As in LTO mode, we have to emit a different module names. */
if (flag_ltrans)
{
part = strrchr (asm_file_name, '/');
if (!part)
part = asm_file_name;
else
part++;
char *modname2;
modname2 = xasprintf ("%s_%s", modname, part);
free (modname);
modname = modname2;
}
hsa_sanitize_name (modname);
moddir.name = brig_emit_string (modname);
free (modname);
}
else
moddir.name = brig_emit_string ("__hsa_module_unnamed", '&');
moddir.base.kind = lendian16 (BRIG_KIND_DIRECTIVE_MODULE);
moddir.hsailMajor = lendian32 (BRIG_VERSION_HSAIL_MAJOR);
moddir.hsailMinor = lendian32 (BRIG_VERSION_HSAIL_MINOR);
moddir.profile = hsa_full_profile_p () ? BRIG_PROFILE_FULL: BRIG_PROFILE_BASE;
if (hsa_machine_large_p ())
moddir.machineModel = BRIG_MACHINE_LARGE;
else
moddir.machineModel = BRIG_MACHINE_SMALL;
moddir.defaultFloatRound = BRIG_ROUND_FLOAT_DEFAULT;
brig_code.add (&moddir, sizeof (moddir));
}
/* Free all BRIG data. */
static void
brig_release_data (void)
{
delete brig_string_htab;
brig_data.release ();
brig_code.release ();
brig_operand.release ();
brig_initialized = 0;
}
/* Enqueue operation OP. Return the offset at which it will be stored. */
static unsigned int
enqueue_op (hsa_op_base *op)
{
unsigned ret;
if (op->m_brig_op_offset)
return op->m_brig_op_offset;
ret = op_queue.projected_size;
op->m_brig_op_offset = op_queue.projected_size;
if (!op_queue.first_op)
op_queue.first_op = op;
else
op_queue.last_op->m_next = op;
op_queue.last_op = op;
if (is_a <hsa_op_immed *> (op))
op_queue.projected_size += sizeof (struct BrigOperandConstantBytes);
else if (is_a <hsa_op_reg *> (op))
op_queue.projected_size += sizeof (struct BrigOperandRegister);
else if (is_a <hsa_op_address *> (op))
op_queue.projected_size += sizeof (struct BrigOperandAddress);
else if (is_a <hsa_op_code_ref *> (op))
op_queue.projected_size += sizeof (struct BrigOperandCodeRef);
else if (is_a <hsa_op_code_list *> (op))
op_queue.projected_size += sizeof (struct BrigOperandCodeList);
else if (is_a <hsa_op_operand_list *> (op))
op_queue.projected_size += sizeof (struct BrigOperandOperandList);
else
gcc_unreachable ();
return ret;
}
static void emit_immediate_operand (hsa_op_immed *imm);
/* Emit directive describing a symbol if it has not been emitted already.
Return the offset of the directive. */
static unsigned
emit_directive_variable (class hsa_symbol *symbol)
{
struct BrigDirectiveVariable dirvar;
unsigned name_offset;
static unsigned res_name_offset;
if (symbol->m_directive_offset)
return symbol->m_directive_offset;
memset (&dirvar, 0, sizeof (dirvar));
dirvar.base.byteCount = lendian16 (sizeof (dirvar));
dirvar.base.kind = lendian16 (BRIG_KIND_DIRECTIVE_VARIABLE);
dirvar.allocation = symbol->m_allocation;
char prefix = symbol->m_global_scope_p ? '&' : '%';
if (symbol->m_decl && TREE_CODE (symbol->m_decl) == RESULT_DECL)
{
if (res_name_offset == 0)
res_name_offset = brig_emit_string (symbol->m_name, '%');
name_offset = res_name_offset;
}
else if (symbol->m_name)
name_offset = brig_emit_string (symbol->m_name, prefix);
else
{
char buf[64];
snprintf (buf, 64, "__%s_%i", hsa_seg_name (symbol->m_segment),
symbol->m_name_number);
name_offset = brig_emit_string (buf, prefix);
}
dirvar.name = lendian32 (name_offset);
if (symbol->m_decl && TREE_CODE (symbol->m_decl) == CONST_DECL)
{
hsa_op_immed *tmp = new hsa_op_immed (DECL_INITIAL (symbol->m_decl));
dirvar.init = lendian32 (enqueue_op (tmp));
}
else
dirvar.init = 0;
dirvar.type = lendian16 (symbol->m_type);
dirvar.segment = symbol->m_segment;
dirvar.align = symbol->m_align;
dirvar.linkage = symbol->m_linkage;
dirvar.dim.lo = symbol->m_dim;
dirvar.dim.hi = symbol->m_dim >> 32;
/* Global variables are just declared and linked via HSA runtime. */
if (symbol->m_linkage != BRIG_ALLOCATION_PROGRAM)
dirvar.modifier |= BRIG_VARIABLE_DEFINITION;
dirvar.reserved = 0;
if (symbol->m_cst_value)
{
dirvar.modifier |= BRIG_VARIABLE_CONST;
dirvar.init = lendian32 (enqueue_op (symbol->m_cst_value));
}
symbol->m_directive_offset = brig_code.add (&dirvar, sizeof (dirvar));
return symbol->m_directive_offset;
}
/* Emit directives describing either a function declaration or definition F and
return the produced BrigDirectiveExecutable structure. The function does
not take into account any instructions when calculating nextModuleEntry
field of the produced BrigDirectiveExecutable structure so when emitting
actual definitions, this field needs to be updated after all of the function
is actually added to the code section. */
static BrigDirectiveExecutable *
emit_function_directives (hsa_function_representation *f, bool is_declaration)
{
struct BrigDirectiveExecutable fndir;
unsigned name_offset, inarg_off, scoped_off, next_toplev_off;
int count = 0;
void *ptr_to_fndir;
hsa_symbol *sym;
if (!f->m_declaration_p)
for (int i = 0; f->m_global_symbols.iterate (i, &sym); i++)
{
gcc_assert (!sym->m_emitted_to_brig);
sym->m_emitted_to_brig = true;
emit_directive_variable (sym);
brig_insn_count++;
}
name_offset = brig_emit_string (f->m_name, '&');
inarg_off = brig_code.total_size + sizeof (fndir)
+ (f->m_output_arg ? sizeof (struct BrigDirectiveVariable) : 0);
scoped_off = inarg_off
+ f->m_input_args.length () * sizeof (struct BrigDirectiveVariable);
if (!f->m_declaration_p)
{
count += f->m_spill_symbols.length ();
count += f->m_private_variables.length ();
}
next_toplev_off = scoped_off + count * sizeof (struct BrigDirectiveVariable);
memset (&fndir, 0, sizeof (fndir));
fndir.base.byteCount = lendian16 (sizeof (fndir));
fndir.base.kind = lendian16 (f->m_kern_p ? BRIG_KIND_DIRECTIVE_KERNEL
: BRIG_KIND_DIRECTIVE_FUNCTION);
fndir.name = lendian32 (name_offset);
fndir.inArgCount = lendian16 (f->m_input_args.length ());
fndir.outArgCount = lendian16 (f->m_output_arg ? 1 : 0);
fndir.firstInArg = lendian32 (inarg_off);
fndir.firstCodeBlockEntry = lendian32 (scoped_off);
fndir.nextModuleEntry = lendian32 (next_toplev_off);
fndir.linkage = f->get_linkage ();
if (!f->m_declaration_p)
fndir.modifier |= BRIG_EXECUTABLE_DEFINITION;
memset (&fndir.reserved, 0, sizeof (fndir.reserved));
/* Once we put a definition of function_offsets, we should not overwrite
it with a declaration of the function. */
if (f->m_internal_fn == NULL)
{
if (!function_offsets->get (f->m_decl) || !is_declaration)
function_offsets->put (f->m_decl, brig_code.total_size);
}
else
{
/* Internal function. */
hsa_internal_fn **slot
= hsa_emitted_internal_decls->find_slot (f->m_internal_fn, INSERT);
hsa_internal_fn *int_fn = new hsa_internal_fn (f->m_internal_fn);
int_fn->m_offset = brig_code.total_size;
*slot = int_fn;
}
brig_code.add (&fndir, sizeof (fndir), &ptr_to_fndir);
if (f->m_output_arg)
emit_directive_variable (f->m_output_arg);
for (unsigned i = 0; i < f->m_input_args.length (); i++)
emit_directive_variable (f->m_input_args[i]);
if (!f->m_declaration_p)
{
for (int i = 0; f->m_spill_symbols.iterate (i, &sym); i++)
{
emit_directive_variable (sym);
brig_insn_count++;
}
for (unsigned i = 0; i < f->m_private_variables.length (); i++)
{
emit_directive_variable (f->m_private_variables[i]);
brig_insn_count++;
}
}
return (BrigDirectiveExecutable *) ptr_to_fndir;
}
/* Emit a label directive for the given HBB. We assume it is about to start on
the current offset in the code section. */
static void
emit_bb_label_directive (hsa_bb *hbb)
{
struct BrigDirectiveLabel lbldir;
lbldir.base.byteCount = lendian16 (sizeof (lbldir));
lbldir.base.kind = lendian16 (BRIG_KIND_DIRECTIVE_LABEL);
char buf[32];
snprintf (buf, 32, "BB_%u_%i", DECL_UID (current_function_decl),
hbb->m_index);
lbldir.name = lendian32 (brig_emit_string (buf, '@'));
hbb->m_label_ref.m_directive_offset = brig_code.add (&lbldir,
sizeof (lbldir));
brig_insn_count++;
}
/* Map a normal HSAIL type to the type of the equivalent BRIG operand
holding such, for constants and registers. */
static BrigType16_t
regtype_for_type (BrigType16_t t)
{
switch (t)
{
case BRIG_TYPE_B1:
return BRIG_TYPE_B1;
case BRIG_TYPE_U8:
case BRIG_TYPE_U16:
case BRIG_TYPE_U32:
case BRIG_TYPE_S8:
case BRIG_TYPE_S16:
case BRIG_TYPE_S32:
case BRIG_TYPE_B8:
case BRIG_TYPE_B16:
case BRIG_TYPE_B32:
case BRIG_TYPE_F16:
case BRIG_TYPE_F32:
case BRIG_TYPE_U8X4:
case BRIG_TYPE_U16X2:
case BRIG_TYPE_S8X4:
case BRIG_TYPE_S16X2:
case BRIG_TYPE_F16X2:
return BRIG_TYPE_B32;
case BRIG_TYPE_U64:
case BRIG_TYPE_S64:
case BRIG_TYPE_F64:
case BRIG_TYPE_B64:
case BRIG_TYPE_U8X8:
case BRIG_TYPE_U16X4:
case BRIG_TYPE_U32X2:
case BRIG_TYPE_S8X8:
case BRIG_TYPE_S16X4:
case BRIG_TYPE_S32X2:
case BRIG_TYPE_F16X4:
case BRIG_TYPE_F32X2:
return BRIG_TYPE_B64;
case BRIG_TYPE_B128:
case BRIG_TYPE_U8X16:
case BRIG_TYPE_U16X8:
case BRIG_TYPE_U32X4:
case BRIG_TYPE_U64X2:
case BRIG_TYPE_S8X16:
case BRIG_TYPE_S16X8:
case BRIG_TYPE_S32X4:
case BRIG_TYPE_S64X2:
case BRIG_TYPE_F16X8:
case BRIG_TYPE_F32X4:
case BRIG_TYPE_F64X2:
return BRIG_TYPE_B128;
default:
gcc_unreachable ();
}
}
/* Return the length of the BRIG type TYPE that is going to be streamed out as
an immediate constant (so it must not be B1). */
unsigned
hsa_get_imm_brig_type_len (BrigType16_t type)
{
BrigType16_t base_type = type & BRIG_TYPE_BASE_MASK;
BrigType16_t pack_type = type & BRIG_TYPE_PACK_MASK;
switch (pack_type)
{
case BRIG_TYPE_PACK_NONE:
break;
case BRIG_TYPE_PACK_32:
return 4;
case BRIG_TYPE_PACK_64:
return 8;
case BRIG_TYPE_PACK_128:
return 16;
default:
gcc_unreachable ();
}
switch (base_type)
{
case BRIG_TYPE_U8:
case BRIG_TYPE_S8:
case BRIG_TYPE_B8:
return 1;
case BRIG_TYPE_U16:
case BRIG_TYPE_S16:
case BRIG_TYPE_F16:
case BRIG_TYPE_B16:
return 2;
case BRIG_TYPE_U32:
case BRIG_TYPE_S32:
case BRIG_TYPE_F32:
case BRIG_TYPE_B32:
return 4;
case BRIG_TYPE_U64:
case BRIG_TYPE_S64:
case BRIG_TYPE_F64:
case BRIG_TYPE_B64:
return 8;
case BRIG_TYPE_B128:
return 16;
default:
gcc_unreachable ();
}
}
/* Emit one scalar VALUE to the buffer DATA intended for BRIG emission.
If NEED_LEN is not equal to zero, shrink or extend the value
to NEED_LEN bytes. Return how many bytes were written. */
static int
emit_immediate_scalar_to_buffer (tree value, char *data, unsigned need_len)
{
union hsa_bytes bytes;
memset (&bytes, 0, sizeof (bytes));
tree type = TREE_TYPE (value);
gcc_checking_assert (TREE_CODE (type) != VECTOR_TYPE);
unsigned data_len = tree_to_uhwi (TYPE_SIZE (type)) / BITS_PER_UNIT;
if (INTEGRAL_TYPE_P (type)
|| (POINTER_TYPE_P (type) && TREE_CODE (value) == INTEGER_CST))
switch (data_len)
{
case 1:
bytes.b8 = (uint8_t) TREE_INT_CST_LOW (value);
break;
case 2:
bytes.b16 = (uint16_t) TREE_INT_CST_LOW (value);
break;
case 4:
bytes.b32 = (uint32_t) TREE_INT_CST_LOW (value);
break;
case 8:
bytes.b64 = (uint64_t) TREE_INT_CST_LOW (value);
break;
default:
gcc_unreachable ();
}
else if (SCALAR_FLOAT_TYPE_P (type))
{
if (data_len == 2)
{
sorry ("Support for HSA does not implement immediate 16 bit FPU "
"operands");
return 2;
}
unsigned int_len = GET_MODE_SIZE (SCALAR_FLOAT_TYPE_MODE (type));
/* There are always 32 bits in each long, no matter the size of
the hosts long. */
long tmp[6];
real_to_target (tmp, TREE_REAL_CST_PTR (value), TYPE_MODE (type));
if (int_len == 4)
bytes.b32 = (uint32_t) tmp[0];
else
{
bytes.b64 = (uint64_t)(uint32_t) tmp[1];
bytes.b64 <<= 32;
bytes.b64 |= (uint32_t) tmp[0];
}
}
else
gcc_unreachable ();
int len;
if (need_len == 0)
len = data_len;
else
len = need_len;
memcpy (data, &bytes, len);
return len;
}
char *
hsa_op_immed::emit_to_buffer (unsigned *brig_repr_size)
{
char *brig_repr;
*brig_repr_size = hsa_get_imm_brig_type_len (m_type);
if (m_tree_value != NULL_TREE)
{
/* Update brig_repr_size for special tree values. */
if (TREE_CODE (m_tree_value) == STRING_CST)
*brig_repr_size = TREE_STRING_LENGTH (m_tree_value);
else if (TREE_CODE (m_tree_value) == CONSTRUCTOR)
*brig_repr_size
= tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (m_tree_value)));
unsigned total_len = *brig_repr_size;
/* As we can have a constructor with fewer elements, fill the memory
with zeros. */
brig_repr = XCNEWVEC (char, total_len);
char *p = brig_repr;
if (TREE_CODE (m_tree_value) == VECTOR_CST)
{
/* Variable-length vectors aren't supported. */
int i, num = VECTOR_CST_NELTS (m_tree_value).to_constant ();
for (i = 0; i < num; i++)
{
tree v = VECTOR_CST_ELT (m_tree_value, i);
unsigned actual = emit_immediate_scalar_to_buffer (v, p, 0);
total_len -= actual;
p += actual;
}
/* Vectors should have the exact size. */
gcc_assert (total_len == 0);
}
else if (TREE_CODE (m_tree_value) == STRING_CST)
memcpy (brig_repr, TREE_STRING_POINTER (m_tree_value),
TREE_STRING_LENGTH (m_tree_value));
else if (TREE_CODE (m_tree_value) == COMPLEX_CST)
{
gcc_assert (total_len % 2 == 0);
unsigned actual;
actual
= emit_immediate_scalar_to_buffer (TREE_REALPART (m_tree_value), p,
total_len / 2);
gcc_assert (actual == total_len / 2);
p += actual;
actual
= emit_immediate_scalar_to_buffer (TREE_IMAGPART (m_tree_value), p,
total_len / 2);
gcc_assert (actual == total_len / 2);
}
else if (TREE_CODE (m_tree_value) == CONSTRUCTOR)
{
unsigned len = CONSTRUCTOR_NELTS (m_tree_value);