authorgravatar for mail@isaacfreund.comIsaac Freund <mail@isaacfreund.com> 2021-12-07 19:34:44+01:00
committergravatar for mail@isaacfreund.comIsaac Freund <mail@isaacfreund.com> 2021-12-10 23:09:02+01:00
log175463d75dbde1e8e4c5a55159ab4e9446fd211c
tree523bb9e42429e97c288838ab42ee59e18edbaca0
parent47c309c34a23bcec9b3d72dade688965893614a4
signaturelock-open Commit is signed but in an unrecognized format.

AstGen: implement @prefetch() builtin


8 files changed, 90 insertions(+), 1 deletions(-)

lib/std/builtin.zig+25
...@@ -651,6 +651,31 @@ pub const CallOptions = struct {...@@ -651,6 +651,31 @@ pub const CallOptions = struct {
651 };651 };
652};652};
653653
654/// This data structure is used by the Zig language code generation and
655/// therefore must be kept in sync with the compiler implementation.
656pub const PrefetchOptions = struct {
657 /// Whether the prefetch should prepare for a read or a write.
658 rw: Rw = .read,
659 /// 0 means no temporal locality. That is, the data can be immediately
660 /// dropped from the cache after it is accessed.
661 ///
662 /// 3 means high temporal locality. That is, the data should be kept in
663 /// the cache as it is likely to be accessed again soon.
664 locality: u2 = 3,
665 /// The cache that the prefetch should be preformed on.
666 cache: Cache = .data,
667
668 pub const Rw = enum {
669 read,
670 write,
671 };
672
673 pub const Cache = enum {
674 instruction,
675 data,
676 };
677};
678
654/// This data structure is used by the Zig language code generation and679/// This data structure is used by the Zig language code generation and
655/// therefore must be kept in sync with the compiler implementation.680/// therefore must be kept in sync with the compiler implementation.
656pub const ExportOptions = struct {681pub const ExportOptions = struct {
src/AstGen.zig+10
...@@ -7226,6 +7226,16 @@ fn builtinCall(...@@ -7226,6 +7226,16 @@ fn builtinCall(
7226 });7226 });
7227 return rvalue(gz, rl, result, node);7227 return rvalue(gz, rl, result, node);
7228 },7228 },
7229 .prefetch => {
7230 const ptr = try expr(gz, scope, .none, params[0]);
7231 const options = try comptimeExpr(gz, scope, .{ .ty = .prefetch_options_type }, params[1]);
7232 const result = try gz.addExtendedPayload(.prefetch, Zir.Inst.BinNode{
7233 .node = gz.nodeIndexToRelative(node),
7234 .lhs = ptr,
7235 .rhs = options,
7236 });
7237 return rvalue(gz, rl, result, node);
7238 },
7229 }7239 }
7230}7240}
72317241
src/BuiltinFn.zig+8
...@@ -67,6 +67,7 @@ pub const Tag = enum {...@@ -67,6 +67,7 @@ pub const Tag = enum {
67 mul_with_overflow,67 mul_with_overflow,
68 panic,68 panic,
69 pop_count,69 pop_count,
70 prefetch,
70 ptr_cast,71 ptr_cast,
71 ptr_to_int,72 ptr_to_int,
72 rem,73 rem,
...@@ -615,6 +616,13 @@ pub const list = list: {...@@ -615,6 +616,13 @@ pub const list = list: {
615 .param_count = 2,616 .param_count = 2,
616 },617 },
617 },618 },
619 .{
620 "@prefetch",
621 .{
622 .tag = .prefetch,
623 .param_count = 2,
624 },
625 },
618 .{626 .{
619 "@ptrCast",627 "@ptrCast",
620 .{628 .{
src/Sema.zig+14
...@@ -1042,6 +1042,7 @@ fn zirExtended(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai...@@ -1042,6 +1042,7 @@ fn zirExtended(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1042 .c_define => return sema.zirCDefine( block, extended),1042 .c_define => return sema.zirCDefine( block, extended),
1043 .wasm_memory_size => return sema.zirWasmMemorySize( block, extended),1043 .wasm_memory_size => return sema.zirWasmMemorySize( block, extended),
1044 .wasm_memory_grow => return sema.zirWasmMemoryGrow( block, extended),1044 .wasm_memory_grow => return sema.zirWasmMemoryGrow( block, extended),
1045 .prefetch => return sema.zirPrefetch( block, extended),
1045 // zig fmt: on1046 // zig fmt: on
1046 }1047 }
1047}1048}
...@@ -11104,6 +11105,16 @@ fn zirWasmMemoryGrow(...@@ -11104,6 +11105,16 @@ fn zirWasmMemoryGrow(
11104 return sema.fail(block, src, "TODO: implement Sema.zirWasmMemoryGrow", .{});11105 return sema.fail(block, src, "TODO: implement Sema.zirWasmMemoryGrow", .{});
11105}11106}
1110611107
11108fn zirPrefetch(
11109 sema: *Sema,
11110 block: *Block,
11111 extended: Zir.Inst.Extended.InstData,
11112) CompileError!Air.Inst.Ref {
11113 const extra = sema.code.extraData(Zir.Inst.BinNode, extended.operand).data;
11114 const src: LazySrcLoc = .{ .node_offset = extra.node };
11115 return sema.fail(block, src, "TODO: implement Sema.zirPrefetch", .{});
11116}
11117
11107fn zirBuiltinExtern(11118fn zirBuiltinExtern(
11108 sema: *Sema,11119 sema: *Sema,
11109 block: *Block,11120 block: *Block,
...@@ -14231,6 +14242,7 @@ fn resolveTypeFields(sema: *Sema, block: *Block, src: LazySrcLoc, ty: Type) Comp...@@ -14231,6 +14242,7 @@ fn resolveTypeFields(sema: *Sema, block: *Block, src: LazySrcLoc, ty: Type) Comp
14231 .float_mode => return sema.resolveBuiltinTypeFields(block, src, "FloatMode"),14242 .float_mode => return sema.resolveBuiltinTypeFields(block, src, "FloatMode"),
14232 .reduce_op => return sema.resolveBuiltinTypeFields(block, src, "ReduceOp"),14243 .reduce_op => return sema.resolveBuiltinTypeFields(block, src, "ReduceOp"),
14233 .call_options => return sema.resolveBuiltinTypeFields(block, src, "CallOptions"),14244 .call_options => return sema.resolveBuiltinTypeFields(block, src, "CallOptions"),
14245 .prefetch_options => return sema.resolveBuiltinTypeFields(block, src, "PrefetchOptions"),
1423414246
14235 .@"union", .union_tagged => {14247 .@"union", .union_tagged => {
14236 const union_obj = ty.cast(Type.Payload.Union).?.data;14248 const union_obj = ty.cast(Type.Payload.Union).?.data;
...@@ -14819,6 +14831,7 @@ fn typeHasOnePossibleValue(...@@ -14819,6 +14831,7 @@ fn typeHasOnePossibleValue(
14819 .float_mode,14831 .float_mode,
14820 .reduce_op,14832 .reduce_op,
14821 .call_options,14833 .call_options,
14834 .prefetch_options,
14822 .export_options,14835 .export_options,
14823 .extern_options,14836 .extern_options,
14824 .type_info,14837 .type_info,
...@@ -15032,6 +15045,7 @@ pub fn addType(sema: *Sema, ty: Type) !Air.Inst.Ref {...@@ -15032,6 +15045,7 @@ pub fn addType(sema: *Sema, ty: Type) !Air.Inst.Ref {
15032 .float_mode => return .float_mode_type,15045 .float_mode => return .float_mode_type,
15033 .reduce_op => return .reduce_op_type,15046 .reduce_op => return .reduce_op_type,
15034 .call_options => return .call_options_type,15047 .call_options => return .call_options_type,
15048 .prefetch_options => return .prefetch_options_type,
15035 .export_options => return .export_options_type,15049 .export_options => return .export_options_type,
15036 .extern_options => return .extern_options_type,15050 .extern_options => return .extern_options_type,
15037 .type_info => return .type_info_type,15051 .type_info => return .type_info_type,
src/Zir.zig+8
...@@ -1572,6 +1572,9 @@ pub const Inst = struct {...@@ -1572,6 +1572,9 @@ pub const Inst = struct {
1572 wasm_memory_size,1572 wasm_memory_size,
1573 /// `operand` is payload index to `BinNode`.1573 /// `operand` is payload index to `BinNode`.
1574 wasm_memory_grow,1574 wasm_memory_grow,
1575 /// The `@prefetch` builtin.
1576 /// `operand` is payload index to `BinNode`.
1577 prefetch,
15751578
1576 pub const InstData = struct {1579 pub const InstData = struct {
1577 opcode: Extended,1580 opcode: Extended,
...@@ -1648,6 +1651,7 @@ pub const Inst = struct {...@@ -1648,6 +1651,7 @@ pub const Inst = struct {
1648 float_mode_type,1651 float_mode_type,
1649 reduce_op_type,1652 reduce_op_type,
1650 call_options_type,1653 call_options_type,
1654 prefetch_options_type,
1651 export_options_type,1655 export_options_type,
1652 extern_options_type,1656 extern_options_type,
1653 type_info_type,1657 type_info_type,
...@@ -1917,6 +1921,10 @@ pub const Inst = struct {...@@ -1917,6 +1921,10 @@ pub const Inst = struct {
1917 .ty = Type.initTag(.type),1921 .ty = Type.initTag(.type),
1918 .val = Value.initTag(.call_options_type),1922 .val = Value.initTag(.call_options_type),
1919 },1923 },
1924 .prefetch_options_type = .{
1925 .ty = Type.initTag(.type),
1926 .val = Value.initTag(.prefetch_options_type),
1927 },
1920 .export_options_type = .{1928 .export_options_type = .{
1921 .ty = Type.initTag(.type),1929 .ty = Type.initTag(.type),
1922 .val = Value.initTag(.export_options_type),1930 .val = Value.initTag(.export_options_type),
src/print_zir.zig+1-1
...@@ -477,7 +477,7 @@ const Writer = struct {...@@ -477,7 +477,7 @@ const Writer = struct {
477 try self.writeSrc(stream, src);477 try self.writeSrc(stream, src);
478 },478 },
479479
480 .builtin_extern, .c_define, .wasm_memory_grow => {480 .builtin_extern, .c_define, .wasm_memory_grow, .prefetch => {
481 const inst_data = self.code.extraData(Zir.Inst.BinNode, extended.operand).data;481 const inst_data = self.code.extraData(Zir.Inst.BinNode, extended.operand).data;
482 const src: LazySrcLoc = .{ .node_offset = inst_data.node };482 const src: LazySrcLoc = .{ .node_offset = inst_data.node };
483 try self.writeInstRef(stream, inst_data.lhs);483 try self.writeInstRef(stream, inst_data.lhs);
src/type.zig+19
...@@ -123,6 +123,7 @@ pub const Type = extern union {...@@ -123,6 +123,7 @@ pub const Type = extern union {
123 .empty_struct_literal,123 .empty_struct_literal,
124 .@"struct",124 .@"struct",
125 .call_options,125 .call_options,
126 .prefetch_options,
126 .export_options,127 .export_options,
127 .extern_options,128 .extern_options,
128 => return .Struct,129 => return .Struct,
...@@ -798,6 +799,7 @@ pub const Type = extern union {...@@ -798,6 +799,7 @@ pub const Type = extern union {
798 .float_mode,799 .float_mode,
799 .reduce_op,800 .reduce_op,
800 .call_options,801 .call_options,
802 .prefetch_options,
801 .export_options,803 .export_options,
802 .extern_options,804 .extern_options,
803 .type_info,805 .type_info,
...@@ -1027,6 +1029,7 @@ pub const Type = extern union {...@@ -1027,6 +1029,7 @@ pub const Type = extern union {
1027 .float_mode => return writer.writeAll("std.builtin.FloatMode"),1029 .float_mode => return writer.writeAll("std.builtin.FloatMode"),
1028 .reduce_op => return writer.writeAll("std.builtin.ReduceOp"),1030 .reduce_op => return writer.writeAll("std.builtin.ReduceOp"),
1029 .call_options => return writer.writeAll("std.builtin.CallOptions"),1031 .call_options => return writer.writeAll("std.builtin.CallOptions"),
1032 .prefetch_options => return writer.writeAll("std.builtin.PrefetchOptions"),
1030 .export_options => return writer.writeAll("std.builtin.ExportOptions"),1033 .export_options => return writer.writeAll("std.builtin.ExportOptions"),
1031 .extern_options => return writer.writeAll("std.builtin.ExternOptions"),1034 .extern_options => return writer.writeAll("std.builtin.ExternOptions"),
1032 .type_info => return writer.writeAll("std.builtin.TypeInfo"),1035 .type_info => return writer.writeAll("std.builtin.TypeInfo"),
...@@ -1318,6 +1321,7 @@ pub const Type = extern union {...@@ -1318,6 +1321,7 @@ pub const Type = extern union {
1318 .float_mode => return "FloatMode",1321 .float_mode => return "FloatMode",
1319 .reduce_op => return "ReduceOp",1322 .reduce_op => return "ReduceOp",
1320 .call_options => return "CallOptions",1323 .call_options => return "CallOptions",
1324 .prefetch_options => return "PrefetchOptions",
1321 .export_options => return "ExportOptions",1325 .export_options => return "ExportOptions",
1322 .extern_options => return "ExternOptions",1326 .extern_options => return "ExternOptions",
1323 .type_info => return "TypeInfo",1327 .type_info => return "TypeInfo",
...@@ -1376,6 +1380,7 @@ pub const Type = extern union {...@@ -1376,6 +1380,7 @@ pub const Type = extern union {
1376 .float_mode,1380 .float_mode,
1377 .reduce_op,1381 .reduce_op,
1378 .call_options,1382 .call_options,
1383 .prefetch_options,
1379 .export_options,1384 .export_options,
1380 .extern_options,1385 .extern_options,
1381 .manyptr_u8,1386 .manyptr_u8,
...@@ -1502,6 +1507,7 @@ pub const Type = extern union {...@@ -1502,6 +1507,7 @@ pub const Type = extern union {
1502 .float_mode => return Value.initTag(.float_mode_type),1507 .float_mode => return Value.initTag(.float_mode_type),
1503 .reduce_op => return Value.initTag(.reduce_op_type),1508 .reduce_op => return Value.initTag(.reduce_op_type),
1504 .call_options => return Value.initTag(.call_options_type),1509 .call_options => return Value.initTag(.call_options_type),
1510 .prefetch_options => return Value.initTag(.prefetch_options_type),
1505 .export_options => return Value.initTag(.export_options_type),1511 .export_options => return Value.initTag(.export_options_type),
1506 .extern_options => return Value.initTag(.extern_options_type),1512 .extern_options => return Value.initTag(.extern_options_type),
1507 .type_info => return Value.initTag(.type_info_type),1513 .type_info => return Value.initTag(.type_info_type),
...@@ -1563,6 +1569,7 @@ pub const Type = extern union {...@@ -1563,6 +1569,7 @@ pub const Type = extern union {
1563 .float_mode,1569 .float_mode,
1564 .reduce_op,1570 .reduce_op,
1565 .call_options,1571 .call_options,
1572 .prefetch_options,
1566 .export_options,1573 .export_options,
1567 .extern_options,1574 .extern_options,
1568 .@"anyframe",1575 .@"anyframe",
...@@ -1750,6 +1757,7 @@ pub const Type = extern union {...@@ -1750,6 +1757,7 @@ pub const Type = extern union {
1750 .float_mode,1757 .float_mode,
1751 .reduce_op,1758 .reduce_op,
1752 .call_options,1759 .call_options,
1760 .prefetch_options,
1753 .export_options,1761 .export_options,
1754 .extern_options,1762 .extern_options,
1755 => return 1,1763 => return 1,
...@@ -1929,6 +1937,7 @@ pub const Type = extern union {...@@ -1929,6 +1937,7 @@ pub const Type = extern union {
1929 .var_args_param => unreachable,1937 .var_args_param => unreachable,
1930 .generic_poison => unreachable,1938 .generic_poison => unreachable,
1931 .call_options => unreachable, // missing call to resolveTypeFields1939 .call_options => unreachable, // missing call to resolveTypeFields
1940 .prefetch_options => unreachable, // missing call to resolveTypeFields
1932 .export_options => unreachable, // missing call to resolveTypeFields1941 .export_options => unreachable, // missing call to resolveTypeFields
1933 .extern_options => unreachable, // missing call to resolveTypeFields1942 .extern_options => unreachable, // missing call to resolveTypeFields
1934 .type_info => unreachable, // missing call to resolveTypeFields1943 .type_info => unreachable, // missing call to resolveTypeFields
...@@ -2269,6 +2278,7 @@ pub const Type = extern union {...@@ -2269,6 +2278,7 @@ pub const Type = extern union {
2269 .float_mode,2278 .float_mode,
2270 .reduce_op,2279 .reduce_op,
2271 .call_options,2280 .call_options,
2281 .prefetch_options,
2272 .export_options,2282 .export_options,
2273 .extern_options,2283 .extern_options,
2274 .type_info,2284 .type_info,
...@@ -2794,6 +2804,7 @@ pub const Type = extern union {...@@ -2794,6 +2804,7 @@ pub const Type = extern union {
2794 .float_mode,2804 .float_mode,
2795 .reduce_op,2805 .reduce_op,
2796 .call_options,2806 .call_options,
2807 .prefetch_options,
2797 .export_options,2808 .export_options,
2798 .extern_options,2809 .extern_options,
2799 .type_info,2810 .type_info,
...@@ -3294,6 +3305,7 @@ pub const Type = extern union {...@@ -3294,6 +3305,7 @@ pub const Type = extern union {
3294 .float_mode,3305 .float_mode,
3295 .reduce_op,3306 .reduce_op,
3296 .call_options,3307 .call_options,
3308 .prefetch_options,
3297 .export_options,3309 .export_options,
3298 .extern_options,3310 .extern_options,
3299 .type_info,3311 .type_info,
...@@ -3502,6 +3514,7 @@ pub const Type = extern union {...@@ -3502,6 +3514,7 @@ pub const Type = extern union {
3502 .float_mode,3514 .float_mode,
3503 .reduce_op,3515 .reduce_op,
3504 .call_options,3516 .call_options,
3517 .prefetch_options,
3505 .export_options,3518 .export_options,
3506 .extern_options,3519 .extern_options,
3507 => @panic("TODO resolve std.builtin types"),3520 => @panic("TODO resolve std.builtin types"),
...@@ -3577,6 +3590,7 @@ pub const Type = extern union {...@@ -3577,6 +3590,7 @@ pub const Type = extern union {
3577 .float_mode,3590 .float_mode,
3578 .reduce_op,3591 .reduce_op,
3579 .call_options,3592 .call_options,
3593 .prefetch_options,
3580 .export_options,3594 .export_options,
3581 .extern_options,3595 .extern_options,
3582 => @panic("TODO resolve std.builtin types"),3596 => @panic("TODO resolve std.builtin types"),
...@@ -3701,6 +3715,7 @@ pub const Type = extern union {...@@ -3701,6 +3715,7 @@ pub const Type = extern union {
3701 .float_mode,3715 .float_mode,
3702 .reduce_op,3716 .reduce_op,
3703 .call_options,3717 .call_options,
3718 .prefetch_options,
3704 .export_options,3719 .export_options,
3705 .extern_options,3720 .extern_options,
3706 .type_info,3721 .type_info,
...@@ -3741,6 +3756,7 @@ pub const Type = extern union {...@@ -3741,6 +3756,7 @@ pub const Type = extern union {
3741 .float_mode,3756 .float_mode,
3742 .reduce_op,3757 .reduce_op,
3743 .call_options,3758 .call_options,
3759 .prefetch_options,
3744 .export_options,3760 .export_options,
3745 .extern_options,3761 .extern_options,
3746 .type_info,3762 .type_info,
...@@ -3801,6 +3817,7 @@ pub const Type = extern union {...@@ -3801,6 +3817,7 @@ pub const Type = extern union {
3801 .float_mode,3817 .float_mode,
3802 .reduce_op,3818 .reduce_op,
3803 .call_options,3819 .call_options,
3820 .prefetch_options,
3804 .export_options,3821 .export_options,
3805 .extern_options,3822 .extern_options,
3806 => @panic("TODO resolve std.builtin types"),3823 => @panic("TODO resolve std.builtin types"),
...@@ -3862,6 +3879,7 @@ pub const Type = extern union {...@@ -3862,6 +3879,7 @@ pub const Type = extern union {
3862 float_mode,3879 float_mode,
3863 reduce_op,3880 reduce_op,
3864 call_options,3881 call_options,
3882 prefetch_options,
3865 export_options,3883 export_options,
3866 extern_options,3884 extern_options,
3867 type_info,3885 type_info,
...@@ -3989,6 +4007,7 @@ pub const Type = extern union {...@@ -3989,6 +4007,7 @@ pub const Type = extern union {
3989 .float_mode,4007 .float_mode,
3990 .reduce_op,4008 .reduce_op,
3991 .call_options,4009 .call_options,
4010 .prefetch_options,
3992 .export_options,4011 .export_options,
3993 .extern_options,4012 .extern_options,
3994 .type_info,4013 .type_info,
src/value.zig+5
...@@ -67,6 +67,7 @@ pub const Value = extern union {...@@ -67,6 +67,7 @@ pub const Value = extern union {
67 float_mode_type,67 float_mode_type,
68 reduce_op_type,68 reduce_op_type,
69 call_options_type,69 call_options_type,
70 prefetch_options_type,
70 export_options_type,71 export_options_type,
71 extern_options_type,72 extern_options_type,
72 type_info_type,73 type_info_type,
...@@ -244,6 +245,7 @@ pub const Value = extern union {...@@ -244,6 +245,7 @@ pub const Value = extern union {
244 .float_mode_type,245 .float_mode_type,
245 .reduce_op_type,246 .reduce_op_type,
246 .call_options_type,247 .call_options_type,
248 .prefetch_options_type,
247 .export_options_type,249 .export_options_type,
248 .extern_options_type,250 .extern_options_type,
249 .type_info_type,251 .type_info_type,
...@@ -434,6 +436,7 @@ pub const Value = extern union {...@@ -434,6 +436,7 @@ pub const Value = extern union {
434 .float_mode_type,436 .float_mode_type,
435 .reduce_op_type,437 .reduce_op_type,
436 .call_options_type,438 .call_options_type,
439 .prefetch_options_type,
437 .export_options_type,440 .export_options_type,
438 .extern_options_type,441 .extern_options_type,
439 .type_info_type,442 .type_info_type,
...@@ -652,6 +655,7 @@ pub const Value = extern union {...@@ -652,6 +655,7 @@ pub const Value = extern union {
652 .float_mode_type => return out_stream.writeAll("std.builtin.FloatMode"),655 .float_mode_type => return out_stream.writeAll("std.builtin.FloatMode"),
653 .reduce_op_type => return out_stream.writeAll("std.builtin.ReduceOp"),656 .reduce_op_type => return out_stream.writeAll("std.builtin.ReduceOp"),
654 .call_options_type => return out_stream.writeAll("std.builtin.CallOptions"),657 .call_options_type => return out_stream.writeAll("std.builtin.CallOptions"),
658 .prefetch_options_type => return out_stream.writeAll("std.builtin.PrefetchOptions"),
655 .export_options_type => return out_stream.writeAll("std.builtin.ExportOptions"),659 .export_options_type => return out_stream.writeAll("std.builtin.ExportOptions"),
656 .extern_options_type => return out_stream.writeAll("std.builtin.ExternOptions"),660 .extern_options_type => return out_stream.writeAll("std.builtin.ExternOptions"),
657 .type_info_type => return out_stream.writeAll("std.builtin.TypeInfo"),661 .type_info_type => return out_stream.writeAll("std.builtin.TypeInfo"),
...@@ -829,6 +833,7 @@ pub const Value = extern union {...@@ -829,6 +833,7 @@ pub const Value = extern union {
829 .float_mode_type => Type.initTag(.float_mode),833 .float_mode_type => Type.initTag(.float_mode),
830 .reduce_op_type => Type.initTag(.reduce_op),834 .reduce_op_type => Type.initTag(.reduce_op),
831 .call_options_type => Type.initTag(.call_options),835 .call_options_type => Type.initTag(.call_options),
836 .prefetch_options_type => Type.initTag(.prefetch_options),
832 .export_options_type => Type.initTag(.export_options),837 .export_options_type => Type.initTag(.export_options),
833 .extern_options_type => Type.initTag(.extern_options),838 .extern_options_type => Type.initTag(.extern_options),
834 .type_info_type => Type.initTag(.type_info),839 .type_info_type => Type.initTag(.type_info),