diff options
| author | Robin Voetter <robin@voetter.nl> | 2023-09-16 01:44:25 +0200 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2023-09-23 12:36:44 -0700 |
| commit | ae17831cc060840c881c9514ef98c0637c446760 (patch) | |
| tree | a9135137424c816d158f31eff1eaa2e6990fda8b /src/codegen/spirv.zig | |
| parent | 3e2553c7123e914f34bdd0c29dcb2683ad3a6726 (diff) | |
| download | zig-ae17831cc060840c881c9514ef98c0637c446760.tar.gz zig-ae17831cc060840c881c9514ef98c0637c446760.zip | |
spirv: lower opt constants
Diffstat (limited to 'src/codegen/spirv.zig')
| -rw-r--r-- | src/codegen/spirv.zig | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/src/codegen/spirv.zig b/src/codegen/spirv.zig index 484f9b0896..fa4ea4ec1b 100644 --- a/src/codegen/spirv.zig +++ b/src/codegen/spirv.zig @@ -1223,6 +1223,40 @@ pub const DeclGen = struct { }); return result_id; }, + .opt => { + const payload_ty = ty.optionalChild(mod); + const maybe_payload_val = val.optionalValue(mod); + + if (!payload_ty.hasRuntimeBits(mod)) { + return try self.constBool(maybe_payload_val != null, .indirect); + } else if (ty.optionalReprIsPayload(mod)) { + // Optional representation is a nullable pointer or slice. + if (maybe_payload_val) |payload_val| { + return try self.constant(payload_ty, payload_val, .indirect); + } else { + const ptr_ty_ref = try self.resolveType(ty, .indirect); + return self.spv.constNull(ptr_ty_ref); + } + } + + // Optional representation is a structure. + // { Payload, Bool } + + const payload_id = if (maybe_payload_val) |payload_val| + try self.constant(payload_ty, payload_val, .indirect) + else + try self.spv.constUndef(try self.resolveType(payload_ty, .indirect)); + + const has_pl_id = try self.constBool(maybe_payload_val != null, .indirect); + + const result_id = self.spv.allocId(); + try self.func.body.emit(self.spv.gpa, .OpCompositeConstruct, .{ + .id_result_type = self.typeId(result_ty_ref), + .id_result = result_id, + .constituents = &.{ payload_id, has_pl_id }, + }); + return result_id; + }, // TODO: We can handle most pointers here (decl refs etc), because now they emit an extra // OpVariable that is not really required. else => { |
