authorgravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2023-05-09 19:50:00+02:00
committergravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2023-05-19 20:19:00+02:00
log8be69f41328ebc0331434fd9d4008985463188c9
tree01b6f4ae95c57f236d59ad1777324580d54f9f09
parent99422cb5284f3e15c1b5a8598a6b1622c0e7b6ca
signaturelock-open Commit is signed but in an unrecognized format.

wasm: simplify merging of branches

Rather than adding all values that were generated in the child branch, we simply discard them as outer branches cannot refer to values produced from an inner branch.

1 files changed, 30 insertions(+), 42 deletions(-)

src/arch/wasm/CodeGen.zig+30-42
......@@ -3259,9 +3259,13 @@ fn airBlock(func: *CodeGen, inst: Air.Inst.Index) InnerError!void {
32593259 .label = func.block_depth,
32603260 .value = block_result,
32613261 });
3262
32623263 try func.genBody(body);
32633264 try func.endBlock();
32643265
3266 const liveness = func.liveness.getBlock(inst);
3267 try func.currentBranch().values.ensureUnusedCapacity(func.gpa, liveness.deaths.len);
3268
32653269 func.finishAir(inst, block_result, &.{});
32663270}
32673271
......@@ -3316,41 +3320,27 @@ fn airCondBr(func: *CodeGen, inst: Air.Inst.Index) InnerError!void {
33163320 try func.addLabel(.br_if, 0);
33173321
33183322 try func.branches.ensureUnusedCapacity(func.gpa, 2);
3319
3320 func.branches.appendAssumeCapacity(.{});
3321 try func.currentBranch().values.ensureUnusedCapacity(func.gpa, @intCast(u32, liveness_condbr.else_deaths.len));
3322 try func.genBody(else_body);
3323 try func.endBlock();
3324 var else_stack = func.branches.pop();
3325 defer else_stack.deinit(func.gpa);
3323 {
3324 func.branches.appendAssumeCapacity(.{});
3325 try func.currentBranch().values.ensureUnusedCapacity(func.gpa, @intCast(u32, liveness_condbr.else_deaths.len));
3326 try func.genBody(else_body);
3327 try func.endBlock();
3328 var else_stack = func.branches.pop();
3329 else_stack.deinit(func.gpa);
3330 }
33263331
33273332 // Outer block that matches the condition
3328 func.branches.appendAssumeCapacity(.{});
3329 try func.currentBranch().values.ensureUnusedCapacity(func.gpa, @intCast(u32, liveness_condbr.then_deaths.len));
3330 try func.genBody(then_body);
3331 var then_stack = func.branches.pop();
3332 defer then_stack.deinit(func.gpa);
3333
3334 try func.mergeBranch(&else_stack);
3335 try func.mergeBranch(&then_stack);
3333 {
3334 func.branches.appendAssumeCapacity(.{});
3335 try func.currentBranch().values.ensureUnusedCapacity(func.gpa, @intCast(u32, liveness_condbr.then_deaths.len));
3336 try func.genBody(then_body);
3337 var then_stack = func.branches.pop();
3338 then_stack.deinit(func.gpa);
3339 }
33363340
33373341 func.finishAir(inst, .none, &.{});
33383342}
33393343
3340fn mergeBranch(func: *CodeGen, branch: *const Branch) !void {
3341 const parent = func.currentBranch();
3342
3343 const target_slice = branch.values.entries.slice();
3344 const target_keys = target_slice.items(.key);
3345 const target_values = target_slice.items(.value);
3346
3347 try parent.values.ensureTotalCapacity(func.gpa, parent.values.capacity() + branch.values.count());
3348 for (target_keys, 0..) |key, index| {
3349 // TODO: process deaths from branches
3350 parent.values.putAssumeCapacity(key, target_values[index]);
3351 }
3352}
3353
33543344fn airCmp(func: *CodeGen, inst: Air.Inst.Index, op: std.math.CompareOperator) InnerError!void {
33553345 const bin_op = func.air.instructions.items(.data)[inst].bin_op;
33563346
......@@ -3860,30 +3850,21 @@ fn airSwitchBr(func: *CodeGen, inst: Air.Inst.Index) InnerError!void {
38603850 }
38613851 }
38623852 func.branches.appendAssumeCapacity(.{});
3863
38643853 try func.currentBranch().values.ensureUnusedCapacity(func.gpa, liveness.deaths[index].len);
3865 for (liveness.deaths[index]) |operand| {
3866 func.processDeath(Air.indexToRef(operand));
3867 }
38683854 try func.genBody(case.body);
38693855 try func.endBlock();
38703856 var case_branch = func.branches.pop();
3871 defer case_branch.deinit(func.gpa);
3872 try func.mergeBranch(&case_branch);
3857 case_branch.deinit(func.gpa);
38733858 }
38743859
38753860 if (has_else_body) {
38763861 func.branches.appendAssumeCapacity(.{});
38773862 const else_deaths = liveness.deaths.len - 1;
38783863 try func.currentBranch().values.ensureUnusedCapacity(func.gpa, liveness.deaths[else_deaths].len);
3879 for (liveness.deaths[else_deaths]) |operand| {
3880 func.processDeath(Air.indexToRef(operand));
3881 }
38823864 try func.genBody(else_body);
38833865 try func.endBlock();
38843866 var else_branch = func.branches.pop();
3885 defer else_branch.deinit(func.gpa);
3886 try func.mergeBranch(&else_branch);
3867 else_branch.deinit(func.gpa);
38873868 }
38883869 func.finishAir(inst, .none, &.{});
38893870}
......@@ -5992,7 +5973,7 @@ fn airTry(func: *CodeGen, inst: Air.Inst.Index) InnerError!void {
59925973 const extra = func.air.extraData(Air.Try, pl_op.payload);
59935974 const body = func.air.extra[extra.end..][0..extra.data.body_len];
59945975 const err_union_ty = func.air.typeOf(pl_op.operand);
5995 const result = try lowerTry(func, err_union, body, err_union_ty, false);
5976 const result = try lowerTry(func, inst, err_union, body, err_union_ty, false);
59965977 func.finishAir(inst, result, &.{pl_op.operand});
59975978}
59985979
......@@ -6002,12 +5983,13 @@ fn airTryPtr(func: *CodeGen, inst: Air.Inst.Index) InnerError!void {
60025983 const err_union_ptr = try func.resolveInst(extra.data.ptr);
60035984 const body = func.air.extra[extra.end..][0..extra.data.body_len];
60045985 const err_union_ty = func.air.typeOf(extra.data.ptr).childType();
6005 const result = try lowerTry(func, err_union_ptr, body, err_union_ty, true);
5986 const result = try lowerTry(func, inst, err_union_ptr, body, err_union_ty, true);
60065987 func.finishAir(inst, result, &.{extra.data.ptr});
60075988}
60085989
60095990fn lowerTry(
60105991 func: *CodeGen,
5992 inst: Air.Inst.Index,
60115993 err_union: WValue,
60125994 body: []const Air.Inst.Index,
60135995 err_union_ty: Type,
......@@ -6035,8 +6017,14 @@ fn lowerTry(
60356017 }
60366018 try func.addTag(.i32_eqz);
60376019 try func.addLabel(.br_if, 0); // jump out of block when error is '0'
6020
6021 const liveness = func.liveness.getCondBr(inst);
6022 try func.branches.append(func.gpa, .{});
6023 try func.currentBranch().values.ensureUnusedCapacity(func.gpa, liveness.else_deaths.len + liveness.then_deaths.len);
60386024 try func.genBody(body);
60396025 try func.endBlock();
6026 var branch = func.branches.pop();
6027 branch.deinit(func.gpa);
60406028 }
60416029
60426030 // if we reach here it means error was not set, and we want the payload