Skip to content

Commit

Permalink
enforce two generation constrains at command line:
Browse files Browse the repository at this point in the history
1) expression complexity: configured through "--upper-expr-complexity <num>". It is a soft limit on how many sub-expressions could be there for any expression. Default is 10

2) block depth: configured through "--max-block-depth <num>". It is a hard limit on the nesting level of blocks. Default is 5
  • Loading branch information
Xuejun Yang committed Jul 26, 2011
1 parent 0b006c5 commit eab8976
Show file tree
Hide file tree
Showing 27 changed files with 225 additions and 76 deletions.
5 changes: 1 addition & 4 deletions src/Block.cpp
Expand Up @@ -110,7 +110,6 @@ Block::make_random(CGContext &cg_context, bool looping)
b->in_array_loop = !(cg_context.iv_bounds.empty());
//b->stm_id = bid++;

++cg_context.stmt_depth;
// Push this block onto the variable scope stack.
curr_func->stack.push_back(b);
curr_func->blocks.push_back(b);
Expand Down Expand Up @@ -148,7 +147,7 @@ Block::make_random(CGContext &cg_context, bool looping)
}

// append nested loop if some must-read/write variables hasn't been accessed
if (b->need_nested_loop(cg_context)) {
if (b->need_nested_loop(cg_context) && cg_context.blk_depth < CGOptions::max_blk_depth()) {
b->append_nested_loop(cg_context);
}

Expand All @@ -162,8 +161,6 @@ Block::make_random(CGContext &cg_context, bool looping)
}

curr_func->stack.pop_back();
--cg_context.stmt_depth;

if (Error::get_error() != SUCCESS) {
//curr_func->stack.pop_back();
delete b;
Expand Down
75 changes: 73 additions & 2 deletions src/Bookkeeper.cpp
Expand Up @@ -43,6 +43,7 @@
#include "FactMgr.h"
#include "CVQualifiers.h"
#include "Statement.h"
#include "Block.h"
#include "CGOptions.h"

using namespace std;
Expand All @@ -53,6 +54,7 @@ using namespace std;
std::vector<int> Bookkeeper::struct_depth_cnts;
int Bookkeeper::union_var_cnt = 0;
std::vector<int> Bookkeeper::expr_depth_cnts;
std::vector<int> Bookkeeper::blk_depth_cnts;
std::vector<int> Bookkeeper::dereference_level_cnts;
int Bookkeeper::address_taken_cnt = 0;
std::vector<int> Bookkeeper::read_dereference_cnts;
Expand Down Expand Up @@ -127,10 +129,49 @@ Bookkeeper::doFinalization()

}

int
Bookkeeper::stat_blk_depths_for_stmt(const Statement* s)
{
size_t i, j;
int cnt = 0;
if (s->eType != eBlock) {
incr_counter(blk_depth_cnts, s->get_blk_depth() -1);
cnt++;
}
vector<const Block*> blks;
s->get_blocks(blks);
for (i=0; i<blks.size(); i++) {
for (j=0; j<blks[i]->stms.size(); j++) {
cnt += stat_blk_depths_for_stmt(blks[i]->stms[j]);
}
}
return cnt;
}

int
Bookkeeper::stat_blk_depths(void)
{
const vector<Function*>& funcs = get_all_functions();
int cnt = 0;
for (size_t i=0; i<funcs.size(); i++) {
cnt += stat_blk_depths_for_stmt(funcs[i]->body);
}
return cnt;
}

void
Bookkeeper::output_stmts_statistics(std::ostream &out)
{
formated_output(out, "stmts: ", Statement::get_current_sid());
size_t i;
int stmt_cnt = stat_blk_depths();
formated_output(out, "stmts: ", stmt_cnt);
formated_output(out, "max block depth: ", (blk_depth_cnts.size() - 1));
out << "breakdown:" << endl;
for (i=0; i<blk_depth_cnts.size(); i++) {
if (blk_depth_cnts[i]) {
out << " depth: " << i << ", occurrence: " << blk_depth_cnts[i] << endl;
}
}
}

void
Expand Down Expand Up @@ -163,14 +204,44 @@ Bookkeeper::output_struct_union_statistics(std::ostream &out)
Bookkeeper::output_bitfields(out);
}

void
Bookkeeper::stat_expr_depths_for_stmt(const Statement* s)
{
size_t i, j;
vector<const Expression*> exprs;
vector<const Block*> blks;
s->get_exprs(exprs);
for (i=0; i<exprs.size(); i++) {
incr_counter(expr_depth_cnts, exprs[i]->get_complexity());
}
s->get_blocks(blks);
for (i=0; i<blks.size(); i++) {
for (j=0; j<blks[i]->stms.size(); j++) {
stat_expr_depths_for_stmt(blks[i]->stms[j]);
}
}
}

