authorgravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2023-10-08 02:44:52+02:00
committergravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2023-10-15 14:00:04+02:00
logd2692af8e2c6e4397f9be94b2b6625ba0e6e725f
tree305ecdd1eb88c38dfe9b51cf279c232f8d1f5453
parentae3efab2260ce84fa25dd874e0c193d919eb80c1
signaturebadge-check Signed by SSH key SHA256:CQ99aPxq+RueiL9u7z0FEki5Fm7V6T8q4PrEGmINrA4

spirv: override function return type to void if it has no runtime bits


2 files changed, 57 insertions(+), 22 deletions(-)

src/codegen/spirv.zig+57-18
......@@ -1180,6 +1180,22 @@ const DeclGen = struct {
11801180 return ty_ref;
11811181 }
11821182
1183 fn resolveFnReturnType(self: *DeclGen, ret_ty: Type) !CacheRef {
1184 const mod = self.module;
1185 if (!ret_ty.hasRuntimeBitsIgnoreComptime(mod)) {
1186 // If the return type is an error set or an error union, then we make this
1187 // anyerror return type instead, so that it can be coerced into a function
1188 // pointer type which has anyerror as the return type.
1189 if (ret_ty.isError(mod)) {
1190 return self.resolveType(Type.anyerror, .direct);
1191 } else {
1192 return self.resolveType(Type.void, .direct);
1193 }
1194 }
1195
1196 return try self.resolveType(ret_ty, .direct);
1197 }
1198
11831199 /// Turn a Zig type into a SPIR-V Type, and return a reference to it.
11841200 fn resolveType(self: *DeclGen, ty: Type, repr: Repr) Error!CacheRef {
11851201 const mod = self.module;
......@@ -1260,7 +1276,7 @@ const DeclGen = struct {
12601276 param_ty_refs[param_index] = try self.resolveType(param_ty, .direct);
12611277 param_index += 1;
12621278 }
1263 const return_ty_ref = try self.resolveType(fn_info.return_type.toType(), .direct);
1279 const return_ty_ref = try self.resolveFnReturnType(fn_info.return_type.toType());
12641280
12651281 const ty_ref = try self.spv.resolve(.{ .function_type = .{
12661282 .return_type = return_ty_ref,
......@@ -1449,9 +1465,11 @@ const DeclGen = struct {
14491465 return ty_ref;
14501466 },
14511467 .Opaque => {
1452 return try self.spv.resolve(.{ .opaque_type = .{
1453 .name = .none, // TODO
1454 } });
1468 return try self.spv.resolve(.{
1469 .opaque_type = .{
1470 .name = .none, // TODO
1471 },
1472 });
14551473 },
14561474
14571475 .Null,
......@@ -1677,16 +1695,17 @@ const DeclGen = struct {
16771695
16781696 if (decl.val.getFunction(mod)) |_| {
16791697 assert(decl.ty.zigTypeTag(mod) == .Fn);
1698 const fn_info = mod.typeToFunc(decl.ty).?;
1699 const return_ty_ref = try self.resolveFnReturnType(fn_info.return_type.toType());
1700
16801701 const prototype_id = try self.resolveTypeId(decl.ty);
16811702 try self.func.prologue.emit(self.spv.gpa, .OpFunction, .{
1682 .id_result_type = try self.resolveTypeId(decl.ty.fnReturnType(mod)),
1703 .id_result_type = self.typeId(return_ty_ref),
16831704 .id_result = decl_id,
16841705 .function_control = .{}, // TODO: We can set inline here if the type requires it.
16851706 .function_type = prototype_id,
16861707 });
16871708
1688 const fn_info = mod.typeToFunc(decl.ty).?;
1689
16901709 try self.args.ensureUnusedCapacity(self.gpa, fn_info.param_types.len);
16911710 for (fn_info.param_types.get(ip)) |param_ty_index| {
16921711 const param_ty = param_ty_index.toType();
......@@ -3385,15 +3404,25 @@ const DeclGen = struct {
33853404
33863405 fn airRet(self: *DeclGen, inst: Air.Inst.Index) !void {
33873406 const operand = self.air.instructions.items(.data)[inst].un_op;
3388 const operand_ty = self.typeOf(operand);
3407 const ret_ty = self.typeOf(operand);
33893408 const mod = self.module;
3390 if (operand_ty.hasRuntimeBits(mod)) {
3391 // TODO: If we return an empty struct, this branch is also hit incorrectly.
3392 const operand_id = try self.resolve(operand);
3393 try self.func.body.emit(self.spv.gpa, .OpReturnValue, .{ .value = operand_id });
3394 } else {
3395 try self.func.body.emit(self.spv.gpa, .OpReturn, {});
3409 if (!ret_ty.hasRuntimeBitsIgnoreComptime(mod)) {
3410 const decl = mod.declPtr(self.decl_index);
3411 const fn_info = mod.typeToFunc(decl.ty).?;
3412 if (fn_info.return_type.toType().isError(mod)) {
3413 // Functions with an empty error set are emitted with an error code
3414 // return type and return zero so they can be function pointers coerced
3415 // to functions that return anyerror.
3416 const err_ty_ref = try self.resolveType(Type.anyerror, .direct);
3417 const no_err_id = try self.constInt(err_ty_ref, 0);
3418 return try self.func.body.emit(self.spv.gpa, .OpReturnValue, .{ .value = no_err_id });
3419 } else {
3420 return try self.func.body.emit(self.spv.gpa, .OpReturn, {});
3421 }
33963422 }
3423
3424 const operand_id = try self.resolve(operand);
3425 try self.func.body.emit(self.spv.gpa, .OpReturnValue, .{ .value = operand_id });
33973426 }
33983427
33993428 fn airRetLoad(self: *DeclGen, inst: Air.Inst.Index) !void {
......@@ -3403,8 +3432,18 @@ const DeclGen = struct {
34033432 const ret_ty = ptr_ty.childType(mod);
34043433
34053434 if (!ret_ty.hasRuntimeBitsIgnoreComptime(mod)) {
3406 try self.func.body.emit(self.spv.gpa, .OpReturn, {});
3407 return;
3435 const decl = mod.declPtr(self.decl_index);
3436 const fn_info = mod.typeToFunc(decl.ty).?;
3437 if (fn_info.return_type.toType().isError(mod)) {
3438 // Functions with an empty error set are emitted with an error code
3439 // return type and return zero so they can be function pointers coerced
3440 // to functions that return anyerror.
3441 const err_ty_ref = try self.resolveType(Type.anyerror, .direct);
3442 const no_err_id = try self.constInt(err_ty_ref, 0);
3443 return try self.func.body.emit(self.spv.gpa, .OpReturnValue, .{ .value = no_err_id });
3444 } else {
3445 return try self.func.body.emit(self.spv.gpa, .OpReturn, {});
3446 }
34083447 }
34093448
34103449 const ptr = try self.resolve(un_op);
......@@ -3991,7 +4030,7 @@ const DeclGen = struct {
39914030 const fn_info = mod.typeToFunc(zig_fn_ty).?;
39924031 const return_type = fn_info.return_type;
39934032
3994 const result_type_id = try self.resolveTypeId(return_type.toType());
4033 const result_type_ref = try self.resolveFnReturnType(return_type.toType());
39954034 const result_id = self.spv.allocId();
39964035 const callee_id = try self.resolve(pl_op.operand);
39974036
......@@ -4012,7 +4051,7 @@ const DeclGen = struct {
40124051 }
40134052
40144053 try self.func.body.emit(self.spv.gpa, .OpFunctionCall, .{
4015 .id_result_type = result_type_id,
4054 .id_result_type = self.typeId(result_type_ref),
40164055 .id_result = result_id,
40174056 .function = callee_id,
40184057 .id_ref_3 = params[0..n_params],
test/behavior/optional.zig-4
......@@ -27,7 +27,6 @@ pub const EmptyStruct = struct {};
2727
2828test "optional pointer to size zero struct" {
2929 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
30 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
3130
3231 var e = EmptyStruct{};
3332 var o: ?*EmptyStruct = &e;
......@@ -152,7 +151,6 @@ fn test_cmp_optional_non_optional() !void {
152151test "unwrap function call with optional pointer return value" {
153152 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
154153 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
155 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
156154
157155 const S = struct {
158156 fn entry() !void {
......@@ -400,7 +398,6 @@ const NoReturn = struct {
400398
401399test "optional of noreturn used with if" {
402400 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
403 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
404401
405402 NoReturn.a = 64;
406403 if (NoReturn.loop()) |_| {
......@@ -412,7 +409,6 @@ test "optional of noreturn used with if" {
412409
413410test "optional of noreturn used with orelse" {
414411 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
415 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
416412
417413 NoReturn.a = 64;
418414 const val = NoReturn.testOrelse();