Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion include/builder/dyn_var.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ class dyn_var_impl : public var {
block::var::Ptr dyn_var = std::make_shared<block::var>();
dyn_var->var_type = create_block_type();
tracer::tag offset = tracer::get_offset_in_function();
// push this tag into the live_dyn_vars set
get_run_state()->insert_live_dyn_var(offset);
dyn_var->preferred_name = utils::find_variable_name_cached(this, offset);
block_var = dyn_var;
dyn_var->static_offset = offset;
Expand Down Expand Up @@ -325,7 +327,11 @@ class dyn_var_impl : public var {
from_builder_vector(a);
}

virtual ~dyn_var_impl() = default;
virtual ~dyn_var_impl() {
if (block_var && is_under_run()) {
get_run_state()->remove_live_dyn_var(block_var->static_offset);
}
}

// Assume that _impl objects will never be created
// Thus addr can always cast the address to dyn_var<T>
Expand Down Expand Up @@ -410,6 +416,9 @@ class dyn_var : public dyn_var_impl<T>, public dyn_var_parent_selector<T, void>,
// constructors so define them here
dyn_var(const dyn_var<T> &t) : dyn_var_impl<T>((builder)t) {}

// Since we are also defining a destructor, we should define a move constructor
dyn_var(dyn_var<T> &&t): dyn_var_impl<T>((builder)t) {}

// Unfortunately because we are changing the return type,
// the implicitly defined copy assignment will always
// shadow the version the parent defines
Expand Down
33 changes: 33 additions & 0 deletions include/builder/live_dyn_var.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#ifndef BUILDER_LIVE_DYN_VAR_H
#define BUILDER_LIVE_DYN_VAR_H

#include "util/tracer.h"
#include <unordered_set>

namespace builder {

class live_tag_set {
std::unordered_set<tracer::tag> live_set;
size_t computed_hash = 0;
public:
// Default empty set
live_tag_set() {}

live_tag_set(std::unordered_set<tracer::tag> &live_set): live_set(live_set) {
// Now update the hash
for (auto s: live_set) {
computed_hash = tracer::hash_combine(computed_hash, s.hash());
}
}

bool operator==(const live_tag_set& other) {
if (this == &other) return true;
if (computed_hash != other.computed_hash) return false;
return live_set == other.live_set;
}
};


}

#endif
10 changes: 10 additions & 0 deletions include/builder/run_states.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <set>
#include <functional>
#include "blocks/stmt.h"
#include "builder/tag_factory.h"

namespace builder {

Expand Down Expand Up @@ -61,6 +62,9 @@ class run_state {
// Annotations to be attached to the next statement
std::set<std::string> current_annotations;

// Set of dyn_variables that are live
std::vector<tracer::tag_id> live_dyn_vars;

/* Tracing and re-execution related members */

// Vector of bools to return on branching
Expand Down Expand Up @@ -103,6 +107,9 @@ class run_state {
current_annotations.clear();
return to_ret;
}

void insert_live_dyn_var(const tracer::tag& new_tag);
void remove_live_dyn_var(const tracer::tag& new_tag);

friend class execution_state;
friend class invocation_state;
Expand Down Expand Up @@ -143,6 +150,9 @@ class invocation_state {
/* ND_VAR state */
std::unordered_map<tracer::tag, std::shared_ptr<nd_var_base>> nd_state_map;

// Tag factory state
tag_factory tag_factory_instance;

// Main invocation function
std::function<void(void)> invocation_function;

Expand Down
24 changes: 24 additions & 0 deletions include/builder/tag_factory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef BUILDER_TAG_FACTORY_H
#define BUILDER_TAG_FACTORY_H

#include "util/tracer.h"

namespace builder {

class tag_factory {
std::unordered_map<tracer::tag, tracer::tag_id> internal_map;
tracer::tag_id next_id = 1;
public:
tracer::tag_id create_tag_id (const tracer::tag& t) {
auto it = internal_map.find(t);
if (it != internal_map.end())
return it->second;
internal_map[t] = next_id++;
return next_id - 1;
}
};

}


#endif
12 changes: 12 additions & 0 deletions include/util/tracer.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@ class builder_context;

namespace tracer {

using tag_id = size_t;

class tag {
public:
std::vector<unsigned long long> pointers;
std::vector<std::shared_ptr<builder::static_var_snapshot_base>> static_var_snapshots;
std::vector<std::pair<std::string, std::string>> static_var_key_values;
std::vector<tag_id> live_dyn_vars;


std::string cached_string;
mutable size_t cached_hash = 0;
Expand All @@ -33,6 +37,7 @@ class tag {
void clear(void) {
pointers.clear();
static_var_snapshots.clear();
live_dyn_vars.clear();
}

// A function to create another tag
Expand Down Expand Up @@ -82,6 +87,13 @@ class tag {
for (unsigned i = 0; i < static_var_snapshots.size(); i++) {
h = hash_combine(h, static_var_snapshots[i]->computed_hash);
}

// Finally combine the hash of the live_dyn_vars

for (unsigned i = 0; i < live_dyn_vars.size(); i++) {
h = hash_combine(h, (size_t) live_dyn_vars[i]);
}

cached_hash = h;
return cached_hash;
}
Expand Down
54 changes: 21 additions & 33 deletions samples/outputs.var_names/sample23
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
STMT_BLOCK
DECL_STMT
SCALAR_TYPE (INT)
VAR (t_2)
NO_INITIALIZATION
DECL_STMT
SCALAR_TYPE (INT)
VAR (x_0)
Expand All @@ -8,49 +12,35 @@ STMT_BLOCK
VAR (var1)
VAR_EXPR
VAR (x_0)
DECL_STMT
SCALAR_TYPE (INT)
VAR (var2)
NO_INITIALIZATION
IF_STMT
GT_EXPR
VAR_EXPR
VAR (var1)
INT_CONST (10)
STMT_BLOCK
DECL_STMT
SCALAR_TYPE (INT)
VAR (t_3)
INT_CONST (9)
EXPR_STMT
ASSIGN_EXPR
VAR_EXPR
VAR (var2)
VAR_EXPR
VAR (t_3)
VAR (t_2)
INT_CONST (9)
STMT_BLOCK
DECL_STMT
SCALAR_TYPE (INT)
VAR (t_4)
INT_CONST (0)
EXPR_STMT
ASSIGN_EXPR
VAR_EXPR
VAR (var2)
VAR_EXPR
VAR (t_4)
VAR (t_2)
INT_CONST (0)
IF_STMT
VAR_EXPR
VAR (var2)
VAR (t_2)
STMT_BLOCK
DECL_STMT
SCALAR_TYPE (INT)
VAR (k_5)
VAR (k_3)
INT_CONST (0)
EXPR_STMT
ASSIGN_EXPR
VAR_EXPR
VAR (k_5)
VAR (k_3)
INT_CONST (0)
STMT_BLOCK
IF_STMT
Expand All @@ -59,31 +49,29 @@ STMT_BLOCK
STMT_BLOCK
DECL_STMT
SCALAR_TYPE (INT)
VAR (k_6)
VAR (k_4)
INT_CONST (0)
EXPR_STMT
ASSIGN_EXPR
VAR_EXPR
VAR (k_6)
VAR (k_4)
INT_CONST (0)
STMT_BLOCK
{
int t_2;
int x_0;
int var1 = x_0;
int var2;
if (var1 > 10) {
int t_3 = 9;
var2 = t_3;
t_2 = 9;
} else {
int t_4 = 0;
var2 = t_4;
t_2 = 0;
}
if (var2) {
int k_5 = 0;
k_5 = 0;
if (t_2) {
int k_3 = 0;
k_3 = 0;
}
if (x_0) {
int k_6 = 0;
k_6 = 0;
int k_4 = 0;
k_4 = 0;
}
}
20 changes: 11 additions & 9 deletions samples/outputs.var_names/sample54
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
void bar (void) {
int z_3;
int* k_4;
int y_0 = 0;
int m_1;
int n_2;
if (y_0) {
z_3 = 1;
k_4 = (&(m_1));
int z_3 = 1;
int& k_4 = m_1;
int b_5;
int a_6 = z_3;
z_3 = z_3 + k_4;
} else {
z_3 = 2;
k_4 = (&(m_1));
int z_7 = 2;
int& k_8 = m_1;
int b_9;
int a_10 = z_7;
z_7 = z_7 + k_8;
}
int b_5;
int a_6 = z_3;
z_3 = z_3 + k_4[0];
m_1 = m_1 + 3;
}

Loading