void
Bookkeeper::stat_expr_depths(void)
{
const vector<Function*>& funcs = get_all_functions();
for (size_t i=0; i<funcs.size(); i++) {
stat_expr_depths_for_stmt(funcs[i]->body);
}
}

void
Bookkeeper::output_expr_statistics(std::ostream &out)
{
size_t i;
stat_expr_depths();
formated_output(out, "max expression depth: ", (expr_depth_cnts.size() - 1));
out << "breakdown:" << endl;
for (i=0; i<expr_depth_cnts.size(); i++) {
out << " depth: " << i << ", occurrence: " << expr_depth_cnts[i] << endl;
if (expr_depth_cnts[i]) {
out << " depth: " << i << ", occurrence: " << expr_depth_cnts[i] << endl;
}
}
}

Expand Down
9 changes: 9 additions & 0 deletions src/Bookkeeper.h
Expand Up @@ -37,6 +37,7 @@ using namespace std;

class Variable;
class Expression;
class Statement;
class Fact;
class Type;

Expand Down Expand Up @@ -85,12 +86,20 @@ class Bookkeeper

static void output_var_freshness(std::ostream &out);

static void stat_expr_depths_for_stmt(const Statement* s);
static void stat_expr_depths(void);

static int stat_blk_depths_for_stmt(const Statement* s);
static int stat_blk_depths(void);

static std::vector<int> struct_depth_cnts;

static int union_var_cnt;

static std::vector<int> expr_depth_cnts;

static std::vector<int> blk_depth_cnts;

static std::vector<int> dereference_level_cnts;

static int address_taken_cnt;
Expand Down
22 changes: 14 additions & 8 deletions src/CGContext.cpp
Expand Up @@ -58,12 +58,11 @@ const CGContext CGContext::empty_context(0, Effect::get_empty_effect(), 0);

