Skip to content

Commit

Permalink
[Wasm] Fix emitting setup code too late.
Browse files Browse the repository at this point in the history
Setup code should *always* be emitted before emitting of the own computation
code, e.g. the setup code must be emitted before compiling a data layout since
this compilation may temporarily introduce boolean variables and emit code
using them into the returned blocks, however, these blocks may be placed
between setup and teardown code which may represent the lifespan of another
boolean variable which then overrides the value of the other one (our DSL is
able to use the same bit again since the `Variable` object is already distroyed
and thus its bit is free to use).
We had this issue previously and thus introduced the `referenced_bits_` field
for `PrimitiveExpr`s, however, beeing able to return blocks of Wasm code and
place them somewhere else circumvents our former solution. Maybe we can extend
the idea of memorizing referenced bits to blocks as well until they are
attached or destroyed.
  • Loading branch information
lucagretscher committed Jun 13, 2023
1 parent 48e00d0 commit 9918e30
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
18 changes: 15 additions & 3 deletions src/backend/WasmOperator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -421,18 +421,22 @@ void Scan::execute(const Match<Scan> &M, setup_t setup, pipeline_t pipeline, tea
oss << table.name << "_mem";
Ptr<void> base_address = Module::Get().get_global<void*>(oss.str().c_str());

/*----- Emit setup code *before* compiling data layout to not overwrite its temporary boolean variables. -----*/
setup();

/*----- Compile data layout to generate sequential load from table. -----*/
auto [inits, loads, jumps] = compile_load_sequential(schema, base_address, table.layout(),
table.schema(M.scan.alias()), tuple_id);

/*----- Generate the loop for the actual scan, with the pipeline emitted into the loop body. -----*/
setup();
inits.attach_to_current();
WHILE (tuple_id < num_rows) {
loads.attach_to_current();
pipeline();
jumps.attach_to_current();
}

/*----- Emit teardown code. -----*/
teardown();
}

Expand Down Expand Up @@ -1867,6 +1871,9 @@ void OrderedGrouping::execute(const Match<OrderedGrouping> &M, setup_t setup, pi
auto S = CodeGenContext::Get().scoped_environment(); // create scoped environment for this function
auto &env = CodeGenContext::Get().env();

/*----- Emit setup code *before* possibly introducing temporary boolean variables to not overwrite them. -----*/
setup();

/*----- Add computed group tuple to current environment. ----*/
for (auto &e : M.grouping.schema().deduplicate()) {
try {
Expand Down Expand Up @@ -1913,8 +1920,9 @@ void OrderedGrouping::execute(const Match<OrderedGrouping> &M, setup_t setup, pi
}

/*----- Resume pipeline. -----*/
setup();
pipeline();

/*----- Emit teardown code. -----*/
teardown();
}
}
Expand Down Expand Up @@ -2427,6 +2435,9 @@ void Aggregation::execute(const Match<Aggregation> &M, setup_t setup, pipeline_t
}
aggregation_child_pipeline(); // call child function

/*----- Emit setup code *before* possibly introducing temporary boolean variables to not overwrite them. -----*/
setup();

/*----- Add computed aggregates tuple to current environment. ----*/
auto &env = CodeGenContext::Get().env();
for (auto &e : M.aggregation.schema().deduplicate()) {
Expand Down Expand Up @@ -2462,8 +2473,9 @@ void Aggregation::execute(const Match<Aggregation> &M, setup_t setup, pipeline_t
}

/*----- Resume pipeline. -----*/
setup();
pipeline();

/*----- Emit teardown code. -----*/
teardown();
}

Expand Down
12 changes: 10 additions & 2 deletions src/backend/WasmUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2136,19 +2136,23 @@ void Buffer<IsGlobal>::resume_pipeline(param_t tuple_schema_)
Ptr<void> base_address = PARAMETER(0);
U32 size = PARAMETER(1);

/*----- Emit setup code *before* compiling data layout to not overwrite its temporary boolean variables. -*/
setup_();

/*----- Compile data layout to generate sequential load from buffer. -----*/
Var<U32> load_tuple_id; // default initialized to 0
auto [load_inits, loads, load_jumps] =
compile_load_sequential(tuple_schema, base_address, layout_, schema_, load_tuple_id);

/*----- Generate loop for loading entire buffer, with the pipeline emitted into the loop body. -----*/
setup_();
load_inits.attach_to_current();
WHILE (load_tuple_id < size) {
loads.attach_to_current();
pipeline_();
load_jumps.attach_to_current();
}

/*----- Emit teardown code. -----*/
teardown_();
}
resume_pipeline_ = std::move(resume_pipeline);
Expand Down Expand Up @@ -2187,19 +2191,23 @@ void Buffer<IsGlobal>::resume_pipeline_inline(param_t tuple_schema_) const
pred = env.extract_predicate().is_true_and_not_null();
U32 num_tuples = pred ? Select(*pred, size, 0U) : size;

/*----- Emit setup code *before* compiling data layout to not overwrite its temporary boolean variables. -----*/
setup_();

/*----- Compile data layout to generate sequential load from buffer. -----*/
Var<U32> load_tuple_id(0); // explicitly (re-)set tuple ID to 0
auto [load_inits, loads, load_jumps] =
compile_load_sequential(tuple_schema, base_address, layout_, schema_, load_tuple_id);

/*----- Generate loop for loading entire buffer, with the pipeline emitted into the loop body. -----*/
setup_();
load_inits.attach_to_current();
WHILE (load_tuple_id < num_tuples) {
loads.attach_to_current();
pipeline_();
load_jumps.attach_to_current();
}

/*----- Emit teardown code. -----*/
teardown_();
}

Expand Down

0 comments on commit 9918e30

Please sign in to comment.