Skip to content

Commit

Permalink
Added support for deferred init static vars
Browse files Browse the repository at this point in the history
  • Loading branch information
AjayBrahmakshatriya committed Jan 17, 2024
1 parent 40013df commit b305ce6
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 7 deletions.
1 change: 1 addition & 0 deletions include/builder/builder_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ class builder_context {
std::string current_label;

std::vector<tracking_tuple> static_var_tuples;
std::vector<tracking_tuple> deferred_static_var_tuples;

std::vector<var *> assume_variables;

Expand Down
7 changes: 0 additions & 7 deletions include/builder/dyn_var.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,6 @@ struct with_name {
with_name(const std::string &n, bool wd = false) : name(n), with_decl(wd) {}
};

// constructor helper to defer the initialization of dyn_var
// This allows declaring dyn_var outside the context, but initialize
// them later
struct defer_init {
// No members
};

template <typename T>
class dyn_var_impl : public var {
public:
Expand Down
7 changes: 7 additions & 0 deletions include/builder/forward_declarations.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ struct extract_signature;
template <typename T, typename... OtherArgs>
struct extract_signature_from_lambda;

// constructor helper to defer the initialization of dyn_var
// This allows declaring dyn_var outside the context, but initialize
// them later
struct defer_init {
// No members
};

// This class does nothing
// Apart from just being used in the copy constructor to
// tell the constructor to no create without context
Expand Down
20 changes: 20 additions & 0 deletions include/builder/static_var.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class static_var : static_var_base {
static_assert(sizeof(T) < MAX_TRACKING_VAR_SIZE, "Currently builder::static_var supports variables of max size "
"= " TOSTRING(MAX_TRACKING_VARIABLE_SIZE));
T val;
bool is_deferred = false;

mutable bool name_checked = false;
void try_get_name() const {
Expand Down Expand Up @@ -91,7 +92,26 @@ class static_var : static_var_base {
val = v;
try_get_name();
}

static_var(const defer_init &) {
// Just like dynamic variables, no registration happens here
is_deferred = true;
}
void deferred_init(void) {
assert(builder_context::current_builder_context != nullptr);
// Deferred static variables are kept separate because they are never untracked
// in the destructor
builder_context::current_builder_context->deferred_static_var_tuples.push_back(
tracking_tuple((unsigned char *)&val, sizeof(T), this));
try_get_name();
}

~static_var() {
if (is_deferred) {
// Must be a deferred init object
return;
}

assert(builder_context::current_builder_context != nullptr);
assert(builder_context::current_builder_context->static_var_tuples.size() > 0);
assert(builder_context::current_builder_context->static_var_tuples.back().ptr == (unsigned char *)&val);
Expand Down
30 changes: 30 additions & 0 deletions samples/outputs.var_names/sample53
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,35 @@ void foo (void) {
} else {
obj_0 = 2;
}
if ((obj_0 % 2) == 0) {
obj_0 = obj_0 + 0;
}
if ((obj_0 % 2) == 0) {
obj_0 = obj_0 + 1;
}
if ((obj_0 % 2) == 0) {
obj_0 = obj_0 + 2;
}
if ((obj_0 % 2) == 0) {
obj_0 = obj_0 + 3;
}
if ((obj_0 % 2) == 0) {
obj_0 = obj_0 + 4;
}
if ((obj_0 % 2) == 0) {
obj_0 = obj_0 + 5;
}
if ((obj_0 % 2) == 0) {
obj_0 = obj_0 + 6;
}
if ((obj_0 % 2) == 0) {
obj_0 = obj_0 + 7;
}
if ((obj_0 % 2) == 0) {
obj_0 = obj_0 + 8;
}
if ((obj_0 % 2) == 0) {
obj_0 = obj_0 + 9;
}
}

30 changes: 30 additions & 0 deletions samples/outputs/sample53
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,35 @@ void foo (void) {
} else {
var0 = 2;
}
if ((var0 % 2) == 0) {
var0 = var0 + 0;
}
if ((var0 % 2) == 0) {
var0 = var0 + 1;
}
if ((var0 % 2) == 0) {
var0 = var0 + 2;
}
if ((var0 % 2) == 0) {
var0 = var0 + 3;
}
if ((var0 % 2) == 0) {
var0 = var0 + 4;
}
if ((var0 % 2) == 0) {
var0 = var0 + 5;
}
if ((var0 % 2) == 0) {
var0 = var0 + 6;
}
if ((var0 % 2) == 0) {
var0 = var0 + 7;
}
if ((var0 % 2) == 0) {
var0 = var0 + 8;
}
if ((var0 % 2) == 0) {
var0 = var0 + 9;
}
}

6 changes: 6 additions & 0 deletions samples/sample53.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,24 @@ using builder::static_var;

struct external_object_t {
dyn_var<int> member = builder::defer_init();
static_var<int> counter = builder::defer_init();
};

static void foo(external_object_t &obj) {
// Init not
obj.member.deferred_init();
obj.counter.deferred_init();

dyn_var<int> x = 0;
if (x) {
obj.member = 1;
} else {
obj.member = 2;
}

for (obj.counter = 0; obj.counter < 10; obj.counter++)
if (obj.member % 2 == 0)
obj.member += obj.counter;
}

int main(int argc, char *argv[]) {
Expand Down
1 change: 1 addition & 0 deletions src/builder/builder_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,7 @@ block::stmt::Ptr builder_context::extract_ast_from_function_internal(std::vector
}
ret_ast = ast;
}
current_builder_context = nullptr;

// Update the memoized table with the stmt block we just created
for (unsigned int i = 0; i < current_block_stmt->stmts.size(); i++) {
Expand Down
14 changes: 14 additions & 0 deletions src/util/tracer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ tag get_offset_in_function_impl(builder::builder_context *current_builder_contex
}
// Now add snapshots of static vars
assert(current_builder_context != nullptr);

for (builder::tracking_tuple tuple : current_builder_context->deferred_static_var_tuples) {
new_tag.static_var_snapshots.push_back(tuple.snapshot());
if (builder::builder_context::current_builder_context->enable_d2x) {
new_tag.static_var_key_values.push_back({tuple.var_ref->var_name, tuple.var_ref->serialize()});
}
}
for (builder::tracking_tuple tuple : current_builder_context->static_var_tuples) {
new_tag.static_var_snapshots.push_back(tuple.snapshot());
if (builder::builder_context::current_builder_context->enable_d2x) {
Expand All @@ -59,6 +66,13 @@ tag get_offset_in_function_impl(builder::builder_context *current_builder_contex

// Now add snapshots of static vars
assert(current_builder_context != nullptr);

for (builder::tracking_tuple tuple : current_builder_context->deferred_static_var_tuples) {
new_tag.static_var_snapshots.push_back(tuple.snapshot());
if (builder::builder_context::current_builder_context->enable_d2x) {
new_tag.static_var_key_values.push_back({tuple.var_ref->var_name, tuple.var_ref->serialize()});
}
}
for (builder::tracking_tuple tuple : current_builder_context->static_var_tuples) {
new_tag.static_var_snapshots.push_back(tuple.snapshot());
if (builder::builder_context::current_builder_context->enable_d2x) {
Expand Down

0 comments on commit b305ce6

Please sign in to comment.