authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-01-18 10:49:54-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-01-18 11:59:09-07:00
log30efcf22d799c055d8ec987aa2f55a11c133b709
tree35febf72fdea7d3f571f01005823442701e02dd0
parentf423b5949b8722d4b290f57c3d06d015e39217b0

stage2: implement `@prefetch`

This reverts commit f423b5949b8722d4b290f57c3d06d015e39217b0, re-instating commit d48e4245b68bf25c7f41804a5012ac157a5ee546.

14 files changed, 287 insertions(+), 45 deletions(-)

lib/std/builtin.zig+2-2
......@@ -643,12 +643,12 @@ pub const PrefetchOptions = struct {
643643 /// The cache that the prefetch should be preformed on.
644644 cache: Cache = .data,
645645
646 pub const Rw = enum {
646 pub const Rw = enum(u1) {
647647 read,
648648 write,
649649 };
650650
651 pub const Cache = enum {
651 pub const Cache = enum(u1) {
652652 instruction,
653653 data,
654654 };
src/Air.zig+12
......@@ -515,6 +515,11 @@ pub const Inst = struct {
515515 /// is a `Ref`. Length of the array is given by the vector type.
516516 vector_init,
517517
518 /// Communicates an intent to load memory.
519 /// Result is always unused.
520 /// Uses the `prefetch` field.
521 prefetch,
522
518523 pub fn fromCmpOp(op: std.math.CompareOperator) Tag {
519524 return switch (op) {
520525 .lt => .cmp_lt,
......@@ -586,6 +591,12 @@ pub const Inst = struct {
586591 ptr: Ref,
587592 order: std.builtin.AtomicOrder,
588593 },
594 prefetch: struct {
595 ptr: Ref,
596 rw: std.builtin.PrefetchOptions.Rw,
597 locality: u2,
598 cache: std.builtin.PrefetchOptions.Cache,
599 },
589600
590601 // Make sure we don't accidentally add a field to make this union
591602 // bigger than expected. Note that in Debug builds, Zig is allowed
......@@ -823,6 +834,7 @@ pub fn typeOfIndex(air: Air, inst: Air.Inst.Index) Type {
823834 .memset,
824835 .memcpy,
825836 .set_union_tag,
837 .prefetch,
826838 => return Type.initTag(.void),
827839
828840 .ptrtoint,
src/Liveness.zig+5
......@@ -342,6 +342,11 @@ fn analyzeInst(
342342 return trackOperands(a, new_set, inst, main_tomb, .{ operand, .none, .none });
343343 },
344344
345 .prefetch => {
346 const prefetch = inst_datas[inst].prefetch;
347 return trackOperands(a, new_set, inst, main_tomb, .{ prefetch.ptr, .none, .none });
348 },
349
345350 .call => {
346351 const inst_data = inst_datas[inst].pl_op;
347352 const callee = inst_data.operand;
src/Sema.zig+82-38
......@@ -10316,49 +10316,53 @@ fn zirStructInitEmpty(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileE
1031610316 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
1031710317 const src = inst_data.src();
1031810318 const obj_ty = try sema.resolveType(block, src, inst_data.operand);
10319 const gpa = sema.gpa;
1032010319
1032110320 switch (obj_ty.zigTypeTag()) {
10322 .Struct => {
10323 // This logic must be synchronized with that in `zirStructInit`.
10324 const struct_ty = try sema.resolveTypeFields(block, src, obj_ty);
10325 const struct_obj = struct_ty.castTag(.@"struct").?.data;
10326
10327 // The init values to use for the struct instance.
10328 const field_inits = try gpa.alloc(Air.Inst.Ref, struct_obj.fields.count());
10329 defer gpa.free(field_inits);
10330
10331 var root_msg: ?*Module.ErrorMsg = null;
10332
10333 for (struct_obj.fields.values()) |field, i| {
10334 if (field.default_val.tag() == .unreachable_value) {
10335 const field_name = struct_obj.fields.keys()[i];
10336 const template = "missing struct field: {s}";
10337 const args = .{field_name};
10338 if (root_msg) |msg| {
10339 try sema.errNote(block, src, msg, template, args);
10340 } else {
10341 root_msg = try sema.errMsg(block, src, template, args);
10342 }
10343 } else {
10344 field_inits[i] = try sema.addConstant(field.ty, field.default_val);
10345 }
10346 }
10347 return sema.finishStructInit(block, src, field_inits, root_msg, struct_obj, struct_ty, false);
10348 },
10349 .Array => {
10350 if (obj_ty.sentinel()) |sentinel| {
10351 const val = try Value.Tag.empty_array_sentinel.create(sema.arena, sentinel);
10352 return sema.addConstant(obj_ty, val);
10353 } else {
10354 return sema.addConstant(obj_ty, Value.initTag(.empty_array));
10355 }
10356 },
10321 .Struct => return structInitEmpty(sema, block, obj_ty, src, src),
10322 .Array => return arrayInitEmpty(sema, obj_ty),
1035710323 .Void => return sema.addConstant(obj_ty, Value.void),
1035810324 else => unreachable,
1035910325 }
1036010326}
1036110327
10328fn structInitEmpty(sema: *Sema, block: *Block, obj_ty: Type, dest_src: LazySrcLoc, init_src: LazySrcLoc) CompileError!Air.Inst.Ref {
10329 const gpa = sema.gpa;
10330 // This logic must be synchronized with that in `zirStructInit`.
10331 const struct_ty = try sema.resolveTypeFields(block, dest_src, obj_ty);
10332 const struct_obj = struct_ty.castTag(.@"struct").?.data;
10333
10334 // The init values to use for the struct instance.
10335 const field_inits = try gpa.alloc(Air.Inst.Ref, struct_obj.fields.count());
10336 defer gpa.free(field_inits);
10337
10338 var root_msg: ?*Module.ErrorMsg = null;
10339
10340 for (struct_obj.fields.values()) |field, i| {
10341 if (field.default_val.tag() == .unreachable_value) {
10342 const field_name = struct_obj.fields.keys()[i];
10343 const template = "missing struct field: {s}";
10344 const args = .{field_name};
10345 if (root_msg) |msg| {
10346 try sema.errNote(block, init_src, msg, template, args);
10347 } else {
10348 root_msg = try sema.errMsg(block, init_src, template, args);
10349 }
10350 } else {
10351 field_inits[i] = try sema.addConstant(field.ty, field.default_val);
10352 }
10353 }
10354 return sema.finishStructInit(block, dest_src, field_inits, root_msg, struct_obj, struct_ty, false);
10355}
10356
10357fn arrayInitEmpty(sema: *Sema, obj_ty: Type) CompileError!Air.Inst.Ref {
10358 if (obj_ty.sentinel()) |sentinel| {
10359 const val = try Value.Tag.empty_array_sentinel.create(sema.arena, sentinel);
10360 return sema.addConstant(obj_ty, val);
10361 } else {
10362 return sema.addConstant(obj_ty, Value.initTag(.empty_array));
10363 }
10364}
10365
1036210366fn zirUnionInitPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
1036310367 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
1036410368 const src = inst_data.src();
......@@ -12326,8 +12330,38 @@ fn zirPrefetch(
1232612330 extended: Zir.Inst.Extended.InstData,
1232712331) CompileError!Air.Inst.Ref {
1232812332 const extra = sema.code.extraData(Zir.Inst.BinNode, extended.operand).data;
12329 const src: LazySrcLoc = .{ .node_offset = extra.node };
12330 return sema.fail(block, src, "TODO: implement Sema.zirPrefetch", .{});
12333 const ptr_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = extra.node };
12334 const opts_src: LazySrcLoc = .{ .node_offset_builtin_call_arg1 = extra.node };
12335 const options_ty = try sema.getBuiltinType(block, opts_src, "PrefetchOptions");
12336 const ptr = sema.resolveInst(extra.lhs);
12337 try sema.checkPtrType(block, ptr_src, sema.typeOf(ptr));
12338 const options = try sema.coerce(block, options_ty, sema.resolveInst(extra.rhs), opts_src);
12339
12340 const rw = try sema.fieldVal(block, opts_src, options, "rw", opts_src);
12341 const rw_val = try sema.resolveConstValue(block, opts_src, rw);
12342 const rw_tag = rw_val.toEnum(std.builtin.PrefetchOptions.Rw);
12343
12344 const locality = try sema.fieldVal(block, opts_src, options, "locality", opts_src);
12345 const locality_val = try sema.resolveConstValue(block, opts_src, locality);
12346 const locality_int = @intCast(u2, locality_val.toUnsignedInt());
12347
12348 const cache = try sema.fieldVal(block, opts_src, options, "cache", opts_src);
12349 const cache_val = try sema.resolveConstValue(block, opts_src, cache);
12350 const cache_tag = cache_val.toEnum(std.builtin.PrefetchOptions.Cache);
12351
12352 if (!block.is_comptime) {
12353 _ = try block.addInst(.{
12354 .tag = .prefetch,
12355 .data = .{ .prefetch = .{
12356 .ptr = ptr,
12357 .rw = rw_tag,
12358 .locality = locality_int,
12359 .cache = cache_tag,
12360 } },
12361 });
12362 }
12363
12364 return Air.Inst.Ref.void_value;
1233112365}
1233212366
1233312367fn zirBuiltinExtern(
......@@ -13787,6 +13821,11 @@ fn coerce(
1378713821 },
1378813822 .Array => switch (inst_ty.zigTypeTag()) {
1378913823 .Vector => return sema.coerceVectorInMemory(block, dest_ty, dest_ty_src, inst, inst_src),
13824 .Struct => {
13825 if (inst == .empty_struct) {
13826 return arrayInitEmpty(sema, dest_ty);
13827 }
13828 },
1379013829 else => {},
1379113830 },
1379213831 .Vector => switch (inst_ty.zigTypeTag()) {
......@@ -13794,6 +13833,11 @@ fn coerce(
1379413833 .Vector => return sema.coerceVectors(block, dest_ty, dest_ty_src, inst, inst_src),
1379513834 else => {},
1379613835 },
13836 .Struct => {
13837 if (inst == .empty_struct) {
13838 return structInitEmpty(sema, block, dest_ty, dest_ty_src, inst_src);
13839 }
13840 },
1379713841 else => {},
1379813842 }
1379913843
src/arch/aarch64/CodeGen.zig+6
......@@ -595,6 +595,7 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void {
595595 .error_name => try self.airErrorName(inst),
596596 .splat => try self.airSplat(inst),
597597 .vector_init => try self.airVectorInit(inst),
598 .prefetch => try self.airPrefetch(inst),
598599
599600 .atomic_store_unordered => try self.airAtomicStore(inst, .Unordered),
600601 .atomic_store_monotonic => try self.airAtomicStore(inst, .Monotonic),
......@@ -2597,6 +2598,11 @@ fn airVectorInit(self: *Self, inst: Air.Inst.Index) !void {
25972598 return bt.finishAir(result);
25982599}
25992600
2601fn airPrefetch(self: *Self, inst: Air.Inst.Index) !void {
2602 const prefetch = self.air.instructions.items(.data)[inst].prefetch;
2603 return self.finishAir(inst, MCValue.dead, .{ prefetch.ptr, .none, .none });
2604}
2605
26002606fn resolveInst(self: *Self, inst: Air.Inst.Ref) InnerError!MCValue {
26012607 // First section of indexes correspond to a set number of constant values.
26022608 const ref_int = @enumToInt(inst);
src/arch/arm/CodeGen.zig+6
......@@ -586,6 +586,7 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void {
586586 .error_name => try self.airErrorName(inst),
587587 .splat => try self.airSplat(inst),
588588 .vector_init => try self.airVectorInit(inst),
589 .prefetch => try self.airPrefetch(inst),
589590
590591 .atomic_store_unordered => try self.airAtomicStore(inst, .Unordered),
591592 .atomic_store_monotonic => try self.airAtomicStore(inst, .Monotonic),
......@@ -3693,6 +3694,11 @@ fn airVectorInit(self: *Self, inst: Air.Inst.Index) !void {
36933694 return bt.finishAir(result);
36943695}
36953696
3697fn airPrefetch(self: *Self, inst: Air.Inst.Index) !void {
3698 const prefetch = self.air.instructions.items(.data)[inst].prefetch;
3699 return self.finishAir(inst, MCValue.dead, .{ prefetch.ptr, .none, .none });
3700}
3701
36963702fn resolveInst(self: *Self, inst: Air.Inst.Ref) InnerError!MCValue {
36973703 // First section of indexes correspond to a set number of constant values.
36983704 const ref_int = @enumToInt(inst);
src/arch/riscv64/CodeGen.zig+6
......@@ -574,6 +574,7 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void {
574574 .error_name => try self.airErrorName(inst),
575575 .splat => try self.airSplat(inst),
576576 .vector_init => try self.airVectorInit(inst),
577 .prefetch => try self.airPrefetch(inst),
577578
578579 .atomic_store_unordered => try self.airAtomicStore(inst, .Unordered),
579580 .atomic_store_monotonic => try self.airAtomicStore(inst, .Monotonic),
......@@ -2096,6 +2097,11 @@ fn airVectorInit(self: *Self, inst: Air.Inst.Index) !void {
20962097 return bt.finishAir(result);
20972098}
20982099
2100fn airPrefetch(self: *Self, inst: Air.Inst.Index) !void {
2101 const prefetch = self.air.instructions.items(.data)[inst].prefetch;
2102 return self.finishAir(inst, MCValue.dead, .{ prefetch.ptr, .none, .none });
2103}
2104
20992105fn resolveInst(self: *Self, inst: Air.Inst.Ref) InnerError!MCValue {
21002106 // First section of indexes correspond to a set number of constant values.
21012107 const ref_int = @enumToInt(inst);
src/arch/wasm/CodeGen.zig+63-4
......@@ -1297,6 +1297,9 @@ fn copyLocal(self: *Self, value: WValue, ty: Type) InnerError!WValue {
12971297fn genInst(self: *Self, inst: Air.Inst.Index) !WValue {
12981298 const air_tags = self.air.instructions.items(.tag);
12991299 return switch (air_tags[inst]) {
1300 .constant => unreachable,
1301 .const_ty => unreachable,
1302
13001303 .add => self.airBinOp(inst, .add),
13011304 .addwrap => self.airWrapBinOp(inst, .add),
13021305 .sub => self.airBinOp(inst, .sub),
......@@ -1330,7 +1333,6 @@ fn genInst(self: *Self, inst: Air.Inst.Index) !WValue {
13301333 .bool_to_int => self.airBoolToInt(inst),
13311334 .call => self.airCall(inst),
13321335 .cond_br => self.airCondBr(inst),
1333 .constant => unreachable,
13341336 .dbg_stmt => WValue.none,
13351337 .intcast => self.airIntcast(inst),
13361338 .float_to_int => self.airFloatToInt(inst),
......@@ -1358,6 +1360,9 @@ fn genInst(self: *Self, inst: Air.Inst.Index) !WValue {
13581360 .ret => self.airRet(inst),
13591361 .ret_ptr => self.airRetPtr(inst),
13601362 .ret_load => self.airRetLoad(inst),
1363 .splat => self.airSplat(inst),
1364 .vector_init => self.airVectorInit(inst),
1365 .prefetch => self.airPrefetch(inst),
13611366
13621367 .slice => self.airSlice(inst),
13631368 .slice_len => self.airSliceLen(inst),
......@@ -1382,7 +1387,55 @@ fn genInst(self: *Self, inst: Air.Inst.Index) !WValue {
13821387 .unwrap_errunion_err => self.airUnwrapErrUnionError(inst),
13831388 .wrap_errunion_payload => self.airWrapErrUnionPayload(inst),
13841389 .wrap_errunion_err => self.airWrapErrUnionErr(inst),
1385 else => |tag| self.fail("TODO: Implement wasm inst: {s}", .{@tagName(tag)}),
1390
1391 .add_sat,
1392 .sub_sat,
1393 .mul_sat,
1394 .div_float,
1395 .div_floor,
1396 .div_exact,
1397 .rem,
1398 .mod,
1399 .max,
1400 .min,
1401 .assembly,
1402 .shl_exact,
1403 .shl_sat,
1404 .ret_addr,
1405 .clz,
1406 .ctz,
1407 .popcount,
1408 .is_err_ptr,
1409 .is_non_err_ptr,
1410 .fptrunc,
1411 .fpext,
1412 .unwrap_errunion_payload_ptr,
1413 .unwrap_errunion_err_ptr,
1414 .set_union_tag,
1415 .get_union_tag,
1416 .ptr_slice_len_ptr,
1417 .ptr_slice_ptr_ptr,
1418 .int_to_float,
1419 .memcpy,
1420 .cmpxchg_weak,
1421 .cmpxchg_strong,
1422 .fence,
1423 .atomic_load,
1424 .atomic_store_unordered,
1425 .atomic_store_monotonic,
1426 .atomic_store_release,
1427 .atomic_store_seq_cst,
1428 .atomic_rmw,
1429 .tag_name,
1430 .error_name,
1431
1432 // For these 4, probably best to wait until https://github.com/ziglang/zig/issues/10248
1433 // is implemented in the frontend before implementing them here in the wasm backend.
1434 .add_with_overflow,
1435 .sub_with_overflow,
1436 .mul_with_overflow,
1437 .shl_with_overflow,
1438 => |tag| self.fail("TODO: Implement wasm inst: {s}", .{@tagName(tag)}),
13861439 };
13871440}
13881441
......@@ -3211,7 +3264,7 @@ fn airFloatToInt(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
32113264 return result;
32123265}
32133266
3214fn airSplat(self: *Self, inst: Air.Inst.Index) !void {
3267fn airSplat(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
32153268 if (self.liveness.isUnused(inst)) return WValue{ .none = {} };
32163269
32173270 const ty_op = self.air.instructions.items(.data)[inst].ty_op;
......@@ -3222,7 +3275,7 @@ fn airSplat(self: *Self, inst: Air.Inst.Index) !void {
32223275 return self.fail("TODO: Implement wasm airSplat", .{});
32233276}
32243277
3225fn airVectorInit(self: *Self, inst: Air.Inst.Index) !void {
3278fn airVectorInit(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
32263279 if (self.liveness.isUnused(inst)) return WValue{ .none = {} };
32273280
32283281 const vector_ty = self.air.typeOfIndex(inst);
......@@ -3234,6 +3287,12 @@ fn airVectorInit(self: *Self, inst: Air.Inst.Index) !void {
32343287 return self.fail("TODO: Wasm backend: implement airVectorInit", .{});
32353288}
32363289
3290fn airPrefetch(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
3291 const prefetch = self.air.instructions.items(.data)[inst].prefetch;
3292 _ = prefetch;
3293 return WValue{ .none = {} };
3294}
3295
32373296fn cmpOptionals(self: *Self, lhs: WValue, rhs: WValue, operand_ty: Type, op: std.math.CompareOperator) InnerError!WValue {
32383297 assert(operand_ty.hasCodeGenBits());
32393298 assert(op == .eq or op == .neq);
src/arch/x86_64/CodeGen.zig+6
......@@ -638,6 +638,7 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void {
638638 .error_name => try self.airErrorName(inst),
639639 .splat => try self.airSplat(inst),
640640 .vector_init => try self.airVectorInit(inst),
641 .prefetch => try self.airPrefetch(inst),
641642
642643 .atomic_store_unordered => try self.airAtomicStore(inst, .Unordered),
643644 .atomic_store_monotonic => try self.airAtomicStore(inst, .Monotonic),
......@@ -3746,6 +3747,11 @@ fn airVectorInit(self: *Self, inst: Air.Inst.Index) !void {
37463747 return bt.finishAir(result);
37473748}
37483749
3750fn airPrefetch(self: *Self, inst: Air.Inst.Index) !void {
3751 const prefetch = self.air.instructions.items(.data)[inst].prefetch;
3752 return self.finishAir(inst, MCValue.dead, .{ prefetch.ptr, .none, .none });
3753}
3754
37493755fn resolveInst(self: *Self, inst: Air.Inst.Ref) InnerError!MCValue {
37503756 // First section of indexes correspond to a set number of constant values.
37513757 const ref_int = @enumToInt(inst);
src/codegen/c.zig+20
......@@ -1278,6 +1278,7 @@ fn genBody(f: *Function, body: []const Air.Inst.Index) error{ AnalysisFail, OutO
12781278 .error_name => try airErrorName(f, inst),
12791279 .splat => try airSplat(f, inst),
12801280 .vector_init => try airVectorInit(f, inst),
1281 .prefetch => try airPrefetch(f, inst),
12811282
12821283 .int_to_float,
12831284 .float_to_int,
......@@ -3089,6 +3090,25 @@ fn airVectorInit(f: *Function, inst: Air.Inst.Index) !CValue {
30893090 return f.fail("TODO: C backend: implement airVectorInit", .{});
30903091}
30913092
3093fn airPrefetch(f: *Function, inst: Air.Inst.Index) !CValue {
3094 const prefetch = f.air.instructions.items(.data)[inst].prefetch;
3095 switch (prefetch.cache) {
3096 .data => {},
3097 // The available prefetch intrinsics do not accept a cache argument; only
3098 // address, rw, and locality. So unless the cache is data, we do not lower
3099 // this instruction.
3100 .instruction => return CValue.none,
3101 }
3102 const ptr = try f.resolveInst(prefetch.ptr);
3103 const writer = f.object.writer();
3104 try writer.writeAll("zig_prefetch(");
3105 try f.writeCValue(writer, ptr);
3106 try writer.print(", {d}, {d});\n", .{
3107 @enumToInt(prefetch.rw), prefetch.locality,
3108 });
3109 return CValue.none;
3110}
3111
30923112fn toMemoryOrder(order: std.builtin.AtomicOrder) [:0]const u8 {
30933113 return switch (order) {
30943114 .Unordered => "memory_order_relaxed",
src/codegen/llvm.zig+62
......@@ -2073,6 +2073,7 @@ pub const FuncGen = struct {
20732073 .error_name => try self.airErrorName(inst),
20742074 .splat => try self.airSplat(inst),
20752075 .vector_init => try self.airVectorInit(inst),
2076 .prefetch => try self.airPrefetch(inst),
20762077
20772078 .atomic_store_unordered => try self.airAtomicStore(inst, .Unordered),
20782079 .atomic_store_monotonic => try self.airAtomicStore(inst, .Monotonic),
......@@ -4384,6 +4385,67 @@ pub const FuncGen = struct {
43844385 return vector;
43854386 }
43864387
4388 fn airPrefetch(self: *FuncGen, inst: Air.Inst.Index) !?*const llvm.Value {
4389 const prefetch = self.air.instructions.items(.data)[inst].prefetch;
4390
4391 comptime assert(@enumToInt(std.builtin.PrefetchOptions.Rw.read) == 0);
4392 comptime assert(@enumToInt(std.builtin.PrefetchOptions.Rw.write) == 1);
4393
4394 // TODO these two asserts should be able to be comptime because the type is a u2
4395 assert(prefetch.locality >= 0);
4396 assert(prefetch.locality <= 3);
4397
4398 comptime assert(@enumToInt(std.builtin.PrefetchOptions.Cache.instruction) == 0);
4399 comptime assert(@enumToInt(std.builtin.PrefetchOptions.Cache.data) == 1);
4400
4401 // LLVM fails during codegen of instruction cache prefetchs for these architectures.
4402 // This is an LLVM bug as the prefetch intrinsic should be a noop if not supported
4403 // by the target.
4404 // To work around this, don't emit llvm.prefetch in this case.
4405 // See https://bugs.llvm.org/show_bug.cgi?id=21037
4406 const target = self.dg.module.getTarget();
4407 switch (prefetch.cache) {
4408 .instruction => switch (target.cpu.arch) {
4409 .x86_64, .i386 => return null,
4410 .arm, .armeb, .thumb, .thumbeb => {
4411 switch (prefetch.rw) {
4412 .write => return null,
4413 else => {},
4414 }
4415 },
4416 else => {},
4417 },
4418 .data => {},
4419 }
4420
4421 const llvm_u8 = self.context.intType(8);
4422 const llvm_ptr_u8 = llvm_u8.pointerType(0);
4423 const llvm_u32 = self.context.intType(32);
4424
4425 const llvm_fn_name = "llvm.prefetch.p0i8";
4426 const fn_val = self.dg.object.llvm_module.getNamedFunction(llvm_fn_name) orelse blk: {
4427 // declare void @llvm.prefetch(i8*, i32, i32, i32)
4428 const llvm_void = self.context.voidType();
4429 const param_types = [_]*const llvm.Type{
4430 llvm_ptr_u8, llvm_u32, llvm_u32, llvm_u32,
4431 };
4432 const fn_type = llvm.functionType(llvm_void, &param_types, param_types.len, .False);
4433 break :blk self.dg.object.llvm_module.addFunction(llvm_fn_name, fn_type);
4434 };
4435
4436 const ptr = try self.resolveInst(prefetch.ptr);
4437 const ptr_u8 = self.builder.buildBitCast(ptr, llvm_ptr_u8, "");
4438
4439 const params = [_]*const llvm.Value{
4440 ptr_u8,
4441 llvm_u32.constInt(@enumToInt(prefetch.rw), .False),
4442 llvm_u32.constInt(prefetch.locality, .False),
4443 llvm_u32.constInt(@enumToInt(prefetch.cache), .False),
4444 };
4445 _ = self.builder.buildCall(fn_val, &params, params.len, .C, .Auto, "");
4446 return null;
4447 }
4448
43874449 fn getErrorNameTable(self: *FuncGen) !*const llvm.Value {
43884450 if (self.dg.object.error_name_table) |table| {
43894451 return table;
src/link/C/zig.h+6
......@@ -74,6 +74,12 @@
7474#define zig_frame_address() 0
7575#endif
7676
77#if defined(__GNUC__)
78#define zig_prefetch(addr, rw, locality) __builtin_prefetch(addr, rw, locality)
79#else
80#define zig_prefetch(addr, rw, locality)
81#endif
82
7783#if __STDC_VERSION__ >= 201112L && !defined(__STDC_NO_ATOMICS__)
7884#include <stdatomic.h>
7985#define zig_cmpxchg_strong(obj, expected, desired, succ, fail) atomic_compare_exchange_strong_explicit(obj, &(expected), desired, succ, fail)
src/print_air.zig+10
......@@ -226,6 +226,7 @@ const Writer = struct {
226226 .cmpxchg_weak, .cmpxchg_strong => try w.writeCmpxchg(s, inst),
227227 .fence => try w.writeFence(s, inst),
228228 .atomic_load => try w.writeAtomicLoad(s, inst),
229 .prefetch => try w.writePrefetch(s, inst),
229230 .atomic_store_unordered => try w.writeAtomicStore(s, inst, .Unordered),
230231 .atomic_store_monotonic => try w.writeAtomicStore(s, inst, .Monotonic),
231232 .atomic_store_release => try w.writeAtomicStore(s, inst, .Release),
......@@ -350,6 +351,15 @@ const Writer = struct {
350351 try s.print(", {s}", .{@tagName(atomic_load.order)});
351352 }
352353
354 fn writePrefetch(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void {
355 const prefetch = w.air.instructions.items(.data)[inst].prefetch;
356
357 try w.writeOperand(s, inst, 0, prefetch.ptr);
358 try s.print(", {s}, {d}, {s}", .{
359 @tagName(prefetch.rw), prefetch.locality, @tagName(prefetch.cache),
360 });
361 }
362
353363 fn writeAtomicStore(
354364 w: *Writer,
355365 s: anytype,
test/behavior.zig+1-1
......@@ -10,6 +10,7 @@ test {
1010 _ = @import("behavior/fn_in_struct_in_comptime.zig");
1111 _ = @import("behavior/hasdecl.zig");
1212 _ = @import("behavior/hasfield.zig");
13 _ = @import("behavior/prefetch.zig");
1314 _ = @import("behavior/pub_enum.zig");
1415 _ = @import("behavior/type_info.zig");
1516 _ = @import("behavior/type.zig");
......@@ -178,7 +179,6 @@ test {
178179 _ = @import("behavior/optional_stage1.zig");
179180 _ = @import("behavior/pointers_stage1.zig");
180181 _ = @import("behavior/popcount_stage1.zig");
181 _ = @import("behavior/prefetch.zig");
182182 _ = @import("behavior/ptrcast_stage1.zig");
183183 _ = @import("behavior/reflection.zig");
184184 _ = @import("behavior/saturating_arithmetic_stage1.zig");