diff options
| -rw-r--r-- | src/cuda-sim/Makefile | 156 | ||||
| -rw-r--r-- | src/cuda-sim/cuda-sim.cc | 11 | ||||
| -rw-r--r-- | src/cuda-sim/cuda-sim.h | 4 | ||||
| -rw-r--r-- | src/cuda-sim/instructions.cc | 1 | ||||
| -rw-r--r-- | src/cuda-sim/ptx.y | 2 | ||||
| -rw-r--r-- | src/cuda-sim/ptx_ir.cc | 675 | ||||
| -rw-r--r-- | src/cuda-sim/ptx_ir.h | 78 | ||||
| -rw-r--r-- | src/cuda-sim/ptx_loader.cc | 29 | ||||
| -rw-r--r-- | src/cuda-sim/ptx_parser.cc | 742 | ||||
| -rw-r--r-- | src/cuda-sim/ptx_parser.h | 122 | ||||
| -rw-r--r-- | src/gpgpu-sim/shader.cc | 1 | ||||
| -rw-r--r-- | src/gpgpusim_entrypoint.cc | 2 |
12 files changed, 1055 insertions, 768 deletions
diff --git a/src/cuda-sim/Makefile b/src/cuda-sim/Makefile index 6140048..597f7c4 100644 --- a/src/cuda-sim/Makefile +++ b/src/cuda-sim/Makefile @@ -91,7 +91,7 @@ ifeq ($(GNUC_CPP0X),1) endif endif -OBJS := ptx_loader.o cuda_device_printf.o instructions.o cuda-sim.o ptx_ir.o ptx_sim.o memory.o ptx-stats.o ptx.tab.o lex.ptx_.o ptxinfo.tab.o lex.ptxinfo_.o +OBJS := ptx_parser.o ptx_loader.o cuda_device_printf.o instructions.o cuda-sim.o ptx_ir.o ptx_sim.o memory.o ptx-stats.o ptx.tab.o lex.ptx_.o ptxinfo.tab.o lex.ptxinfo_.o CUDART_VERSION:=$(shell nvcc --version | awk '/release/ {print $$5;}' | sed 's/,//' | sed 's/\./ /' | awk '{printf("%02u%02u", 10*int($$1), 10*$$2);}') @@ -166,7 +166,159 @@ depend: instructions.o: instructions.h ptx.tab.c cuda_device_printf.o: ptx.tab.c ptx_ir.o: ptx.tab.c ptx_parser_decode.def -ptx_loader.o: ptx.tab.c +ptx_loader.o: ptx.tab.c ptx_parser_decode.def +ptx_parser.o: ptx.tab.c ptx_parser_decode.def # DO NOT DELETE +cuda_device_printf.o: ptx_ir.h ../abstract_hardware_model.h +cuda_device_printf.o: /usr/include/assert.h /usr/include/features.h +cuda_device_printf.o: /usr/include/sys/cdefs.h /usr/include/bits/wordsize.h +cuda_device_printf.o: /usr/include/gnu/stubs.h /usr/include/gnu/stubs-64.h +cuda_device_printf.o: ptx_sim.h /usr/include/stdlib.h +cuda_device_printf.o: /usr/include/sys/types.h /usr/include/bits/types.h +cuda_device_printf.o: /usr/include/bits/typesizes.h /usr/include/time.h +cuda_device_printf.o: /usr/include/endian.h /usr/include/bits/endian.h +cuda_device_printf.o: /usr/include/bits/byteswap.h /usr/include/sys/select.h +cuda_device_printf.o: /usr/include/bits/select.h /usr/include/bits/sigset.h +cuda_device_printf.o: /usr/include/bits/time.h /usr/include/sys/sysmacros.h +cuda_device_printf.o: /usr/include/bits/pthreadtypes.h /usr/include/alloca.h +cuda_device_printf.o: dram_callback.h opcodes.h opcodes.def memory.h +cuda_device_printf.o: /usr/include/string.h /usr/include/stdio.h +cuda_device_printf.o: /usr/include/libio.h /usr/include/_G_config.h +cuda_device_printf.o: /usr/include/wchar.h /usr/include/bits/stdio_lim.h +cuda_device_printf.o: /usr/include/bits/sys_errlist.h ../option_parser.h +cuda-sim.o: cuda-sim.h ../abstract_hardware_model.h /usr/include/stdlib.h +cuda-sim.o: /usr/include/features.h /usr/include/sys/cdefs.h +cuda-sim.o: /usr/include/bits/wordsize.h /usr/include/gnu/stubs.h +cuda-sim.o: /usr/include/gnu/stubs-64.h /usr/include/sys/types.h +cuda-sim.o: /usr/include/bits/types.h /usr/include/bits/typesizes.h +cuda-sim.o: /usr/include/time.h /usr/include/endian.h +cuda-sim.o: /usr/include/bits/endian.h /usr/include/bits/byteswap.h +cuda-sim.o: /usr/include/sys/select.h /usr/include/bits/select.h +cuda-sim.o: /usr/include/bits/sigset.h /usr/include/bits/time.h +cuda-sim.o: /usr/include/sys/sysmacros.h /usr/include/bits/pthreadtypes.h +cuda-sim.o: /usr/include/alloca.h ptx_ir.h /usr/include/assert.h ptx_sim.h +cuda-sim.o: dram_callback.h opcodes.h opcodes.def memory.h +cuda-sim.o: /usr/include/string.h /usr/include/stdio.h /usr/include/libio.h +cuda-sim.o: /usr/include/_G_config.h /usr/include/wchar.h +cuda-sim.o: /usr/include/bits/stdio_lim.h /usr/include/bits/sys_errlist.h +cuda-sim.o: ../option_parser.h ../intersim/statwraper.h ptx-stats.h +cuda-sim.o: ptx_loader.h ptx_parser.h +instructions.o: ptx_ir.h ../abstract_hardware_model.h /usr/include/assert.h +instructions.o: /usr/include/features.h /usr/include/sys/cdefs.h +instructions.o: /usr/include/bits/wordsize.h /usr/include/gnu/stubs.h +instructions.o: /usr/include/gnu/stubs-64.h ptx_sim.h /usr/include/stdlib.h +instructions.o: /usr/include/sys/types.h /usr/include/bits/types.h +instructions.o: /usr/include/bits/typesizes.h /usr/include/time.h +instructions.o: /usr/include/endian.h /usr/include/bits/endian.h +instructions.o: /usr/include/bits/byteswap.h /usr/include/sys/select.h +instructions.o: /usr/include/bits/select.h /usr/include/bits/sigset.h +instructions.o: /usr/include/bits/time.h /usr/include/sys/sysmacros.h +instructions.o: /usr/include/bits/pthreadtypes.h /usr/include/alloca.h +instructions.o: dram_callback.h opcodes.h opcodes.def memory.h +instructions.o: /usr/include/string.h /usr/include/stdio.h +instructions.o: /usr/include/libio.h /usr/include/_G_config.h +instructions.o: /usr/include/wchar.h /usr/include/bits/stdio_lim.h +instructions.o: /usr/include/bits/sys_errlist.h ../option_parser.h +instructions.o: /usr/include/math.h /usr/include/bits/huge_val.h +instructions.o: /usr/include/bits/mathdef.h /usr/include/bits/mathcalls.h +instructions.o: /usr/include/fenv.h /usr/include/bits/fenv.h cuda-math.h +instructions.o: ptx_loader.h +memory.o: memory.h ../abstract_hardware_model.h /usr/include/assert.h +memory.o: /usr/include/features.h /usr/include/sys/cdefs.h +memory.o: /usr/include/bits/wordsize.h /usr/include/gnu/stubs.h +memory.o: /usr/include/gnu/stubs-64.h /usr/include/string.h +memory.o: /usr/include/stdio.h /usr/include/bits/types.h +memory.o: /usr/include/bits/typesizes.h /usr/include/libio.h +memory.o: /usr/include/_G_config.h /usr/include/wchar.h +memory.o: /usr/include/bits/stdio_lim.h /usr/include/bits/sys_errlist.h +memory.o: /usr/include/stdlib.h /usr/include/sys/types.h /usr/include/time.h +memory.o: /usr/include/endian.h /usr/include/bits/endian.h +memory.o: /usr/include/bits/byteswap.h /usr/include/sys/select.h +memory.o: /usr/include/bits/select.h /usr/include/bits/sigset.h +memory.o: /usr/include/bits/time.h /usr/include/sys/sysmacros.h +memory.o: /usr/include/bits/pthreadtypes.h /usr/include/alloca.h ../debug.h +memory.o: ../abstract_hardware_model.h +ptx_ir.o: ptx_parser.h ../abstract_hardware_model.h ptx_ir.h +ptx_ir.o: /usr/include/assert.h /usr/include/features.h +ptx_ir.o: /usr/include/sys/cdefs.h /usr/include/bits/wordsize.h +ptx_ir.o: /usr/include/gnu/stubs.h /usr/include/gnu/stubs-64.h ptx_sim.h +ptx_ir.o: /usr/include/stdlib.h /usr/include/sys/types.h +ptx_ir.o: /usr/include/bits/types.h /usr/include/bits/typesizes.h +ptx_ir.o: /usr/include/time.h /usr/include/endian.h +ptx_ir.o: /usr/include/bits/endian.h /usr/include/bits/byteswap.h +ptx_ir.o: /usr/include/sys/select.h /usr/include/bits/select.h +ptx_ir.o: /usr/include/bits/sigset.h /usr/include/bits/time.h +ptx_ir.o: /usr/include/sys/sysmacros.h /usr/include/bits/pthreadtypes.h +ptx_ir.o: /usr/include/alloca.h dram_callback.h opcodes.h opcodes.def +ptx_ir.o: memory.h /usr/include/string.h /usr/include/stdio.h +ptx_ir.o: /usr/include/libio.h /usr/include/_G_config.h /usr/include/wchar.h +ptx_ir.o: /usr/include/bits/stdio_lim.h /usr/include/bits/sys_errlist.h +ptx_ir.o: ../option_parser.h cuda-sim.h +ptx_loader.o: ptx_loader.h ptx_ir.h ../abstract_hardware_model.h +ptx_loader.o: /usr/include/assert.h /usr/include/features.h +ptx_loader.o: /usr/include/sys/cdefs.h /usr/include/bits/wordsize.h +ptx_loader.o: /usr/include/gnu/stubs.h /usr/include/gnu/stubs-64.h ptx_sim.h +ptx_loader.o: /usr/include/stdlib.h /usr/include/sys/types.h +ptx_loader.o: /usr/include/bits/types.h /usr/include/bits/typesizes.h +ptx_loader.o: /usr/include/time.h /usr/include/endian.h +ptx_loader.o: /usr/include/bits/endian.h /usr/include/bits/byteswap.h +ptx_loader.o: /usr/include/sys/select.h /usr/include/bits/select.h +ptx_loader.o: /usr/include/bits/sigset.h /usr/include/bits/time.h +ptx_loader.o: /usr/include/sys/sysmacros.h /usr/include/bits/pthreadtypes.h +ptx_loader.o: /usr/include/alloca.h dram_callback.h opcodes.h opcodes.def +ptx_loader.o: memory.h /usr/include/string.h /usr/include/stdio.h +ptx_loader.o: /usr/include/libio.h /usr/include/_G_config.h +ptx_loader.o: /usr/include/wchar.h /usr/include/bits/stdio_lim.h +ptx_loader.o: /usr/include/bits/sys_errlist.h ../option_parser.h cuda-sim.h +ptx_loader.o: ptx_parser.h /usr/include/dirent.h /usr/include/bits/dirent.h +ptx_loader.o: /usr/include/bits/posix1_lim.h /usr/include/bits/local_lim.h +ptx_loader.o: /usr/include/linux/limits.h +ptx_parser.o: ptx_parser.h ../abstract_hardware_model.h ptx_ir.h +ptx_parser.o: /usr/include/assert.h /usr/include/features.h +ptx_parser.o: /usr/include/sys/cdefs.h /usr/include/bits/wordsize.h +ptx_parser.o: /usr/include/gnu/stubs.h /usr/include/gnu/stubs-64.h ptx_sim.h +ptx_parser.o: /usr/include/stdlib.h /usr/include/sys/types.h +ptx_parser.o: /usr/include/bits/types.h /usr/include/bits/typesizes.h +ptx_parser.o: /usr/include/time.h /usr/include/endian.h +ptx_parser.o: /usr/include/bits/endian.h /usr/include/bits/byteswap.h +ptx_parser.o: /usr/include/sys/select.h /usr/include/bits/select.h +ptx_parser.o: /usr/include/bits/sigset.h /usr/include/bits/time.h +ptx_parser.o: /usr/include/sys/sysmacros.h /usr/include/bits/pthreadtypes.h +ptx_parser.o: /usr/include/alloca.h dram_callback.h opcodes.h opcodes.def +ptx_parser.o: memory.h /usr/include/string.h /usr/include/stdio.h +ptx_parser.o: /usr/include/libio.h /usr/include/_G_config.h +ptx_parser.o: /usr/include/wchar.h /usr/include/bits/stdio_lim.h +ptx_parser.o: /usr/include/bits/sys_errlist.h ../option_parser.h +ptx_sim.o: ptx_sim.h /usr/include/stdlib.h /usr/include/features.h +ptx_sim.o: /usr/include/sys/cdefs.h /usr/include/bits/wordsize.h +ptx_sim.o: /usr/include/gnu/stubs.h /usr/include/gnu/stubs-64.h +ptx_sim.o: /usr/include/sys/types.h /usr/include/bits/types.h +ptx_sim.o: /usr/include/bits/typesizes.h /usr/include/time.h +ptx_sim.o: /usr/include/endian.h /usr/include/bits/endian.h +ptx_sim.o: /usr/include/bits/byteswap.h /usr/include/sys/select.h +ptx_sim.o: /usr/include/bits/select.h /usr/include/bits/sigset.h +ptx_sim.o: /usr/include/bits/time.h /usr/include/sys/sysmacros.h +ptx_sim.o: /usr/include/bits/pthreadtypes.h /usr/include/alloca.h +ptx_sim.o: dram_callback.h ../abstract_hardware_model.h /usr/include/assert.h +ptx_sim.o: opcodes.h opcodes.def ptx_ir.h memory.h /usr/include/string.h +ptx_sim.o: /usr/include/stdio.h /usr/include/libio.h /usr/include/_G_config.h +ptx_sim.o: /usr/include/wchar.h /usr/include/bits/stdio_lim.h +ptx_sim.o: /usr/include/bits/sys_errlist.h ../option_parser.h +ptx-stats.o: ptx_ir.h ../abstract_hardware_model.h /usr/include/assert.h +ptx-stats.o: /usr/include/features.h /usr/include/sys/cdefs.h +ptx-stats.o: /usr/include/bits/wordsize.h /usr/include/gnu/stubs.h +ptx-stats.o: /usr/include/gnu/stubs-64.h ptx_sim.h /usr/include/stdlib.h +ptx-stats.o: /usr/include/sys/types.h /usr/include/bits/types.h +ptx-stats.o: /usr/include/bits/typesizes.h /usr/include/time.h +ptx-stats.o: /usr/include/endian.h /usr/include/bits/endian.h +ptx-stats.o: /usr/include/bits/byteswap.h /usr/include/sys/select.h +ptx-stats.o: /usr/include/bits/select.h /usr/include/bits/sigset.h +ptx-stats.o: /usr/include/bits/time.h /usr/include/sys/sysmacros.h +ptx-stats.o: /usr/include/bits/pthreadtypes.h /usr/include/alloca.h +ptx-stats.o: dram_callback.h opcodes.h opcodes.def memory.h +ptx-stats.o: /usr/include/string.h /usr/include/stdio.h /usr/include/libio.h +ptx-stats.o: /usr/include/_G_config.h /usr/include/wchar.h +ptx-stats.o: /usr/include/bits/stdio_lim.h /usr/include/bits/sys_errlist.h +ptx-stats.o: ../option_parser.h ptx-stats.h diff --git a/src/cuda-sim/cuda-sim.cc b/src/cuda-sim/cuda-sim.cc index f5c283e..6a0ee36 100644 --- a/src/cuda-sim/cuda-sim.cc +++ b/src/cuda-sim/cuda-sim.cc @@ -78,6 +78,7 @@ #include "memory.h" #include "ptx-stats.h" #include "ptx_loader.h" +#include "ptx_parser.h" extern bool g_interactive_debugger_enabled; @@ -90,8 +91,6 @@ int g_debug_execution = 0; int g_debug_thread_uid = 0; addr_t g_debug_pc = 0xBEEF1518; -const char *g_filename; -bool g_debug_ir_generation = false; unsigned g_ptx_sim_num_insn = 0; std::map<const struct textureReference*,const struct cudaArray*> TextureToArrayMap; // texture bindings @@ -1113,7 +1112,6 @@ void init_inst_classification_stat() { g_inst_op_classification_stat[g_ptx_kernel_count] = StatCreate(kernelname,1,100); } -unsigned g_max_regs_per_thread = 0; std::map<std::string,function_info*> *g_kernel_name_to_function_lookup=NULL; std::map<std::string,symbol_table*> g_kernel_name_to_symtab_lookup; @@ -1211,8 +1209,6 @@ void gpgpu_ptx_sim_register_kernel(const char *hostFun, const char *deviceFun) printf("GPGPU-Sim PTX: __cudaRegisterFunction %s : 0x%Lx\n", deviceFun, (unsigned long long)hostFun); } -extern int ptx_lineno; - void register_ptx_function( const char *name, function_info *impl, symbol_table *symtab ) { printf("GPGPU-Sim PTX: parsing function %s\n", name ); @@ -1328,7 +1324,6 @@ void read_sim_environment_variables() { ptx_debug = 0; g_debug_execution = 0; - g_debug_ir_generation = false; g_interactive_debugger_enabled = false; char *mode = getenv("PTX_SIM_MODE_FUNC"); @@ -1336,7 +1331,6 @@ void read_sim_environment_variables() sscanf(mode,"%u", &g_ptx_sim_mode); printf("GPGPU-Sim PTX: simulation mode %d (can change with PTX_SIM_MODE_FUNC environment variable:\n", g_ptx_sim_mode); printf(" 1=functional simulation only, 0=detailed performance simulator)\n"); - g_filename = getenv("PTX_SIM_KERNELFILE"); char *dbg_inter = getenv("GPGPUSIM_DEBUG"); if ( dbg_inter && strlen(dbg_inter) ) { printf("GPGPU-Sim PTX: enabling interactive debugger\n"); @@ -1377,9 +1371,6 @@ void read_sim_environment_variables() if ( g_debug_execution >= 40 ) { ptx_debug = 1; } - if ( g_debug_execution >= 30 ) { - g_debug_ir_generation = true; - } } diff --git a/src/cuda-sim/cuda-sim.h b/src/cuda-sim/cuda-sim.h index 2e67e15..6662b4c 100644 --- a/src/cuda-sim/cuda-sim.h +++ b/src/cuda-sim/cuda-sim.h @@ -15,9 +15,6 @@ extern int g_ptx_sim_mode; extern memory_space *g_global_mem; extern int g_debug_execution; extern int g_debug_thread_uid; -extern unsigned g_max_regs_per_thread; -extern bool g_debug_ir_generation; -extern const char *g_filename; extern std::map<std::string,function_info*> *g_kernel_name_to_function_lookup; extern std::map<std::string,symbol_table*> g_kernel_name_to_symtab_lookup; @@ -42,5 +39,6 @@ extern int gpgpu_ptx_sim_sizeofTexture(const char* name); extern const char* gpgpu_ptx_sim_findNamefromTexture(const struct textureReference* texref); extern const struct textureReference* gpgpu_ptx_sim_accessTextureofName(const char* name); extern void read_sim_environment_variables(); +extern void register_ptx_function( const char *name, function_info *impl, symbol_table *symtab ); #endif diff --git a/src/cuda-sim/instructions.cc b/src/cuda-sim/instructions.cc index b3a0342..0924eab 100644 --- a/src/cuda-sim/instructions.cc +++ b/src/cuda-sim/instructions.cc @@ -87,7 +87,6 @@ const char *g_opcode_string[NUM_OPCODES] = { #undef OP_DEF }; -extern std::map<unsigned,std::string> g_ptx_token_decode; extern std::map<struct textureReference*,struct cudaArray*> TextureToArrayMap; // texture bindings extern std::map<struct textureReference*,struct textureInfo*> TextureToInfoMap; // texture bindings extern std::map<std::string, struct textureReference*> NameToTextureMap; diff --git a/src/cuda-sim/ptx.y b/src/cuda-sim/ptx.y index f788e01..a1e1428 100644 --- a/src/cuda-sim/ptx.y +++ b/src/cuda-sim/ptx.y @@ -206,7 +206,7 @@ %type <ptr_value> function_decl %{ - #include "ptx_ir.h" + #include "ptx_parser.h" #include <stdlib.h> #include <string.h> #include <math.h> diff --git a/src/cuda-sim/ptx_ir.cc b/src/cuda-sim/ptx_ir.cc index 51ad05f..cd70daf 100644 --- a/src/cuda-sim/ptx_ir.cc +++ b/src/cuda-sim/ptx_ir.cc @@ -61,6 +61,7 @@ * Vancouver, BC V6T 1Z4 */ +#include "ptx_parser.h" #include "ptx_ir.h" #include "ptx.tab.h" #include "opcodes.h" @@ -69,612 +70,8 @@ #include <list> #include <assert.h> #include <algorithm> -#include <stdarg.h> #include "cuda-sim.h" -#include "../option_parser.h" - -extern "C" int ptx_error( const char *s ); -extern int ptx_lineno; - -// the program intermediate representation... -symbol_table *g_global_symbol_table = NULL; -std::map<std::string,symbol_table*> g_sym_name_to_symbol_table; -static symbol_table *g_current_symbol_table = NULL; -static std::list<ptx_instruction*> g_instructions; -static symbol *g_last_symbol = NULL; - -int g_error_detected = 0; - -// type specifier stuff: -memory_space_t g_space_spec = undefined_space; -int g_scalar_type_spec = -1; -int g_vector_spec = -1; -int g_alignment_spec = -1; -int g_extern_spec = 0; - -// variable declaration stuff: -type_info *g_var_type = NULL; - -// instruction definition stuff: -const symbol *g_pred; -int g_neg_pred; -symbol *g_label; -int g_opcode = -1; -std::list<operand_info> g_operands; -std::list<int> g_options; -std::list<int> g_scalar_type; - -#define DPRINTF(...) \ - if( g_debug_ir_generation ) { \ - printf(" %s:%u => ",g_filename,ptx_lineno); \ - printf(" (%s:%u) ", __FILE__, __LINE__); \ - printf(__VA_ARGS__); \ - printf("\n"); \ - fflush(stdout); \ - } - -unsigned g_entry_func_param_index=0; -function_info *g_func_info = NULL; -function_info *g_entrypoint_func_info = NULL; -symbol_table *g_entrypoint_symbol_table = NULL; -std::map<unsigned,std::string> g_ptx_token_decode; -operand_info g_return_var; - -void init_parser() -{ - g_global_symbol_table = g_current_symbol_table = new symbol_table("global",0,NULL); - ptx_lineno = 1; - -#define DEF(X,Y) g_ptx_token_decode[X] = Y; -#include "ptx_parser_decode.def" -#undef DEF -} - -void init_directive_state() -{ - DPRINTF("init_directive_state"); - g_space_spec=undefined_space; - g_scalar_type_spec=-1; - g_vector_spec=-1; - g_opcode=-1; - g_alignment_spec = -1; - g_extern_spec = 0; - g_scalar_type.clear(); - g_operands.clear(); - g_last_symbol = NULL; -} - -void init_instruction_state() -{ - DPRINTF("init_instruction_state"); - g_pred = NULL; - g_neg_pred = 0; - g_label = NULL; - g_opcode = -1; - g_options.clear(); - g_return_var = operand_info(); - init_directive_state(); -} - -extern void register_ptx_function( const char *name, function_info *impl, symbol_table *symtab ); - -static int g_entry_point; - -void start_function( int entry_point ) -{ - DPRINTF("start_function"); - init_directive_state(); - init_instruction_state(); - g_entry_point = entry_point; - g_func_info = NULL; - g_entry_func_param_index=0; -} - -char *g_add_identifier_cached__identifier = NULL; -int g_add_identifier_cached__array_dim; -int g_add_identifier_cached__array_ident; - -void add_function_name( const char *name ) -{ - DPRINTF("add_function_name %s %s", name, ((g_entry_point==1)?"(entrypoint)":((g_entry_point==2)?"(extern)":""))); - bool prior_decl = g_global_symbol_table->add_function_decl( name, g_entry_point, &g_func_info, &g_current_symbol_table ); - if( g_entry_point ) { - g_entrypoint_func_info = g_func_info; - g_entrypoint_symbol_table = g_current_symbol_table; - } - if( g_add_identifier_cached__identifier ) { - add_identifier( g_add_identifier_cached__identifier, - g_add_identifier_cached__array_dim, - g_add_identifier_cached__array_ident ); - free( g_add_identifier_cached__identifier ); - g_add_identifier_cached__identifier = NULL; - g_func_info->add_return_var( g_last_symbol ); - init_directive_state(); - } - if( prior_decl ) { - g_func_info->remove_args(); - } - g_global_symbol_table->add_function( g_func_info ); -} - -void add_directive() -{ - DPRINTF("add_directive"); - init_directive_state(); -} - -#define mymax(a,b) ((a)>(b)?(a):(b)) - -void end_function() -{ - DPRINTF("end_function"); - - init_directive_state(); - init_instruction_state(); - g_max_regs_per_thread = mymax( g_max_regs_per_thread, (g_current_symbol_table->next_reg_num()-1)); - g_func_info->add_inst( g_instructions ); - g_instructions.clear(); - gpgpu_ptx_assemble( g_func_info->get_name(), g_func_info ); - g_current_symbol_table = g_global_symbol_table; - - DPRINTF("function %s, PC = %d\n", g_func_info->get_name().c_str(), g_func_info->get_start_PC()); -} - -#define parse_error(msg, ...) parse_error_impl(__FILE__,__LINE__, msg, ##__VA_ARGS__) -#define parse_assert(cond,msg, ...) parse_assert_impl((cond),__FILE__,__LINE__, msg, ##__VA_ARGS__) - -void parse_error_impl( const char *file, unsigned line, const char *msg, ... ) -{ - va_list ap; - char buf[1024]; - va_start(ap,msg); - vsnprintf(buf,1024,msg,ap); - va_end(ap); - - g_error_detected = 1; - printf("%s:%u: Parse error: %s (%s:%u)\n\n", g_filename, ptx_lineno, buf, file, line); - ptx_error(NULL); - abort(); - exit(1); -} - -void parse_assert_impl( int test_value, const char *file, unsigned line, const char *msg, ... ) -{ - va_list ap; - char buf[1024]; - va_start(ap,msg); - vsnprintf(buf,1024,msg,ap); - va_end(ap); - - if ( test_value == 0 ) - parse_error_impl(file,line, msg); -} - -extern "C" char linebuf[1024]; - - -void set_return() -{ - parse_assert( (g_opcode == CALL_OP), "only call can have return value"); - g_operands.front().set_return(); - g_return_var = g_operands.front(); -} - -std::map<std::string,std::map<unsigned,const ptx_instruction*> > g_inst_lookup; - -const ptx_instruction *ptx_instruction_lookup( const char *filename, unsigned linenumber ) -{ - std::map<std::string,std::map<unsigned,const ptx_instruction*> >::iterator f=g_inst_lookup.find(filename); - if( f == g_inst_lookup.end() ) - return NULL; - std::map<unsigned,const ptx_instruction*>::iterator l=f->second.find(linenumber); - if( l == f->second.end() ) - return NULL; - return l->second; -} - -void add_instruction() -{ - DPRINTF("add_instruction: %s", ((g_opcode>0)?g_opcode_string[g_opcode]:"<label>") ); - ptx_instruction *i = new ptx_instruction( g_opcode, - g_pred, - g_neg_pred, - g_label, - g_operands, - g_return_var, - g_options, - g_scalar_type, - g_space_spec, - g_filename, - ptx_lineno, - linebuf ); - g_instructions.push_back(i); - g_inst_lookup[g_filename][ptx_lineno] = i; - init_instruction_state(); -} - -void add_variables() -{ - DPRINTF("add_variables"); - if ( !g_operands.empty() ) { - assert( g_last_symbol != NULL ); - g_last_symbol->add_initializer(g_operands); - } - init_directive_state(); -} - -void set_variable_type() -{ - DPRINTF("set_variable_type space_spec=%s scalar_type_spec=%s", - g_ptx_token_decode[g_space_spec.get_type()].c_str(), - g_ptx_token_decode[g_scalar_type_spec].c_str() ); - parse_assert( g_space_spec != undefined_space, "variable has no space specification" ); - parse_assert( g_scalar_type_spec != -1, "variable has no type information" ); // need to extend for structs? - g_var_type = g_current_symbol_table->add_type( g_space_spec, - g_scalar_type_spec, - g_vector_spec, - g_alignment_spec, - g_extern_spec ); -} - -bool check_for_duplicates( const char *identifier ) -{ - const symbol *s = g_current_symbol_table->lookup(identifier); - return ( s != NULL ); -} - -extern std::set<std::string> g_globals; -extern std::set<std::string> g_constants; - -int g_func_decl = 0; -int g_ident_add_uid = 0; -unsigned g_const_alloc = 1; - -void add_identifier( const char *identifier, int array_dim, unsigned array_ident ) -{ - if( g_func_decl && (g_func_info == NULL) ) { - // return variable decl... - assert( g_add_identifier_cached__identifier == NULL ); - g_add_identifier_cached__identifier = strdup(identifier); - g_add_identifier_cached__array_dim = array_dim; - g_add_identifier_cached__array_ident = array_ident; - return; - } - DPRINTF("add_identifier \"%s\" (%u)", identifier, g_ident_add_uid); - g_ident_add_uid++; - type_info *type = g_var_type; - type_info_key ti = type->get_key(); - int basic_type; - int regnum; - size_t num_bits; - unsigned addr, addr_pad; - ti.type_decode(num_bits,basic_type); - - bool duplicates = check_for_duplicates( identifier ); - if( duplicates ) { - symbol *s = g_current_symbol_table->lookup(identifier); - g_last_symbol = s; - if( g_func_decl ) - return; - std::string msg = std::string(identifier) + " was delcared previous at " + s->decl_location() + " skipping new declaration"; - printf("GPGPU-Sim PTX: Warning %s\n", msg.c_str()); - return; - } - - assert( g_var_type != NULL ); - switch ( array_ident ) { - case ARRAY_IDENTIFIER: - type = g_current_symbol_table->get_array_type(type,array_dim); - num_bits = array_dim * num_bits; - break; - case ARRAY_IDENTIFIER_NO_DIM: - type = g_current_symbol_table->get_array_type(type,(unsigned)-1); - num_bits = 0; - break; - default: - break; - } - g_last_symbol = g_current_symbol_table->add_variable(identifier,type,num_bits/8,g_filename,ptx_lineno); - switch ( ti.get_memory_space().get_type() ) { - case reg_space: { - regnum = g_current_symbol_table->next_reg_num(); - int arch_regnum = -1; - for (int d = 0; d < strlen(identifier); d++) { - if (isdigit(identifier[d])) { - sscanf(identifier + d, "%d", &arch_regnum); - break; - } - } - if (strcmp(identifier, "%sp") == 0) { - arch_regnum = 0; - } - g_last_symbol->set_regno(regnum, arch_regnum); - } break; - case shared_space: - printf("GPGPU-Sim PTX: allocating shared region for \"%s\" from 0x%x to 0x%lx (shared memory space)\n", - identifier, - g_current_symbol_table->get_shared_next(), - g_current_symbol_table->get_shared_next() + num_bits/8 ); - fflush(stdout); - assert( (num_bits%8) == 0 ); - addr = g_current_symbol_table->get_shared_next(); - addr_pad = num_bits ? (((num_bits/8) - (addr % (num_bits/8))) % (num_bits/8)) : 0; - g_last_symbol->set_address( addr+addr_pad ); - g_current_symbol_table->alloc_shared( num_bits/8 + addr_pad ); - break; - case const_space: - if( array_ident == ARRAY_IDENTIFIER_NO_DIM ) { - printf("GPGPU-Sim PTX: deferring allocation of constant region for \"%s\" (need size information)\n", identifier ); - } else { - printf("GPGPU-Sim PTX: allocating constant region for \"%s\" from 0x%x to 0x%lx (global memory space) %u\n", - identifier, - g_current_symbol_table->get_global_next(), - g_current_symbol_table->get_global_next() + num_bits/8, - g_const_alloc++ ); - fflush(stdout); - assert( (num_bits%8) == 0 ); - addr = g_current_symbol_table->get_global_next(); - addr_pad = num_bits ? (((num_bits/8) - (addr % (num_bits/8))) % (num_bits/8)) : 0; - g_last_symbol->set_address( addr + addr_pad ); - g_current_symbol_table->alloc_global( num_bits/8 + addr_pad ); - } - if( g_current_symbol_table == g_global_symbol_table ) { - g_constants.insert( identifier ); - } - assert( g_current_symbol_table != NULL ); - g_sym_name_to_symbol_table[ identifier ] = g_current_symbol_table; - break; - case global_space: - printf("GPGPU-Sim PTX: allocating global region for \"%s\" from 0x%x to 0x%lx (global memory space)\n", - identifier, - g_current_symbol_table->get_global_next(), - g_current_symbol_table->get_global_next() + num_bits/8 ); - fflush(stdout); - assert( (num_bits%8) == 0 ); - addr = g_current_symbol_table->get_global_next(); - addr_pad = num_bits ? (((num_bits/8) - (addr % (num_bits/8))) % (num_bits/8)) : 0; - g_last_symbol->set_address( addr+addr_pad ); - g_current_symbol_table->alloc_global( num_bits/8 + addr_pad ); - g_globals.insert( identifier ); - assert( g_current_symbol_table != NULL ); - g_sym_name_to_symbol_table[ identifier ] = g_current_symbol_table; - break; - case local_space: - if( g_func_info == NULL ) { - printf("GPGPU-Sim PTX: not allocating .local \"%s\" declared at global scope\n", identifier); - break; - } - printf("GPGPU-Sim PTX: allocating stack frame region for .local \"%s\" from 0x%x to 0x%lx\n", - identifier, - g_current_symbol_table->get_local_next(), - g_current_symbol_table->get_local_next() + num_bits/8 ); - fflush(stdout); - assert( (num_bits%8) == 0 ); - g_last_symbol->set_address( g_current_symbol_table->get_local_next() ); - g_current_symbol_table->alloc_local( num_bits/8 ); - g_func_info->set_framesize( g_current_symbol_table->get_local_next() ); - break; - case tex_space: - printf("GPGPU-Sim PTX: encountered texture directive %s.\n", identifier); - break; - case param_space_local: - printf("GPGPU-Sim PTX: allocating stack frame region for .param \"%s\" from 0x%x to 0x%lx\n", - identifier, - g_current_symbol_table->get_local_next(), - g_current_symbol_table->get_local_next() + num_bits/8 ); - fflush(stdout); - assert( (num_bits%8) == 0 ); - g_last_symbol->set_address( g_current_symbol_table->get_local_next() ); - g_current_symbol_table->alloc_local( num_bits/8 ); - g_func_info->set_framesize( g_current_symbol_table->get_local_next() ); - break; - case param_space_kernel: - break; - default: - abort(); - break; - } - - assert( !ti.is_param_unclassified() ); - if ( ti.is_param_kernel() ) { - g_func_info->add_param_name_type_size(g_entry_func_param_index,identifier, ti.scalar_type(), num_bits ); - g_entry_func_param_index++; - } -} - -void add_function_arg() -{ - if( g_func_info ) { - DPRINTF("add_function_arg \"%s\"", g_last_symbol->name().c_str() ); - g_func_info->add_arg(g_last_symbol); - } -} - -void add_extern_spec() -{ - DPRINTF("add_extern_spec"); - g_extern_spec = 1; -} - -void add_alignment_spec( int spec ) -{ - DPRINTF("add_alignment_spec"); - parse_assert( g_alignment_spec == -1, "multiple .align specifiers per variable declaration not allowed." ); - g_alignment_spec = spec; -} - -void add_space_spec( enum _memory_space_t spec, int value ) -{ - DPRINTF("add_space_spec \"%s\"", g_ptx_token_decode[spec].c_str() ); - parse_assert( g_space_spec == undefined_space, "multiple space specifiers not allowed." ); - if( spec == param_space_unclassified ) { - if( g_func_decl ) { - if( g_entry_point == 1) - g_space_spec = param_space_kernel; - else - g_space_spec = param_space_local; - } else - g_space_spec = param_space_unclassified; - } else { - g_space_spec = spec; - if( g_space_spec == const_space ) - g_space_spec.set_bank((unsigned)value); - } -} - -void add_vector_spec(int spec ) -{ - DPRINTF("add_vector_spec"); - parse_assert( g_vector_spec == -1, "multiple vector specifiers not allowed." ); - g_vector_spec = spec; -} - -void add_scalar_type_spec( int type_spec ) -{ - DPRINTF("add_scalar_type_spec \"%s\"", g_ptx_token_decode[type_spec].c_str()); - g_scalar_type.push_back( type_spec ); - if ( g_scalar_type.size() > 1 ) { - parse_assert( (g_opcode == -1) || (g_opcode == CVT_OP) || (g_opcode == SET_OP) || (g_opcode == SLCT_OP) - || (g_opcode == TEX_OP), - "only cvt, set, slct, and tex can have more than one type specifier."); - } - g_scalar_type_spec = type_spec; -} - -void add_label( const char *identifier ) -{ - DPRINTF("add_label"); - symbol *s = g_current_symbol_table->lookup(identifier); - if ( s != NULL ) { - g_label = s; - } else { - g_label = g_current_symbol_table->add_variable(identifier,NULL,0,g_filename,ptx_lineno); - } -} - -void add_opcode( int opcode ) -{ - g_opcode = opcode; -} - -void add_pred( const char *identifier, int neg ) -{ - DPRINTF("add_pred"); - const symbol *s = g_current_symbol_table->lookup(identifier); - if ( s == NULL ) { - std::string msg = std::string("predicate \"") + identifier + "\" has no declaration."; - parse_error( msg.c_str() ); - } - g_pred = s; - g_neg_pred = neg; -} - -void add_option( int option ) -{ - DPRINTF("add_option"); - g_options.push_back( option ); -} - -void add_2vector_operand( const char *d1, const char *d2 ) -{ - DPRINTF("add_2vector_operand"); - const symbol *s1 = g_current_symbol_table->lookup(d1); - const symbol *s2 = g_current_symbol_table->lookup(d2); - parse_assert( s1 != NULL && s2 != NULL, "v2 component(s) missing declarations."); - g_operands.push_back( operand_info(s1,s2,NULL,NULL) ); -} - -void add_3vector_operand( const char *d1, const char *d2, const char *d3 ) -{ - DPRINTF("add_3vector_operand"); - const symbol *s1 = g_current_symbol_table->lookup(d1); - const symbol *s2 = g_current_symbol_table->lookup(d2); - const symbol *s3 = g_current_symbol_table->lookup(d3); - parse_assert( s1 != NULL && s2 != NULL && s3 != NULL, "v3 component(s) missing declarations."); - g_operands.push_back( operand_info(s1,s2,s3,NULL) ); -} - -void add_4vector_operand( const char *d1, const char *d2, const char *d3, const char *d4 ) -{ - DPRINTF("add_4vector_operand"); - const symbol *s1 = g_current_symbol_table->lookup(d1); - const symbol *s2 = g_current_symbol_table->lookup(d2); - const symbol *s3 = g_current_symbol_table->lookup(d3); - const symbol *s4 = g_current_symbol_table->lookup(d4); - parse_assert( s1 != NULL && s2 != NULL && s3 != NULL && s4 != NULL, "v4 component(s) missing declarations."); - g_operands.push_back( operand_info(s1,s2,s3,s4) ); -} - -void add_builtin_operand( int builtin, int dim_modifier ) -{ - DPRINTF("add_builtin_operand"); - g_operands.push_back( operand_info(builtin,dim_modifier) ); -} - -void add_memory_operand() -{ - DPRINTF("add_memory_operand"); - assert( !g_operands.empty() ); - g_operands.back().make_memory_operand(); -} - -void add_literal_int( int value ) -{ - DPRINTF("add_literal_int"); - g_operands.push_back( operand_info(value) ); -} - -void add_literal_float( float value ) -{ - DPRINTF("add_literal_float"); - g_operands.push_back( operand_info(value) ); -} - -void add_literal_double( double value ) -{ - DPRINTF("add_literal_double"); - g_operands.push_back( operand_info(value) ); -} - -void add_scalar_operand( const char *identifier ) -{ - DPRINTF("add_scalar_operand"); - const symbol *s = g_current_symbol_table->lookup(identifier); - if ( s == NULL ) { - if ( g_opcode == BRA_OP ) { - // forward branch target... - s = g_current_symbol_table->add_variable(identifier,NULL,0,g_filename,ptx_lineno); - } else { - std::string msg = std::string("operand \"") + identifier + "\" has no declaration."; - parse_error( msg.c_str() ); - } - } - g_operands.push_back( operand_info(s) ); -} - -void add_neg_pred_operand( const char *identifier ) -{ - DPRINTF("add_neg_pred_operand"); - const symbol *s = g_current_symbol_table->lookup(identifier); - if ( s == NULL ) { - s = g_current_symbol_table->add_variable(identifier,NULL,1,g_filename,ptx_lineno); - } - operand_info op(s); - op.set_neg_pred(); - g_operands.push_back( op ); -} - -void add_address_operand( const char *identifier, int offset ) -{ - DPRINTF("add_address_operand"); - const symbol *s = g_current_symbol_table->lookup(identifier); - if ( s == NULL ) { - std::string msg = std::string("operand \"") + identifier + "\" has no declaration."; - parse_error( msg.c_str() ); - } - g_operands.push_back( operand_info(s,offset) ); -} unsigned symbol::sm_next_uid = 1; @@ -781,13 +178,13 @@ symbol *symbol_table::add_variable( const char *identifier, const type_info *typ return s; } -void symbol_table::add_function( function_info *func ) +void symbol_table::add_function( function_info *func, const char *filename, unsigned linenumber ) { std::map<std::string, symbol *>::iterator i = m_symbols.find( func->get_name() ); if( i != m_symbols.end() ) return; char buf[1024]; - snprintf(buf,1024,"%s:%u",g_filename,ptx_lineno); + snprintf(buf,1024,"%s:%u",filename,linenumber); type_info *type = add_type( func ); symbol *s = new symbol(func->get_name().c_str(),type,buf,0); s->set_function(func); @@ -883,12 +280,6 @@ unsigned operand_info::get_uid() return result; } -void add_array_initializer() -{ - g_last_symbol->add_initializer(g_operands); -} - - std::list<ptx_instruction*>::iterator function_info::find_next_real_instruction( std::list<ptx_instruction*>::iterator i) { while( (i != m_instructions.end()) && (*i)->is_label() ) @@ -1258,56 +649,6 @@ void function_info::print_basic_block_dot() printf("}\n"); } -extern "C" void add_version_info( float ver ) -{ - g_global_symbol_table->set_ptx_version(ver,0); -} - - -extern "C" void add_file( unsigned num, const char *filename ) -{ - if( g_filename == NULL ) { - char *b = strdup(filename); - char *l=b; - char *n=b; - while( *n != '\0' ) { - if( *n == '/' ) - l = n+1; - n++; - } - - char *p = strtok(l,"."); - char buf[1024]; - snprintf(buf,1024,"%s.ptx",p); - - char *q = strtok(NULL,"."); - if( q && !strcmp(q,"cu") ) { - g_filename = strdup(buf); - } - - free( b ); - } - - g_current_symbol_table = g_global_symbol_table; -} - -extern "C" void *reset_symtab() -{ - void *result = g_current_symbol_table; - g_current_symbol_table = g_global_symbol_table; - return result; -} - -extern "C" void set_symtab(void*symtab) -{ - g_current_symbol_table = (symbol_table*)symtab; -} - -extern "C" void add_pragma( const char *str ) -{ - printf("GPGPU-Sim PTX: Warning -- ignoring pragma '%s'\n", str ); -} - unsigned ptx_kernel_shmem_size( void *kernel_impl ) { function_info *f = (function_info*)kernel_impl; @@ -1350,7 +691,7 @@ unsigned type_info_key::type_decode( int type, size_t &size, int &basic_type ) case TEXREF_TYPE: case SAMPLERREF_TYPE: case SURFREF_TYPE: size=32; basic_type=3; return 16; default: - printf("ERROR ** type_decode() does not know about \"%s\"\n", g_ptx_token_decode[type].c_str() ); + printf("ERROR ** type_decode() does not know about \"%s\"\n", decode_token(type) ); assert(0); return 0xDEADBEEF; } @@ -1698,8 +1039,7 @@ void gpgpu_ptx_sim_load_ptx_from_string( const char *p, unsigned source_num ) fprintf(fp,"%s",p); fclose(fp); } - g_filename = strdup(buf); - init_parser(); + init_parser(buf); ptx__scan_string(p); int errors = ptx_parse (); if ( errors ) { @@ -1716,9 +1056,9 @@ void gpgpu_ptx_sim_load_ptx_from_string( const char *p, unsigned source_num ) } if ( g_debug_execution >= 100 ) - print_ptx_file(p,source_num,g_filename); + print_ptx_file(p,source_num,buf); - printf("GPGPU-Sim PTX: finished parsing EMBEDDED .ptx file %s\n",g_filename); + printf("GPGPU-Sim PTX: finished parsing EMBEDDED .ptx file %s\n",buf); char fname[1024]; snprintf(fname,1024,"_ptx_XXXXXX"); @@ -1772,7 +1112,6 @@ void gpgpu_ptx_sim_load_ptx_from_string( const char *p, unsigned source_num ) printf("GPGPU-Sim PTX: ERROR ** while loading PTX (c) %d\n", result); exit(1); } - g_filename = NULL; } diff --git a/src/cuda-sim/ptx_ir.h b/src/cuda-sim/ptx_ir.h index 4600be5..fc7f192 100644 --- a/src/cuda-sim/ptx_ir.h +++ b/src/cuda-sim/ptx_ir.h @@ -67,21 +67,19 @@ #include "../abstract_hardware_model.h" -#ifdef __cplusplus +#include <cstdlib> +#include <cstring> +#include <string> +#include <list> +#include <map> +#include <vector> +#include <assert.h> - #include <cstdlib> - #include <cstring> - #include <string> - #include <list> - #include <map> - #include <vector> - #include <assert.h> +#include "ptx.tab.h" +#include "ptx_sim.h" +#include "dram_callback.h" - #include "ptx.tab.h" - #include "ptx_sim.h" - #include "dram_callback.h" - - #include "memory.h" +#include "memory.h" class type_info_key { public: @@ -324,7 +322,7 @@ public: symbol* lookup( const char *identifier ); std::string get_scope_name() const { return m_scope_name; } symbol *add_variable( const char *identifier, const type_info *type, unsigned size, const char *filename, unsigned line ); - void add_function( function_info *func ); + void add_function( function_info *func, const char *filename, unsigned linenumber ); bool add_function_decl( const char *name, int entry_point, function_info **func_info, symbol_table **symbol_table ); type_info *add_type( memory_space_t space_spec, int scalar_type_spec, int vector_spec, int alignment_spec, int extern_spec ); type_info *add_type( function_info *func ); @@ -1261,7 +1259,6 @@ extern symbol_table *g_entrypoint_symbol_table; extern function_info *g_entrypoint_func_info; extern symbol_table *g_global_symbol_table; extern std::map<std::string,symbol_table*> g_sym_name_to_symbol_table; -void init_parser(); #define GLOBAL_HEAP_START 0x10000000 // start allocating from this address (lower values used for allocating globals in .ptx file) @@ -1282,55 +1279,4 @@ void gpgpu_ptx_assemble( std::string kname, void *kinfo ); #include "../option_parser.h" void ptx_reg_options(option_parser_t opp); - -extern "C" { -#endif - - void start_function( int entry_point ); - void add_function_name( const char *fname ); - void init_directive_state(); - void add_directive(); - void end_function(); - void add_identifier( const char *s, int array_dim, unsigned array_ident ); - void add_function_arg(); - void add_scalar_type_spec( int type_spec ); - void add_scalar_operand( const char *identifier ); - void add_neg_pred_operand( const char *identifier ); - void add_variables(); - void set_variable_type(); - void add_opcode( int opcode ); - void add_pred( const char *identifier, int negate ); - void add_2vector_operand( const char *d1, const char *d2 ); - void add_3vector_operand( const char *d1, const char *d2, const char *d3 ); - void add_4vector_operand( const char *d1, const char *d2, const char *d3, const char *d4 ); - void add_option(int option ); - void add_builtin_operand( int builtin, int dim_modifier ); - void add_memory_operand( ); - void add_literal_int( int value ); - void add_literal_float( float value ); - void add_literal_double( double value ); - void add_address_operand( const char *identifier, int offset ); - void add_label( const char *idenfiier ); - void add_vector_spec(int spec ); - void add_space_spec( enum _memory_space_t spec, int value ); - void add_extern_spec(); - void add_instruction(); - void set_return(); - void add_alignment_spec( int spec ); - void add_array_initializer(); - void add_file( unsigned num, const char *filename ); - void add_version_info( float ver ); - void *reset_symtab(); - void set_symtab(void*); - void add_pragma( const char *str ); - - -#define NON_ARRAY_IDENTIFIER 1 -#define ARRAY_IDENTIFIER_NO_DIM 2 -#define ARRAY_IDENTIFIER 3 - -#ifdef __cplusplus -} -#endif - #endif diff --git a/src/cuda-sim/ptx_loader.cc b/src/cuda-sim/ptx_loader.cc index 8c13f6d..fb4e8dd 100644 --- a/src/cuda-sim/ptx_loader.cc +++ b/src/cuda-sim/ptx_loader.cc @@ -65,6 +65,7 @@ #include "ptx_loader.h" #include "ptx_ir.h" #include "cuda-sim.h" +#include "ptx_parser.h" #include <dirent.h> /// globals @@ -211,7 +212,7 @@ void gpgpu_ptx_sim_load_gpu_kernels() ptx_in = fopen( g_filename, "r" ); gpgpu_ptx_sim_init_memory(); if (ptx_in) { - init_parser(); + init_parser(g_filename); ptx_parse(); ptxinfo_in = open_ptxinfo(g_filename); ptxinfo_parse(); @@ -227,28 +228,25 @@ void gpgpu_ptx_sim_load_gpu_kernels() load_constants(g_global_symbol_table,STATIC_ALLOC_LIMIT); } } else { - g_filename = NULL; + const char *filename = NULL; struct dirent **namelist; - int n; - - n = scandir(".", &namelist, ptx_file_filter, alphasort); + int n = scandir(".", &namelist, ptx_file_filter, alphasort); if (n < 0) - perror("scandir"); + perror("GPGPU-Sim PTX: no PTX files returned by scandir"); else { while (n--) { - if ( g_filename != NULL ) { + if ( filename != NULL ) { printf("Loader error: support for multiple .ptx files not yet enabled\n"); abort(); } - g_filename = strdup(namelist[n]->d_name); - printf("Parsing %s..\n", g_filename); - ptx_in = fopen( g_filename, "r" ); + filename = namelist[n]->d_name; + printf("Parsing %s..\n", filename); + ptx_in = fopen( filename, "r" ); free(namelist[n]); - init_parser(); + init_parser(filename); ptx_parse (); - ptxinfo_in = open_ptxinfo(g_filename); + ptxinfo_in = open_ptxinfo(filename); ptxinfo_parse(); - g_filename = NULL; load_static_globals(g_global_symbol_table,STATIC_ALLOC_LIMIT,0xFFFFFFFF); load_constants(g_global_symbol_table,STATIC_ALLOC_LIMIT); } @@ -267,11 +265,10 @@ void gpgpu_ptx_sim_load_gpu_kernels() } if ( g_error_detected ) { - printf( "GPGPU-Sim PTX: Program parsing completed: Errors detected.\n" ); + printf( "GPGPU-Sim PTX: PTX parsing errors detected -- exiting.\n" ); exit(1); - } else { - printf( "GPGPU-Sim PTX: Program parsing completed (max %u registers used per thread).\n", g_max_regs_per_thread ); } + printf( "GPGPU-Sim PTX: Program parsing completed\n" ); if ( g_kernel_name_to_function_lookup ) { for ( std::map<std::string,function_info*>::iterator f=g_kernel_name_to_function_lookup->begin(); diff --git a/src/cuda-sim/ptx_parser.cc b/src/cuda-sim/ptx_parser.cc new file mode 100644 index 0000000..17e4ca2 --- /dev/null +++ b/src/cuda-sim/ptx_parser.cc @@ -0,0 +1,742 @@ +/* + * Copyright (c) 2009 by Tor M. Aamodt, Wilson W. L. Fung, Ali Bakhoda, + * George L. Yuan and the University of British Columbia + * Vancouver, BC V6T 1Z4 + * All Rights Reserved. + * + * THIS IS A LEGAL DOCUMENT BY DOWNLOADING GPGPU-SIM, YOU ARE AGREEING TO THESE + * TERMS AND CONDITIONS. + * + * 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 OWNERS 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. + * + * NOTE: The files libcuda/cuda_runtime_api.c and src/cuda-sim/cuda-math.h + * are derived from the CUDA Toolset available from http://www.nvidia.com/cuda + * (property of NVIDIA). The files benchmarks/BlackScholes/ and + * benchmarks/template/ are derived from the CUDA SDK available from + * http://www.nvidia.com/cuda (also property of NVIDIA). The files from + * src/intersim/ are derived from Booksim (a simulator provided with the + * textbook "Principles and Practices of Interconnection Networks" available + * from http://cva.stanford.edu/books/ppin/). As such, those files are bound by + * the corresponding legal terms and conditions set forth separately (original + * copyright notices are left in files from these sources and where we have + * modified a file our copyright notice appears before the original copyright + * notice). + * + * Using this version of GPGPU-Sim requires a complete installation of CUDA + * which is distributed seperately by NVIDIA under separate terms and + * conditions. To use this version of GPGPU-Sim with OpenCL requires a + * recent version of NVIDIA's drivers which support OpenCL. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. 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. + * + * 3. 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. + * + * 4. This version of GPGPU-SIM is distributed freely for non-commercial use only. + * + * 5. No nonprofit user may place any restrictions on the use of this software, + * including as modified by the user, by any other authorized user. + * + * 6. GPGPU-SIM was developed primarily by Tor M. Aamodt, Wilson W. L. Fung, + * Ali Bakhoda, George L. Yuan, at the University of British Columbia, + * Vancouver, BC V6T 1Z4 + */ + +#include "ptx_parser.h" +#include "ptx_ir.h" +#include <stdarg.h> + +extern "C" int ptx_error( const char *s ); +extern int ptx_lineno; + +static bool g_debug_ir_generation=false; +const char *g_filename; +unsigned g_max_regs_per_thread = 0; + +// the program intermediate representation... +symbol_table *g_global_symbol_table = NULL; +std::map<std::string,symbol_table*> g_sym_name_to_symbol_table; +static symbol_table *g_current_symbol_table = NULL; +static std::list<ptx_instruction*> g_instructions; +static symbol *g_last_symbol = NULL; + +int g_error_detected = 0; + +// type specifier stuff: +memory_space_t g_space_spec = undefined_space; +int g_scalar_type_spec = -1; +int g_vector_spec = -1; +int g_alignment_spec = -1; +int g_extern_spec = 0; + +// variable declaration stuff: +type_info *g_var_type = NULL; + +// instruction definition stuff: +const symbol *g_pred; +int g_neg_pred; +symbol *g_label; +int g_opcode = -1; +std::list<operand_info> g_operands; +std::list<int> g_options; +std::list<int> g_scalar_type; + +#define DPRINTF(...) \ + if( g_debug_ir_generation ) { \ + printf(" %s:%u => ",g_filename,ptx_lineno); \ + printf(" (%s:%u) ", __FILE__, __LINE__); \ + printf(__VA_ARGS__); \ + printf("\n"); \ + fflush(stdout); \ + } + +unsigned g_entry_func_param_index=0; +function_info *g_func_info = NULL; +function_info *g_entrypoint_func_info = NULL; +symbol_table *g_entrypoint_symbol_table = NULL; +std::map<unsigned,std::string> g_ptx_token_decode; +operand_info g_return_var; + +const char *decode_token( int type ) +{ + return g_ptx_token_decode[type].c_str(); +} + +void read_parser_environment_variables() +{ + g_filename = getenv("PTX_SIM_KERNELFILE"); + char *dbg_level = getenv("PTX_SIM_DEBUG"); + if ( dbg_level && strlen(dbg_level) ) { + int debug_execution=0; + sscanf(dbg_level,"%d", &debug_execution); + if ( debug_execution >= 30 ) + g_debug_ir_generation=true; + } +} + +void init_parser( const char *ptx_filename ) +{ + g_filename = strdup(ptx_filename); + g_global_symbol_table = g_current_symbol_table = new symbol_table("global",0,NULL); + ptx_lineno = 1; + +#define DEF(X,Y) g_ptx_token_decode[X] = Y; +#include "ptx_parser_decode.def" +#undef DEF +} + +void init_directive_state() +{ + DPRINTF("init_directive_state"); + g_space_spec=undefined_space; + g_scalar_type_spec=-1; + g_vector_spec=-1; + g_opcode=-1; + g_alignment_spec = -1; + g_extern_spec = 0; + g_scalar_type.clear(); + g_operands.clear(); + g_last_symbol = NULL; +} + +void init_instruction_state() +{ + DPRINTF("init_instruction_state"); + g_pred = NULL; + g_neg_pred = 0; + g_label = NULL; + g_opcode = -1; + g_options.clear(); + g_return_var = operand_info(); + init_directive_state(); +} + +static int g_entry_point; + +void start_function( int entry_point ) +{ + DPRINTF("start_function"); + init_directive_state(); + init_instruction_state(); + g_entry_point = entry_point; + g_func_info = NULL; + g_entry_func_param_index=0; +} + +char *g_add_identifier_cached__identifier = NULL; +int g_add_identifier_cached__array_dim; +int g_add_identifier_cached__array_ident; + +void add_function_name( const char *name ) +{ + DPRINTF("add_function_name %s %s", name, ((g_entry_point==1)?"(entrypoint)":((g_entry_point==2)?"(extern)":""))); + bool prior_decl = g_global_symbol_table->add_function_decl( name, g_entry_point, &g_func_info, &g_current_symbol_table ); + if( g_entry_point ) { + g_entrypoint_func_info = g_func_info; + g_entrypoint_symbol_table = g_current_symbol_table; + } + if( g_add_identifier_cached__identifier ) { + add_identifier( g_add_identifier_cached__identifier, + g_add_identifier_cached__array_dim, + g_add_identifier_cached__array_ident ); + free( g_add_identifier_cached__identifier ); + g_add_identifier_cached__identifier = NULL; + g_func_info->add_return_var( g_last_symbol ); + init_directive_state(); + } + if( prior_decl ) { + g_func_info->remove_args(); + } + g_global_symbol_table->add_function( g_func_info, g_filename, ptx_lineno ); +} + +void add_directive() +{ + DPRINTF("add_directive"); + init_directive_state(); +} + +#define mymax(a,b) ((a)>(b)?(a):(b)) + +void end_function() +{ + DPRINTF("end_function"); + + init_directive_state(); + init_instruction_state(); + g_max_regs_per_thread = mymax( g_max_regs_per_thread, (g_current_symbol_table->next_reg_num()-1)); + g_func_info->add_inst( g_instructions ); + g_instructions.clear(); + gpgpu_ptx_assemble( g_func_info->get_name(), g_func_info ); + g_current_symbol_table = g_global_symbol_table; + + DPRINTF("function %s, PC = %d\n", g_func_info->get_name().c_str(), g_func_info->get_start_PC()); +} + +#define parse_error(msg, ...) parse_error_impl(__FILE__,__LINE__, msg, ##__VA_ARGS__) +#define parse_assert(cond,msg, ...) parse_assert_impl((cond),__FILE__,__LINE__, msg, ##__VA_ARGS__) + +void parse_error_impl( const char *file, unsigned line, const char *msg, ... ) +{ + va_list ap; + char buf[1024]; + va_start(ap,msg); + vsnprintf(buf,1024,msg,ap); + va_end(ap); + + g_error_detected = 1; + printf("%s:%u: Parse error: %s (%s:%u)\n\n", g_filename, ptx_lineno, buf, file, line); + ptx_error(NULL); + abort(); + exit(1); +} + +void parse_assert_impl( int test_value, const char *file, unsigned line, const char *msg, ... ) +{ + va_list ap; + char buf[1024]; + va_start(ap,msg); + vsnprintf(buf,1024,msg,ap); + va_end(ap); + + if ( test_value == 0 ) + parse_error_impl(file,line, msg); +} + +extern "C" char linebuf[1024]; + + +void set_return() +{ + parse_assert( (g_opcode == CALL_OP), "only call can have return value"); + g_operands.front().set_return(); + g_return_var = g_operands.front(); +} + +std::map<std::string,std::map<unsigned,const ptx_instruction*> > g_inst_lookup; + +const ptx_instruction *ptx_instruction_lookup( const char *filename, unsigned linenumber ) +{ + std::map<std::string,std::map<unsigned,const ptx_instruction*> >::iterator f=g_inst_lookup.find(filename); + if( f == g_inst_lookup.end() ) + return NULL; + std::map<unsigned,const ptx_instruction*>::iterator l=f->second.find(linenumber); + if( l == f->second.end() ) + return NULL; + return l->second; +} + +void add_instruction() +{ + DPRINTF("add_instruction: %s", ((g_opcode>0)?g_opcode_string[g_opcode]:"<label>") ); + ptx_instruction *i = new ptx_instruction( g_opcode, + g_pred, + g_neg_pred, + g_label, + g_operands, + g_return_var, + g_options, + g_scalar_type, + g_space_spec, + g_filename, + ptx_lineno, + linebuf ); + g_instructions.push_back(i); + g_inst_lookup[g_filename][ptx_lineno] = i; + init_instruction_state(); +} + +void add_variables() +{ + DPRINTF("add_variables"); + if ( !g_operands.empty() ) { + assert( g_last_symbol != NULL ); + g_last_symbol->add_initializer(g_operands); + } + init_directive_state(); +} + +void set_variable_type() +{ + DPRINTF("set_variable_type space_spec=%s scalar_type_spec=%s", + g_ptx_token_decode[g_space_spec.get_type()].c_str(), + g_ptx_token_decode[g_scalar_type_spec].c_str() ); + parse_assert( g_space_spec != undefined_space, "variable has no space specification" ); + parse_assert( g_scalar_type_spec != -1, "variable has no type information" ); // need to extend for structs? + g_var_type = g_current_symbol_table->add_type( g_space_spec, + g_scalar_type_spec, + g_vector_spec, + g_alignment_spec, + g_extern_spec ); +} + +bool check_for_duplicates( const char *identifier ) +{ + const symbol *s = g_current_symbol_table->lookup(identifier); + return ( s != NULL ); +} + +extern std::set<std::string> g_globals; +extern std::set<std::string> g_constants; + +int g_func_decl = 0; +int g_ident_add_uid = 0; +unsigned g_const_alloc = 1; + +void add_identifier( const char *identifier, int array_dim, unsigned array_ident ) +{ + if( g_func_decl && (g_func_info == NULL) ) { + // return variable decl... + assert( g_add_identifier_cached__identifier == NULL ); + g_add_identifier_cached__identifier = strdup(identifier); + g_add_identifier_cached__array_dim = array_dim; + g_add_identifier_cached__array_ident = array_ident; + return; + } + DPRINTF("add_identifier \"%s\" (%u)", identifier, g_ident_add_uid); + g_ident_add_uid++; + type_info *type = g_var_type; + type_info_key ti = type->get_key(); + int basic_type; + int regnum; + size_t num_bits; + unsigned addr, addr_pad; + ti.type_decode(num_bits,basic_type); + + bool duplicates = check_for_duplicates( identifier ); + if( duplicates ) { + symbol *s = g_current_symbol_table->lookup(identifier); + g_last_symbol = s; + if( g_func_decl ) + return; + std::string msg = std::string(identifier) + " was delcared previous at " + s->decl_location() + " skipping new declaration"; + printf("GPGPU-Sim PTX: Warning %s\n", msg.c_str()); + return; + } + + assert( g_var_type != NULL ); + switch ( array_ident ) { + case ARRAY_IDENTIFIER: + type = g_current_symbol_table->get_array_type(type,array_dim); + num_bits = array_dim * num_bits; + break; + case ARRAY_IDENTIFIER_NO_DIM: + type = g_current_symbol_table->get_array_type(type,(unsigned)-1); + num_bits = 0; + break; + default: + break; + } + g_last_symbol = g_current_symbol_table->add_variable(identifier,type,num_bits/8,g_filename,ptx_lineno); + switch ( ti.get_memory_space().get_type() ) { + case reg_space: { + regnum = g_current_symbol_table->next_reg_num(); + int arch_regnum = -1; + for (int d = 0; d < strlen(identifier); d++) { + if (isdigit(identifier[d])) { + sscanf(identifier + d, "%d", &arch_regnum); + break; + } + } + if (strcmp(identifier, "%sp") == 0) { + arch_regnum = 0; + } + g_last_symbol->set_regno(regnum, arch_regnum); + } break; + case shared_space: + printf("GPGPU-Sim PTX: allocating shared region for \"%s\" from 0x%x to 0x%lx (shared memory space)\n", + identifier, + g_current_symbol_table->get_shared_next(), + g_current_symbol_table->get_shared_next() + num_bits/8 ); + fflush(stdout); + assert( (num_bits%8) == 0 ); + addr = g_current_symbol_table->get_shared_next(); + addr_pad = num_bits ? (((num_bits/8) - (addr % (num_bits/8))) % (num_bits/8)) : 0; + g_last_symbol->set_address( addr+addr_pad ); + g_current_symbol_table->alloc_shared( num_bits/8 + addr_pad ); + break; + case const_space: + if( array_ident == ARRAY_IDENTIFIER_NO_DIM ) { + printf("GPGPU-Sim PTX: deferring allocation of constant region for \"%s\" (need size information)\n", identifier ); + } else { + printf("GPGPU-Sim PTX: allocating constant region for \"%s\" from 0x%x to 0x%lx (global memory space) %u\n", + identifier, + g_current_symbol_table->get_global_next(), + g_current_symbol_table->get_global_next() + num_bits/8, + g_const_alloc++ ); + fflush(stdout); + assert( (num_bits%8) == 0 ); + addr = g_current_symbol_table->get_global_next(); + addr_pad = num_bits ? (((num_bits/8) - (addr % (num_bits/8))) % (num_bits/8)) : 0; + g_last_symbol->set_address( addr + addr_pad ); + g_current_symbol_table->alloc_global( num_bits/8 + addr_pad ); + } + if( g_current_symbol_table == g_global_symbol_table ) { + g_constants.insert( identifier ); + } + assert( g_current_symbol_table != NULL ); + g_sym_name_to_symbol_table[ identifier ] = g_current_symbol_table; + break; + case global_space: + printf("GPGPU-Sim PTX: allocating global region for \"%s\" from 0x%x to 0x%lx (global memory space)\n", + identifier, + g_current_symbol_table->get_global_next(), + g_current_symbol_table->get_global_next() + num_bits/8 ); + fflush(stdout); + assert( (num_bits%8) == 0 ); + addr = g_current_symbol_table->get_global_next(); + addr_pad = num_bits ? (((num_bits/8) - (addr % (num_bits/8))) % (num_bits/8)) : 0; + g_last_symbol->set_address( addr+addr_pad ); + g_current_symbol_table->alloc_global( num_bits/8 + addr_pad ); + g_globals.insert( identifier ); + assert( g_current_symbol_table != NULL ); + g_sym_name_to_symbol_table[ identifier ] = g_current_symbol_table; + break; + case local_space: + if( g_func_info == NULL ) { + printf("GPGPU-Sim PTX: not allocating .local \"%s\" declared at global scope\n", identifier); + break; + } + printf("GPGPU-Sim PTX: allocating stack frame region for .local \"%s\" from 0x%x to 0x%lx\n", + identifier, + g_current_symbol_table->get_local_next(), + g_current_symbol_table->get_local_next() + num_bits/8 ); + fflush(stdout); + assert( (num_bits%8) == 0 ); + g_last_symbol->set_address( g_current_symbol_table->get_local_next() ); + g_current_symbol_table->alloc_local( num_bits/8 ); + g_func_info->set_framesize( g_current_symbol_table->get_local_next() ); + break; + case tex_space: + printf("GPGPU-Sim PTX: encountered texture directive %s.\n", identifier); + break; + case param_space_local: + printf("GPGPU-Sim PTX: allocating stack frame region for .param \"%s\" from 0x%x to 0x%lx\n", + identifier, + g_current_symbol_table->get_local_next(), + g_current_symbol_table->get_local_next() + num_bits/8 ); + fflush(stdout); + assert( (num_bits%8) == 0 ); + g_last_symbol->set_address( g_current_symbol_table->get_local_next() ); + g_current_symbol_table->alloc_local( num_bits/8 ); + g_func_info->set_framesize( g_current_symbol_table->get_local_next() ); + break; + case param_space_kernel: + break; + default: + abort(); + break; + } + + assert( !ti.is_param_unclassified() ); + if ( ti.is_param_kernel() ) { + g_func_info->add_param_name_type_size(g_entry_func_param_index,identifier, ti.scalar_type(), num_bits ); + g_entry_func_param_index++; + } +} + +void add_function_arg() +{ + if( g_func_info ) { + DPRINTF("add_function_arg \"%s\"", g_last_symbol->name().c_str() ); + g_func_info->add_arg(g_last_symbol); + } +} + +void add_extern_spec() +{ + DPRINTF("add_extern_spec"); + g_extern_spec = 1; +} + +void add_alignment_spec( int spec ) +{ + DPRINTF("add_alignment_spec"); + parse_assert( g_alignment_spec == -1, "multiple .align specifiers per variable declaration not allowed." ); + g_alignment_spec = spec; +} + +void add_space_spec( enum _memory_space_t spec, int value ) +{ + DPRINTF("add_space_spec \"%s\"", g_ptx_token_decode[spec].c_str() ); + parse_assert( g_space_spec == undefined_space, "multiple space specifiers not allowed." ); + if( spec == param_space_unclassified ) { + if( g_func_decl ) { + if( g_entry_point == 1) + g_space_spec = param_space_kernel; + else + g_space_spec = param_space_local; + } else + g_space_spec = param_space_unclassified; + } else { + g_space_spec = spec; + if( g_space_spec == const_space ) + g_space_spec.set_bank((unsigned)value); + } +} + +void add_vector_spec(int spec ) +{ + DPRINTF("add_vector_spec"); + parse_assert( g_vector_spec == -1, "multiple vector specifiers not allowed." ); + g_vector_spec = spec; +} + +void add_scalar_type_spec( int type_spec ) +{ + DPRINTF("add_scalar_type_spec \"%s\"", g_ptx_token_decode[type_spec].c_str()); + g_scalar_type.push_back( type_spec ); + if ( g_scalar_type.size() > 1 ) { + parse_assert( (g_opcode == -1) || (g_opcode == CVT_OP) || (g_opcode == SET_OP) || (g_opcode == SLCT_OP) + || (g_opcode == TEX_OP), + "only cvt, set, slct, and tex can have more than one type specifier."); + } + g_scalar_type_spec = type_spec; +} + +void add_label( const char *identifier ) +{ + DPRINTF("add_label"); + symbol *s = g_current_symbol_table->lookup(identifier); + if ( s != NULL ) { + g_label = s; + } else { + g_label = g_current_symbol_table->add_variable(identifier,NULL,0,g_filename,ptx_lineno); + } +} + +void add_opcode( int opcode ) +{ + g_opcode = opcode; +} + +void add_pred( const char *identifier, int neg ) +{ + DPRINTF("add_pred"); + const symbol *s = g_current_symbol_table->lookup(identifier); + if ( s == NULL ) { + std::string msg = std::string("predicate \"") + identifier + "\" has no declaration."; + parse_error( msg.c_str() ); + } + g_pred = s; + g_neg_pred = neg; +} + +void add_option( int option ) +{ + DPRINTF("add_option"); + g_options.push_back( option ); +} + +void add_2vector_operand( const char *d1, const char *d2 ) +{ + DPRINTF("add_2vector_operand"); + const symbol *s1 = g_current_symbol_table->lookup(d1); + const symbol *s2 = g_current_symbol_table->lookup(d2); + parse_assert( s1 != NULL && s2 != NULL, "v2 component(s) missing declarations."); + g_operands.push_back( operand_info(s1,s2,NULL,NULL) ); +} + +void add_3vector_operand( const char *d1, const char *d2, const char *d3 ) +{ + DPRINTF("add_3vector_operand"); + const symbol *s1 = g_current_symbol_table->lookup(d1); + const symbol *s2 = g_current_symbol_table->lookup(d2); + const symbol *s3 = g_current_symbol_table->lookup(d3); + parse_assert( s1 != NULL && s2 != NULL && s3 != NULL, "v3 component(s) missing declarations."); + g_operands.push_back( operand_info(s1,s2,s3,NULL) ); +} + +void add_4vector_operand( const char *d1, const char *d2, const char *d3, const char *d4 ) +{ + DPRINTF("add_4vector_operand"); + const symbol *s1 = g_current_symbol_table->lookup(d1); + const symbol *s2 = g_current_symbol_table->lookup(d2); + const symbol *s3 = g_current_symbol_table->lookup(d3); + const symbol *s4 = g_current_symbol_table->lookup(d4); + parse_assert( s1 != NULL && s2 != NULL && s3 != NULL && s4 != NULL, "v4 component(s) missing declarations."); + g_operands.push_back( operand_info(s1,s2,s3,s4) ); +} + +void add_builtin_operand( int builtin, int dim_modifier ) +{ + DPRINTF("add_builtin_operand"); + g_operands.push_back( operand_info(builtin,dim_modifier) ); +} + +void add_memory_operand() +{ + DPRINTF("add_memory_operand"); + assert( !g_operands.empty() ); + g_operands.back().make_memory_operand(); +} + +void add_literal_int( int value ) +{ + DPRINTF("add_literal_int"); + g_operands.push_back( operand_info(value) ); +} + +void add_literal_float( float value ) +{ + DPRINTF("add_literal_float"); + g_operands.push_back( operand_info(value) ); +} + +void add_literal_double( double value ) +{ + DPRINTF("add_literal_double"); + g_operands.push_back( operand_info(value) ); +} + +void add_scalar_operand( const char *identifier ) +{ + DPRINTF("add_scalar_operand"); + const symbol *s = g_current_symbol_table->lookup(identifier); + if ( s == NULL ) { + if ( g_opcode == BRA_OP ) { + // forward branch target... + s = g_current_symbol_table->add_variable(identifier,NULL,0,g_filename,ptx_lineno); + } else { + std::string msg = std::string("operand \"") + identifier + "\" has no declaration."; + parse_error( msg.c_str() ); + } + } + g_operands.push_back( operand_info(s) ); +} + +void add_neg_pred_operand( const char *identifier ) +{ + DPRINTF("add_neg_pred_operand"); + const symbol *s = g_current_symbol_table->lookup(identifier); + if ( s == NULL ) { + s = g_current_symbol_table->add_variable(identifier,NULL,1,g_filename,ptx_lineno); + } + operand_info op(s); + op.set_neg_pred(); + g_operands.push_back( op ); +} + +void add_address_operand( const char *identifier, int offset ) +{ + DPRINTF("add_address_operand"); + const symbol *s = g_current_symbol_table->lookup(identifier); + if ( s == NULL ) { + std::string msg = std::string("operand \"") + identifier + "\" has no declaration."; + parse_error( msg.c_str() ); + } + g_operands.push_back( operand_info(s,offset) ); +} + +void add_array_initializer() +{ + g_last_symbol->add_initializer(g_operands); +} + +void add_version_info( float ver ) +{ + g_global_symbol_table->set_ptx_version(ver,0); +} + +void add_file( unsigned num, const char *filename ) +{ + if( g_filename == NULL ) { + char *b = strdup(filename); + char *l=b; + char *n=b; + while( *n != '\0' ) { + if( *n == '/' ) + l = n+1; + n++; + } + + char *p = strtok(l,"."); + char buf[1024]; + snprintf(buf,1024,"%s.ptx",p); + + char *q = strtok(NULL,"."); + if( q && !strcmp(q,"cu") ) { + g_filename = strdup(buf); + } + + free( b ); + } + + g_current_symbol_table = g_global_symbol_table; +} + +void *reset_symtab() +{ + void *result = g_current_symbol_table; + g_current_symbol_table = g_global_symbol_table; + return result; +} + +void set_symtab(void*symtab) +{ + g_current_symbol_table = (symbol_table*)symtab; +} + +void add_pragma( const char *str ) +{ + printf("GPGPU-Sim PTX: Warning -- ignoring pragma '%s'\n", str ); +} diff --git a/src/cuda-sim/ptx_parser.h b/src/cuda-sim/ptx_parser.h new file mode 100644 index 0000000..076fa11 --- /dev/null +++ b/src/cuda-sim/ptx_parser.h @@ -0,0 +1,122 @@ +/* + * Copyright (c) 2009 by Tor M. Aamodt, Wilson W. L. Fung, Ali Bakhoda, + * George L. Yuan, Dan O'Connor, Joey Ting, Henry Wong and the + * University of British Columbia + * Vancouver, BC V6T 1Z4 + * All Rights Reserved. + * + * THIS IS A LEGAL DOCUMENT BY DOWNLOADING GPGPU-SIM, YOU ARE AGREEING TO THESE + * TERMS AND CONDITIONS. + * + * 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 OWNERS 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. + * + * NOTE: The files libcuda/cuda_runtime_api.c and src/cuda-sim/cuda-math.h + * are derived from the CUDA Toolset available from http://www.nvidia.com/cuda + * (property of NVIDIA). The files benchmarks/BlackScholes/ and + * benchmarks/template/ are derived from the CUDA SDK available from + * http://www.nvidia.com/cuda (also property of NVIDIA). The files from + * src/intersim/ are derived from Booksim (a simulator provided with the + * textbook "Principles and Practices of Interconnection Networks" available + * from http://cva.stanford.edu/books/ppin/). As such, those files are bound by + * the corresponding legal terms and conditions set forth separately (original + * copyright notices are left in files from these sources and where we have + * modified a file our copyright notice appears before the original copyright + * notice). + * + * Using this version of GPGPU-Sim requires a complete installation of CUDA + * which is distributed seperately by NVIDIA under separate terms and + * conditions. To use this version of GPGPU-Sim with OpenCL requires a + * recent version of NVIDIA's drivers which support OpenCL. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. 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. + * + * 3. 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. + * + * 4. This version of GPGPU-SIM is distributed freely for non-commercial use only. + * + * 5. No nonprofit user may place any restrictions on the use of this software, + * including as modified by the user, by any other authorized user. + * + * 6. GPGPU-SIM was developed primarily by Tor M. Aamodt, Wilson W. L. Fung, + * Ali Bakhoda, George L. Yuan, at the University of British Columbia, + * Vancouver, BC V6T 1Z4 + */ + +#ifndef ptx_parser_INCLUDED +#define ptx_parser_INCLUDED + +#include "../abstract_hardware_model.h" + +extern const char *g_filename; +#ifdef __cplusplus +extern "C" { +#endif +const char *decode_token( int type ); +void read_parser_environment_variables(); +void init_parser(const char*); +void start_function( int entry_point ); +void add_function_name( const char *fname ); +void init_directive_state(); +void add_directive(); +void end_function(); +void add_identifier( const char *s, int array_dim, unsigned array_ident ); +void add_function_arg(); +void add_scalar_type_spec( int type_spec ); +void add_scalar_operand( const char *identifier ); +void add_neg_pred_operand( const char *identifier ); +void add_variables(); +void set_variable_type(); +void add_opcode( int opcode ); +void add_pred( const char *identifier, int negate ); +void add_2vector_operand( const char *d1, const char *d2 ); +void add_3vector_operand( const char *d1, const char *d2, const char *d3 ); +void add_4vector_operand( const char *d1, const char *d2, const char *d3, const char *d4 ); +void add_option(int option ); +void add_builtin_operand( int builtin, int dim_modifier ); +void add_memory_operand( ); +void add_literal_int( int value ); +void add_literal_float( float value ); +void add_literal_double( double value ); +void add_address_operand( const char *identifier, int offset ); +void add_label( const char *idenfiier ); +void add_vector_spec(int spec ); +void add_space_spec( enum _memory_space_t spec, int value ); +void add_extern_spec(); +void add_instruction(); +void set_return(); +void add_alignment_spec( int spec ); +void add_array_initializer(); +void add_file( unsigned num, const char *filename ); +void add_version_info( float ver ); +void *reset_symtab(); +void set_symtab(void*); +void add_pragma( const char *str ); +#ifdef __cplusplus +} +#endif + +#define NON_ARRAY_IDENTIFIER 1 +#define ARRAY_IDENTIFIER_NO_DIM 2 +#define ARRAY_IDENTIFIER 3 + +#endif diff --git a/src/gpgpu-sim/shader.cc b/src/gpgpu-sim/shader.cc index d3780a8..6cb83eb 100644 --- a/src/gpgpu-sim/shader.cc +++ b/src/gpgpu-sim/shader.cc @@ -222,7 +222,6 @@ extern int n_pdom_sc_single_stat; extern int gpgpu_cuda_sim; extern unsigned long long gpu_tot_sim_cycle; -extern unsigned g_max_regs_per_thread; void ptx_decode_inst( void *thd, unsigned *op, int *i1, int *i2, int *i3, int *i4, int *o1, int *o2, int *o3, int *o4, int *vectorin, int *vectorout, int *arch_reg ); void ptx_exec_inst( void *thd, address_type *addr, memory_space_t *space, unsigned *data_size, dram_callback_t* callback, unsigned warp_active_mask); void ptx_sim_free_sm( class ptx_thread_info** thread_info ); diff --git a/src/gpgpusim_entrypoint.cc b/src/gpgpusim_entrypoint.cc index d7569ce..6ce564b 100644 --- a/src/gpgpusim_entrypoint.cc +++ b/src/gpgpusim_entrypoint.cc @@ -69,6 +69,7 @@ #include "option_parser.h" #include "cuda-sim/cuda-sim.h" #include "cuda-sim/ptx_ir.h" +#include "cuda-sim/ptx_parser.h" #define MAX(a,b) (((a)>(b))?(a):(b)) @@ -94,6 +95,7 @@ void gpgpu_ptx_sim_init_perf() { print_splash(); read_sim_environment_variables(); + read_parser_environment_variables(); opp = option_parser_create(); option_parser_register(opp, "-network_mode", OPT_INT32, &g_network_mode, "Interconnection network mode", "1"); option_parser_register(opp, "-inter_config_file", OPT_CSTR, &g_network_config_filename, "Interconnection network config file", "mesh"); |
