6#ifndef MAX_UNROLL_ITERATIONS
7#define MAX_UNROLL_ITERATIONS (4)
29is_side_effect_call_name(
const char* name)
31 return !strncmp(name,
"write",5) || !strncmp(name,
"reduce",6) || !strncmp(name,
"print",5) || !strncmp(name,
"fprintf",7)
32 || !strncmp(name,
"atomic",6) || !strncmp(name,
"AC_INTERNAL_write",17) || !strncmp(name,
"ac_dummy_write",14);
35get_func_call_name(
const ASTNode* node)
38 const ASTNode*
id = get_node_by_token(IDENTIFIER,node->
lhs);
39 return id ?
id->buffer : NULL;
44 if(node->
lhs) collect_callee_names(node->
lhs,dst);
45 if(node->
rhs) collect_callee_names(node->
rhs,dst);
46 const char* name = get_func_call_name(node);
47 if(name && str_vec_get_index(*dst,name) < 0) push(dst,name);
50eval_loop_bound(
const ASTNode* bound,
int* res)
52 const char* str = intern(combine_buffers_new(bound));
59 ASTNode* copy = astnode_dup(bound,NULL);
60 *res = eval_int(copy,const_int_values,const_ints,run_const_int_values,run_const_ints,
false,&err);
67get_const_loop_trip_count_base(
const ASTNode* node,
bool* literal_bounds,
int* start_value)
69 if(!is_for_iteration_statement(node))
return -1;
71 if(!for_expr || !for_expr->
rhs || for_expr->
rhs->token != RANGE)
return -1;
72 const ASTNode* start_node = for_expr->
rhs->lhs->lhs;
73 const ASTNode* end_node = for_expr->
rhs->rhs->lhs;
74 if(!start_node || !end_node)
return -1;
75 *literal_bounds = is_number(intern(combine_buffers_new(start_node))) && is_number(intern(combine_buffers_new(end_node)));
76 int start = 0, end = 0;
77 if(!eval_loop_bound(start_node,&start) || !eval_loop_bound(end_node,&end))
return -1;
79 return end > start ? end-start : 0;
82is_long_const_loop(
const ASTNode* node)
91 if(str_vec_get_index(info->
stencils,name) >= 0)
96 const int index = str_vec_get_index(info->
funcs,name);
97 if(
index < 0)
return tmp;
101mark_used_outside_long_loops(
loop_hoist_info* info,
const char* array,
const char* stencil)
103 const char* key = sprintf_intern(
"%s:%s",array,stencil);
108used_outside_long_loops(
const loop_hoist_info* info,
const char* array,
const char* stencil)
110 const char* key = sprintf_intern(
"%s:%s",array,stencil);
116 if(node->
token == IDENTIFIER && node->
buffer && str_vec_get_index(*dst,node->
buffer) < 0) push(dst,node->
buffer);
117 if(node->
lhs) collect_identifiers(node->
lhs,dst);
118 if(node->
rhs) collect_identifiers(node->
rhs,dst);
124collect_stencil_uses_outside_long_loops(
const ASTNode* node,
const bool in_function,
const bool in_call_args,
loop_hoist_info* info)
126 if(is_long_const_loop(node))
return;
128 if(in_func && !in_call_args && node->
token == IDENTIFIER && node->
buffer)
129 mark_used_outside_long_loops(info,node->
buffer,intern(
"value_stencil"));
130 const char* name = in_func ? get_func_call_name(node) : NULL;
131 if(name && node->
rhs)
138 collect_identifiers(node->
rhs,&ids);
139 for(
size_t i = 0; i < ids.
size; ++i)
140 for(
size_t j = 0; j <
stencils->size; ++j)
141 mark_used_outside_long_loops(info,ids.
data[i],
stencils->data[j]);
146 if(node->
lhs) collect_stencil_uses_outside_long_loops(node->
lhs,in_func,in_call_args,info);
147 if(node->
rhs) collect_stencil_uses_outside_long_loops(node->
rhs,in_func,in_call_args || name != NULL,info);
150get_loop_hoist_info(
const ASTNode* root)
153 memset(&info,0,
sizeof(info));
157 free_node_vec(&nodes);
163 for(
size_t i = 0; i < funcs.
size; ++i)
175 free_node_vec(&funcs);
176 free_str_vec(&func_names);
184 for(
size_t i = 0; i < n; ++i)
187 for(
size_t j = 0; j < callees->size; ++j)
189 const char* callee = callees->data[j];
190 const int index = str_vec_get_index(info.
funcs,callee);
192 if(str_vec_get_index(info.
stencils,callee) >= 0 && str_vec_get_index(*own_stencils,callee) < 0) { push(own_stencils,callee); changed =
true; }
196 for(
size_t k = 0; k < callee_stencils->size; ++k)
197 if(str_vec_get_index(*own_stencils,callee_stencils->data[k]) < 0) { push(own_stencils,callee_stencils->data[k]); changed =
true; }
205 collect_stencil_uses_outside_long_loops(root,
false,
false,&info);
214 free_str_vec(callees);
226 free_str_vec(&info->
funcs);
231subtree_has_identifier(
const ASTNode* node,
const char*
id)
233 if(node->
token == IDENTIFIER && node->
buffer ==
id)
return true;
234 return (node->
lhs && subtree_has_identifier(node->
lhs,
id)) || (node->
rhs && subtree_has_identifier(node->
rhs,
id));
239 if(node->
token == IDENTIFIER && node->
buffer && str_vec_get_index(ids,node->
buffer) >= 0)
return true;
240 return (node->
lhs && subtree_has_any_identifier(node->
lhs,ids)) || (node->
rhs && subtree_has_any_identifier(node->
rhs,ids));
245 if(node->
token == IDENTIFIER && node->
buffer && str_vec_get_index(*dst,node->
buffer) < 0) push(dst,node->
buffer);
246 if(node->
lhs) collect_identifiers_outside_indexes(node->
lhs,dst);
256 if(node->
lhs) collect_assigned_identifiers(node->
lhs,dst);
257 if(node->
rhs) collect_assigned_identifiers(node->
rhs,dst);
261collect_loop_indexed_arrays(
const ASTNode* node,
const char* loop_variable,
string_vec* dst)
265 const ASTNode* base = get_node_by_token(IDENTIFIER,node->
lhs);
266 if(base && str_vec_get_index(*dst,base->
buffer) < 0) push(dst,base->
buffer);
268 if(node->
lhs) collect_loop_indexed_arrays(node->
lhs,loop_variable,dst);
269 if(node->
rhs) collect_loop_indexed_arrays(node->
rhs,loop_variable,dst);
272is_loop_statement(
const ASTNode* node)
274 if(is_for_iteration_statement(node))
return true;
275 return node->
lhs && node->
lhs->lhs && node->
lhs->lhs->token == WHILE;
278is_logical_binary_expression(
const ASTNode* node)
280 if(!node_is_binary_expr(node))
return false;
281 const ASTNode* op = get_node_by_token(BINARY_OP,node->
rhs->lhs);
287 const char* name = get_func_call_name(node);
288 if(!name || !node->
rhs)
return false;
289 if(node->
parent && node->
parent->token == NON_RETURNING_FUNC_CALL)
return false;
290 const int index = str_vec_get_index(info->
funcs,name);
291 const bool is_stencil = str_vec_get_index(info->
stencils,name) >= 0;
293 if(!subtree_has_identifier(node->
rhs,loop_variable))
return false;
294 if(subtree_has_any_identifier(node->
rhs,assigned))
return false;
298subtree_has_func_call(
const ASTNode* node)
301 return (node->
lhs && subtree_has_func_call(node->
lhs)) || (node->
rhs && subtree_has_func_call(node->
rhs));
306is_uniform_condition(
const ASTNode* node,
const char* loop_variable,
const string_vec assigned)
308 return !subtree_has_func_call(node) && !subtree_has_identifier(node,loop_variable) && !subtree_has_any_identifier(node,assigned);
318 if(*write_seen)
return;
319 if((node->
type &
NODE_FUNCTION_CALL) && !conditional && is_hoistable_call(node,loop_variable,assigned,info))
326 const bool uniform = node->
lhs && is_uniform_condition(node->
lhs,loop_variable,assigned);
327 if(node->
lhs) find_hoistable_calls(node->
lhs,loop_variable,assigned,info,conditional,write_seen,dst);
328 if(node->
rhs) find_hoistable_calls(node->
rhs,loop_variable,assigned,info,conditional || !uniform,write_seen,dst);
331 const bool cond = conditional || (node->
type &
NODE_TERNARY) || is_loop_statement(node);
332 if(node->
lhs) find_hoistable_calls(node->
lhs,loop_variable,assigned,info,cond,write_seen,dst);
333 if(node->
rhs) find_hoistable_calls(node->
rhs,loop_variable,assigned,info,cond || is_logical_binary_expression(node),write_seen,dst);
334 const char* name = get_func_call_name(node);
337 const int index = str_vec_get_index(info->
funcs,name);
342create_non_returning_call_statement(
const char* func_name)
345 ASTNode* call = create_func_call(func_name,params);
347 astnode_set_postfix(
";",basic_statement);
348 basic_statement->
token = NON_RETURNING_FUNC_CALL;
349 return create_basic_statement(basic_statement);
357 const char* loop_variable = node->
lhs->rhs->lhs->buffer;
359 collect_assigned_identifiers(body,&assigned);
361 bool write_seen =
false;
362 find_hoistable_calls(body,loop_variable,assigned,info,
false,&write_seen,&calls);
363 free_str_vec(&assigned);
366 free_node_vec(&calls);
370 bool used_elsewhere =
true;
371 for(
size_t i = 0; i < calls.
size && used_elsewhere; ++i)
374 collect_loop_indexed_arrays(calls.
data[i]->
rhs,loop_variable,&arrays);
377 if(arrays.
size == 0) used_elsewhere =
false;
378 for(
size_t a = 0; a < arrays.
size && used_elsewhere; ++a)
379 for(
size_t st = 0; st <
stencils->size && used_elsewhere; ++st)
380 if(!used_outside_long_loops(info,arrays.
data[a],
stencils->data[st])) used_elsewhere =
false;
382 free_str_vec(&arrays);
386 free_node_vec(&calls);
391 push_to_statement_list(head,create_non_returning_call_statement(
"ac_dynamic_stencils_begin"));
395 for(
size_t i = 0; i < calls.
size; ++i)
398 const char* call_str = intern(combine_all_new(call));
399 const int index = str_vec_get_index(hoisted_calls,call_str);
402 replace_node(call,create_primary_expression(hoisted_vars.
data[
index]));
406 const char* var_name = sprintf_intern(
"ac_loop_hoisted_%d",info->
hoist_counter++);
408 const char* type = get_expr_type(call);
409 ASTNode* call_copy = astnode_dup(call,NULL);
410 replace_node(call,create_primary_expression(var_name));
411 push_to_statement_list(head,create_assignment(create_declaration(var_name,type,CONST_STR),call_copy,EQ_STR));
412 push(&hoisted_calls,call_str);
413 push(&hoisted_vars,var_name);
415 free_str_vec(&hoisted_calls);
416 free_str_vec(&hoisted_vars);
417 push_to_statement_list(head,create_non_returning_call_statement(
"ac_dynamic_stencils_end"));
418 free_node_vec(&calls);
428 if(node->
lhs) unroll_constant_loops_recursive(node->
lhs,info);
429 if(node->
rhs) unroll_constant_loops_recursive(node->
rhs,info);
433 TRAVERSE_PREAMBLE_PARAMS(unroll_constant_loops_recursive,info);
434 bool literal_bounds =
false;
436 const int trip_count = get_const_loop_trip_count_base(node,&literal_bounds,&start_value);
437 if(trip_count < 0)
return;
439 if(!literal_bounds)
return;
441 const char* loop_variable = for_expr->
lhs->buffer;
448 const int end_value = start_value + trip_count;
450 for(
int i = start_value; i < end_value; ++i)
452 const char* replacement = intern(itoa(i));
453 ASTNode* res = astnode_dup(iterated_body,NULL);
454 replace_variable_with_constant_int(res,loop_variable,replacement);
455 push_node(&nodes,res);
458 ASTNode* res = build_list_node(nodes,
"");
461 if(res == NULL)
return;
462 free_node_vec(&nodes);
463 replace_node(node,res);
471 unroll_constant_loops_recursive(root,&info);
472 free_loop_hoist_info(&info);
@ NODE_STENCIL
Definition ast.h:75
@ NODE_ASSIGNMENT
Definition ast.h:84
@ NODE_FUNCTION_CALL
Definition ast.h:92
@ NODE_TERNARY
Definition ast.h:81
@ NODE_ARRAY_ACCESS
Definition ast.h:89
@ NODE_FUNCTION
Definition ast.h:98
@ NODE_DECLARATION
Definition ast.h:72
@ NODE_DFUNCTION
Definition ast.h:66
@ NODE_STATEMENT_LIST_HEAD
Definition ast.h:90
@ NODE_IF
Definition ast.h:91
@ NODE_BEGIN_SCOPE
Definition ast.h:71
@ NODE_UNKNOWN
Definition ast.h:64
HASHMAP_WEAK int hashmap_create(const hashmap_uint32_t initial_capacity, struct hashmap_s *const out_hashmap)
Create a hashmap.
Definition hashtable.h:295
HASHMAP_WEAK void hashmap_destroy(struct hashmap_s *const hashmap)
Destroy the hashmap.
Definition hashtable.h:494
HASHMAP_WEAK void * hashmap_get(const struct hashmap_s *const hashmap, const void *const key, const hashmap_uint32_t len)
Get an element from the hashmap.
Definition hashtable.h:364
HASHMAP_WEAK int hashmap_put(struct hashmap_s *const hashmap, const void *const key, const hashmap_uint32_t len, void *const value)
Put an element into the hashmap.
Definition hashtable.h:334
#define MAX_UNROLL_ITERATIONS
Definition loop_hoisting.h:7
long_loop_action
Definition loop_hoisting.h:351
@ LOOP_KEEP_ROLLED
Definition loop_hoisting.h:351
@ LOOP_UNROLL
Definition loop_hoisting.h:351
@ LOOP_HOISTED
Definition loop_hoisting.h:351
void unroll_constant_loops(ASTNode *root)
Definition loop_hoisting.h:468
int counter
Definition string_vec.h:123
#define VEC_INITIALIZER
Definition string_vec.h:7
struct astnode_s * lhs
Definition ast.h:148
struct astnode_s * rhs
Definition ast.h:149
NodeType type
Definition ast.h:157
struct astnode_s * parent
Definition ast.h:147
int token
Definition ast.h:156
const char * buffer
Definition ast.h:150
Definition loop_hoisting.h:18
bool * has_side_effects
Definition loop_hoisting.h:23
int hoist_counter
Definition loop_hoisting.h:25
struct hashmap_s used_outside_long_loops
Definition loop_hoisting.h:24
node_vec func_callees
Definition loop_hoisting.h:21
string_vec funcs
Definition loop_hoisting.h:20
node_vec func_stencils
Definition loop_hoisting.h:22
string_vec stencils
Definition loop_hoisting.h:19
size_t size
Definition ast.h:876
const ASTNode ** data
Definition ast.h:875
Definition string_vec.h:9
size_t size
Definition string_vec.h:11
const char ** data
Definition string_vec.h:10
__device__ AcReal stencils[NUM_STENCILS][STENCIL_DEPTH][STENCIL_HEIGHT][STENCIL_WIDTH]
static __device__ const int const int index
Definition user_kernel.h:128