Add control to enable function evaluation at compile time.

Pure functions with constant operands can be evaluated at compile
time rather than at run time. This patch provides a global control
to enable this optimisation. Until constant function support is
complete, by default it is disabled.
This commit is contained in:
Martin Whitaker 2013-03-07 23:33:52 +00:00 committed by Stephen Williams
parent 5929a00845
commit 09f90ab4e1
3 changed files with 29 additions and 13 deletions

View File

@ -104,6 +104,15 @@ extern bool debug_elaborate;
extern bool debug_synth2; extern bool debug_synth2;
extern bool debug_optimizer; extern bool debug_optimizer;
/* Control evaluation of functions at compile time:
* 0 = only for functions in constant expressions
* 1 = only for automatic functions
* 2 = for all functions
* Level 2 should only be used if the user can guarantee that a
* function's local variables are never accessed from outside the
* function. */
extern unsigned opt_const_func;
/* Possibly temporary flag to control virtualization of pin arrays */ /* Possibly temporary flag to control virtualization of pin arrays */
extern bool disable_virtual_pins; extern bool disable_virtual_pins;

View File

@ -1985,10 +1985,16 @@ NetExpr* NetEUFunc::eval_tree()
if (!func()->is_const_func()) if (!func()->is_const_func())
return 0; return 0;
// If we neither want nor need to evaluate the function at
// compile time, give up now.
if (!opt_const_func && !need_const_)
return 0;
// Variables inside static functions can be accessed from outside // Variables inside static functions can be accessed from outside
// the function, so we can't be sure they are constant unless the // the function, so we can't be sure they are constant unless the
// function was called in a constant context. // function was called in a constant context or the user has told
if (!func()->is_auto() && !need_const_) // us this is safe.
if (!func()->is_auto() && !need_const_ && (opt_const_func < 2))
return 0; return 0;
// Run through the input parameters to check they are constants. // Run through the input parameters to check they are constants.
@ -2000,17 +2006,13 @@ NetExpr* NetEUFunc::eval_tree()
return 0; return 0;
} }
if (need_const_) { NetFuncDef*def = func_->func_def();
NetFuncDef*def = func_->func_def(); ivl_assert(*this, def);
ivl_assert(*this, def);
vector<NetExpr*>args(parms_.size()); vector<NetExpr*>args(parms_.size());
for (unsigned idx = 0 ; idx < parms_.size() ; idx += 1) for (unsigned idx = 0 ; idx < parms_.size() ; idx += 1)
args[idx] = parms_[idx]->dup_expr(); args[idx] = parms_[idx]->dup_expr();
NetExpr*res = def->evaluate_function(*this, args); NetExpr*res = def->evaluate_function(*this, args);
return res; return res;
}
return 0;
} }

View File

@ -169,6 +169,11 @@ bool debug_elaborate = false;
bool debug_synth2 = false; bool debug_synth2 = false;
bool debug_optimizer = false; bool debug_optimizer = false;
/*
* Optimization control flags.
*/
unsigned opt_const_func = 0;
/* /*
* Miscellaneous flags. * Miscellaneous flags.
*/ */