Skip to content

Commit a5b1ccb

Browse files
committed
remove needless unsafe from intrinsics
1 parent fb42c2b commit a5b1ccb

14 files changed

+130
-152
lines changed

crates/spirv-std/src/arch.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ pub fn kill() -> ! {
153153
/// <https://htmlpreview.github.io/?https://github.com/KhronosGroup/SPIRV-Registry/blob/master/extensions/KHR/SPV_KHR_shader_clock.html>
154154
#[spirv_std_macros::gpu_only]
155155
#[doc(alias = "OpReadClockKHR")]
156-
pub unsafe fn read_clock_khr<const SCOPE: u32>() -> u64 {
156+
pub fn read_clock_khr<const SCOPE: u32>() -> u64 {
157157
unsafe {
158158
let mut result: u64;
159159

@@ -175,7 +175,7 @@ pub unsafe fn read_clock_khr<const SCOPE: u32>() -> u64 {
175175
/// bits and the second component containing the 32 most significant bits.'
176176
#[spirv_std_macros::gpu_only]
177177
#[doc(alias = "OpReadClockKHR")]
178-
pub unsafe fn read_clock_uvec2_khr<V: Vector<u32, 2>, const SCOPE: u32>() -> V {
178+
pub fn read_clock_uvec2_khr<V: Vector<u32, 2>, const SCOPE: u32>() -> V {
179179
unsafe {
180180
let mut result = V::default();
181181

crates/spirv-std/src/arch/barrier.rs

Lines changed: 59 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use core::arch::asm;
3333
#[spirv_std_macros::gpu_only]
3434
#[doc(alias = "OpControlBarrier")]
3535
#[inline]
36-
pub unsafe fn control_barrier<
36+
pub fn control_barrier<
3737
const EXECUTION: u32, // Scope
3838
const MEMORY: u32, // Scope
3939
const SEMANTICS: u32, // Semantics
@@ -70,7 +70,7 @@ pub unsafe fn control_barrier<
7070
#[spirv_std_macros::gpu_only]
7171
#[doc(alias = "OpMemoryBarrier")]
7272
#[inline]
73-
pub unsafe fn memory_barrier<
73+
pub fn memory_barrier<
7474
const MEMORY: u32, // Scope
7575
const SEMANTICS: u32, // Semantics
7676
>() {
@@ -93,16 +93,14 @@ pub unsafe fn memory_barrier<
9393
/// From <https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/groupmemorybarrier>
9494
#[spirv_std_macros::gpu_only]
9595
#[inline]
96-
pub unsafe fn workgroup_memory_barrier() {
97-
unsafe {
98-
memory_barrier::<
99-
{ crate::memory::Scope::Workgroup as u32 },
100-
{
101-
crate::memory::Semantics::WORKGROUP_MEMORY.bits()
102-
| crate::memory::Semantics::ACQUIRE_RELEASE.bits()
103-
},
104-
>();
105-
}
96+
pub fn workgroup_memory_barrier() {
97+
memory_barrier::<
98+
{ crate::memory::Scope::Workgroup as u32 },
99+
{
100+
crate::memory::Semantics::WORKGROUP_MEMORY.bits()
101+
| crate::memory::Semantics::ACQUIRE_RELEASE.bits()
102+
},
103+
>();
106104
}
107105

108106
/// Blocks execution of all threads in a group until all group shared accesses have been completed and all threads in the group have reached this call.
@@ -112,17 +110,15 @@ pub unsafe fn workgroup_memory_barrier() {
112110
/// From <https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/groupmemorybarrierwithgroupsync>
113111
#[spirv_std_macros::gpu_only]
114112
#[inline]
115-
pub unsafe fn workgroup_memory_barrier_with_group_sync() {
116-
unsafe {
117-
control_barrier::<
118-
{ crate::memory::Scope::Workgroup as u32 },
119-
{ crate::memory::Scope::Workgroup as u32 },
120-
{
121-
crate::memory::Semantics::WORKGROUP_MEMORY.bits()
122-
| crate::memory::Semantics::ACQUIRE_RELEASE.bits()
123-
},
124-
>();
125-
}
113+
pub fn workgroup_memory_barrier_with_group_sync() {
114+
control_barrier::<
115+
{ crate::memory::Scope::Workgroup as u32 },
116+
{ crate::memory::Scope::Workgroup as u32 },
117+
{
118+
crate::memory::Semantics::WORKGROUP_MEMORY.bits()
119+
| crate::memory::Semantics::ACQUIRE_RELEASE.bits()
120+
},
121+
>();
126122
}
127123

128124
/// Blocks execution of all threads in a group until all device memory accesses have been completed.
@@ -132,17 +128,15 @@ pub unsafe fn workgroup_memory_barrier_with_group_sync() {
132128
/// From <https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/devicememorybarrier>
133129
#[spirv_std_macros::gpu_only]
134130
#[inline]
135-
pub unsafe fn device_memory_barrier() {
136-
unsafe {
137-
memory_barrier::<
138-
{ crate::memory::Scope::Device as u32 },
139-
{
140-
crate::memory::Semantics::IMAGE_MEMORY.bits()
141-
| crate::memory::Semantics::UNIFORM_MEMORY.bits()
142-
| crate::memory::Semantics::ACQUIRE_RELEASE.bits()
143-
},
144-
>();
145-
}
131+
pub fn device_memory_barrier() {
132+
memory_barrier::<
133+
{ crate::memory::Scope::Device as u32 },
134+
{
135+
crate::memory::Semantics::IMAGE_MEMORY.bits()
136+
| crate::memory::Semantics::UNIFORM_MEMORY.bits()
137+
| crate::memory::Semantics::ACQUIRE_RELEASE.bits()
138+
},
139+
>();
146140
}
147141

148142
/// Blocks execution of all threads in a group until all device memory accesses have been completed and all threads in the group have reached this call.
@@ -152,18 +146,16 @@ pub unsafe fn device_memory_barrier() {
152146
/// From <https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/devicememorybarrierwithgroupsync>
153147
#[spirv_std_macros::gpu_only]
154148
#[inline]
155-
pub unsafe fn device_memory_barrier_with_group_sync() {
156-
unsafe {
157-
control_barrier::<
158-
{ crate::memory::Scope::Workgroup as u32 },
159-
{ crate::memory::Scope::Device as u32 },
160-
{
161-
crate::memory::Semantics::IMAGE_MEMORY.bits()
162-
| crate::memory::Semantics::UNIFORM_MEMORY.bits()
163-
| crate::memory::Semantics::ACQUIRE_RELEASE.bits()
164-
},
165-
>();
166-
}
149+
pub fn device_memory_barrier_with_group_sync() {
150+
control_barrier::<
151+
{ crate::memory::Scope::Workgroup as u32 },
152+
{ crate::memory::Scope::Device as u32 },
153+
{
154+
crate::memory::Semantics::IMAGE_MEMORY.bits()
155+
| crate::memory::Semantics::UNIFORM_MEMORY.bits()
156+
| crate::memory::Semantics::ACQUIRE_RELEASE.bits()
157+
},
158+
>();
167159
}
168160

169161
/// Blocks execution of all threads in a group until all memory accesses have been completed.
@@ -173,18 +165,16 @@ pub unsafe fn device_memory_barrier_with_group_sync() {
173165
/// From <https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/allmemorybarrier>
174166
#[spirv_std_macros::gpu_only]
175167
#[inline]
176-
pub unsafe fn all_memory_barrier() {
177-
unsafe {
178-
memory_barrier::<
179-
{ crate::memory::Scope::Device as u32 },
180-
{
181-
crate::memory::Semantics::WORKGROUP_MEMORY.bits()
182-
| crate::memory::Semantics::IMAGE_MEMORY.bits()
183-
| crate::memory::Semantics::UNIFORM_MEMORY.bits()
184-
| crate::memory::Semantics::ACQUIRE_RELEASE.bits()
185-
},
186-
>();
187-
}
168+
pub fn all_memory_barrier() {
169+
memory_barrier::<
170+
{ crate::memory::Scope::Device as u32 },
171+
{
172+
crate::memory::Semantics::WORKGROUP_MEMORY.bits()
173+
| crate::memory::Semantics::IMAGE_MEMORY.bits()
174+
| crate::memory::Semantics::UNIFORM_MEMORY.bits()
175+
| crate::memory::Semantics::ACQUIRE_RELEASE.bits()
176+
},
177+
>();
188178
}
189179

190180
/// Blocks execution of all threads in a group until all memory accesses have been completed and all threads in the group have reached this call.
@@ -194,17 +184,15 @@ pub unsafe fn all_memory_barrier() {
194184
/// From <https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/allmemorybarrierwithgroupsync>
195185
#[spirv_std_macros::gpu_only]
196186
#[inline]
197-
pub unsafe fn all_memory_barrier_with_group_sync() {
198-
unsafe {
199-
control_barrier::<
200-
{ crate::memory::Scope::Workgroup as u32 },
201-
{ crate::memory::Scope::Device as u32 },
202-
{
203-
crate::memory::Semantics::WORKGROUP_MEMORY.bits()
204-
| crate::memory::Semantics::IMAGE_MEMORY.bits()
205-
| crate::memory::Semantics::UNIFORM_MEMORY.bits()
206-
| crate::memory::Semantics::ACQUIRE_RELEASE.bits()
207-
},
208-
>();
209-
}
187+
pub fn all_memory_barrier_with_group_sync() {
188+
control_barrier::<
189+
{ crate::memory::Scope::Workgroup as u32 },
190+
{ crate::memory::Scope::Device as u32 },
191+
{
192+
crate::memory::Semantics::WORKGROUP_MEMORY.bits()
193+
| crate::memory::Semantics::IMAGE_MEMORY.bits()
194+
| crate::memory::Semantics::UNIFORM_MEMORY.bits()
195+
| crate::memory::Semantics::ACQUIRE_RELEASE.bits()
196+
},
197+
>();
210198
}

crates/spirv-std/src/arch/demote_to_helper_invocation_ext.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use core::arch::asm;
1919
/// of [demote_to_helper_invocation].
2020
#[spirv_std_macros::gpu_only]
2121
#[doc(alias = "OpDemoteToHelperInvocationEXT", alias = "discard")]
22-
pub unsafe fn demote_to_helper_invocation() {
22+
pub fn demote_to_helper_invocation() {
2323
unsafe {
2424
asm!("OpDemoteToHelperInvocationEXT");
2525
}

crates/spirv-std/src/arch/subgroup.rs

Lines changed: 31 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -64,18 +64,16 @@ pub enum GroupOperation {
6464
#[doc(alias = "subgroupBarrier")]
6565
#[inline]
6666
pub fn subgroup_barrier() {
67-
unsafe {
68-
barrier::control_barrier::<
69-
SUBGROUP,
70-
SUBGROUP,
71-
{
72-
Semantics::ACQUIRE_RELEASE.bits()
73-
| Semantics::UNIFORM_MEMORY.bits()
74-
| Semantics::WORKGROUP_MEMORY.bits()
75-
| Semantics::IMAGE_MEMORY.bits()
76-
},
77-
>();
78-
}
67+
barrier::control_barrier::<
68+
SUBGROUP,
69+
SUBGROUP,
70+
{
71+
Semantics::ACQUIRE_RELEASE.bits()
72+
| Semantics::UNIFORM_MEMORY.bits()
73+
| Semantics::WORKGROUP_MEMORY.bits()
74+
| Semantics::IMAGE_MEMORY.bits()
75+
},
76+
>();
7977
}
8078

8179
/// The function `subgroupMemoryBarrier()` enforces the ordering of all memory
@@ -87,17 +85,15 @@ pub fn subgroup_barrier() {
8785
#[doc(alias = "subgroupMemoryBarrier")]
8886
#[inline]
8987
pub fn subgroup_memory_barrier() {
90-
unsafe {
91-
barrier::memory_barrier::<
92-
SUBGROUP,
93-
{
94-
Semantics::ACQUIRE_RELEASE.bits()
95-
| Semantics::UNIFORM_MEMORY.bits()
96-
| Semantics::WORKGROUP_MEMORY.bits()
97-
| Semantics::IMAGE_MEMORY.bits()
98-
},
99-
>();
100-
}
88+
barrier::memory_barrier::<
89+
SUBGROUP,
90+
{
91+
Semantics::ACQUIRE_RELEASE.bits()
92+
| Semantics::UNIFORM_MEMORY.bits()
93+
| Semantics::WORKGROUP_MEMORY.bits()
94+
| Semantics::IMAGE_MEMORY.bits()
95+
},
96+
>();
10197
}
10298

10399
/// The function `subgroupMemoryBarrierBuffer()` enforces the ordering of all
@@ -109,12 +105,10 @@ pub fn subgroup_memory_barrier() {
109105
#[doc(alias = "subgroupMemoryBarrierBuffer")]
110106
#[inline]
111107
pub fn subgroup_memory_barrier_buffer() {
112-
unsafe {
113-
barrier::memory_barrier::<
114-
SUBGROUP,
115-
{ Semantics::ACQUIRE_RELEASE.bits() | Semantics::UNIFORM_MEMORY.bits() },
116-
>();
117-
}
108+
barrier::memory_barrier::<
109+
SUBGROUP,
110+
{ Semantics::ACQUIRE_RELEASE.bits() | Semantics::UNIFORM_MEMORY.bits() },
111+
>();
118112
}
119113

120114
/// The function `subgroupMemoryBarrierShared()` enforces the ordering of all
@@ -128,12 +122,10 @@ pub fn subgroup_memory_barrier_buffer() {
128122
#[doc(alias = "subgroupMemoryBarrierShared")]
129123
#[inline]
130124
pub fn subgroup_memory_barrier_shared() {
131-
unsafe {
132-
barrier::memory_barrier::<
133-
SUBGROUP,
134-
{ Semantics::ACQUIRE_RELEASE.bits() | Semantics::WORKGROUP_MEMORY.bits() },
135-
>();
136-
}
125+
barrier::memory_barrier::<
126+
SUBGROUP,
127+
{ Semantics::ACQUIRE_RELEASE.bits() | Semantics::WORKGROUP_MEMORY.bits() },
128+
>();
137129
}
138130

139131
/// The function `subgroupMemoryBarrierImage()` enforces the ordering of all
@@ -145,12 +137,10 @@ pub fn subgroup_memory_barrier_shared() {
145137
#[doc(alias = "subgroupMemoryBarrierImage")]
146138
#[inline]
147139
pub fn subgroup_memory_barrier_image() {
148-
unsafe {
149-
barrier::memory_barrier::<
150-
SUBGROUP,
151-
{ Semantics::ACQUIRE_RELEASE.bits() | Semantics::IMAGE_MEMORY.bits() },
152-
>();
153-
}
140+
barrier::memory_barrier::<
141+
SUBGROUP,
142+
{ Semantics::ACQUIRE_RELEASE.bits() | Semantics::IMAGE_MEMORY.bits() },
143+
>();
154144
}
155145

156146
/// Result is true only in the active invocation with the lowest id in the group, otherwise result is false.

tests/compiletests/ui/arch/subgroup/subgroup_ballot.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
%1 = OpFunction %2 None %3
22
%4 = OpFunctionParameter %2
33
%5 = OpLabel
4-
OpLine %6 378 13
4+
OpLine %6 368 13
55
%7 = OpGroupNonUniformBallot %8 %9 %4
6-
OpLine %6 417 13
6+
OpLine %6 407 13
77
%10 = OpGroupNonUniformInverseBallot %2 %9 %7
88
OpNoLine
99
OpReturnValue %10

tests/compiletests/ui/arch/subgroup/subgroup_ballot_bit_count.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
%1 = OpFunction %2 None %3
22
%4 = OpFunctionParameter %5
33
%6 = OpLabel
4-
OpLine %7 512 0
4+
OpLine %7 502 0
55
%8 = OpGroupNonUniformBallotBitCount %2 %9 Reduce %4
66
OpNoLine
77
OpReturnValue %8

tests/compiletests/ui/arch/subgroup/subgroup_broadcast_first.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
%1 = OpFunction %2 None %3
22
%4 = OpFunctionParameter %2
33
%5 = OpLabel
4-
OpLine %6 344 13
4+
OpLine %6 334 13
55
%7 = OpGroupNonUniformBroadcastFirst %2 %8 %4
66
OpNoLine
77
OpReturnValue %7

tests/compiletests/ui/arch/subgroup/subgroup_cluster_size_0_fail.stderr

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
error[E0080]: evaluation panicked: `ClusterSize` must be at least 1
2-
--> $SPIRV_STD_SRC/arch/subgroup.rs:840:1
2+
--> $SPIRV_STD_SRC/arch/subgroup.rs:830:1
33
|
4-
840 | / macro_subgroup_op_clustered!(impl Integer, "OpGroupNonUniformIAdd", subgroup_clustered_i_add; r"
5-
841 | | An integer add group operation of all `value` operands contributed by active invocations in the group.
6-
842 | |
7-
843 | | Result Type must be a scalar or vector of integer type.
4+
830 | / macro_subgroup_op_clustered!(impl Integer, "OpGroupNonUniformIAdd", subgroup_clustered_i_add; r"
5+
831 | | An integer add group operation of all `value` operands contributed by active invocations in the group.
6+
832 | |
7+
833 | | Result Type must be a scalar or vector of integer type.
88
... |
9-
856 | | * `ClusterSize` must not be greater than the size of the group
10-
857 | | ");
9+
846 | | * `ClusterSize` must not be greater than the size of the group
10+
847 | | ");
1111
| |__^ evaluation of `spirv_std::arch::subgroup_clustered_i_add::<0, u32, u32>::{constant#0}` failed here
1212
|
1313
= note: this error originates in the macro `$crate::panic::panic_2021` which comes from the expansion of the macro `macro_subgroup_op_clustered` (in Nightly builds, run with -Z macro-backtrace for more info)
1414

1515
note: erroneous constant encountered
16-
--> $SPIRV_STD_SRC/arch/subgroup.rs:840:1
16+
--> $SPIRV_STD_SRC/arch/subgroup.rs:830:1
1717
|
18-
840 | / macro_subgroup_op_clustered!(impl Integer, "OpGroupNonUniformIAdd", subgroup_clustered_i_add; r"
19-
841 | | An integer add group operation of all `value` operands contributed by active invocations in the group.
20-
842 | |
21-
843 | | Result Type must be a scalar or vector of integer type.
18+
830 | / macro_subgroup_op_clustered!(impl Integer, "OpGroupNonUniformIAdd", subgroup_clustered_i_add; r"
19+
831 | | An integer add group operation of all `value` operands contributed by active invocations in the group.
20+
832 | |
21+
833 | | Result Type must be a scalar or vector of integer type.
2222
... |
23-
856 | | * `ClusterSize` must not be greater than the size of the group
24-
857 | | ");
23+
846 | | * `ClusterSize` must not be greater than the size of the group
24+
847 | | ");
2525
| |__^
2626
|
2727
= note: this note originates in the macro `macro_subgroup_op_clustered` (in Nightly builds, run with -Z macro-backtrace for more info)

0 commit comments

Comments
 (0)