Skip to content

Commit 8a10dbc

Browse files
committed
AstGen: implement @prefetch() builtin
1 parent ec2725e commit 8a10dbc

File tree

8 files changed

+90
-1
lines changed

8 files changed

+90
-1
lines changed

lib/std/builtin.zig

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,31 @@ pub const CallOptions = struct {
651651
};
652652
};
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.
656+
pub 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+
654679
/// This data structure is used by the Zig language code generation and
655680
/// therefore must be kept in sync with the compiler implementation.
656681
pub const ExportOptions = struct {

src/AstGen.zig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7226,6 +7226,16 @@ fn builtinCall(
72267226
});
72277227
return rvalue(gz, rl, result, node);
72287228
},
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+
},
72297239
}
72307240
}
72317241

src/BuiltinFn.zig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ pub const Tag = enum {
6767
mul_with_overflow,
6868
panic,
6969
pop_count,
70+
prefetch,
7071
ptr_cast,
7172
ptr_to_int,
7273
rem,
@@ -615,6 +616,13 @@ pub const list = list: {
615616
.param_count = 2,
616617
},
617618
},
619+
.{
620+
"@prefetch",
621+
.{
622+
.tag = .prefetch,
623+
.param_count = 2,
624+
},
625+
},
618626
.{
619627
"@ptrCast",
620628
.{

src/Sema.zig

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,6 +1042,7 @@ fn zirExtended(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
10421042
.c_define => return sema.zirCDefine( block, extended),
10431043
.wasm_memory_size => return sema.zirWasmMemorySize( block, extended),
10441044
.wasm_memory_grow => return sema.zirWasmMemoryGrow( block, extended),
1045+
.prefetch => return sema.zirPrefetch( block, extended),
10451046
// zig fmt: on
10461047
}
10471048
}
@@ -11104,6 +11105,16 @@ fn zirWasmMemoryGrow(
1110411105
return sema.fail(block, src, "TODO: implement Sema.zirWasmMemoryGrow", .{});
1110511106
}
1110611107

11108+
fn 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+
1110711118
fn zirBuiltinExtern(
1110811119
sema: *Sema,
1110911120
block: *Block,
@@ -14231,6 +14242,7 @@ fn resolveTypeFields(sema: *Sema, block: *Block, src: LazySrcLoc, ty: Type) Comp
1423114242
.float_mode => return sema.resolveBuiltinTypeFields(block, src, "FloatMode"),
1423214243
.reduce_op => return sema.resolveBuiltinTypeFields(block, src, "ReduceOp"),
1423314244
.call_options => return sema.resolveBuiltinTypeFields(block, src, "CallOptions"),
14245+
.prefetch_options => return sema.resolveBuiltinTypeFields(block, src, "PrefetchOptions"),
1423414246

1423514247
.@"union", .union_tagged => {
1423614248
const union_obj = ty.cast(Type.Payload.Union).?.data;
@@ -14819,6 +14831,7 @@ fn typeHasOnePossibleValue(
1481914831
.float_mode,
1482014832
.reduce_op,
1482114833
.call_options,
14834+
.prefetch_options,
1482214835
.export_options,
1482314836
.extern_options,
1482414837
.type_info,
@@ -15032,6 +15045,7 @@ pub fn addType(sema: *Sema, ty: Type) !Air.Inst.Ref {
1503215045
.float_mode => return .float_mode_type,
1503315046
.reduce_op => return .reduce_op_type,
1503415047
.call_options => return .call_options_type,
15048+
.prefetch_options => return .prefetch_options_type,
1503515049
.export_options => return .export_options_type,
1503615050
.extern_options => return .extern_options_type,
1503715051
.type_info => return .type_info_type,

src/Zir.zig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1572,6 +1572,9 @@ pub const Inst = struct {
15721572
wasm_memory_size,
15731573
/// `operand` is payload index to `BinNode`.
15741574
wasm_memory_grow,
1575+
/// The `@prefetch` builtin.
1576+
/// `operand` is payload index to `BinNode`.
1577+
prefetch,
15751578

15761579
pub const InstData = struct {
15771580
opcode: Extended,
@@ -1648,6 +1651,7 @@ pub const Inst = struct {
16481651
float_mode_type,
16491652
reduce_op_type,
16501653
call_options_type,
1654+
prefetch_options_type,
16511655
export_options_type,
16521656
extern_options_type,
16531657
type_info_type,
@@ -1917,6 +1921,10 @@ pub const Inst = struct {
19171921
.ty = Type.initTag(.type),
19181922
.val = Value.initTag(.call_options_type),
19191923
},
1924+
.prefetch_options_type = .{
1925+
.ty = Type.initTag(.type),
1926+
.val = Value.initTag(.prefetch_options_type),
1927+
},
19201928
.export_options_type = .{
19211929
.ty = Type.initTag(.type),
19221930
.val = Value.initTag(.export_options_type),

src/print_zir.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ const Writer = struct {
477477
try self.writeSrc(stream, src);
478478
},
479479

480-
.builtin_extern, .c_define, .wasm_memory_grow => {
480+
.builtin_extern, .c_define, .wasm_memory_grow, .prefetch => {
481481
const inst_data = self.code.extraData(Zir.Inst.BinNode, extended.operand).data;
482482
const src: LazySrcLoc = .{ .node_offset = inst_data.node };
483483
try self.writeInstRef(stream, inst_data.lhs);

src/type.zig

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ pub const Type = extern union {
123123
.empty_struct_literal,
124124
.@"struct",
125125
.call_options,
126+
.prefetch_options,
126127
.export_options,
127128
.extern_options,
128129
=> return .Struct,
@@ -798,6 +799,7 @@ pub const Type = extern union {
798799
.float_mode,
799800
.reduce_op,
800801
.call_options,
802+
.prefetch_options,
801803
.export_options,
802804
.extern_options,
803805
.type_info,
@@ -1027,6 +1029,7 @@ pub const Type = extern union {
10271029
.float_mode => return writer.writeAll("std.builtin.FloatMode"),
10281030
.reduce_op => return writer.writeAll("std.builtin.ReduceOp"),
10291031
.call_options => return writer.writeAll("std.builtin.CallOptions"),
1032+
.prefetch_options => return writer.writeAll("std.builtin.PrefetchOptions"),
10301033
.export_options => return writer.writeAll("std.builtin.ExportOptions"),
10311034
.extern_options => return writer.writeAll("std.builtin.ExternOptions"),
10321035
.type_info => return writer.writeAll("std.builtin.TypeInfo"),
@@ -1318,6 +1321,7 @@ pub const Type = extern union {
13181321
.float_mode => return "FloatMode",
13191322
.reduce_op => return "ReduceOp",
13201323
.call_options => return "CallOptions",
1324+
.prefetch_options => return "PrefetchOptions",
13211325
.export_options => return "ExportOptions",
13221326
.extern_options => return "ExternOptions",
13231327
.type_info => return "TypeInfo",
@@ -1376,6 +1380,7 @@ pub const Type = extern union {
13761380
.float_mode,
13771381
.reduce_op,
13781382
.call_options,
1383+
.prefetch_options,
13791384
.export_options,
13801385
.extern_options,
13811386
.manyptr_u8,
@@ -1502,6 +1507,7 @@ pub const Type = extern union {
15021507
.float_mode => return Value.initTag(.float_mode_type),
15031508
.reduce_op => return Value.initTag(.reduce_op_type),
15041509
.call_options => return Value.initTag(.call_options_type),
1510+
.prefetch_options => return Value.initTag(.prefetch_options_type),
15051511
.export_options => return Value.initTag(.export_options_type),
15061512
.extern_options => return Value.initTag(.extern_options_type),
15071513
.type_info => return Value.initTag(.type_info_type),
@@ -1563,6 +1569,7 @@ pub const Type = extern union {
15631569
.float_mode,
15641570
.reduce_op,
15651571
.call_options,
1572+
.prefetch_options,
15661573
.export_options,
15671574
.extern_options,
15681575
.@"anyframe",
@@ -1750,6 +1757,7 @@ pub const Type = extern union {
17501757
.float_mode,
17511758
.reduce_op,
17521759
.call_options,
1760+
.prefetch_options,
17531761
.export_options,
17541762
.extern_options,
17551763
=> return 1,
@@ -1929,6 +1937,7 @@ pub const Type = extern union {
19291937
.var_args_param => unreachable,
19301938
.generic_poison => unreachable,
19311939
.call_options => unreachable, // missing call to resolveTypeFields
1940+
.prefetch_options => unreachable, // missing call to resolveTypeFields
19321941
.export_options => unreachable, // missing call to resolveTypeFields
19331942
.extern_options => unreachable, // missing call to resolveTypeFields
19341943
.type_info => unreachable, // missing call to resolveTypeFields
@@ -2269,6 +2278,7 @@ pub const Type = extern union {
22692278
.float_mode,
22702279
.reduce_op,
22712280
.call_options,
2281+
.prefetch_options,
22722282
.export_options,
22732283
.extern_options,
22742284
.type_info,
@@ -2794,6 +2804,7 @@ pub const Type = extern union {
27942804
.float_mode,
27952805
.reduce_op,
27962806
.call_options,
2807+
.prefetch_options,
27972808
.export_options,
27982809
.extern_options,
27992810
.type_info,
@@ -3294,6 +3305,7 @@ pub const Type = extern union {
32943305
.float_mode,
32953306
.reduce_op,
32963307
.call_options,
3308+
.prefetch_options,
32973309
.export_options,
32983310
.extern_options,
32993311
.type_info,
@@ -3502,6 +3514,7 @@ pub const Type = extern union {
35023514
.float_mode,
35033515
.reduce_op,
35043516
.call_options,
3517+
.prefetch_options,
35053518
.export_options,
35063519
.extern_options,
35073520
=> @panic("TODO resolve std.builtin types"),
@@ -3577,6 +3590,7 @@ pub const Type = extern union {
35773590
.float_mode,
35783591
.reduce_op,
35793592
.call_options,
3593+
.prefetch_options,
35803594
.export_options,
35813595
.extern_options,
35823596
=> @panic("TODO resolve std.builtin types"),
@@ -3701,6 +3715,7 @@ pub const Type = extern union {
37013715
.float_mode,
37023716
.reduce_op,
37033717
.call_options,
3718+
.prefetch_options,
37043719
.export_options,
37053720
.extern_options,
37063721
.type_info,
@@ -3741,6 +3756,7 @@ pub const Type = extern union {
37413756
.float_mode,
37423757
.reduce_op,
37433758
.call_options,
3759+
.prefetch_options,
37443760
.export_options,
37453761
.extern_options,
37463762
.type_info,
@@ -3801,6 +3817,7 @@ pub const Type = extern union {
38013817
.float_mode,
38023818
.reduce_op,
38033819
.call_options,
3820+
.prefetch_options,
38043821
.export_options,
38053822
.extern_options,
38063823
=> @panic("TODO resolve std.builtin types"),
@@ -3862,6 +3879,7 @@ pub const Type = extern union {
38623879
float_mode,
38633880
reduce_op,
38643881
call_options,
3882+
prefetch_options,
38653883
export_options,
38663884
extern_options,
38673885
type_info,
@@ -3989,6 +4007,7 @@ pub const Type = extern union {
39894007
.float_mode,
39904008
.reduce_op,
39914009
.call_options,
4010+
.prefetch_options,
39924011
.export_options,
39934012
.extern_options,
39944013
.type_info,

src/value.zig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ pub const Value = extern union {
6767
float_mode_type,
6868
reduce_op_type,
6969
call_options_type,
70+
prefetch_options_type,
7071
export_options_type,
7172
extern_options_type,
7273
type_info_type,
@@ -244,6 +245,7 @@ pub const Value = extern union {
244245
.float_mode_type,
245246
.reduce_op_type,
246247
.call_options_type,
248+
.prefetch_options_type,
247249
.export_options_type,
248250
.extern_options_type,
249251
.type_info_type,
@@ -434,6 +436,7 @@ pub const Value = extern union {
434436
.float_mode_type,
435437
.reduce_op_type,
436438
.call_options_type,
439+
.prefetch_options_type,
437440
.export_options_type,
438441
.extern_options_type,
439442
.type_info_type,
@@ -652,6 +655,7 @@ pub const Value = extern union {
652655
.float_mode_type => return out_stream.writeAll("std.builtin.FloatMode"),
653656
.reduce_op_type => return out_stream.writeAll("std.builtin.ReduceOp"),
654657
.call_options_type => return out_stream.writeAll("std.builtin.CallOptions"),
658+
.prefetch_options_type => return out_stream.writeAll("std.builtin.PrefetchOptions"),
655659
.export_options_type => return out_stream.writeAll("std.builtin.ExportOptions"),
656660
.extern_options_type => return out_stream.writeAll("std.builtin.ExternOptions"),
657661
.type_info_type => return out_stream.writeAll("std.builtin.TypeInfo"),
@@ -829,6 +833,7 @@ pub const Value = extern union {
829833
.float_mode_type => Type.initTag(.float_mode),
830834
.reduce_op_type => Type.initTag(.reduce_op),
831835
.call_options_type => Type.initTag(.call_options),
836+
.prefetch_options_type => Type.initTag(.prefetch_options),
832837
.export_options_type => Type.initTag(.export_options),
833838
.extern_options_type => Type.initTag(.extern_options),
834839
.type_info_type => Type.initTag(.type_info),

0 commit comments

Comments
 (0)