Declarations for omp private variables
[cloog.git] / source / program.c
blobc446291e2b5f8d995d822eb93d4714ecd3b218dc
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");
297 static void print_declarations(FILE *file, int n, char **names)
299 int i;
301 fprintf(file, " int %s", names[0]);
302 for (i = 1; i < n; i++)
303 fprintf(file, ", %s", names[i]);
305 fprintf(file, ";\n");
308 static void print_iterator_declarations(FILE *file, CloogProgram *program,
309 CloogOptions *options)
311 CloogNames *names = program->names;
313 if (names->nb_scattering) {
314 fprintf(file, " /* Scattering iterators. */\n");
315 print_declarations(file, names->nb_scattering, names->scattering);
317 if (names->nb_iterators) {
318 fprintf(file, " /* Original iterators. */\n");
319 print_declarations(file, names->nb_iterators, names->iterators);
323 static void print_callable_preamble(FILE *file, CloogProgram *program,
324 CloogOptions *options)
326 int j;
327 CloogBlockList *blocklist;
328 CloogBlock *block;
329 CloogStatement *statement;
331 fprintf(file, "extern void hash(int);\n\n");
333 print_macros(file);
335 for (blocklist = program->blocklist; blocklist; blocklist = blocklist->next) {
336 block = blocklist->block;
337 for (statement = block->statement; statement; statement = statement->next) {
338 fprintf(file, "#define S%d(", statement->number);
339 if (block->depth > 0) {
340 fprintf(file, "%s", program->names->iterators[0]);
341 for(j = 1; j < block->depth; j++)
342 fprintf(file, ",%s", program->names->iterators[j]);
344 fprintf(file,") { hash(%d);", statement->number);
345 for(j = 0; j < block->depth; j++)
346 fprintf(file, " hash(%s);", program->names->iterators[j]);
347 fprintf(file, " }\n");
350 fprintf(file, "\nvoid test(");
351 if (program->names->nb_parameters > 0) {
352 fprintf(file, "int %s", program->names->parameters[0]);
353 for(j = 1; j < program->names->nb_parameters; j++)
354 fprintf(file, ", int %s", program->names->parameters[j]);
356 fprintf(file, ")\n{\n");
357 print_iterator_declarations(file, program, options);
360 static void print_callable_postamble(FILE *file, CloogProgram *program)
362 fprintf(file, "}\n");
366 * add tags clast loops according to information in scop's osl_loop extension
368 int annotate_loops( osl_scop_p program , struct clast_stmt *root){
370 int j, nclastloops, nclaststmts;
371 struct clast_for **clastloops = NULL;
372 int *claststmts = NULL;
373 int ret = 0;
375 if (program == NULL) {
376 return ret;
379 osl_loop_p ll = osl_generic_lookup(program->extension, OSL_URI_LOOP);
380 while (ll) {
381 //for each loop
382 osl_loop_p loop = ll;
384 ClastFilter filter = {loop->iter, loop->stmt_ids,
385 loop->nb_stmts, subset};
386 clast_filter(root, filter, &clastloops, &nclastloops,
387 &claststmts, &nclaststmts);
389 /* There should be at least one */
390 if (nclastloops==0) { //FROM PLUTO
391 /* Sometimes loops may disappear (1) tile size larger than trip count
392 * 2) it's a scalar dimension but can't be determined from the
393 * trans matrix */
394 printf("Warning: parallel poly loop not found in AST\n");
395 ll = ll->next;
396 continue;
398 for (j=0; j<nclastloops; j++) {
400 if (loop->directive & CLAST_PARALLEL_VEC) {
401 clastloops[j]->parallel |= CLAST_PARALLEL_VEC;
402 ret |= CLAST_PARALLEL_VEC;
405 if (loop->directive & CLAST_PARALLEL_OMP) {
406 clastloops[j]->parallel |= CLAST_PARALLEL_OMP;
407 ret |= CLAST_PARALLEL_OMP;
408 clastloops[j]->private_vars = strdup(loop->private_vars);
412 if (clastloops) { free(clastloops); clastloops=NULL;}
413 if (claststmts) { free(claststmts); claststmts=NULL;}
415 ll = ll->next;
418 return ret;
423 * cloog_program_osl_pprint function:
424 * this function pretty-prints the C or FORTRAN code generated from an
425 * OpenScop specification by overwriting SCoP in a given code, if the
426 * options -compilable or -callable are not set. The SCoP coordinates are
427 * provided through the OpenScop "Coordinates" extension. It returns 1 if
428 * it succeeds to find an OpenScop coordinates information
429 * to pretty-print the generated code, 0 otherwise.
430 * \param[in] file The output stream (possibly stdout).
431 * \param[in] program The generated pseudo-AST to pretty-print.
432 * \param[in] options CLooG options (contains the OpenSCop specification).
433 * \return 1 on success to pretty-print at the place of a SCoP, 0 otherwise.
435 int cloog_program_osl_pprint(FILE * file, CloogProgram * program,
436 CloogOptions * options) {
437 #ifdef OSL_SUPPORT
438 int lines = 0;
439 int columns = 0;
440 int read = 1;
441 char c;
442 osl_scop_p scop = options->scop;
443 osl_coordinates_p coordinates;
444 char* parvar[2] = {"lbv", "ubv"};
445 struct clast_stmt *root;
446 FILE * original;
447 int annotate_result = 0;
449 if (scop && !options->compilable && !options->callable) {
450 coordinates = osl_generic_lookup(scop->extension, OSL_URI_COORDINATES);
451 if (coordinates) {
452 original = fopen(coordinates->name, "r");
453 if (!original) {
454 cloog_msg(options, CLOOG_WARNING,
455 "unable to open the file specified in the SCoP "
456 "coordinates\n");
457 return 0;
460 /* Print the macros the generated code may need. */
461 print_macros(file);
463 /* Print what was before the SCoP in the original file. */
464 while (((lines < coordinates->line_start - 1) ||
465 (columns < coordinates->column_start - 1)) && (read != EOF)) {
466 read = fscanf(original, "%c", &c);
467 columns++;
468 if (read != EOF) {
469 if (c == '\n') {
470 lines++;
471 columns = 0;
473 fprintf(file, "%c", c);
477 /* Carriage return to preserve indentation if necessary. */
478 if (coordinates->column_start > 0)
479 fprintf(file, "\n");
481 /* Generate the clast from the pseudo-AST then pretty-print it. */
482 root = cloog_clast_create(program, options);
483 annotate_result = annotate_loops(options->scop, root);
484 print_iterator_declarations(file, program, options);
485 if(annotate_result & CLAST_PARALLEL_VEC)
486 print_declarations(file, 2, parvar);
487 clast_pprint(file, root, coordinates->indent, options);
488 cloog_clast_free(root);
490 /* Print what was after the SCoP in the original file. */
491 while (read != EOF) {
492 read = fscanf(original, "%c", &c);
493 columns++;
494 if (read != EOF) {
495 if (((lines == coordinates->line_end - 1) &&
496 (columns > coordinates->column_end)) ||
497 (lines > coordinates->line_end - 1))
498 fprintf(file, "%c", c);
499 if (c == '\n') {
500 lines++;
501 columns = 0;
506 fclose(original);
507 return 1;
510 #endif
511 return 0;
515 * cloog_program_pprint function:
516 * This function prints the content of a CloogProgram structure (program) into a
517 * file (file, possibly stdout), in a C-like language.
518 * - June 22nd 2005: Adaptation for GMP.
520 void cloog_program_pprint(file, program, options)
521 FILE * file ;
522 CloogProgram * program ;
523 CloogOptions * options ;
525 int i, j, indentation = 0;
526 CloogStatement * statement ;
527 CloogBlockList * blocklist ;
528 CloogBlock * block ;
529 struct clast_stmt *root;
531 if (cloog_program_osl_pprint(file, program, options))
532 return;
534 if (program->language == 'f')
535 options->language = CLOOG_LANGUAGE_FORTRAN ;
536 else
537 options->language = CLOOG_LANGUAGE_C ;
539 #ifdef CLOOG_RUSAGE
540 print_comment(file, options, "Generated from %s by %s in %.2fs.",
541 options->name, cloog_version(), options->time);
542 #else
543 print_comment(file, options, "Generated from %s by %s.",
544 options->name, cloog_version());
545 #endif
546 #ifdef CLOOG_MEMORY
547 print_comment(file, options, "CLooG asked for %d KBytes.", options->memory);
548 cloog_msg(CLOOG_INFO, "%.2fs and %dKB used for code generation.\n",
549 options->time,options->memory);
550 #endif
552 /* If the option "compilable" is set, we provide the whole stuff to generate
553 * a compilable code. This code just do nothing, but now the user can edit
554 * the source and set the statement macros and parameters values.
556 if (options->compilable && (program->language == 'c'))
557 { /* The headers. */
558 fprintf(file,"/* DON'T FORGET TO USE -lm OPTION TO COMPILE. */\n\n") ;
559 fprintf(file,"/* Useful headers. */\n") ;
560 fprintf(file,"#include <stdio.h>\n") ;
561 fprintf(file,"#include <stdlib.h>\n") ;
562 fprintf(file,"#include <math.h>\n\n") ;
564 /* The value of parameters. */
565 fprintf(file,"/* Parameter value. */\n") ;
566 for (i = 1; i <= program->names->nb_parameters; i++)
567 fprintf(file, "#define PARVAL%d %d\n", i, options->compilable);
569 /* The macros. */
570 print_macros(file);
572 /* The statement macros. */
573 fprintf(file,"/* Statement macros (please set). */\n") ;
574 blocklist = program->blocklist ;
575 while (blocklist != NULL)
576 { block = blocklist->block ;
577 statement = block->statement ;
578 while (statement != NULL)
579 { fprintf(file,"#define S%d(",statement->number) ;
580 if (block->depth > 0)
581 { fprintf(file,"%s",program->names->iterators[0]) ;
582 for(j=1;j<block->depth;j++)
583 fprintf(file,",%s",program->names->iterators[j]) ;
585 fprintf(file,") {total++;") ;
586 if (block->depth > 0) {
587 fprintf(file, " printf(\"S%d %%d", statement->number);
588 for(j=1;j<block->depth;j++)
589 fprintf(file, " %%d");
591 fprintf(file,"\\n\",%s",program->names->iterators[0]) ;
592 for(j=1;j<block->depth;j++)
593 fprintf(file,",%s",program->names->iterators[j]) ;
594 fprintf(file,");") ;
596 fprintf(file,"}\n") ;
598 statement = statement->next ;
600 blocklist = blocklist->next ;
603 /* The iterator and parameter declaration. */
604 fprintf(file,"\nint main() {\n") ;
605 print_iterator_declarations(file, program, options);
606 if (program->names->nb_parameters > 0)
607 { fprintf(file," /* Parameters. */\n") ;
608 fprintf(file, " int %s=PARVAL1",program->names->parameters[0]);
609 for(i=2;i<=program->names->nb_parameters;i++)
610 fprintf(file, ", %s=PARVAL%d", program->names->parameters[i-1], i);
612 fprintf(file,";\n");
614 fprintf(file," int total=0;\n");
615 fprintf(file,"\n") ;
617 /* And we adapt the identation. */
618 indentation += 2 ;
619 } else if (options->callable && program->language == 'c') {
620 print_callable_preamble(file, program, options);
621 indentation += 2;
624 root = cloog_clast_create(program, options);
625 clast_pprint(file, root, indentation, options);
626 cloog_clast_free(root);
628 /* The end of the compilable code in case of 'compilable' option. */
629 if (options->compilable && (program->language == 'c'))
631 fprintf(file, "\n printf(\"Number of integral points: %%d.\\n\",total);");
632 fprintf(file, "\n return 0;\n}\n");
633 } else if (options->callable && program->language == 'c')
634 print_callable_postamble(file, program);
638 /******************************************************************************
639 * Memory deallocation function *
640 ******************************************************************************/
644 * cloog_program_free function:
645 * This function frees the allocated memory for a CloogProgram structure.
647 void cloog_program_free(CloogProgram * program)
648 { cloog_names_free(program->names) ;
649 cloog_loop_free(program->loop) ;
650 cloog_domain_free(program->context) ;
651 cloog_block_list_free(program->blocklist) ;
652 if (program->scaldims != NULL)
653 free(program->scaldims) ;
655 free(program) ;
659 /******************************************************************************
660 * Reading function *
661 ******************************************************************************/
664 static void cloog_program_construct_block_list(CloogProgram *p)
666 CloogLoop *loop;
667 CloogBlockList **next = &p->blocklist;
669 for (loop = p->loop; loop; loop = loop->next) {
670 *next = cloog_block_list_alloc(loop->block);
671 next = &(*next)->next;
677 * Construct a CloogProgram structure from a given context and
678 * union domain representing the iteration domains and scattering functions.
680 CloogProgram *cloog_program_alloc(CloogDomain *context, CloogUnionDomain *ud,
681 CloogOptions *options)
683 int i;
684 char prefix[] = "c";
685 CloogScatteringList * scatteringl;
686 CloogNames *n;
687 CloogProgram * p ;
689 /* Memory allocation for the CloogProgram structure. */
690 p = cloog_program_malloc() ;
692 if (options->language == CLOOG_LANGUAGE_FORTRAN)
693 p->language = 'f';
694 else
695 p->language = 'c';
697 p->names = n = cloog_names_alloc();
699 /* We then read the context data. */
700 p->context = context;
701 n->nb_parameters = ud->n_name[CLOOG_PARAM];
703 /* First part of the CloogNames structure: the parameter names. */
704 if (ud->name[CLOOG_PARAM]) {
705 n->parameters = ud->name[CLOOG_PARAM];
706 ud->name[CLOOG_PARAM] = NULL;
707 } else
708 n->parameters = cloog_names_generate_items(n->nb_parameters, NULL,
709 FIRST_PARAMETER);
711 n->nb_iterators = ud->n_name[CLOOG_ITER];
712 if (ud->name[CLOOG_ITER]) {
713 n->iterators = ud->name[CLOOG_ITER];
714 ud->name[CLOOG_ITER] = NULL;
715 } else
716 n->iterators = cloog_names_generate_items(n->nb_iterators, NULL,
717 FIRST_ITERATOR);
719 if (ud->domain) {
720 CloogNamedDomainList *l;
721 CloogLoop **next = &p->loop;
722 CloogScatteringList **next_scat = &scatteringl;
724 scatteringl = NULL;
725 for (i = 0, l = ud->domain; l; ++i, l = l->next) {
726 *next = cloog_loop_from_domain(options->state, l->domain, i);
727 l->domain = NULL;
728 (*next)->block->statement->name = l->name;
729 (*next)->block->statement->usr = l->usr;
730 l->name = NULL;
732 if (l->scattering) {
733 *next_scat = ALLOC(CloogScatteringList);
734 (*next_scat)->scatt = l->scattering;
735 l->scattering = NULL;
736 (*next_scat)->next = NULL;
738 next_scat = &(*next_scat)->next;
741 next = &(*next)->next;
744 if (scatteringl != NULL) {
745 p->nb_scattdims = cloog_scattering_dimension(scatteringl->scatt,
746 p->loop->domain);
747 n->nb_scattering = p->nb_scattdims;
748 if (ud->name[CLOOG_SCAT]) {
749 n->scattering = ud->name[CLOOG_SCAT];
750 ud->name[CLOOG_SCAT] = NULL;
751 } else
752 n->scattering = cloog_names_generate_items(n->nb_scattering, prefix, -1);
754 /* The boolean array for scalar dimensions is created and set to 0. */
755 p->scaldims = (int *)malloc(p->nb_scattdims*(sizeof(int))) ;
756 if (p->scaldims == NULL)
757 cloog_die("memory overflow.\n");
758 for (i=0;i<p->nb_scattdims;i++)
759 p->scaldims[i] = 0 ;
761 /* We try to find blocks in the input problem to reduce complexity. */
762 if (!options->noblocks)
763 cloog_program_block(p, scatteringl, options);
764 if (!options->noscalars)
765 cloog_program_extract_scalars(p, scatteringl, options);
767 cloog_program_scatter(p, scatteringl, options);
768 cloog_scattering_list_free(scatteringl);
770 if (!options->noblocks)
771 p->loop = cloog_loop_block(p->loop, p->scaldims, p->nb_scattdims);
773 else
774 { p->nb_scattdims = 0 ;
775 p->scaldims = NULL ;
778 cloog_names_scalarize(p->names,p->nb_scattdims,p->scaldims) ;
780 cloog_program_construct_block_list(p);
782 else
783 { p->loop = NULL ;
784 p->blocklist = NULL ;
785 p->scaldims = NULL ;
788 cloog_union_domain_free(ud);
790 return(p) ;
795 * cloog_program_read function:
796 * This function read the informations to put in a CloogProgram structure from
797 * a file (file, possibly stdin). It returns a pointer to a CloogProgram
798 * structure containing the read informations.
799 * - October 25th 2001: first version.
800 * - September 9th 2002: - the big reading function is now split in several
801 * functions (one per read data structure).
802 * - adaptation to the new file format with naming.
804 CloogProgram *cloog_program_read(FILE *file, CloogOptions *options)
806 CloogInput *input;
807 CloogProgram *p;
809 input = cloog_input_read(file, options);
810 p = cloog_program_alloc(input->context, input->ud, options);
811 free(input);
813 return p;
817 /******************************************************************************
818 * Processing functions *
819 ******************************************************************************/
823 * cloog_program_malloc function:
824 * This function allocates the memory space for a CloogProgram structure and
825 * sets its fields with default values. Then it returns a pointer to the
826 * allocated space.
827 * - November 21th 2005: first version.
829 CloogProgram * cloog_program_malloc()
830 { CloogProgram * program ;
832 /* Memory allocation for the CloogProgram structure. */
833 program = (CloogProgram *)malloc(sizeof(CloogProgram)) ;
834 if (program == NULL)
835 cloog_die("memory overflow.\n");
837 /* We set the various fields with default values. */
838 program->language = 'c' ;
839 program->nb_scattdims = 0 ;
840 program->context = NULL ;
841 program->loop = NULL ;
842 program->names = NULL ;
843 program->blocklist = NULL ;
844 program->scaldims = NULL ;
845 program->usr = NULL;
847 return program ;
852 * cloog_program_generate function:
853 * This function calls the Quillere algorithm for loop scanning. (see the
854 * Quillere paper) and calls the loop simplification function.
855 * - depth is the loop depth we want to optimize (guard free as possible),
856 * the first loop depth is 1 and anegative value is the infinity depth.
857 * - sep_level is the level number where we want to start loop separation.
859 * - October 26th 2001: first version.
860 * - April 19th 2005: some basic fixes and memory usage feature.
861 * - April 29th 2005: (bug fix, bug found by DaeGon Kim) see case 2 below.
863 CloogProgram * cloog_program_generate(program, options)
864 CloogProgram * program ;
865 CloogOptions * options ;
867 #ifdef CLOOG_RUSAGE
868 float time;
869 struct rusage start, end ;
870 #endif
871 CloogLoop * loop ;
872 #ifdef CLOOG_MEMORY
873 char status_path[MAX_STRING_VAL] ;
874 FILE * status ;
876 /* We initialize the memory need to 0. */
877 options->memory = 0 ;
878 #endif
880 if (options->override)
882 cloog_msg(options, CLOOG_WARNING,
883 "you are using -override option, be aware that the "
884 "generated\n code may be incorrect.\n") ;
886 else
887 { /* Playing with options may be dangerous, here are two possible issues :
888 * 1. Using -l option less than scattering dimension number may lead to
889 * an illegal target code (since the scattering is not respected), if
890 * it is the case, we set -l depth to the first acceptable value.
892 if ((program->nb_scattdims > options->l) && (options->l >= 0))
894 cloog_msg(options, CLOOG_WARNING,
895 "-l depth is less than the scattering dimension number "
896 "(the \n generated code may be incorrect), it has been "
897 "automaticaly set\n to this value (use option -override "
898 "to override).\n") ;
899 options->l = program->nb_scattdims ;
902 /* 2. Using -f option greater than one while -l depth is greater than the
903 * scattering dimension number may lead to iteration duplication (try
904 * test/daegon_lu_osp.cloog with '-f 3' to test) because of the step 4b
905 * of the cloog_loop_generate function, if it is the case, we set -l to
906 * the first acceptable value.
908 if (((options->f > 1) || (options->f < 0)) &&
909 ((options->l > program->nb_scattdims) || (options->l < 0)))
911 cloog_msg(options, CLOOG_WARNING,
912 "-f depth is more than one, -l depth has been "
913 "automaticaly set\n to the scattering dimension number "
914 "(target code may have\n duplicated iterations), -l depth "
915 "has been automaticaly set to\n this value (use option "
916 "-override to override).\n") ;
917 options->l = program->nb_scattdims ;
921 #ifdef CLOOG_RUSAGE
922 getrusage(RUSAGE_SELF, &start) ;
923 #endif
924 if (program->loop != NULL)
925 { loop = program->loop ;
927 /* Here we go ! */
928 loop = cloog_loop_generate(loop, program->context, 0, 0,
929 program->scaldims,
930 program->nb_scattdims,
931 options);
933 #ifdef CLOOG_MEMORY
934 /* We read into the status file of the process how many memory it uses. */
935 sprintf(status_path,"/proc/%d/status",getpid()) ;
936 status = fopen(status_path, "r") ;
937 while (fscanf(status,"%s",status_path) && strcmp(status_path,"VmData:")!=0);
938 fscanf(status,"%d",&(options->memory)) ;
939 fclose(status) ;
940 #endif
942 if ((!options->nosimplify) && (program->loop != NULL))
943 loop = cloog_loop_simplify(loop, program->context, 0,
944 program->nb_scattdims, options);
946 program->loop = loop ;
949 #ifdef CLOOG_RUSAGE
950 getrusage(RUSAGE_SELF, &end) ;
951 /* We calculate the time spent in code generation. */
952 time = (end.ru_utime.tv_usec - start.ru_utime.tv_usec)/(float)(MEGA) ;
953 time += (float)(end.ru_utime.tv_sec - start.ru_utime.tv_sec) ;
954 options->time = time ;
955 #endif
957 return program ;
962 * cloog_program_block function:
963 * this function gives a last chance to the lazy user to consider statement
964 * blocks instead of some statement lists where the whole list may be
965 * considered as a single statement from a code generation point of view.
966 * For instance two statements with the same iteration domain and the same
967 * scattering functions may be considered as a block. This function is lazy
968 * and can only find very simple forms of trivial blocks (see
969 * cloog_domain_lazy_block function for more details). The useless loops and
970 * scattering functions are removed and freed while the statement list of
971 * according blocks are filled.
972 * - program is the whole program structure (befaore applying scattering),
973 * - scattering is the list of scattering functions.
975 * - April 30th 2005: first attempt.
976 * - June 10-11th 2005: first working version.
978 void cloog_program_block(CloogProgram *program,
979 CloogScatteringList *scattering, CloogOptions *options)
980 { int blocked_reference=0, blocked=0, nb_blocked=0 ;
981 CloogLoop * reference, * start, * loop ;
982 CloogScatteringList * scatt_reference, * scatt_loop, * scatt_start;
984 if ((program->loop == NULL) || (program->loop->next == NULL))
985 return ;
987 /* The process will use three variables for the linked list :
988 * - 'start' is the starting point of a new block,
989 * - 'reference' is the node of the block used for the block checking,
990 * - 'loop' is the candidate to be inserted inside the block.
991 * At the beginning of the process, the linked lists are as follow:
992 * O------>O------>O------>O------>NULL
993 * | |
994 * start loop
995 * reference
998 reference = program->loop ;
999 start = program->loop ;
1000 loop = reference->next ;
1001 scatt_reference = scattering ;
1002 scatt_start = scattering ;
1003 scatt_loop = scattering->next ;
1005 while (loop != NULL)
1006 { if (cloog_domain_lazy_equal(reference->domain,loop->domain) &&
1007 cloog_scattering_lazy_block(scatt_reference->scatt, scatt_loop->scatt,
1008 scattering,program->nb_scattdims))
1009 { /* If we find a block we update the links:
1010 * +---------------+
1011 * | v
1012 * O O------>O------>O------>NULL
1013 * | |
1014 * start loop
1015 * reference
1017 blocked = 1 ;
1018 nb_blocked ++ ;
1019 cloog_block_merge(start->block,loop->block); /* merge frees loop->block */
1020 loop->block = NULL ;
1021 start->next = loop->next ;
1022 scatt_start->next = scatt_loop->next ;
1024 else
1025 { /* If we didn't find a block, the next start of a block is updated:
1026 * O------>O------>O------>O------>NULL
1027 * | |
1028 * reference start
1029 * loop
1031 blocked= 0 ;
1032 start = loop ;
1033 scatt_start = scatt_loop ;
1036 /* If the reference node has been included into a block, we can free it. */
1037 if (blocked_reference)
1038 { reference->next = NULL ;
1039 cloog_loop_free(reference) ;
1040 cloog_scattering_free(scatt_reference->scatt);
1041 free(scatt_reference) ;
1044 /* The reference and the loop are now updated for the next try, the
1045 * starting position depends on the previous step.
1046 * O ? O------>O------>O------>NULL
1047 * | |
1048 * reference loop
1050 reference = loop ;
1051 loop = loop->next ;
1052 scatt_reference = scatt_loop ;
1053 scatt_loop = scatt_loop->next ;
1055 /* We mark the new reference as being blocked or not, if will be freed
1056 * during the next while loop execution.
1058 if (blocked)
1059 blocked_reference = 1 ;
1060 else
1061 blocked_reference = 0 ;
1064 /* We free the last blocked reference if any (since in the while loop it was
1065 * freed during the next loop execution, it was not possible to free the
1066 * last one inside).
1068 if (blocked_reference)
1069 { reference->next = NULL ;
1070 cloog_loop_free(reference) ;
1071 cloog_scattering_free(scatt_reference->scatt);
1072 free(scatt_reference) ;
1075 if (nb_blocked != 0)
1076 cloog_msg(options, CLOOG_INFO, "%d domains have been blocked.\n", nb_blocked);
1081 * cloog_program_extract_scalars function:
1082 * this functions finds and removes the dimensions of the scattering functions
1083 * when they are scalar (i.e. of the shape "dim + scalar = 0") for all
1084 * scattering functions. The reason is that the processing of such dimensions
1085 * is trivial and do not need neither a row and a column in the matrix
1086 * representation of the domain (this will save memory) neither the full
1087 * Quillere processing (this will save time). The scalar dimensions data are
1088 * dispatched in the CloogProgram structure (the boolean vector scaldims will
1089 * say which original dimensions are scalar or not) and to the CloogBlock
1090 * structures (each one has a scaldims vector that contains the scalar values).
1091 * - June 14th 2005: first developments.
1092 * - June 30th 2005: first version.
1094 void cloog_program_extract_scalars(CloogProgram *program,
1095 CloogScatteringList *scattering, CloogOptions *options)
1096 { int i, j, scalar, current, nb_scaldims=0 ;
1097 CloogScatteringList *start;
1098 CloogScattering *old;
1099 CloogLoop *loop;
1100 CloogBlock * block ;
1102 start = scattering ;
1104 for (i=0;i<program->nb_scattdims;i++)
1105 { scalar = 1 ;
1106 scattering = start ;
1107 while (scattering != NULL)
1108 { if (!cloog_scattering_lazy_isscalar(scattering->scatt, i, NULL))
1109 { scalar = 0 ;
1110 break ;
1112 scattering = scattering->next ;
1115 if (scalar)
1116 { nb_scaldims ++ ;
1117 program->scaldims[i] = 1 ;
1121 /* If there are no scalar dimensions, we can continue directly. */
1122 if (!nb_scaldims)
1123 return ;
1125 /* Otherwise, in each block, we have to put the number of scalar dimensions,
1126 * and to allocate the memory for the scalar values.
1128 for (loop = program->loop; loop; loop = loop->next) {
1129 block = loop->block;
1130 block->nb_scaldims = nb_scaldims ;
1131 block->scaldims = (cloog_int_t *)malloc(nb_scaldims*sizeof(cloog_int_t));
1132 for (i=0;i<nb_scaldims;i++)
1133 cloog_int_init(block->scaldims[i]);
1136 /* Then we have to fill these scalar values, so we can erase those dimensions
1137 * from the scattering functions. It's easier to begin with the last one,
1138 * since there would be an offset otherwise (if we remove the i^th dimension,
1139 * then the next one is not the (i+1)^th but still the i^th...).
1141 current = nb_scaldims - 1 ;
1142 for (i=program->nb_scattdims-1;i>=0;i--)
1143 if (program->scaldims[i])
1145 scattering = start ;
1146 for (loop = program->loop; loop; loop = loop->next) {
1147 block = loop->block;
1148 if (!cloog_scattering_lazy_isscalar(scattering->scatt, i,
1149 &block->scaldims[current])) {
1150 /* We should have found a scalar value: if not, there is an error. */
1151 cloog_die("dimension %d is not scalar as expected.\n", i);
1153 scattering = scattering->next ;
1156 scattering = start ;
1157 while (scattering != NULL) {
1158 old = scattering->scatt;
1159 scattering->scatt = cloog_scattering_erase_dimension(old, i);
1160 cloog_scattering_free(old);
1161 scattering = scattering->next ;
1163 current-- ;
1166 /* We postprocess the scaldims array in such a way that each entry is how
1167 * many scalar dimensions follows + 1 (the current one). This will make
1168 * some other processing easier (e.g. knowledge of some offsets).
1170 for (i=0;i<program->nb_scattdims-1;i++)
1171 { if (program->scaldims[i])
1172 { j = i + 1 ;
1173 while ((j < program->nb_scattdims) && program->scaldims[j])
1174 { program->scaldims[i] ++ ;
1175 j ++ ;
1180 if (nb_scaldims != 0)
1181 cloog_msg(options, CLOOG_INFO, "%d dimensions (over %d) are scalar.\n",
1182 nb_scaldims,program->nb_scattdims) ;
1187 * cloog_program_scatter function:
1188 * This function adds the scattering (scheduling) informations in a program.
1189 * If names is NULL, this function create names itself such that the i^th
1190 * name is ci.
1191 * - November 6th 2001: first version.
1193 void cloog_program_scatter(CloogProgram *program,
1194 CloogScatteringList *scattering, CloogOptions *options)
1195 { int scattering_dim, scattering_dim2, not_enough_constraints=0 ;
1196 CloogLoop * loop ;
1198 if ((program != NULL) && (scattering != NULL))
1199 { loop = program->loop ;
1201 /* We compute the scattering dimension and check it is >=0. */
1202 scattering_dim = cloog_scattering_dimension(scattering->scatt, loop->domain);
1203 if (scattering_dim < 0)
1204 cloog_die("scattering has not enough dimensions.\n");
1205 if (!cloog_scattering_fully_specified(scattering->scatt, loop->domain))
1206 not_enough_constraints ++ ;
1208 /* The scattering dimension may have been modified by scalar extraction. */
1209 scattering_dim = cloog_scattering_dimension(scattering->scatt, loop->domain);
1211 /* Finally we scatter all loops. */
1212 cloog_loop_scatter(loop, scattering->scatt);
1213 loop = loop->next ;
1214 scattering = scattering->next ;
1216 while ((loop != NULL) && (scattering != NULL))
1217 { scattering_dim2 = cloog_scattering_dimension(scattering->scatt,
1218 loop->domain);
1219 if (scattering_dim2 != scattering_dim)
1220 cloog_die("scattering dimensions are not the same.\n") ;
1221 if (!cloog_scattering_fully_specified(scattering->scatt, loop->domain))
1222 not_enough_constraints ++ ;
1224 cloog_loop_scatter(loop, scattering->scatt);
1225 loop = loop->next ;
1226 scattering = scattering->next ;
1228 if ((loop != NULL) || (scattering != NULL))
1229 cloog_msg(options, CLOOG_WARNING,
1230 "there is not a scattering for each statement.\n");
1232 if (not_enough_constraints)
1233 cloog_msg(options, CLOOG_WARNING, "not enough constraints for "
1234 "%d scattering function(s).\n",not_enough_constraints) ;