Enable OpenScop loop extension without coordinates extension
[cloog.git] / source / program.c
blobaea15fd9e50068c25affccf365ed23f28c33aa88
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_iterator_declarations(FILE *file, CloogProgram *program,
310 CloogOptions *options)
312 CloogNames *names = program->names;
314 if (names->nb_scattering) {
315 fprintf(file, " /* Scattering iterators. */\n");
316 print_declarations(file, names->nb_scattering, names->scattering, 2);
318 if (names->nb_iterators) {
319 fprintf(file, " /* Original iterators. */\n");
320 print_declarations(file, names->nb_iterators, names->iterators, 2);
324 static void print_callable_preamble(FILE *file, CloogProgram *program,
325 CloogOptions *options)
327 int j;
328 CloogBlockList *blocklist;
329 CloogBlock *block;
330 CloogStatement *statement;
332 fprintf(file, "extern void hash(int);\n\n");
334 print_macros(file);
336 for (blocklist = program->blocklist; blocklist; blocklist = blocklist->next) {
337 block = blocklist->block;
338 for (statement = block->statement; statement; statement = statement->next) {
339 fprintf(file, "#define S%d(", statement->number);
340 if (block->depth > 0) {
341 fprintf(file, "%s", program->names->iterators[0]);
342 for(j = 1; j < block->depth; j++)
343 fprintf(file, ",%s", program->names->iterators[j]);
345 fprintf(file,") { hash(%d);", statement->number);
346 for(j = 0; j < block->depth; j++)
347 fprintf(file, " hash(%s);", program->names->iterators[j]);
348 fprintf(file, " }\n");
351 fprintf(file, "\nvoid test(");
352 if (program->names->nb_parameters > 0) {
353 fprintf(file, "int %s", program->names->parameters[0]);
354 for(j = 1; j < program->names->nb_parameters; j++)
355 fprintf(file, ", int %s", program->names->parameters[j]);
357 fprintf(file, ")\n{\n");
358 print_iterator_declarations(file, program, options);
361 static void print_callable_postamble(FILE *file, CloogProgram *program)
363 fprintf(file, "}\n");
366 #ifdef OSL_SUPPORT
367 static int get_osl_loop_flags (osl_scop_p scop) {
368 int flags = 0;
369 osl_loop_p ll = osl_generic_lookup(scop->extension, OSL_URI_LOOP);
370 while (ll) {
371 flags |= ll->directive;
372 ll = ll->next;
375 return flags;
378 static void print_iterator_declarations_osl(FILE *file, CloogProgram *program,
379 int indent, CloogOptions *options)
381 osl_coordinates_p co = NULL;
382 int i;
383 int loopflags = 0;
384 char* vecvar[2] = {"lbv", "ubv"};
385 char* parvar[2] = {"lbp", "ubp"};
387 osl_scop_p scop = options->scop;
388 CloogNames *names = program->names;
390 if (names->nb_scattering) {
391 for (i = 0; i < indent; i++)
392 fprintf(file, " ");
393 fprintf(file, "/* Scattering iterators. */\n");
394 print_declarations(file, names->nb_scattering, names->scattering, indent);
397 co = osl_generic_lookup(scop->extension, OSL_URI_COORDINATES);
398 if (co==NULL //if coordinates exist then iterators already declared in file
399 && names->nb_iterators) {
400 for (i = 0; i < indent; i++)
401 fprintf(file, " ");
402 fprintf(file, "/* Original iterators. */\n");
403 print_declarations(file, names->nb_iterators, names->iterators, indent);
406 loopflags = get_osl_loop_flags(scop);
407 if(loopflags & CLAST_PARALLEL_OMP)
408 print_declarations(file, 2, parvar, indent);
409 if(loopflags & CLAST_PARALLEL_VEC)
410 print_declarations(file, 2, vecvar, indent);
412 fprintf(file, "\n");
416 * add tags clast loops according to information in scop's osl_loop extension
418 static int annotate_loops(osl_scop_p program, struct clast_stmt *root){
420 int j, nclastloops, nclaststmts;
421 struct clast_for **clastloops = NULL;
422 int *claststmts = NULL;
423 int ret = 0;
425 if (program == NULL) {
426 return ret;
429 osl_loop_p ll = osl_generic_lookup(program->extension, OSL_URI_LOOP);
430 while (ll) {
431 //for each loop
432 osl_loop_p loop = ll;
433 ClastFilter filter = { loop->iter, loop->stmt_ids,
434 loop->nb_stmts, subset};
436 clast_filter(root, filter, &clastloops, &nclastloops,
437 &claststmts, &nclaststmts);
439 /* There should be at least one */
440 if (nclastloops==0) { //FROM PLUTO
441 /* Sometimes loops may disappear (1) tile size larger than trip count
442 * 2) it's a scalar dimension but can't be determined from the
443 * trans matrix */
444 printf("Warning: parallel poly loop not found in AST\n");
445 ll = ll->next;
446 continue;
448 for (j=0; j<nclastloops; j++) {
450 if (loop->directive & CLAST_PARALLEL_VEC) {
451 clastloops[j]->parallel |= CLAST_PARALLEL_VEC;
452 ret |= CLAST_PARALLEL_VEC;
455 if (loop->directive & CLAST_PARALLEL_OMP) {
456 clastloops[j]->parallel |= CLAST_PARALLEL_OMP;
457 ret |= CLAST_PARALLEL_OMP;
458 clastloops[j]->private_vars = strdup(loop->private_vars);
462 if (clastloops) { free(clastloops); clastloops=NULL;}
463 if (claststmts) { free(claststmts); claststmts=NULL;}
465 ll = ll->next;
468 return ret;
470 #endif
473 * cloog_program_osl_pprint function:
474 * this function pretty-prints the C or FORTRAN code generated from an
475 * OpenScop specification by overwriting SCoP in a given code, if the
476 * options -compilable or -callable are not set. The SCoP coordinates are
477 * provided through the OpenScop "Coordinates" extension. It returns 1 if
478 * it succeeds to find an OpenScop coordinates information
479 * to pretty-print the generated code, 0 otherwise.
480 * \param[in] file The output stream (possibly stdout).
481 * \param[in] program The generated pseudo-AST to pretty-print.
482 * \param[in] options CLooG options (contains the OpenSCop specification).
483 * \return 1 on success to pretty-print at the place of a SCoP, 0 otherwise.
485 int cloog_program_osl_pprint(FILE * file, CloogProgram * program,
486 CloogOptions * options) {
487 #ifdef OSL_SUPPORT
488 int lines = 0;
489 int columns = 0;
490 int read = 1;
491 int indentation = 0;
492 char c;
493 osl_scop_p scop = options->scop;
494 osl_coordinates_p coordinates;
495 struct clast_stmt *root;
496 FILE* original = NULL;
498 if (scop && !options->compilable && !options->callable) {
499 #ifdef CLOOG_RUSAGE
500 print_comment(file, options, "Generated from %s by %s in %.2fs.",
501 options->name, cloog_version(), options->time);
502 #else
503 print_comment(file, options, "Generated from %s by %s.",
504 options->name, cloog_version());
505 #endif
506 coordinates = osl_generic_lookup(scop->extension, OSL_URI_COORDINATES);
507 if (coordinates) {
508 original = fopen(coordinates->name, "r");
509 indentation = coordinates->indent;
510 if (!original) {
511 cloog_msg(options, CLOOG_WARNING,
512 "unable to open the file specified in the SCoP "
513 "coordinates\n");
514 coordinates = NULL;
518 /* Print the macros the generated code may need. */
519 print_macros(file);
521 /* Print what was before the SCoP in the original file (if any). */
522 if (coordinates) {
523 while (((lines < coordinates->line_start - 1) ||
524 (columns < coordinates->column_start - 1)) && (read != EOF)) {
525 read = fscanf(original, "%c", &c);
526 columns++;
527 if (read != EOF) {
528 if (c == '\n') {
529 lines++;
530 columns = 0;
532 fprintf(file, "%c", c);
536 /* Carriage return to preserve indentation if necessary. */
537 if (coordinates->column_start > 0)
538 fprintf(file, "\n");
541 /* Generate the clast from the pseudo-AST then pretty-print it. */
542 root = cloog_clast_create(program, options);
543 annotate_loops(options->scop, root);
544 print_iterator_declarations_osl(file, program, indentation, options);
545 clast_pprint(file, root, indentation, options);
546 cloog_clast_free(root);
548 /* Print what was after the SCoP in the original file (if any). */
549 if (coordinates) {
550 while (read != EOF) {
551 read = fscanf(original, "%c", &c);
552 columns++;
553 if (read != EOF) {
554 if (((lines == coordinates->line_end - 1) &&
555 (columns > coordinates->column_end)) ||
556 (lines > coordinates->line_end - 1))
557 fprintf(file, "%c", c);
558 if (c == '\n') {
559 lines++;
560 columns = 0;
565 fclose(original);
568 return 1;
570 #endif
571 return 0;
575 * cloog_program_pprint function:
576 * This function prints the content of a CloogProgram structure (program) into a
577 * file (file, possibly stdout), in a C-like language.
578 * - June 22nd 2005: Adaptation for GMP.
580 void cloog_program_pprint(file, program, options)
581 FILE * file ;
582 CloogProgram * program ;
583 CloogOptions * options ;
585 int i, j, indentation = 0;
586 CloogStatement * statement ;
587 CloogBlockList * blocklist ;
588 CloogBlock * block ;
589 struct clast_stmt *root;
591 if (cloog_program_osl_pprint(file, program, options))
592 return;
594 if (program->language == 'f')
595 options->language = CLOOG_LANGUAGE_FORTRAN ;
596 else
597 options->language = CLOOG_LANGUAGE_C ;
599 #ifdef CLOOG_RUSAGE
600 print_comment(file, options, "Generated from %s by %s in %.2fs.",
601 options->name, cloog_version(), options->time);
602 #else
603 print_comment(file, options, "Generated from %s by %s.",
604 options->name, cloog_version());
605 #endif
606 #ifdef CLOOG_MEMORY
607 print_comment(file, options, "CLooG asked for %d KBytes.", options->memory);
608 cloog_msg(CLOOG_INFO, "%.2fs and %dKB used for code generation.\n",
609 options->time,options->memory);
610 #endif
612 /* If the option "compilable" is set, we provide the whole stuff to generate
613 * a compilable code. This code just do nothing, but now the user can edit
614 * the source and set the statement macros and parameters values.
616 if (options->compilable && (program->language == 'c'))
617 { /* The headers. */
618 fprintf(file,"/* DON'T FORGET TO USE -lm OPTION TO COMPILE. */\n\n") ;
619 fprintf(file,"/* Useful headers. */\n") ;
620 fprintf(file,"#include <stdio.h>\n") ;
621 fprintf(file,"#include <stdlib.h>\n") ;
622 fprintf(file,"#include <math.h>\n\n") ;
624 /* The value of parameters. */
625 fprintf(file,"/* Parameter value. */\n") ;
626 for (i = 1; i <= program->names->nb_parameters; i++)
627 fprintf(file, "#define PARVAL%d %d\n", i, options->compilable);
629 /* The macros. */
630 print_macros(file);
632 /* The statement macros. */
633 fprintf(file,"/* Statement macros (please set). */\n") ;
634 blocklist = program->blocklist ;
635 while (blocklist != NULL)
636 { block = blocklist->block ;
637 statement = block->statement ;
638 while (statement != NULL)
639 { fprintf(file,"#define S%d(",statement->number) ;
640 if (block->depth > 0)
641 { fprintf(file,"%s",program->names->iterators[0]) ;
642 for(j=1;j<block->depth;j++)
643 fprintf(file,",%s",program->names->iterators[j]) ;
645 fprintf(file,") {total++;") ;
646 if (block->depth > 0) {
647 fprintf(file, " printf(\"S%d %%d", statement->number);
648 for(j=1;j<block->depth;j++)
649 fprintf(file, " %%d");
651 fprintf(file,"\\n\",%s",program->names->iterators[0]) ;
652 for(j=1;j<block->depth;j++)
653 fprintf(file,",%s",program->names->iterators[j]) ;
654 fprintf(file,");") ;
656 fprintf(file,"}\n") ;
658 statement = statement->next ;
660 blocklist = blocklist->next ;
663 /* The iterator and parameter declaration. */
664 fprintf(file,"\nint main() {\n") ;
665 print_iterator_declarations(file, program, options);
666 if (program->names->nb_parameters > 0)
667 { fprintf(file," /* Parameters. */\n") ;
668 fprintf(file, " int %s=PARVAL1",program->names->parameters[0]);
669 for(i=2;i<=program->names->nb_parameters;i++)
670 fprintf(file, ", %s=PARVAL%d", program->names->parameters[i-1], i);
672 fprintf(file,";\n");
674 fprintf(file," int total=0;\n");
675 fprintf(file,"\n") ;
677 /* And we adapt the identation. */
678 indentation += 2 ;
679 } else if (options->callable && program->language == 'c') {
680 print_callable_preamble(file, program, options);
681 indentation += 2;
684 root = cloog_clast_create(program, options);
685 clast_pprint(file, root, indentation, options);
686 cloog_clast_free(root);
688 /* The end of the compilable code in case of 'compilable' option. */
689 if (options->compilable && (program->language == 'c'))
691 fprintf(file, "\n printf(\"Number of integral points: %%d.\\n\",total);");
692 fprintf(file, "\n return 0;\n}\n");
693 } else if (options->callable && program->language == 'c')
694 print_callable_postamble(file, program);
698 /******************************************************************************
699 * Memory deallocation function *
700 ******************************************************************************/
704 * cloog_program_free function:
705 * This function frees the allocated memory for a CloogProgram structure.
707 void cloog_program_free(CloogProgram * program)
708 { cloog_names_free(program->names) ;
709 cloog_loop_free(program->loop) ;
710 cloog_domain_free(program->context) ;
711 cloog_block_list_free(program->blocklist) ;
712 if (program->scaldims != NULL)
713 free(program->scaldims) ;
715 free(program) ;
719 /******************************************************************************
720 * Reading function *
721 ******************************************************************************/
724 static void cloog_program_construct_block_list(CloogProgram *p)
726 CloogLoop *loop;
727 CloogBlockList **next = &p->blocklist;
729 for (loop = p->loop; loop; loop = loop->next) {
730 *next = cloog_block_list_alloc(loop->block);
731 next = &(*next)->next;
737 * Construct a CloogProgram structure from a given context and
738 * union domain representing the iteration domains and scattering functions.
740 CloogProgram *cloog_program_alloc(CloogDomain *context, CloogUnionDomain *ud,
741 CloogOptions *options)
743 int i;
744 char prefix[] = "c";
745 CloogScatteringList * scatteringl;
746 CloogNames *n;
747 CloogProgram * p ;
749 /* Memory allocation for the CloogProgram structure. */
750 p = cloog_program_malloc() ;
752 if (options->language == CLOOG_LANGUAGE_FORTRAN)
753 p->language = 'f';
754 else
755 p->language = 'c';
757 p->names = n = cloog_names_alloc();
759 /* We then read the context data. */
760 p->context = context;
761 n->nb_parameters = ud->n_name[CLOOG_PARAM];
763 /* First part of the CloogNames structure: the parameter names. */
764 if (ud->name[CLOOG_PARAM]) {
765 n->parameters = ud->name[CLOOG_PARAM];
766 ud->name[CLOOG_PARAM] = NULL;
767 } else
768 n->parameters = cloog_names_generate_items(n->nb_parameters, NULL,
769 FIRST_PARAMETER);
771 n->nb_iterators = ud->n_name[CLOOG_ITER];
772 if (ud->name[CLOOG_ITER]) {
773 n->iterators = ud->name[CLOOG_ITER];
774 ud->name[CLOOG_ITER] = NULL;
775 } else
776 n->iterators = cloog_names_generate_items(n->nb_iterators, NULL,
777 FIRST_ITERATOR);
779 if (ud->domain) {
780 CloogNamedDomainList *l;
781 CloogLoop **next = &p->loop;
782 CloogScatteringList **next_scat = &scatteringl;
784 scatteringl = NULL;
785 for (i = 0, l = ud->domain; l; ++i, l = l->next) {
786 *next = cloog_loop_from_domain(options->state, l->domain, i);
787 l->domain = NULL;
788 (*next)->block->statement->name = l->name;
789 (*next)->block->statement->usr = l->usr;
790 l->name = NULL;
792 if (l->scattering) {
793 *next_scat = ALLOC(CloogScatteringList);
794 (*next_scat)->scatt = l->scattering;
795 l->scattering = NULL;
796 (*next_scat)->next = NULL;
798 next_scat = &(*next_scat)->next;
801 next = &(*next)->next;
804 if (scatteringl != NULL) {
805 p->nb_scattdims = cloog_scattering_dimension(scatteringl->scatt,
806 p->loop->domain);
807 n->nb_scattering = p->nb_scattdims;
808 if (ud->name[CLOOG_SCAT]) {
809 n->scattering = ud->name[CLOOG_SCAT];
810 ud->name[CLOOG_SCAT] = NULL;
811 } else
812 n->scattering = cloog_names_generate_items(n->nb_scattering, prefix, -1);
814 /* The boolean array for scalar dimensions is created and set to 0. */
815 p->scaldims = (int *)malloc(p->nb_scattdims*(sizeof(int))) ;
816 if (p->scaldims == NULL)
817 cloog_die("memory overflow.\n");
818 for (i=0;i<p->nb_scattdims;i++)
819 p->scaldims[i] = 0 ;
821 /* We try to find blocks in the input problem to reduce complexity. */
822 if (!options->noblocks)
823 cloog_program_block(p, scatteringl, options);
824 if (!options->noscalars)
825 cloog_program_extract_scalars(p, scatteringl, options);
827 cloog_program_scatter(p, scatteringl, options);
828 cloog_scattering_list_free(scatteringl);
830 if (!options->noblocks)
831 p->loop = cloog_loop_block(p->loop, p->scaldims, p->nb_scattdims);
833 else
834 { p->nb_scattdims = 0 ;
835 p->scaldims = NULL ;
838 cloog_names_scalarize(p->names,p->nb_scattdims,p->scaldims) ;
840 cloog_program_construct_block_list(p);
842 else
843 { p->loop = NULL ;
844 p->blocklist = NULL ;
845 p->scaldims = NULL ;
848 cloog_union_domain_free(ud);
850 return(p) ;
855 * cloog_program_read function:
856 * This function read the informations to put in a CloogProgram structure from
857 * a file (file, possibly stdin). It returns a pointer to a CloogProgram
858 * structure containing the read informations.
859 * - October 25th 2001: first version.
860 * - September 9th 2002: - the big reading function is now split in several
861 * functions (one per read data structure).
862 * - adaptation to the new file format with naming.
864 CloogProgram *cloog_program_read(FILE *file, CloogOptions *options)
866 CloogInput *input;
867 CloogProgram *p;
869 input = cloog_input_read(file, options);
870 p = cloog_program_alloc(input->context, input->ud, options);
871 free(input);
873 return p;
877 /******************************************************************************
878 * Processing functions *
879 ******************************************************************************/
883 * cloog_program_malloc function:
884 * This function allocates the memory space for a CloogProgram structure and
885 * sets its fields with default values. Then it returns a pointer to the
886 * allocated space.
887 * - November 21th 2005: first version.
889 CloogProgram * cloog_program_malloc()
890 { CloogProgram * program ;
892 /* Memory allocation for the CloogProgram structure. */
893 program = (CloogProgram *)malloc(sizeof(CloogProgram)) ;
894 if (program == NULL)
895 cloog_die("memory overflow.\n");
897 /* We set the various fields with default values. */
898 program->language = 'c' ;
899 program->nb_scattdims = 0 ;
900 program->context = NULL ;
901 program->loop = NULL ;
902 program->names = NULL ;
903 program->blocklist = NULL ;
904 program->scaldims = NULL ;
905 program->usr = NULL;
907 return program ;
912 * cloog_program_generate function:
913 * This function calls the Quillere algorithm for loop scanning. (see the
914 * Quillere paper) and calls the loop simplification function.
915 * - depth is the loop depth we want to optimize (guard free as possible),
916 * the first loop depth is 1 and anegative value is the infinity depth.
917 * - sep_level is the level number where we want to start loop separation.
919 * - October 26th 2001: first version.
920 * - April 19th 2005: some basic fixes and memory usage feature.
921 * - April 29th 2005: (bug fix, bug found by DaeGon Kim) see case 2 below.
923 CloogProgram * cloog_program_generate(program, options)
924 CloogProgram * program ;
925 CloogOptions * options ;
927 #ifdef CLOOG_RUSAGE
928 float time;
929 struct rusage start, end ;
930 #endif
931 CloogLoop * loop ;
932 #ifdef CLOOG_MEMORY
933 char status_path[MAX_STRING_VAL] ;
934 FILE * status ;
936 /* We initialize the memory need to 0. */
937 options->memory = 0 ;
938 #endif
940 if (options->override)
942 cloog_msg(options, CLOOG_WARNING,
943 "you are using -override option, be aware that the "
944 "generated\n code may be incorrect.\n") ;
946 else
947 { /* Playing with options may be dangerous, here are two possible issues :
948 * 1. Using -l option less than scattering dimension number may lead to
949 * an illegal target code (since the scattering is not respected), if
950 * it is the case, we set -l depth to the first acceptable value.
952 if ((program->nb_scattdims > options->l) && (options->l >= 0))
954 cloog_msg(options, CLOOG_WARNING,
955 "-l depth is less than the scattering dimension number "
956 "(the \n generated code may be incorrect), it has been "
957 "automaticaly set\n to this value (use option -override "
958 "to override).\n") ;
959 options->l = program->nb_scattdims ;
962 /* 2. Using -f option greater than one while -l depth is greater than the
963 * scattering dimension number may lead to iteration duplication (try
964 * test/daegon_lu_osp.cloog with '-f 3' to test) because of the step 4b
965 * of the cloog_loop_generate function, if it is the case, we set -l to
966 * the first acceptable value.
968 if (((options->f > 1) || (options->f < 0)) &&
969 ((options->l > program->nb_scattdims) || (options->l < 0)))
971 cloog_msg(options, CLOOG_WARNING,
972 "-f depth is more than one, -l depth has been "
973 "automaticaly set\n to the scattering dimension number "
974 "(target code may have\n duplicated iterations), -l depth "
975 "has been automaticaly set to\n this value (use option "
976 "-override to override).\n") ;
977 options->l = program->nb_scattdims ;
981 #ifdef CLOOG_RUSAGE
982 getrusage(RUSAGE_SELF, &start) ;
983 #endif
984 if (program->loop != NULL)
985 { loop = program->loop ;
987 /* Here we go ! */
988 loop = cloog_loop_generate(loop, program->context, 0, 0,
989 program->scaldims,
990 program->nb_scattdims,
991 options);
993 #ifdef CLOOG_MEMORY
994 /* We read into the status file of the process how many memory it uses. */
995 sprintf(status_path,"/proc/%d/status",getpid()) ;
996 status = fopen(status_path, "r") ;
997 while (fscanf(status,"%s",status_path) && strcmp(status_path,"VmData:")!=0);
998 fscanf(status,"%d",&(options->memory)) ;
999 fclose(status) ;
1000 #endif
1002 if ((!options->nosimplify) && (program->loop != NULL))
1003 loop = cloog_loop_simplify(loop, program->context, 0,
1004 program->nb_scattdims, options);
1006 program->loop = loop ;
1009 #ifdef CLOOG_RUSAGE
1010 getrusage(RUSAGE_SELF, &end) ;
1011 /* We calculate the time spent in code generation. */
1012 time = (end.ru_utime.tv_usec - start.ru_utime.tv_usec)/(float)(MEGA) ;
1013 time += (float)(end.ru_utime.tv_sec - start.ru_utime.tv_sec) ;
1014 options->time = time ;
1015 #endif
1017 return program ;
1022 * cloog_program_block function:
1023 * this function gives a last chance to the lazy user to consider statement
1024 * blocks instead of some statement lists where the whole list may be
1025 * considered as a single statement from a code generation point of view.
1026 * For instance two statements with the same iteration domain and the same
1027 * scattering functions may be considered as a block. This function is lazy
1028 * and can only find very simple forms of trivial blocks (see
1029 * cloog_domain_lazy_block function for more details). The useless loops and
1030 * scattering functions are removed and freed while the statement list of
1031 * according blocks are filled.
1032 * - program is the whole program structure (befaore applying scattering),
1033 * - scattering is the list of scattering functions.
1035 * - April 30th 2005: first attempt.
1036 * - June 10-11th 2005: first working version.
1038 void cloog_program_block(CloogProgram *program,
1039 CloogScatteringList *scattering, CloogOptions *options)
1040 { int blocked_reference=0, blocked=0, nb_blocked=0 ;
1041 CloogLoop * reference, * start, * loop ;
1042 CloogScatteringList * scatt_reference, * scatt_loop, * scatt_start;
1044 if ((program->loop == NULL) || (program->loop->next == NULL))
1045 return ;
1047 /* The process will use three variables for the linked list :
1048 * - 'start' is the starting point of a new block,
1049 * - 'reference' is the node of the block used for the block checking,
1050 * - 'loop' is the candidate to be inserted inside the block.
1051 * At the beginning of the process, the linked lists are as follow:
1052 * O------>O------>O------>O------>NULL
1053 * | |
1054 * start loop
1055 * reference
1058 reference = program->loop ;
1059 start = program->loop ;
1060 loop = reference->next ;
1061 scatt_reference = scattering ;
1062 scatt_start = scattering ;
1063 scatt_loop = scattering->next ;
1065 while (loop != NULL)
1066 { if (cloog_domain_lazy_equal(reference->domain,loop->domain) &&
1067 cloog_scattering_lazy_block(scatt_reference->scatt, scatt_loop->scatt,
1068 scattering,program->nb_scattdims))
1069 { /* If we find a block we update the links:
1070 * +---------------+
1071 * | v
1072 * O O------>O------>O------>NULL
1073 * | |
1074 * start loop
1075 * reference
1077 blocked = 1 ;
1078 nb_blocked ++ ;
1079 cloog_block_merge(start->block,loop->block); /* merge frees loop->block */
1080 loop->block = NULL ;
1081 start->next = loop->next ;
1082 scatt_start->next = scatt_loop->next ;
1084 else
1085 { /* If we didn't find a block, the next start of a block is updated:
1086 * O------>O------>O------>O------>NULL
1087 * | |
1088 * reference start
1089 * loop
1091 blocked= 0 ;
1092 start = loop ;
1093 scatt_start = scatt_loop ;
1096 /* If the reference node has been included into a block, we can free it. */
1097 if (blocked_reference)
1098 { reference->next = NULL ;
1099 cloog_loop_free(reference) ;
1100 cloog_scattering_free(scatt_reference->scatt);
1101 free(scatt_reference) ;
1104 /* The reference and the loop are now updated for the next try, the
1105 * starting position depends on the previous step.
1106 * O ? O------>O------>O------>NULL
1107 * | |
1108 * reference loop
1110 reference = loop ;
1111 loop = loop->next ;
1112 scatt_reference = scatt_loop ;
1113 scatt_loop = scatt_loop->next ;
1115 /* We mark the new reference as being blocked or not, if will be freed
1116 * during the next while loop execution.
1118 if (blocked)
1119 blocked_reference = 1 ;
1120 else
1121 blocked_reference = 0 ;
1124 /* We free the last blocked reference if any (since in the while loop it was
1125 * freed during the next loop execution, it was not possible to free the
1126 * last one inside).
1128 if (blocked_reference)
1129 { reference->next = NULL ;
1130 cloog_loop_free(reference) ;
1131 cloog_scattering_free(scatt_reference->scatt);
1132 free(scatt_reference) ;
1135 if (nb_blocked != 0)
1136 cloog_msg(options, CLOOG_INFO, "%d domains have been blocked.\n", nb_blocked);
1141 * cloog_program_extract_scalars function:
1142 * this functions finds and removes the dimensions of the scattering functions
1143 * when they are scalar (i.e. of the shape "dim + scalar = 0") for all
1144 * scattering functions. The reason is that the processing of such dimensions
1145 * is trivial and do not need neither a row and a column in the matrix
1146 * representation of the domain (this will save memory) neither the full
1147 * Quillere processing (this will save time). The scalar dimensions data are
1148 * dispatched in the CloogProgram structure (the boolean vector scaldims will
1149 * say which original dimensions are scalar or not) and to the CloogBlock
1150 * structures (each one has a scaldims vector that contains the scalar values).
1151 * - June 14th 2005: first developments.
1152 * - June 30th 2005: first version.
1154 void cloog_program_extract_scalars(CloogProgram *program,
1155 CloogScatteringList *scattering, CloogOptions *options)
1156 { int i, j, scalar, current, nb_scaldims=0 ;
1157 CloogScatteringList *start;
1158 CloogScattering *old;
1159 CloogLoop *loop;
1160 CloogBlock * block ;
1162 start = scattering ;
1164 for (i=0;i<program->nb_scattdims;i++)
1165 { scalar = 1 ;
1166 scattering = start ;
1167 while (scattering != NULL)
1168 { if (!cloog_scattering_lazy_isscalar(scattering->scatt, i, NULL))
1169 { scalar = 0 ;
1170 break ;
1172 scattering = scattering->next ;
1175 if (scalar)
1176 { nb_scaldims ++ ;
1177 program->scaldims[i] = 1 ;
1181 /* If there are no scalar dimensions, we can continue directly. */
1182 if (!nb_scaldims)
1183 return ;
1185 /* Otherwise, in each block, we have to put the number of scalar dimensions,
1186 * and to allocate the memory for the scalar values.
1188 for (loop = program->loop; loop; loop = loop->next) {
1189 block = loop->block;
1190 block->nb_scaldims = nb_scaldims ;
1191 block->scaldims = (cloog_int_t *)malloc(nb_scaldims*sizeof(cloog_int_t));
1192 for (i=0;i<nb_scaldims;i++)
1193 cloog_int_init(block->scaldims[i]);
1196 /* Then we have to fill these scalar values, so we can erase those dimensions
1197 * from the scattering functions. It's easier to begin with the last one,
1198 * since there would be an offset otherwise (if we remove the i^th dimension,
1199 * then the next one is not the (i+1)^th but still the i^th...).
1201 current = nb_scaldims - 1 ;
1202 for (i=program->nb_scattdims-1;i>=0;i--)
1203 if (program->scaldims[i])
1205 scattering = start ;
1206 for (loop = program->loop; loop; loop = loop->next) {
1207 block = loop->block;
1208 if (!cloog_scattering_lazy_isscalar(scattering->scatt, i,
1209 &block->scaldims[current])) {
1210 /* We should have found a scalar value: if not, there is an error. */
1211 cloog_die("dimension %d is not scalar as expected.\n", i);
1213 scattering = scattering->next ;
1216 scattering = start ;
1217 while (scattering != NULL) {
1218 old = scattering->scatt;
1219 scattering->scatt = cloog_scattering_erase_dimension(old, i);
1220 cloog_scattering_free(old);
1221 scattering = scattering->next ;
1223 current-- ;
1226 /* We postprocess the scaldims array in such a way that each entry is how
1227 * many scalar dimensions follows + 1 (the current one). This will make
1228 * some other processing easier (e.g. knowledge of some offsets).
1230 for (i=0;i<program->nb_scattdims-1;i++)
1231 { if (program->scaldims[i])
1232 { j = i + 1 ;
1233 while ((j < program->nb_scattdims) && program->scaldims[j])
1234 { program->scaldims[i] ++ ;
1235 j ++ ;
1240 if (nb_scaldims != 0)
1241 cloog_msg(options, CLOOG_INFO, "%d dimensions (over %d) are scalar.\n",
1242 nb_scaldims,program->nb_scattdims) ;
1247 * cloog_program_scatter function:
1248 * This function adds the scattering (scheduling) informations in a program.
1249 * If names is NULL, this function create names itself such that the i^th
1250 * name is ci.
1251 * - November 6th 2001: first version.
1253 void cloog_program_scatter(CloogProgram *program,
1254 CloogScatteringList *scattering, CloogOptions *options)
1255 { int scattering_dim, scattering_dim2, not_enough_constraints=0 ;
1256 CloogLoop * loop ;
1258 if ((program != NULL) && (scattering != NULL))
1259 { loop = program->loop ;
1261 /* We compute the scattering dimension and check it is >=0. */
1262 scattering_dim = cloog_scattering_dimension(scattering->scatt, loop->domain);
1263 if (scattering_dim < 0)
1264 cloog_die("scattering has not enough dimensions.\n");
1265 if (!cloog_scattering_fully_specified(scattering->scatt, loop->domain))
1266 not_enough_constraints ++ ;
1268 /* The scattering dimension may have been modified by scalar extraction. */
1269 scattering_dim = cloog_scattering_dimension(scattering->scatt, loop->domain);
1271 /* Finally we scatter all loops. */
1272 cloog_loop_scatter(loop, scattering->scatt);
1273 loop = loop->next ;
1274 scattering = scattering->next ;
1276 while ((loop != NULL) && (scattering != NULL))
1277 { scattering_dim2 = cloog_scattering_dimension(scattering->scatt,
1278 loop->domain);
1279 if (scattering_dim2 != scattering_dim)
1280 cloog_die("scattering dimensions are not the same.\n") ;
1281 if (!cloog_scattering_fully_specified(scattering->scatt, loop->domain))
1282 not_enough_constraints ++ ;
1284 cloog_loop_scatter(loop, scattering->scatt);
1285 loop = loop->next ;
1286 scattering = scattering->next ;
1288 if ((loop != NULL) || (scattering != NULL))
1289 cloog_msg(options, CLOOG_WARNING,
1290 "there is not a scattering for each statement.\n");
1292 if (not_enough_constraints)
1293 cloog_msg(options, CLOOG_WARNING, "not enough constraints for "
1294 "%d scattering function(s).\n",not_enough_constraints) ;