Skip to content

Commit 5105c3c

Browse files
authored
Merge pull request #23158 from alichraghi/ali_spirv
spirv: miscellaneous stuff #2
2 parents 074dd4d + ee06b2c commit 5105c3c

22 files changed

+407
-186
lines changed

lib/std/Target/spirv.zig

+19-19
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,18 @@ pub const Feature = enum {
1010
v1_4,
1111
v1_5,
1212
v1_6,
13-
int8,
14-
int16,
1513
int64,
1614
float16,
1715
float64,
18-
addresses,
1916
matrix,
2017
storage_push_constant16,
18+
arbitrary_precision_integers,
2119
kernel,
20+
addresses,
2221
generic_pointer,
2322
vector16,
2423
shader,
24+
physical_storage_buffer,
2525
};
2626

2727
pub const featureSet = CpuFeature.FeatureSetFns(Feature).featureSet;
@@ -69,16 +69,6 @@ pub const all_features = blk: {
6969
.description = "Enable version 1.6",
7070
.dependencies = featureSet(&[_]Feature{.v1_5}),
7171
};
72-
result[@intFromEnum(Feature.int8)] = .{
73-
.llvm_name = null,
74-
.description = "Enable Int8 capability",
75-
.dependencies = featureSet(&[_]Feature{.v1_0}),
76-
};
77-
result[@intFromEnum(Feature.int16)] = .{
78-
.llvm_name = null,
79-
.description = "Enable Int16 capability",
80-
.dependencies = featureSet(&[_]Feature{.v1_0}),
81-
};
8272
result[@intFromEnum(Feature.int64)] = .{
8373
.llvm_name = null,
8474
.description = "Enable Int64 capability",
@@ -94,11 +84,6 @@ pub const all_features = blk: {
9484
.description = "Enable Float64 capability",
9585
.dependencies = featureSet(&[_]Feature{.v1_0}),
9686
};
97-
result[@intFromEnum(Feature.addresses)] = .{
98-
.llvm_name = null,
99-
.description = "Enable either the Addresses capability or, SPV_KHR_physical_storage_buffer extension and the PhysicalStorageBufferAddresses capability",
100-
.dependencies = featureSet(&[_]Feature{.v1_0}),
101-
};
10287
result[@intFromEnum(Feature.matrix)] = .{
10388
.llvm_name = null,
10489
.description = "Enable Matrix capability",
@@ -109,11 +94,21 @@ pub const all_features = blk: {
10994
.description = "Enable SPV_KHR_16bit_storage extension and the StoragePushConstant16 capability",
11095
.dependencies = featureSet(&[_]Feature{.v1_3}),
11196
};
97+
result[@intFromEnum(Feature.arbitrary_precision_integers)] = .{
98+
.llvm_name = null,
99+
.description = "Enable SPV_INTEL_arbitrary_precision_integers extension and the ArbitraryPrecisionIntegersINTEL capability",
100+
.dependencies = featureSet(&[_]Feature{.v1_5}),
101+
};
112102
result[@intFromEnum(Feature.kernel)] = .{
113103
.llvm_name = null,
114104
.description = "Enable Kernel capability",
115105
.dependencies = featureSet(&[_]Feature{.v1_0}),
116106
};
107+
result[@intFromEnum(Feature.addresses)] = .{
108+
.llvm_name = null,
109+
.description = "Enable Addresses capability",
110+
.dependencies = featureSet(&[_]Feature{.v1_0}),
111+
};
117112
result[@intFromEnum(Feature.generic_pointer)] = .{
118113
.llvm_name = null,
119114
.description = "Enable GenericPointer capability",
@@ -129,6 +124,11 @@ pub const all_features = blk: {
129124
.description = "Enable Shader capability",
130125
.dependencies = featureSet(&[_]Feature{ .v1_0, .matrix }),
131126
};
127+
result[@intFromEnum(Feature.physical_storage_buffer)] = .{
128+
.llvm_name = null,
129+
.description = "Enable SPV_KHR_physical_storage_buffer extension and the PhysicalStorageBufferAddresses capability",
130+
.dependencies = featureSet(&[_]Feature{.v1_0}),
131+
};
132132
const ti = @typeInfo(Feature);
133133
for (&result, 0..) |*elem, i| {
134134
elem.index = i;
@@ -147,7 +147,7 @@ pub const cpu = struct {
147147
pub const vulkan_v1_2: CpuModel = .{
148148
.name = "vulkan_v1_2",
149149
.llvm_name = null,
150-
.features = featureSet(&[_]Feature{ .v1_5, .shader, .addresses }),
150+
.features = featureSet(&[_]Feature{ .v1_5, .shader, .physical_storage_buffer }),
151151
};
152152

153153
pub const opencl_v2: CpuModel = .{

lib/std/gpu.zig

+10-5
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ pub fn fragmentDepth(comptime ptr: *addrspace(.output) f32) void {
8080
/// Forms the main linkage for `input` and `output` address spaces.
8181
/// `ptr` must be a reference to variable or struct field.
8282
pub fn location(comptime ptr: anytype, comptime loc: u32) void {
83-
asm volatile ("OpDecorate %ptr Location $loc"
83+
asm volatile (
84+
\\OpDecorate %ptr Location $loc
8485
:
8586
: [ptr] "" (ptr),
8687
[loc] "c" (loc),
@@ -110,7 +111,8 @@ pub const Origin = enum(u32) {
110111
/// The coordinates appear to originate in the specified `origin`.
111112
/// Only valid with the `Fragment` calling convention.
112113
pub fn fragmentOrigin(comptime entry_point: anytype, comptime origin: Origin) void {
113-
asm volatile ("OpExecutionMode %entry_point $origin"
114+
asm volatile (
115+
\\OpExecutionMode %entry_point $origin
114116
:
115117
: [entry_point] "" (entry_point),
116118
[origin] "c" (@intFromEnum(origin)),
@@ -137,7 +139,8 @@ pub const DepthMode = enum(u32) {
137139

138140
/// Only valid with the `Fragment` calling convention.
139141
pub fn depthMode(comptime entry_point: anytype, comptime mode: DepthMode) void {
140-
asm volatile ("OpExecutionMode %entry_point $mode"
142+
asm volatile (
143+
\\OpExecutionMode %entry_point $mode
141144
:
142145
: [entry_point] "" (entry_point),
143146
[mode] "c" (mode),
@@ -147,7 +150,8 @@ pub fn depthMode(comptime entry_point: anytype, comptime mode: DepthMode) void {
147150
/// Indicates the workgroup size in the `x`, `y`, and `z` dimensions.
148151
/// Only valid with the `GLCompute` or `Kernel` calling conventions.
149152
pub fn workgroupSize(comptime entry_point: anytype, comptime size: @Vector(3, u32)) void {
150-
asm volatile ("OpExecutionMode %entry_point LocalSize %x %y %z"
153+
asm volatile (
154+
\\OpExecutionMode %entry_point LocalSize %x %y %z
151155
:
152156
: [entry_point] "" (entry_point),
153157
[x] "c" (size[0]),
@@ -159,7 +163,8 @@ pub fn workgroupSize(comptime entry_point: anytype, comptime size: @Vector(3, u3
159163
/// A hint to the client, which indicates the workgroup size in the `x`, `y`, and `z` dimensions.
160164
/// Only valid with the `GLCompute` or `Kernel` calling conventions.
161165
pub fn workgroupSizeHint(comptime entry_point: anytype, comptime size: @Vector(3, u32)) void {
162-
asm volatile ("OpExecutionMode %entry_point LocalSizeHint %x %y %z"
166+
asm volatile (
167+
\\OpExecutionMode %entry_point LocalSizeHint %x %y %z
163168
:
164169
: [entry_point] "" (entry_point),
165170
[x] "c" (size[0]),

src/Sema.zig

+53-24
Original file line numberDiff line numberDiff line change
@@ -3648,7 +3648,7 @@ fn indexablePtrLen(
36483648
const object_ty = sema.typeOf(object);
36493649
const is_pointer_to = object_ty.isSinglePointer(zcu);
36503650
const indexable_ty = if (is_pointer_to) object_ty.childType(zcu) else object_ty;
3651-
try checkIndexable(sema, block, src, indexable_ty);
3651+
try sema.checkIndexable(block, src, indexable_ty);
36523652
const field_name = try zcu.intern_pool.getOrPutString(sema.gpa, pt.tid, "len", .no_embedded_nulls);
36533653
return sema.fieldVal(block, src, object, field_name, src);
36543654
}
@@ -10103,6 +10103,7 @@ fn zirIntFromPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!
1010310103
}
1010410104
try sema.requireRuntimeBlock(block, block.nodeOffset(inst_data.src_node), ptr_src);
1010510105
try sema.validateRuntimeValue(block, ptr_src, operand);
10106+
try sema.checkLogicalPtrOperation(block, ptr_src, ptr_ty);
1010610107
if (!is_vector or zcu.backendSupportsFeature(.all_vector_instructions)) {
1010710108
return block.addBitCast(dest_ty, operand);
1010810109
}
@@ -16389,6 +16390,8 @@ fn analyzeArithmetic(
1638916390
};
1639016391

1639116392
try sema.requireRuntimeBlock(block, src, runtime_src);
16393+
try sema.checkLogicalPtrOperation(block, src, lhs_ty);
16394+
try sema.checkLogicalPtrOperation(block, src, rhs_ty);
1639216395
const lhs_int = try block.addBitCast(.usize, lhs);
1639316396
const rhs_int = try block.addBitCast(.usize, rhs);
1639416397
const address = try block.addBinOp(.sub_wrap, lhs_int, rhs_int);
@@ -16620,24 +16623,7 @@ fn analyzePtrArithmetic(
1662016623
};
1662116624

1662216625
try sema.requireRuntimeBlock(block, op_src, runtime_src);
16623-
16624-
const target = zcu.getTarget();
16625-
if (target_util.arePointersLogical(target, ptr_info.flags.address_space)) {
16626-
return sema.failWithOwnedErrorMsg(block, msg: {
16627-
const msg = try sema.errMsg(op_src, "illegal pointer arithmetic on pointer of type '{}'", .{ptr_ty.fmt(pt)});
16628-
errdefer msg.destroy(sema.gpa);
16629-
16630-
const backend = target_util.zigBackend(target, zcu.comp.config.use_llvm);
16631-
try sema.errNote(op_src, msg, "arithmetic cannot be performed on pointers with address space '{s}' on target {s}-{s} by compiler backend {s}", .{
16632-
@tagName(ptr_info.flags.address_space),
16633-
target.cpu.arch.genericName(),
16634-
@tagName(target.os.tag),
16635-
@tagName(backend),
16636-
});
16637-
16638-
break :msg msg;
16639-
});
16640-
}
16626+
try sema.checkLogicalPtrOperation(block, op_src, ptr_ty);
1664116627

1664216628
return block.addInst(.{
1664316629
.tag = air_tag,
@@ -22501,6 +22487,7 @@ fn zirPtrFromInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!
2250122487
});
2250222488
}
2250322489
try sema.requireRuntimeBlock(block, src, operand_src);
22490+
try sema.checkLogicalPtrOperation(block, src, ptr_ty);
2250422491
if (!is_vector or zcu.backendSupportsFeature(.all_vector_instructions)) {
2250522492
if (block.wantSafety() and (try elem_ty.hasRuntimeBitsSema(pt) or elem_ty.zigTypeTag(zcu) == .@"fn")) {
2250622493
if (!ptr_ty.isAllowzeroPtr(zcu)) {
@@ -23165,8 +23152,9 @@ fn ptrCastFull(
2316523152

2316623153
try sema.validateRuntimeValue(block, operand_src, operand);
2316723154

23168-
const need_null_check = block.wantSafety() and operand_ty.ptrAllowsZero(zcu) and !dest_ty.ptrAllowsZero(zcu);
23169-
const need_align_check = block.wantSafety() and dest_align.compare(.gt, src_align);
23155+
const can_cast_to_int = !target_util.arePointersLogical(zcu.getTarget(), operand_ty.ptrAddressSpace(zcu));
23156+
const need_null_check = can_cast_to_int and block.wantSafety() and operand_ty.ptrAllowsZero(zcu) and !dest_ty.ptrAllowsZero(zcu);
23157+
const need_align_check = can_cast_to_int and block.wantSafety() and dest_align.compare(.gt, src_align);
2317023158

2317123159
// `operand` might be a slice. If `need_operand_ptr`, we'll populate `operand_ptr` with the raw pointer.
2317223160
const need_operand_ptr = src_info.flags.size != .slice or // we already have it
@@ -23832,6 +23820,32 @@ fn checkPtrType(
2383223820
return sema.fail(block, ty_src, "expected pointer type, found '{}'", .{ty.fmt(pt)});
2383323821
}
2383423822

23823+
fn checkLogicalPtrOperation(sema: *Sema, block: *Block, src: LazySrcLoc, ty: Type) !void {
23824+
const pt = sema.pt;
23825+
const zcu = pt.zcu;
23826+
if (zcu.intern_pool.indexToKey(ty.toIntern()) == .ptr_type) {
23827+
const target = zcu.getTarget();
23828+
const as = ty.ptrAddressSpace(zcu);
23829+
if (target_util.arePointersLogical(target, as)) {
23830+
return sema.failWithOwnedErrorMsg(block, msg: {
23831+
const msg = try sema.errMsg(src, "illegal operation on logical pointer of type '{}'", .{ty.fmt(pt)});
23832+
errdefer msg.destroy(sema.gpa);
23833+
try sema.errNote(
23834+
src,
23835+
msg,
23836+
"cannot perform arithmetic on pointers with address space '{s}' on target {s}-{s}",
23837+
.{
23838+
@tagName(as),
23839+
target.cpu.arch.genericName(),
23840+
@tagName(target.os.tag),
23841+
},
23842+
);
23843+
break :msg msg;
23844+
});
23845+
}
23846+
}
23847+
}
23848+
2383523849
fn checkVectorElemType(
2383623850
sema: *Sema,
2383723851
block: *Block,
@@ -28326,7 +28340,7 @@ fn elemPtr(
2832628340
.pointer => indexable_ptr_ty.childType(zcu),
2832728341
else => return sema.fail(block, indexable_ptr_src, "expected pointer, found '{}'", .{indexable_ptr_ty.fmt(pt)}),
2832828342
};
28329-
try checkIndexable(sema, block, src, indexable_ty);
28343+
try sema.checkIndexable(block, src, indexable_ty);
2833028344

2833128345
const elem_ptr = switch (indexable_ty.zigTypeTag(zcu)) {
2833228346
.array, .vector => try sema.elemPtrArray(block, src, indexable_ptr_src, indexable_ptr, elem_index_src, elem_index, init, oob_safety),
@@ -28362,7 +28376,7 @@ fn elemPtrOneLayerOnly(
2836228376
const pt = sema.pt;
2836328377
const zcu = pt.zcu;
2836428378

28365-
try checkIndexable(sema, block, src, indexable_ty);
28379+
try sema.checkIndexable(block, src, indexable_ty);
2836628380

2836728381
switch (indexable_ty.ptrSize(zcu)) {
2836828382
.slice => return sema.elemPtrSlice(block, src, indexable_src, indexable, elem_index_src, elem_index, oob_safety),
@@ -28376,6 +28390,8 @@ fn elemPtrOneLayerOnly(
2837628390
const elem_ptr = try ptr_val.ptrElem(index, pt);
2837728391
return Air.internedToRef(elem_ptr.toIntern());
2837828392
}
28393+
28394+
try sema.checkLogicalPtrOperation(block, src, indexable_ty);
2837928395
const result_ty = try indexable_ty.elemPtrType(null, pt);
2838028396

2838128397
return block.addPtrElemPtr(indexable, elem_index, result_ty);
@@ -28412,7 +28428,7 @@ fn elemVal(
2841228428
const pt = sema.pt;
2841328429
const zcu = pt.zcu;
2841428430

28415-
try checkIndexable(sema, block, src, indexable_ty);
28431+
try sema.checkIndexable(block, src, indexable_ty);
2841628432

2841728433
// TODO in case of a vector of pointers, we need to detect whether the element
2841828434
// index is a scalar or vector instead of unconditionally casting to usize.
@@ -28438,6 +28454,7 @@ fn elemVal(
2843828454
return Air.internedToRef((try pt.getCoerced(elem_val, elem_ty)).toIntern());
2843928455
}
2844028456

28457+
try sema.checkLogicalPtrOperation(block, src, indexable_ty);
2844128458
return block.addBinOp(.ptr_elem_val, indexable, elem_index);
2844228459
},
2844328460
.one => {
@@ -28477,6 +28494,9 @@ fn validateRuntimeElemAccess(
2847728494
parent_ty: Type,
2847828495
parent_src: LazySrcLoc,
2847928496
) CompileError!void {
28497+
const pt = sema.pt;
28498+
const zcu = pt.zcu;
28499+
2848028500
if (try elem_ty.comptimeOnlySema(sema.pt)) {
2848128501
const msg = msg: {
2848228502
const msg = try sema.errMsg(
@@ -28492,6 +28512,14 @@ fn validateRuntimeElemAccess(
2849228512
};
2849328513
return sema.failWithOwnedErrorMsg(block, msg);
2849428514
}
28515+
28516+
if (zcu.intern_pool.indexToKey(parent_ty.toIntern()) == .ptr_type) {
28517+
const target = zcu.getTarget();
28518+
const as = parent_ty.ptrAddressSpace(zcu);
28519+
if (target_util.arePointersLogical(target, as)) {
28520+
return sema.fail(block, elem_index_src, "cannot access element of logical pointer '{}'", .{parent_ty.fmt(pt)});
28521+
}
28522+
}
2849528523
}
2849628524

2849728525
fn tupleFieldPtr(
@@ -31158,6 +31186,7 @@ fn coerceCompatiblePtrs(
3115831186
if (block.wantSafety() and inst_allows_zero and !dest_ty.ptrAllowsZero(zcu) and
3115931187
(try dest_ty.elemType2(zcu).hasRuntimeBitsSema(pt) or dest_ty.elemType2(zcu).zigTypeTag(zcu) == .@"fn"))
3116031188
{
31189+
try sema.checkLogicalPtrOperation(block, inst_src, inst_ty);
3116131190
const actual_ptr = if (inst_ty.isSlice(zcu))
3116231191
try sema.analyzeSlicePtr(block, inst_src, inst, inst_ty)
3116331192
else

0 commit comments

Comments
 (0)