memory leak
[cloog/uuh.git] / source / program.c
blob753172f7eaf214f062d73004d3229753d9b28bd3
2 /**-------------------------------------------------------------------**
3 ** CLooG **
4 **-------------------------------------------------------------------**
5 ** program.c **
6 **-------------------------------------------------------------------**
7 ** First version: october 25th 2001 **
8 **-------------------------------------------------------------------**/
11 /******************************************************************************
12 * CLooG : the Chunky Loop Generator (experimental) *
13 ******************************************************************************
14 * *
15 * Copyright (C) 2001-2005 Cedric Bastoul *
16 * *
17 * This library is free software; you can redistribute it and/or *
18 * modify it under the terms of the GNU Lesser General Public *
19 * License as published by the Free Software Foundation; either *
20 * version 2.1 of the License, or (at your option) any later version. *
21 * *
22 * This library is distributed in the hope that it will be useful, *
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
25 * Lesser General Public License for more details. *
26 * *
27 * You should have received a copy of the GNU Lesser General Public *
28 * License along with this library; if not, write to the Free Software *
29 * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
30 * Boston, MA 02110-1301 USA *
31 * *
32 * CLooG, the Chunky Loop Generator *
33 * Written by Cedric Bastoul, Cedric.Bastoul@inria.fr *
34 * *
35 ******************************************************************************/
36 /* CAUTION: the english used for comments is probably the worst you ever read,
37 * please feel free to correct and improve it !
41 # include <sys/types.h>
42 # include <sys/time.h>
43 #include <stdarg.h>
44 # include <stdlib.h>
45 # include <stdio.h>
46 # include <string.h>
47 # include <ctype.h>
48 # include <unistd.h>
49 # include "../include/cloog/cloog.h"
50 #ifdef CLOOG_RUSAGE
51 # include <sys/resource.h>
52 #endif
54 #define ALLOC(type) (type*)malloc(sizeof(type))
56 #ifdef OSL_SUPPORT
57 #include <osl/scop.h>
58 #include <osl/extensions/coordinates.h>
59 #include <osl/extensions/loop.h>
60 #endif
62 /******************************************************************************
63 * Structure display function *
64 ******************************************************************************/
67 /**
68 * cloog_program_print function:
69 * this function is a human-friendly way to display the CloogProgram data
70 * structure, it shows all the different fields and includes an indentation
71 * level (level) in order to work with others print_structure functions.
72 * - July 1st 2005: first version based on the old cloog_program_print function.
74 void cloog_program_print_structure(file, program, level)
75 FILE * file ;
76 CloogProgram * program ;
77 int level ;
78 { int i, j ;
80 /* Go to the right level. */
81 for (i=0; i<level; i++)
82 fprintf(file,"|\t") ;
84 fprintf(file,"+-- CloogProgram\n") ;
86 /* A blank line. */
87 for (i=0; i<=level+1; i++)
88 fprintf(file,"|\t") ;
89 fprintf(file,"\n") ;
91 /* Print the language. */
92 for (i=0; i<=level; i++)
93 fprintf(file,"|\t") ;
94 fprintf(file, "Language: %c\n",program->language) ;
96 /* A blank line. */
97 for (i=0; i<=level+1; i++)
98 fprintf(file,"|\t") ;
99 fprintf(file,"\n") ;
101 /* Print the scattering dimension number. */
102 for (i=0; i<=level; i++)
103 fprintf(file,"|\t") ;
104 fprintf(file,"Scattering dimension number: %d\n",program->nb_scattdims) ;
106 /* A blank line. */
107 for (i=0; i<=level+1; i++)
108 fprintf(file,"|\t") ;
109 fprintf(file,"\n") ;
111 /* Print the scalar scattering dimension informations. */
112 for (i=0; i<=level; i++)
113 fprintf(file,"|\t") ;
114 if (program->scaldims != NULL)
115 { fprintf(file,"Scalar dimensions:") ;
116 for (i=0;i<program->nb_scattdims;i++)
117 fprintf(file," %d:%d ",i,program->scaldims[i]) ;
118 fprintf(file,"\n") ;
120 else
121 fprintf(file,"No scalar scattering dimensions\n") ;
123 /* A blank line. */
124 for (i=0; i<=level+1; i++)
125 fprintf(file,"|\t") ;
126 fprintf(file,"\n") ;
128 /* Print the parameter and the iterator names. */
129 cloog_names_print_structure(file,program->names,level+1) ;
131 /* A blank line. */
132 for (i=0; i<=level+1; i++)
133 fprintf(file,"|\t") ;
134 fprintf(file,"\n") ;
136 /* Print the context. */
137 cloog_domain_print_structure(file, program->context, level+1, "Context");
139 /* Print the loop. */
140 cloog_loop_print_structure(file,program->loop,level+1) ;
142 /* One more time something that is here only for a better look. */
143 for (j=0; j<2; j++)
144 { for (i=0; i<=level; i++)
145 fprintf(file,"|\t") ;
147 fprintf(file,"\n") ;
153 * cloog_program_dump_cloog function:
154 * This function dumps a CloogProgram structure supposed to be completely
155 * filled in a CLooG input file (foo possibly stdout) such as CLooG can
156 * rebuild almost exactly the data structure from the input file.
158 * If the scattering is already applied, the scattering parameter is supposed to
159 * be NULL. In this case the number of scattering functions is lost, since they
160 * are included inside the iteration domains. This can only lead to a less
161 * beautiful pretty printing.
163 * In case the scattering is not yet applied it can be passed to this function
164 * and will be included in the CLooG input file dump.
166 void cloog_program_dump_cloog(FILE * foo, CloogProgram * program,
167 CloogScatteringList *scattering)
169 int i;
170 CloogLoop * loop ;
171 CloogScatteringList *tmp_scatt;
173 fprintf(foo,
174 "# CLooG -> CLooG\n"
175 "# This is an automatic dump of a CLooG input file from a CloogProgram data\n"
176 "# structure. WARNING: it is highly dangerous and MAY be correct ONLY if\n"
177 "# - it has been dumped before loop generation.\n"
178 "# - option -noscalars is used (it removes scalar dimensions otherwise)\n"
179 "# - option -l is at least the original scattering dimension number\n"
180 "# ASK THE AUTHOR IF YOU *NEED* SOMETHING MORE ROBUST\n") ;
182 /* Language. */
183 if (program->language == 'c')
184 fprintf(foo,"# Language: C\n") ;
185 else
186 fprintf(foo,"# Language: FORTRAN\n") ;
187 fprintf(foo,"%c\n\n",program->language) ;
189 /* Context. */
190 fprintf(foo, "# Context (%d parameter(s)):\n", program->names->nb_parameters);
191 cloog_domain_print_constraints(foo, program->context, 0);
192 fprintf(foo,"1 # Parameter name(s)\n") ;
193 for (i=0;i<program->names->nb_parameters;i++)
194 fprintf(foo,"%s ",program->names->parameters[i]) ;
196 /* Statement number. */
197 i = 0 ;
198 loop = program->loop ;
199 while (loop != NULL)
200 { i++ ;
201 loop = loop->next ;
203 fprintf(foo,"\n\n# Statement number:\n%d\n\n",i) ;
205 /* Iteration domains. */
206 i = 1 ;
207 loop = program->loop ;
208 while (loop != NULL)
209 { /* Name of the domain. */
210 fprintf(foo,"# Iteration domain of statement %d.\n",i) ;
212 cloog_domain_print_constraints(foo, loop->domain, 1);
213 fprintf(foo,"0 0 0 # For future options.\n\n") ;
215 i++ ;
216 loop = loop->next ;
218 fprintf(foo,"\n1 # Iterator name(s)\n") ;
220 /* Scattering already applied? In this case print the scattering names as
221 * additional iterator names. */
222 if (!scattering)
223 for (i = 0; i < program->names->nb_scattering; i++)
224 fprintf(foo, "%s ", program->names->scattering[i]);
225 for (i=0;i<program->names->nb_iterators;i++)
226 fprintf(foo,"%s ",program->names->iterators[i]);
227 fprintf(foo,"\n\n") ;
229 /* Exit, if scattering is already applied. */
230 if (!scattering) {
231 fprintf(foo, "# No scattering functions.\n0\n\n");
232 return;
235 /* Scattering relations. */
236 fprintf(foo, "# --------------------- SCATTERING --------------------\n");
238 i = 0;
239 for (tmp_scatt = scattering; tmp_scatt; tmp_scatt = tmp_scatt->next)
240 i++;
242 fprintf(foo, "%d # Scattering functions", i);
244 for (tmp_scatt = scattering; tmp_scatt; tmp_scatt = tmp_scatt->next)
245 cloog_scattering_print_constraints(foo, tmp_scatt->scatt);
247 fprintf(foo, "\n1 # Scattering dimension name(s)\n");
249 for (i = 0; i < program->names->nb_scattering; i++)
250 fprintf(foo, "%s ", program->names->scattering[i]);
255 * cloog_program_print function:
256 * This function prints the content of a CloogProgram structure (program) into a
257 * file (file, possibly stdout).
258 * - July 1st 2005: Now this very old function (probably as old as CLooG) is
259 * only a frontend to cloog_program_print_structure, with a
260 * quite better human-readable representation.
262 void cloog_program_print(FILE * file, CloogProgram * program)
263 { cloog_program_print_structure(file,program,0) ;
267 static void print_comment(FILE *file, CloogOptions *options,
268 const char *fmt, ...)
270 va_list args;
272 va_start(args, fmt);
273 if (options->language == CLOOG_LANGUAGE_FORTRAN) {
274 fprintf(file, "! ");
275 vfprintf(file, fmt, args);
276 fprintf(file, "\n");
277 } else {
278 fprintf(file, "/* ");
279 vfprintf(file, fmt, args);
280 fprintf(file, " */\n");
284 static void print_macros(FILE *file)
286 fprintf(file, "/* Useful macros. */\n") ;
287 fprintf(file,
288 "#define floord(n,d) (((n)<0) ? -((-(n)+(d)-1)/(d)) : (n)/(d))\n");
289 fprintf(file,
290 "#define ceild(n,d) (((n)<0) ? -((-(n))/(d)) : ((n)+(d)-1)/(d))\n");
291 fprintf(file, "#define max(x,y) ((x) > (y) ? (x) : (y))\n") ;
292 fprintf(file, "#define min(x,y) ((x) < (y) ? (x) : (y))\n\n") ;
293 fprintf(file, "#ifdef TIME \n#define IF_TIME(foo) foo; \n"
294 "#else\n#define IF_TIME(foo)\n#endif\n\n");
297 static void print_declarations(FILE *file, int n, char **names, int indentation)
299 int i;
301 for (i = 0; i < indentation; i++)
302 fprintf(file, " ");
303 fprintf(file, "int %s", names[0]);
304 for (i = 1; i < n; i++)
305 fprintf(file, ", %s", names[i]);
306 fprintf(file, ";\n");
309 static void print_scattering_declarations(FILE *file, CloogProgram *program,
310 int indentation)
312 int i, j, found = 0;
313 int nb_scatnames = 0;
314 CloogNames *names = program->names;
316 // Copy pointer only to those scatering names that do not duplicate
317 // iterator names.
318 char **scatnames = (char **) malloc(sizeof(char *) * names->nb_scattering);
319 for (i = 0; i < names->nb_scattering; ++i) {
320 for (j = 0; j < names->nb_iterators; ++j) {
321 found = 0;
322 if (strcmp(names->scattering[i], names->iterators[j]) == 0) {
323 found = 1;
326 if (!found) {
327 // Save a pointer (intentional!) to the names in the new array.
328 scatnames[nb_scatnames++] = names->scattering[i];
332 if (nb_scatnames) {
333 for (i = 0; i < indentation; i++)
334 fprintf(file, " ");
335 fprintf(file, "/* Scattering iterators. */\n");
336 print_declarations(file, nb_scatnames, scatnames, indentation);
338 free(scatnames);
341 static void print_iterator_declarations(FILE *file, CloogProgram *program,
342 CloogOptions *options)
344 CloogNames *names = program->names;
346 print_scattering_declarations(file, program, 2);
347 if (names->nb_iterators) {
348 fprintf(file, " /* Original iterators. */\n");
349 print_declarations(file, names->nb_iterators, names->iterators, 2);
353 static void print_callable_preamble(FILE *file, CloogProgram *program,
354 CloogOptions *options)
356 int j;
357 CloogBlockList *blocklist;
358 CloogBlock *block;
359 CloogStatement *statement;
361 fprintf(file, "extern void hash(int);\n\n");
363 print_macros(file);
365 for (blocklist = program->blocklist; blocklist; blocklist = blocklist->next) {
366 block = blocklist->block;
367 for (statement = block->statement; statement; statement = statement->next) {
368 fprintf(file, "#define S%d(", statement->number);
369 if (block->depth > 0) {
370 fprintf(file, "%s", program->names->iterators[0]);
371 for(j = 1; j < block->depth; j++)
372 fprintf(file, ",%s", program->names->iterators[j]);
374 fprintf(file,") { hash(%d);", statement->number);
375 for(j = 0; j < block->depth; j++)
376 fprintf(file, " hash(%s);", program->names->iterators[j]);
377 fprintf(file, " }\n");
380 fprintf(file, "\nvoid test(");
381 if (program->names->nb_parameters > 0) {
382 fprintf(file, "int %s", program->names->parameters[0]);
383 for(j = 1; j < program->names->nb_parameters; j++)
384 fprintf(file, ", int %s", program->names->parameters[j]);
386 fprintf(file, ")\n{\n");
387 print_iterator_declarations(file, program, options);
390 static void print_callable_postamble(FILE *file, CloogProgram *program)
392 fprintf(file, "}\n");
395 #ifdef OSL_SUPPORT
396 static int get_osl_loop_flags (osl_scop_p scop) {
397 int flags = 0;
398 osl_loop_p ll = osl_generic_lookup(scop->extension, OSL_URI_LOOP);
399 while (ll) {
400 flags |= ll->directive;
401 ll = ll->next;
404 return flags;
407 static void print_iterator_declarations_osl(FILE *file, CloogProgram *program,
408 int indent, CloogOptions *options)
410 osl_coordinates_p co = NULL;
411 int i;
412 int loopflags = 0;
413 char* vecvar[2] = {"lbv", "ubv"};
414 char* parvar[2] = {"lbp", "ubp"};
416 osl_scop_p scop = options->scop;
417 CloogNames *names = program->names;
419 print_scattering_declarations(file, program, indent);
421 co = osl_generic_lookup(scop->extension, OSL_URI_COORDINATES);
422 if (co==NULL //if coordinates exist then iterators already declared in file
423 && names->nb_iterators) {
424 for (i = 0; i < indent; i++)
425 fprintf(file, " ");
426 fprintf(file, "/* Original iterators. */\n");
427 print_declarations(file, names->nb_iterators, names->iterators, indent);
430 loopflags = get_osl_loop_flags(scop);
431 if(loopflags & CLAST_PARALLEL_OMP)
432 print_declarations(file, 2, parvar, indent);
433 if(loopflags & CLAST_PARALLEL_VEC)
434 print_declarations(file, 2, vecvar, indent);
436 fprintf(file, "\n");
440 * add tags clast loops according to information in scop's osl_loop extension
442 static int annotate_loops(osl_scop_p program, struct clast_stmt *root){
444 int j, nclastloops, nclaststmts;
445 struct clast_for **clastloops = NULL;
446 int *claststmts = NULL;
447 int ret = 0;
449 if (program == NULL) {
450 return ret;
453 osl_loop_p ll = osl_generic_lookup(program->extension, OSL_URI_LOOP);
454 while (ll) {
455 //for each loop
456 osl_loop_p loop = ll;
457 ClastFilter filter = { loop->iter, loop->stmt_ids,
458 loop->nb_stmts, subset};
460 clast_filter(root, filter, &clastloops, &nclastloops,
461 &claststmts, &nclaststmts);
463 /* There should be at least one */
464 if (nclastloops==0) { //FROM PLUTO
465 /* Sometimes loops may disappear (1) tile size larger than trip count
466 * 2) it's a scalar dimension but can't be determined from the
467 * trans matrix */
468 printf("Warning: parallel poly loop not found in AST\n");
469 ll = ll->next;
470 continue;
472 for (j=0; j<nclastloops; j++) {
474 if (loop->directive & CLAST_PARALLEL_VEC) {
475 clastloops[j]->parallel |= CLAST_PARALLEL_VEC;
476 ret |= CLAST_PARALLEL_VEC;
479 if (loop->directive & CLAST_PARALLEL_OMP) {
480 clastloops[j]->parallel |= CLAST_PARALLEL_OMP;
481 ret |= CLAST_PARALLEL_OMP;
482 clastloops[j]->private_vars = strdup(loop->private_vars);
486 if (clastloops) { free(clastloops); clastloops=NULL;}
487 if (claststmts) { free(claststmts); claststmts=NULL;}
489 ll = ll->next;
492 return ret;
494 #endif
497 * cloog_program_osl_pprint function:
498 * this function pretty-prints the C or FORTRAN code generated from an
499 * OpenScop specification by overwriting SCoP in a given code, if the
500 * options -compilable or -callable are not set. The SCoP coordinates are
501 * provided through the OpenScop "Coordinates" extension. It returns 1 if
502 * it succeeds to find an OpenScop coordinates information
503 * to pretty-print the generated code, 0 otherwise.
504 * \param[in] file The output stream (possibly stdout).
505 * \param[in] program The generated pseudo-AST to pretty-print.
506 * \param[in] options CLooG options (contains the OpenSCop specification).
507 * \return 1 on success to pretty-print at the place of a SCoP, 0 otherwise.
509 int cloog_program_osl_pprint(FILE * file, CloogProgram * program,
510 CloogOptions * options) {
511 #ifdef OSL_SUPPORT
512 int lines = 0;
513 int columns = 0;
514 int read = 1;
515 int indentation = 0;
516 char c;
517 osl_scop_p scop = options->scop;
518 osl_coordinates_p coordinates;
519 struct clast_stmt *root;
520 FILE* original = NULL;
522 if (scop && !options->compilable && !options->callable) {
523 #ifdef CLOOG_RUSAGE
524 print_comment(file, options, "Generated from %s by %s in %.2fs.",
525 options->name, cloog_version(), options->time);
526 #else
527 print_comment(file, options, "Generated from %s by %s.",
528 options->name, cloog_version());
529 #endif
530 coordinates = osl_generic_lookup(scop->extension, OSL_URI_COORDINATES);
531 if (coordinates) {
532 original = fopen(coordinates->name, "r");
533 indentation = coordinates->indent;
534 if (!original) {
535 cloog_msg(options, CLOOG_WARNING,
536 "unable to open the file specified in the SCoP "
537 "coordinates\n");
538 coordinates = NULL;
542 /* Print the macros the generated code may need. */
543 print_macros(file);
545 /* Print what was before the SCoP in the original file (if any). */
546 if (coordinates) {
547 while (((lines < coordinates->line_start - 1) ||
548 (columns < coordinates->column_start - 1)) && (read != EOF)) {
549 read = fscanf(original, "%c", &c);
550 columns++;
551 if (read != EOF) {
552 if (c == '\n') {
553 lines++;
554 columns = 0;
556 fprintf(file, "%c", c);
560 /* Carriage return to preserve indentation if necessary. */
561 if (coordinates->column_start > 0)
562 fprintf(file, "\n");
565 /* Generate the clast from the pseudo-AST then pretty-print it. */
566 root = cloog_clast_create(program, options);
567 annotate_loops(options->scop, root);
568 print_iterator_declarations_osl(file, program, indentation, options);
569 clast_pprint(file, root, indentation, options);
570 cloog_clast_free(root);
572 /* Print what was after the SCoP in the original file (if any). */
573 if (coordinates) {
574 while (read != EOF) {
575 read = fscanf(original, "%c", &c);
576 columns++;
577 if (read != EOF) {
578 if (((lines == coordinates->line_end - 1) &&
579 (columns > coordinates->column_end)) ||
580 (lines > coordinates->line_end - 1))
581 fprintf(file, "%c", c);
582 if (c == '\n') {
583 lines++;
584 columns = 0;
589 fclose(original);
592 return 1;
594 #endif
595 return 0;
599 * cloog_program_pprint function:
600 * This function prints the content of a CloogProgram structure (program) into a
601 * file (file, possibly stdout), in a C-like language.
602 * - June 22nd 2005: Adaptation for GMP.
604 void cloog_program_pprint(file, program, options)
605 FILE * file ;
606 CloogProgram * program ;
607 CloogOptions * options ;
609 int i, j, indentation = 0;
610 CloogStatement * statement ;
611 CloogBlockList * blocklist ;
612 CloogBlock * block ;
613 struct clast_stmt *root;
615 if (cloog_program_osl_pprint(file, program, options))
616 return;
618 if (program->language == 'f')
619 options->language = CLOOG_LANGUAGE_FORTRAN ;
620 else
621 options->language = CLOOG_LANGUAGE_C ;
623 #ifdef CLOOG_RUSAGE
624 print_comment(file, options, "Generated from %s by %s in %.2fs.",
625 options->name, cloog_version(), options->time);
626 #else
627 print_comment(file, options, "Generated from %s by %s.",
628 options->name, cloog_version());
629 #endif
630 #ifdef CLOOG_MEMORY
631 print_comment(file, options, "CLooG asked for %d KBytes.", options->memory);
632 cloog_msg(CLOOG_INFO, "%.2fs and %dKB used for code generation.\n",
633 options->time,options->memory);
634 #endif
636 /* If the option "compilable" is set, we provide the whole stuff to generate
637 * a compilable code. This code just do nothing, but now the user can edit
638 * the source and set the statement macros and parameters values.
640 if (options->compilable && (program->language == 'c'))
641 { /* The headers. */
642 fprintf(file,"/* DON'T FORGET TO USE -lm OPTION TO COMPILE. */\n\n") ;
643 fprintf(file,"/* Useful headers. */\n") ;
644 fprintf(file,"#include <stdio.h>\n") ;
645 fprintf(file,"#include <stdlib.h>\n") ;
646 fprintf(file,"#include <math.h>\n\n") ;
648 /* The value of parameters. */
649 fprintf(file,"/* Parameter value. */\n") ;
650 for (i = 1; i <= program->names->nb_parameters; i++)
651 fprintf(file, "#define PARVAL%d %d\n", i, options->compilable);
653 /* The macros. */
654 print_macros(file);
656 /* The statement macros. */
657 fprintf(file,"/* Statement macros (please set). */\n") ;
658 blocklist = program->blocklist ;
659 while (blocklist != NULL)
660 { block = blocklist->block ;
661 statement = block->statement ;
662 while (statement != NULL)
663 { fprintf(file,"#define S%d(",statement->number) ;
664 if (block->depth > 0)
665 { fprintf(file,"%s",program->names->iterators[0]) ;
666 for(j=1;j<block->depth;j++)
667 fprintf(file,",%s",program->names->iterators[j]) ;
669 fprintf(file,") {total++;") ;
670 if (block->depth > 0) {
671 fprintf(file, " printf(\"S%d %%d", statement->number);
672 for(j=1;j<block->depth;j++)
673 fprintf(file, " %%d");
675 fprintf(file,"\\n\",%s",program->names->iterators[0]) ;
676 for(j=1;j<block->depth;j++)
677 fprintf(file,",%s",program->names->iterators[j]) ;
678 fprintf(file,");") ;
680 fprintf(file,"}\n") ;
682 statement = statement->next ;
684 blocklist = blocklist->next ;
687 /* The iterator and parameter declaration. */
688 fprintf(file,"\nint main() {\n") ;
689 print_iterator_declarations(file, program, options);
690 if (program->names->nb_parameters > 0)
691 { fprintf(file," /* Parameters. */\n") ;
692 fprintf(file, " int %s=PARVAL1",program->names->parameters[0]);
693 for(i=2;i<=program->names->nb_parameters;i++)
694 fprintf(file, ", %s=PARVAL%d", program->names->parameters[i-1], i);
696 fprintf(file,";\n");
698 fprintf(file," int total=0;\n");
699 fprintf(file,"\n") ;
701 /* And we adapt the identation. */
702 indentation += 2 ;
703 } else if (options->callable && program->language == 'c') {
704 print_callable_preamble(file, program, options);
705 indentation += 2;
708 root = cloog_clast_create(program, options);
709 clast_pprint(file, root, indentation, options);
710 cloog_clast_free(root);
712 /* The end of the compilable code in case of 'compilable' option. */
713 if (options->compilable && (program->language == 'c'))
715 fprintf(file, "\n printf(\"Number of integral points: %%d.\\n\",total);");
716 fprintf(file, "\n return 0;\n}\n");
717 } else if (options->callable && program->language == 'c')
718 print_callable_postamble(file, program);
722 /******************************************************************************
723 * Memory deallocation function *
724 ******************************************************************************/
728 * cloog_program_free function:
729 * This function frees the allocated memory for a CloogProgram structure.
731 void cloog_program_free(CloogProgram * program)
732 { cloog_names_free(program->names) ;
733 cloog_loop_free(program->loop) ;
734 cloog_domain_free(program->context) ;
735 cloog_block_list_free(program->blocklist) ;
736 if (program->scaldims != NULL)
737 free(program->scaldims) ;
739 free(program) ;
743 /******************************************************************************
744 * Reading function *
745 ******************************************************************************/
748 static void cloog_program_construct_block_list(CloogProgram *p)
750 CloogLoop *loop;
751 CloogBlockList **next = &p->blocklist;
753 for (loop = p->loop; loop; loop = loop->next) {
754 *next = cloog_block_list_alloc(loop->block);
755 next = &(*next)->next;
761 * Construct a CloogProgram structure from a given context and
762 * union domain representing the iteration domains and scattering functions.
764 CloogProgram *cloog_program_alloc(CloogDomain *context, CloogUnionDomain *ud,
765 CloogOptions *options)
767 int i;
768 char prefix[] = "c";
769 CloogScatteringList * scatteringl;
770 CloogNames *n;
771 CloogProgram * p ;
773 /* Memory allocation for the CloogProgram structure. */
774 p = cloog_program_malloc() ;
776 if (options->language == CLOOG_LANGUAGE_FORTRAN)
777 p->language = 'f';
778 else
779 p->language = 'c';
781 p->names = n = cloog_names_alloc();
783 /* We then read the context data. */
784 p->context = context;
785 n->nb_parameters = ud->n_name[CLOOG_PARAM];
787 /* First part of the CloogNames structure: the parameter names. */
788 if (ud->name[CLOOG_PARAM]) {
789 n->parameters = ud->name[CLOOG_PARAM];
790 ud->name[CLOOG_PARAM] = NULL;
791 } else
792 n->parameters = cloog_names_generate_items(n->nb_parameters, NULL,
793 FIRST_PARAMETER);
795 n->nb_iterators = ud->n_name[CLOOG_ITER];
796 if (ud->name[CLOOG_ITER]) {
797 n->iterators = ud->name[CLOOG_ITER];
798 ud->name[CLOOG_ITER] = NULL;
799 } else
800 n->iterators = cloog_names_generate_items(n->nb_iterators, NULL,
801 FIRST_ITERATOR);
803 if (ud->domain) {
804 CloogNamedDomainList *l;
805 CloogLoop **next = &p->loop;
806 CloogScatteringList **next_scat = &scatteringl;
808 scatteringl = NULL;
809 for (i = 0, l = ud->domain; l; ++i, l = l->next) {
810 *next = cloog_loop_from_domain(options->state, l->domain, i);
811 l->domain = NULL;
812 (*next)->block->statement->name = l->name;
813 (*next)->block->statement->usr = l->usr;
814 l->name = NULL;
816 if (l->scattering) {
817 *next_scat = ALLOC(CloogScatteringList);
818 (*next_scat)->scatt = l->scattering;
819 l->scattering = NULL;
820 (*next_scat)->next = NULL;
822 next_scat = &(*next_scat)->next;
825 next = &(*next)->next;
828 if (scatteringl != NULL) {
829 p->nb_scattdims = cloog_scattering_dimension(scatteringl->scatt,
830 p->loop->domain);
831 n->nb_scattering = p->nb_scattdims;
832 if (ud->name[CLOOG_SCAT]) {
833 n->scattering = ud->name[CLOOG_SCAT];
834 ud->name[CLOOG_SCAT] = NULL;
835 } else
836 n->scattering = cloog_names_generate_items(n->nb_scattering, prefix, -1);
838 /* The boolean array for scalar dimensions is created and set to 0. */
839 p->scaldims = (int *)malloc(p->nb_scattdims*(sizeof(int))) ;
840 if (p->scaldims == NULL)
841 cloog_die("memory overflow.\n");
842 for (i=0;i<p->nb_scattdims;i++)
843 p->scaldims[i] = 0 ;
845 /* We try to find blocks in the input problem to reduce complexity. */
846 if (!options->noblocks)
847 cloog_program_block(p, scatteringl, options);
848 if (!options->noscalars)
849 cloog_program_extract_scalars(p, scatteringl, options);
851 cloog_program_scatter(p, scatteringl, options);
852 cloog_scattering_list_free(scatteringl);
854 if (!options->noblocks)
855 p->loop = cloog_loop_block(p->loop, p->scaldims, p->nb_scattdims);
857 else
858 { p->nb_scattdims = 0 ;
859 p->scaldims = NULL ;
862 cloog_names_scalarize(p->names,p->nb_scattdims,p->scaldims) ;
864 cloog_program_construct_block_list(p);
866 else
867 { p->loop = NULL ;
868 p->blocklist = NULL ;
869 p->scaldims = NULL ;
872 cloog_union_domain_free(ud);
874 return(p) ;
879 * cloog_program_read function:
880 * This function read the informations to put in a CloogProgram structure from
881 * a file (file, possibly stdin). It returns a pointer to a CloogProgram
882 * structure containing the read informations.
883 * - October 25th 2001: first version.
884 * - September 9th 2002: - the big reading function is now split in several
885 * functions (one per read data structure).
886 * - adaptation to the new file format with naming.
888 CloogProgram *cloog_program_read(FILE *file, CloogOptions *options)
890 CloogInput *input;
891 CloogProgram *p;
893 input = cloog_input_read(file, options);
894 p = cloog_program_alloc(input->context, input->ud, options);
895 free(input);
897 return p;
901 /******************************************************************************
902 * Processing functions *
903 ******************************************************************************/
907 * cloog_program_malloc function:
908 * This function allocates the memory space for a CloogProgram structure and
909 * sets its fields with default values. Then it returns a pointer to the
910 * allocated space.
911 * - November 21th 2005: first version.
913 CloogProgram * cloog_program_malloc()
914 { CloogProgram * program ;
916 /* Memory allocation for the CloogProgram structure. */
917 program = (CloogProgram *)malloc(sizeof(CloogProgram)) ;
918 if (program == NULL)
919 cloog_die("memory overflow.\n");
921 /* We set the various fields with default values. */
922 program->language = 'c' ;
923 program->nb_scattdims = 0 ;
924 program->context = NULL ;
925 program->loop = NULL ;
926 program->names = NULL ;
927 program->blocklist = NULL ;
928 program->scaldims = NULL ;
929 program->usr = NULL;
931 return program ;
936 * cloog_program_generate function:
937 * This function calls the Quillere algorithm for loop scanning. (see the
938 * Quillere paper) and calls the loop simplification function.
939 * - depth is the loop depth we want to optimize (guard free as possible),
940 * the first loop depth is 1 and anegative value is the infinity depth.
941 * - sep_level is the level number where we want to start loop separation.
943 * - October 26th 2001: first version.
944 * - April 19th 2005: some basic fixes and memory usage feature.
945 * - April 29th 2005: (bug fix, bug found by DaeGon Kim) see case 2 below.
947 CloogProgram * cloog_program_generate(program, options)
948 CloogProgram * program ;
949 CloogOptions * options ;
951 #ifdef CLOOG_RUSAGE
952 float time;
953 struct rusage start, end ;
954 #endif
955 CloogLoop * loop ;
956 #ifdef CLOOG_MEMORY
957 char status_path[MAX_STRING_VAL] ;
958 FILE * status ;
960 /* We initialize the memory need to 0. */
961 options->memory = 0 ;
962 #endif
964 if (options->override)
966 cloog_msg(options, CLOOG_WARNING,
967 "you are using -override option, be aware that the "
968 "generated\n code may be incorrect.\n") ;
970 else
971 { /* Playing with options may be dangerous, here are two possible issues :
972 * 1. Using -l option less than scattering dimension number may lead to
973 * an illegal target code (since the scattering is not respected), if
974 * it is the case, we set -l depth to the first acceptable value.
976 if ((program->nb_scattdims > options->l) && (options->l >= 0))
978 cloog_msg(options, CLOOG_WARNING,
979 "-l depth is less than the scattering dimension number "
980 "(the \n generated code may be incorrect), it has been "
981 "automaticaly set\n to this value (use option -override "
982 "to override).\n") ;
983 options->l = program->nb_scattdims ;
986 /* 2. Using -f option greater than one while -l depth is greater than the
987 * scattering dimension number may lead to iteration duplication (try
988 * test/daegon_lu_osp.cloog with '-f 3' to test) because of the step 4b
989 * of the cloog_loop_generate function, if it is the case, we set -l to
990 * the first acceptable value.
992 if (((options->f > 1) || (options->f < 0)) &&
993 ((options->l > program->nb_scattdims) || (options->l < 0)))
995 cloog_msg(options, CLOOG_WARNING,
996 "-f depth is more than one, -l depth has been "
997 "automaticaly set\n to the scattering dimension number "
998 "(target code may have\n duplicated iterations), -l depth "
999 "has been automaticaly set to\n this value (use option "
1000 "-override to override).\n") ;
1001 options->l = program->nb_scattdims ;
1005 #ifdef CLOOG_RUSAGE
1006 getrusage(RUSAGE_SELF, &start) ;
1007 #endif
1008 if (program->loop != NULL)
1009 { loop = program->loop ;
1011 /* Here we go ! */
1012 loop = cloog_loop_generate(loop, program->context, 0, 0,
1013 program->scaldims,
1014 program->nb_scattdims,
1015 options);
1017 #ifdef CLOOG_MEMORY
1018 /* We read into the status file of the process how many memory it uses. */
1019 sprintf(status_path,"/proc/%d/status",getpid()) ;
1020 status = fopen(status_path, "r") ;
1021 while (fscanf(status,"%s",status_path) && strcmp(status_path,"VmData:")!=0);
1022 fscanf(status,"%d",&(options->memory)) ;
1023 fclose(status) ;
1024 #endif
1026 if ((!options->nosimplify) && (program->loop != NULL))
1027 loop = cloog_loop_simplify(loop, program->context, 0,
1028 program->nb_scattdims, options);
1030 program->loop = loop ;
1033 #ifdef CLOOG_RUSAGE
1034 getrusage(RUSAGE_SELF, &end) ;
1035 /* We calculate the time spent in code generation. */
1036 time = (end.ru_utime.tv_usec - start.ru_utime.tv_usec)/(float)(MEGA) ;
1037 time += (float)(end.ru_utime.tv_sec - start.ru_utime.tv_sec) ;
1038 options->time = time ;
1039 #endif
1041 return program ;
1046 * cloog_program_block function:
1047 * this function gives a last chance to the lazy user to consider statement
1048 * blocks instead of some statement lists where the whole list may be
1049 * considered as a single statement from a code generation point of view.
1050 * For instance two statements with the same iteration domain and the same
1051 * scattering functions may be considered as a block. This function is lazy
1052 * and can only find very simple forms of trivial blocks (see
1053 * cloog_domain_lazy_block function for more details). The useless loops and
1054 * scattering functions are removed and freed while the statement list of
1055 * according blocks are filled.
1056 * - program is the whole program structure (befaore applying scattering),
1057 * - scattering is the list of scattering functions.
1059 * - April 30th 2005: first attempt.
1060 * - June 10-11th 2005: first working version.
1062 void cloog_program_block(CloogProgram *program,
1063 CloogScatteringList *scattering, CloogOptions *options)
1064 { int blocked_reference=0, blocked=0, nb_blocked=0 ;
1065 CloogLoop * reference, * start, * loop ;
1066 CloogScatteringList * scatt_reference, * scatt_loop, * scatt_start;
1068 if ((program->loop == NULL) || (program->loop->next == NULL))
1069 return ;
1071 /* The process will use three variables for the linked list :
1072 * - 'start' is the starting point of a new block,
1073 * - 'reference' is the node of the block used for the block checking,
1074 * - 'loop' is the candidate to be inserted inside the block.
1075 * At the beginning of the process, the linked lists are as follow:
1076 * O------>O------>O------>O------>NULL
1077 * | |
1078 * start loop
1079 * reference
1082 reference = program->loop ;
1083 start = program->loop ;
1084 loop = reference->next ;
1085 scatt_reference = scattering ;
1086 scatt_start = scattering ;
1087 scatt_loop = scattering->next ;
1089 while (loop != NULL)
1090 { if (cloog_domain_lazy_equal(reference->domain,loop->domain) &&
1091 cloog_scattering_lazy_block(scatt_reference->scatt, scatt_loop->scatt,
1092 scattering,program->nb_scattdims))
1093 { /* If we find a block we update the links:
1094 * +---------------+
1095 * | v
1096 * O O------>O------>O------>NULL
1097 * | |
1098 * start loop
1099 * reference
1101 blocked = 1 ;
1102 nb_blocked ++ ;
1103 cloog_block_merge(start->block,loop->block); /* merge frees loop->block */
1104 loop->block = NULL ;
1105 start->next = loop->next ;
1106 scatt_start->next = scatt_loop->next ;
1108 else
1109 { /* If we didn't find a block, the next start of a block is updated:
1110 * O------>O------>O------>O------>NULL
1111 * | |
1112 * reference start
1113 * loop
1115 blocked= 0 ;
1116 start = loop ;
1117 scatt_start = scatt_loop ;
1120 /* If the reference node has been included into a block, we can free it. */
1121 if (blocked_reference)
1122 { reference->next = NULL ;
1123 cloog_loop_free(reference) ;
1124 cloog_scattering_free(scatt_reference->scatt);
1125 free(scatt_reference) ;
1128 /* The reference and the loop are now updated for the next try, the
1129 * starting position depends on the previous step.
1130 * O ? O------>O------>O------>NULL
1131 * | |
1132 * reference loop
1134 reference = loop ;
1135 loop = loop->next ;
1136 scatt_reference = scatt_loop ;
1137 scatt_loop = scatt_loop->next ;
1139 /* We mark the new reference as being blocked or not, if will be freed
1140 * during the next while loop execution.
1142 if (blocked)
1143 blocked_reference = 1 ;
1144 else
1145 blocked_reference = 0 ;
1148 /* We free the last blocked reference if any (since in the while loop it was
1149 * freed during the next loop execution, it was not possible to free the
1150 * last one inside).
1152 if (blocked_reference)
1153 { reference->next = NULL ;
1154 cloog_loop_free(reference) ;
1155 cloog_scattering_free(scatt_reference->scatt);
1156 free(scatt_reference) ;
1159 if (nb_blocked != 0)
1160 cloog_msg(options, CLOOG_INFO, "%d domains have been blocked.\n", nb_blocked);
1165 * cloog_program_extract_scalars function:
1166 * this functions finds and removes the dimensions of the scattering functions
1167 * when they are scalar (i.e. of the shape "dim + scalar = 0") for all
1168 * scattering functions. The reason is that the processing of such dimensions
1169 * is trivial and do not need neither a row and a column in the matrix
1170 * representation of the domain (this will save memory) neither the full
1171 * Quillere processing (this will save time). The scalar dimensions data are
1172 * dispatched in the CloogProgram structure (the boolean vector scaldims will
1173 * say which original dimensions are scalar or not) and to the CloogBlock
1174 * structures (each one has a scaldims vector that contains the scalar values).
1175 * - June 14th 2005: first developments.
1176 * - June 30th 2005: first version.
1178 void cloog_program_extract_scalars(CloogProgram *program,
1179 CloogScatteringList *scattering, CloogOptions *options)
1180 { int i, j, scalar, current, nb_scaldims=0 ;
1181 CloogScatteringList *start;
1182 CloogScattering *old;
1183 CloogLoop *loop;
1184 CloogBlock * block ;
1186 start = scattering ;
1188 for (i=0;i<program->nb_scattdims;i++)
1189 { scalar = 1 ;
1190 scattering = start ;
1191 while (scattering != NULL)
1192 { if (!cloog_scattering_lazy_isscalar(scattering->scatt, i, NULL))
1193 { scalar = 0 ;
1194 break ;
1196 scattering = scattering->next ;
1199 if (scalar)
1200 { nb_scaldims ++ ;
1201 program->scaldims[i] = 1 ;
1205 /* If there are no scalar dimensions, we can continue directly. */
1206 if (!nb_scaldims)
1207 return ;
1209 /* Otherwise, in each block, we have to put the number of scalar dimensions,
1210 * and to allocate the memory for the scalar values.
1212 for (loop = program->loop; loop; loop = loop->next) {
1213 block = loop->block;
1214 block->nb_scaldims = nb_scaldims ;
1215 block->scaldims = (cloog_int_t *)malloc(nb_scaldims*sizeof(cloog_int_t));
1216 for (i=0;i<nb_scaldims;i++)
1217 cloog_int_init(block->scaldims[i]);
1220 /* Then we have to fill these scalar values, so we can erase those dimensions
1221 * from the scattering functions. It's easier to begin with the last one,
1222 * since there would be an offset otherwise (if we remove the i^th dimension,
1223 * then the next one is not the (i+1)^th but still the i^th...).
1225 current = nb_scaldims - 1 ;
1226 for (i=program->nb_scattdims-1;i>=0;i--)
1227 if (program->scaldims[i])
1229 scattering = start ;
1230 for (loop = program->loop; loop; loop = loop->next) {
1231 block = loop->block;
1232 if (!cloog_scattering_lazy_isscalar(scattering->scatt, i,
1233 &block->scaldims[current])) {
1234 /* We should have found a scalar value: if not, there is an error. */
1235 cloog_die("dimension %d is not scalar as expected.\n", i);
1237 scattering = scattering->next ;
1240 scattering = start ;
1241 while (scattering != NULL) {
1242 old = scattering->scatt;
1243 scattering->scatt = cloog_scattering_erase_dimension(old, i);
1244 cloog_scattering_free(old);
1245 scattering = scattering->next ;
1247 current-- ;
1250 /* We postprocess the scaldims array in such a way that each entry is how
1251 * many scalar dimensions follows + 1 (the current one). This will make
1252 * some other processing easier (e.g. knowledge of some offsets).
1254 for (i=0;i<program->nb_scattdims-1;i++)
1255 { if (program->scaldims[i])
1256 { j = i + 1 ;
1257 while ((j < program->nb_scattdims) && program->scaldims[j])
1258 { program->scaldims[i] ++ ;
1259 j ++ ;
1264 if (nb_scaldims != 0)
1265 cloog_msg(options, CLOOG_INFO, "%d dimensions (over %d) are scalar.\n",
1266 nb_scaldims,program->nb_scattdims) ;
1271 * cloog_program_scatter function:
1272 * This function adds the scattering (scheduling) informations in a program.
1273 * If names is NULL, this function create names itself such that the i^th
1274 * name is ci.
1275 * - November 6th 2001: first version.
1277 void cloog_program_scatter(CloogProgram *program,
1278 CloogScatteringList *scattering, CloogOptions *options)
1279 { int scattering_dim, scattering_dim2, not_enough_constraints=0 ;
1280 CloogLoop * loop ;
1282 if ((program != NULL) && (scattering != NULL))
1283 { loop = program->loop ;
1285 /* We compute the scattering dimension and check it is >=0. */
1286 scattering_dim = cloog_scattering_dimension(scattering->scatt, loop->domain);
1287 if (scattering_dim < 0)
1288 cloog_die("scattering has not enough dimensions.\n");
1289 if (!cloog_scattering_fully_specified(scattering->scatt, loop->domain))
1290 not_enough_constraints ++ ;
1292 /* The scattering dimension may have been modified by scalar extraction. */
1293 scattering_dim = cloog_scattering_dimension(scattering->scatt, loop->domain);
1295 /* Finally we scatter all loops. */
1296 cloog_loop_scatter(loop, scattering->scatt);
1297 loop = loop->next ;
1298 scattering = scattering->next ;
1300 while ((loop != NULL) && (scattering != NULL))
1301 { scattering_dim2 = cloog_scattering_dimension(scattering->scatt,
1302 loop->domain);
1303 if (scattering_dim2 != scattering_dim)
1304 cloog_die("scattering dimensions are not the same.\n") ;
1305 if (!cloog_scattering_fully_specified(scattering->scatt, loop->domain))
1306 not_enough_constraints ++ ;
1308 cloog_loop_scatter(loop, scattering->scatt);
1309 loop = loop->next ;
1310 scattering = scattering->next ;
1312 if ((loop != NULL) || (scattering != NULL))
1313 cloog_msg(options, CLOOG_WARNING,
1314 "there is not a scattering for each statement.\n");
1316 if (not_enough_constraints)
1317 cloog_msg(options, CLOOG_WARNING, "not enough constraints for "
1318 "%d scattering function(s).\n",not_enough_constraints) ;