///////////////////////////////////////////////////////////////////////////////
/*
* A convenience constructor. Set `stmt_depth' and `flags' to zero, and set
* `no_read_vars' and `no_write_vars' each to be an empty set.
* A convenience constructor. Context is created when entering functions
*/
CGContext::CGContext(Function *current_func, const Effect &eff_context, Effect *eff_accum)
: current_func(current_func),
stmt_depth(0),
blk_depth(0),
expr_depth(0),
flags(0),
curr_blk(0),
Expand All @@ -78,7 +77,7 @@ CGContext::CGContext(Function *current_func, const Effect &eff_context, Effect *
// adapt current context to create context of parameters
CGContext::CGContext(const CGContext &cgc, const Effect &eff_context, Effect *eff_accum)
: current_func(cgc.current_func),
stmt_depth(cgc.stmt_depth),
blk_depth(cgc.blk_depth),
expr_depth(cgc.expr_depth),
flags(cgc.flags),
call_chain(cgc.call_chain),
Expand All @@ -95,8 +94,8 @@ CGContext::CGContext(const CGContext &cgc, const Effect &eff_context, Effect *ef
// adapt current context to create context of callee
CGContext::CGContext(const CGContext &cgc, Function* f, const Effect &eff_context, Effect *eff_accum)
: current_func(f),
stmt_depth(cgc.stmt_depth),
expr_depth(cgc.expr_depth),
blk_depth(0),
expr_depth(0),
flags(cgc.flags),
call_chain(cgc.call_chain),
curr_blk(cgc.curr_blk),
Expand All @@ -111,8 +110,8 @@ CGContext::CGContext(const CGContext &cgc, Function* f, const Effect &eff_contex
// adapt current context to create context of loop body
CGContext::CGContext(const CGContext &cgc, RWDirective* rwd, const Variable* iv, unsigned int bound)
: current_func(cgc.current_func),
stmt_depth(cgc.stmt_depth),
expr_depth(cgc.expr_depth),
blk_depth(cgc.blk_depth),
expr_depth(0),
flags(cgc.flags | IN_LOOP),
call_chain(cgc.call_chain),
curr_blk(cgc.curr_blk),
Expand Down Expand Up @@ -405,6 +404,13 @@ CGContext::add_effect(const Effect &e, bool include_lhs_effects)
sanity_check();
}

void
CGContext::merge_param_context(const CGContext& param_cg_context, bool include_lhs_effects)
{
add_effect(*param_cg_context.get_effect_accum(), include_lhs_effects);
expr_depth = param_cg_context.expr_depth;
}

/*
*
*/
Expand Down
3 changes: 2 additions & 1 deletion src/CGContext.h
Expand Up @@ -138,6 +138,7 @@ class CGContext
void add_effect(const Effect &e, bool include_lhs_effects=false);
void add_external_effect(const Effect &e);
void add_visible_effect(const Effect &e, const Block* b);
void merge_param_context(const CGContext& param_cg_context, bool include_lhs_effects=false);

int find_variable_scope(const Variable* var) const;

Expand All @@ -154,7 +155,7 @@ class CGContext


public: // XXX
int stmt_depth;
int blk_depth;
int expr_depth;
unsigned int flags;
std::vector<const Block*> call_chain;
Expand Down
6 changes: 3 additions & 3 deletions src/CGOptions.cpp
Expand Up @@ -77,7 +77,7 @@ DEFINE_GETTER_SETTER_STRING_REF(output_file)
DEFINE_GETTER_SETTER_INT (max_funcs)
DEFINE_GETTER_SETTER_INT (max_params)
DEFINE_GETTER_SETTER_INT (max_block_size)
DEFINE_GETTER_SETTER_INT (max_stmt_depth)
DEFINE_GETTER_SETTER_INT (max_blk_depth)
DEFINE_GETTER_SETTER_INT (max_expr_depth)
DEFINE_GETTER_SETTER_BOOL(wrap_volatiles)
DEFINE_GETTER_SETTER_BOOL(allow_const_volatile)
Expand Down Expand Up @@ -176,7 +176,7 @@ CGOptions::set_default_settings(void)
max_funcs(CGOPTIONS_DEFAULT_MAX_FUNCS);
max_params(CGOPTIONS_DEFAULT_MAX_PARAMS);
max_block_size(CGOPTIONS_DEFAULT_MAX_BLOCK_SIZE);
max_stmt_depth(CGOPTIONS_DEFAULT_MAX_STMT_DEPTH);
max_blk_depth(CGOPTIONS_DEFAULT_MAX_BLOCK_DEPTH);
max_expr_depth(CGOPTIONS_DEFAULT_MAX_EXPR_DEPTH);
max_struct_fields(CGOPTIONS_DEFAULT_MAX_STRUCT_FIELDS);
max_union_fields(CGOPTIONS_DEFAULT_MAX_UNION_FIELDS);
Expand Down Expand Up @@ -409,7 +409,7 @@ CGOptions::has_conflict(void)
conflict_msg_ = "max-funcs must be at least 1";
return true;
}
if (CGOptions::max_stmt_depth() < 1) {
if (CGOptions::max_blk_depth() < 1) {
conflict_msg_ = "max-stmt-depth must be at least 1";
return true;
}
Expand Down
12 changes: 6 additions & 6 deletions src/CGOptions.h
Expand Up @@ -47,13 +47,13 @@ using namespace std;
#define CGOPTIONS_DEFAULT_FUNC1_MAX_PARAMS (3)
#define CGOPTIONS_DEFAULT_COVERAGE_TEST_SIZE (500)
#define CGOPTIONS_DEFAULT_MAX_BLOCK_SIZE (4)
#define CGOPTIONS_DEFAULT_MAX_STMT_DEPTH (5)
#define CGOPTIONS_DEFAULT_MAX_EXPR_DEPTH (5)
#define CGOPTIONS_DEFAULT_MAX_BLOCK_DEPTH (5)
#define CGOPTIONS_DEFAULT_MAX_EXPR_DEPTH (10)
#define CGOPTIONS_DEFAULT_MAX_STRUCT_FIELDS (10)
#define CGOPTIONS_DEFAULT_MAX_UNION_FIELDS (5)
#define CGOPTIONS_DEFAULT_MAX_NESTED_STRUCT_LEVEL (0)
#define CGOPTIONS_DEFAULT_MAX_INDIRECT_LEVEL (2)
#define CGOPTIONS_DEFAULT_MAX_ARRAY_DIMENSIONS (3)
#define CGOPTIONS_DEFAULT_MAX_ARRAY_DIMENSIONS (3)
#define CGOPTIONS_DEFAULT_MAX_ARRAY_LENGTH_PER_DIMENSION (10)
#define CGOPTIONS_DEFAULT_MAX_ARRAY_LENGTH (256)
#define CGOPTIONS_DEFAULT_MAX_ARRAY_NUM_IN_LOOP (4)
Expand Down Expand Up @@ -93,8 +93,8 @@ class CGOptions {
static int max_block_size(void);
static int max_block_size(int p);

static int max_stmt_depth(void);
static int max_stmt_depth(int p);
static int max_blk_depth(void);
static int max_blk_depth(int p);

static int max_expr_depth(void);
static int max_expr_depth(int p);
Expand Down Expand Up @@ -408,7 +408,7 @@ class CGOptions {
static std::string output_file_;
static int max_params_;
static int max_block_size_;
static int max_stmt_depth_;
static int max_blk_depth_;
static int max_expr_depth_;
static bool wrap_volatiles_;
static bool allow_const_volatile_;
Expand Down
1 change: 1 addition & 0 deletions src/Constant.h
Expand Up @@ -80,6 +80,7 @@ class Constant : public Expression
virtual bool equals(int num) const;

virtual void get_referenced_ptrs(std::vector<const Variable*>& /*ptrs*/) const {};
virtual unsigned int get_complexity(void) const { return 1;}
// unsigned long SizeInBytes(void) const;
virtual void Output(std::ostream &) const;

Expand Down

0 comments on commit eab8976

Please sign in to comment.