Skip to content

Commit a3fcdd7

Browse files
committed
add @SpirvType builtin
1 parent aa8aa66 commit a3fcdd7

31 files changed

+765
-115
lines changed

doc/langref.html.in

+7
Original file line numberDiff line numberDiff line change
@@ -5705,6 +5705,13 @@ fn cmpxchgWeakButNotAtomic(comptime T: type, ptr: *T, expected_value: T, new_val
57055705
</p>
57065706
{#header_close#}
57075707

5708+
{#header_open|@SpirvType#}
5709+
<pre>{#syntax#}@SpirvType(comptime info: std.builtin.SpirvType) type{#endsyntax#}</pre>
5710+
<p>
5711+
Reifies an SPIR-V type information into an {#syntax#}opaque{#endsyntax#} type.
5712+
</p>
5713+
{#header_close#}
5714+
57085715
{#header_open|@tagName#}
57095716
<pre>{#syntax#}@tagName(value: anytype) [:0]const u8{#endsyntax#}</pre>
57105717
<p>

lib/std/builtin.zig

+51
Original file line numberDiff line numberDiff line change
@@ -806,6 +806,57 @@ pub const Type = union(enum) {
806806
};
807807
};
808808

809+
pub const SpirvType = union(enum(u2)) {
810+
sampler,
811+
image: Image,
812+
sampled_image: type,
813+
runtime_array: type,
814+
815+
pub const Image = struct {
816+
usage: Usage,
817+
format: Format,
818+
dim: Dimensionality,
819+
depth: Depth,
820+
access: Access,
821+
arrayed: bool,
822+
multisampled: bool,
823+
824+
pub const Usage = union(enum(u2)) {
825+
unknown: type,
826+
sampled: type,
827+
storage,
828+
};
829+
830+
pub const Format = enum(u4) {
831+
unknown,
832+
rgba32f,
833+
rgba32i,
834+
rgba32u,
835+
rgba16f,
836+
rgba16i,
837+
rgba16u,
838+
rgba8unorm,
839+
rgba8snorm,
840+
rgba8i,
841+
rgba8u,
842+
r32f,
843+
r32i,
844+
r32u,
845+
};
846+
847+
pub const Dimensionality = enum(u2) {
848+
@"1d",
849+
@"2d",
850+
@"3d",
851+
cube,
852+
};
853+
854+
pub const Depth = enum(u2) { unknown, depth, not_depth };
855+
856+
pub const Access = enum(u2) { unknown, read_only, write_only, read_write };
857+
};
858+
};
859+
809860
/// This data structure is used by the Zig language code generation and
810861
/// therefore must be kept in sync with the compiler implementation.
811862
pub const FloatMode = enum {

lib/std/zig.zig

+2
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,7 @@ pub const SimpleComptimeReason = enum(u32) {
720720
// Evaluating at comptime because a builtin operand must be comptime-known.
721721
// These messages all mention a specific builtin.
722722
operand_Type,
723+
operand_SpirvType,
723724
operand_setEvalBranchQuota,
724725
operand_setFloatMode,
725726
operand_branchHint,
@@ -802,6 +803,7 @@ pub const SimpleComptimeReason = enum(u32) {
802803
return switch (r) {
803804
// zig fmt: off
804805
.operand_Type => "operand to '@Type' must be comptime-known",
806+
.operand_SpirvType => "operand to '@SpirvType' must be comptime-known",
805807
.operand_setEvalBranchQuota => "operand to '@setEvalBranchQuota' must be comptime-known",
806808
.operand_setFloatMode => "operand to '@setFloatMode' must be comptime-known",
807809
.operand_branchHint => "operand to '@branchHint' must be comptime-known",

lib/std/zig/AstGen.zig

+10
Original file line numberDiff line numberDiff line change
@@ -9497,6 +9497,16 @@ fn builtinCall(
94979497
const result = new_index.toRef();
94989498
return rvalue(gz, ri, result, node);
94999499
},
9500+
.SpirvType => {
9501+
const spirv_type_info_ty = try gz.addBuiltinValue(node, .spirv_type_info);
9502+
const operand = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = spirv_type_info_ty } }, params[0], .operand_SpirvType);
9503+
const result = try gz.addExtendedPayload(.reify_spirv, Zir.Inst.Reify{
9504+
.node = node, // Absolute node index -- see the definition of `Reify`.
9505+
.operand = operand,
9506+
.src_line = astgen.source_line,
9507+
});
9508+
return rvalue(gz, ri, result, node);
9509+
},
95009510
.panic => {
95019511
try emitDbgNode(gz, node);
95029512
return simpleUnOp(gz, scope, ri, node, .{ .rl = .{ .coerced_ty = .slice_const_u8_type } }, params[0], .panic);

lib/std/zig/AstRlAnnotate.zig

+1
Original file line numberDiff line numberDiff line change
@@ -914,6 +914,7 @@ fn builtinCall(astrl: *AstRlAnnotate, block: ?*Block, ri: ResultInfo, node: Ast.
914914
.error_name,
915915
.set_runtime_safety,
916916
.Type,
917+
.SpirvType,
917918
.c_undef,
918919
.c_include,
919920
.wasm_memory_size,

lib/std/zig/BuiltinFn.zig

+8
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ pub const Tag = enum {
112112
trap,
113113
truncate,
114114
Type,
115+
SpirvType,
115116
type_info,
116117
type_name,
117118
TypeOf,
@@ -951,6 +952,13 @@ pub const list = list: {
951952
.param_count = 1,
952953
},
953954
},
955+
.{
956+
"@SpirvType",
957+
.{
958+
.tag = .SpirvType,
959+
.param_count = 1,
960+
},
961+
},
954962
.{
955963
"@typeInfo",
956964
.{

lib/std/zig/Zir.zig

+9-11
Original file line numberDiff line numberDiff line change
@@ -2032,6 +2032,9 @@ pub const Inst = struct {
20322032
/// `operand` is payload index to `Reify`.
20332033
/// `small` contains `NameStrategy`.
20342034
reify,
2035+
/// Implement builtin `@SpirvType`.
2036+
/// `operand` is payload index to `Reify`.
2037+
reify_spirv,
20352038
/// Implements the `@asyncCall` builtin.
20362039
/// `operand` is payload index to `AsyncCall`.
20372040
builtin_async_call,
@@ -3004,7 +3007,7 @@ pub const Inst = struct {
30043007
flags: Flags,
30053008
callee: Ref,
30063009

3007-
pub const Flags = packed struct {
3010+
pub const Flags = packed struct(u32) {
30083011
/// std.builtin.CallModifier in packed form
30093012
pub const PackedModifier = u3;
30103013
pub const PackedArgsLen = u27;
@@ -3015,8 +3018,6 @@ pub const Inst = struct {
30153018
args_len: PackedArgsLen,
30163019

30173020
comptime {
3018-
if (@sizeOf(Flags) != 4 or @bitSizeOf(Flags) != 32)
3019-
@compileError("Layout of Call.Flags needs to be updated!");
30203021
if (@bitSizeOf(std.builtin.CallModifier) != @bitSizeOf(PackedModifier))
30213022
@compileError("Call.Flags.PackedModifier needs to be updated!");
30223023
}
@@ -3053,15 +3054,10 @@ pub const Inst = struct {
30533054
callee: Ref,
30543055
args: Ref,
30553056

3056-
pub const Flags = packed struct {
3057+
pub const Flags = packed struct(u32) {
30573058
is_nosuspend: bool,
30583059
ensure_result_used: bool,
30593060
_: u30 = undefined,
3060-
3061-
comptime {
3062-
if (@sizeOf(Flags) != 4 or @bitSizeOf(Flags) != 32)
3063-
@compileError("Layout of BuiltinCall.Flags needs to be updated!");
3064-
}
30653061
};
30663062
};
30673063

@@ -3471,6 +3467,7 @@ pub const Inst = struct {
34713467
export_options,
34723468
extern_options,
34733469
type_info,
3470+
spirv_type_info,
34743471
branch_hint,
34753472
// Values
34763473
calling_convention_c,
@@ -4379,8 +4376,8 @@ fn findTrackableInner(
43794376
try zir.findTrackableBody(gpa, contents, defers, body);
43804377
},
43814378

4382-
// Reifications and opaque declarations need tracking, but have no body.
4383-
.reify, .opaque_decl => return contents.other.append(gpa, inst),
4379+
// Reifications and opaque declarations need tracking, but have no body..
4380+
.reify, .reify_spirv, .opaque_decl => return contents.other.append(gpa, inst),
43844381

43854382
// Struct declarations need tracking and have bodies.
43864383
.struct_decl => {
@@ -5127,6 +5124,7 @@ pub fn assertTrackable(zir: Zir, inst_idx: Zir.Inst.Index) void {
51275124
.enum_decl,
51285125
.opaque_decl,
51295126
.reify,
5127+
.reify_spirv,
51305128
=> {}, // tracked in order, as the owner instructions of explicit container types
51315129
else => unreachable, // assertion failure; not trackable
51325130
},

0 commit comments

Comments
 (0)