1
2   /**-------------------------------------------------------------------**
3    **                              CLooG                                **
4    **-------------------------------------------------------------------**
5    **                            options.c                              **
6    **-------------------------------------------------------------------**
7    **                  First version: april 19th 2003                   **
8    **-------------------------------------------------------------------**/
9
10
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
37
38#include <stdarg.h>
39# include <stdlib.h>
40# include <stdio.h>
41# include <string.h>
42# include "../include/cloog/cloog.h"
43
44#ifdef OSL_SUPPORT
45#include <osl/scop.h>
46#endif
47
48
49/******************************************************************************
50 *                          Error reporting functions                         *
51 ******************************************************************************/
52
53void cloog_vmsg(CloogOptions *options, enum cloog_msg_type type,
54		const char *msg, va_list ap)
55{
56  const char *type_msg;
57
58  if (options && options->quiet &&
59      (type == CLOOG_WARNING || type == CLOOG_INFO))
60    return;
61
62  switch(type) {
63  case CLOOG_WARNING:
64	type_msg = "WARNING";
65	break;
66  case CLOOG_INFO:
67	type_msg = "INFO";
68	break;
69  case CLOOG_ERROR:
70  default:
71	type_msg = "ERROR";
72	break;
73  }
74  fprintf(stderr, "[CLooG] %s: ", type_msg);
75  vfprintf(stderr, msg, ap);
76}
77
78/**
79 * Print message to stderr.
80 * @param msg printf format string
81 */
82void cloog_msg(CloogOptions *options, enum cloog_msg_type type,
83		const char *msg, ...)
84{
85  va_list args;
86
87  va_start(args, msg);
88  cloog_vmsg(options, type, msg, args);
89  va_end(args);
90}
91
92/**
93 * Print error message to stderr and exit.
94 * @param msg printf format string
95 */
96void cloog_die(const char *msg, ...)
97{
98  va_list args;
99
100  va_start(args, msg);
101  cloog_vmsg(NULL, CLOOG_ERROR, msg, args);
102  va_end(args);
103  exit(1);
104}
105
106/******************************************************************************
107 *                          Structure display function                        *
108 ******************************************************************************/
109
110
111/**
112 * cloog_option_print function:
113 * This function prints the content of a CloogOptions structure (program) into
114 * a file (foo, possibly stdout).
115 * - April 19th 2003: first version.
116 */
117void cloog_options_print(FILE * foo, CloogOptions * options)
118{
119  int i;
120
121  fprintf(foo,"Options:\n") ;
122  fprintf(foo,"OPTIONS FOR LOOP GENERATION\n") ;
123  fprintf(foo,"l           = %3d,\n",options->l) ;
124  fprintf(foo,"f           = %3d,\n",options->f) ;
125  fprintf(foo,"fs           = %3d,\n",options->f) ;
126  if (options->fs_ls_size>=1) {
127      fprintf(foo,"fs           = ");
128      for (i=0; i<options->fs_ls_size; i++) {
129          fprintf(foo,"%3d,\n",options->fs[i]) ;
130      }
131      fprintf(foo,"\n");
132      fprintf(foo,"ls           = ");
133      for (i=0; i<options->fs_ls_size; i++) {
134          fprintf(foo,"%3d,\n",options->ls[i]) ;
135      }
136      fprintf(foo,"\n");
137  }
138  fprintf(foo,"stop        = %3d,\n",options->stop) ;
139  fprintf(foo,"strides     = %3d,\n",options->strides) ;
140  fprintf(foo,"sh          = %3d,\n",options->sh);
141  fprintf(foo,"OPTIONS FOR PRETTY PRINTING\n") ;
142  fprintf(foo,"esp         = %3d,\n",options->esp) ;
143  fprintf(foo,"fsp         = %3d,\n",options->fsp) ;
144  fprintf(foo,"otl         = %3d.\n",options->otl) ;
145  fprintf(foo,"block       = %3d.\n",options->block) ;
146  fprintf(foo,"compilable  = %3d.\n",options->compilable) ;
147  fprintf(foo,"callable    = %3d.\n",options->callable) ;
148  fprintf(foo,"MISC OPTIONS\n") ;
149  fprintf(foo,"name        = %3s.\n", options->name);
150  fprintf(foo,"openscop    = %3d.\n", options->openscop);
151  if (options->scop != NULL)
152    fprintf(foo,"scop        = (present but not printed).\n");
153  else
154    fprintf(foo,"scop        = NULL.\n");
155  fprintf(foo,"UNDOCUMENTED OPTIONS FOR THE AUTHOR ONLY\n") ;
156  fprintf(foo,"leaks       = %3d.\n",options->leaks) ;
157  fprintf(foo,"backtrack   = %3d.\n",options->backtrack);
158  fprintf(foo,"override    = %3d.\n",options->override) ;
159  fprintf(foo,"structure   = %3d.\n",options->structure) ;
160  fprintf(foo,"noscalars   = %3d.\n",options->noscalars) ;
161  fprintf(foo,"noblocks    = %3d.\n",options->noblocks) ;
162  fprintf(foo,"nosimplify  = %3d.\n",options->nosimplify) ;
163}
164
165
166/******************************************************************************
167 *                         Memory deallocation function                       *
168 ******************************************************************************/
169
170
171/**
172 * cloog_options_free function:
173 * This function frees the allocated memory for a CloogOptions structure.
174 * - April 19th 2003: first version.
175 */
176void cloog_options_free(CloogOptions *options)
177{
178#ifdef OSL_SUPPORT
179  if (options->scop != NULL) {
180    osl_scop_free(options->scop);
181  }
182#endif
183  free(options->fs);
184  free(options->ls);
185  free(options);
186}
187
188
189/******************************************************************************
190 *                            Processing functions                            *
191 ******************************************************************************/
192
193
194/**
195 * cloog_options_help function:
196 * This function displays the quick help when the user set the option -help
197 * while calling cloog. Prints are cutted to respect the 509 characters
198 * limitation of the ISO C 89 compilers.
199 * - August 5th 2002: first version.
200 */
201void cloog_options_help()
202{ printf(
203  "Usage: cloog [ options | file ] ...\n"
204  "Options for code generation:\n"
205  "  -l <depth>            Last loop depth to optimize (-1: infinity)\n"
206  "                        (default setting: -1).\n"
207  "  -f <depth>            First loop depth to start loop separation (-1: "
208  "infinity)\n                        (default setting:  1).\n") ;
209  printf(
210  "  -stop <depth>         Loop depth to stop code generation (-1: infinity)"
211  "\n                        (default setting: -1).\n"
212  "  -strides <boolean>    Handle non-unit strides (1) or not (0)\n"
213  "                        (default setting:  0).\n"
214  "  -first-unroll <depth> First loop dimension to unroll (-1: no unrolling)\n");
215  printf(
216  "\nOptions for pretty printing:\n"
217  "  -otl <boolean>        Simplify loops running one time (1) or not (0)\n"
218  "                        (default setting:  1).\n") ;
219  printf(
220  "  -esp <boolean>        Allow complex equalities spreading (1) or not (0)\n"
221  "                        (default setting:  0).\n");
222  printf(
223  "  -fsp <level>          First level to begin the spreading\n"
224  "                        (default setting:  1).\n"
225  "  -block <boolean>      Make a new statement block per iterator in C\n"
226  "                        programs (1) or not (0) (default setting: 0).\n") ;
227  printf(
228  "  -compilable <number>  Compilable code by using preprocessor (not 0) or"
229  "\n                        not (0), number being the value of the parameters"
230  "\n                        (default setting:  0).\n"
231  "  -callable <boolean>   Testable code by using preprocessor (not 0) or"
232  "\n                        not (0) (default setting:  0).\n");
233  printf(
234  "\nGeneral options:\n"
235  "  -o <output>           Name of the output file; 'stdout' is a special\n"
236  "                        value: when used, output is standard output\n"
237  "                        (default setting: stdout).\n"
238#ifdef OSL_SUPPORT
239  "  -openscop             Input file has OpenScop format.\n"
240#endif
241  "  -v, --version         Display the version information (and more).\n"
242  "  -q, --quiet           Don't print any informational messages.\n"
243  "  -h, --help            Display this information.\n\n") ;
244  printf(
245  "The special value 'stdin' for 'file' makes CLooG to read data on\n"
246  "standard input.\n\n"
247  "For bug reporting or any suggestions, please send an email to the author\n"
248  "<cedric.bastoul@inria.fr>.\n") ;
249}
250
251
252/**
253 * cloog_options_version function:
254 * This function displays some version informations when the user set the
255 * option -version while calling cloog. Prints are cutted to respect the 509
256 * characters limitation of the ISO C 89 compilers.
257 * - August 5th 2002: first version.
258 */
259void cloog_options_version()
260{ printf("%s       The Chunky Loop Generator\n", cloog_version());
261  printf(
262  "-----\n"
263  "This is a loop generator for scanning Z-polyhedra. It is based on the "
264  "work of\nF. Quillere and C. Bastoul on high level code generation and of "
265  "the PolyLib Team\non polyhedral computation. This program is distributed "
266  "under the terms of the\nGNU Lesser General Public License "
267  "(details at http://www.gnu.org/licenses/lgpl-2.1.html).\n"
268  "-----\n") ;
269  printf(
270  "It would be fair to refer the following paper in any publication "
271  "resulting from\nthe use of this software or its library:\n"
272  "@InProceedings{Bas04,\n"
273  "author    =  {Cedric Bastoul},\n"
274  "title     =  {Code Generation in the Polyhedral Model Is Easier Than You "
275  "Think},\n"
276  "booktitle =  {PACT'13 IEEE International Conference on Parallel "
277  "Architecture\n             and Compilation Techniques},\n"
278  "pages     =  {7--16},\n"
279  "month     =  {september},\n"
280  "year      =  2004,\n"
281  "address   =  {Juan-les-Pins}\n"
282  "}\n"
283  "-----\n"
284  "For any information, please ask the author at "
285  "<cedric.bastoul@inria.fr>.\n") ;
286}
287
288
289/**
290 * cloog_options_set function:
291 * This function sets the value of an option thanks to the user's calling line.
292 * - option is the value to set,
293 * - argc are the elements of the user's calling line,
294 * - number is the number of the element corresponding to the considered option,
295 *   this function adds 1 to number to pass away the option value.
296 **
297 * - August 5th 2002: first version.
298 * - June 29th 2003: (debug) lack of argument now detected.
299 */
300void cloog_options_set(int * option, int argv, char ** argc, int * number)
301{ char ** endptr ;
302
303  if (*number+1 >= argv)
304    cloog_die("an option lacks of argument.\n");
305
306  endptr = NULL ;
307  *option = strtol(argc[*number+1],endptr,10) ;
308  if (endptr != NULL)
309    cloog_die("value '%s' for option '%s' is not valid.\n",
310	      argc[*number+1], argc[*number]);
311  *number = *number + 1 ;
312}
313
314
315/**
316 * cloog_options_malloc function:
317 * This functions allocate the memory space for a CLoogOptions structure and
318 * fill its fields with the defaults values. It returns a pointer to the
319 * allocated CloogOptions structure.
320 * - April    19th 2003: first version.
321 * - November 21th 2005: name changed (before it was cloog_options_init).
322 */
323CloogOptions *cloog_options_malloc(CloogState *state)
324{ CloogOptions * options ;
325
326  /* Memory allocation for the CloogOptions structure. */
327  options = (CloogOptions *)malloc(sizeof(CloogOptions)) ;
328  if (options == NULL)
329    cloog_die("memory overflow.\n");
330
331  options->state = state;
332
333  /* We set the various fields with default values. */
334  /* OPTIONS FOR LOOP GENERATION */
335  options->l           = -1 ;  /* Last level to optimize: infinity. */
336  options->f           =  1 ;  /* First level to optimize: the first. */
337  options->ls          = NULL ; /* Statement-wise l option is not set */
338  options->fs          = NULL ; /* Statement-wise f option is not set */
339  options->fs_ls_size  = 0;    /* No statement-wise f/s control */
340  options->stop        = -1 ;  /* Generate all the code. */
341  options->strides     =  0 ;  /* Generate a code with unit strides. */
342  options->sh	       =  0;   /* Compute actual convex hull. */
343  options->first_unroll = -1;  /* First level to unroll: none. */
344  options->name	       = "";
345  /* OPTIONS FOR PRETTY PRINTING */
346  options->esp         =  1 ;  /* We want Equality SPreading.*/
347  options->fsp         =  1 ;  /* The First level to SPread is the first. */
348  options->otl         =  1 ;  /* We want to fire One Time Loops. */
349  options->block       =  0 ;  /* We don't want to force statement blocks. */
350  options->compilable  =  0 ;  /* No compilable code. */
351  options->callable    =  0 ;  /* No callable code. */
352  options->quiet       =  0;   /* Do print informational messages. */
353  options->save_domains = 0;   /* Don't save domains. */
354  /* MISC OPTIONS */
355  options->language    = CLOOG_LANGUAGE_C; /* The default output language is C. */
356  options->openscop    =  0 ;  /* The input file has not the OpenScop format.*/
357  options->scop        =  NULL;/* No default SCoP.*/
358  /* UNDOCUMENTED OPTIONS FOR THE AUTHOR ONLY */
359  options->leaks       =  0 ;  /* I don't want to print allocation statistics.*/
360  options->backtrack   =  0;   /* Perform backtrack in Quillere's algorithm.*/
361  options->override    =  0 ;  /* I don't want to override CLooG decisions.*/
362  options->structure   =  0 ;  /* I don't want to print internal structure.*/
363  options->noblocks    =  0 ;  /* I do want to make statement blocks.*/
364  options->noscalars   =  0 ;  /* I do want to use scalar dimensions.*/
365  options->nosimplify  =  0 ;  /* I do want to simplify polyhedra.*/
366
367  return options ;
368}
369
370
371
372/**
373 * cloog_options_read function:
374 * This functions reads all the options and the input/output files thanks
375 * the the user's calling line elements (in argc). It fills a CloogOptions
376 * structure and the FILE structure corresponding to input and output files.
377 * - August 5th 2002: first version.
378 * - April 19th 2003: now in options.c and support of the CloogOptions structure.
379 */
380void cloog_options_read(CloogState *state, int argc, char **argv,
381			FILE **input, FILE **output, CloogOptions **options)
382{ int i, infos=0, input_is_set=0 ;
383
384  /* CloogOptions structure allocation and initialization. */
385  *options = cloog_options_malloc(state);
386
387  /* The default output is the standard output. */
388  *output = stdout ;
389
390  for (i=1;i<argc;i++)
391  if (argv[i][0] == '-')
392  { if (strcmp(argv[i],"-l")   == 0)
393    cloog_options_set(&(*options)->l,argc,argv,&i) ;
394    else
395    if (strcmp(argv[i],"-f")   == 0)
396    cloog_options_set(&(*options)->f,argc,argv,&i) ;
397    else
398    if (strcmp(argv[i],"-stop")   == 0)
399    cloog_options_set(&(*options)->stop,argc,argv,&i) ;
400    else
401    if (strcmp(argv[i],"-strides")   == 0)
402    cloog_options_set(&(*options)->strides,argc,argv,&i) ;
403    else if (strcmp(argv[i],"-sh")   == 0)
404      cloog_options_set(&(*options)->sh,argc,argv,&i) ;
405    else if (!strcmp(argv[i], "-first-unroll"))
406      cloog_options_set(&(*options)->first_unroll, argc, argv, &i);
407    else
408    if (strcmp(argv[i],"-otl") == 0)
409    cloog_options_set(&(*options)->otl,argc,argv,&i) ;
410    else
411    if (strcmp(argv[i],"-openscop") == 0) {
412#ifdef OSL_SUPPORT
413      (*options)->openscop = 1 ;
414#else
415      cloog_die("CLooG has not been compiled with OpenScop support.\n");
416#endif
417    }
418    else
419    if (strcmp(argv[i],"-esp") == 0)
420    cloog_options_set(&(*options)->esp,argc,argv,&i) ;
421    else
422    if (strcmp(argv[i],"-fsp") == 0)
423    cloog_options_set(&(*options)->fsp,argc,argv,&i) ;
424    else
425    if (strcmp(argv[i],"-block") == 0)
426    cloog_options_set(&(*options)->block,argc,argv,&i) ;
427    else
428    if (strcmp(argv[i],"-compilable") == 0)
429      cloog_options_set(&(*options)->compilable, argc, argv, &i);
430    else if (strcmp(argv[i], "-callable") == 0)
431      cloog_options_set(&(*options)->callable, argc, argv, &i);
432    else
433    if (strcmp(argv[i],"-loopo") == 0) /* Special option for the LooPo team ! */
434    { (*options)->esp   = 0 ;
435      (*options)->block = 1 ;
436    }
437    else
438    if (strcmp(argv[i],"-bipbip") == 0)/* Special option for the author only !*/
439      (*options)->backtrack = 0;
440    else
441    if (strcmp(argv[i],"-leaks") == 0)
442    (*options)->leaks = 1 ;
443    else
444    if (strcmp(argv[i],"-nobacktrack") == 0)
445      (*options)->backtrack = 0;
446    else if (strcmp(argv[i], "-backtrack") == 0)
447      (*options)->backtrack = 1;
448    else
449    if (strcmp(argv[i],"-override") == 0)
450    (*options)->override = 1 ;
451    else
452    if (strcmp(argv[i],"-noblocks") == 0)
453    (*options)->noblocks = 1 ;
454    else
455    if (strcmp(argv[i],"-noscalars") == 0)
456    (*options)->noscalars = 1 ;
457    else
458    if (strcmp(argv[i],"-nosimplify") == 0)
459    (*options)->nosimplify = 1 ;
460    else
461    if ((strcmp(argv[i],"-struct") == 0) || (strcmp(argv[i],"-structure") == 0))
462    (*options)->structure = 1 ;
463    else
464    if ((strcmp(argv[i],"--help") == 0) || (strcmp(argv[i],"-h") == 0))
465    { cloog_options_help() ;
466      infos = 1 ;
467    }
468    else
469    if ((strcmp(argv[i],"--version") == 0) || (strcmp(argv[i],"-v") == 0))
470    { cloog_options_version() ;
471      infos = 1 ;
472    } else if ((strcmp(argv[i],"--quiet") == 0) || (strcmp(argv[i],"-q") == 0))
473      (*options)->quiet = 1;
474    else
475    if (strcmp(argv[i],"-o") == 0)
476    { if (i+1 >= argc)
477        cloog_die("no output name for -o option.\n");
478
479      /* stdout is a special value, when used, we set output to standard
480       * output.
481       */
482      if (strcmp(argv[i+1],"stdout") == 0)
483      *output = stdout ;
484      else
485      { *output = fopen(argv[i+1],"w") ;
486        if (*output == NULL)
487          cloog_die("can't create output file %s.\n", argv[i+1]);
488      }
489      i ++ ;
490    }
491    else
492      cloog_msg(*options, CLOOG_WARNING, "unknown %s option.\n", argv[i]);
493  }
494  else
495  { if (!input_is_set)
496    { input_is_set = 1 ;
497      (*options)->name = argv[i] ;
498      /* stdin is a special value, when used, we set input to standard input. */
499      if (strcmp(argv[i],"stdin") == 0)
500      *input = stdin ;
501      else
502      { *input = fopen(argv[i],"r") ;
503        if (*input == NULL)
504          cloog_die("%s file does not exist.\n", argv[i]);
505      }
506    }
507    else
508      cloog_die("multiple input files.\n");
509  }
510  if (!input_is_set)
511  { if (!infos)
512      cloog_die("no input file (-h for help).\n");
513    exit(1) ;
514  }
515}
516
517#ifdef OSL_SUPPORT
518/**
519 * This function extracts CLooG option values from an OpenScop scop and
520 * updates an existing CloogOption structure with those values. If the
521 * options were already set, they are updated without warning.
522 * \param[in]     scop    Input Scop.
523 * \param[in,out] options CLooG options to be updated.
524 */
525void cloog_options_copy_from_osl_scop(osl_scop_p scop,
526                                      CloogOptions *options) {
527  if (!options)
528    cloog_die("Options must be provided.\n");
529
530  if (scop) {
531    /* Extract the language. */
532    if (!strcmp(scop->language, "FORTRAN"))
533      options->language = CLOOG_LANGUAGE_FORTRAN;
534    else
535      options->language = CLOOG_LANGUAGE_C;
536
537    /* Store the input SCoP in the option structure. */
538    options->scop = scop;
539  }
540}
541#endif
542
543