2 /*+-----------------------------------------------------------------**
4 **-----------------------------------------------------------------**
6 **-----------------------------------------------------------------**
7 ** First version: 30/04/2008 **
8 **-----------------------------------------------------------------**
11 *****************************************************************************
12 * OpenScop: Structures and formats for polyhedral tools to talk together *
13 *****************************************************************************
14 * ,___,,_,__,,__,,__,,__,,_,__,,_,__,,__,,___,_,__,,_,__, *
15 * / / / // // // // / / / // // / / // / /|,_, *
16 * / / / // // // // / / / // // / / // / / / /\ *
17 * |~~~|~|~~~|~~~|~~~|~~~|~|~~~|~|~~~|~~~|~~~|~|~~~|~|~~~|/_/ \ *
18 * | G |C| P | = | L | P |=| = |C| = | = | = |=| = |=| C |\ \ /\ *
19 * | R |l| o | = | e | l |=| = |a| = | = | = |=| = |=| L | \# \ /\ *
20 * | A |a| l | = | t | u |=| = |n| = | = | = |=| = |=| o | |\# \ \ *
21 * | P |n| l | = | s | t |=| = |d| = | = | = | | |=| o | | \# \ \ *
22 * | H | | y | | e | o | | = |l| | | = | | | | G | | \ \ \ *
23 * | I | | | | e | | | | | | | | | | | | | \ \ \ *
24 * | T | | | | | | | | | | | | | | | | | \ \ \ *
25 * | E | | | | | | | | | | | | | | | | | \ \ \ *
26 * | * |*| * | * | * | * |*| * |*| * | * | * |*| * |*| * | / \* \ \ *
27 * | O |p| e | n | S | c |o| p |-| L | i | b |r| a |r| y |/ \ \ / *
28 * '---'-'---'---'---'---'-'---'-'---'---'---'-'---'-'---' '--' *
30 * Copyright (C) 2008 University Paris-Sud 11 and INRIA *
32 * (3-clause BSD license) *
33 * Redistribution and use in source and binary forms, with or without *
34 * modification, are permitted provided that the following conditions *
37 * 1. Redistributions of source code must retain the above copyright notice, *
38 * this list of conditions and the following disclaimer. *
39 * 2. Redistributions in binary form must reproduce the above copyright *
40 * notice, this list of conditions and the following disclaimer in the *
41 * documentation and/or other materials provided with the distribution. *
42 * 3. The name of the author may not be used to endorse or promote products *
43 * derived from this software without specific prior written permission. *
45 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR *
46 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES *
47 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. *
48 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, *
49 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT *
50 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, *
51 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY *
52 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *
53 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF *
54 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
56 * OpenScop Library, a library to manipulate OpenScop formats and data *
57 * structures. Written by: *
58 * Cedric Bastoul <Cedric.Bastoul@u-psud.fr> and *
59 * Louis-Noel Pouchet <Louis-Noel.pouchet@inria.fr> *
61 *****************************************************************************/
67 # include <osl/scop.h>
70 /*+***************************************************************************
71 * Structure display functions *
72 *****************************************************************************/
76 * osl_scop_idump function:
77 * this function displays an osl_scop_t structure (*scop) into a
78 * file (file, possibly stdout) in a way that trends to be understandable. It
79 * includes an indentation level (level) in order to work with others
80 * print_structure functions.
81 * \param file The file where the information has to be printed.
82 * \param scop The scop structure whose information has to be printed.
83 * \param level Number of spaces before printing, for each line.
85 void osl_scop_idump(FILE * file
, osl_scop_p scop
, int level
) {
88 // Go to the right level.
89 for (j
= 0; j
< level
; j
++)
93 fprintf(file
, "+-- osl_scop_t\n");
95 fprintf(file
, "+-- NULL scop\n");
97 while (scop
!= NULL
) {
99 // Go to the right level.
100 for (j
= 0; j
< level
; j
++)
101 fprintf(file
, "|\t");
102 fprintf(file
, "| osl_scop_t\n");
108 for (j
= 0; j
<= level
+1; j
++)
109 fprintf(file
, "|\t");
112 // Print the version.
113 for (j
= 0; j
< level
; j
++)
114 fprintf(file
, "|\t");
115 fprintf(file
, "|\tVersion: %d\n", scop
->version
);
118 for (j
= 0; j
<= level
+1; j
++)
119 fprintf(file
, "|\t");
122 // Print the language.
123 for (j
= 0; j
< level
; j
++)
124 fprintf(file
, "|\t");
125 fprintf(file
, "|\tLanguage: %s\n", scop
->language
);
128 for (j
= 0; j
<= level
+1; j
++)
129 fprintf(file
, "|\t");
132 // Print the context of the scop.
133 osl_relation_idump(file
, scop
->context
, level
+1);
135 // Print the parameters.
136 osl_generic_idump(file
, scop
->parameters
, level
+1);
138 // Print the statements.
139 osl_statement_idump(file
, scop
->statement
, level
+1);
141 // Print the registered extension interfaces.
142 osl_interface_idump(file
, scop
->registry
, level
+1);
144 // Print the extensions.
145 osl_generic_idump(file
, scop
->extension
, level
+1);
151 for (j
= 0; j
<= level
; j
++)
152 fprintf(file
, "|\t");
153 fprintf(file
, "V\n");
158 for (j
= 0; j
<= level
; j
++)
159 fprintf(file
, "|\t");
165 * osl_scop_dump function:
166 * this function prints the content of an osl_scop_t structure (*scop)
167 * into a file (file, possibly stdout).
168 * \param file The file where the information has to be printed.
169 * \param scop The scop structure whose information has to be printed.
171 void osl_scop_dump(FILE * file
, osl_scop_p scop
) {
172 osl_scop_idump(file
, scop
, 0);
177 * osl_scop_names function:
178 * this function generates as set of names for all the dimensions
179 * involved in a given scop.
180 * \param[in] scop The scop (list) we have to generate names for.
181 * \return A set of generated names for the input scop dimensions.
184 osl_names_p
osl_scop_names(osl_scop_p scop
) {
185 int nb_parameters
= OSL_UNDEFINED
;
186 int nb_iterators
= OSL_UNDEFINED
;
187 int nb_scattdims
= OSL_UNDEFINED
;
188 int nb_localdims
= OSL_UNDEFINED
;
189 int array_id
= OSL_UNDEFINED
;
191 osl_scop_get_attributes(scop
, &nb_parameters
, &nb_iterators
,
192 &nb_scattdims
, &nb_localdims
, &array_id
);
194 return osl_names_generate("P", nb_parameters
,
203 * osl_scop_print function:
204 * this function prints the content of an osl_scop_t structure (*scop)
205 * into a file (file, possibly stdout) in the OpenScop textual format.
206 * \param file The file where the information has to be printed.
207 * \param scop The scop structure whose information has to be printed.
209 void osl_scop_print(FILE * file
, osl_scop_p scop
) {
210 int parameters_backedup
= 0;
211 osl_strings_p parameters_backup
= NULL
;
215 fprintf(file
, "# NULL scop\n");
219 fprintf(file
, "# [File generated by the OpenScop Library %s]\n",
223 if (osl_scop_integrity_check(scop
) == 0)
224 OSL_warning("OpenScop integrity check failed. "
225 "Something may go wrong.");
227 // Generate the names for the various dimensions.
228 names
= osl_scop_names(scop
);
230 while (scop
!= NULL
) {
231 // If possible, replace parameter names with scop iterator names.
232 if (osl_generic_has_URI(scop
->parameters
, OSL_URI_STRINGS
)) {
233 parameters_backedup
= 1;
234 parameters_backup
= names
->parameters
;
235 names
->parameters
= scop
->parameters
->data
;
238 fprintf(file
, "\n"OSL_TAG_START_SCOP
"\n\n");
239 fprintf(file
, "# =============================================== "
241 fprintf(file
, "# Language\n");
242 fprintf(file
, "%s\n\n", scop
->language
);
244 fprintf(file
, "# Context\n");
245 osl_relation_pprint(file
, scop
->context
, names
);
248 osl_util_print_provided(file
,
249 osl_generic_has_URI(scop
->parameters
, OSL_URI_STRINGS
),
251 osl_generic_print(file
, scop
->parameters
);
253 fprintf(file
, "\n# Number of statements\n");
254 fprintf(file
, "%d\n\n",osl_statement_number(scop
->statement
));
256 osl_statement_pprint(file
, scop
->statement
, names
);
258 if (scop
->extension
) {
259 fprintf(file
, "# =============================================== "
261 osl_generic_print(file
, scop
->extension
);
263 fprintf(file
, "\n"OSL_TAG_END_SCOP
"\n\n");
265 // If necessary, switch back parameter names.
266 if (parameters_backedup
) {
267 parameters_backedup
= 0;
268 names
->parameters
= parameters_backup
;
274 osl_names_free(names
);
278 /*****************************************************************************
280 *****************************************************************************/
284 * osl_scop_pread function ("precision read"):
285 * this function reads a list of scop structures from a file (possibly stdin)
286 * complying to the OpenScop textual format and returns a pointer to this
287 * scop list. If some relation properties (number of input/output/local
288 * dimensions and number of parameters) are undefined, it will define them
289 * according to the available information.
290 * \param[in] file The file where the scop has to be read.
291 * \param[in] precision The precision of the relation elements.
292 * \return A pointer to the scop structure that has been read.
294 osl_scop_p
osl_scop_pread(FILE * file
, int precision
) {
295 osl_scop_p list
= NULL
, current
= NULL
, scop
;
296 osl_statement_p stmt
= NULL
;
297 osl_statement_p prev
= NULL
;
298 osl_interface_p interface
;
299 osl_strings_p language
;
301 char buffer
[OSL_MAX_STRING
];
313 tmp
= osl_util_read_uptotag(file
, OSL_TAG_START_SCOP
);
316 OSL_debug("no more scop in the file");
320 scop
= osl_scop_malloc();
321 osl_scop_register_default_extensions(scop
);
327 // Read the language.
328 language
= osl_strings_read(file
);
329 if (osl_strings_size(language
) == 0)
330 OSL_error("no language (backend) specified");
332 if (osl_strings_size(language
) > 1)
333 OSL_warning("uninterpreted information (after language)");
335 if (language
!= NULL
) {
336 scop
->language
= strdup(language
->string
[0]);
337 osl_strings_free(language
);
340 // Read the context domain.
341 scop
->context
= osl_relation_pread(file
, precision
);
343 // Read the parameters.
344 scop
->parameter_type
= OSL_TYPE_STRING
;
345 if (osl_util_read_int(file
, NULL
) > 0) {
346 interface
= osl_strings_interface();
347 start
= osl_util_skip_blank_and_comments(file
, buffer
);
348 scop
->parameters
= osl_generic_sread(start
, interface
);
349 osl_interface_free(interface
);
353 // III. STATEMENT PART
356 // Read the number of statements.
357 nb_statements
= osl_util_read_int(file
, NULL
);
359 for (i
= 0; i
< nb_statements
; i
++) {
360 // Read each statement.
361 stmt
= osl_statement_pread(file
, precision
);
362 if (scop
->statement
== NULL
)
363 scop
->statement
= stmt
;
370 // IV. EXTENSION PART (TO THE END TAG)
373 // Read up the end tag (if any), and store extensions.
374 scop
->extension
= osl_generic_read(file
, scop
->registry
);
376 // Add the new scop to the list.
382 current
->next
= scop
;
387 if (!osl_scop_integrity_check(list
))
388 OSL_warning("scop integrity check failed");
395 * osl_scop_read function:
396 * this function is equivalent to osl_scop_pread() except that
397 * the precision corresponds to the precision environment variable or
398 * to the highest available precision if it is not defined.
399 * \see{osl_scop_pread}
401 osl_scop_p
osl_scop_read(FILE * foo
) {
402 int precision
= osl_util_get_precision();
403 return osl_scop_pread(foo
, precision
);
407 /*+***************************************************************************
408 * Memory allocation/deallocation functions *
409 *****************************************************************************/
413 * osl_scop_malloc function:
414 * this function allocates the memory space for a osl_scop_t structure and
415 * sets its fields with default values. Then it returns a pointer to the
417 * \return A pointer to an empty scop with fields set to default values.
419 osl_scop_p
osl_scop_malloc() {
422 OSL_malloc(scop
, osl_scop_p
, sizeof(osl_scop_t
));
424 scop
->language
= NULL
;
425 scop
->context
= NULL
;
426 scop
->parameter_type
= OSL_UNDEFINED
;
427 scop
->parameters
= NULL
;
428 scop
->statement
= NULL
;
429 scop
->registry
= NULL
;
430 scop
->extension
= NULL
;
439 * osl_scop_free function:
440 * This function frees the allocated memory for a osl_scop_t structure.
441 * \param scop The pointer to the scop we want to free.
443 void osl_scop_free(osl_scop_p scop
) {
446 while (scop
!= NULL
) {
447 if (scop
->language
!= NULL
)
448 free(scop
->language
);
449 osl_generic_free(scop
->parameters
);
450 osl_relation_free(scop
->context
);
451 osl_statement_free(scop
->statement
);
452 osl_interface_free(scop
->registry
);
453 osl_generic_free(scop
->extension
);
462 /*+***************************************************************************
463 * Processing functions *
464 *****************************************************************************/
468 * osl_scop_register_default_extensions function:
469 * this function registers the default OpenScop Library extensions to an
471 * \param scop The scop for which default options have to be registered.
473 void osl_scop_register_default_extensions(osl_scop_p scop
) {
475 osl_interface_add(&(scop
->registry
), osl_textual_interface());
476 osl_interface_add(&(scop
->registry
), osl_comment_interface());
477 osl_interface_add(&(scop
->registry
), osl_arrays_interface());
478 osl_interface_add(&(scop
->registry
), osl_lines_interface());
479 osl_interface_add(&(scop
->registry
), osl_irregular_interface());
484 * osl_scop_clone function:
485 * This functions builds and returns a "hard copy" (not a pointer copy)
486 * of a osl_statement_t data structure provided as parameter.
487 * Note that the usr field is not touched by this function.
488 * \param statement The pointer to the scop we want to clone.
489 * \return A pointer to the full clone of the scop provided as parameter.
491 osl_scop_p
osl_scop_clone(osl_scop_p scop
) {
492 osl_scop_p clone
= NULL
, node
, previous
= NULL
;
495 while (scop
!= NULL
) {
496 node
= osl_scop_malloc();
497 node
->version
= scop
->version
;
498 if (scop
->language
!= NULL
)
499 node
->language
= strdup(scop
->language
);
500 node
->context
= osl_relation_clone(scop
->context
);
501 node
->parameter_type
= scop
->parameter_type
;
502 node
->parameters
= osl_generic_clone(scop
->parameters
);
503 node
->statement
= osl_statement_clone(scop
->statement
);
504 node
->registry
= osl_interface_clone(scop
->registry
);
505 node
->extension
= osl_generic_clone(scop
->extension
);
513 previous
->next
= node
;
514 previous
= previous
->next
;
525 * osl_scop_equal function:
526 * this function returns true if the two scops are the same, false
527 * otherwise (the usr field is not tested).
528 * \param s1 The first scop.
529 * \param s2 The second scop.
530 * \return 1 if s1 and s2 are the same (content-wise), 0 otherwise.
532 int osl_scop_equal(osl_scop_p s1
, osl_scop_p s2
) {
534 while ((s1
!= NULL
) && (s2
!= NULL
)) {
538 if (s1
->version
!= s2
->version
) {
539 OSL_info("versions are not the same");
543 if (strcmp(s1
->language
, s2
->language
) != 0) {
544 OSL_info("languages are not the same");
548 if (!osl_relation_equal(s1
->context
, s2
->context
)) {
549 OSL_info("contexts are not the same");
553 if (s1
->parameter_type
!= s2
->parameter_type
) {
554 OSL_info("parameter types are not the same");
558 if (!osl_generic_equal(s1
->parameters
, s2
->parameters
)) {
559 OSL_info("parameters are not the same");
563 if (!osl_statement_equal(s1
->statement
, s2
->statement
)) {
564 OSL_info("statements are not the same");
568 if (!osl_interface_equal(s1
->registry
, s2
->registry
)) {
569 OSL_info("registries are not the same");
573 if (!osl_generic_equal(s1
->extension
, s2
->extension
)) {
574 OSL_info("extensions are not the same");
582 if (((s1
== NULL
) && (s2
!= NULL
)) || ((s1
!= NULL
) && (s2
== NULL
)))
590 * osl_scop_integrity_check function:
591 * This function checks that a scop is "well formed". It returns 0 if the
592 * check failed or 1 if no problem has been detected.
593 * \param scop The scop we want to check.
594 * \return 0 if the integrity check fails, 1 otherwise.
596 int osl_scop_integrity_check(osl_scop_p scop
) {
597 int expected_nb_parameters
;
600 while (scop
!= NULL
) {
601 // Check the language.
602 if ((scop
->language
!= NULL
) &&
603 (!strcmp(scop
->language
, "caml") || !strcmp(scop
->language
, "Caml") ||
604 !strcmp(scop
->language
, "ocaml") || !strcmp(scop
->language
, "OCaml")))
605 fprintf(stderr
, "[OpenScop] Alert: What ?! Caml ?! Are you sure ?!?!\n");
607 // Check the context.
608 if (!osl_relation_integrity_check(scop
->context
,
615 // Get the number of parameters.
616 if (scop
->context
!= NULL
)
617 expected_nb_parameters
= scop
->context
->nb_parameters
;
619 expected_nb_parameters
= OSL_UNDEFINED
;
621 // TODO : check the number of parameter strings.
623 if (!osl_statement_integrity_check(scop
->statement
,
624 expected_nb_parameters
))
635 * osl_scop_get_nb_parameters function:
636 * this function returns the number of global parameters of a given SCoP.
637 * \param scop The scop we want to know the number of global parameters.
638 * \return The number of global parameters in the scop.
640 int osl_scop_get_nb_parameters(osl_scop_p scop
) {
642 if (scop
->context
== NULL
) {
643 OSL_warning("no context domain, assuming 0 parameters");
647 return scop
->context
->nb_parameters
;
653 * osl_scop_register_extension function:
654 * this function registers a list of extension interfaces to a scop, i.e., it
655 * adds them to the scop registry. In addition, it will extract extensions
656 * corresponding to those interfaces from the textual form of the extensions
657 * (if any) and add them to the scop extension list.
658 * \param scop The scop for which an extension has to be registered.
659 * \param interface The extension interface to register within the scop.
661 void osl_scop_register_extension(osl_scop_p scop
, osl_interface_p interface
) {
662 osl_generic_p textual
, new;
663 char * extension_string
;
665 if ((interface
!= NULL
) && (scop
!= NULL
)) {
666 osl_interface_add(&scop
->registry
, interface
);
668 textual
= osl_generic_lookup(scop
->extension
, interface
->URI
);
669 if (textual
!= NULL
) {
670 extension_string
= ((osl_textual_p
)textual
->data
)->textual
;
671 new = osl_generic_sread(extension_string
, interface
);
672 osl_generic_add(&scop
->extension
, new);
679 * osl_scop_get_attributes function:
680 * this function returns, through its parameters, the maximum values of the
681 * relation attributes (nb_iterators, nb_parameters etc) in the scop.
682 * HOWEVER, it updates the parameter value iff the attribute is greater than
683 * the input parameter value. Hence it may be used to get the attributes as
684 * well as to find the maximum attributes for several scop lists. The array
685 * identifier 0 is used when there is no array identifier (AND this is OK),
686 * OSL_UNDEFINED is used to report it is impossible to provide the property
687 * while it should. This function is not intended for checking, the input
688 * scop should be correct.
689 * \param[in] scop The scop to extract attributes values.
690 * \param[in,out] nb_parameters Number of parameter attribute.
691 * \param[in,out] nb_iterators Number of iterators attribute.
692 * \param[in,out] nb_scattdims Number of scattering dimensions attribute.
693 * \param[in,out] nb_localdims Number of local dimensions attribute.
694 * \param[in,out] array_id Maximum array identifier attribute.
696 void osl_scop_get_attributes(osl_scop_p scop
,
702 int local_nb_parameters
= OSL_UNDEFINED
;
703 int local_nb_iterators
= OSL_UNDEFINED
;
704 int local_nb_scattdims
= OSL_UNDEFINED
;
705 int local_nb_localdims
= OSL_UNDEFINED
;
706 int local_array_id
= OSL_UNDEFINED
;
708 while (scop
!= NULL
) {
709 osl_relation_get_attributes(scop
->context
,
710 &local_nb_parameters
,
716 osl_statement_get_attributes(scop
->statement
,
717 &local_nb_parameters
,
723 *nb_parameters
= OSL_max(*nb_parameters
, local_nb_parameters
);
724 *nb_iterators
= OSL_max(*nb_iterators
, local_nb_iterators
);
725 *nb_scattdims
= OSL_max(*nb_scattdims
, local_nb_scattdims
);
726 *nb_localdims
= OSL_max(*nb_localdims
, local_nb_localdims
);
727 *array_id
= OSL_max(*array_id
, local_array_id
);
734 * osl_scop_normalize_scattering function:
735 * this function modifies a scop such that all scattering relation have
736 * the same number of output dimensions (additional output dimensions are
737 * set as being equal to zero).
738 * \param[in,out] scop The scop to nomalize the scattering functions.
740 void osl_scop_normalize_scattering(osl_scop_p scop
) {
741 int max_scattering_dims
= 0;
742 osl_statement_p statement
;
743 osl_relation_p extended
;
745 if ((scop
!= NULL
) && (scop
->statement
!= NULL
)) {
746 // Get the max number of scattering dimensions.
747 statement
= scop
->statement
;
748 while (statement
!= NULL
) {
749 if (statement
->scattering
!= NULL
) {
750 max_scattering_dims
= OSL_max(max_scattering_dims
,
751 statement
->scattering
->nb_output_dims
);
753 statement
= statement
->next
;
757 statement
= scop
->statement
;
758 while (statement
!= NULL
) {
759 if (statement
->scattering
!= NULL
) {
760 extended
= osl_relation_extend_output(statement
->scattering
,
761 max_scattering_dims
);
762 osl_relation_free(statement
->scattering
);
763 statement
->scattering
= extended
;
765 statement
= statement
->next
;