authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-09-20 19:31:43-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-09-21 03:54:37-04:00
log85e32043440f3f555419af2a5964618df79cfdce
tree07261572d548eb2ba7afc03fb5bc7e2a9037259c
parent902f6db67b2d25f238fb13e458a12e06df62dadb

stage2: free up 2 ZIR tags

cmpxchg_weak and cmpxchg_strong are not very common; demote them to extended operations to make some headroom. This commit does not change any behavior, only memory layout of the compiler.

5 files changed, 111 insertions(+), 104 deletions(-)

src/AstGen.zig+12-8
......@@ -2439,8 +2439,6 @@ fn addEnsureResult(gz: *GenZir, maybe_unused_result: Zir.Inst.Ref, statement: As
24392439 .shr_exact,
24402440 .bit_offset_of,
24412441 .offset_of,
2442 .cmpxchg_strong,
2443 .cmpxchg_weak,
24442442 .splat,
24452443 .reduce,
24462444 .shuffle,
......@@ -7753,8 +7751,8 @@ fn builtinCall(
77537751 .c_undef => return simpleCBuiltin(gz, scope, rl, node, params[0], .c_undef),
77547752 .c_include => return simpleCBuiltin(gz, scope, rl, node, params[0], .c_include),
77557753
7756 .cmpxchg_strong => return cmpxchg(gz, scope, rl, node, params, .cmpxchg_strong),
7757 .cmpxchg_weak => return cmpxchg(gz, scope, rl, node, params, .cmpxchg_weak),
7754 .cmpxchg_strong => return cmpxchg(gz, scope, rl, node, params, 1),
7755 .cmpxchg_weak => return cmpxchg(gz, scope, rl, node, params, 0),
77587756 // zig fmt: on
77597757
77607758 .wasm_memory_size => {
......@@ -8080,11 +8078,12 @@ fn cmpxchg(
80808078 rl: ResultLoc,
80818079 node: Ast.Node.Index,
80828080 params: []const Ast.Node.Index,
8083 tag: Zir.Inst.Tag,
8081 small: u16,
80848082) InnerError!Zir.Inst.Ref {
80858083 const int_type = try typeExpr(gz, scope, params[0]);
8086 const result = try gz.addPlNode(tag, node, Zir.Inst.Cmpxchg{
8084 const result = try gz.addExtendedPayloadSmall(.cmpxchg, small, Zir.Inst.Cmpxchg{
80878085 // zig fmt: off
8086 .node = gz.nodeIndexToRelative(node),
80888087 .ptr = try expr(gz, scope, .none, params[1]),
80898088 .expected_value = try expr(gz, scope, .{ .ty = int_type }, params[2]),
80908089 .new_value = try expr(gz, scope, .{ .coerced_ty = int_type }, params[3]),
......@@ -10904,9 +10903,14 @@ const GenZir = struct {
1090410903 return new_index;
1090510904 }
1090610905
10907 fn addExtendedPayload(
10906 fn addExtendedPayload(gz: *GenZir, opcode: Zir.Inst.Extended, extra: anytype) !Zir.Inst.Ref {
10907 return addExtendedPayloadSmall(gz, opcode, undefined, extra);
10908 }
10909
10910 fn addExtendedPayloadSmall(
1090810911 gz: *GenZir,
1090910912 opcode: Zir.Inst.Extended,
10913 small: u16,
1091010914 extra: anytype,
1091110915 ) !Zir.Inst.Ref {
1091210916 const gpa = gz.astgen.gpa;
......@@ -10920,7 +10924,7 @@ const GenZir = struct {
1092010924 .tag = .extended,
1092110925 .data = .{ .extended = .{
1092210926 .opcode = opcode,
10923 .small = undefined,
10927 .small = small,
1092410928 .operand = payload_index,
1092510929 } },
1092610930 });
src/Autodoc.zig+73-66
......@@ -998,72 +998,6 @@ fn walkInstruction(
998998 const un_tok = data[inst_index].un_tok;
999999 return try self.walkRef(file, parent_scope, parent_src, un_tok.operand, need_type);
10001000 },
1001 .cmpxchg_strong, .cmpxchg_weak => {
1002 const pl_node = data[inst_index].pl_node;
1003 const extra = file.zir.extraData(Zir.Inst.Cmpxchg, pl_node.payload_index);
1004
1005 const last_type_index = self.exprs.items.len;
1006 const last_type = self.exprs.items[last_type_index - 1];
1007 const type_index = self.exprs.items.len;
1008 try self.exprs.append(self.arena, last_type);
1009
1010 const ptr_index = self.exprs.items.len;
1011 var ptr: DocData.WalkResult = try self.walkRef(
1012 file,
1013 parent_scope,
1014 parent_src,
1015 extra.data.ptr,
1016 false,
1017 );
1018 try self.exprs.append(self.arena, ptr.expr);
1019
1020 const expected_value_index = self.exprs.items.len;
1021 var expected_value: DocData.WalkResult = try self.walkRef(
1022 file,
1023 parent_scope,
1024 parent_src,
1025 extra.data.expected_value,
1026 false,
1027 );
1028 try self.exprs.append(self.arena, expected_value.expr);
1029
1030 const new_value_index = self.exprs.items.len;
1031 var new_value: DocData.WalkResult = try self.walkRef(
1032 file,
1033 parent_scope,
1034 parent_src,
1035 extra.data.new_value,
1036 false,
1037 );
1038 try self.exprs.append(self.arena, new_value.expr);
1039
1040 const success_order_index = self.exprs.items.len;
1041 var success_order: DocData.WalkResult = try self.walkRef(
1042 file,
1043 parent_scope,
1044 parent_src,
1045 extra.data.success_order,
1046 false,
1047 );
1048 try self.exprs.append(self.arena, success_order.expr);
1049
1050 const failure_order_index = self.exprs.items.len;
1051 var failure_order: DocData.WalkResult = try self.walkRef(
1052 file,
1053 parent_scope,
1054 parent_src,
1055 extra.data.failure_order,
1056 false,
1057 );
1058 try self.exprs.append(self.arena, failure_order.expr);
1059
1060 const cmpxchg_index = self.exprs.items.len;
1061 try self.exprs.append(self.arena, .{ .cmpxchg = .{ .name = @tagName(tags[inst_index]), .type = type_index, .ptr = ptr_index, .expected_value = expected_value_index, .new_value = new_value_index, .success_order = success_order_index, .failure_order = failure_order_index } });
1062 return DocData.WalkResult{
1063 .typeRef = .{ .type = @enumToInt(Ref.type_type) },
1064 .expr = .{ .cmpxchgIndex = cmpxchg_index },
1065 };
1066 },
10671001 .str => {
10681002 const str = data[inst_index].str.get(file.zir);
10691003
......@@ -3047,6 +2981,79 @@ fn walkInstruction(
30472981 .expr = .{ .builtinIndex = bin_index },
30482982 };
30492983 },
2984 .cmpxchg => {
2985 const extra = file.zir.extraData(Zir.Inst.Cmpxchg, extended.operand).data;
2986
2987 const last_type_index = self.exprs.items.len;
2988 const last_type = self.exprs.items[last_type_index - 1];
2989 const type_index = self.exprs.items.len;
2990 try self.exprs.append(self.arena, last_type);
2991
2992 const ptr_index = self.exprs.items.len;
2993 var ptr: DocData.WalkResult = try self.walkRef(
2994 file,
2995 parent_scope,
2996 parent_src,
2997 extra.ptr,
2998 false,
2999 );
3000 try self.exprs.append(self.arena, ptr.expr);
3001
3002 const expected_value_index = self.exprs.items.len;
3003 var expected_value: DocData.WalkResult = try self.walkRef(
3004 file,
3005 parent_scope,
3006 parent_src,
3007 extra.expected_value,
3008 false,
3009 );
3010 try self.exprs.append(self.arena, expected_value.expr);
3011
3012 const new_value_index = self.exprs.items.len;
3013 var new_value: DocData.WalkResult = try self.walkRef(
3014 file,
3015 parent_scope,
3016 parent_src,
3017 extra.new_value,
3018 false,
3019 );
3020 try self.exprs.append(self.arena, new_value.expr);
3021
3022 const success_order_index = self.exprs.items.len;
3023 var success_order: DocData.WalkResult = try self.walkRef(
3024 file,
3025 parent_scope,
3026 parent_src,
3027 extra.success_order,
3028 false,
3029 );
3030 try self.exprs.append(self.arena, success_order.expr);
3031
3032 const failure_order_index = self.exprs.items.len;
3033 var failure_order: DocData.WalkResult = try self.walkRef(
3034 file,
3035 parent_scope,
3036 parent_src,
3037 extra.failure_order,
3038 false,
3039 );
3040 try self.exprs.append(self.arena, failure_order.expr);
3041
3042 const cmpxchg_index = self.exprs.items.len;
3043 try self.exprs.append(self.arena, .{ .cmpxchg = .{
3044 .name = @tagName(tags[inst_index]),
3045 .type = type_index,
3046 .ptr = ptr_index,
3047 .expected_value = expected_value_index,
3048 .new_value = new_value_index,
3049 .success_order = success_order_index,
3050 .failure_order = failure_order_index,
3051 } });
3052 return DocData.WalkResult{
3053 .typeRef = .{ .type = @enumToInt(Ref.type_type) },
3054 .expr = .{ .cmpxchgIndex = cmpxchg_index },
3055 };
3056 },
30503057 }
30513058 },
30523059 }
src/Sema.zig+16-13
......@@ -839,8 +839,6 @@ fn analyzeBodyInner(
839839 .bit_reverse => try sema.zirBitReverse(block, inst),
840840 .bit_offset_of => try sema.zirBitOffsetOf(block, inst),
841841 .offset_of => try sema.zirOffsetOf(block, inst),
842 .cmpxchg_strong => try sema.zirCmpxchg(block, inst, .cmpxchg_strong),
843 .cmpxchg_weak => try sema.zirCmpxchg(block, inst, .cmpxchg_weak),
844842 .splat => try sema.zirSplat(block, inst),
845843 .reduce => try sema.zirReduce(block, inst),
846844 .shuffle => try sema.zirShuffle(block, inst),
......@@ -957,6 +955,8 @@ fn analyzeBodyInner(
957955 .int_to_error => try sema.zirIntToError( block, extended),
958956 .reify => try sema.zirReify( block, extended, inst),
959957 .builtin_async_call => try sema.zirBuiltinAsyncCall( block, extended),
958 .cmpxchg => try sema.zirCmpxchg( block, extended),
959
960960 // zig fmt: on
961961 .fence => {
962962 try sema.zirFence(block, extended);
......@@ -18891,19 +18891,22 @@ fn resolveAtomicRmwOp(
1889118891fn zirCmpxchg(
1889218892 sema: *Sema,
1889318893 block: *Block,
18894 inst: Zir.Inst.Index,
18895 air_tag: Air.Inst.Tag,
18894 extended: Zir.Inst.Extended.InstData,
1889618895) CompileError!Air.Inst.Ref {
18897 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
18898 const extra = sema.code.extraData(Zir.Inst.Cmpxchg, inst_data.payload_index).data;
18899 const src = inst_data.src();
18896 const extra = sema.code.extraData(Zir.Inst.Cmpxchg, extended.operand).data;
18897 const air_tag: Air.Inst.Tag = switch (extended.small) {
18898 0 => .cmpxchg_weak,
18899 1 => .cmpxchg_strong,
18900 else => unreachable,
18901 };
18902 const src = LazySrcLoc.nodeOffset(extra.node);
1890018903 // zig fmt: off
18901 const elem_ty_src : LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
18902 const ptr_src : LazySrcLoc = .{ .node_offset_builtin_call_arg1 = inst_data.src_node };
18903 const expected_src : LazySrcLoc = .{ .node_offset_builtin_call_arg2 = inst_data.src_node };
18904 const new_value_src : LazySrcLoc = .{ .node_offset_builtin_call_arg3 = inst_data.src_node };
18905 const success_order_src: LazySrcLoc = .{ .node_offset_builtin_call_arg4 = inst_data.src_node };
18906 const failure_order_src: LazySrcLoc = .{ .node_offset_builtin_call_arg5 = inst_data.src_node };
18904 const elem_ty_src : LazySrcLoc = .{ .node_offset_builtin_call_arg0 = extra.node };
18905 const ptr_src : LazySrcLoc = .{ .node_offset_builtin_call_arg1 = extra.node };
18906 const expected_src : LazySrcLoc = .{ .node_offset_builtin_call_arg2 = extra.node };
18907 const new_value_src : LazySrcLoc = .{ .node_offset_builtin_call_arg3 = extra.node };
18908 const success_order_src: LazySrcLoc = .{ .node_offset_builtin_call_arg4 = extra.node };
18909 const failure_order_src: LazySrcLoc = .{ .node_offset_builtin_call_arg5 = extra.node };
1890718910 // zig fmt: on
1890818911 const expected_value = try sema.resolveInst(extra.expected_value);
1890918912 const elem_ty = sema.typeOf(expected_value);
src/Zir.zig+5-12
......@@ -890,12 +890,6 @@ pub const Inst = struct {
890890 /// Implements the `@offsetOf` builtin.
891891 /// Uses the `pl_node` union field with payload `Bin`.
892892 offset_of,
893 /// Implements the `@cmpxchgStrong` builtin.
894 /// Uses the `pl_node` union field with payload `Cmpxchg`.
895 cmpxchg_strong,
896 /// Implements the `@cmpxchgWeak` builtin.
897 /// Uses the `pl_node` union field with payload `Cmpxchg`.
898 cmpxchg_weak,
899893 /// Implements the `@splat` builtin.
900894 /// Uses the `pl_node` union field with payload `Bin`.
901895 splat,
......@@ -1211,8 +1205,6 @@ pub const Inst = struct {
12111205 .shr_exact,
12121206 .bit_offset_of,
12131207 .offset_of,
1214 .cmpxchg_strong,
1215 .cmpxchg_weak,
12161208 .splat,
12171209 .reduce,
12181210 .shuffle,
......@@ -1498,8 +1490,6 @@ pub const Inst = struct {
14981490 .shr_exact,
14991491 .bit_offset_of,
15001492 .offset_of,
1501 .cmpxchg_strong,
1502 .cmpxchg_weak,
15031493 .splat,
15041494 .reduce,
15051495 .shuffle,
......@@ -1781,8 +1771,6 @@ pub const Inst = struct {
17811771
17821772 .bit_offset_of = .pl_node,
17831773 .offset_of = .pl_node,
1784 .cmpxchg_strong = .pl_node,
1785 .cmpxchg_weak = .pl_node,
17861774 .splat = .pl_node,
17871775 .reduce = .pl_node,
17881776 .shuffle = .pl_node,
......@@ -1972,6 +1960,10 @@ pub const Inst = struct {
19721960 /// Implements the `@asyncCall` builtin.
19731961 /// `operand` is payload index to `AsyncCall`.
19741962 builtin_async_call,
1963 /// Implements the `@cmpxchgStrong` and `@cmpxchgWeak` builtins.
1964 /// `small` 0=>weak 1=>strong
1965 /// `operand` is payload index to `Cmpxchg`.
1966 cmpxchg,
19751967
19761968 pub const InstData = struct {
19771969 opcode: Extended,
......@@ -3392,6 +3384,7 @@ pub const Inst = struct {
33923384 };
33933385
33943386 pub const Cmpxchg = struct {
3387 node: i32,
33953388 ptr: Ref,
33963389 expected_value: Ref,
33973390 new_value: Ref,
src/print_zir.zig+5-5
......@@ -272,7 +272,6 @@ const Writer = struct {
272272 .struct_init_ref,
273273 => try self.writeStructInit(stream, inst),
274274
275 .cmpxchg_strong, .cmpxchg_weak => try self.writeCmpxchg(stream, inst),
276275 .atomic_load => try self.writeAtomicLoad(stream, inst),
277276 .atomic_store => try self.writeAtomicStore(stream, inst),
278277 .atomic_rmw => try self.writeAtomicRmw(stream, inst),
......@@ -532,6 +531,7 @@ const Writer = struct {
532531 try self.writeSrc(stream, src);
533532 },
534533 .builtin_async_call => try self.writeBuiltinAsyncCall(stream, extended),
534 .cmpxchg => try self.writeCmpxchg(stream, extended),
535535 }
536536 }
537537
......@@ -912,9 +912,9 @@ const Writer = struct {
912912 try self.writeSrc(stream, inst_data.src());
913913 }
914914
915 fn writeCmpxchg(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
916 const inst_data = self.code.instructions.items(.data)[inst].pl_node;
917 const extra = self.code.extraData(Zir.Inst.Cmpxchg, inst_data.payload_index).data;
915 fn writeCmpxchg(self: *Writer, stream: anytype, extended: Zir.Inst.Extended.InstData) !void {
916 const extra = self.code.extraData(Zir.Inst.Cmpxchg, extended.operand).data;
917 const src = LazySrcLoc.nodeOffset(extra.node);
918918
919919 try self.writeInstRef(stream, extra.ptr);
920920 try stream.writeAll(", ");
......@@ -926,7 +926,7 @@ const Writer = struct {
926926 try stream.writeAll(", ");
927927 try self.writeInstRef(stream, extra.failure_order);
928928 try stream.writeAll(") ");
929 try self.writeSrc(stream, inst_data.src());
929 try self.writeSrc(stream, src);
930930 }
931931
932932 fn writeAtomicLoad(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {