Skip to content

Add emit_int_div_checks option to skip integer division safety wrappers#9443

Open
kvark wants to merge 2 commits intogfx-rs:trunkfrom
kvark:int-div-checks
Open

Add emit_int_div_checks option to skip integer division safety wrappers#9443
kvark wants to merge 2 commits intogfx-rs:trunkfrom
kvark:int-div-checks

Conversation

@kvark
Copy link
Copy Markdown
Member

@kvark kvark commented Apr 18, 2026

Connections
Related to #4385

Description
SPIR-V and MSL backends wrap integer division/modulo in helper functions that guard against division by zero (and signed MIN/-1 overflow). This is correct per WGSL spec but adds ~10 extra instructions per division.

For compute shaders doing implicit GEMM convolutions, this overhead is significant: 6 divisions per im2col element × thousands of elements per tile load = measurable performance impact.

New option on both backends:
emit_int_div_checks: bool (default: true, preserving current behavior)

When set to false:
SPIR-V: emits raw OpSDiv/OpUDiv/OpSRem/OpUMod without wrapper functions
MSL: emits raw lhs/rhs and lhs%rhs without naga_div/naga_mod wrappers

Testing
All 200 naga snapshot tests pass with no changes (default is true).
Includes a SPIRV reftest.

Squash or Rebase?

Squash

Checklist

  • I self-reviewed and fully understand this PR.
  • WebGPU implementations built with wgpu may be affected behaviorally.
  • Validation and feature gates are in place to confine behavioral changes.
  • Tests demonstrate the validation and altered logic works.
  • CHANGELOG.md entries for the user-facing effects of this change are present.
  • The PR is minimal, and doesn't make sense to land as multiple PRs.
  • Commits are logically scoped and individually reviewable.
  • The PR description has enough context to understand the motivation and solution implemented.

@kvark kvark force-pushed the int-div-checks branch 2 times, most recently from 8642947 to 86836df Compare April 18, 2026 06:35
@JMS55
Copy link
Copy Markdown
Collaborator

JMS55 commented Apr 18, 2026

Can you add this (and anything else missing) to ShaderRuntimeChecks?

@kvark
Copy link
Copy Markdown
Member Author

kvark commented Apr 19, 2026

@JMS55 good idea! Done

@kvark
Copy link
Copy Markdown
Member Author

kvark commented Apr 21, 2026

I'll wait for proper review before rebasing.

Copy link
Copy Markdown
Member

@cwfitzgerald cwfitzgerald left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some small things

Comment thread naga/src/back/spv/block.rs Outdated
Comment thread CHANGELOG.md Outdated
Comment thread naga/tests/in/wgsl/int-div-unchecked.toml Outdated
kvark and others added 2 commits April 29, 2026 23:08
SPIR-V and MSL backends wrap integer division/modulo in helper functions
that guard against division by zero (and signed MIN/-1 overflow). This
is correct per WGSL spec but adds ~10 extra instructions per division.

For compute shaders doing implicit GEMM convolutions, this overhead is
significant: 6 divisions per im2col element × thousands of elements per
tile load = measurable performance impact.

New option on both backends:
  emit_int_div_checks: bool (default: true, preserving current behavior)

When set to false:
  SPIR-V: emits raw OpSDiv/OpUDiv/OpSRem/OpUMod without wrapper functions
  MSL: emits raw lhs/rhs and lhs%rhs without naga_div/naga_mod wrappers

All 200 naga snapshot tests pass with no changes (default is true).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@kvark
Copy link
Copy Markdown
Member Author

kvark commented Apr 30, 2026

@cwfitzgerald thanks for reviewing! Everything addressed now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants