mirror of
https://github.com/storytold/wgpu.git
synced 2026-11-19 14:01:08 +01:00
Add emit_int_div_checks option to skip integer division safety wrappers (#9443)
Co-authored-by: Connor Fitzgerald <connorwadefitzgerald@gmail.com>
This commit is contained in:
1 parent
08b244df91
commit
10f97e5777
15 files changed
+120
-8
No files matched your search
@@ -225,6 +225,7 @@ By @beholdnec in [#8505](https://github.com/gfx-rs/wgpu/pull/8505).
|
||||
- Enforce that `@must_use` appear only on function declarations. By @dnsn021 in [#9367](https://github.com/gfx-rs/wgpu/pull/9367).
|
||||
- Fix typo in `naga::back::msl::Error::UnsupportedWritable*` variant names. By @ErichDonGubler in [#9376](https://github.com/gfx-rs/wgpu/pull/9376).
|
||||
- Added support for `enable wgpu_binding_array;`. By @39ali in [#9298](https://github.com/gfx-rs/wgpu/pull/9298).
|
||||
- Ability to disable integer division safety checks on Vulkan and Metal. By @kvark in [#9443](https://github.com/gfx-rs/wgpu/pull/9443).
|
||||
- \[hlsl\] more `matCx2` fixes. By @teoxoy in [#9507](https://github.com/gfx-rs/wgpu/pull/9507).
|
||||
|
||||
#### Vulkan
|
||||
|
||||
@@ -128,6 +128,7 @@ pub struct SpirvOutParameters {
|
||||
pub binding_map: naga::back::spv::BindingMap,
|
||||
pub ray_query_initialization_tracking: bool,
|
||||
pub use_storage_input_output_16: bool,
|
||||
pub emit_int_div_checks: bool,
|
||||
}
|
||||
impl Default for SpirvOutParameters {
|
||||
fn default() -> Self {
|
||||
@@ -141,6 +142,7 @@ impl Default for SpirvOutParameters {
|
||||
separate_entry_points: false,
|
||||
ray_query_initialization_tracking: true,
|
||||
use_storage_input_output_16: true,
|
||||
emit_int_div_checks: true,
|
||||
binding_map: naga::back::spv::BindingMap::default(),
|
||||
}
|
||||
}
|
||||
@@ -179,6 +181,7 @@ impl SpirvOutParameters {
|
||||
task_dispatch_limits: shared_info.task_limits,
|
||||
mesh_shader_primitive_indices_clamp: shared_info.mesh_output_validation,
|
||||
trace_ray_argument_validation: true,
|
||||
emit_int_div_checks: self.emit_int_div_checks,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -319,6 +319,11 @@ pub struct Options {
|
||||
pub task_dispatch_limits: Option<TaskDispatchLimits>,
|
||||
/// Whether to validate the output of a mesh shader workgroup.
|
||||
pub mesh_shader_primitive_indices_clamp: bool,
|
||||
/// If true (the default), integer division and modulo operations use
|
||||
/// wrapper functions that guard against division by zero and signed
|
||||
/// overflow. Set to false to emit raw division for faster compute shaders
|
||||
/// where the developer guarantees non-zero divisors.
|
||||
pub emit_int_div_checks: bool,
|
||||
}
|
||||
|
||||
impl Default for Options {
|
||||
@@ -334,6 +339,7 @@ impl Default for Options {
|
||||
force_loop_bounding: true,
|
||||
task_dispatch_limits: None,
|
||||
mesh_shader_primitive_indices_clamp: true,
|
||||
emit_int_div_checks: true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -532,6 +532,7 @@ pub struct Writer<W> {
|
||||
need_bake_expressions: back::NeedBakeExpressions,
|
||||
pub(super) namer: proc::Namer,
|
||||
pub(super) wrapped_functions: FastHashSet<WrappedFunction>,
|
||||
emit_int_div_checks: bool,
|
||||
#[cfg(test)]
|
||||
put_expression_stack_pointers: FastHashSet<*const ()>,
|
||||
#[cfg(test)]
|
||||
@@ -800,6 +801,8 @@ pub(super) struct ExpressionContext<'a> {
|
||||
pub(super) guarded_indices: HandleSet<crate::Expression>,
|
||||
/// See [`Writer::gen_force_bounded_loop_statements`] for details.
|
||||
pub(super) force_loop_bounding: bool,
|
||||
/// Whether to emit safety checks for integer division/modulo.
|
||||
emit_int_div_checks: bool,
|
||||
}
|
||||
|
||||
impl<'a> ExpressionContext<'a> {
|
||||
@@ -892,6 +895,7 @@ impl<W: Write> Writer<W> {
|
||||
need_bake_expressions: Default::default(),
|
||||
namer: proc::Namer::default(),
|
||||
wrapped_functions: FastHashSet::default(),
|
||||
emit_int_div_checks: true,
|
||||
#[cfg(test)]
|
||||
put_expression_stack_pointers: Default::default(),
|
||||
#[cfg(test)]
|
||||
@@ -2254,6 +2258,7 @@ impl<W: Write> Writer<W> {
|
||||
|
||||
if op == crate::BinaryOperator::Divide
|
||||
&& (kind == crate::ScalarKind::Sint || kind == crate::ScalarKind::Uint)
|
||||
&& context.emit_int_div_checks
|
||||
{
|
||||
write!(self.out, "{DIV_FUNCTION}(")?;
|
||||
self.put_expression(left, context, true)?;
|
||||
@@ -2262,6 +2267,7 @@ impl<W: Write> Writer<W> {
|
||||
write!(self.out, ")")?;
|
||||
} else if op == crate::BinaryOperator::Modulo
|
||||
&& (kind == crate::ScalarKind::Sint || kind == crate::ScalarKind::Uint)
|
||||
&& context.emit_int_div_checks
|
||||
{
|
||||
write!(self.out, "{MOD_FUNCTION}(")?;
|
||||
self.put_expression(left, context, true)?;
|
||||
@@ -4364,6 +4370,7 @@ impl<W: Write> Writer<W> {
|
||||
options: &Options,
|
||||
pipeline_options: &PipelineOptions,
|
||||
) -> Result<TranslationInfo, Error> {
|
||||
self.emit_int_div_checks = options.emit_int_div_checks;
|
||||
self.names.clear();
|
||||
self.namer.reset(
|
||||
module,
|
||||
@@ -5727,7 +5734,7 @@ template <typename A>
|
||||
(
|
||||
crate::BinaryOperator::Divide,
|
||||
Some(crate::ScalarKind::Sint | crate::ScalarKind::Uint),
|
||||
) => {
|
||||
) if self.emit_int_div_checks => {
|
||||
let Some(left_wrapped_ty) = left_ty.vector_size_and_scalar() else {
|
||||
return Ok(());
|
||||
};
|
||||
@@ -5810,7 +5817,7 @@ template <typename A>
|
||||
(
|
||||
crate::BinaryOperator::Modulo,
|
||||
Some(crate::ScalarKind::Sint | crate::ScalarKind::Uint),
|
||||
) => {
|
||||
) if self.emit_int_div_checks => {
|
||||
let Some(left_wrapped_ty) = left_ty.vector_size_and_scalar() else {
|
||||
return Ok(());
|
||||
};
|
||||
@@ -6833,6 +6840,7 @@ template <typename A>
|
||||
mod_info,
|
||||
pipeline_options,
|
||||
force_loop_bounding: options.force_loop_bounding,
|
||||
emit_int_div_checks: options.emit_int_div_checks,
|
||||
},
|
||||
result_struct: None,
|
||||
};
|
||||
@@ -8051,6 +8059,7 @@ template <typename A>
|
||||
mod_info,
|
||||
pipeline_options,
|
||||
force_loop_bounding: options.force_loop_bounding,
|
||||
emit_int_div_checks: options.emit_int_div_checks,
|
||||
},
|
||||
result_struct: if ep.stage == crate::ShaderStage::Task {
|
||||
None
|
||||
|
||||
@@ -1178,11 +1178,14 @@ impl BlockContext<'_> {
|
||||
_ => unimplemented!(),
|
||||
},
|
||||
crate::BinaryOperator::Modulo => match left_ty_inner.scalar_kind() {
|
||||
// TODO: handle undefined behavior
|
||||
// if right == 0 return ? see https://github.com/gpuweb/gpuweb/issues/2798
|
||||
Some(crate::ScalarKind::Float) => spirv::Op::FRem,
|
||||
Some(crate::ScalarKind::Sint | crate::ScalarKind::Uint) => {
|
||||
unreachable!("Should have been handled by wrapped function")
|
||||
Some(crate::ScalarKind::Sint) => {
|
||||
assert!(!self.writer.emit_int_div_checks);
|
||||
spirv::Op::SRem
|
||||
}
|
||||
Some(crate::ScalarKind::Uint) => {
|
||||
assert!(!self.writer.emit_int_div_checks);
|
||||
spirv::Op::UMod
|
||||
}
|
||||
_ => unimplemented!(),
|
||||
},
|
||||
|
||||
@@ -941,6 +941,7 @@ pub struct Writer {
|
||||
zero_initialize_workgroup_memory: ZeroInitializeWorkgroupMemoryMode,
|
||||
force_loop_bounding: bool,
|
||||
use_storage_input_output_16: bool,
|
||||
emit_int_div_checks: bool,
|
||||
void_type: Word,
|
||||
tuple_of_u32s_ty_id: Option<Word>,
|
||||
//TODO: convert most of these into vectors, addressable by handle indices
|
||||
@@ -1114,6 +1115,17 @@ pub struct Options<'a> {
|
||||
///
|
||||
/// Currently this validation is unimplemented.
|
||||
pub mesh_shader_primitive_indices_clamp: bool,
|
||||
|
||||
/// If true (the default), integer division and modulo operations emit
|
||||
/// wrapper functions that replace a zero divisor with one, and for signed
|
||||
/// integers also guard against `INT_MIN / -1` overflow. This matches the
|
||||
/// WGSL spec's requirement that these cases produce defined results.
|
||||
///
|
||||
/// Set to `false` to emit raw `OpSDiv`/`OpUDiv`/`OpSRem`/`OpUMod`
|
||||
/// instructions without checks. This is faster but produces
|
||||
/// implementation-defined results when the divisor is zero. Appropriate
|
||||
/// for compute shaders where the developer guarantees non-zero divisors.
|
||||
pub emit_int_div_checks: bool,
|
||||
}
|
||||
|
||||
impl Default for Options<'_> {
|
||||
@@ -1139,6 +1151,7 @@ impl Default for Options<'_> {
|
||||
debug_info: None,
|
||||
task_dispatch_limits: None,
|
||||
mesh_shader_primitive_indices_clamp: true,
|
||||
emit_int_div_checks: true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -96,6 +96,7 @@ impl Writer {
|
||||
ray_query_initialization_tracking: options.ray_query_initialization_tracking,
|
||||
trace_ray_argument_validation: options.trace_ray_argument_validation,
|
||||
use_storage_input_output_16: options.use_storage_input_output_16,
|
||||
emit_int_div_checks: options.emit_int_div_checks,
|
||||
void_type,
|
||||
tuple_of_u32s_ty_id: None,
|
||||
lookup_type: crate::FastHashMap::default(),
|
||||
@@ -181,6 +182,7 @@ impl Writer {
|
||||
binding_map: take(&mut self.binding_map),
|
||||
task_dispatch_limits: self.task_dispatch_limits,
|
||||
mesh_shader_primitive_indices_clamp: self.mesh_shader_primitive_indices_clamp,
|
||||
emit_int_div_checks: self.emit_int_div_checks,
|
||||
|
||||
// Initialized afresh:
|
||||
id_gen,
|
||||
@@ -561,7 +563,7 @@ impl Writer {
|
||||
(
|
||||
crate::BinaryOperator::Divide | crate::BinaryOperator::Modulo,
|
||||
crate::ScalarKind::Sint | crate::ScalarKind::Uint,
|
||||
) => {
|
||||
) if self.emit_int_div_checks => {
|
||||
self.write_wrapped_binary_op(
|
||||
op,
|
||||
expr_ty,
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
targets = "SPIRV | METAL"
|
||||
|
||||
[spv]
|
||||
emit_int_div_checks = false
|
||||
|
||||
[msl]
|
||||
emit_int_div_checks = false
|
||||
@@ -0,0 +1,15 @@
|
||||
// Test that integer division and modulo emit raw ops when
|
||||
// `emit_int_div_checks` is disabled.
|
||||
|
||||
@compute @workgroup_size(1)
|
||||
fn main() {
|
||||
let a = 5i;
|
||||
let b = 2i;
|
||||
let div_s = a / b;
|
||||
let mod_s = a % b;
|
||||
|
||||
let c = 5u;
|
||||
let d = 2u;
|
||||
let div_u = c / d;
|
||||
let mod_u = c % d;
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
// language: metal1.0
|
||||
#include <metal_stdlib>
|
||||
#include <simd/simd.h>
|
||||
|
||||
using metal::uint;
|
||||
|
||||
|
||||
kernel void main_(
|
||||
) {
|
||||
int div_s = 5 / 2;
|
||||
int mod_s = 5 % 2;
|
||||
uint div_u = 5u / 2u;
|
||||
uint mod_u = 5u % 2u;
|
||||
return;
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
; SPIR-V
|
||||
; Version: 1.1
|
||||
; Generator: rspirv
|
||||
; Bound: 17
|
||||
OpCapability Shader
|
||||
%1 = OpExtInstImport "GLSL.std.450"
|
||||
OpMemoryModel Logical GLSL450
|
||||
OpEntryPoint GLCompute %4 "main"
|
||||
OpExecutionMode %4 LocalSize 1 1 1
|
||||
%2 = OpTypeVoid
|
||||
%5 = OpTypeFunction %2
|
||||
%6 = OpTypeInt 32 1
|
||||
%7 = OpConstant %6 5
|
||||
%8 = OpConstant %6 2
|
||||
%9 = OpTypeInt 32 0
|
||||
%10 = OpConstant %9 5
|
||||
%11 = OpConstant %9 2
|
||||
%4 = OpFunction %2 None %5
|
||||
%3 = OpLabel
|
||||
OpBranch %12
|
||||
%12 = OpLabel
|
||||
%13 = OpSDiv %6 %7 %8
|
||||
%14 = OpSRem %6 %7 %8
|
||||
%15 = OpUDiv %9 %10 %11
|
||||
%16 = OpUMod %9 %10 %11
|
||||
OpReturn
|
||||
OpFunctionEnd
|
||||
@@ -220,6 +220,7 @@ impl super::Device {
|
||||
.module
|
||||
.runtime_checks
|
||||
.mesh_shader_primitive_indices_clamp,
|
||||
emit_int_div_checks: stage.module.runtime_checks.int_div_checks,
|
||||
};
|
||||
|
||||
let pipeline_options = naga::back::msl::PipelineOptions {
|
||||
|
||||
@@ -2755,6 +2755,7 @@ impl super::Adapter {
|
||||
}),
|
||||
mesh_shader_primitive_indices_clamp: true,
|
||||
trace_ray_argument_validation: true,
|
||||
emit_int_div_checks: true,
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -720,7 +720,8 @@ impl super::Device {
|
||||
|| naga_shader.debug_source.is_some()
|
||||
|| !stage.zero_initialize_workgroup_memory
|
||||
|| !runtime_checks.task_shader_dispatch_tracking
|
||||
|| !runtime_checks.mesh_shader_primitive_indices_clamp;
|
||||
|| !runtime_checks.mesh_shader_primitive_indices_clamp
|
||||
|| !runtime_checks.int_div_checks;
|
||||
|
||||
let mut temp_options;
|
||||
let options = if needs_temp_options {
|
||||
@@ -759,6 +760,7 @@ impl super::Device {
|
||||
}
|
||||
temp_options.mesh_shader_primitive_indices_clamp =
|
||||
runtime_checks.mesh_shader_primitive_indices_clamp;
|
||||
temp_options.emit_int_div_checks = runtime_checks.int_div_checks;
|
||||
|
||||
&temp_options
|
||||
} else {
|
||||
|
||||
@@ -50,6 +50,12 @@ pub struct ShaderRuntimeChecks {
|
||||
/// If false, mesh shaders won't clamp the output primitives' vertex indices, which can lead to
|
||||
/// undefined behavior and arbitrary memory access.
|
||||
pub mesh_shader_primitive_indices_clamp: bool,
|
||||
|
||||
/// If false, integer division and modulo operations will use raw instructions
|
||||
/// without guards against division by zero or signed integer overflow
|
||||
/// (`INT_MIN / -1`). The caller **MUST** ensure that all divisors are non-zero
|
||||
/// and that no signed overflow occurs.
|
||||
pub int_div_checks: bool,
|
||||
}
|
||||
|
||||
impl ShaderRuntimeChecks {
|
||||
@@ -85,6 +91,7 @@ impl ShaderRuntimeChecks {
|
||||
ray_query_initialization_tracking: all_checks,
|
||||
task_shader_dispatch_tracking: all_checks,
|
||||
mesh_shader_primitive_indices_clamp: all_checks,
|
||||
int_div_checks: all_checks,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user