Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add @SpirvType builtin #23326

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions doc/langref.html.in
Original file line number Diff line number Diff line change
Expand Up @@ -5705,6 +5705,13 @@ fn cmpxchgWeakButNotAtomic(comptime T: type, ptr: *T, expected_value: T, new_val
</p>
{#header_close#}

{#header_open|@SpirvType#}
<pre>{#syntax#}@SpirvType(comptime info: std.builtin.SpirvType) type{#endsyntax#}</pre>
<p>
Reifies an SPIR-V type information into an {#syntax#}opaque{#endsyntax#} type.
</p>
{#header_close#}

{#header_open|@tagName#}
<pre>{#syntax#}@tagName(value: anytype) [:0]const u8{#endsyntax#}</pre>
<p>
Expand Down
51 changes: 51 additions & 0 deletions lib/std/builtin.zig
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,57 @@ pub const Type = union(enum) {
};
};

pub const SpirvType = union(enum(u2)) {
sampler,
image: Image,
sampled_image: type,
runtime_array: type,

pub const Image = struct {
usage: Usage,
format: Format,
dim: Dimensionality,
depth: Depth,
access: Access,
arrayed: bool,
multisampled: bool,

pub const Usage = union(enum(u2)) {
unknown: type,
sampled: type,
storage,
};

pub const Format = enum(u4) {
unknown,
rgba32f,
rgba32i,
rgba32u,
rgba16f,
rgba16i,
rgba16u,
rgba8unorm,
rgba8snorm,
rgba8i,
rgba8u,
r32f,
r32i,
r32u,
};

pub const Dimensionality = enum(u2) {
@"1d",
@"2d",
@"3d",
cube,
};

pub const Depth = enum(u2) { unknown, depth, not_depth };

pub const Access = enum(u2) { unknown, read_only, write_only, read_write };
};
};

/// This data structure is used by the Zig language code generation and
/// therefore must be kept in sync with the compiler implementation.
pub const FloatMode = enum {
Expand Down
2 changes: 2 additions & 0 deletions lib/std/zig.zig
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,7 @@ pub const SimpleComptimeReason = enum(u32) {
// Evaluating at comptime because a builtin operand must be comptime-known.
// These messages all mention a specific builtin.
operand_Type,
operand_SpirvType,
operand_setEvalBranchQuota,
operand_setFloatMode,
operand_branchHint,
Expand Down Expand Up @@ -802,6 +803,7 @@ pub const SimpleComptimeReason = enum(u32) {
return switch (r) {
// zig fmt: off
.operand_Type => "operand to '@Type' must be comptime-known",
.operand_SpirvType => "operand to '@SpirvType' must be comptime-known",
.operand_setEvalBranchQuota => "operand to '@setEvalBranchQuota' must be comptime-known",
.operand_setFloatMode => "operand to '@setFloatMode' must be comptime-known",
.operand_branchHint => "operand to '@branchHint' must be comptime-known",
Expand Down
10 changes: 10 additions & 0 deletions lib/std/zig/AstGen.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9497,6 +9497,16 @@ fn builtinCall(
const result = new_index.toRef();
return rvalue(gz, ri, result, node);
},
.SpirvType => {
const spirv_type_info_ty = try gz.addBuiltinValue(node, .spirv_type_info);
const operand = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = spirv_type_info_ty } }, params[0], .operand_SpirvType);
const result = try gz.addExtendedPayload(.reify_spirv, Zir.Inst.Reify{
.node = node, // Absolute node index -- see the definition of `Reify`.
.operand = operand,
.src_line = astgen.source_line,
});
return rvalue(gz, ri, result, node);
},
.panic => {
try emitDbgNode(gz, node);
return simpleUnOp(gz, scope, ri, node, .{ .rl = .{ .coerced_ty = .slice_const_u8_type } }, params[0], .panic);
Expand Down
1 change: 1 addition & 0 deletions lib/std/zig/AstRlAnnotate.zig
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,7 @@ fn builtinCall(astrl: *AstRlAnnotate, block: ?*Block, ri: ResultInfo, node: Ast.
.error_name,
.set_runtime_safety,
.Type,
.SpirvType,
.c_undef,
.c_include,
.wasm_memory_size,
Expand Down
8 changes: 8 additions & 0 deletions lib/std/zig/BuiltinFn.zig
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ pub const Tag = enum {
trap,
truncate,
Type,
SpirvType,
type_info,
type_name,
TypeOf,
Expand Down Expand Up @@ -951,6 +952,13 @@ pub const list = list: {
.param_count = 1,
},
},
.{
"@SpirvType",
.{
.tag = .SpirvType,
.param_count = 1,
},
},
.{
"@typeInfo",
.{
Expand Down
20 changes: 9 additions & 11 deletions lib/std/zig/Zir.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2032,6 +2032,9 @@ pub const Inst = struct {
/// `operand` is payload index to `Reify`.
/// `small` contains `NameStrategy`.
reify,
/// Implement builtin `@SpirvType`.
/// `operand` is payload index to `Reify`.
reify_spirv,
/// Implements the `@asyncCall` builtin.
/// `operand` is payload index to `AsyncCall`.
builtin_async_call,
Expand Down Expand Up @@ -3004,7 +3007,7 @@ pub const Inst = struct {
flags: Flags,
callee: Ref,

pub const Flags = packed struct {
pub const Flags = packed struct(u32) {
/// std.builtin.CallModifier in packed form
pub const PackedModifier = u3;
pub const PackedArgsLen = u27;
Expand All @@ -3015,8 +3018,6 @@ pub const Inst = struct {
args_len: PackedArgsLen,

comptime {
if (@sizeOf(Flags) != 4 or @bitSizeOf(Flags) != 32)
@compileError("Layout of Call.Flags needs to be updated!");
if (@bitSizeOf(std.builtin.CallModifier) != @bitSizeOf(PackedModifier))
@compileError("Call.Flags.PackedModifier needs to be updated!");
}
Expand Down Expand Up @@ -3053,15 +3054,10 @@ pub const Inst = struct {
callee: Ref,
args: Ref,

pub const Flags = packed struct {
pub const Flags = packed struct(u32) {
is_nosuspend: bool,
ensure_result_used: bool,
_: u30 = undefined,

comptime {
if (@sizeOf(Flags) != 4 or @bitSizeOf(Flags) != 32)
@compileError("Layout of BuiltinCall.Flags needs to be updated!");
}
};
};

Expand Down Expand Up @@ -3471,6 +3467,7 @@ pub const Inst = struct {
export_options,
extern_options,
type_info,
spirv_type_info,
branch_hint,
// Values
calling_convention_c,
Expand Down Expand Up @@ -4379,8 +4376,8 @@ fn findTrackableInner(
try zir.findTrackableBody(gpa, contents, defers, body);
},

// Reifications and opaque declarations need tracking, but have no body.
.reify, .opaque_decl => return contents.other.append(gpa, inst),
// Reifications and opaque declarations need tracking, but have no body..
.reify, .reify_spirv, .opaque_decl => return contents.other.append(gpa, inst),

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