Skip to content

Commit fd6b3db

Browse files
authored
Merge pull request #17293 from Snektron/spirv-aaaa
spirv: more instructions
2 parents a1e0b99 + 8c15322 commit fd6b3db

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

96 files changed

+1136
-764
lines changed

.github/CODEOWNERS

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Autodoc
1+
# Autodoc
22
/src/Autodoc.zig @kristoff-it
33
/src/autodoc/* @kristoff-it
44
/lib/docs/* @kristoff-it
@@ -11,3 +11,6 @@
1111

1212
# resinator
1313
/src/resinator/* @squeek502
14+
15+
# SPIR-V selfhosted backend
16+
/src/codegen/spirv* @Snektron

lib/std/testing.zig

+12-4
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,15 @@ pub var base_allocator_instance = std.heap.FixedBufferAllocator.init("");
2121
/// TODO https://github.com/ziglang/zig/issues/5738
2222
pub var log_level = std.log.Level.warn;
2323

24-
fn print(comptime fmt: []const u8, args: anytype) void {
25-
// Disable printing in tests for simple backends.
26-
if (builtin.zig_backend == .stage2_spirv64) return;
24+
// Disable printing in tests for simple backends.
25+
pub const backend_can_print = builtin.zig_backend != .stage2_spirv64;
2726

28-
std.debug.print(fmt, args);
27+
fn print(comptime fmt: []const u8, args: anytype) void {
28+
if (@inComptime()) {
29+
@compileError(std.fmt.comptimePrint(fmt, args));
30+
} else if (backend_can_print) {
31+
std.debug.print(fmt, args);
32+
}
2933
}
3034

3135
/// This function is intended to be used only in tests. It prints diagnostics to stderr
@@ -300,6 +304,10 @@ pub fn expectEqualSlices(comptime T: type, expected: []const T, actual: []const
300304
break :diff_index if (expected.len == actual.len) return else shortest;
301305
};
302306

307+
if (!backend_can_print) {
308+
return error.TestExpectedEqual;
309+
}
310+
303311
print("slices differ. first difference occurs at index {d} (0x{X})\n", .{ diff_index, diff_index });
304312

305313
// TODO: Should this be configurable by the caller?

src/InternPool.zig

+10-5
Original file line numberDiff line numberDiff line change
@@ -1556,20 +1556,25 @@ pub const Key = union(enum) {
15561556
// These are strange: we'll sometimes represent them as f128, even if the
15571557
// underlying type is smaller. f80 is an exception: see float_c_longdouble_f80.
15581558
const a_val = switch (a_info.storage) {
1559-
inline else => |val| @as(f128, @floatCast(val)),
1559+
inline else => |val| @as(u128, @bitCast(@as(f128, @floatCast(val)))),
15601560
};
15611561
const b_val = switch (b_info.storage) {
1562-
inline else => |val| @as(f128, @floatCast(val)),
1562+
inline else => |val| @as(u128, @bitCast(@as(f128, @floatCast(val)))),
15631563
};
15641564
return a_val == b_val;
15651565
}
15661566

15671567
const StorageTag = @typeInfo(Key.Float.Storage).Union.tag_type.?;
15681568
assert(@as(StorageTag, a_info.storage) == @as(StorageTag, b_info.storage));
15691569

1570-
return switch (a_info.storage) {
1571-
inline else => |val, tag| val == @field(b_info.storage, @tagName(tag)),
1572-
};
1570+
switch (a_info.storage) {
1571+
inline else => |val, tag| {
1572+
const Bits = std.meta.Int(.unsigned, @bitSizeOf(@TypeOf(val)));
1573+
const a_bits: Bits = @bitCast(val);
1574+
const b_bits: Bits = @bitCast(@field(b_info.storage, @tagName(tag)));
1575+
return a_bits == b_bits;
1576+
},
1577+
}
15731578
},
15741579

15751580
.opaque_type => |a_info| {

src/arch/wasm/CodeGen.zig

+21-7
Original file line numberDiff line numberDiff line change
@@ -6253,8 +6253,10 @@ fn airMulWithOverflow(func: *CodeGen, inst: Air.Inst.Index) InnerError!void {
62536253
func.finishAir(inst, result_ptr, &.{ extra.lhs, extra.rhs });
62546254
}
62556255

6256-
fn airMaxMin(func: *CodeGen, inst: Air.Inst.Index, op: enum { max, min }) InnerError!void {
6256+
fn airMaxMin(func: *CodeGen, inst: Air.Inst.Index, op: Op) InnerError!void {
6257+
assert(op == .max or op == .min);
62576258
const mod = func.bin_file.base.options.module.?;
6259+
const target = mod.getTarget();
62586260
const bin_op = func.air.instructions.items(.data)[inst].bin_op;
62596261

62606262
const ty = func.typeOfIndex(inst);
@@ -6269,13 +6271,25 @@ fn airMaxMin(func: *CodeGen, inst: Air.Inst.Index, op: enum { max, min }) InnerE
62696271
const lhs = try func.resolveInst(bin_op.lhs);
62706272
const rhs = try func.resolveInst(bin_op.rhs);
62716273

6272-
// operands to select from
6273-
try func.lowerToStack(lhs);
6274-
try func.lowerToStack(rhs);
6275-
_ = try func.cmp(lhs, rhs, ty, if (op == .max) .gt else .lt);
6274+
if (ty.zigTypeTag(mod) == .Float) {
6275+
var fn_name_buf: [64]u8 = undefined;
6276+
const float_bits = ty.floatBits(target);
6277+
const fn_name = std.fmt.bufPrint(&fn_name_buf, "{s}f{s}{s}", .{
6278+
target_util.libcFloatPrefix(float_bits),
6279+
@tagName(op),
6280+
target_util.libcFloatSuffix(float_bits),
6281+
}) catch unreachable;
6282+
const result = try func.callIntrinsic(fn_name, &.{ ty.ip_index, ty.ip_index }, ty, &.{ lhs, rhs });
6283+
try func.lowerToStack(result);
6284+
} else {
6285+
// operands to select from
6286+
try func.lowerToStack(lhs);
6287+
try func.lowerToStack(rhs);
6288+
_ = try func.cmp(lhs, rhs, ty, if (op == .max) .gt else .lt);
62766289

6277-
// based on the result from comparison, return operand 0 or 1.
6278-
try func.addTag(.select);
6290+
// based on the result from comparison, return operand 0 or 1.
6291+
try func.addTag(.select);
6292+
}
62796293

62806294
// store result in local
62816295
const result_ty = if (isByRef(ty, mod)) Type.u32 else ty;

src/codegen/llvm.zig

+1
Original file line numberDiff line numberDiff line change
@@ -3050,6 +3050,7 @@ pub const Object = struct {
30503050
decl_val: InternPool.Index,
30513051
llvm_addr_space: Builder.AddrSpace,
30523052
) Error!Builder.Variable.Index {
3053+
// TODO: Add address space to the anon_decl_map
30533054
const gop = try o.anon_decl_map.getOrPut(o.gpa, decl_val);
30543055
if (gop.found_existing) return gop.value_ptr.ptr(&o.builder).kind.variable;
30553056
errdefer assert(o.anon_decl_map.remove(decl_val));

0 commit comments

Comments
 (0)