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 countTrailingZeros #2243

Merged
merged 16 commits into from
Feb 20, 2023
Merged

Add countTrailingZeros #2243

merged 16 commits into from
Feb 20, 2023

Conversation

gents83
Copy link
Contributor

@gents83 gents83 commented Feb 5, 2023

Adds countTrailingZeros from gfx-rs/wgpu#4402

Use lsb or ctz.

It uses:

  • HLSL: firstbitlow
  • GLSL: findLSB
  • MSL: ctz
  • SPV: spirv::GLOp::FindILsb

PS: It's my first integration in Naga, so please double check it carefully :)

src/back/glsl/mod.rs Outdated Show resolved Hide resolved
@evahop
Copy link
Contributor

evahop commented Feb 5, 2023

@gents83
Please disregard my review, I did not mean to start one if i did.
The behavior of countTrailingZeros is different than that of findLSB, firstbitlow etc. So the backends need to produce something equivalent. The majority of which produce -1 for zero where countTrailingZeros should return 32. I think you where much closer in your initial attempts.

I've updated a mistake from a previous comment to avoid confusion,
but something along the lines of x == 0 ? 32 : findLSB(x) - 1 seems right to me.

In the case of findLSB it's not supported bellow 400 & es 310 so there's a version check to ensure it's supported.

@evahop
Copy link
Contributor

evahop commented Feb 5, 2023

Actually that's not quite right either, since findLSB index starts at zero. Subtracting 1 puts us back at -1. I think we can just do away with the subtraction.

@gents83
Copy link
Contributor Author

gents83 commented Feb 6, 2023

It should be lsb except when zero, right?

0...0001 = 0
0...0010 = 1
0...0100 = 2
....
1...0000 = 31
0...0000 = 32

Sounds correct?

@teoxoy
Copy link
Member

teoxoy commented Feb 6, 2023

Sounds correct, so GLSL, SPV and HLSL need the following transform:

min(uint(findLSB(n)), 32)

min should be faster than branching here.

@codecov-commenter
Copy link

codecov-commenter commented Feb 11, 2023

Codecov Report

Merging #2243 (25921d0) into master (60c0fc0) will increase coverage by 0.02%.
The diff coverage is 94.11%.

📣 This organization is not using Codecov’s GitHub App Integration. We recommend you install it so Codecov can continue to function properly for your repositories. Learn more

@@            Coverage Diff             @@
##           master    gfx-rs/naga#2243      +/-   ##
==========================================
+ Coverage   82.02%   82.04%   +0.02%     
==========================================
  Files          80       80              
  Lines       43161    43246      +85     
==========================================
+ Hits        35401    35481      +80     
- Misses       7760     7765       +5     
Impacted Files Coverage Δ
src/lib.rs 33.88% <ø> (ø)
src/proc/typifier.rs 76.24% <ø> (ø)
src/valid/expression.rs 65.99% <ø> (ø)
src/back/hlsl/writer.rs 91.14% <87.50%> (-0.05%) ⬇️
src/back/glsl/mod.rs 86.20% <95.00%> (+0.07%) ⬆️
src/back/spv/block.rs 91.36% <97.29%> (+0.13%) ⬆️
src/back/msl/writer.rs 90.92% <100.00%> (+<0.01%) ⬆️
src/back/wgsl/writer.rs 91.70% <100.00%> (+<0.01%) ⬆️
src/front/wgsl/parse/conv.rs 98.97% <100.00%> (+<0.01%) ⬆️
src/proc/mod.rs 75.57% <100.00%> (+0.08%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@gents83
Copy link
Contributor Author

gents83 commented Feb 11, 2023

@evahop & @teoxoy it should be fine now
PS: sorry for the multiple commits, for some reasons I wasn't able to have makefile running correctly locally

src/back/hlsl/writer.rs Outdated Show resolved Hide resolved
Copy link
Member

@teoxoy teoxoy left a comment

Choose a reason for hiding this comment

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

Thanks for the PR, the logic looks good!
Left a few comments regarding a few bitcasts.

src/back/hlsl/writer.rs Outdated Show resolved Hide resolved
src/back/hlsl/writer.rs Outdated Show resolved Hide resolved
src/back/spv/block.rs Outdated Show resolved Hide resolved
src/back/spv/block.rs Outdated Show resolved Hide resolved
src/back/spv/block.rs Outdated Show resolved Hide resolved
@teoxoy
Copy link
Member

teoxoy commented Feb 13, 2023

Also, @evahop, thanks for helping out with this PR!

src/back/spv/block.rs Outdated Show resolved Hide resolved
@gents83 gents83 requested a review from teoxoy February 19, 2023 09:18
@gents83
Copy link
Contributor Author

gents83 commented Feb 19, 2023

Tnx for precious suggestions!

Copy link
Member

@teoxoy teoxoy left a comment

Choose a reason for hiding this comment

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

HLSL backend looks good now.
I noticed that the spv backend is not generating what it should.
I also opened #2258 to fix the countLeadingZeros impl which is not working properly for the same reasons.

src/back/spv/block.rs Outdated Show resolved Hide resolved
src/back/spv/block.rs Outdated Show resolved Hide resolved
@teoxoy
Copy link
Member

teoxoy commented Feb 20, 2023

@gents83 - just a heads up that we landed #2257, you can now use get_constant_composite

gents83 and others added 2 commits February 20, 2023 20:02
with suggestion of @teoxoy

Co-authored-by: Teodor Tanasoaia <28601907+teoxoy@users.noreply.github.com>
with suggestion of @teoxoy

Co-authored-by: Teodor Tanasoaia <28601907+teoxoy@users.noreply.github.com>
@gents83
Copy link
Contributor Author

gents83 commented Feb 20, 2023

@gents83 - just a heads up that we landed #2257, you can now use get_constant_composite

Ok - integrating this one too :)

@gents83 gents83 requested a review from teoxoy February 20, 2023 19:09
Copy link
Member

@teoxoy teoxoy left a comment

Choose a reason for hiding this comment

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

Looks good!

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.

4 participants