Skip to content

Commit 9918e30

Browse files
committed
[Wasm] Fix emitting setup code too late.
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.
1 parent 48e00d0 commit 9918e30

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

src/backend/WasmOperator.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -421,18 +421,22 @@ void Scan::execute(const Match<Scan> &M, setup_t setup, pipeline_t pipeline, tea
421421
oss << table.name << "_mem";
422422
Ptr<void> base_address = Module::Get().get_global<void*>(oss.str().c_str());
423423

424+
/*----- Emit setup code *before* compiling data layout to not overwrite its temporary boolean variables. -----*/
425+
setup();
426+
424427
/*----- Compile data layout to generate sequential load from table. -----*/
425428
auto [inits, loads, jumps] = compile_load_sequential(schema, base_address, table.layout(),
426429
table.schema(M.scan.alias()), tuple_id);
427430

428431
/*----- Generate the loop for the actual scan, with the pipeline emitted into the loop body. -----*/
429-
setup();
430432
inits.attach_to_current();
431433
WHILE (tuple_id < num_rows) {
432434
loads.attach_to_current();
433435
pipeline();
434436
jumps.attach_to_current();
435437
}
438+
439+
/*----- Emit teardown code. -----*/
436440
teardown();
437441
}
438442

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

1874+
/*----- Emit setup code *before* possibly introducing temporary boolean variables to not overwrite them. -----*/
1875+
setup();
1876+
18701877
/*----- Add computed group tuple to current environment. ----*/
18711878
for (auto &e : M.grouping.schema().deduplicate()) {
18721879
try {
@@ -1913,8 +1920,9 @@ void OrderedGrouping::execute(const Match<OrderedGrouping> &M, setup_t setup, pi
19131920
}
19141921

19151922
/*----- Resume pipeline. -----*/
1916-
setup();
19171923
pipeline();
1924+
1925+
/*----- Emit teardown code. -----*/
19181926
teardown();
19191927
}
19201928
}
@@ -2427,6 +2435,9 @@ void Aggregation::execute(const Match<Aggregation> &M, setup_t setup, pipeline_t
24272435
}
24282436
aggregation_child_pipeline(); // call child function
24292437

2438+
/*----- Emit setup code *before* possibly introducing temporary boolean variables to not overwrite them. -----*/
2439+
setup();
2440+
24302441
/*----- Add computed aggregates tuple to current environment. ----*/
24312442
auto &env = CodeGenContext::Get().env();
24322443
for (auto &e : M.aggregation.schema().deduplicate()) {
@@ -2462,8 +2473,9 @@ void Aggregation::execute(const Match<Aggregation> &M, setup_t setup, pipeline_t
24622473
}
24632474

24642475
/*----- Resume pipeline. -----*/
2465-
setup();
24662476
pipeline();
2477+
2478+
/*----- Emit teardown code. -----*/
24672479
teardown();
24682480
}
24692481

src/backend/WasmUtil.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2136,19 +2136,23 @@ void Buffer<IsGlobal>::resume_pipeline(param_t tuple_schema_)
21362136
Ptr<void> base_address = PARAMETER(0);
21372137
U32 size = PARAMETER(1);
21382138

2139+
/*----- Emit setup code *before* compiling data layout to not overwrite its temporary boolean variables. -*/
2140+
setup_();
2141+
21392142
/*----- Compile data layout to generate sequential load from buffer. -----*/
21402143
Var<U32> load_tuple_id; // default initialized to 0
21412144
auto [load_inits, loads, load_jumps] =
21422145
compile_load_sequential(tuple_schema, base_address, layout_, schema_, load_tuple_id);
21432146

21442147
/*----- Generate loop for loading entire buffer, with the pipeline emitted into the loop body. -----*/
2145-
setup_();
21462148
load_inits.attach_to_current();
21472149
WHILE (load_tuple_id < size) {
21482150
loads.attach_to_current();
21492151
pipeline_();
21502152
load_jumps.attach_to_current();
21512153
}
2154+
2155+
/*----- Emit teardown code. -----*/
21522156
teardown_();
21532157
}
21542158
resume_pipeline_ = std::move(resume_pipeline);
@@ -2187,19 +2191,23 @@ void Buffer<IsGlobal>::resume_pipeline_inline(param_t tuple_schema_) const
21872191
pred = env.extract_predicate().is_true_and_not_null();
21882192
U32 num_tuples = pred ? Select(*pred, size, 0U) : size;
21892193

2194+
/*----- Emit setup code *before* compiling data layout to not overwrite its temporary boolean variables. -----*/
2195+
setup_();
2196+
21902197
/*----- Compile data layout to generate sequential load from buffer. -----*/
21912198
Var<U32> load_tuple_id(0); // explicitly (re-)set tuple ID to 0
21922199
auto [load_inits, loads, load_jumps] =
21932200
compile_load_sequential(tuple_schema, base_address, layout_, schema_, load_tuple_id);
21942201

21952202
/*----- Generate loop for loading entire buffer, with the pipeline emitted into the loop body. -----*/
2196-
setup_();
21972203
load_inits.attach_to_current();
21982204
WHILE (load_tuple_id < num_tuples) {
21992205
loads.attach_to_current();
22002206
pipeline_();
22012207
load_jumps.attach_to_current();
22022208
}
2209+
2210+
/*----- Emit teardown code. -----*/
22032211
teardown_();
22042212
}
22052213

0 commit comments

Comments
 (0)