-
Notifications
You must be signed in to change notification settings - Fork 545
/
Copy pathcuda-sim.cc
2143 lines (1911 loc) · 70.6 KB
/
cuda-sim.cc
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
// Copyright (c) 2009-2011, Tor M. Aamodt, Ali Bakhoda, Wilson W.L. Fung,
// George L. Yuan, Jimmy Kwa
// The University of British Columbia
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
// Redistributions in binary form must reproduce the above copyright notice, this
// list of conditions and the following disclaimer in the documentation and/or
// other materials provided with the distribution.
// Neither the name of The University of British Columbia nor the names of its
// contributors may be used to endorse or promote products derived from this
// software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "cuda-sim.h"
#include "instructions.h"
#include "ptx_ir.h"
#include "ptx.tab.hpp"
#include "ptx_sim.h"
#include <stdio.h>
#include "opcodes.h"
#include "../statwrapper.h"
#include <set>
#include <map>
#include "../abstract_hardware_model.h"
#include "memory.h"
#include "ptx-stats.h"
#include "ptx_loader.h"
#include "ptx_parser.h"
#include "../gpgpu-sim/gpu-sim.h"
#include "ptx_sim.h"
#include "../gpgpusim_entrypoint.h"
#include "decuda_pred_table/decuda_pred_table.h"
#include "../stream_manager.h"
#include "cuda_device_runtime.h"
int gpgpu_ptx_instruction_classification;
void ** g_inst_classification_stat = NULL;
void ** g_inst_op_classification_stat= NULL;
int g_ptx_kernel_count = -1; // used for classification stat collection purposes
int g_debug_execution = 0;
int g_debug_thread_uid = 0;
addr_t g_debug_pc = 0xBEEF1518;
// Output debug information to file options
unsigned g_ptx_sim_num_insn = 0;
unsigned gpgpu_param_num_shaders = 0;
char *opcode_latency_int, *opcode_latency_fp, *opcode_latency_dp;
char *opcode_initiation_int, *opcode_initiation_fp, *opcode_initiation_dp;
char *cdp_latency_str;
unsigned cdp_latency[5];
void ptx_opcocde_latency_options (option_parser_t opp) {
option_parser_register(opp, "-ptx_opcode_latency_int", OPT_CSTR, &opcode_latency_int,
"Opcode latencies for integers <ADD,MAX,MUL,MAD,DIV>"
"Default 1,1,19,25,145",
"1,1,19,25,145");
option_parser_register(opp, "-ptx_opcode_latency_fp", OPT_CSTR, &opcode_latency_fp,
"Opcode latencies for single precision floating points <ADD,MAX,MUL,MAD,DIV>"
"Default 1,1,1,1,30",
"1,1,1,1,30");
option_parser_register(opp, "-ptx_opcode_latency_dp", OPT_CSTR, &opcode_latency_dp,
"Opcode latencies for double precision floating points <ADD,MAX,MUL,MAD,DIV>"
"Default 8,8,8,8,335",
"8,8,8,8,335");
option_parser_register(opp, "-ptx_opcode_initiation_int", OPT_CSTR, &opcode_initiation_int,
"Opcode initiation intervals for integers <ADD,MAX,MUL,MAD,DIV>"
"Default 1,1,4,4,32",
"1,1,4,4,32");
option_parser_register(opp, "-ptx_opcode_initiation_fp", OPT_CSTR, &opcode_initiation_fp,
"Opcode initiation intervals for single precision floating points <ADD,MAX,MUL,MAD,DIV>"
"Default 1,1,1,1,5",
"1,1,1,1,5");
option_parser_register(opp, "-ptx_opcode_initiation_dp", OPT_CSTR, &opcode_initiation_dp,
"Opcode initiation intervals for double precision floating points <ADD,MAX,MUL,MAD,DIV>"
"Default 8,8,8,8,130",
"8,8,8,8,130");
option_parser_register(opp, "-cdp_latency", OPT_CSTR, &cdp_latency_str,
"CDP API latency <cudaStreamCreateWithFlags, \
cudaGetParameterBufferV2_init_perWarp, cudaGetParameterBufferV2_perKernel, \
cudaLaunchDeviceV2_init_perWarp, cudaLaunchDevicV2_perKernel>"
"Default 7200,8000,100,12000,1600",
"7200,8000,100,12000,1600");
}
static address_type get_converge_point(address_type pc);
void gpgpu_t::gpgpu_ptx_sim_bindNameToTexture(const char* name, const struct textureReference* texref, int dim, int readmode, int ext)
{
std::string texname(name);
m_NameToTextureRef[texname] = texref;
const textureReferenceAttr *texAttr = new textureReferenceAttr(texref, dim, (enum cudaTextureReadMode)readmode, ext);
m_TextureRefToAttribute[texref] = texAttr;
}
const char* gpgpu_t::gpgpu_ptx_sim_findNamefromTexture(const struct textureReference* texref)
{
std::map<std::string, const struct textureReference*>::iterator itr = m_NameToTextureRef.begin();
while (itr != m_NameToTextureRef.end()) {
if ((*itr).second == texref) {
const char *p = ((*itr).first).c_str();
return p;
}
itr++;
}
return NULL;
}
unsigned int intLOGB2( unsigned int v ) {
unsigned int shift;
unsigned int r;
r = 0;
shift = (( v & 0xFFFF0000) != 0 ) << 4; v >>= shift; r |= shift;
shift = (( v & 0xFF00 ) != 0 ) << 3; v >>= shift; r |= shift;
shift = (( v & 0xF0 ) != 0 ) << 2; v >>= shift; r |= shift;
shift = (( v & 0xC ) != 0 ) << 1; v >>= shift; r |= shift;
shift = (( v & 0x2 ) != 0 ) << 0; v >>= shift; r |= shift;
return r;
}
void gpgpu_t::gpgpu_ptx_sim_bindTextureToArray(const struct textureReference* texref, const struct cudaArray* array)
{
m_TextureRefToCudaArray[texref] = array;
unsigned int texel_size_bits = array->desc.w + array->desc.x + array->desc.y + array->desc.z;
unsigned int texel_size = texel_size_bits/8;
unsigned int Tx, Ty;
int r;
printf("GPGPU-Sim PTX: texel size = %d\n", texel_size);
printf("GPGPU-Sim PTX: texture cache linesize = %d\n", m_function_model_config.get_texcache_linesize());
//first determine base Tx size for given linesize
switch (m_function_model_config.get_texcache_linesize()) {
case 16: Tx = 4; break;
case 32: Tx = 8; break;
case 64: Tx = 8; break;
case 128: Tx = 16; break;
case 256: Tx = 16; break;
default:
printf("GPGPU-Sim PTX: Line size of %d bytes currently not supported.\n", m_function_model_config.get_texcache_linesize());
assert(0);
break;
}
r = texel_size >> 2;
//modify base Tx size to take into account size of each texel in bytes
while (r != 0) {
Tx = Tx >> 1;
r = r >> 2;
}
//by now, got the correct Tx size, calculate correct Ty size
Ty = m_function_model_config.get_texcache_linesize()/(Tx*texel_size);
printf("GPGPU-Sim PTX: Tx = %d; Ty = %d, Tx_numbits = %d, Ty_numbits = %d\n", Tx, Ty, intLOGB2(Tx), intLOGB2(Ty));
printf("GPGPU-Sim PTX: Texel size = %d bytes; texel_size_numbits = %d\n", texel_size, intLOGB2(texel_size));
printf("GPGPU-Sim PTX: Binding texture to array starting at devPtr32 = 0x%x\n", array->devPtr32);
printf("GPGPU-Sim PTX: Texel size = %d bytes\n", texel_size);
struct textureInfo* texInfo = (struct textureInfo*) malloc(sizeof(struct textureInfo));
texInfo->Tx = Tx;
texInfo->Ty = Ty;
texInfo->Tx_numbits = intLOGB2(Tx);
texInfo->Ty_numbits = intLOGB2(Ty);
texInfo->texel_size = texel_size;
texInfo->texel_size_numbits = intLOGB2(texel_size);
m_TextureRefToTexureInfo[texref] = texInfo;
}
unsigned g_assemble_code_next_pc=0;
std::map<unsigned,function_info*> g_pc_to_finfo;
std::vector<ptx_instruction*> function_info::s_g_pc_to_insn;
#define MAX_INST_SIZE 8 /*bytes*/
void function_info::ptx_assemble()
{
if( m_assembled ) {
return;
}
// get the instructions into instruction memory...
unsigned num_inst = m_instructions.size();
m_instr_mem_size = MAX_INST_SIZE*(num_inst+1);
m_instr_mem = new ptx_instruction*[ m_instr_mem_size ];
printf("GPGPU-Sim PTX: instruction assembly for function \'%s\'... ", m_name.c_str() );
fflush(stdout);
std::list<ptx_instruction*>::iterator i;
addr_t PC = g_assemble_code_next_pc; // globally unique address (across functions)
// start function on an aligned address
for( unsigned i=0; i < (PC%MAX_INST_SIZE); i++ )
s_g_pc_to_insn.push_back((ptx_instruction*)NULL);
PC += PC%MAX_INST_SIZE;
m_start_PC = PC;
addr_t n=0; // offset in m_instr_mem
s_g_pc_to_insn.reserve(s_g_pc_to_insn.size() + MAX_INST_SIZE*m_instructions.size());
for ( i=m_instructions.begin(); i != m_instructions.end(); i++ ) {
ptx_instruction *pI = *i;
if ( pI->is_label() ) {
const symbol *l = pI->get_label();
labels[l->name()] = n;
} else {
g_pc_to_finfo[PC] = this;
m_instr_mem[n] = pI;
s_g_pc_to_insn.push_back(pI);
assert(pI == s_g_pc_to_insn[PC]);
pI->set_m_instr_mem_index(n);
pI->set_PC(PC);
assert( pI->inst_size() <= MAX_INST_SIZE );
for( unsigned i=1; i < pI->inst_size(); i++ ) {
s_g_pc_to_insn.push_back((ptx_instruction*)NULL);
m_instr_mem[n+i]=NULL;
}
n += pI->inst_size();
PC += pI->inst_size();
}
}
g_assemble_code_next_pc=PC;
for ( unsigned ii=0; ii < n; ii += m_instr_mem[ii]->inst_size() ) { // handle branch instructions
ptx_instruction *pI = m_instr_mem[ii];
if ( pI->get_opcode() == BRA_OP || pI->get_opcode() == BREAKADDR_OP || pI->get_opcode() == CALLP_OP) {
operand_info &target = pI->dst(); //get operand, e.g. target name
if ( labels.find(target.name()) == labels.end() ) {
printf("GPGPU-Sim PTX: Loader error (%s:%u): Branch label \"%s\" does not appear in assembly code.",
pI->source_file(),pI->source_line(), target.name().c_str() );
abort();
}
unsigned index = labels[ target.name() ]; //determine address from name
unsigned PC = m_instr_mem[index]->get_PC();
m_symtab->set_label_address( target.get_symbol(), PC );
target.set_type(label_t);
}
}
printf(" done.\n");
fflush(stdout);
printf("GPGPU-Sim PTX: finding reconvergence points for \'%s\'...\n", m_name.c_str() );
create_basic_blocks();
connect_basic_blocks();
bool modified = false;
do {
find_dominators();
find_idominators();
modified = connect_break_targets();
} while (modified == true);
if ( g_debug_execution>=50 ) {
print_basic_blocks();
print_basic_block_links();
print_basic_block_dot();
}
if ( g_debug_execution>=2 ) {
print_dominators();
}
find_postdominators();
find_ipostdominators();
if ( g_debug_execution>=50 ) {
print_postdominators();
print_ipostdominators();
}
printf("GPGPU-Sim PTX: pre-decoding instructions for \'%s\'...\n", m_name.c_str() );
for ( unsigned ii=0; ii < n; ii += m_instr_mem[ii]->inst_size() ) { // handle branch instructions
ptx_instruction *pI = m_instr_mem[ii];
pI->pre_decode();
}
printf("GPGPU-Sim PTX: ... done pre-decoding instructions for \'%s\'.\n", m_name.c_str() );
fflush(stdout);
m_assembled = true;
}
addr_t shared_to_generic( unsigned smid, addr_t addr )
{
assert( addr < SHARED_MEM_SIZE_MAX );
return SHARED_GENERIC_START + smid*SHARED_MEM_SIZE_MAX + addr;
}
addr_t global_to_generic( addr_t addr )
{
return addr;
}
bool isspace_shared( unsigned smid, addr_t addr )
{
addr_t start = SHARED_GENERIC_START + smid*SHARED_MEM_SIZE_MAX;
addr_t end = SHARED_GENERIC_START + (smid+1)*SHARED_MEM_SIZE_MAX;
if( (addr >= end) || (addr < start) )
return false;
return true;
}
bool isspace_global( addr_t addr )
{
return (addr >= GLOBAL_HEAP_START) || (addr < STATIC_ALLOC_LIMIT);
}
memory_space_t whichspace( addr_t addr )
{
if( (addr >= GLOBAL_HEAP_START) || (addr < STATIC_ALLOC_LIMIT) ) {
return global_space;
} else if( addr >= SHARED_GENERIC_START ) {
return shared_space;
} else {
return local_space;
}
}
addr_t generic_to_shared( unsigned smid, addr_t addr )
{
assert(isspace_shared(smid,addr));
return addr - (SHARED_GENERIC_START + smid*SHARED_MEM_SIZE_MAX);
}
addr_t local_to_generic( unsigned smid, unsigned hwtid, addr_t addr )
{
assert(addr < LOCAL_MEM_SIZE_MAX);
return LOCAL_GENERIC_START + (TOTAL_LOCAL_MEM_PER_SM * smid) + (LOCAL_MEM_SIZE_MAX * hwtid) + addr;
}
bool isspace_local( unsigned smid, unsigned hwtid, addr_t addr )
{
addr_t start = LOCAL_GENERIC_START + (TOTAL_LOCAL_MEM_PER_SM * smid) + (LOCAL_MEM_SIZE_MAX * hwtid);
addr_t end = LOCAL_GENERIC_START + (TOTAL_LOCAL_MEM_PER_SM * smid) + (LOCAL_MEM_SIZE_MAX * (hwtid+1));
if( (addr >= end) || (addr < start) )
return false;
return true;
}
addr_t generic_to_local( unsigned smid, unsigned hwtid, addr_t addr )
{
assert(isspace_local(smid,hwtid,addr));
return addr - (LOCAL_GENERIC_START + (TOTAL_LOCAL_MEM_PER_SM * smid) + (LOCAL_MEM_SIZE_MAX * hwtid));
}
addr_t generic_to_global( addr_t addr )
{
return addr;
}
void* gpgpu_t::gpu_malloc( size_t size )
{
unsigned long long result = m_dev_malloc;
if(g_debug_execution >= 3) {
printf("GPGPU-Sim PTX: allocating %zu bytes on GPU starting at address 0x%Lx\n", size, m_dev_malloc );
fflush(stdout);
}
m_dev_malloc += size;
if (size%256) m_dev_malloc += (256 - size%256); //align to 256 byte boundaries
return(void*) result;
}
void* gpgpu_t::gpu_mallocarray( size_t size )
{
unsigned long long result = m_dev_malloc;
if(g_debug_execution >= 3) {
printf("GPGPU-Sim PTX: allocating %zu bytes on GPU starting at address 0x%Lx\n", size, m_dev_malloc );
fflush(stdout);
}
m_dev_malloc += size;
if (size%256) m_dev_malloc += (256 - size%256); //align to 256 byte boundaries
return(void*) result;
}
void gpgpu_t::memcpy_to_gpu( size_t dst_start_addr, const void *src, size_t count )
{
if(g_debug_execution >= 3) {
printf("GPGPU-Sim PTX: copying %zu bytes from CPU[0x%Lx] to GPU[0x%Lx] ... ", count, (unsigned long long) src, (unsigned long long) dst_start_addr );
fflush(stdout);
}
char *src_data = (char*)src;
for (unsigned n=0; n < count; n ++ )
m_global_mem->write(dst_start_addr+n,1, src_data+n,NULL,NULL);
if(g_debug_execution >= 3) {
printf( " done.\n");
fflush(stdout);
}
}
void gpgpu_t::memcpy_from_gpu( void *dst, size_t src_start_addr, size_t count )
{
if(g_debug_execution >= 3) {
printf("GPGPU-Sim PTX: copying %zu bytes from GPU[0x%Lx] to CPU[0x%Lx] ...", count, (unsigned long long) src_start_addr, (unsigned long long) dst );
fflush(stdout);
}
unsigned char *dst_data = (unsigned char*)dst;
for (unsigned n=0; n < count; n ++ )
m_global_mem->read(src_start_addr+n,1,dst_data+n);
if(g_debug_execution >= 3) {
printf( " done.\n");
fflush(stdout);
}
}
void gpgpu_t::memcpy_gpu_to_gpu( size_t dst, size_t src, size_t count )
{
if(g_debug_execution >= 3) {
printf("GPGPU-Sim PTX: copying %zu bytes from GPU[0x%Lx] to GPU[0x%Lx] ...", count,
(unsigned long long) src, (unsigned long long) dst );
fflush(stdout);
}
for (unsigned n=0; n < count; n ++ ) {
unsigned char tmp;
m_global_mem->read(src+n,1,&tmp);
m_global_mem->write(dst+n,1, &tmp,NULL,NULL);
}
if(g_debug_execution >= 3) {
printf( " done.\n");
fflush(stdout);
}
}
void gpgpu_t::gpu_memset( size_t dst_start_addr, int c, size_t count )
{
if(g_debug_execution >= 3) {
printf("GPGPU-Sim PTX: setting %zu bytes of memory to 0x%x starting at 0x%Lx... ",
count, (unsigned char) c, (unsigned long long) dst_start_addr );
fflush(stdout);
}
unsigned char c_value = (unsigned char)c;
for (unsigned n=0; n < count; n ++ )
m_global_mem->write(dst_start_addr+n,1,&c_value,NULL,NULL);
if(g_debug_execution >= 3) {
printf( " done.\n");
fflush(stdout);
}
}
void ptx_print_insn( address_type pc, FILE *fp )
{
std::map<unsigned,function_info*>::iterator f = g_pc_to_finfo.find(pc);
if( f == g_pc_to_finfo.end() ) {
fprintf(fp,"<no instruction at address 0x%x>", pc );
return;
}
function_info *finfo = f->second;
assert( finfo );
finfo->print_insn(pc,fp);
}
std::string ptx_get_insn_str( address_type pc )
{
std::map<unsigned,function_info*>::iterator f = g_pc_to_finfo.find(pc);
if( f == g_pc_to_finfo.end() ) {
#define STR_SIZE 255
char buff[STR_SIZE];
buff[STR_SIZE - 1] = '\0';
snprintf(buff, STR_SIZE,"<no instruction at address 0x%x>", pc );
return std::string(buff);
}
function_info *finfo = f->second;
assert( finfo );
return finfo->get_insn_str(pc);
}
void ptx_instruction::set_fp_or_int_archop(){
oprnd_type=UN_OP;
if((m_opcode == MEMBAR_OP)||(m_opcode == SSY_OP )||(m_opcode == BRA_OP) || (m_opcode == BAR_OP) || (m_opcode == RET_OP) || (m_opcode == RETP_OP) || (m_opcode == NOP_OP) || (m_opcode == EXIT_OP) || (m_opcode == CALLP_OP) || (m_opcode == CALL_OP)){
// do nothing
}else if((m_opcode == CVT_OP || m_opcode == SET_OP || m_opcode == SLCT_OP)){
if(get_type2()==F16_TYPE || get_type2()==F32_TYPE || get_type2() == F64_TYPE || get_type2() == FF64_TYPE){
oprnd_type= FP_OP;
}else oprnd_type=INT_OP;
}else{
if(get_type()==F16_TYPE || get_type()==F32_TYPE || get_type() == F64_TYPE || get_type() == FF64_TYPE){
oprnd_type= FP_OP;
}else oprnd_type=INT_OP;
}
}
void ptx_instruction::set_mul_div_or_other_archop(){
sp_op=OTHER_OP;
if((m_opcode != MEMBAR_OP) && (m_opcode != SSY_OP) && (m_opcode != BRA_OP) && (m_opcode != BAR_OP) && (m_opcode != EXIT_OP) && (m_opcode != NOP_OP) && (m_opcode != RETP_OP) && (m_opcode != RET_OP) && (m_opcode != CALLP_OP) && (m_opcode != CALL_OP)){
if(get_type()==F32_TYPE || get_type() == F64_TYPE || get_type() == FF64_TYPE){
switch(get_opcode()){
case MUL_OP:
case MAD_OP:
sp_op=FP_MUL_OP;
break;
case DIV_OP:
sp_op=FP_DIV_OP;
break;
case LG2_OP:
sp_op=FP_LG_OP;
break;
case RSQRT_OP:
case SQRT_OP:
sp_op=FP_SQRT_OP;
break;
case RCP_OP:
sp_op=FP_DIV_OP;
break;
case SIN_OP:
case COS_OP:
sp_op=FP_SIN_OP;
break;
case EX2_OP:
sp_op=FP_EXP_OP;
break;
default:
if(op==ALU_OP)
sp_op=FP__OP;
break;
}
}else {
switch(get_opcode()){
case MUL24_OP:
case MAD24_OP:
sp_op=INT_MUL24_OP;
break;
case MUL_OP:
case MAD_OP:
if(get_type()==U32_TYPE || get_type()==S32_TYPE || get_type()==B32_TYPE)
sp_op=INT_MUL32_OP;
else
sp_op=INT_MUL_OP;
break;
case DIV_OP:
sp_op=INT_DIV_OP;
break;
default:
if(op==ALU_OP)
sp_op=INT__OP;
break;
}
}
}
}
void ptx_instruction::set_bar_type()
{
if(m_opcode==BAR_OP) {
switch(m_barrier_op){
case SYNC_OPTION:
bar_type = SYNC;
break;
case ARRIVE_OPTION:
bar_type = ARRIVE;
break;
case RED_OPTION:
bar_type = RED;
switch(m_atomic_spec){
case ATOMIC_POPC:
red_type = POPC_RED;
break;
case ATOMIC_AND:
red_type = AND_RED;
break;
case ATOMIC_OR:
red_type = OR_RED;
break;
}
break;
default:
abort();
}
}
}
void ptx_instruction::set_opcode_and_latency()
{
unsigned int_latency[5];
unsigned fp_latency[5];
unsigned dp_latency[5];
unsigned int_init[5];
unsigned fp_init[5];
unsigned dp_init[5];
/*
* [0] ADD,SUB
* [1] MAX,Min
* [2] MUL
* [3] MAD
* [4] DIV
*/
sscanf(opcode_latency_int, "%u,%u,%u,%u,%u",
&int_latency[0],&int_latency[1],&int_latency[2],
&int_latency[3],&int_latency[4]);
sscanf(opcode_latency_fp, "%u,%u,%u,%u,%u",
&fp_latency[0],&fp_latency[1],&fp_latency[2],
&fp_latency[3],&fp_latency[4]);
sscanf(opcode_latency_dp, "%u,%u,%u,%u,%u",
&dp_latency[0],&dp_latency[1],&dp_latency[2],
&dp_latency[3],&dp_latency[4]);
sscanf(opcode_initiation_int, "%u,%u,%u,%u,%u",
&int_init[0],&int_init[1],&int_init[2],
&int_init[3],&int_init[4]);
sscanf(opcode_initiation_fp, "%u,%u,%u,%u,%u",
&fp_init[0],&fp_init[1],&fp_init[2],
&fp_init[3],&fp_init[4]);
sscanf(opcode_initiation_dp, "%u,%u,%u,%u,%u",
&dp_init[0],&dp_init[1],&dp_init[2],
&dp_init[3],&dp_init[4]);
sscanf(cdp_latency_str, "%u,%u,%u,%u,%u",
&cdp_latency[0],&cdp_latency[1],&cdp_latency[2],
&cdp_latency[3],&cdp_latency[4]);
if(!m_operands.empty()){
std::vector<operand_info>::iterator it;
for(it=++m_operands.begin();it!=m_operands.end();it++){
num_operands++;
if((it->is_reg() || it->is_vector())){
num_regs++;
}
}
}
op = ALU_OP;
mem_op= NOT_TEX;
initiation_interval = latency = 1;
switch( m_opcode ) {
case MOV_OP:
assert( !(has_memory_read() && has_memory_write()) );
if ( has_memory_read() ) op = LOAD_OP;
if ( has_memory_write() ) op = STORE_OP;
break;
case LD_OP: op = LOAD_OP; break;
case LDU_OP: op = LOAD_OP; break;
case ST_OP: op = STORE_OP; break;
case BRA_OP: op = BRANCH_OP; break;
case BREAKADDR_OP: op = BRANCH_OP; break;
case TEX_OP: op = LOAD_OP; mem_op=TEX; break;
case ATOM_OP: op = LOAD_OP; break;
case BAR_OP: op = BARRIER_OP; break;
case MEMBAR_OP: op = MEMORY_BARRIER_OP; break;
case CALL_OP:
{
if(m_is_printf || m_is_cdp) {
op = ALU_OP;
}
else
op = CALL_OPS;
break;
}
case CALLP_OP:
{
if(m_is_printf || m_is_cdp) {
op = ALU_OP;
}
else
op = CALL_OPS;
break;
}
case RET_OP: case RETP_OP: op = RET_OPS;break;
case ADD_OP: case ADDP_OP: case ADDC_OP: case SUB_OP: case SUBC_OP:
//ADD,SUB latency
switch(get_type()){
case F32_TYPE:
latency = fp_latency[0];
initiation_interval = fp_init[0];
break;
case F64_TYPE:
case FF64_TYPE:
latency = dp_latency[0];
initiation_interval = dp_init[0];
break;
case B32_TYPE:
case U32_TYPE:
case S32_TYPE:
default: //Use int settings for default
latency = int_latency[0];
initiation_interval = int_init[0];
break;
}
break;
case MAX_OP: case MIN_OP:
//MAX,MIN latency
switch(get_type()){
case F32_TYPE:
latency = fp_latency[1];
initiation_interval = fp_init[1];
break;
case F64_TYPE:
case FF64_TYPE:
latency = dp_latency[1];
initiation_interval = dp_init[1];
break;
case B32_TYPE:
case U32_TYPE:
case S32_TYPE:
default: //Use int settings for default
latency = int_latency[1];
initiation_interval = int_init[1];
break;
}
break;
case MUL_OP:
//MUL latency
switch(get_type()){
case F32_TYPE:
latency = fp_latency[2];
initiation_interval = fp_init[2];
op = ALU_SFU_OP;
break;
case F64_TYPE:
case FF64_TYPE:
latency = dp_latency[2];
initiation_interval = dp_init[2];
op = ALU_SFU_OP;
break;
case B32_TYPE:
case U32_TYPE:
case S32_TYPE:
default: //Use int settings for default
latency = int_latency[2];
initiation_interval = int_init[2];
op = SFU_OP;
break;
}
break;
case MAD_OP: case MADC_OP: case MADP_OP:
//MAD latency
switch(get_type()){
case F32_TYPE:
latency = fp_latency[3];
initiation_interval = fp_init[3];
break;
case F64_TYPE:
case FF64_TYPE:
latency = dp_latency[3];
initiation_interval = dp_init[3];
break;
case B32_TYPE:
case U32_TYPE:
case S32_TYPE:
default: //Use int settings for default
latency = int_latency[3];
initiation_interval = int_init[3];
op = SFU_OP;
break;
}
break;
case DIV_OP:
// Floating point only
op = SFU_OP;
switch(get_type()){
case F32_TYPE:
latency = fp_latency[4];
initiation_interval = fp_init[4];
break;
case F64_TYPE:
case FF64_TYPE:
latency = dp_latency[4];
initiation_interval = dp_init[4];
break;
case B32_TYPE:
case U32_TYPE:
case S32_TYPE:
default: //Use int settings for default
latency = int_latency[4];
initiation_interval = int_init[4];
break;
}
break;
case SQRT_OP: case SIN_OP: case COS_OP: case EX2_OP: case LG2_OP: case RSQRT_OP: case RCP_OP:
//Using double to approximate those
latency = dp_latency[2];
initiation_interval = dp_init[2];
op = SFU_OP;
break;
case SHFL_OP:
latency = 32;
initiation_interval = 15;
break;
default:
break;
}
set_fp_or_int_archop();
set_mul_div_or_other_archop();
}
void ptx_thread_info::ptx_fetch_inst( inst_t &inst ) const
{
addr_t pc = get_pc();
const ptx_instruction *pI = m_func_info->get_instruction(pc);
inst = (const inst_t&)*pI;
assert( inst.valid() );
}
static unsigned datatype2size( unsigned data_type )
{
unsigned data_size;
switch ( data_type ) {
case B8_TYPE:
case S8_TYPE:
case U8_TYPE:
data_size = 1; break;
case B16_TYPE:
case S16_TYPE:
case U16_TYPE:
case F16_TYPE:
data_size = 2; break;
case B32_TYPE:
case S32_TYPE:
case U32_TYPE:
case F32_TYPE:
data_size = 4; break;
case B64_TYPE:
case BB64_TYPE:
case S64_TYPE:
case U64_TYPE:
case F64_TYPE:
case FF64_TYPE:
data_size = 8; break;
case BB128_TYPE:
data_size = 16; break;
default: assert(0); break;
}
return data_size;
}
void ptx_instruction::pre_decode()
{
pc = m_PC;
isize = m_inst_size;
for( unsigned i=0; i<4; i++) {
out[i] = 0;
in[i] = 0;
}
is_vectorin = 0;
is_vectorout = 0;
std::fill_n(arch_reg.src, MAX_REG_OPERANDS, -1);
std::fill_n(arch_reg.dst, MAX_REG_OPERANDS, -1);
pred = 0;
ar1 = 0;
ar2 = 0;
space = m_space_spec;
memory_op = no_memory_op;
data_size = 0;
if ( has_memory_read() || has_memory_write() ) {
unsigned to_type = get_type();
data_size = datatype2size(to_type);
memory_op = has_memory_read() ? memory_load : memory_store;
}
bool has_dst = false ;
switch ( get_opcode() ) {
#define OP_DEF(OP,FUNC,STR,DST,CLASSIFICATION) case OP: has_dst = (DST!=0); break;
#define OP_W_DEF(OP,FUNC,STR,DST,CLASSIFICATION) case OP: has_dst = (DST!=0); break;
#include "opcodes.def"
#undef OP_DEF
#undef OP_W_DEF
default:
printf( "Execution error: Invalid opcode (0x%x)\n", get_opcode() );
break;
}
switch( m_cache_option ) {
case CA_OPTION: cache_op = CACHE_ALL; break;
case CG_OPTION: cache_op = CACHE_GLOBAL; break;
case CS_OPTION: cache_op = CACHE_STREAMING; break;
case LU_OPTION: cache_op = CACHE_LAST_USE; break;
case CV_OPTION: cache_op = CACHE_VOLATILE; break;
case WB_OPTION: cache_op = CACHE_WRITE_BACK; break;
case WT_OPTION: cache_op = CACHE_WRITE_THROUGH; break;
default:
if( m_opcode == LD_OP || m_opcode == LDU_OP )
cache_op = CACHE_ALL;
else if( m_opcode == ST_OP )
cache_op = CACHE_WRITE_BACK;
else if( m_opcode == ATOM_OP )
cache_op = CACHE_GLOBAL;
break;
}
set_opcode_and_latency();
set_bar_type();
// Get register operands
int n=0,m=0;
ptx_instruction::const_iterator opr=op_iter_begin();
for ( ; opr != op_iter_end(); opr++, n++ ) { //process operands
const operand_info &o = *opr;
if ( has_dst && n==0 ) {
// Do not set the null register "_" as an architectural register
if ( o.is_reg() && !o.is_non_arch_reg() ) {
out[0] = o.reg_num();
arch_reg.dst[0] = o.arch_reg_num();
} else if ( o.is_vector() ) {
is_vectorin = 1;
unsigned num_elem = o.get_vect_nelem();
if( num_elem >= 1 ) out[0] = o.reg1_num();
if( num_elem >= 2 ) out[1] = o.reg2_num();
if( num_elem >= 3 ) out[2] = o.reg3_num();
if( num_elem >= 4 ) out[3] = o.reg4_num();
for (int i = 0; i < num_elem; i++)
arch_reg.dst[i] = o.arch_reg_num(i);
}
} else {
if ( o.is_reg() && !o.is_non_arch_reg() ) {
int reg_num = o.reg_num();
arch_reg.src[m] = o.arch_reg_num();
switch ( m ) {
case 0: in[0] = reg_num; break;
case 1: in[1] = reg_num; break;
case 2: in[2] = reg_num; break;
default: break;
}
m++;
} else if ( o.is_vector() ) {
//assert(m == 0); //only support 1 vector operand (for textures) right now
is_vectorout = 1;
unsigned num_elem = o.get_vect_nelem();
if( num_elem >= 1 ) in[0] = o.reg1_num();
if( num_elem >= 2 ) in[1] = o.reg2_num();
if( num_elem >= 3 ) in[2] = o.reg3_num();
if( num_elem >= 4 ) in[3] = o.reg4_num();
for (int i = 0; i < num_elem; i++)
arch_reg.src[i] = o.arch_reg_num(i);
m+=4;
}
}
}
// Get predicate
if(has_pred()) {
const operand_info &p = get_pred();
pred = p.reg_num();
}
// Get address registers inside memory operands.
// Assuming only one memory operand per instruction,
// and maximum of two address registers for one memory operand.
if( has_memory_read() || has_memory_write() ) {
ptx_instruction::const_iterator op=op_iter_begin();
for ( ; op != op_iter_end(); op++, n++ ) { //process operands
const operand_info &o = *op;
if(o.is_memory_operand()) {
// We do not support the null register as a memory operand
assert( !o.is_non_arch_reg() );
// Check PTXPlus-type operand
// memory operand with addressing (ex. s[0x4] or g[$r1])
if(o.is_memory_operand2()) {
// memory operand with one address register (ex. g[$r1+0x4] or s[$r2+=0x4])
if(o.get_double_operand_type() == 0 || o.get_double_operand_type() == 3){
ar1 = o.reg_num();
arch_reg.src[4] = o.arch_reg_num();
// TODO: address register in $r2+=0x4 should be an output register as well
}
// memory operand with two address register (ex. s[$r1+$r1] or g[$r1+=$r2])
else if(o.get_double_operand_type() == 1 || o.get_double_operand_type() == 2) {
ar1 = o.reg1_num();
arch_reg.src[4] = o.arch_reg_num();
ar2 = o.reg2_num();
arch_reg.src[5] = o.arch_reg_num();
// TODO: first address register in $r1+=$r2 should be an output register as well
}
}
else if(o.is_immediate_address()){
}
// Regular PTX operand
else if (o.get_symbol()->type()->get_key().is_reg()) { // Memory operand contains a register
ar1 = o.reg_num();
arch_reg.src[4] = o.arch_reg_num();
}
}
}
}
// get reconvergence pc
reconvergence_pc = get_converge_point(pc);
m_decoded=true;
}
void function_info::add_param_name_type_size( unsigned index, std::string name, int type, size_t size, bool ptr, memory_space_t space )
{
unsigned parsed_index;
char buffer[2048];