authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-04-07 11:12:44-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2023-04-07 11:12:44-04:00
log48f98494fda57c5eca7b4ac899046c1dd285e471
treefd96476ec302a30c51f24ac91fc5ea5e2b6637d9
parent086639630800fc52bd727163e85d174ec1ac1103
parenta7f674d6c1ba3d72a324aa918929c5beb36a8306
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #15195 from mlugg/fix/liveness-loop-defer-deaths

Liveness: defer deaths of externally-scoped instructions in loop bodies

11 files changed, 806 insertions(+), 139 deletions(-)

src/Liveness.zig+619-1
......@@ -25,6 +25,7 @@ tomb_bits: []usize,
2525/// array. The meaning of the data depends on the AIR tag.
2626/// * `cond_br` - points to a `CondBr` in `extra` at this index.
2727/// * `switch_br` - points to a `SwitchBr` in `extra` at this index.
28/// * `loop` - points to a `Loop` in `extra` at this index.
2829/// * `asm`, `call`, `aggregate_init` - the value is a set of bits which are the extra tomb
2930/// bits of operands.
3031/// The main tomb bits are still used and the extra ones are starting with the lsb of the
......@@ -51,6 +52,11 @@ pub const SwitchBr = struct {
5152 else_death_count: u32,
5253};
5354
55/// Trailing is the set of instructions whose lifetimes end at the end of the loop body.
56pub const Loop = struct {
57 death_count: u32,
58};
59
5460pub fn analyze(gpa: Allocator, air: Air) Allocator.Error!Liveness {
5561 const tracy = trace(@src());
5662 defer tracy.end();
......@@ -76,6 +82,11 @@ pub fn analyze(gpa: Allocator, air: Air) Allocator.Error!Liveness {
7682 const main_body = air.getMainBody();
7783 try a.table.ensureTotalCapacity(gpa, @intCast(u32, main_body.len));
7884 try analyzeWithContext(&a, null, main_body);
85 {
86 var to_remove: std.AutoHashMapUnmanaged(Air.Inst.Index, void) = .{};
87 defer to_remove.deinit(gpa);
88 try removeDeaths(&a, &to_remove, main_body);
89 }
7990 return Liveness{
8091 .tomb_bits = a.tomb_bits,
8192 .special = a.special,
......@@ -650,6 +661,18 @@ pub fn getSwitchBr(l: Liveness, gpa: Allocator, inst: Air.Inst.Index, cases_len:
650661 };
651662}
652663
664pub const LoopSlice = struct {
665 deaths: []const Air.Inst.Index,
666};
667
668pub fn getLoop(l: Liveness, inst: Air.Inst.Index) LoopSlice {
669 const index: usize = l.special.get(inst) orelse return .{
670 .deaths = &.{},
671 };
672 const death_count = l.extra[index];
673 return .{ .deaths = l.extra[index + 1 ..][0..death_count] };
674}
675
653676pub fn deinit(l: *Liveness, gpa: Allocator) void {
654677 gpa.free(l.tomb_bits);
655678 gpa.free(l.extra);
......@@ -1138,7 +1161,39 @@ fn analyzeInst(
11381161 .loop => {
11391162 const extra = a.air.extraData(Air.Block, inst_datas[inst].ty_pl.payload);
11401163 const body = a.air.extra[extra.end..][0..extra.data.body_len];
1141 try analyzeWithContext(a, new_set, body);
1164
1165 var body_table: std.AutoHashMapUnmanaged(Air.Inst.Index, void) = .{};
1166 defer body_table.deinit(gpa);
1167
1168 // Instructions outside the loop body cannot die within the loop, since further loop
1169 // iterations may occur. Track deaths from the loop body - we'll remove all of these
1170 // retroactively, and add them to our extra data.
1171
1172 try analyzeWithContext(a, &body_table, body);
1173
1174 if (new_set) |ns| {
1175 try ns.ensureUnusedCapacity(gpa, body_table.count());
1176 var it = body_table.keyIterator();
1177 while (it.next()) |key| {
1178 _ = ns.putAssumeCapacity(key.*, {});
1179 }
1180 }
1181
1182 try a.extra.ensureUnusedCapacity(gpa, std.meta.fields(Loop).len + body_table.count());
1183 const extra_index = a.addExtraAssumeCapacity(Loop{
1184 .death_count = body_table.count(),
1185 });
1186 {
1187 var it = body_table.keyIterator();
1188 while (it.next()) |key| {
1189 a.extra.appendAssumeCapacity(key.*);
1190 }
1191 }
1192 try a.special.put(gpa, inst, extra_index);
1193
1194 // We'll remove invalid deaths in a separate pass after main liveness analysis. See
1195 // removeDeaths for more details.
1196
11421197 return; // Loop has no operands and it is always unreferenced.
11431198 },
11441199 .@"try" => {
......@@ -1412,3 +1467,566 @@ const ExtraTombs = struct {
14121467 et.big_tomb_bits_extra.deinit(et.analysis.gpa);
14131468 }
14141469};
1470
1471/// Remove any deaths invalidated by the deaths from an enclosing `loop`. Reshuffling deaths stored
1472/// in `extra` causes it to become non-dense, but that's fine - we won't remove too much data.
1473/// Making it dense would be a lot more work - it'd require recomputing every index in `special`.
1474fn removeDeaths(
1475 a: *Analysis,
1476 to_remove: *std.AutoHashMapUnmanaged(Air.Inst.Index, void),
1477 body: []const Air.Inst.Index,
1478) error{OutOfMemory}!void {
1479 for (body) |inst| {
1480 try removeInstDeaths(a, to_remove, inst);
1481 }
1482}
1483
1484fn removeInstDeaths(
1485 a: *Analysis,
1486 to_remove: *std.AutoHashMapUnmanaged(Air.Inst.Index, void),
1487 inst: Air.Inst.Index,
1488) !void {
1489 const inst_tags = a.air.instructions.items(.tag);
1490 const inst_datas = a.air.instructions.items(.data);
1491
1492 switch (inst_tags[inst]) {
1493 .add,
1494 .add_optimized,
1495 .addwrap,
1496 .addwrap_optimized,
1497 .add_sat,
1498 .sub,
1499 .sub_optimized,
1500 .subwrap,
1501 .subwrap_optimized,
1502 .sub_sat,
1503 .mul,
1504 .mul_optimized,
1505 .mulwrap,
1506 .mulwrap_optimized,
1507 .mul_sat,
1508 .div_float,
1509 .div_float_optimized,
1510 .div_trunc,
1511 .div_trunc_optimized,
1512 .div_floor,
1513 .div_floor_optimized,
1514 .div_exact,
1515 .div_exact_optimized,
1516 .rem,
1517 .rem_optimized,
1518 .mod,
1519 .mod_optimized,
1520 .bit_and,
1521 .bit_or,
1522 .xor,
1523 .cmp_lt,
1524 .cmp_lt_optimized,
1525 .cmp_lte,
1526 .cmp_lte_optimized,
1527 .cmp_eq,
1528 .cmp_eq_optimized,
1529 .cmp_gte,
1530 .cmp_gte_optimized,
1531 .cmp_gt,
1532 .cmp_gt_optimized,
1533 .cmp_neq,
1534 .cmp_neq_optimized,
1535 .bool_and,
1536 .bool_or,
1537 .store,
1538 .array_elem_val,
1539 .slice_elem_val,
1540 .ptr_elem_val,
1541 .shl,
1542 .shl_exact,
1543 .shl_sat,
1544 .shr,
1545 .shr_exact,
1546 .atomic_store_unordered,
1547 .atomic_store_monotonic,
1548 .atomic_store_release,
1549 .atomic_store_seq_cst,
1550 .set_union_tag,
1551 .min,
1552 .max,
1553 => {
1554 const o = inst_datas[inst].bin_op;
1555 removeOperandDeaths(a, to_remove, inst, .{ o.lhs, o.rhs, .none });
1556 },
1557
1558 .vector_store_elem => {
1559 const o = inst_datas[inst].vector_store_elem;
1560 const extra = a.air.extraData(Air.Bin, o.payload).data;
1561 removeOperandDeaths(a, to_remove, inst, .{ o.vector_ptr, extra.lhs, extra.rhs });
1562 },
1563
1564 .arg,
1565 .alloc,
1566 .ret_ptr,
1567 .constant,
1568 .const_ty,
1569 .trap,
1570 .breakpoint,
1571 .dbg_stmt,
1572 .dbg_inline_begin,
1573 .dbg_inline_end,
1574 .dbg_block_begin,
1575 .dbg_block_end,
1576 .unreach,
1577 .fence,
1578 .ret_addr,
1579 .frame_addr,
1580 .wasm_memory_size,
1581 .err_return_trace,
1582 .save_err_return_trace_index,
1583 .c_va_start,
1584 .work_item_id,
1585 .work_group_size,
1586 .work_group_id,
1587 => {},
1588
1589 .not,
1590 .bitcast,
1591 .load,
1592 .fpext,
1593 .fptrunc,
1594 .intcast,
1595 .trunc,
1596 .optional_payload,
1597 .optional_payload_ptr,
1598 .optional_payload_ptr_set,
1599 .errunion_payload_ptr_set,
1600 .wrap_optional,
1601 .unwrap_errunion_payload,
1602 .unwrap_errunion_err,
1603 .unwrap_errunion_payload_ptr,
1604 .unwrap_errunion_err_ptr,
1605 .wrap_errunion_payload,
1606 .wrap_errunion_err,
1607 .slice_ptr,
1608 .slice_len,
1609 .ptr_slice_len_ptr,
1610 .ptr_slice_ptr_ptr,
1611 .struct_field_ptr_index_0,
1612 .struct_field_ptr_index_1,
1613 .struct_field_ptr_index_2,
1614 .struct_field_ptr_index_3,
1615 .array_to_slice,
1616 .float_to_int,
1617 .float_to_int_optimized,
1618 .int_to_float,
1619 .get_union_tag,
1620 .clz,
1621 .ctz,
1622 .popcount,
1623 .byte_swap,
1624 .bit_reverse,
1625 .splat,
1626 .error_set_has_value,
1627 .addrspace_cast,
1628 .c_va_arg,
1629 .c_va_copy,
1630 => {
1631 const o = inst_datas[inst].ty_op;
1632 removeOperandDeaths(a, to_remove, inst, .{ o.operand, .none, .none });
1633 },
1634
1635 .is_null,
1636 .is_non_null,
1637 .is_null_ptr,
1638 .is_non_null_ptr,
1639 .is_err,
1640 .is_non_err,
1641 .is_err_ptr,
1642 .is_non_err_ptr,
1643 .ptrtoint,
1644 .bool_to_int,
1645 .ret,
1646 .ret_load,
1647 .is_named_enum_value,
1648 .tag_name,
1649 .error_name,
1650 .sqrt,
1651 .sin,
1652 .cos,
1653 .tan,
1654 .exp,
1655 .exp2,
1656 .log,
1657 .log2,
1658 .log10,
1659 .fabs,
1660 .floor,
1661 .ceil,
1662 .round,
1663 .trunc_float,
1664 .neg,
1665 .neg_optimized,
1666 .cmp_lt_errors_len,
1667 .set_err_return_trace,
1668 .c_va_end,
1669 => {
1670 const operand = inst_datas[inst].un_op;
1671 removeOperandDeaths(a, to_remove, inst, .{ operand, .none, .none });
1672 },
1673
1674 .add_with_overflow,
1675 .sub_with_overflow,
1676 .mul_with_overflow,
1677 .shl_with_overflow,
1678 .ptr_add,
1679 .ptr_sub,
1680 .ptr_elem_ptr,
1681 .slice_elem_ptr,
1682 .slice,
1683 => {
1684 const ty_pl = inst_datas[inst].ty_pl;
1685 const extra = a.air.extraData(Air.Bin, ty_pl.payload).data;
1686 removeOperandDeaths(a, to_remove, inst, .{ extra.lhs, extra.rhs, .none });
1687 },
1688
1689 .dbg_var_ptr,
1690 .dbg_var_val,
1691 => {
1692 const operand = inst_datas[inst].pl_op.operand;
1693 removeOperandDeaths(a, to_remove, inst, .{ operand, .none, .none });
1694 },
1695
1696 .prefetch => {
1697 const prefetch = inst_datas[inst].prefetch;
1698 removeOperandDeaths(a, to_remove, inst, .{ prefetch.ptr, .none, .none });
1699 },
1700
1701 .call, .call_always_tail, .call_never_tail, .call_never_inline => {
1702 const inst_data = inst_datas[inst].pl_op;
1703 const callee = inst_data.operand;
1704 const extra = a.air.extraData(Air.Call, inst_data.payload);
1705 const args = @ptrCast([]const Air.Inst.Ref, a.air.extra[extra.end..][0..extra.data.args_len]);
1706
1707 var death_remover = BigTombDeathRemover.init(a, to_remove, inst);
1708 death_remover.feed(callee);
1709 for (args) |operand| {
1710 death_remover.feed(operand);
1711 }
1712 death_remover.finish();
1713 },
1714 .select => {
1715 const pl_op = inst_datas[inst].pl_op;
1716 const extra = a.air.extraData(Air.Bin, pl_op.payload).data;
1717 removeOperandDeaths(a, to_remove, inst, .{ pl_op.operand, extra.lhs, extra.rhs });
1718 },
1719 .shuffle => {
1720 const extra = a.air.extraData(Air.Shuffle, inst_datas[inst].ty_pl.payload).data;
1721 removeOperandDeaths(a, to_remove, inst, .{ extra.a, extra.b, .none });
1722 },
1723 .reduce, .reduce_optimized => {
1724 const reduce = inst_datas[inst].reduce;
1725 removeOperandDeaths(a, to_remove, inst, .{ reduce.operand, .none, .none });
1726 },
1727 .cmp_vector, .cmp_vector_optimized => {
1728 const extra = a.air.extraData(Air.VectorCmp, inst_datas[inst].ty_pl.payload).data;
1729 removeOperandDeaths(a, to_remove, inst, .{ extra.lhs, extra.rhs, .none });
1730 },
1731 .aggregate_init => {
1732 const ty_pl = inst_datas[inst].ty_pl;
1733 const aggregate_ty = a.air.getRefType(ty_pl.ty);
1734 const len = @intCast(usize, aggregate_ty.arrayLen());
1735 const elements = @ptrCast([]const Air.Inst.Ref, a.air.extra[ty_pl.payload..][0..len]);
1736
1737 var death_remover = BigTombDeathRemover.init(a, to_remove, inst);
1738 for (elements) |elem| {
1739 death_remover.feed(elem);
1740 }
1741 death_remover.finish();
1742 },
1743 .union_init => {
1744 const extra = a.air.extraData(Air.UnionInit, inst_datas[inst].ty_pl.payload).data;
1745 removeOperandDeaths(a, to_remove, inst, .{ extra.init, .none, .none });
1746 },
1747 .struct_field_ptr, .struct_field_val => {
1748 const extra = a.air.extraData(Air.StructField, inst_datas[inst].ty_pl.payload).data;
1749 removeOperandDeaths(a, to_remove, inst, .{ extra.struct_operand, .none, .none });
1750 },
1751 .field_parent_ptr => {
1752 const extra = a.air.extraData(Air.FieldParentPtr, inst_datas[inst].ty_pl.payload).data;
1753 removeOperandDeaths(a, to_remove, inst, .{ extra.field_ptr, .none, .none });
1754 },
1755 .cmpxchg_strong, .cmpxchg_weak => {
1756 const extra = a.air.extraData(Air.Cmpxchg, inst_datas[inst].ty_pl.payload).data;
1757 removeOperandDeaths(a, to_remove, inst, .{ extra.ptr, extra.expected_value, extra.new_value });
1758 },
1759 .mul_add => {
1760 const pl_op = inst_datas[inst].pl_op;
1761 const extra = a.air.extraData(Air.Bin, pl_op.payload).data;
1762 removeOperandDeaths(a, to_remove, inst, .{ extra.lhs, extra.rhs, pl_op.operand });
1763 },
1764 .atomic_load => {
1765 const ptr = inst_datas[inst].atomic_load.ptr;
1766 removeOperandDeaths(a, to_remove, inst, .{ ptr, .none, .none });
1767 },
1768 .atomic_rmw => {
1769 const pl_op = inst_datas[inst].pl_op;
1770 const extra = a.air.extraData(Air.AtomicRmw, pl_op.payload).data;
1771 removeOperandDeaths(a, to_remove, inst, .{ pl_op.operand, extra.operand, .none });
1772 },
1773 .memset,
1774 .memcpy,
1775 => {
1776 const pl_op = inst_datas[inst].pl_op;
1777 const extra = a.air.extraData(Air.Bin, pl_op.payload).data;
1778 removeOperandDeaths(a, to_remove, inst, .{ pl_op.operand, extra.lhs, extra.rhs });
1779 },
1780
1781 .br => {
1782 const br = inst_datas[inst].br;
1783 removeOperandDeaths(a, to_remove, inst, .{ br.operand, .none, .none });
1784 },
1785 .assembly => {
1786 const extra = a.air.extraData(Air.Asm, inst_datas[inst].ty_pl.payload);
1787 var extra_i: usize = extra.end;
1788 const outputs = @ptrCast([]const Air.Inst.Ref, a.air.extra[extra_i..][0..extra.data.outputs_len]);
1789 extra_i += outputs.len;
1790 const inputs = @ptrCast([]const Air.Inst.Ref, a.air.extra[extra_i..][0..extra.data.inputs_len]);
1791 extra_i += inputs.len;
1792
1793 var death_remover = BigTombDeathRemover.init(a, to_remove, inst);
1794 for (outputs) |output| {
1795 if (output != .none) {
1796 death_remover.feed(output);
1797 }
1798 }
1799 for (inputs) |input| {
1800 death_remover.feed(input);
1801 }
1802 death_remover.finish();
1803 },
1804 .block => {
1805 const extra = a.air.extraData(Air.Block, inst_datas[inst].ty_pl.payload);
1806 const body = a.air.extra[extra.end..][0..extra.data.body_len];
1807 try removeDeaths(a, to_remove, body);
1808 },
1809 .loop => {
1810 const extra = a.air.extraData(Air.Block, inst_datas[inst].ty_pl.payload);
1811 const body = a.air.extra[extra.end..][0..extra.data.body_len];
1812
1813 const liveness_extra_idx = a.special.get(inst) orelse {
1814 try removeDeaths(a, to_remove, body);
1815 return;
1816 };
1817
1818 const death_count = a.extra.items[liveness_extra_idx];
1819 var deaths = a.extra.items[liveness_extra_idx + 1 ..][0..death_count];
1820
1821 // Remove any deaths in `to_remove` from this loop's deaths
1822 deaths.len = removeExtraDeaths(to_remove, deaths);
1823 a.extra.items[liveness_extra_idx] = @intCast(u32, deaths.len);
1824
1825 // Temporarily add any deaths of ours to `to_remove`
1826 try to_remove.ensureUnusedCapacity(a.gpa, @intCast(u32, deaths.len));
1827 for (deaths) |d| {
1828 to_remove.putAssumeCapacity(d, {});
1829 }
1830 try removeDeaths(a, to_remove, body);
1831 for (deaths) |d| {
1832 _ = to_remove.remove(d);
1833 }
1834 },
1835 .@"try" => {
1836 const pl_op = inst_datas[inst].pl_op;
1837 const extra = a.air.extraData(Air.Try, pl_op.payload);
1838 const body = a.air.extra[extra.end..][0..extra.data.body_len];
1839 try removeDeaths(a, to_remove, body);
1840 removeOperandDeaths(a, to_remove, inst, .{ pl_op.operand, .none, .none });
1841 },
1842 .try_ptr => {
1843 const extra = a.air.extraData(Air.TryPtr, inst_datas[inst].ty_pl.payload);
1844 const body = a.air.extra[extra.end..][0..extra.data.body_len];
1845 try removeDeaths(a, to_remove, body);
1846 removeOperandDeaths(a, to_remove, inst, .{ extra.data.ptr, .none, .none });
1847 },
1848 .cond_br => {
1849 const inst_data = inst_datas[inst].pl_op;
1850 const condition = inst_data.operand;
1851 const extra = a.air.extraData(Air.CondBr, inst_data.payload);
1852 const then_body = a.air.extra[extra.end..][0..extra.data.then_body_len];
1853 const else_body = a.air.extra[extra.end + then_body.len ..][0..extra.data.else_body_len];
1854
1855 if (a.special.get(inst)) |liveness_extra_idx| {
1856 const then_death_count = a.extra.items[liveness_extra_idx + 0];
1857 const else_death_count = a.extra.items[liveness_extra_idx + 1];
1858 var then_deaths = a.extra.items[liveness_extra_idx + 2 ..][0..then_death_count];
1859 var else_deaths = a.extra.items[liveness_extra_idx + 2 + then_death_count ..][0..else_death_count];
1860
1861 const new_then_death_count = removeExtraDeaths(to_remove, then_deaths);
1862 const new_else_death_count = removeExtraDeaths(to_remove, else_deaths);
1863
1864 a.extra.items[liveness_extra_idx + 0] = new_then_death_count;
1865 a.extra.items[liveness_extra_idx + 1] = new_else_death_count;
1866
1867 if (new_then_death_count < then_death_count) {
1868 // `else` deaths need to be moved earlier in `extra`
1869 const src = a.extra.items[liveness_extra_idx + 2 + then_death_count ..];
1870 const dest = a.extra.items[liveness_extra_idx + 2 + new_then_death_count ..];
1871 std.mem.copy(u32, dest, src[0..new_else_death_count]);
1872 }
1873 }
1874
1875 try removeDeaths(a, to_remove, then_body);
1876 try removeDeaths(a, to_remove, else_body);
1877
1878 removeOperandDeaths(a, to_remove, inst, .{ condition, .none, .none });
1879 },
1880 .switch_br => {
1881 const pl_op = inst_datas[inst].pl_op;
1882 const condition = pl_op.operand;
1883 const switch_br = a.air.extraData(Air.SwitchBr, pl_op.payload);
1884
1885 var air_extra_index: usize = switch_br.end;
1886 for (0..switch_br.data.cases_len) |_| {
1887 const case = a.air.extraData(Air.SwitchBr.Case, air_extra_index);
1888 const case_body = a.air.extra[case.end + case.data.items_len ..][0..case.data.body_len];
1889 air_extra_index = case.end + case.data.items_len + case_body.len;
1890 try removeDeaths(a, to_remove, case_body);
1891 }
1892 { // else
1893 const else_body = a.air.extra[air_extra_index..][0..switch_br.data.else_body_len];
1894 try removeDeaths(a, to_remove, else_body);
1895 }
1896
1897 if (a.special.get(inst)) |liveness_extra_idx| {
1898 const else_death_count = a.extra.items[liveness_extra_idx];
1899 var read_idx = liveness_extra_idx + 1;
1900 var write_idx = read_idx; // write_idx <= read_idx always
1901 for (0..switch_br.data.cases_len) |_| {
1902 const case_death_count = a.extra.items[read_idx];
1903 const case_deaths = a.extra.items[read_idx + 1 ..][0..case_death_count];
1904 const new_death_count = removeExtraDeaths(to_remove, case_deaths);
1905 a.extra.items[write_idx] = new_death_count;
1906 if (write_idx < read_idx) {
1907 std.mem.copy(u32, a.extra.items[write_idx + 1 ..], a.extra.items[read_idx + 1 ..][0..new_death_count]);
1908 }
1909 read_idx += 1 + case_death_count;
1910 write_idx += 1 + new_death_count;
1911 }
1912 const else_deaths = a.extra.items[read_idx..][0..else_death_count];
1913 const new_else_death_count = removeExtraDeaths(to_remove, else_deaths);
1914 a.extra.items[liveness_extra_idx] = new_else_death_count;
1915 if (write_idx < read_idx) {
1916 std.mem.copy(u32, a.extra.items[write_idx..], a.extra.items[read_idx..][0..new_else_death_count]);
1917 }
1918 }
1919
1920 removeOperandDeaths(a, to_remove, inst, .{ condition, .none, .none });
1921 },
1922 .wasm_memory_grow => {
1923 const pl_op = inst_datas[inst].pl_op;
1924 removeOperandDeaths(a, to_remove, inst, .{ pl_op.operand, .none, .none });
1925 },
1926 }
1927}
1928
1929fn removeOperandDeaths(
1930 a: *Analysis,
1931 to_remove: *const std.AutoHashMapUnmanaged(Air.Inst.Index, void),
1932 inst: Air.Inst.Index,
1933 operands: [bpi - 1]Air.Inst.Ref,
1934) void {
1935 const usize_index = (inst * bpi) / @bitSizeOf(usize);
1936
1937 const cur_tomb = @truncate(Bpi, a.tomb_bits[usize_index] >>
1938 @intCast(Log2Int(usize), (inst % (@bitSizeOf(usize) / bpi)) * bpi));
1939
1940 var toggle_bits: Bpi = 0;
1941
1942 for (operands, 0..) |op_ref, i| {
1943 const mask = @as(Bpi, 1) << @intCast(OperandInt, i);
1944 const op_int = @enumToInt(op_ref);
1945 if (op_int < Air.Inst.Ref.typed_value_map.len) continue;
1946 const operand: Air.Inst.Index = op_int - @intCast(u32, Air.Inst.Ref.typed_value_map.len);
1947 if ((cur_tomb & mask) != 0 and to_remove.contains(operand)) {
1948 log.debug("remove death of %{} in %{}", .{ operand, inst });
1949 toggle_bits ^= mask;
1950 }
1951 }
1952
1953 a.tomb_bits[usize_index] ^= @as(usize, toggle_bits) <<
1954 @intCast(Log2Int(usize), (inst % (@bitSizeOf(usize) / bpi)) * bpi);
1955}
1956
1957fn removeExtraDeaths(
1958 to_remove: *const std.AutoHashMapUnmanaged(Air.Inst.Index, void),
1959 deaths: []Air.Inst.Index,
1960) u32 {
1961 var new_len = @intCast(u32, deaths.len);
1962 var i: usize = 0;
1963 while (i < new_len) {
1964 if (to_remove.contains(deaths[i])) {
1965 log.debug("remove extra death of %{}", .{deaths[i]});
1966 deaths[i] = deaths[new_len - 1];
1967 new_len -= 1;
1968 } else {
1969 i += 1;
1970 }
1971 }
1972 return new_len;
1973}
1974
1975const BigTombDeathRemover = struct {
1976 a: *Analysis,
1977 to_remove: *const std.AutoHashMapUnmanaged(Air.Inst.Index, void),
1978 inst: Air.Inst.Index,
1979
1980 operands: [bpi - 1]Air.Inst.Ref = .{.none} ** (bpi - 1),
1981 next_oper: OperandInt = 0,
1982
1983 bit_index: u32 = 0,
1984 // Initialized once we finish the small tomb operands: see `feed`
1985 extra_start: u32 = undefined,
1986 extra_offset: u32 = 0,
1987
1988 fn init(a: *Analysis, to_remove: *const std.AutoHashMapUnmanaged(Air.Inst.Index, void), inst: Air.Inst.Index) BigTombDeathRemover {
1989 return .{
1990 .a = a,
1991 .to_remove = to_remove,
1992 .inst = inst,
1993 };
1994 }
1995
1996 fn feed(dr: *BigTombDeathRemover, operand: Air.Inst.Ref) void {
1997 if (dr.next_oper < bpi - 1) {
1998 dr.operands[dr.next_oper] = operand;
1999 dr.next_oper += 1;
2000 if (dr.next_oper == bpi - 1) {
2001 removeOperandDeaths(dr.a, dr.to_remove, dr.inst, dr.operands);
2002 if (dr.a.special.get(dr.inst)) |idx| dr.extra_start = idx;
2003 }
2004 return;
2005 }
2006
2007 defer dr.bit_index += 1;
2008
2009 const op_int = @enumToInt(operand);
2010 if (op_int < Air.Inst.Ref.typed_value_map.len) return;
2011
2012 const op_inst: Air.Inst.Index = op_int - @intCast(u32, Air.Inst.Ref.typed_value_map.len);
2013
2014 while (dr.bit_index - dr.extra_offset * 31 >= 31) {
2015 dr.extra_offset += 1;
2016 }
2017 const dies = @truncate(u1, dr.a.extra.items[dr.extra_start + dr.extra_offset] >>
2018 @intCast(u5, dr.bit_index - dr.extra_offset * 31)) != 0;
2019
2020 if (dies and dr.to_remove.contains(op_inst)) {
2021 log.debug("remove big death of %{}", .{op_inst});
2022 dr.a.extra.items[dr.extra_start + dr.extra_offset] ^=
2023 (@as(u32, 1) << @intCast(u5, dr.bit_index - dr.extra_offset * 31));
2024 }
2025 }
2026
2027 fn finish(dr: *BigTombDeathRemover) void {
2028 if (dr.next_oper < bpi) {
2029 removeOperandDeaths(dr.a, dr.to_remove, dr.inst, dr.operands);
2030 }
2031 }
2032};
src/arch/aarch64/CodeGen.zig+8
......@@ -5000,9 +5000,17 @@ fn airLoop(self: *Self, inst: Air.Inst.Index) !void {
50005000 const ty_pl = self.air.instructions.items(.data)[inst].ty_pl;
50015001 const loop = self.air.extraData(Air.Block, ty_pl.payload);
50025002 const body = self.air.extra[loop.end..][0..loop.data.body_len];
5003 const liveness_loop = self.liveness.getLoop(inst);
50035004 const start_index = @intCast(u32, self.mir_instructions.len);
5005
50045006 try self.genBody(body);
50055007 try self.jump(start_index);
5008
5009 try self.ensureProcessDeathCapacity(liveness_loop.deaths.len);
5010 for (liveness_loop.deaths) |operand| {
5011 self.processDeath(operand);
5012 }
5013
50065014 return self.finishAirBookkeeping();
50075015}
50085016
src/arch/arm/CodeGen.zig+8
......@@ -4923,9 +4923,17 @@ fn airLoop(self: *Self, inst: Air.Inst.Index) !void {
49234923 const ty_pl = self.air.instructions.items(.data)[inst].ty_pl;
49244924 const loop = self.air.extraData(Air.Block, ty_pl.payload);
49254925 const body = self.air.extra[loop.end..][0..loop.data.body_len];
4926 const liveness_loop = self.liveness.getLoop(inst);
49264927 const start_index = @intCast(Mir.Inst.Index, self.mir_instructions.len);
4928
49274929 try self.genBody(body);
49284930 try self.jump(start_index);
4931
4932 try self.ensureProcessDeathCapacity(liveness_loop.deaths.len);
4933 for (liveness_loop.deaths) |operand| {
4934 self.processDeath(operand);
4935 }
4936
49294937 return self.finishAirBookkeeping();
49304938}
49314939
src/arch/sparc64/CodeGen.zig+8
......@@ -1750,9 +1750,17 @@ fn airLoop(self: *Self, inst: Air.Inst.Index) !void {
17501750 const ty_pl = self.air.instructions.items(.data)[inst].ty_pl;
17511751 const loop = self.air.extraData(Air.Block, ty_pl.payload);
17521752 const body = self.air.extra[loop.end .. loop.end + loop.data.body_len];
1753 const liveness_loop = self.liveness.getLoop(inst);
17531754 const start = @intCast(u32, self.mir_instructions.len);
1755
17541756 try self.genBody(body);
17551757 try self.jump(start);
1758
1759 try self.ensureProcessDeathCapacity(liveness_loop.deaths.len);
1760 for (liveness_loop.deaths) |operand| {
1761 self.processDeath(operand);
1762 }
1763
17561764 return self.finishAirBookkeeping();
17571765}
17581766
src/arch/wasm/CodeGen.zig+6
......@@ -3042,6 +3042,7 @@ fn airLoop(func: *CodeGen, inst: Air.Inst.Index) InnerError!void {
30423042 const ty_pl = func.air.instructions.items(.data)[inst].ty_pl;
30433043 const loop = func.air.extraData(Air.Block, ty_pl.payload);
30443044 const body = func.air.extra[loop.end..][0..loop.data.body_len];
3045 const liveness_loop = func.liveness.getLoop(inst);
30453046
30463047 // result type of loop is always 'noreturn', meaning we can always
30473048 // emit the wasm type 'block_empty'.
......@@ -3052,6 +3053,11 @@ fn airLoop(func: *CodeGen, inst: Air.Inst.Index) InnerError!void {
30523053 try func.addLabel(.br, 0);
30533054 try func.endBlock();
30543055
3056 try func.currentBranch().values.ensureUnusedCapacity(func.gpa, @intCast(u32, liveness_loop.deaths.len));
3057 for (liveness_loop.deaths) |death| {
3058 func.processDeath(Air.indexToRef(death));
3059 }
3060
30553061 func.finishAir(inst, .none, &.{});
30563062}
30573063
src/arch/x86_64/CodeGen.zig+7
......@@ -6185,6 +6185,7 @@ fn airLoop(self: *Self, inst: Air.Inst.Index) !void {
61856185 const loop = self.air.extraData(Air.Block, ty_pl.payload);
61866186 const body = self.air.extra[loop.end..][0..loop.data.body_len];
61876187 const jmp_target = @intCast(u32, self.mir_instructions.len);
6188 const liveness_loop = self.liveness.getLoop(inst);
61886189
61896190 {
61906191 try self.branch_stack.append(.{});
......@@ -6208,6 +6209,12 @@ fn airLoop(self: *Self, inst: Air.Inst.Index) !void {
62086209 try self.canonicaliseBranches(true, &dummy_branch, &branch, true, false);
62096210
62106211 _ = try self.asmJmpReloc(jmp_target);
6212
6213 try self.ensureProcessDeathCapacity(liveness_loop.deaths.len);
6214 for (liveness_loop.deaths) |operand| {
6215 self.processDeath(operand);
6216 }
6217
62116218 return self.finishAirBookkeeping();
62126219}
62136220
src/codegen/c.zig+122-131
......@@ -55,7 +55,7 @@ const BlockData = struct {
5555 result: CValue,
5656};
5757
58pub const CValueMap = std.AutoHashMap(Air.Inst.Ref, CValue);
58pub const CValueMap = std.AutoHashMap(Air.Inst.Index, CValue);
5959
6060pub const LazyFnKey = union(enum) {
6161 tag_name: Decl.Index,
......@@ -77,9 +77,8 @@ pub const LazyFnMap = std.AutoArrayHashMapUnmanaged(LazyFnKey, LazyFnValue);
7777const LoopDepth = u16;
7878const Local = struct {
7979 cty_idx: CType.Index,
80 /// How many loops the last definition was nested in.
81 loop_depth: LoopDepth,
8280 alignas: CType.AlignAs,
81 is_in_clone: bool,
8382
8483 pub fn getType(local: Local) LocalType {
8584 return .{ .cty_idx = local.cty_idx, .alignas = local.alignas };
......@@ -90,7 +89,6 @@ const LocalIndex = u16;
9089const LocalType = struct { cty_idx: CType.Index, alignas: CType.AlignAs };
9190const LocalsList = std.AutoArrayHashMapUnmanaged(LocalIndex, void);
9291const LocalsMap = std.AutoArrayHashMapUnmanaged(LocalType, LocalsList);
93const LocalsStack = std.ArrayListUnmanaged(LocalsMap);
9492
9593const ValueRenderLocation = enum {
9694 FunctionArgument,
......@@ -279,41 +277,43 @@ pub const Function = struct {
279277 /// Which locals are available for reuse, based on Type.
280278 /// Only locals in the last stack entry are available for reuse,
281279 /// other entries will become available on loop exit.
282 free_locals_stack: LocalsStack = .{},
283 free_locals_clone_depth: LoopDepth = 0,
280 free_locals_map: LocalsMap = .{},
281 is_in_clone: bool = false,
284282 /// Locals which will not be freed by Liveness. This is used after a
285 /// Function body is lowered in order to make `free_locals_stack` have
283 /// Function body is lowered in order to make `free_locals_map` have
286284 /// 100% of the locals within so that it can be used to render the block
287285 /// of variable declarations at the top of a function, sorted descending
288286 /// by type alignment.
289287 /// The value is whether the alloc is static or not.
290288 allocs: std.AutoArrayHashMapUnmanaged(LocalIndex, bool) = .{},
291 /// Needed for memory used by the keys of free_locals_stack entries.
289 /// Needed for memory used by the keys of free_locals_map entries.
292290 arena: std.heap.ArenaAllocator,
293291
294 fn resolveInst(f: *Function, inst: Air.Inst.Ref) !CValue {
295 const gop = try f.value_map.getOrPut(inst);
296 if (gop.found_existing) return gop.value_ptr.*;
297
298 const val = f.air.value(inst).?;
299 const ty = f.air.typeOf(inst);
300
301 const result: CValue = if (lowersToArray(ty, f.object.dg.module.getTarget())) result: {
302 const writer = f.object.code_header.writer();
303 const alignment = 0;
304 const decl_c_value = try f.allocLocalValue(ty, alignment);
305 const gpa = f.object.dg.gpa;
306 try f.allocs.put(gpa, decl_c_value.new_local, true);
307 try writer.writeAll("static ");
308 try f.object.dg.renderTypeAndName(writer, ty, decl_c_value, Const, alignment, .complete);
309 try writer.writeAll(" = ");
310 try f.object.dg.renderValue(writer, ty, val, .StaticInitializer);
311 try writer.writeAll(";\n ");
312 break :result decl_c_value;
313 } else .{ .constant = inst };
292 fn resolveInst(f: *Function, ref: Air.Inst.Ref) !CValue {
293 if (Air.refToIndex(ref)) |inst| {
294 const gop = try f.value_map.getOrPut(inst);
295 if (gop.found_existing) return gop.value_ptr.*;
314296
315 gop.value_ptr.* = result;
316 return result;
297 const val = f.air.value(ref).?;
298 const ty = f.air.typeOf(ref);
299
300 const result: CValue = if (lowersToArray(ty, f.object.dg.module.getTarget())) result: {
301 const writer = f.object.code_header.writer();
302 const alignment = 0;
303 const decl_c_value = try f.allocLocalValue(ty, alignment);
304 const gpa = f.object.dg.gpa;
305 try f.allocs.put(gpa, decl_c_value.new_local, true);
306 try writer.writeAll("static ");
307 try f.object.dg.renderTypeAndName(writer, ty, decl_c_value, Const, alignment, .complete);
308 try writer.writeAll(" = ");
309 try f.object.dg.renderValue(writer, ty, val, .StaticInitializer);
310 try writer.writeAll(";\n ");
311 break :result decl_c_value;
312 } else .{ .constant = ref };
313
314 gop.value_ptr.* = result;
315 return result;
316 } else return .{ .constant = ref };
317317 }
318318
319319 fn wantSafety(f: *Function) bool {
......@@ -323,18 +323,14 @@ pub const Function = struct {
323323 };
324324 }
325325
326 fn getFreeLocals(f: *Function) *LocalsMap {
327 return &f.free_locals_stack.items[f.free_locals_stack.items.len - 1];
328 }
329
330326 /// Skips the reuse logic.
331327 fn allocLocalValue(f: *Function, ty: Type, alignment: u32) !CValue {
332328 const gpa = f.object.dg.gpa;
333329 const target = f.object.dg.module.getTarget();
334330 try f.locals.append(gpa, .{
335331 .cty_idx = try f.typeToIndex(ty, .complete),
336 .loop_depth = @intCast(LoopDepth, f.free_locals_stack.items.len - 1),
337332 .alignas = CType.AlignAs.init(alignment, ty.abiAlignment(target)),
333 .is_in_clone = f.is_in_clone,
338334 });
339335 return .{ .new_local = @intCast(LocalIndex, f.locals.items.len - 1) };
340336 }
......@@ -348,13 +344,11 @@ pub const Function = struct {
348344 /// Only allocates the local; does not print anything.
349345 fn allocAlignedLocal(f: *Function, ty: Type, _: CQualifiers, alignment: u32) !CValue {
350346 const target = f.object.dg.module.getTarget();
351 if (f.getFreeLocals().getPtr(.{
347 if (f.free_locals_map.getPtr(.{
352348 .cty_idx = try f.typeToIndex(ty, .complete),
353349 .alignas = CType.AlignAs.init(alignment, ty.abiAlignment(target)),
354350 })) |locals_list| {
355351 if (locals_list.popOrNull()) |local_entry| {
356 const local = &f.locals.items[local_entry.key];
357 local.loop_depth = @intCast(LoopDepth, f.free_locals_stack.items.len - 1);
358352 return .{ .new_local = local_entry.key };
359353 }
360354 }
......@@ -485,10 +479,7 @@ pub const Function = struct {
485479 const gpa = f.object.dg.gpa;
486480 f.allocs.deinit(gpa);
487481 f.locals.deinit(gpa);
488 for (f.free_locals_stack.items) |*free_locals| {
489 deinitFreeLocalsMap(gpa, free_locals);
490 }
491 f.free_locals_stack.deinit(gpa);
482 deinitFreeLocalsMap(gpa, &f.free_locals_map);
492483 f.blocks.deinit(gpa);
493484 f.value_map.deinit();
494485 f.lazy_fns.deinit(gpa);
......@@ -2592,8 +2583,7 @@ pub fn genFunc(f: *Function) !void {
25922583 o.code_header.appendSliceAssumeCapacity("{\n ");
25932584 const empty_header_len = o.code_header.items.len;
25942585
2595 f.free_locals_stack.clearRetainingCapacity();
2596 try f.free_locals_stack.append(gpa, .{});
2586 f.free_locals_map.clearRetainingCapacity();
25972587
25982588 const main_body = f.air.getMainBody();
25992589 try genBody(f, main_body);
......@@ -2605,7 +2595,8 @@ pub fn genFunc(f: *Function) !void {
26052595 // Liveness analysis, however, locals from alloc instructions will be
26062596 // missing. These are added now to complete the map. Then we can sort by
26072597 // alignment, descending.
2608 const free_locals = f.getFreeLocals();
2598 const free_locals = &f.free_locals_map;
2599 assert(f.value_map.count() == 0); // there must not be any unfreed locals
26092600 for (f.allocs.keys(), f.allocs.values()) |local_index, value| {
26102601 if (value) continue; // static
26112602 const local = f.locals.items[local_index];
......@@ -3007,7 +2998,7 @@ fn genBodyInner(f: *Function, body: []const Air.Inst.Index) error{ AnalysisFail,
30072998 if (result_value == .new_local) {
30082999 log.debug("map %{d} to t{d}", .{ inst, result_value.new_local });
30093000 }
3010 try f.value_map.putNoClobber(Air.indexToRef(inst), switch (result_value) {
3001 try f.value_map.putNoClobber(inst, switch (result_value) {
30113002 .none => continue,
30123003 .new_local => |i| .{ .local = i },
30133004 else => result_value,
......@@ -3093,17 +3084,21 @@ fn airPtrElemPtr(f: *Function, inst: Air.Inst.Index) !CValue {
30933084 const child_ty = ptr_ty.childType();
30943085
30953086 const ptr = try f.resolveInst(bin_op.lhs);
3096 if (!child_ty.hasRuntimeBitsIgnoreComptime()) {
3097 if (f.liveness.operandDies(inst, 1)) try die(f, inst, bin_op.rhs);
3098 return ptr;
3099 }
31003087 const index = try f.resolveInst(bin_op.rhs);
31013088 try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs });
31023089
31033090 const writer = f.object.writer();
31043091 const local = try f.allocLocal(inst, f.air.typeOfIndex(inst));
31053092 try f.writeCValue(writer, local, .Other);
3106 try writer.writeAll(" = (");
3093 try writer.writeAll(" = ");
3094
3095 if (!child_ty.hasRuntimeBitsIgnoreComptime()) {
3096 try f.writeCValue(writer, ptr, .Initializer);
3097 try writer.writeAll(";\n");
3098 return local;
3099 }
3100
3101 try writer.writeByte('(');
31073102 try f.renderType(writer, inst_ty);
31083103 try writer.writeAll(")&(");
31093104 if (ptr_ty.ptrSize() == .One) {
......@@ -3229,12 +3224,11 @@ fn airArrayElemVal(f: *Function, inst: Air.Inst.Index) !CValue {
32293224}
32303225
32313226fn airAlloc(f: *Function, inst: Air.Inst.Index) !CValue {
3232 const inst_ty = f.air.typeOfIndex(inst);
3227 if (f.liveness.isUnused(inst)) return .none;
32333228
3229 const inst_ty = f.air.typeOfIndex(inst);
32343230 const elem_type = inst_ty.elemType();
3235 if (!elem_type.isFnOrHasRuntimeBitsIgnoreComptime()) {
3236 return .{ .undef = inst_ty };
3237 }
3231 if (!elem_type.isFnOrHasRuntimeBitsIgnoreComptime()) return .{ .undef = inst_ty };
32383232
32393233 const target = f.object.dg.module.getTarget();
32403234 const local = try f.allocAlignedLocal(
......@@ -3249,12 +3243,11 @@ fn airAlloc(f: *Function, inst: Air.Inst.Index) !CValue {
32493243}
32503244
32513245fn airRetPtr(f: *Function, inst: Air.Inst.Index) !CValue {
3252 const inst_ty = f.air.typeOfIndex(inst);
3246 if (f.liveness.isUnused(inst)) return .none;
32533247
3248 const inst_ty = f.air.typeOfIndex(inst);
32543249 const elem_ty = inst_ty.elemType();
3255 if (!elem_ty.isFnOrHasRuntimeBitsIgnoreComptime()) {
3256 return .{ .undef = inst_ty };
3257 }
3250 if (!elem_ty.isFnOrHasRuntimeBitsIgnoreComptime()) return .{ .undef = inst_ty };
32583251
32593252 const target = f.object.dg.module.getTarget();
32603253 const local = try f.allocAlignedLocal(
......@@ -3274,10 +3267,22 @@ fn airArg(f: *Function, inst: Air.Inst.Index) !CValue {
32743267
32753268 const i = f.next_arg_index;
32763269 f.next_arg_index += 1;
3277 return if (inst_cty != try f.typeToIndex(inst_ty, .complete))
3270 const result: CValue = if (inst_cty != try f.typeToIndex(inst_ty, .complete))
32783271 .{ .arg_array = i }
32793272 else
32803273 .{ .arg = i };
3274
3275 if (f.liveness.isUnused(inst)) {
3276 const writer = f.object.writer();
3277 try writer.writeByte('(');
3278 try f.renderType(writer, Type.void);
3279 try writer.writeByte(')');
3280 try f.writeCValue(writer, result, .Other);
3281 try writer.writeAll(";\n");
3282 return .none;
3283 }
3284
3285 return result;
32813286}
32823287
32833288fn airLoad(f: *Function, inst: Air.Inst.Index) !CValue {
......@@ -4191,21 +4196,23 @@ fn airCall(
41914196 var lowered_ret_buf: LowerFnRetTyBuffer = undefined;
41924197 const lowered_ret_ty = lowerFnRetTy(ret_ty, &lowered_ret_buf, target);
41934198
4194 const result_local = if (modifier == .always_tail) r: {
4195 try writer.writeAll("zig_always_tail return ");
4196 break :r .none;
4197 } else if (!lowered_ret_ty.hasRuntimeBitsIgnoreComptime())
4198 .none
4199 else if (f.liveness.isUnused(inst)) r: {
4200 try writer.writeByte('(');
4201 try f.renderType(writer, Type.void);
4202 try writer.writeByte(')');
4203 break :r .none;
4204 } else r: {
4205 const local = try f.allocLocal(inst, try lowered_ret_ty.copy(f.arena.allocator()));
4206 try f.writeCValue(writer, local, .Other);
4207 try writer.writeAll(" = ");
4208 break :r local;
4199 const result_local = result: {
4200 if (modifier == .always_tail) {
4201 try writer.writeAll("zig_always_tail return ");
4202 break :result .none;
4203 } else if (!lowered_ret_ty.hasRuntimeBitsIgnoreComptime()) {
4204 break :result .none;
4205 } else if (f.liveness.isUnused(inst)) {
4206 try writer.writeByte('(');
4207 try f.renderType(writer, Type.void);
4208 try writer.writeByte(')');
4209 break :result .none;
4210 } else {
4211 const local = try f.allocLocal(inst, try lowered_ret_ty.copy(f.arena.allocator()));
4212 try f.writeCValue(writer, local, .Other);
4213 try writer.writeAll(" = ");
4214 break :result local;
4215 }
42094216 };
42104217
42114218 callee: {
......@@ -4250,9 +4257,9 @@ fn airCall(
42504257 }
42514258 try writer.writeAll(");\n");
42524259
4253 const result = r: {
4260 const result = result: {
42544261 if (result_local == .none or !lowersToArray(ret_ty, target))
4255 break :r result_local;
4262 break :result result_local;
42564263
42574264 const array_local = try f.allocLocal(inst, ret_ty);
42584265 try writer.writeAll("memcpy(");
......@@ -4263,7 +4270,7 @@ fn airCall(
42634270 try f.renderType(writer, ret_ty);
42644271 try writer.writeAll("));\n");
42654272 try freeLocal(f, inst, result_local.new_local, 0);
4266 break :r array_local;
4273 break :result array_local;
42674274 };
42684275
42694276 return result;
......@@ -4480,7 +4487,7 @@ fn airBitcast(f: *Function, inst: Air.Inst.Index) !CValue {
44804487 {
44814488 try f.writeCValue(writer, local, .Other);
44824489 try writer.writeAll(" = ");
4483 try f.writeCValue(writer, operand, .Other);
4490 try f.writeCValue(writer, operand, .Initializer);
44844491 try writer.writeAll(";\n");
44854492 return local;
44864493 }
......@@ -4630,30 +4637,16 @@ fn airLoop(f: *Function, inst: Air.Inst.Index) !CValue {
46304637 const ty_pl = f.air.instructions.items(.data)[inst].ty_pl;
46314638 const loop = f.air.extraData(Air.Block, ty_pl.payload);
46324639 const body = f.air.extra[loop.end..][0..loop.data.body_len];
4640 const liveness_loop = f.liveness.getLoop(inst);
46334641 const writer = f.object.writer();
46344642
4635 const gpa = f.object.dg.gpa;
4636 try f.free_locals_stack.insert(gpa, f.free_locals_stack.items.len - 1, .{});
4637
46384643 try writer.writeAll("for (;;) ");
46394644 try genBody(f, body);
46404645 try writer.writeByte('\n');
46414646
4642 var old_free_locals = f.free_locals_stack.pop();
4643 defer deinitFreeLocalsMap(gpa, &old_free_locals);
4644 const new_free_locals = f.getFreeLocals();
4645 var it = new_free_locals.iterator();
4646 while (it.next()) |entry| {
4647 const gop = try old_free_locals.getOrPut(gpa, entry.key_ptr.*);
4648 if (gop.found_existing) {
4649 try gop.value_ptr.ensureUnusedCapacity(gpa, entry.value_ptr.count());
4650 for (entry.value_ptr.keys()) |local_index| {
4651 gop.value_ptr.putAssumeCapacityNoClobber(local_index, {});
4652 }
4653 } else gop.value_ptr.* = entry.value_ptr.move();
4647 for (liveness_loop.deaths) |operand| {
4648 try die(f, inst, Air.indexToRef(operand));
46544649 }
4655 deinitFreeLocalsMap(gpa, new_free_locals);
4656 new_free_locals.* = old_free_locals.move();
46574650
46584651 return .none;
46594652}
......@@ -4673,7 +4666,7 @@ fn airCondBr(f: *Function, inst: Air.Inst.Index) !CValue {
46734666 const gpa = f.object.dg.gpa;
46744667 var cloned_map = try f.value_map.clone();
46754668 defer cloned_map.deinit();
4676 var cloned_frees = try cloneFreeLocalsMap(gpa, f.getFreeLocals());
4669 var cloned_frees = try cloneFreeLocalsMap(gpa, &f.free_locals_map);
46774670 defer deinitFreeLocalsMap(gpa, &cloned_frees);
46784671
46794672 // Remember how many locals there were before entering the then branch so
......@@ -4684,8 +4677,8 @@ fn airCondBr(f: *Function, inst: Air.Inst.Index) !CValue {
46844677 // that we can notice and make sure not to use them in the else branch.
46854678 // Any new allocs must be removed from the free list.
46864679 const pre_allocs_len = @intCast(LocalIndex, f.allocs.count());
4687 const pre_clone_depth = f.free_locals_clone_depth;
4688 f.free_locals_clone_depth = @intCast(LoopDepth, f.free_locals_stack.items.len);
4680 const was_in_clone = f.is_in_clone;
4681 f.is_in_clone = true;
46894682
46904683 for (liveness_condbr.then_deaths) |operand| {
46914684 try die(f, inst, Air.indexToRef(operand));
......@@ -4706,10 +4699,10 @@ fn airCondBr(f: *Function, inst: Air.Inst.Index) !CValue {
47064699
47074700 f.value_map.deinit();
47084701 f.value_map = cloned_map.move();
4709 const free_locals = f.getFreeLocals();
4702 const free_locals = &f.free_locals_map;
47104703 deinitFreeLocalsMap(gpa, free_locals);
47114704 free_locals.* = cloned_frees.move();
4712 f.free_locals_clone_depth = pre_clone_depth;
4705 f.is_in_clone = was_in_clone;
47134706 for (liveness_condbr.else_deaths) |operand| {
47144707 try die(f, inst, Air.indexToRef(operand));
47154708 }
......@@ -4781,7 +4774,7 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index) !CValue {
47814774 if (case_i != last_case_i) {
47824775 const old_value_map = f.value_map;
47834776 f.value_map = try old_value_map.clone();
4784 var free_locals = f.getFreeLocals();
4777 var free_locals = &f.free_locals_map;
47854778 const old_free_locals = free_locals.*;
47864779 free_locals.* = try cloneFreeLocalsMap(gpa, free_locals);
47874780
......@@ -4793,14 +4786,13 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index) !CValue {
47934786 // we can notice and make sure not to use them in subsequent branches.
47944787 // Any new allocs must be removed from the free list.
47954788 const pre_allocs_len = @intCast(LocalIndex, f.allocs.count());
4796 const pre_clone_depth = f.free_locals_clone_depth;
4797 f.free_locals_clone_depth = @intCast(LoopDepth, f.free_locals_stack.items.len);
4789 const was_in_clone = f.is_in_clone;
4790 f.is_in_clone = true;
47984791
47994792 {
48004793 defer {
4801 f.free_locals_clone_depth = pre_clone_depth;
4794 f.is_in_clone = was_in_clone;
48024795 f.value_map.deinit();
4803 free_locals = f.getFreeLocals();
48044796 deinitFreeLocalsMap(gpa, free_locals);
48054797 f.value_map = old_value_map;
48064798 free_locals.* = old_free_locals;
......@@ -4862,8 +4854,8 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue {
48624854 const inputs = @ptrCast([]const Air.Inst.Ref, f.air.extra[extra_i..][0..extra.data.inputs_len]);
48634855 extra_i += inputs.len;
48644856
4865 const result = r: {
4866 if (!is_volatile and f.liveness.isUnused(inst)) break :r .none;
4857 const result = result: {
4858 if (!is_volatile and f.liveness.isUnused(inst)) break :result .none;
48674859
48684860 const writer = f.object.writer();
48694861 const inst_ty = f.air.typeOfIndex(inst);
......@@ -5091,7 +5083,7 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue {
50915083 }
50925084 }
50935085
5094 break :r local;
5086 break :result if (f.liveness.isUnused(inst)) .none else local;
50955087 };
50965088
50975089 var bt = iterateBigTomb(f, inst);
......@@ -7063,21 +7055,22 @@ fn airUnionInit(f: *Function, inst: Air.Inst.Index) !CValue {
70637055
70647056fn airPrefetch(f: *Function, inst: Air.Inst.Index) !CValue {
70657057 const prefetch = f.air.instructions.items(.data)[inst].prefetch;
7066 switch (prefetch.cache) {
7067 .data => {},
7068 // The available prefetch intrinsics do not accept a cache argument; only
7069 // address, rw, and locality. So unless the cache is data, we do not lower
7070 // this instruction.
7071 .instruction => return .none,
7072 }
7058
70737059 const ptr = try f.resolveInst(prefetch.ptr);
70747060 try reap(f, inst, &.{prefetch.ptr});
7061
70757062 const writer = f.object.writer();
7076 try writer.writeAll("zig_prefetch(");
7077 try f.writeCValue(writer, ptr, .FunctionArgument);
7078 try writer.print(", {d}, {d});\n", .{
7079 @enumToInt(prefetch.rw), prefetch.locality,
7080 });
7063 switch (prefetch.cache) {
7064 .data => {
7065 try writer.writeAll("zig_prefetch(");
7066 try f.writeCValue(writer, ptr, .FunctionArgument);
7067 try writer.print(", {d}, {d});\n", .{ @enumToInt(prefetch.rw), prefetch.locality });
7068 },
7069 // The available prefetch intrinsics do not accept a cache argument; only
7070 // address, rw, and locality.
7071 .instruction => {},
7072 }
7073
70817074 return .none;
70827075}
70837076
......@@ -7857,8 +7850,8 @@ fn reap(f: *Function, inst: Air.Inst.Index, operands: []const Air.Inst.Ref) !voi
78577850
78587851fn die(f: *Function, inst: Air.Inst.Index, ref: Air.Inst.Ref) !void {
78597852 const ref_inst = Air.refToIndex(ref) orelse return;
7853 const c_value = (f.value_map.fetchRemove(ref_inst) orelse return).value;
78607854 if (f.air.instructions.items(.tag)[ref_inst] == .constant) return;
7861 const c_value = (f.value_map.fetchRemove(ref) orelse return).value;
78627855 const local_index = switch (c_value) {
78637856 .local, .new_local => |l| l,
78647857 else => return,
......@@ -7870,8 +7863,8 @@ fn freeLocal(f: *Function, inst: Air.Inst.Index, local_index: LocalIndex, ref_in
78707863 const gpa = f.object.dg.gpa;
78717864 const local = &f.locals.items[local_index];
78727865 log.debug("%{d}: freeing t{d} (operand %{d})", .{ inst, local_index, ref_inst });
7873 if (local.loop_depth < f.free_locals_clone_depth) return;
7874 const gop = try f.free_locals_stack.items[local.loop_depth].getOrPut(gpa, local.getType());
7866 if (f.is_in_clone != local.is_in_clone) return;
7867 const gop = try f.free_locals_map.getOrPut(gpa, local.getType());
78757868 if (!gop.found_existing) gop.value_ptr.* = .{};
78767869 if (std.debug.runtime_safety) {
78777870 // If this trips, an unfreeable allocation was attempted to be freed.
......@@ -7935,23 +7928,21 @@ fn noticeBranchFrees(
79357928 pre_allocs_len: LocalIndex,
79367929 inst: Air.Inst.Index,
79377930) !void {
7938 const free_locals = f.getFreeLocals();
7939
79407931 for (f.locals.items[pre_locals_len..], pre_locals_len..) |*local, local_i| {
79417932 const local_index = @intCast(LocalIndex, local_i);
79427933 if (f.allocs.contains(local_index)) {
79437934 if (std.debug.runtime_safety) {
79447935 // new allocs are no longer freeable, so make sure they aren't in the free list
7945 if (free_locals.getPtr(local.getType())) |locals_list| {
7936 if (f.free_locals_map.getPtr(local.getType())) |locals_list| {
79467937 assert(!locals_list.contains(local_index));
79477938 }
79487939 }
79497940 continue;
79507941 }
79517942
7952 // free more deeply nested locals from other branches at current depth
7953 assert(local.loop_depth >= f.free_locals_stack.items.len - 1);
7954 local.loop_depth = @intCast(LoopDepth, f.free_locals_stack.items.len - 1);
7943 // free cloned locals from other branches at current cloned-ness
7944 std.debug.assert(local.is_in_clone or !f.is_in_clone);
7945 local.is_in_clone = f.is_in_clone;
79557946 try freeLocal(f, inst, local_index, 0);
79567947 }
79577948
......@@ -7959,6 +7950,6 @@ fn noticeBranchFrees(
79597950 const local_index = @intCast(LocalIndex, local_i);
79607951 const local = &f.locals.items[local_index];
79617952 // new allocs are no longer freeable, so remove them from the free list
7962 if (free_locals.getPtr(local.getType())) |locals_list| _ = locals_list.swapRemove(local_index);
7953 if (f.free_locals_map.getPtr(local.getType())) |locals_list| _ = locals_list.swapRemove(local_index);
79637954 }
79647955}
src/print_air.zig+28-3
......@@ -267,9 +267,9 @@ const Writer = struct {
267267 .c_va_copy,
268268 => try w.writeTyOp(s, inst),
269269
270 .block,
271 .loop,
272 => try w.writeBlock(s, inst),
270 .block => try w.writeBlock(s, inst),
271
272 .loop => try w.writeLoop(s, inst),
273273
274274 .slice,
275275 .slice_elem_ptr,
......@@ -401,6 +401,31 @@ const Writer = struct {
401401 try s.writeAll("}");
402402 }
403403
404 fn writeLoop(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void {
405 const ty_pl = w.air.instructions.items(.data)[inst].ty_pl;
406 const extra = w.air.extraData(Air.Block, ty_pl.payload);
407 const body = w.air.extra[extra.end..][0..extra.data.body_len];
408 const liveness_loop = w.liveness.getLoop(inst);
409
410 try w.writeType(s, w.air.getRefType(ty_pl.ty));
411 if (w.skip_body) return s.writeAll(", ...");
412 try s.writeAll(", {\n");
413 const old_indent = w.indent;
414 w.indent += 2;
415 try w.writeBody(s, body);
416 if (liveness_loop.deaths.len != 0) {
417 try s.writeByteNTimes(' ', w.indent);
418 for (liveness_loop.deaths, 0..) |operand, i| {
419 if (i != 0) try s.writeAll(" ");
420 try s.print("%{d}!", .{operand});
421 }
422 try s.writeAll("\n");
423 }
424 w.indent = old_indent;
425 try s.writeByteNTimes(' ', w.indent);
426 try s.writeAll("}");
427 }
428
404429 fn writeAggregateInit(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void {
405430 const ty_pl = w.air.instructions.items(.data)[inst].ty_pl;
406431 const vector_ty = w.air.getRefType(ty_pl.ty);
test/behavior/const_slice_child.zig-1
......@@ -7,7 +7,6 @@ const expect = testing.expect;
77var argv: [*]const [*]const u8 = undefined;
88
99test "const slice child" {
10 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
1110 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
1211 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
1312 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
test/behavior/for.zig-2
......@@ -227,7 +227,6 @@ test "else continue outer for" {
227227
228228test "for loop with else branch" {
229229 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
230 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
231230
232231 {
233232 var x = [_]u32{ 1, 2 };
......@@ -377,7 +376,6 @@ test "raw pointer and slice" {
377376test "raw pointer and counter" {
378377 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
379378 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
380 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
381379 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
382380
383381 var buf: [10]u8 = undefined;
test/behavior/slice.zig-1
......@@ -238,7 +238,6 @@ test "C pointer" {
238238test "C pointer slice access" {
239239 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
240240 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
241 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;
242241 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
243242
244243 var buf: [10]u32 = [1]u32{42} ** 10;