PIPS
openmp_generation.c
Go to the documentation of this file.
1 #ifdef HAVE_CONFIG_H
2  #include "pips_config.h"
3 #endif
4 
5 
6 #include <stdio.h>
7 #include <ctype.h>
8 #include <string.h>
9 #include "boolean.h"
10 #include <stdbool.h>
11 
12 #include "genC.h"
13 #include "linear.h"
14 #include "ri.h"
15 #include "effects.h"
16 #include "database.h"
17 #include "misc.h"
18 #include "text.h"
19 #include "text-util.h"
20 #include "ri-util.h"
21 #include "effects-util.h"
22 #include "accel-util.h"
23 
24 #include "effects-generic.h"
25 #include "effects-simple.h"
26 
27 #include "pipsdbm.h"
28 #include "resources.h"
29 #include "control.h"
30 #include "conversion.h"
31 #include "properties.h"
32 #include "transformations.h"
33 
34 #include "effects-convex.h"
35 #include "genC.h"
36 
37 #include "complexity_ri.h"
38 #include "dg.h"
39 
40 /* Instantiation of the dependence graph: */
43 #include "graph.h"
44 #include "ricedg.h"
45 #include "chains.h"
46 #include "task_parallelization.h"
47 
48 static bool omp_parallel = false;
49 
50 
52 {
54  string data = strdup(concatenate( "omp taskwait ",NULL));
57  list list_stmts = CONS(STATEMENT, st, CONS(STATEMENT, copy_statement(stmt), NIL));
59  return;
60 }
62  string data = strdup(concatenate( "omp parallel default(shared) ",NULL));
64  list stmts = sequence_statements(statement_sequence(stmt)), body = NIL, decls = NIL;
66  FOREACH(STATEMENT, st, stmts){
68  decls = CONS(STATEMENT, st, decls);
69  else
70  if(!return_statement_p(st))
71  body = CONS(STATEMENT, st, body);
72  else
73  return_st = st;
74  }
75  if(gen_length(body)>0){
76  st_body = make_block_statement(gen_nreverse(body));
77  add_pragma_str_to_statement (st_body, data, true);
78  decls = CONS(STATEMENT, st_body, decls);
79  }
80  if(gen_length(decls)>0){
82  }
83  }
84  else
86  return;
87 }
88 
89 static bool gen_synchronization(statement stmt, bool nested_p, int length)
90 {
92  switch(synchronization_tag(sync)){
94  if(length>1){
95  add_pragma_str_to_statement(stmt, "omp task", true);
96  if(!omp_parallel)
97  omp_parallel = true;
98  }
99  break;
101  if(!nested_p){
102  add_pragma_str_to_statement(stmt, "omp single", true);
103  nested_p = true;
104  }
105  else
106  {
109  }
110  if(!omp_parallel)
111  omp_parallel = true;
112  break;
113  default:
114  break;
115  }
116  return nested_p;
117 }
118 
119 static bool gen_openmp(statement stmt, bool nested_p){
121  switch(instruction_tag(inst))
122  {
124  {
125  MAPL(stmt_ptr,
126  {
127  statement st = STATEMENT(CAR( stmt_ptr));
128  bool nested_p_local = gen_synchronization(st,nested_p, gen_length(sequence_statements(statement_sequence(stmt))));
129  gen_openmp(st, nested_p_local);
130  },
131  instruction_block(inst));
132  break;
133  }
134  case is_instruction_test :
135  {
136  test t = instruction_test(inst);
137  bool nested_p_t = gen_synchronization(test_true(t),nested_p,1);
138  bool nested_p_f = gen_synchronization(test_false(t),nested_p,1);
139  gen_openmp(test_true(t), nested_p_t);
140  gen_openmp(test_false(t), nested_p_f);
141  break;
142  }
143  case is_instruction_loop :
144  {
145  loop l = statement_loop(stmt);
146  statement body = loop_body(l);
147  nested_p = gen_synchronization(body,nested_p,1);
148  gen_openmp(body, nested_p);
149  break;
150  }
151  default:
152  break;
153  }
154  return true;
155 }
156 
157 
158 
159 /* OpenMP generation pass */
161 {
162  statement module_stat_i = (statement)db_get_memory_resource(DBR_SHARED_SPIRE_CODE, module_name, true);
163  statement module_stat = copy_statement(module_stat_i);
164  set_ordering_to_statement(module_stat);
166  set_current_module_statement(module_stat);
167  gen_openmp(module_stat, false);
168  if(omp_parallel)
169  gen_omp_parallel(module_stat);
171  insert_statement(module_stat, return_st, false);
172  module_reorder(module_stat);
173  DB_PUT_MEMORY_RESOURCE(DBR_PARALLELIZED_CODE, module_name, module_stat);
177  return true;
178 }
179 
statement return_st
Definition: HBDSC.c:53
statement copy_statement(statement p)
STATEMENT.
Definition: ri.c:2186
instruction make_instruction_sequence(sequence _field_)
Definition: ri.c:1169
synchronization make_synchronization_none(void)
Definition: ri.c:2424
sequence make_sequence(list a)
Definition: ri.c:2125
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
statement make_block_statement(list)
Make a block statement from a list of statement.
Definition: statement.c:616
void reset_current_module_entity(void)
Reset the current module entity.
Definition: static.c:97
void reset_current_module_statement(void)
Reset the current module statement.
Definition: static.c:221
statement set_current_module_statement(statement)
Set the current module statement.
Definition: static.c:165
entity set_current_module_entity(entity)
static.c
Definition: static.c:66
list gen_nreverse(list cp)
reverse a list in place
Definition: list.c:304
#define NIL
The empty list (nil in Lisp)
Definition: newgen_list.h:47
size_t gen_length(const list l)
Definition: list.c:150
#define CONS(_t_, _i_, _l_)
List element cell constructor (insert an element at the beginning of a list)
Definition: newgen_list.h:150
#define CAR(pcons)
Get the value of the first element of a list.
Definition: newgen_list.h:92
#define FOREACH(_fe_CASTER, _fe_item, _fe_list)
Apply/map an instruction block on all the elements of a list.
Definition: newgen_list.h:179
#define MAPL(_map_list_cp, _code, _l)
Apply some code on the addresses of all the elements of a list.
Definition: newgen_list.h:203
string db_get_memory_resource(const char *rname, const char *oname, bool pure)
Return the pointer to the resource, whatever it is.
Definition: database.c:755
#define DB_PUT_MEMORY_RESOURCE(res_name, own_name, res_val)
conform to old interface.
Definition: pipsdbm-local.h:66
sequence statement_sequence(statement)
Get the sequence of a statement sequence.
Definition: statement.c:1328
loop statement_loop(statement)
Get the loop of a statement.
Definition: statement.c:1374
bool statement_sequence_p(statement)
Statement classes induced from instruction type.
Definition: statement.c:335
bool return_statement_p(statement)
Test if a statement is a C or Fortran "return".
Definition: statement.c:172
void insert_statement(statement, statement, bool)
This is the normal entry point.
Definition: statement.c:2570
statement make_continue_statement(entity)
Definition: statement.c:953
bool declaration_statement_p(statement)
Had to be optimized according to Beatrice Creusillet.
Definition: statement.c:224
string concatenate(const char *,...)
Return the concatenation of the given strings.
Definition: string.c:183
static bool omp_parallel
static bool gen_openmp(statement stmt, bool nested_p)
dg_vertex_label vertex_label
bool openmp_task_generation(char *module_name)
OpenMP generation pass.
static bool gen_synchronization(statement stmt, bool nested_p, int length)
dg_arc_label arc_label
Instantiation of the dependence graph:
static void gen_omp_taskwait(statement stmt)
static void gen_omp_parallel(statement stmt)
hash_table set_ordering_to_statement(statement s)
To be used instead of initialize_ordering_to_statement() to make sure that the hash table ots is in s...
Definition: ordering.c:172
void reset_ordering_to_statement(void)
Reset the mapping from ordering to statement.
Definition: ordering.c:185
bool module_reorder(statement body)
Reorder a module and recompute order to statement if any.
Definition: reorder.c:244
#define is_instruction_block
soft block->sequence transition
#define instruction_block(i)
entity module_name_to_entity(const char *mn)
This is an alias for local_name_to_top_level_entity.
Definition: entity.c:1479
entity entity_empty_label(void)
Definition: entity.c:1105
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 loop_body(x)
Definition: ri.h:1644
@ is_synchronization_barrier
Definition: ri.h:2626
@ is_synchronization_spawn
Definition: ri.h:2625
#define synchronization_tag(x)
Definition: ri.h:2651
#define test_false(x)
Definition: ri.h:2837
#define statement_synchronization(x)
Definition: ri.h:2466
@ is_instruction_test
Definition: ri.h:1470
@ is_instruction_loop
Definition: ri.h:1471
#define instruction_tag(x)
Definition: ri.h:1511
#define test_true(x)
Definition: ri.h:2835
#define sequence_statements(x)
Definition: ri.h:2360
#define statement_instruction(x)
Definition: ri.h:2458
#define instruction_test(x)
Definition: ri.h:1517
#define statement_undefined_p(x)
Definition: ri.h:2420
#define statement_undefined
Definition: ri.h:2419
#define STATEMENT(x)
STATEMENT.
Definition: ri.h:2413
char * strdup()
The structure used to build lists in NewGen.
Definition: newgen_list.h:41
Definition: statement.c:54