aboutsummaryrefslogtreecommitdiff
path: root/src/codegen
diff options
context:
space:
mode:
authorAli Cheraghi <alichraghi@proton.me>2025-02-19 00:06:10 +0330
committerRobin Voetter <robin@voetter.nl>2025-02-24 19:12:33 +0100
commita0eec9ce9e2f4ae40729949957bef98a2513fef7 (patch)
treef17a4ac38256a6b0601a68439f562dc477c57989 /src/codegen
parent1cc388d5263058f9e9eecc410eea825a9a58e143 (diff)
downloadzig-a0eec9ce9e2f4ae40729949957bef98a2513fef7.tar.gz
zig-a0eec9ce9e2f4ae40729949957bef98a2513fef7.zip
spirv: replace some unreachables with compile errors
Diffstat (limited to 'src/codegen')
-rw-r--r--src/codegen/spirv.zig56
-rw-r--r--src/codegen/spirv/Module.zig12
2 files changed, 44 insertions, 24 deletions
diff --git a/src/codegen/spirv.zig b/src/codegen/spirv.zig
index ec96c56ae9..de7f3a0699 100644
--- a/src/codegen/spirv.zig
+++ b/src/codegen/spirv.zig
@@ -1018,7 +1018,7 @@ const NavGen = struct {
const comp_ty_id = try self.resolveType(ty, .direct);
return try self.constructComposite(comp_ty_id, constituents.items);
},
- .tuple_type => unreachable, // TODO
+ .tuple_type => return self.todo("implement tuple types", .{}),
else => unreachable,
},
.un => |un| {
@@ -1255,6 +1255,7 @@ const NavGen = struct {
fn ptrType(self: *NavGen, child_ty: Type, storage_class: StorageClass, child_repr: Repr) !IdRef {
const zcu = self.pt.zcu;
+ const ip = &zcu.intern_pool;
const key = .{ child_ty.toIntern(), storage_class, child_repr };
const entry = try self.ptr_types.getOrPut(self.gpa, key);
if (entry.found_existing) {
@@ -1285,7 +1286,12 @@ const NavGen = struct {
}
}
- try self.spv.decorate(result_id, .{ .ArrayStride = .{ .array_stride = @intCast(child_ty.abiSize(zcu)) } });
+ switch (ip.indexToKey(child_ty.toIntern())) {
+ .func_type, .opaque_type => {},
+ else => {
+ try self.spv.decorate(result_id, .{ .ArrayStride = .{ .array_stride = @intCast(child_ty.abiSize(zcu)) } });
+ },
+ }
}
try self.spv.sections.types_globals_constants.emit(self.spv.gpa, .OpTypePointer, .{
@@ -1704,7 +1710,10 @@ const NavGen = struct {
return result_id;
},
.@"union" => return try self.resolveUnionType(ty),
- .error_set => return try self.resolveType(Type.u16, repr),
+ .error_set => {
+ const err_int_ty = try pt.errorIntType();
+ return try self.resolveType(err_int_ty, repr);
+ },
.error_union => {
const payload_ty = ty.errorUnionPayload(zcu);
const error_ty_id = try self.resolveType(Type.anyerror, .indirect);
@@ -2329,7 +2338,7 @@ const NavGen = struct {
// NOTE: Vulkan's FMA instruction does *NOT* produce the right values!
// its precision guarantees do NOT match zigs and it does NOT match OpenCLs!
// it needs to be emulated!
- .vulkan, .opengl => unreachable, // TODO: See above
+ .vulkan, .opengl => return self.todo("implement fma operation for {s} os", .{@tagName(target.os.tag)}),
else => unreachable,
};
@@ -2529,12 +2538,12 @@ const NavGen = struct {
.vulkan, .opengl => switch (op) {
.i_abs => 5, // SAbs
.f_abs => 4, // FAbs
- .clz => unreachable, // TODO
- .ctz => unreachable, // TODO
.floor => 8, // Floor
.ceil => 9, // Ceil
.trunc => 3, // Trunc
.round => 1, // Round
+ .clz,
+ .ctz,
.sqrt,
.sin,
.cos,
@@ -2544,7 +2553,7 @@ const NavGen = struct {
.log,
.log2,
.log10,
- => unreachable, // TODO
+ => return self.todo("implement unary operation '{s}' for {s} os", .{ @tagName(op), @tagName(target.os.tag) }),
else => unreachable,
},
else => unreachable,
@@ -2810,6 +2819,8 @@ const NavGen = struct {
/// TODO is to also write out the error as a function call parameter, and to somehow fetch
/// the name of an error in the text executor.
fn generateTestEntryPoint(self: *NavGen, name: []const u8, spv_test_decl_index: SpvModule.Decl.Index) !void {
+ const target = self.spv.target;
+
const anyerror_ty_id = try self.resolveType(Type.anyerror, .direct);
const ptr_anyerror_ty = try self.pt.ptrType(.{
.child = Type.anyerror.toIntern(),
@@ -2819,12 +2830,12 @@ const NavGen = struct {
const spv_decl_index = try self.spv.allocDecl(.func);
const kernel_id = self.spv.declPtr(spv_decl_index).result_id;
- // for some reason we don't need to decorate the push constant here...
- try self.spv.declareDeclDeps(spv_decl_index, &.{spv_test_decl_index});
- const section = &self.spv.sections.functions;
+ var decl_deps = std.ArrayList(SpvModule.Decl.Index).init(self.gpa);
+ defer decl_deps.deinit();
+ try decl_deps.append(spv_test_decl_index);
- const target = self.spv.target;
+ const section = &self.spv.sections.functions;
const p_error_id = self.spv.allocId();
switch (target.os.tag) {
@@ -2904,6 +2915,7 @@ const NavGen = struct {
const spv_err_decl_index = self.object.error_push_constant.?.push_constant_ptr;
const push_constant_id = self.spv.declPtr(spv_err_decl_index).result_id;
+ try decl_deps.append(spv_err_decl_index);
const zero_id = try self.constInt(Type.u32, 0);
// We cannot use OpInBoundsAccessChain to dereference cross-storage class, so we have to use
@@ -2953,6 +2965,7 @@ const NavGen = struct {
else => unreachable,
};
+ try self.spv.declareDeclDeps(spv_decl_index, decl_deps.items);
try self.spv.declareEntryPoint(spv_decl_index, test_name, execution_mode);
}
@@ -3372,6 +3385,7 @@ const NavGen = struct {
.switch_br => return self.airSwitchBr(inst),
.unreach, .trap => return self.airUnreach(),
+ .dbg_empty_stmt => return,
.dbg_stmt => return self.airDbgStmt(inst),
.dbg_inline_block => try self.airDbgInlineBlock(inst),
.dbg_var_ptr, .dbg_var_val, .dbg_arg_inline => return self.airDbgVar(inst),
@@ -3651,6 +3665,7 @@ const NavGen = struct {
}
fn abs(self: *NavGen, result_ty: Type, value: Temporary) !Temporary {
+ const zcu = self.pt.zcu;
const operand_info = self.arithmeticTypeInfo(value.ty);
switch (operand_info.class) {
@@ -3658,11 +3673,9 @@ const NavGen = struct {
.integer, .strange_integer => {
const abs_value = try self.buildUnary(.i_abs, value);
- // TODO: We may need to bitcast the result to a uint
- // depending on the result type. Do that when
- // bitCast is implemented for vectors.
- // This is only relevant for Vulkan
- assert(self.spv.hasFeature(.kernel)); // TODO
+ if (value.ty.intInfo(zcu).signedness == .signed and self.spv.hasFeature(.shader)) {
+ return self.todo("perform bitcast after @abs", .{});
+ }
return try self.normalize(abs_value, self.arithmeticTypeInfo(result_ty));
},
@@ -3980,8 +3993,6 @@ const NavGen = struct {
.float, .bool => unreachable,
}
- assert(self.spv.hasFeature(.kernel)); // TODO
-
const count = try self.buildUnary(op, operand);
// Result of OpenCL ctz/clz returns operand.ty, and we want result_ty.
@@ -4307,7 +4318,8 @@ const NavGen = struct {
},
.error_set => {
assert(!is_vector);
- return try self.cmp(op, lhs.pun(Type.u16), rhs.pun(Type.u16));
+ const err_int_ty = try pt.errorIntType();
+ return try self.cmp(op, lhs.pun(err_int_ty), rhs.pun(err_int_ty));
},
.pointer => {
assert(!is_vector);
@@ -4411,7 +4423,7 @@ const NavGen = struct {
else => unreachable,
};
},
- else => unreachable,
+ else => |ty| return self.todo("implement cmp operation for '{s}' type", .{@tagName(ty)}),
}
const info = self.arithmeticTypeInfo(scalar_ty);
@@ -5233,13 +5245,13 @@ const NavGen = struct {
return self.accessChain(result_ty_id, object_ptr, &.{field_index});
},
.@"struct" => switch (object_ty.containerLayout(zcu)) {
- .@"packed" => unreachable, // TODO
+ .@"packed" => return self.todo("implement field access for packed structs", .{}),
else => {
return try self.accessChain(result_ty_id, object_ptr, &.{field_index});
},
},
.@"union" => switch (object_ty.containerLayout(zcu)) {
- .@"packed" => unreachable, // TODO
+ .@"packed" => return self.todo("implement field access for packed unions", .{}),
else => {
const layout = self.unionLayout(object_ty);
if (!layout.has_payload) {
diff --git a/src/codegen/spirv/Module.zig b/src/codegen/spirv/Module.zig
index 317e32c878..30f9b0bc54 100644
--- a/src/codegen/spirv/Module.zig
+++ b/src/codegen/spirv/Module.zig
@@ -345,22 +345,30 @@ pub fn finalize(self: *Module, a: Allocator) ![]Word {
if (self.target.cpu.features.isEnabled(feature.index)) {
const feature_tag: std.Target.spirv.Feature = @enumFromInt(feature.index);
switch (feature_tag) {
+ // Versions
.v1_0, .v1_1, .v1_2, .v1_3, .v1_4, .v1_5, .v1_6 => {},
+ // Features with no dependencies
.int8 => try self.addCapability(.Int8),
.int16 => try self.addCapability(.Int16),
.int64 => try self.addCapability(.Int64),
.float16 => try self.addCapability(.Float16),
.float64 => try self.addCapability(.Float64),
+ .matrix => try self.addCapability(.Matrix),
+ .storage_push_constant16 => {
+ try self.addExtension("SPV_KHR_16bit_storage");
+ try self.addCapability(.StoragePushConstant16);
+ },
.addresses => if (self.hasFeature(.shader)) {
- try self.addCapability(.PhysicalStorageBufferAddresses);
try self.addExtension("SPV_KHR_physical_storage_buffer");
+ try self.addCapability(.PhysicalStorageBufferAddresses);
} else {
try self.addCapability(.Addresses);
},
- .matrix => try self.addCapability(.Matrix),
+ // Kernel
.kernel => try self.addCapability(.Kernel),
.generic_pointer => try self.addCapability(.GenericPointer),
.vector16 => try self.addCapability(.Vector16),
+ // Shader
.shader => try self.addCapability(.Shader),
}
}