2 /**-------------------------------------------------------------------**
4 **-------------------------------------------------------------------**
6 **-------------------------------------------------------------------**
7 ** First version: october 25th 2001 **
8 **-------------------------------------------------------------------**/
11 /******************************************************************************
12 * CLooG : the Chunky Loop Generator (experimental) *
13 ******************************************************************************
15 * Copyright (C) 2001-2005 Cedric Bastoul *
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. *
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. *
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 *
32 * CLooG, the Chunky Loop Generator *
33 * Written by Cedric Bastoul, Cedric.Bastoul@inria.fr *
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>
49 # include "../include/cloog/cloog.h"
51 # include <sys/resource.h>
54 #define ALLOC(type) (type*)malloc(sizeof(type))
58 #include <osl/extensions/coordinates.h>
61 /******************************************************************************
62 * Structure display function *
63 ******************************************************************************/
67 * cloog_program_print function:
68 * this function is a human-friendly way to display the CloogProgram data
69 * structure, it shows all the different fields and includes an indentation
70 * level (level) in order to work with others print_structure functions.
71 * - July 1st 2005: first version based on the old cloog_program_print function.
73 void cloog_program_print_structure(file
, program
, level
)
75 CloogProgram
* program
;
79 /* Go to the right level. */
80 for (i
=0; i
<level
; i
++)
83 fprintf(file
,"+-- CloogProgram\n") ;
86 for (i
=0; i
<=level
+1; i
++)
90 /* Print the language. */
91 for (i
=0; i
<=level
; i
++)
93 fprintf(file
, "Language: %c\n",program
->language
) ;
96 for (i
=0; i
<=level
+1; i
++)
100 /* Print the scattering dimension number. */
101 for (i
=0; i
<=level
; i
++)
102 fprintf(file
,"|\t") ;
103 fprintf(file
,"Scattering dimension number: %d\n",program
->nb_scattdims
) ;
106 for (i
=0; i
<=level
+1; i
++)
107 fprintf(file
,"|\t") ;
110 /* Print the scalar scattering dimension informations. */
111 for (i
=0; i
<=level
; i
++)
112 fprintf(file
,"|\t") ;
113 if (program
->scaldims
!= NULL
)
114 { fprintf(file
,"Scalar dimensions:") ;
115 for (i
=0;i
<program
->nb_scattdims
;i
++)
116 fprintf(file
," %d:%d ",i
,program
->scaldims
[i
]) ;
120 fprintf(file
,"No scalar scattering dimensions\n") ;
123 for (i
=0; i
<=level
+1; i
++)
124 fprintf(file
,"|\t") ;
127 /* Print the parameter and the iterator names. */
128 cloog_names_print_structure(file
,program
->names
,level
+1) ;
131 for (i
=0; i
<=level
+1; i
++)
132 fprintf(file
,"|\t") ;
135 /* Print the context. */
136 cloog_domain_print_structure(file
, program
->context
, level
+1, "Context");
138 /* Print the loop. */
139 cloog_loop_print_structure(file
,program
->loop
,level
+1) ;
141 /* One more time something that is here only for a better look. */
143 { for (i
=0; i
<=level
; i
++)
144 fprintf(file
,"|\t") ;
152 * cloog_program_dump_cloog function:
153 * This function dumps a CloogProgram structure supposed to be completely
154 * filled in a CLooG input file (foo possibly stdout) such as CLooG can
155 * rebuild almost exactly the data structure from the input file.
157 * If the scattering is already applied, the scattering parameter is supposed to
158 * be NULL. In this case the number of scattering functions is lost, since they
159 * are included inside the iteration domains. This can only lead to a less
160 * beautiful pretty printing.
162 * In case the scattering is not yet applied it can be passed to this function
163 * and will be included in the CLooG input file dump.
165 void cloog_program_dump_cloog(FILE * foo
, CloogProgram
* program
,
166 CloogScatteringList
*scattering
)
170 CloogScatteringList
*tmp_scatt
;
174 "# This is an automatic dump of a CLooG input file from a CloogProgram data\n"
175 "# structure. WARNING: it is highly dangerous and MAY be correct ONLY if\n"
176 "# - it has been dumped before loop generation.\n"
177 "# - option -noscalars is used (it removes scalar dimensions otherwise)\n"
178 "# - option -l is at least the original scattering dimension number\n"
179 "# ASK THE AUTHOR IF YOU *NEED* SOMETHING MORE ROBUST\n") ;
182 if (program
->language
== 'c')
183 fprintf(foo
,"# Language: C\n") ;
185 fprintf(foo
,"# Language: FORTRAN\n") ;
186 fprintf(foo
,"%c\n\n",program
->language
) ;
189 fprintf(foo
, "# Context (%d parameter(s)):\n", program
->names
->nb_parameters
);
190 cloog_domain_print_constraints(foo
, program
->context
, 0);
191 fprintf(foo
,"1 # Parameter name(s)\n") ;
192 for (i
=0;i
<program
->names
->nb_parameters
;i
++)
193 fprintf(foo
,"%s ",program
->names
->parameters
[i
]) ;
195 /* Statement number. */
197 loop
= program
->loop
;
202 fprintf(foo
,"\n\n# Statement number:\n%d\n\n",i
) ;
204 /* Iteration domains. */
206 loop
= program
->loop
;
208 { /* Name of the domain. */
209 fprintf(foo
,"# Iteration domain of statement %d.\n",i
) ;
211 cloog_domain_print_constraints(foo
, loop
->domain
, 1);
212 fprintf(foo
,"0 0 0 # For future options.\n\n") ;
217 fprintf(foo
,"\n1 # Iterator name(s)\n") ;
219 /* Scattering already applied? In this case print the scattering names as
220 * additional iterator names. */
222 for (i
= 0; i
< program
->names
->nb_scattering
; i
++)
223 fprintf(foo
, "%s ", program
->names
->scattering
[i
]);
224 for (i
=0;i
<program
->names
->nb_iterators
;i
++)
225 fprintf(foo
,"%s ",program
->names
->iterators
[i
]);
226 fprintf(foo
,"\n\n") ;
228 /* Exit, if scattering is already applied. */
230 fprintf(foo
, "# No scattering functions.\n0\n\n");
234 /* Scattering relations. */
235 fprintf(foo
, "# --------------------- SCATTERING --------------------\n");
238 for (tmp_scatt
= scattering
; tmp_scatt
; tmp_scatt
= tmp_scatt
->next
)
241 fprintf(foo
, "%d # Scattering functions", i
);
243 for (tmp_scatt
= scattering
; tmp_scatt
; tmp_scatt
= tmp_scatt
->next
)
244 cloog_scattering_print_constraints(foo
, tmp_scatt
->scatt
);
246 fprintf(foo
, "\n1 # Scattering dimension name(s)\n");
248 for (i
= 0; i
< program
->names
->nb_scattering
; i
++)
249 fprintf(foo
, "%s ", program
->names
->scattering
[i
]);
254 * cloog_program_print function:
255 * This function prints the content of a CloogProgram structure (program) into a
256 * file (file, possibly stdout).
257 * - July 1st 2005: Now this very old function (probably as old as CLooG) is
258 * only a frontend to cloog_program_print_structure, with a
259 * quite better human-readable representation.
261 void cloog_program_print(FILE * file
, CloogProgram
* program
)
262 { cloog_program_print_structure(file
,program
,0) ;
266 static void print_comment(FILE *file
, CloogOptions
*options
,
267 const char *fmt
, ...)
272 if (options
->language
== CLOOG_LANGUAGE_FORTRAN
) {
274 vfprintf(file
, fmt
, args
);
277 fprintf(file
, "/* ");
278 vfprintf(file
, fmt
, args
);
279 fprintf(file
, " */\n");
283 static void print_macros(FILE *file
)
285 fprintf(file
, "/* Useful macros. */\n") ;
287 "#define floord(n,d) (((n)<0) ? -((-(n)+(d)-1)/(d)) : (n)/(d))\n");
289 "#define ceild(n,d) (((n)<0) ? -((-(n))/(d)) : ((n)+(d)-1)/(d))\n");
290 fprintf(file
, "#define max(x,y) ((x) > (y) ? (x) : (y))\n") ;
291 fprintf(file
, "#define min(x,y) ((x) < (y) ? (x) : (y))\n\n") ;
294 static void print_declarations(FILE *file
, int n
, char **names
)
298 fprintf(file
, " int %s", names
[0]);
299 for (i
= 1; i
< n
; i
++)
300 fprintf(file
, ", %s", names
[i
]);
302 fprintf(file
, ";\n");
305 static void print_iterator_declarations(FILE *file
, CloogProgram
*program
,
306 CloogOptions
*options
)
308 CloogNames
*names
= program
->names
;
310 if (names
->nb_scattering
) {
311 fprintf(file
, " /* Scattering iterators. */\n");
312 print_declarations(file
, names
->nb_scattering
, names
->scattering
);
314 if (names
->nb_iterators
) {
315 fprintf(file
, " /* Original iterators. */\n");
316 print_declarations(file
, names
->nb_iterators
, names
->iterators
);
320 static void print_callable_preamble(FILE *file
, CloogProgram
*program
,
321 CloogOptions
*options
)
324 CloogBlockList
*blocklist
;
326 CloogStatement
*statement
;
328 fprintf(file
, "extern void hash(int);\n\n");
332 for (blocklist
= program
->blocklist
; blocklist
; blocklist
= blocklist
->next
) {
333 block
= blocklist
->block
;
334 for (statement
= block
->statement
; statement
; statement
= statement
->next
) {
335 fprintf(file
, "#define S%d(", statement
->number
);
336 if (block
->depth
> 0) {
337 fprintf(file
, "%s", program
->names
->iterators
[0]);
338 for(j
= 1; j
< block
->depth
; j
++)
339 fprintf(file
, ",%s", program
->names
->iterators
[j
]);
341 fprintf(file
,") { hash(%d);", statement
->number
);
342 for(j
= 0; j
< block
->depth
; j
++)
343 fprintf(file
, " hash(%s);", program
->names
->iterators
[j
]);
344 fprintf(file
, " }\n");
347 fprintf(file
, "\nvoid test(");
348 if (program
->names
->nb_parameters
> 0) {
349 fprintf(file
, "int %s", program
->names
->parameters
[0]);
350 for(j
= 1; j
< program
->names
->nb_parameters
; j
++)
351 fprintf(file
, ", int %s", program
->names
->parameters
[j
]);
353 fprintf(file
, ")\n{\n");
354 print_iterator_declarations(file
, program
, options
);
357 static void print_callable_postamble(FILE *file
, CloogProgram
*program
)
359 fprintf(file
, "}\n");
363 * cloog_program_osl_pprint function:
364 * this function pretty-prints the C or FORTRAN code generated from an
365 * OpenScop specification by overwriting SCoP in a given code, if the
366 * options -compilable or -callable are not set. The SCoP coordinates are
367 * provided through the OpenScop "Coordinates" extension. It returns 1 if
368 * it succeeds to find an OpenScop coordinates information
369 * to pretty-print the generated code, 0 otherwise.
370 * \param[in] file The output stream (possibly stdout).
371 * \param[in] program The generated pseudo-AST to pretty-print.
372 * \param[in] options CLooG options (contains the OpenSCop specification).
373 * \return 1 on success to pretty-print at the place of a SCoP, 0 otherwise.
375 int cloog_program_osl_pprint(FILE * file
, CloogProgram
* program
,
376 CloogOptions
* options
) {
382 osl_scop_p scop
= options
->scop
;
383 osl_coordinates_p coordinates
;
384 struct clast_stmt
*root
;
387 if (scop
&& !options
->compilable
&& !options
->callable
) {
388 coordinates
= osl_generic_lookup(scop
->extension
, OSL_URI_COORDINATES
);
390 original
= fopen(coordinates
->name
, "r");
392 cloog_msg(options
, CLOOG_WARNING
,
393 "unable to open the file specified in the SCoP "
398 /* Print the macros the generated code may need. */
401 /* Print what was before the SCoP in the original file. */
402 while (((lines
< coordinates
->line_start
- 1) ||
403 (columns
< coordinates
->column_start
- 1)) && (read
!= EOF
)) {
404 read
= fscanf(original
, "%c", &c
);
411 fprintf(file
, "%c", c
);
415 /* Carriage return to preserve indentation if necessary. */
416 if (coordinates
->column_start
> 0)
419 /* Generate the clast from the pseudo-AST then pretty-print it. */
420 root
= cloog_clast_create(program
, options
);
421 clast_pprint(file
, root
, coordinates
->indent
, options
);
422 cloog_clast_free(root
);
424 /* Print what was after the SCoP in the original file. */
425 while (read
!= EOF
) {
426 read
= fscanf(original
, "%c", &c
);
429 if (((lines
== coordinates
->line_end
- 1) &&
430 (columns
> coordinates
->column_end
)) ||
431 (lines
> coordinates
->line_end
- 1))
432 fprintf(file
, "%c", c
);
449 * cloog_program_pprint function:
450 * This function prints the content of a CloogProgram structure (program) into a
451 * file (file, possibly stdout), in a C-like language.
452 * - June 22nd 2005: Adaptation for GMP.
454 void cloog_program_pprint(file
, program
, options
)
456 CloogProgram
* program
;
457 CloogOptions
* options
;
459 int i
, j
, indentation
= 0;
460 CloogStatement
* statement
;
461 CloogBlockList
* blocklist
;
463 struct clast_stmt
*root
;
465 if (cloog_program_osl_pprint(file
, program
, options
))
468 if (program
->language
== 'f')
469 options
->language
= CLOOG_LANGUAGE_FORTRAN
;
471 options
->language
= CLOOG_LANGUAGE_C
;
474 print_comment(file
, options
, "Generated from %s by %s in %.2fs.",
475 options
->name
, cloog_version(), options
->time
);
477 print_comment(file
, options
, "Generated from %s by %s.",
478 options
->name
, cloog_version());
481 print_comment(file
, options
, "CLooG asked for %d KBytes.", options
->memory
);
482 cloog_msg(CLOOG_INFO
, "%.2fs and %dKB used for code generation.\n",
483 options
->time
,options
->memory
);
486 /* If the option "compilable" is set, we provide the whole stuff to generate
487 * a compilable code. This code just do nothing, but now the user can edit
488 * the source and set the statement macros and parameters values.
490 if (options
->compilable
&& (program
->language
== 'c'))
492 fprintf(file
,"/* DON'T FORGET TO USE -lm OPTION TO COMPILE. */\n\n") ;
493 fprintf(file
,"/* Useful headers. */\n") ;
494 fprintf(file
,"#include <stdio.h>\n") ;
495 fprintf(file
,"#include <stdlib.h>\n") ;
496 fprintf(file
,"#include <math.h>\n\n") ;
498 /* The value of parameters. */
499 fprintf(file
,"/* Parameter value. */\n") ;
500 for (i
= 1; i
<= program
->names
->nb_parameters
; i
++)
501 fprintf(file
, "#define PARVAL%d %d\n", i
, options
->compilable
);
506 /* The statement macros. */
507 fprintf(file
,"/* Statement macros (please set). */\n") ;
508 blocklist
= program
->blocklist
;
509 while (blocklist
!= NULL
)
510 { block
= blocklist
->block
;
511 statement
= block
->statement
;
512 while (statement
!= NULL
)
513 { fprintf(file
,"#define S%d(",statement
->number
) ;
514 if (block
->depth
> 0)
515 { fprintf(file
,"%s",program
->names
->iterators
[0]) ;
516 for(j
=1;j
<block
->depth
;j
++)
517 fprintf(file
,",%s",program
->names
->iterators
[j
]) ;
519 fprintf(file
,") {total++;") ;
520 if (block
->depth
> 0) {
521 fprintf(file
, " printf(\"S%d %%d", statement
->number
);
522 for(j
=1;j
<block
->depth
;j
++)
523 fprintf(file
, " %%d");
525 fprintf(file
,"\\n\",%s",program
->names
->iterators
[0]) ;
526 for(j
=1;j
<block
->depth
;j
++)
527 fprintf(file
,",%s",program
->names
->iterators
[j
]) ;
530 fprintf(file
,"}\n") ;
532 statement
= statement
->next
;
534 blocklist
= blocklist
->next
;
537 /* The iterator and parameter declaration. */
538 fprintf(file
,"\nint main() {\n") ;
539 print_iterator_declarations(file
, program
, options
);
540 if (program
->names
->nb_parameters
> 0)
541 { fprintf(file
," /* Parameters. */\n") ;
542 fprintf(file
, " int %s=PARVAL1",program
->names
->parameters
[0]);
543 for(i
=2;i
<=program
->names
->nb_parameters
;i
++)
544 fprintf(file
, ", %s=PARVAL%d", program
->names
->parameters
[i
-1], i
);
548 fprintf(file
," int total=0;\n");
551 /* And we adapt the identation. */
553 } else if (options
->callable
&& program
->language
== 'c') {
554 print_callable_preamble(file
, program
, options
);
558 root
= cloog_clast_create(program
, options
);
559 clast_pprint(file
, root
, indentation
, options
);
560 cloog_clast_free(root
);
562 /* The end of the compilable code in case of 'compilable' option. */
563 if (options
->compilable
&& (program
->language
== 'c'))
565 fprintf(file
, "\n printf(\"Number of integral points: %%d.\\n\",total);");
566 fprintf(file
, "\n return 0;\n}\n");
567 } else if (options
->callable
&& program
->language
== 'c')
568 print_callable_postamble(file
, program
);
572 /******************************************************************************
573 * Memory deallocation function *
574 ******************************************************************************/
578 * cloog_program_free function:
579 * This function frees the allocated memory for a CloogProgram structure.
581 void cloog_program_free(CloogProgram
* program
)
582 { cloog_names_free(program
->names
) ;
583 cloog_loop_free(program
->loop
) ;
584 cloog_domain_free(program
->context
) ;
585 cloog_block_list_free(program
->blocklist
) ;
586 if (program
->scaldims
!= NULL
)
587 free(program
->scaldims
) ;
593 /******************************************************************************
595 ******************************************************************************/
598 static void cloog_program_construct_block_list(CloogProgram
*p
)
601 CloogBlockList
**next
= &p
->blocklist
;
603 for (loop
= p
->loop
; loop
; loop
= loop
->next
) {
604 *next
= cloog_block_list_alloc(loop
->block
);
605 next
= &(*next
)->next
;
611 * Construct a CloogProgram structure from a given context and
612 * union domain representing the iteration domains and scattering functions.
614 CloogProgram
*cloog_program_alloc(CloogDomain
*context
, CloogUnionDomain
*ud
,
615 CloogOptions
*options
)
619 CloogScatteringList
* scatteringl
;
623 /* Memory allocation for the CloogProgram structure. */
624 p
= cloog_program_malloc() ;
626 if (options
->language
== CLOOG_LANGUAGE_FORTRAN
)
631 p
->names
= n
= cloog_names_alloc();
633 /* We then read the context data. */
634 p
->context
= context
;
635 n
->nb_parameters
= ud
->n_name
[CLOOG_PARAM
];
637 /* First part of the CloogNames structure: the parameter names. */
638 if (ud
->name
[CLOOG_PARAM
]) {
639 n
->parameters
= ud
->name
[CLOOG_PARAM
];
640 ud
->name
[CLOOG_PARAM
] = NULL
;
642 n
->parameters
= cloog_names_generate_items(n
->nb_parameters
, NULL
,
645 n
->nb_iterators
= ud
->n_name
[CLOOG_ITER
];
646 if (ud
->name
[CLOOG_ITER
]) {
647 n
->iterators
= ud
->name
[CLOOG_ITER
];
648 ud
->name
[CLOOG_ITER
] = NULL
;
650 n
->iterators
= cloog_names_generate_items(n
->nb_iterators
, NULL
,
654 CloogNamedDomainList
*l
;
655 CloogLoop
**next
= &p
->loop
;
656 CloogScatteringList
**next_scat
= &scatteringl
;
659 for (i
= 0, l
= ud
->domain
; l
; ++i
, l
= l
->next
) {
660 *next
= cloog_loop_from_domain(options
->state
, l
->domain
, i
);
662 (*next
)->block
->statement
->name
= l
->name
;
663 (*next
)->block
->statement
->usr
= l
->usr
;
667 *next_scat
= ALLOC(CloogScatteringList
);
668 (*next_scat
)->scatt
= l
->scattering
;
669 l
->scattering
= NULL
;
670 (*next_scat
)->next
= NULL
;
672 next_scat
= &(*next_scat
)->next
;
675 next
= &(*next
)->next
;
678 if (scatteringl
!= NULL
) {
679 p
->nb_scattdims
= cloog_scattering_dimension(scatteringl
->scatt
,
681 n
->nb_scattering
= p
->nb_scattdims
;
682 if (ud
->name
[CLOOG_SCAT
]) {
683 n
->scattering
= ud
->name
[CLOOG_SCAT
];
684 ud
->name
[CLOOG_SCAT
] = NULL
;
686 n
->scattering
= cloog_names_generate_items(n
->nb_scattering
, prefix
, -1);
688 /* The boolean array for scalar dimensions is created and set to 0. */
689 p
->scaldims
= (int *)malloc(p
->nb_scattdims
*(sizeof(int))) ;
690 if (p
->scaldims
== NULL
)
691 cloog_die("memory overflow.\n");
692 for (i
=0;i
<p
->nb_scattdims
;i
++)
695 /* We try to find blocks in the input problem to reduce complexity. */
696 if (!options
->noblocks
)
697 cloog_program_block(p
, scatteringl
, options
);
698 if (!options
->noscalars
)
699 cloog_program_extract_scalars(p
, scatteringl
, options
);
701 cloog_program_scatter(p
, scatteringl
, options
);
702 cloog_scattering_list_free(scatteringl
);
704 if (!options
->noblocks
)
705 p
->loop
= cloog_loop_block(p
->loop
, p
->scaldims
, p
->nb_scattdims
);
708 { p
->nb_scattdims
= 0 ;
712 cloog_names_scalarize(p
->names
,p
->nb_scattdims
,p
->scaldims
) ;
714 cloog_program_construct_block_list(p
);
718 p
->blocklist
= NULL
;
722 cloog_union_domain_free(ud
);
729 * cloog_program_read function:
730 * This function read the informations to put in a CloogProgram structure from
731 * a file (file, possibly stdin). It returns a pointer to a CloogProgram
732 * structure containing the read informations.
733 * - October 25th 2001: first version.
734 * - September 9th 2002: - the big reading function is now split in several
735 * functions (one per read data structure).
736 * - adaptation to the new file format with naming.
738 CloogProgram
*cloog_program_read(FILE *file
, CloogOptions
*options
)
743 input
= cloog_input_read(file
, options
);
744 p
= cloog_program_alloc(input
->context
, input
->ud
, options
);
751 /******************************************************************************
752 * Processing functions *
753 ******************************************************************************/
757 * cloog_program_malloc function:
758 * This function allocates the memory space for a CloogProgram structure and
759 * sets its fields with default values. Then it returns a pointer to the
761 * - November 21th 2005: first version.
763 CloogProgram
* cloog_program_malloc()
764 { CloogProgram
* program
;
766 /* Memory allocation for the CloogProgram structure. */
767 program
= (CloogProgram
*)malloc(sizeof(CloogProgram
)) ;
769 cloog_die("memory overflow.\n");
771 /* We set the various fields with default values. */
772 program
->language
= 'c' ;
773 program
->nb_scattdims
= 0 ;
774 program
->context
= NULL
;
775 program
->loop
= NULL
;
776 program
->names
= NULL
;
777 program
->blocklist
= NULL
;
778 program
->scaldims
= NULL
;
786 * cloog_program_generate function:
787 * This function calls the Quillere algorithm for loop scanning. (see the
788 * Quillere paper) and calls the loop simplification function.
789 * - depth is the loop depth we want to optimize (guard free as possible),
790 * the first loop depth is 1 and anegative value is the infinity depth.
791 * - sep_level is the level number where we want to start loop separation.
793 * - October 26th 2001: first version.
794 * - April 19th 2005: some basic fixes and memory usage feature.
795 * - April 29th 2005: (bug fix, bug found by DaeGon Kim) see case 2 below.
797 CloogProgram
* cloog_program_generate(program
, options
)
798 CloogProgram
* program
;
799 CloogOptions
* options
;
803 struct rusage start
, end
;
807 char status_path
[MAX_STRING_VAL
] ;
810 /* We initialize the memory need to 0. */
811 options
->memory
= 0 ;
814 if (options
->override
)
816 cloog_msg(options
, CLOOG_WARNING
,
817 "you are using -override option, be aware that the "
818 "generated\n code may be incorrect.\n") ;
821 { /* Playing with options may be dangerous, here are two possible issues :
822 * 1. Using -l option less than scattering dimension number may lead to
823 * an illegal target code (since the scattering is not respected), if
824 * it is the case, we set -l depth to the first acceptable value.
826 if ((program
->nb_scattdims
> options
->l
) && (options
->l
>= 0))
828 cloog_msg(options
, CLOOG_WARNING
,
829 "-l depth is less than the scattering dimension number "
830 "(the \n generated code may be incorrect), it has been "
831 "automaticaly set\n to this value (use option -override "
833 options
->l
= program
->nb_scattdims
;
836 /* 2. Using -f option greater than one while -l depth is greater than the
837 * scattering dimension number may lead to iteration duplication (try
838 * test/daegon_lu_osp.cloog with '-f 3' to test) because of the step 4b
839 * of the cloog_loop_generate function, if it is the case, we set -l to
840 * the first acceptable value.
842 if (((options
->f
> 1) || (options
->f
< 0)) &&
843 ((options
->l
> program
->nb_scattdims
) || (options
->l
< 0)))
845 cloog_msg(options
, CLOOG_WARNING
,
846 "-f depth is more than one, -l depth has been "
847 "automaticaly set\n to the scattering dimension number "
848 "(target code may have\n duplicated iterations), -l depth "
849 "has been automaticaly set to\n this value (use option "
850 "-override to override).\n") ;
851 options
->l
= program
->nb_scattdims
;
856 getrusage(RUSAGE_SELF
, &start
) ;
858 if (program
->loop
!= NULL
)
859 { loop
= program
->loop
;
862 loop
= cloog_loop_generate(loop
, program
->context
, 0, 0,
864 program
->nb_scattdims
,
868 /* We read into the status file of the process how many memory it uses. */
869 sprintf(status_path
,"/proc/%d/status",getpid()) ;
870 status
= fopen(status_path
, "r") ;
871 while (fscanf(status
,"%s",status_path
) && strcmp(status_path
,"VmData:")!=0);
872 fscanf(status
,"%d",&(options
->memory
)) ;
876 if ((!options
->nosimplify
) && (program
->loop
!= NULL
))
877 loop
= cloog_loop_simplify(loop
, program
->context
, 0,
878 program
->nb_scattdims
, options
);
880 program
->loop
= loop
;
884 getrusage(RUSAGE_SELF
, &end
) ;
885 /* We calculate the time spent in code generation. */
886 time
= (end
.ru_utime
.tv_usec
- start
.ru_utime
.tv_usec
)/(float)(MEGA
) ;
887 time
+= (float)(end
.ru_utime
.tv_sec
- start
.ru_utime
.tv_sec
) ;
888 options
->time
= time
;
896 * cloog_program_block function:
897 * this function gives a last chance to the lazy user to consider statement
898 * blocks instead of some statement lists where the whole list may be
899 * considered as a single statement from a code generation point of view.
900 * For instance two statements with the same iteration domain and the same
901 * scattering functions may be considered as a block. This function is lazy
902 * and can only find very simple forms of trivial blocks (see
903 * cloog_domain_lazy_block function for more details). The useless loops and
904 * scattering functions are removed and freed while the statement list of
905 * according blocks are filled.
906 * - program is the whole program structure (befaore applying scattering),
907 * - scattering is the list of scattering functions.
909 * - April 30th 2005: first attempt.
910 * - June 10-11th 2005: first working version.
912 void cloog_program_block(CloogProgram
*program
,
913 CloogScatteringList
*scattering
, CloogOptions
*options
)
914 { int blocked_reference
=0, blocked
=0, nb_blocked
=0 ;
915 CloogLoop
* reference
, * start
, * loop
;
916 CloogScatteringList
* scatt_reference
, * scatt_loop
, * scatt_start
;
918 if ((program
->loop
== NULL
) || (program
->loop
->next
== NULL
))
921 /* The process will use three variables for the linked list :
922 * - 'start' is the starting point of a new block,
923 * - 'reference' is the node of the block used for the block checking,
924 * - 'loop' is the candidate to be inserted inside the block.
925 * At the beginning of the process, the linked lists are as follow:
926 * O------>O------>O------>O------>NULL
932 reference
= program
->loop
;
933 start
= program
->loop
;
934 loop
= reference
->next
;
935 scatt_reference
= scattering
;
936 scatt_start
= scattering
;
937 scatt_loop
= scattering
->next
;
940 { if (cloog_domain_lazy_equal(reference
->domain
,loop
->domain
) &&
941 cloog_scattering_lazy_block(scatt_reference
->scatt
, scatt_loop
->scatt
,
942 scattering
,program
->nb_scattdims
))
943 { /* If we find a block we update the links:
946 * O O------>O------>O------>NULL
953 cloog_block_merge(start
->block
,loop
->block
); /* merge frees loop->block */
955 start
->next
= loop
->next
;
956 scatt_start
->next
= scatt_loop
->next
;
959 { /* If we didn't find a block, the next start of a block is updated:
960 * O------>O------>O------>O------>NULL
967 scatt_start
= scatt_loop
;
970 /* If the reference node has been included into a block, we can free it. */
971 if (blocked_reference
)
972 { reference
->next
= NULL
;
973 cloog_loop_free(reference
) ;
974 cloog_scattering_free(scatt_reference
->scatt
);
975 free(scatt_reference
) ;
978 /* The reference and the loop are now updated for the next try, the
979 * starting position depends on the previous step.
980 * O ? O------>O------>O------>NULL
986 scatt_reference
= scatt_loop
;
987 scatt_loop
= scatt_loop
->next
;
989 /* We mark the new reference as being blocked or not, if will be freed
990 * during the next while loop execution.
993 blocked_reference
= 1 ;
995 blocked_reference
= 0 ;
998 /* We free the last blocked reference if any (since in the while loop it was
999 * freed during the next loop execution, it was not possible to free the
1002 if (blocked_reference
)
1003 { reference
->next
= NULL
;
1004 cloog_loop_free(reference
) ;
1005 cloog_scattering_free(scatt_reference
->scatt
);
1006 free(scatt_reference
) ;
1009 if (nb_blocked
!= 0)
1010 cloog_msg(options
, CLOOG_INFO
, "%d domains have been blocked.\n", nb_blocked
);
1015 * cloog_program_extract_scalars function:
1016 * this functions finds and removes the dimensions of the scattering functions
1017 * when they are scalar (i.e. of the shape "dim + scalar = 0") for all
1018 * scattering functions. The reason is that the processing of such dimensions
1019 * is trivial and do not need neither a row and a column in the matrix
1020 * representation of the domain (this will save memory) neither the full
1021 * Quillere processing (this will save time). The scalar dimensions data are
1022 * dispatched in the CloogProgram structure (the boolean vector scaldims will
1023 * say which original dimensions are scalar or not) and to the CloogBlock
1024 * structures (each one has a scaldims vector that contains the scalar values).
1025 * - June 14th 2005: first developments.
1026 * - June 30th 2005: first version.
1028 void cloog_program_extract_scalars(CloogProgram
*program
,
1029 CloogScatteringList
*scattering
, CloogOptions
*options
)
1030 { int i
, j
, scalar
, current
, nb_scaldims
=0 ;
1031 CloogScatteringList
*start
;
1032 CloogScattering
*old
;
1034 CloogBlock
* block
;
1036 start
= scattering
;
1038 for (i
=0;i
<program
->nb_scattdims
;i
++)
1040 scattering
= start
;
1041 while (scattering
!= NULL
)
1042 { if (!cloog_scattering_lazy_isscalar(scattering
->scatt
, i
, NULL
))
1046 scattering
= scattering
->next
;
1051 program
->scaldims
[i
] = 1 ;
1055 /* If there are no scalar dimensions, we can continue directly. */
1059 /* Otherwise, in each block, we have to put the number of scalar dimensions,
1060 * and to allocate the memory for the scalar values.
1062 for (loop
= program
->loop
; loop
; loop
= loop
->next
) {
1063 block
= loop
->block
;
1064 block
->nb_scaldims
= nb_scaldims
;
1065 block
->scaldims
= (cloog_int_t
*)malloc(nb_scaldims
*sizeof(cloog_int_t
));
1066 for (i
=0;i
<nb_scaldims
;i
++)
1067 cloog_int_init(block
->scaldims
[i
]);
1070 /* Then we have to fill these scalar values, so we can erase those dimensions
1071 * from the scattering functions. It's easier to begin with the last one,
1072 * since there would be an offset otherwise (if we remove the i^th dimension,
1073 * then the next one is not the (i+1)^th but still the i^th...).
1075 current
= nb_scaldims
- 1 ;
1076 for (i
=program
->nb_scattdims
-1;i
>=0;i
--)
1077 if (program
->scaldims
[i
])
1079 scattering
= start
;
1080 for (loop
= program
->loop
; loop
; loop
= loop
->next
) {
1081 block
= loop
->block
;
1082 if (!cloog_scattering_lazy_isscalar(scattering
->scatt
, i
,
1083 &block
->scaldims
[current
])) {
1084 /* We should have found a scalar value: if not, there is an error. */
1085 cloog_die("dimension %d is not scalar as expected.\n", i
);
1087 scattering
= scattering
->next
;
1090 scattering
= start
;
1091 while (scattering
!= NULL
) {
1092 old
= scattering
->scatt
;
1093 scattering
->scatt
= cloog_scattering_erase_dimension(old
, i
);
1094 cloog_scattering_free(old
);
1095 scattering
= scattering
->next
;
1100 /* We postprocess the scaldims array in such a way that each entry is how
1101 * many scalar dimensions follows + 1 (the current one). This will make
1102 * some other processing easier (e.g. knowledge of some offsets).
1104 for (i
=0;i
<program
->nb_scattdims
-1;i
++)
1105 { if (program
->scaldims
[i
])
1107 while ((j
< program
->nb_scattdims
) && program
->scaldims
[j
])
1108 { program
->scaldims
[i
] ++ ;
1114 if (nb_scaldims
!= 0)
1115 cloog_msg(options
, CLOOG_INFO
, "%d dimensions (over %d) are scalar.\n",
1116 nb_scaldims
,program
->nb_scattdims
) ;
1121 * cloog_program_scatter function:
1122 * This function adds the scattering (scheduling) informations in a program.
1123 * If names is NULL, this function create names itself such that the i^th
1125 * - November 6th 2001: first version.
1127 void cloog_program_scatter(CloogProgram
*program
,
1128 CloogScatteringList
*scattering
, CloogOptions
*options
)
1129 { int scattering_dim
, scattering_dim2
, not_enough_constraints
=0 ;
1132 if ((program
!= NULL
) && (scattering
!= NULL
))
1133 { loop
= program
->loop
;
1135 /* We compute the scattering dimension and check it is >=0. */
1136 scattering_dim
= cloog_scattering_dimension(scattering
->scatt
, loop
->domain
);
1137 if (scattering_dim
< 0)
1138 cloog_die("scattering has not enough dimensions.\n");
1139 if (!cloog_scattering_fully_specified(scattering
->scatt
, loop
->domain
))
1140 not_enough_constraints
++ ;
1142 /* The scattering dimension may have been modified by scalar extraction. */
1143 scattering_dim
= cloog_scattering_dimension(scattering
->scatt
, loop
->domain
);
1145 /* Finally we scatter all loops. */
1146 cloog_loop_scatter(loop
, scattering
->scatt
);
1148 scattering
= scattering
->next
;
1150 while ((loop
!= NULL
) && (scattering
!= NULL
))
1151 { scattering_dim2
= cloog_scattering_dimension(scattering
->scatt
,
1153 if (scattering_dim2
!= scattering_dim
)
1154 cloog_die("scattering dimensions are not the same.\n") ;
1155 if (!cloog_scattering_fully_specified(scattering
->scatt
, loop
->domain
))
1156 not_enough_constraints
++ ;
1158 cloog_loop_scatter(loop
, scattering
->scatt
);
1160 scattering
= scattering
->next
;
1162 if ((loop
!= NULL
) || (scattering
!= NULL
))
1163 cloog_msg(options
, CLOOG_WARNING
,
1164 "there is not a scattering for each statement.\n");
1166 if (not_enough_constraints
)
1167 cloog_msg(options
, CLOOG_WARNING
, "not enough constraints for "
1168 "%d scattering function(s).\n",not_enough_constraints
) ;