summaryrefslogtreecommitdiff
path: root/src/cuda-sim/ptx_loader.cc
diff options
context:
space:
mode:
authorsspenst <[email protected]>2016-06-13 01:08:36 -0700
committersspenst <[email protected]>2016-06-13 01:08:36 -0700
commit547ce656018a6439e658be3c283721a961b0217f (patch)
treecbd81e6b3d3fdee79ce9f934fe4c92a5c984cc36 /src/cuda-sim/ptx_loader.cc
parent7356669b66425ee73ba61466f37fe25b886a3641 (diff)
If ptxas notices any duplicate errors, they now automatically get resolved and the program continues with the duplicate function/variable declarations removed.
Diffstat (limited to 'src/cuda-sim/ptx_loader.cc')
-rw-r--r--src/cuda-sim/ptx_loader.cc126
1 files changed, 123 insertions, 3 deletions
diff --git a/src/cuda-sim/ptx_loader.cc b/src/cuda-sim/ptx_loader.cc
index f7bf70e..9bb5008 100644
--- a/src/cuda-sim/ptx_loader.cc
+++ b/src/cuda-sim/ptx_loader.cc
@@ -46,6 +46,8 @@ bool g_override_embedded_ptx = false;
extern int ptx_parse();
extern int ptx__scan_string(const char*);
+extern std::map<unsigned,const char*> get_duplicate();
+
const char *g_ptxinfo_filename;
extern int ptxinfo_parse();
extern int ptxinfo_debug;
@@ -182,6 +184,109 @@ symbol_table *gpgpu_ptx_sim_load_ptx_from_string( const char *p, unsigned source
return symtab;
}
+void fix_duplicate_errors(char fname2[1024]) {
+ char tempfile[1024] = "_temp_ptx";
+ char commandline[1024];
+
+ // change the name of the ptx file to _temp_ptx
+ snprintf(commandline,1024,"mv %s %s",fname2,tempfile);
+ printf("Running: %s\n", commandline);
+ int result = system(commandline);
+ if (result != 0) {
+ printf("GPGPU-Sim PTX: ERROR ** while changing filename from %s to %s", fname2, tempfile);
+ exit(1);
+ }
+
+ // store all of the ptx into a char array
+ FILE *ptxsource = fopen(tempfile,"r");
+ fseek(ptxsource, 0, SEEK_END);
+ long filesize = ftell(ptxsource);
+ rewind(ptxsource);
+ char *ptxdata = (char*)malloc((filesize+1)*sizeof(char));
+ fread(ptxdata, filesize, 1, ptxsource);
+ fclose(ptxsource);
+
+ FILE *ptxdest = fopen(fname2,"w");
+ std::map<unsigned,const char*> duplicate = get_duplicate();
+ unsigned offset;
+ unsigned oldlinenum = 1;
+ unsigned linenum;
+ char *startptr = ptxdata;
+ char *funcptr;
+ char *tempptr = ptxdata - 1;
+ char *lineptr = ptxdata - 1;
+
+ // recreate the ptx file without duplications
+ for ( std::map<unsigned,const char*>::iterator iter = duplicate.begin();
+ iter != duplicate.end();
+ iter++){
+ // find the line of the next error
+ linenum = iter->first;
+ for (int i = oldlinenum; i < linenum; i++) {
+ lineptr = strchr(lineptr + 1, '\n');
+ }
+
+ // find the end of the current section to be copied over
+ // then find the start of the next section that will be copied
+ if (strcmp("function", iter->second) == 0) {
+ // get location of most recent .func
+ while (tempptr < lineptr && tempptr != NULL) {
+ funcptr = tempptr;
+ tempptr = strstr(funcptr + 1, ".func");
+ }
+
+ // get the start of the previous line
+ offset = 0;
+ while (*(funcptr - offset) != '\n') offset++;
+
+ fwrite(startptr, sizeof(char), funcptr - offset + 1 - startptr, ptxdest);
+
+ //find next location of startptr
+ if (*(lineptr + 3) == ';') {
+ // for function definitions
+ startptr = lineptr + 5;
+ } else if (*(lineptr + 3) == '{') {
+ // for functions enclosed with curly brackets
+ offset = 5;
+ unsigned bracket = 1;
+ while (bracket != 0) {
+ if (*(lineptr + offset) == '{') bracket++;
+ else if (*(lineptr + offset) == '}') bracket--;
+ offset++;
+ }
+ startptr = lineptr + offset + 1;
+ } else {
+ printf("GPGPU-Sim PTX: ERROR ** Unrecognized function format\n");
+ abort();
+ }
+ } else if (strcmp("variable", iter->second) == 0) {
+ fwrite(startptr, sizeof(char), (int)(lineptr + 1 - startptr), ptxdest);
+
+ //find next location of startptr
+ offset = 1;
+ while (*(lineptr + offset) != '\n') offset++;
+ startptr = lineptr + offset + 1;
+ } else {
+ printf("GPGPU-Sim PTX: ERROR ** Unsupported duplicate type: %s\n", iter->second);
+ }
+
+ oldlinenum = linenum;
+ }
+ // copy over the rest of the file
+ fwrite(startptr, sizeof(char), ptxdata + filesize - startptr, ptxdest);
+
+ // cleanup
+ free(ptxdata);
+ fclose(ptxdest);
+ snprintf(commandline,1024,"rm -f %s",tempfile);
+ printf("Running: %s\n", commandline);
+ result = system(commandline);
+ if (result != 0) {
+ printf("GPGPU-Sim PTX: ERROR ** while deleting %s", tempfile);
+ exit(1);
+ }
+}
+
void gpgpu_ptxinfo_load_from_string( const char *p_for_info, unsigned source_num, unsigned sm_version )
{
char fname[1024];
@@ -225,14 +330,29 @@ void gpgpu_ptxinfo_load_from_string( const char *p_for_info, unsigned source_num
printf("GPGPU-Sim PTX: generating ptxinfo using \"%s\"\n", commandline);
result = system(commandline);
if( result != 0 ) {
- printf("GPGPU-Sim PTX: ERROR ** while loading PTX (b) %d\n", result);
- printf(" Ensure ptxas is in your path.\n");
- exit(1);
+ // 65280 = duplicate errors
+ if (result == 65280) {
+ ptxinfo_in = fopen(tempfile_ptxinfo,"r");
+ g_ptxinfo_filename = tempfile_ptxinfo;
+ ptxinfo_parse();
+
+ fix_duplicate_errors(fname2);
+ snprintf(commandline,1024,"$CUDA_INSTALL_PATH/bin/ptxas %s -v %s --output-file /dev/null 2> %s",
+ extra_flags, fname2, tempfile_ptxinfo);
+ printf("GPGPU-Sim PTX: regenerating ptxinfo using \"%s\"\n", commandline);
+ result = system(commandline);
+ }
+ if (result != 0) {
+ printf("GPGPU-Sim PTX: ERROR ** while loading PTX (b) %d\n", result);
+ printf(" Ensure ptxas is in your path.\n");
+ exit(1);
+ }
}
ptxinfo_in = fopen(tempfile_ptxinfo,"r");
g_ptxinfo_filename = tempfile_ptxinfo;
ptxinfo_parse();
+
if( ! g_save_embedded_ptx ) {
snprintf(commandline,1024,"rm -f %s %s %s", fname, fname2, tempfile_ptxinfo);
printf("GPGPU-Sim PTX: removing ptxinfo using \"%s\"\n", commandline);