-
Notifications
You must be signed in to change notification settings - Fork 117
/
Copy pathexecutable.cpp
1980 lines (1748 loc) · 64.6 KB
/
executable.cpp
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
////////////////////////////////////////////////////////////////////////////////
//
// The University of Illinois/NCSA
// Open Source License (NCSA)
//
// Copyright (c) 2014-2020, Advanced Micro Devices, Inc. All rights reserved.
//
// Developed by:
//
// AMD Research and AMD HSA Software Development
//
// Advanced Micro Devices, Inc.
//
// www.amd.com
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal with the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// - Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimers.
// - Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimers in
// the documentation and/or other materials provided with the distribution.
// - Neither the names of Advanced Micro Devices, Inc,
// nor the names of its contributors may be used to endorse or promote
// products derived from this Software without specific prior written
// permission.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS WITH THE SOFTWARE.
//
////////////////////////////////////////////////////////////////////////////////
#include "executable.hpp"
#include <libelf.h>
#include <limits.h>
#include <link.h>
#include <unistd.h>
#include <algorithm>
#include <cstddef>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <atomic>
#include <fstream>
#include "inc/amd_hsa_elf.h"
#include "inc/amd_hsa_kernel_code.h"
#include "core/inc/amd_hsa_code.hpp"
#include "amd_hsa_code_util.hpp"
#include "amd_options.hpp"
#include "core/util/utils.h"
#include "AMDHSAKernelDescriptor.h"
using namespace rocr::amd::hsa;
using namespace rocr::amd::hsa::common;
// r_version history:
// 1: Initial debug protocol
// 2: New trap handler ABI. The reason for halting a wave is recorded in ttmp11[8:7].
// 3: New trap handler ABI. A wave halted at S_ENDPGM rewinds its PC by 8 bytes, and sets ttmp11[9]=1.
// 4: New trap handler ABI. Save the trap id in ttmp11[16:9]
// 5: New trap handler ABI. Save the PC in ttmp11[22:7] ttmp6[31:0], and park the wave if stopped
// 6: New trap handler ABI. ttmp6[25:0] contains dispatch index modulo queue size
// 7: New trap handler ABI. Send interrupts as a bitmask, coalescing concurrent exceptions.
// 8: New trap handler ABI. for gfx940: Initialize ttmp[4:5] if ttmp11[31] == 0.
// 9: New trap handler ABI. For gfx11: Save PC in ttmp11[22:7] ttmp6[31:0], and park the wave if stopped.
// 10: New trap handler ABI. Set status.skip_export when halting the wave.
// For gfx940, set ttmp6[31] = 0 if ttmp11[31] == 0.
HSA_API r_debug _amdgpu_r_debug;
static __forceinline link_map*& r_debug_tail() {
static link_map* r_debug_tail_ = nullptr;
return r_debug_tail_;
}
namespace rocr {
// Having a side effect prevents call site optimization that allows removal of a noinline function call
// with no side effect.
__attribute__((noinline)) void _loader_debug_state() {
static volatile int function_needs_a_side_effect = 0;
function_needs_a_side_effect ^= 1;
}
namespace amd {
namespace hsa {
namespace loader {
class LoaderOptions {
public:
explicit LoaderOptions(std::ostream &error = std::cerr);
const amd::options::NoArgOption* Help() const { return &help; }
const amd::options::NoArgOption* DumpCode() const { return &dump_code; }
const amd::options::NoArgOption* DumpIsa() const { return &dump_isa; }
const amd::options::NoArgOption* DumpExec() const { return &dump_exec; }
const amd::options::NoArgOption* DumpAll() const { return &dump_all; }
const amd::options::ValueOption<std::string>* DumpDir() const { return &dump_dir; }
const amd::options::PrefixOption* Substitute() const { return &substitute; }
bool ParseOptions(const std::string& options);
void Reset();
void PrintHelp(std::ostream& out) const;
private:
/// @brief Copy constructor - not available.
LoaderOptions(const LoaderOptions&);
/// @brief Assignment operator - not available.
LoaderOptions& operator=(const LoaderOptions&);
amd::options::NoArgOption help;
amd::options::NoArgOption dump_code;
amd::options::NoArgOption dump_isa;
amd::options::NoArgOption dump_exec;
amd::options::NoArgOption dump_all;
amd::options::ValueOption<std::string> dump_dir;
amd::options::PrefixOption substitute;
amd::options::OptionParser option_parser;
};
LoaderOptions::LoaderOptions(std::ostream& error) :
help("help", "print help"),
dump_code("dump-code", "Dump finalizer output code object"),
dump_isa("dump-isa", "Dump finalizer output to ISA text file"),
dump_exec("dump-exec", "Dump executable to text file"),
dump_all("dump-all", "Dump all finalizer input and output (as above)"),
dump_dir("dump-dir", "Dump directory"),
substitute("substitute", "Substitute code object with given index or index range on loading from file"),
option_parser(false, error)
{
option_parser.AddOption(&help);
option_parser.AddOption(&dump_code);
option_parser.AddOption(&dump_isa);
option_parser.AddOption(&dump_exec);
option_parser.AddOption(&dump_all);
option_parser.AddOption(&dump_dir);
option_parser.AddOption(&substitute);
}
bool LoaderOptions::ParseOptions(const std::string& options)
{
return option_parser.ParseOptions(options.c_str());
}
void LoaderOptions::Reset()
{
option_parser.Reset();
}
void LoaderOptions::PrintHelp(std::ostream& out) const
{
option_parser.PrintHelp(out);
}
static const char *LOADER_DUMP_PREFIX = "amdcode";
Loader* Loader::Create(Context* context)
{
return new AmdHsaCodeLoader(context);
}
void Loader::Destroy(Loader *loader)
{
// Loader resets the link_map, but the executables and loaded code objects are not deleted.
_amdgpu_r_debug.r_map = nullptr;
_amdgpu_r_debug.r_state = r_debug::RT_CONSISTENT;
r_debug_tail() = nullptr;
delete loader;
}
Executable* AmdHsaCodeLoader::CreateExecutable(
hsa_profile_t profile, const char *options, hsa_default_float_rounding_mode_t default_float_rounding_mode)
{
WriterLockGuard<ReaderWriterLock> writer_lock(rw_lock_);
executables.push_back(new ExecutableImpl(profile, context, executables.size(), default_float_rounding_mode));
return executables.back();
}
Executable* AmdHsaCodeLoader::CreateExecutable(
std::unique_ptr<Context> isolated_context,
hsa_profile_t profile,
const char *options,
hsa_default_float_rounding_mode_t default_float_rounding_mode)
{
WriterLockGuard<ReaderWriterLock> writer_lock(rw_lock_);
executables.push_back(new ExecutableImpl(profile, std::move(isolated_context), executables.size(), default_float_rounding_mode));
return executables.back();
}
static void AddCodeObjectInfoIntoDebugMap(link_map* map) {
if (r_debug_tail()) {
r_debug_tail()->l_next = map;
map->l_prev = r_debug_tail();
map->l_next = nullptr;
} else {
_amdgpu_r_debug.r_map = map;
map->l_prev = nullptr;
map->l_next = nullptr;
}
r_debug_tail() = map;
}
static void RemoveCodeObjectInfoFromDebugMap(link_map* map) {
if (r_debug_tail() == map) {
r_debug_tail() = map->l_prev;
}
if (_amdgpu_r_debug.r_map == map) {
_amdgpu_r_debug.r_map = map->l_next;
}
if (map->l_prev) {
map->l_prev->l_next = map->l_next;
}
if (map->l_next) {
map->l_next->l_prev = map->l_prev;
}
free(map->l_name);
memset(map, 0, sizeof(link_map));
}
hsa_status_t AmdHsaCodeLoader::FreezeExecutable(Executable *executable, const char *options) {
hsa_status_t status = executable->Freeze(options);
if (status != HSA_STATUS_SUCCESS) {
return status;
}
// Assuming runtime atomic implements C++ std::memory_order
WriterLockGuard<ReaderWriterLock> writer_lock(rw_lock_);
atomic::Store(&_amdgpu_r_debug.r_state, r_debug::RT_ADD, std::memory_order_relaxed);
atomic::Fence(std::memory_order_acq_rel);
_loader_debug_state();
atomic::Fence(std::memory_order_acq_rel);
for (auto &lco : reinterpret_cast<ExecutableImpl*>(executable)->loaded_code_objects) {
AddCodeObjectInfoIntoDebugMap(&(lco->r_debug_info));
}
atomic::Store(&_amdgpu_r_debug.r_state, r_debug::RT_CONSISTENT, std::memory_order_release);
_loader_debug_state();
return HSA_STATUS_SUCCESS;
}
void AmdHsaCodeLoader::DestroyExecutable(Executable *executable) {
// Assuming runtime atomic implements C++ std::memory_order
WriterLockGuard<ReaderWriterLock> writer_lock(rw_lock_);
atomic::Store(&_amdgpu_r_debug.r_state, r_debug::RT_DELETE, std::memory_order_relaxed);
atomic::Fence(std::memory_order_acq_rel);
_loader_debug_state();
atomic::Fence(std::memory_order_acq_rel);
for (auto &lco : reinterpret_cast<ExecutableImpl*>(executable)->loaded_code_objects) {
RemoveCodeObjectInfoFromDebugMap(&(lco->r_debug_info));
}
atomic::Store(&_amdgpu_r_debug.r_state, r_debug::RT_CONSISTENT, std::memory_order_release);
_loader_debug_state();
executables[((ExecutableImpl*)executable)->id()] = nullptr;
delete executable;
}
hsa_status_t AmdHsaCodeLoader::IterateExecutables(
hsa_status_t (*callback)(
hsa_executable_t executable,
void *data),
void *data)
{
WriterLockGuard<ReaderWriterLock> writer_lock(rw_lock_);
assert(callback);
for (auto &exec : executables) {
if(exec != nullptr){
hsa_status_t status = callback(Executable::Handle(exec), data);
if (status != HSA_STATUS_SUCCESS) {
return status;
}
}
}
return HSA_STATUS_SUCCESS;
}
hsa_status_t AmdHsaCodeLoader::QuerySegmentDescriptors(
hsa_ven_amd_loader_segment_descriptor_t *segment_descriptors,
size_t *num_segment_descriptors)
{
if (!num_segment_descriptors) {
return HSA_STATUS_ERROR_INVALID_ARGUMENT;
}
if (*num_segment_descriptors == 0 && segment_descriptors) {
return HSA_STATUS_ERROR_INVALID_ARGUMENT;
}
if (*num_segment_descriptors != 0 && !segment_descriptors) {
return HSA_STATUS_ERROR_INVALID_ARGUMENT;
}
this->EnableReadOnlyMode();
size_t actual_num_segment_descriptors = 0;
for (auto &executable : executables) {
if (executable) {
actual_num_segment_descriptors += executable->GetNumSegmentDescriptors();
}
}
if (*num_segment_descriptors == 0) {
*num_segment_descriptors = actual_num_segment_descriptors;
this->DisableReadOnlyMode();
return HSA_STATUS_SUCCESS;
}
if (*num_segment_descriptors != actual_num_segment_descriptors) {
this->DisableReadOnlyMode();
return HSA_STATUS_ERROR_INCOMPATIBLE_ARGUMENTS;
}
size_t i = 0;
for (auto &executable : executables) {
if (executable) {
i += executable->QuerySegmentDescriptors(segment_descriptors, actual_num_segment_descriptors, i);
}
}
this->DisableReadOnlyMode();
return HSA_STATUS_SUCCESS;
}
uint64_t AmdHsaCodeLoader::FindHostAddress(uint64_t device_address)
{
ReaderLockGuard<ReaderWriterLock> reader_lock(rw_lock_);
if (device_address == 0) {
return 0;
}
for (auto &exec : executables) {
if (exec != nullptr) {
uint64_t host_address = exec->FindHostAddress(device_address);
if (host_address != 0) {
return host_address;
}
}
}
return 0;
}
void AmdHsaCodeLoader::PrintHelp(std::ostream& out)
{
LoaderOptions().PrintHelp(out);
}
void AmdHsaCodeLoader::EnableReadOnlyMode()
{
rw_lock_.ReaderLock();
for (auto &executable : executables) {
if (executable) {
((ExecutableImpl*)executable)->EnableReadOnlyMode();
}
}
}
void AmdHsaCodeLoader::DisableReadOnlyMode()
{
rw_lock_.ReaderUnlock();
for (auto &executable : executables) {
if (executable) {
((ExecutableImpl*)executable)->DisableReadOnlyMode();
}
}
}
//===----------------------------------------------------------------------===//
// SymbolImpl. //
//===----------------------------------------------------------------------===//
bool SymbolImpl::GetInfo(hsa_symbol_info32_t symbol_info, void *value) {
static_assert(
(symbol_attribute32_t(HSA_CODE_SYMBOL_INFO_TYPE) ==
symbol_attribute32_t(HSA_EXECUTABLE_SYMBOL_INFO_TYPE)),
"attributes are not compatible"
);
static_assert(
(symbol_attribute32_t(HSA_CODE_SYMBOL_INFO_TYPE) ==
symbol_attribute32_t(HSA_EXECUTABLE_SYMBOL_INFO_TYPE)),
"attributes are not compatible"
);
static_assert(
(symbol_attribute32_t(HSA_CODE_SYMBOL_INFO_NAME_LENGTH) ==
symbol_attribute32_t(HSA_EXECUTABLE_SYMBOL_INFO_NAME_LENGTH)),
"attributes are not compatible"
);
static_assert(
(symbol_attribute32_t(HSA_CODE_SYMBOL_INFO_NAME) ==
symbol_attribute32_t(HSA_EXECUTABLE_SYMBOL_INFO_NAME)),
"attributes are not compatible"
);
static_assert(
(symbol_attribute32_t(HSA_CODE_SYMBOL_INFO_MODULE_NAME_LENGTH) ==
symbol_attribute32_t(HSA_EXECUTABLE_SYMBOL_INFO_MODULE_NAME_LENGTH)),
"attributes are not compatible"
);
static_assert(
(symbol_attribute32_t(HSA_CODE_SYMBOL_INFO_MODULE_NAME) ==
symbol_attribute32_t(HSA_EXECUTABLE_SYMBOL_INFO_MODULE_NAME)),
"attributes are not compatible"
);
static_assert(
(symbol_attribute32_t(HSA_CODE_SYMBOL_INFO_LINKAGE) ==
symbol_attribute32_t(HSA_EXECUTABLE_SYMBOL_INFO_LINKAGE)),
"attributes are not compatible"
);
static_assert(
(symbol_attribute32_t(HSA_CODE_SYMBOL_INFO_IS_DEFINITION) ==
symbol_attribute32_t(HSA_EXECUTABLE_SYMBOL_INFO_IS_DEFINITION)),
"attributes are not compatible"
);
assert(value);
switch (symbol_info) {
case HSA_CODE_SYMBOL_INFO_TYPE: {
*((hsa_symbol_kind_t*)value) = kind;
break;
}
case HSA_CODE_SYMBOL_INFO_NAME_LENGTH: {
*((uint32_t*)value) = symbol_name.size();
break;
}
case HSA_CODE_SYMBOL_INFO_NAME: {
memset(value, 0x0, symbol_name.size());
memcpy(value, symbol_name.c_str(), symbol_name.size());
break;
}
case HSA_CODE_SYMBOL_INFO_MODULE_NAME_LENGTH: {
*((uint32_t*)value) = module_name.size();
break;
}
case HSA_CODE_SYMBOL_INFO_MODULE_NAME: {
memset(value, 0x0, module_name.size());
memcpy(value, module_name.c_str(), module_name.size());
break;
}
case HSA_CODE_SYMBOL_INFO_LINKAGE: {
*((hsa_symbol_linkage_t*)value) = linkage;
break;
}
case HSA_CODE_SYMBOL_INFO_IS_DEFINITION: {
*((bool*)value) = is_definition;
break;
}
case HSA_EXECUTABLE_SYMBOL_INFO_KERNEL_CALL_CONVENTION: {
*((uint32_t*)value) = 0;
break;
}
case HSA_EXECUTABLE_SYMBOL_INFO_KERNEL_OBJECT:
case HSA_EXECUTABLE_SYMBOL_INFO_VARIABLE_ADDRESS: {
if (!is_loaded) {
return false;
}
*((uint64_t*)value) = address;
break;
}
case HSA_EXECUTABLE_SYMBOL_INFO_AGENT: {
if (!is_loaded) {
return false;
}
*((hsa_agent_t*)value) = agent;
break;
}
default: {
return false;
}
}
return true;
}
//===----------------------------------------------------------------------===//
// KernelSymbol. //
//===----------------------------------------------------------------------===//
bool KernelSymbol::GetInfo(hsa_symbol_info32_t symbol_info, void *value) {
static_assert(
(symbol_attribute32_t(HSA_CODE_SYMBOL_INFO_KERNEL_KERNARG_SEGMENT_SIZE) ==
symbol_attribute32_t(HSA_EXECUTABLE_SYMBOL_INFO_KERNEL_KERNARG_SEGMENT_SIZE)),
"attributes are not compatible"
);
static_assert(
(symbol_attribute32_t(HSA_CODE_SYMBOL_INFO_KERNEL_KERNARG_SEGMENT_ALIGNMENT) ==
symbol_attribute32_t(HSA_EXECUTABLE_SYMBOL_INFO_KERNEL_KERNARG_SEGMENT_ALIGNMENT)),
"attributes are not compatible"
);
static_assert(
(symbol_attribute32_t(HSA_CODE_SYMBOL_INFO_KERNEL_GROUP_SEGMENT_SIZE) ==
symbol_attribute32_t(HSA_EXECUTABLE_SYMBOL_INFO_KERNEL_GROUP_SEGMENT_SIZE)),
"attributes are not compatible"
);
static_assert(
(symbol_attribute32_t(HSA_CODE_SYMBOL_INFO_KERNEL_PRIVATE_SEGMENT_SIZE) ==
symbol_attribute32_t(HSA_EXECUTABLE_SYMBOL_INFO_KERNEL_PRIVATE_SEGMENT_SIZE)),
"attributes are not compatible"
);
static_assert(
(symbol_attribute32_t(HSA_CODE_SYMBOL_INFO_KERNEL_DYNAMIC_CALLSTACK) ==
symbol_attribute32_t(HSA_EXECUTABLE_SYMBOL_INFO_KERNEL_DYNAMIC_CALLSTACK)),
"attributes are not compatible"
);
assert(value);
switch (symbol_info) {
case HSA_CODE_SYMBOL_INFO_KERNEL_KERNARG_SEGMENT_SIZE: {
*((uint32_t*)value) = kernarg_segment_size;
break;
}
case HSA_CODE_SYMBOL_INFO_KERNEL_KERNARG_SEGMENT_ALIGNMENT: {
*((uint32_t*)value) = kernarg_segment_alignment;
break;
}
case HSA_CODE_SYMBOL_INFO_KERNEL_GROUP_SEGMENT_SIZE: {
*((uint32_t*)value) = group_segment_size;
break;
}
case HSA_CODE_SYMBOL_INFO_KERNEL_PRIVATE_SEGMENT_SIZE: {
*((uint32_t*)value) = private_segment_size;
break;
}
case HSA_CODE_SYMBOL_INFO_KERNEL_DYNAMIC_CALLSTACK: {
*((bool*)value) = is_dynamic_callstack;
break;
}
case HSA_CODE_SYMBOL_INFO_KERNEL_WAVEFRONT_SIZE: {
*((uint32_t*)value) = wavefront_size;
break;
}
case HSA_EXT_EXECUTABLE_SYMBOL_INFO_KERNEL_OBJECT_SIZE: {
*((uint32_t*)value) = size;
break;
}
case HSA_EXT_EXECUTABLE_SYMBOL_INFO_KERNEL_OBJECT_ALIGN: {
*((uint32_t*)value) = alignment;
break;
}
default: {
return SymbolImpl::GetInfo(symbol_info, value);
}
}
return true;
}
//===----------------------------------------------------------------------===//
// VariableSymbol. //
//===----------------------------------------------------------------------===//
bool VariableSymbol::GetInfo(hsa_symbol_info32_t symbol_info, void *value) {
static_assert(
(symbol_attribute32_t(HSA_CODE_SYMBOL_INFO_VARIABLE_ALLOCATION) ==
symbol_attribute32_t(HSA_EXECUTABLE_SYMBOL_INFO_VARIABLE_ALLOCATION)),
"attributes are not compatible"
);
static_assert(
(symbol_attribute32_t(HSA_CODE_SYMBOL_INFO_VARIABLE_SEGMENT) ==
symbol_attribute32_t(HSA_EXECUTABLE_SYMBOL_INFO_VARIABLE_SEGMENT)),
"attributes are not compatible"
);
static_assert(
(symbol_attribute32_t(HSA_CODE_SYMBOL_INFO_VARIABLE_ALIGNMENT) ==
symbol_attribute32_t(HSA_EXECUTABLE_SYMBOL_INFO_VARIABLE_ALIGNMENT)),
"attributes are not compatible"
);
static_assert(
(symbol_attribute32_t(HSA_CODE_SYMBOL_INFO_VARIABLE_SIZE) ==
symbol_attribute32_t(HSA_EXECUTABLE_SYMBOL_INFO_VARIABLE_SIZE)),
"attributes are not compatible"
);
static_assert(
(symbol_attribute32_t(HSA_CODE_SYMBOL_INFO_VARIABLE_IS_CONST) ==
symbol_attribute32_t(HSA_EXECUTABLE_SYMBOL_INFO_VARIABLE_IS_CONST)),
"attributes are not compatible"
);
switch (symbol_info) {
case HSA_CODE_SYMBOL_INFO_VARIABLE_ALLOCATION: {
*((hsa_variable_allocation_t*)value) = allocation;
break;
}
case HSA_CODE_SYMBOL_INFO_VARIABLE_SEGMENT: {
*((hsa_variable_segment_t*)value) = segment;
break;
}
case HSA_CODE_SYMBOL_INFO_VARIABLE_ALIGNMENT: {
*((uint32_t*)value) = alignment;
break;
}
case HSA_CODE_SYMBOL_INFO_VARIABLE_SIZE: {
*((uint32_t*)value) = size;
break;
}
case HSA_CODE_SYMBOL_INFO_VARIABLE_IS_CONST: {
*((bool*)value) = is_constant;
break;
}
default: {
return SymbolImpl::GetInfo(symbol_info, value);
}
}
return true;
}
bool LoadedCodeObjectImpl::GetInfo(amd_loaded_code_object_info_t attribute, void *value)
{
assert(value);
switch (attribute) {
case AMD_LOADED_CODE_OBJECT_INFO_ELF_IMAGE:
((hsa_code_object_t*)value)->handle = reinterpret_cast<uint64_t>(elf_data);
break;
case AMD_LOADED_CODE_OBJECT_INFO_ELF_IMAGE_SIZE:
*((size_t*)value) = elf_size;
break;
default: {
return false;
}
}
return true;
}
hsa_status_t LoadedCodeObjectImpl::IterateLoadedSegments(
hsa_status_t (*callback)(
amd_loaded_segment_t loaded_segment,
void *data),
void *data)
{
assert(callback);
for (auto &loaded_segment : loaded_segments) {
hsa_status_t status = callback(LoadedSegment::Handle(loaded_segment), data);
if (status != HSA_STATUS_SUCCESS) {
return status;
}
}
return HSA_STATUS_SUCCESS;
}
void LoadedCodeObjectImpl::Print(std::ostream& out)
{
out << "Code Object" << std::endl;
}
bool Segment::GetInfo(amd_loaded_segment_info_t attribute, void *value)
{
assert(value);
switch (attribute) {
case AMD_LOADED_SEGMENT_INFO_TYPE: {
*((amdgpu_hsa_elf_segment_t*)value) = segment;
break;
}
case AMD_LOADED_SEGMENT_INFO_ELF_BASE_ADDRESS: {
*((uint64_t*)value) = vaddr;
break;
}
case AMD_LOADED_SEGMENT_INFO_LOAD_BASE_ADDRESS: {
*((uint64_t*)value) = reinterpret_cast<uint64_t>(this->Address(this->VAddr()));
break;
}
case AMD_LOADED_SEGMENT_INFO_SIZE: {
*((size_t*)value) = size;
break;
}
default: {
return false;
}
}
return true;
}
uint64_t Segment::Offset(uint64_t addr)
{
assert(IsAddressInSegment(addr));
return addr - vaddr;
}
void* Segment::Address(uint64_t addr)
{
return owner->context()->SegmentAddress(segment, agent, ptr, Offset(addr));
}
bool Segment::Freeze()
{
return !frozen ? (frozen = owner->context()->SegmentFreeze(segment, agent, ptr, size)) : true;
}
bool Segment::IsAddressInSegment(uint64_t addr)
{
return vaddr <= addr && addr < vaddr + size;
}
void Segment::Copy(uint64_t addr, const void* src, size_t size)
{
// loader must do copies before freezing.
assert(!frozen);
if (size > 0) {
owner->context()->SegmentCopy(segment, agent, ptr, Offset(addr), src, size);
}
}
void Segment::Print(std::ostream& out)
{
out << "Segment" << std::endl
<< " Type: " << AmdHsaElfSegmentToString(segment)
<< " Size: " << size
<< " VAddr: " << vaddr << std::endl
<< " Ptr: " << std::hex << ptr << std::dec
<< std::endl;
}
void Segment::Destroy()
{
owner->context()->SegmentFree(segment, agent, ptr, size);
}
//===----------------------------------------------------------------------===//
// ExecutableImpl. //
//===----------------------------------------------------------------------===//
ExecutableImpl::ExecutableImpl(
const hsa_profile_t &_profile,
Context *context,
size_t id,
hsa_default_float_rounding_mode_t default_float_rounding_mode)
: Executable()
, profile_(_profile)
, context_(context)
, id_(id)
, default_float_rounding_mode_(default_float_rounding_mode)
, state_(HSA_EXECUTABLE_STATE_UNFROZEN)
, program_allocation_segment(nullptr)
{
}
ExecutableImpl::ExecutableImpl(
const hsa_profile_t &_profile,
std::unique_ptr<Context> unique_context,
size_t id,
hsa_default_float_rounding_mode_t default_float_rounding_mode)
: Executable()
, profile_(_profile)
, unique_context_(std::move(unique_context))
, id_(id)
, default_float_rounding_mode_(default_float_rounding_mode)
, state_(HSA_EXECUTABLE_STATE_UNFROZEN)
, program_allocation_segment(nullptr)
{
context_ = unique_context_.get();
}
ExecutableImpl::~ExecutableImpl() {
for (ExecutableObject* o : objects) {
o->Destroy();
delete o;
}
objects.clear();
for (auto &symbol_entry : program_symbols_) {
delete symbol_entry.second;
}
for (auto &symbol_entry : agent_symbols_) {
delete symbol_entry.second;
}
}
hsa_status_t ExecutableImpl::DefineProgramExternalVariable(
const char *name, void *address)
{
WriterLockGuard<ReaderWriterLock> writer_lock(rw_lock_);
assert(name);
if (HSA_EXECUTABLE_STATE_FROZEN == state_) {
return HSA_STATUS_ERROR_FROZEN_EXECUTABLE;
}
auto symbol_entry = program_symbols_.find(std::string(name));
if (symbol_entry != program_symbols_.end()) {
return HSA_STATUS_ERROR_VARIABLE_ALREADY_DEFINED;
}
program_symbols_.insert(
std::make_pair(std::string(name),
new VariableSymbol(true,
"", // Only program linkage symbols can be
// defined.
std::string(name),
HSA_SYMBOL_LINKAGE_PROGRAM,
true,
HSA_VARIABLE_ALLOCATION_PROGRAM,
HSA_VARIABLE_SEGMENT_GLOBAL,
0, // TODO: size.
0, // TODO: align.
false, // TODO: const.
true,
reinterpret_cast<uint64_t>(address))));
return HSA_STATUS_SUCCESS;
}
hsa_status_t ExecutableImpl::DefineAgentExternalVariable(
const char *name,
hsa_agent_t agent,
hsa_variable_segment_t segment,
void *address)
{
WriterLockGuard<ReaderWriterLock> writer_lock(rw_lock_);
assert(name);
if (HSA_EXECUTABLE_STATE_FROZEN == state_) {
return HSA_STATUS_ERROR_FROZEN_EXECUTABLE;
}
auto symbol_entry = agent_symbols_.find(std::make_pair(std::string(name), agent));
if (symbol_entry != agent_symbols_.end()) {
return HSA_STATUS_ERROR_VARIABLE_ALREADY_DEFINED;
}
auto insert_status = agent_symbols_.insert(
std::make_pair(std::make_pair(std::string(name), agent),
new VariableSymbol(true,
"", // Only program linkage symbols can be
// defined.
std::string(name),
HSA_SYMBOL_LINKAGE_PROGRAM,
true,
HSA_VARIABLE_ALLOCATION_AGENT,
segment,
0, // TODO: size.
0, // TODO: align.
false, // TODO: const.
true,
reinterpret_cast<uint64_t>(address))));
assert(insert_status.second);
insert_status.first->second->agent = agent;
return HSA_STATUS_SUCCESS;
}
bool ExecutableImpl::IsProgramSymbol(const char *symbol_name) {
assert(symbol_name);
ReaderLockGuard<ReaderWriterLock> reader_lock(rw_lock_);
return program_symbols_.find(std::string(symbol_name)) != program_symbols_.end();
}
Symbol* ExecutableImpl::GetSymbol(
const char *symbol_name,
const hsa_agent_t *agent)
{
ReaderLockGuard<ReaderWriterLock> reader_lock(rw_lock_);
return this->GetSymbolInternal(symbol_name, agent);
}
Symbol* ExecutableImpl::GetSymbolInternal(
const char *symbol_name,
const hsa_agent_t *agent)
{
assert(symbol_name);
std::string mangled_name = std::string(symbol_name);
if (mangled_name.empty()) {
return nullptr;
}
if (!agent) {
auto program_symbol = program_symbols_.find(mangled_name);
if (program_symbol != program_symbols_.end()) {
return program_symbol->second;
}
return nullptr;
}
auto agent_symbol = agent_symbols_.find(std::make_pair(mangled_name, *agent));
if (agent_symbol != agent_symbols_.end()) {
return agent_symbol->second;
}
return nullptr;
}
hsa_status_t ExecutableImpl::IterateSymbols(
iterate_symbols_f callback, void *data)
{
ReaderLockGuard<ReaderWriterLock> reader_lock(rw_lock_);
assert(callback);
for (auto &symbol_entry : program_symbols_) {
hsa_status_t hsc =
callback(Executable::Handle(this), Symbol::Handle(symbol_entry.second), data);
if (HSA_STATUS_SUCCESS != hsc) {
return hsc;
}
}
for (auto &symbol_entry : agent_symbols_) {
hsa_status_t hsc =
callback(Executable::Handle(this), Symbol::Handle(symbol_entry.second), data);
if (HSA_STATUS_SUCCESS != hsc) {
return hsc;
}
}
return HSA_STATUS_SUCCESS;
}
hsa_status_t ExecutableImpl::IterateAgentSymbols(
hsa_agent_t agent,
hsa_status_t (*callback)(hsa_executable_t exec,
hsa_agent_t agent,
hsa_executable_symbol_t symbol,
void *data),
void *data) {
ReaderLockGuard<ReaderWriterLock> reader_lock(rw_lock_);
assert(callback);
for (auto &symbol_entry : agent_symbols_) {
if (symbol_entry.second->GetAgent().handle != agent.handle) {
continue;
}
hsa_status_t status = callback(
Executable::Handle(this), agent, Symbol::Handle(symbol_entry.second),
data);
if (status != HSA_STATUS_SUCCESS) {
return status;
}
}
return HSA_STATUS_SUCCESS;
}
hsa_status_t ExecutableImpl::IterateProgramSymbols(
hsa_status_t (*callback)(hsa_executable_t exec,
hsa_executable_symbol_t symbol,
void *data),
void *data) {
ReaderLockGuard<ReaderWriterLock> reader_lock(rw_lock_);
assert(callback);
for (auto &symbol_entry : program_symbols_) {
hsa_status_t status = callback(
Executable::Handle(this), Symbol::Handle(symbol_entry.second), data);
if (status != HSA_STATUS_SUCCESS) {
return status;
}
}
return HSA_STATUS_SUCCESS;
}
hsa_status_t ExecutableImpl::IterateLoadedCodeObjects(
hsa_status_t (*callback)(
hsa_executable_t executable,
hsa_loaded_code_object_t loaded_code_object,
void *data),
void *data)
{
ReaderLockGuard<ReaderWriterLock> reader_lock(rw_lock_);
assert(callback);
for (auto &loaded_code_object : loaded_code_objects) {
hsa_status_t status = callback(
Executable::Handle(this),
LoadedCodeObject::Handle(loaded_code_object),
data);
if (status != HSA_STATUS_SUCCESS) {
return status;
}
}
return HSA_STATUS_SUCCESS;
}
size_t ExecutableImpl::GetNumSegmentDescriptors()
{
// assuming we are in readonly mode.
size_t actual_num_segment_descriptors = 0;
for (auto &obj : loaded_code_objects) {
actual_num_segment_descriptors += obj->LoadedSegments().size();
}