PIPS
generate_pragma.c
Go to the documentation of this file.
1 /*
2  Copyright 1989-2016 MINES ParisTech
3 
4  This file is part of PIPS.
5 
6  PIPS is free software: you can redistribute it and/or modify it
7  under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  any later version.
10 
11  PIPS is distributed in the hope that it will be useful, but WITHOUT ANY
12  WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  FITNESS FOR A PARTICULAR PURPOSE.
14 
15  See the GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with PIPS. If not, see <http://www.gnu.org/licenses/>.
19 
20 */
21 
22 // do not compile unless required
23 #include "phases.h"
24 #ifdef BUILDER_OMPIFY_CODE
25 
26 /**
27  * @file generate_pragma.c
28  * @brief This file holds transformations on sequentiel code
29  * that generates pragmas according to the information available
30  * in the pips RI.
31  * The generated code is sequential in order to allow
32  * further pips transformations the user might want to apply later on.
33  * The type of pragma generated are:
34  * 1- OpenMP pragma: parallel, for and private clauses
35  *
36  * @author pierre villalon <pierre.villalon@hpc-project.com>
37  * @date 2009-05-24
38  */
39 #ifdef HAVE_CONFIG_H
40  #include "pips_config.h"
41 #endif
42 
43 #include "genC.h"
44 #include "linear.h"
45 
46 #include "misc.h"
47 #include "properties.h"
48 #include "pipsdbm.h"
49 
50 #include "text.h"
51 #include "text-util.h"
52 
53 #include "ri.h"
54 #include "effects.h"
55 #include "ri-util.h"
56 #include "prettyprint.h"
57 
58 #include "control.h" // PIPS_PHASE_POSTLUDE
59 #include "reductions.h" // omp_pragma_expr_for
60 #include "accel-util.h"
61 
62 /////////////////////////////////////////////////////PRAGMA AS EXPRESSION
63 
64 
65 /// @brief generate pragma as a list of expressions for a loop
66 /// @return void
67 /// @param l, the loop to decorate with pragma
68 static void generate_expr_omp_pragma_loop (loop l) {
69 
72  // Note that founding such a simple parallel for loop
73  // might show some problems in the code. For example dead code or the usage
74  // of initialized variables. In such a case PIPS follows the principle :
75  // If the code is false or dead then, do it in parallel, it will still be
76  // false or dead.
78 
79  return;
80 }
81 
82 /////////////////////////////////////////////////////PRAGMA AS STRING
83 
84 
85 /// @brief generate pragma for as a string
86 /// @return true if a pragma has been generated
87 /// @param l, the loop to analyze for omp for
88 /// @param stmt, the statament where the pragma should be attached
89 static bool pragma_str_for (loop l, statement stmt) {
90  text t = text_undefined;
91  string str = string_undefined;
92  // get the pragma as text and convert to string
93  t = text_omp_directive (l, 0);
94  str = text_to_string (t);
95  // text appends one uselless \n at the end of the string so remove it
96  chop_newline (str, false);
97  if ((str !=string_undefined) && (str != NULL) && (strcmp (str, "") != 0)) {
98  string tmp = string_undefined;
100  case is_language_fortran:
101  // for fortran case we need to look at the O of OMP and skip !$
102  tmp = strchr(str, 'O');
103  break;
104  case is_language_c:
105  // for C case we need to look at the o of omp and skip #pragma"
106  tmp = strchr(str, 'o');
107  break;
109  pips_internal_error("Need to update F95 case");
110  break;
111  default:
112  pips_internal_error("Language unknown !");
113  break;
114  }
115  // insert the pragma as a string to the current statement
116  if ((tmp !=string_undefined) && (tmp != NULL) && (strcmp (tmp, "") != 0)) {
117  add_pragma_str_to_statement (stmt, tmp, true);
118  pips_debug (5, "new for pragma as an extension added: %s \n", str);
119  return true;
120  }
121  }
122  return false;
123 }
124 
125 
126 /// @brief generate pragma as a string for a loop
127 /// @return void
128 /// @param l, the loop to decorate with pragma
129 static void generate_str_omp_pragma_loop (loop l) {
130 
133 
134  // Note that founding such a simple parallel for loop
135  // might show some problems in the code. For example dead code or the usage
136  // of unitialized variables. In such a case PIPS follows the principle :
137  // If the code is false or dead then, do it in parallel, it will still be
138  // false or dead.
139  pragma_str_for (l, stmt);
140 
141  return;
142 }
143 
144 //////////////////////////////////////////////////////////////
145 // the phases function name
146 
147 bool ompify_code (const char* module_name) {
148  // Use this module name and this environment variable to set
150  "OMPIFY_CODE_DEBUG_LEVEL");
151  // we want omp syntax so save and change the current PRETTYPRINT_PARALLEL
152  // property
153  string previous = strdup(get_string_property("PRETTYPRINT_PARALLEL"));
154  set_string_property("PRETTYPRINT_PARALLEL", "omp");
155  // we need to know which type of pragma need to be generated
156  const char* type = get_string_property("PRAGMA_TYPE");
157 
158  // generate pragma string or expression using the correct language:
160  if(value_code_p(mv)) {
161  code c = value_code(mv);
163  } else {
164  /* Should never arise */
166  }
167 
168  // generate omp pragma for parallel loops
169  // We need to access to the statement containing the current loop, forloop
170  // so ask NewGen gen_recurse to keep this informations for us
171  // Iterate on all the loop
172  if (strcmp (type, "str") == 0)
174  generate_str_omp_pragma_loop);
175  else if (strcmp (type, "expr") == 0)
177  generate_expr_omp_pragma_loop);
178  else pips_assert ("not expected property", false);
179 
180  // Restore the previous PRETTYPRINT_PARALLEL property for the next
181  set_string_property("PRETTYPRINT_PARALLEL", previous);
182  free(previous);
183 
184  // Put back the new statement module
186 
187  return true;
188 }
189 
190 #endif // BUILDER_OMPIFY_CODE
bool ompify_code(const char *)
generate_pragma.c
bool statement_has_omp_parallel_directive_p(statement)
manage_pragma.c
Definition: manage_pragma.c:52
static statement module_statement
Definition: alias_check.c:125
struct _newgen_struct_statement_ * statement
Definition: cloning.h:21
const char * module_name(const char *s)
Return the module part of an entity name.
Definition: entity_names.c:296
char * get_string_property(const char *)
#define gen_recurse(start, domain_number, flt, rwt)
Definition: genC.h:283
void free(void *)
gen_chunk * gen_get_ancestor(int, const void *)
return the first ancestor object found of the given type.
Definition: genClib.c:3560
bool gen_true(__attribute__((unused)) gen_chunk *unused)
Return true and ignore the argument.
Definition: genClib.c:2780
#define PIPS_PHASE_POSTLUDE(new_module_statement)
End a transformation phase by putting back into PIPS the (possibly) modified statement.
#define PIPS_PHASE_PRELUDE(module_name, debug_env_var)
Start a phase that use a module CODE.
void set_prettyprint_language_from_property(enum language_utype native)
set the prettyprint language according to the property PRETTYPRINT_LANGUAGE @description If the prope...
Definition: language.c:103
enum language_utype get_prettyprint_language_tag()
Definition: language.c:67
#define pips_debug
these macros use the GNU extensions that allow variadic macros, including with an empty list.
Definition: misc-local.h:145
#define pips_assert(what, predicate)
common macros, two flavors depending on NDEBUG
Definition: misc-local.h:172
#define pips_internal_error
Definition: misc-local.h:149
string chop_newline(string, bool)
Definition: string.c:294
#define string_undefined
Definition: newgen_types.h:40
text text_omp_directive(loop l, int m)
Definition: misc.c:3084
void set_string_property(const char *, const char *)
bool omp_pragma_expr_for(loop l, statement stmt)
generate "pragma omp for" as a list of expressions
Definition: pragma.c:366
entity module_name_to_entity(const char *mn)
This is an alias for local_name_to_top_level_entity.
Definition: entity.c:1479
void add_pragma_str_to_statement(statement st, const char *s, bool copy_flag)
Add a string as a pragma to a statement.
Definition: pragma.c:425
#define value_code_p(x)
Definition: ri.h:3065
#define loop_domain
newgen_language_domain_defined
Definition: ri.h:218
#define statement_domain
newgen_sizeofexpression_domain_defined
Definition: ri.h:362
#define value_code(x)
Definition: ri.h:3067
#define code_language(x)
Definition: ri.h:792
#define language_tag(x)
Definition: ri.h:1590
@ is_language_fortran
Definition: ri.h:1566
@ is_language_fortran95
Definition: ri.h:1568
@ is_language_c
Definition: ri.h:1567
#define entity_initial(x)
Definition: ri.h:2796
char * strdup()
Definition: statement.c:54
string text_to_string(text t)
SG: moved here from ricedg.
Definition: print.c:239
#define text_undefined
Definition: text.h:91