From 09f90ab4e116e7764c40677e0d1575fea734b9bf Mon Sep 17 00:00:00 2001 From: Martin Whitaker Date: Thu, 7 Mar 2013 23:33:52 +0000 Subject: [PATCH] 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. --- compiler.h | 9 +++++++++ eval_tree.cc | 28 +++++++++++++++------------- main.cc | 5 +++++ 3 files changed, 29 insertions(+), 13 deletions(-) diff --git a/compiler.h b/compiler.h index 5cfd6dbed..d52d6d5ab 100644 --- a/compiler.h +++ b/compiler.h @@ -104,6 +104,15 @@ extern bool debug_elaborate; extern bool debug_synth2; 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 */ extern bool disable_virtual_pins; diff --git a/eval_tree.cc b/eval_tree.cc index bb8ca78c6..ebd6218a2 100644 --- a/eval_tree.cc +++ b/eval_tree.cc @@ -1985,10 +1985,16 @@ NetExpr* NetEUFunc::eval_tree() if (!func()->is_const_func()) 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 // the function, so we can't be sure they are constant unless the - // function was called in a constant context. - if (!func()->is_auto() && !need_const_) + // function was called in a constant context or the user has told + // us this is safe. + if (!func()->is_auto() && !need_const_ && (opt_const_func < 2)) return 0; // Run through the input parameters to check they are constants. @@ -2000,17 +2006,13 @@ NetExpr* NetEUFunc::eval_tree() return 0; } - if (need_const_) { - NetFuncDef*def = func_->func_def(); - ivl_assert(*this, def); + NetFuncDef*def = func_->func_def(); + ivl_assert(*this, def); - vectorargs(parms_.size()); - for (unsigned idx = 0 ; idx < parms_.size() ; idx += 1) - args[idx] = parms_[idx]->dup_expr(); + vectorargs(parms_.size()); + for (unsigned idx = 0 ; idx < parms_.size() ; idx += 1) + args[idx] = parms_[idx]->dup_expr(); - NetExpr*res = def->evaluate_function(*this, args); - return res; - } - - return 0; + NetExpr*res = def->evaluate_function(*this, args); + return res; } diff --git a/main.cc b/main.cc index 091c084a2..0ac22e2bd 100644 --- a/main.cc +++ b/main.cc @@ -169,6 +169,11 @@ bool debug_elaborate = false; bool debug_synth2 = false; bool debug_optimizer = false; +/* + * Optimization control flags. + */ +unsigned opt_const_func = 0; + /* * Miscellaneous flags. */