Skip to content

Fix asuint operator when applied to int values. - #7396

Merged
Steven Perron (s-perron) merged 1 commit into
microsoft:mainfrom
jimblandy:asuint-int-result-sign
Oct 21, 2025
Merged

Steven Perron (s-perron) merged 1 commit into
microsoft:mainfrom
jimblandy:asuint-int-result-sign

Conversation

@jimblandy

@jimblandy Jim Blandy (jimblandy) commented Apr 29, 2025 •

Copy link
Copy Markdown
Contributor

Bother to mark the APSInt result as unsigned, to avoid tripping IntExprEvaluator::Success's assertion that SI.isSigned() matches E's type.

Fixes #7395.

@jimblandy

Copy link
Copy Markdown
Contributor Author

Antonio Maiorano (@amaiorano) Hi! Corentin Wallez (@Kangz) suggested that I 'at' you on this PR. I'm working on Firefox's WebGPU implementation, and #7395 is a blocker for us transitioning from the SDK's DXC to our own builds. If you have time, could you take a look?

I was also surprised that Chrome hadn't run into this problem itself, and was a bit worried that I had missed some fixes that hadn't been upstreamed. Corentin says that, as far as he knows, all of Google's DXC fixes have been upstreamed here; is that your understanding as well?

@amaiorano

Copy link
Copy Markdown
Collaborator

Antonio Maiorano (@amaiorano) Hi! Corentin Wallez (@Kangz) suggested that I 'at' you on this PR. I'm working on Firefox's WebGPU implementation, and #7395 is a blocker for us transitioning from the SDK's DXC to our own builds. If you have time, could you take a look?

I was also surprised that Chrome hadn't run into this problem itself, and was a bit worried that I had missed some fixes that hadn't been upstreamed. Corentin says that, as far as he knows, all of Google's DXC fixes have been upstreamed here; is that your understanding as well?

All our fixes are indeed upstreamed. Just to be clear, this isn't actually a "crash", but an assertion, right? It seems to compile fine in a non-assert build: https://godbolt.org/z/a8q8zxMza

Having said that, we do enable asserts in our release build, as this has helped tremendously to reduce the number of security issues (better to trap than to crash in some random location). If you're interested in doing this for Firefox, btw, you'll probably want to take advantage of some of the changes I landed to reduce the binary size of dxcompiler (see here).

Anyway, back to this bug, I tried to write WGSL that would produce the same output as in your example, but I'm not able to. The closest I came was with this WGSL:

@compute @workgroup_size(1)
fn main() {
  // var i = u32(i32(1));
  // var i = u32(1i);
  let a = 1i;
  var i = u32(a);
}

With tint, we produce the following HLSL:

[numthreads(1, 1, 1)]
void main() {
  int a = int(1);
  uint i = uint(a);
}

Not quite the same, and this seems to compile. There may be a way to craft WGSL to make Tint produce something similar, but I haven't been able to in the past few minutes. I guess this is why we haven't run into this one yet!

@jimblandy

Jim Blandy (jimblandy) commented May 1, 2025 •

Copy link
Copy Markdown
Contributor Author

Just to be clear, this isn't actually a "crash", but an assertion, right?

Yes - sorry. It is an assertion failure. Following the Dawn team's advice, Mozilla is indeed doing our own build of DXC with assertions enabled, for the security benefits.

If you're interested in doing this for Firefox, btw, you'll probably want to take advantage of some of the changes I landed to reduce the binary size of dxcompiler (see here).

I think we've managed to get all that in our build script - thank you!

I tried to write WGSL that would produce the same output as in your example, but I'm not able to.

It may be that Naga simply leans on asuint whereas Tint prefers uint casts. I am not sure why we're using asuint.

Are you able to review the PR itself?

In any case, thank you for taking a look. It's good to know that we're on the right track.

@amaiorano

Antonio Maiorano (amaiorano) commented May 1, 2025 •

Copy link
Copy Markdown
Collaborator

I'm on my phone, so I'll review tomorrow; however, a couple of things right off the bat:

  • Since the change you made is in Clang/LLVM code, it will need the HLSL change begin/end comment tags (I forget the exact text, but it should be easy enough to find). Having said that, usually if a change is made to Clang/LLVM code, you should check what was done in upstream LLVM. If they have a similar patch, then it can be taken verbatim without the need for tags. This is preferable. (If that's what you did already, then it's fine as is).

  • Add at least a test that would have asserted before, but passes now. I'll point out some good examples for you tomorrow (or take a look at some my of recent patches).

@amaiorano

Copy link
Copy Markdown
Collaborator

Ah, okay, now that I'm on my desktop, I can see clearly that your change is in HandleIntrinsicCall, which is an HLSL change. So no need to worry about adding the // HLSL Change Starts/Ends tags (they're already around the function).

So all that's needed is a test. Take a look at this PR of mine. You can add your test somewhere under tools/clang/test/DXC/. To run tests, use ninja check-all to run them all. You can also run the command at the top of the test file manually, like in my PR, I woud do:

ninja dxc FileCheck
./bin/dxc /T ps_6_5 -fcgl ../tools/clang/test/DXC/FinishCodeGen/unreachable-discard.hlsl | ./bin/FileCheck ../tools/clang/test/DXC/FinishCodeGen/unreachable-discard.hlsl

@jimblandy

Copy link
Copy Markdown
Contributor Author

@microsoft-github-policy-service agree company="Mozilla"

@jimblandy

Copy link
Copy Markdown
Contributor Author

Antonio Maiorano (@amaiorano) Okay, I've added a test. Please take a look.

@amaiorano

Copy link
Copy Markdown
Collaborator

Antonio Maiorano (@amaiorano) Okay, I've added a test. Please take a look.

Yeah, I saw and already approved. I added Chris B (@llvm-beanz) as second reviewer (two are required).

@amaiorano

Antonio Maiorano (amaiorano) commented May 5, 2025 •

Copy link
Copy Markdown
Collaborator

Hi Jim, you can hit the "Squash and merge" when you're ready.
EDIT: Hmm, it seems it can be merged, but I would like Chris B (@llvm-beanz) to take a look.

@jimblandy

Copy link
Copy Markdown
Contributor Author

Chris B (@llvm-beanz) Would you be able to take a look at this? Firefox's WebGPU is blocked by #7395, and we'd prefer to get the fixes approved before we include them in Firefox's DXC build.

@jimblandy

Copy link
Copy Markdown
Contributor Author

Chris B (@llvm-beanz) Ping!

@amaiorano

Copy link
Copy Markdown
Collaborator

Added Gregory Roth (@pow2clk) as well. PTAL.

@jimblandy

Copy link
Copy Markdown
Contributor Author

Chris B (@llvm-beanz) Gregory Roth (@pow2clk) Friendly review ping!

@llvm-beanz Chris B (llvm-beanz) left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

One comment, otherwise looks good.

Bother to mark the APSInt result as unsigned, to avoid tripping
IntExprEvaluator::Success's assertion that SI.isSigned() matches E's
type.

Fixes microsoft#7395.
@jimblandy

Jim Blandy (jimblandy) commented Jul 21, 2025 •

Copy link
Copy Markdown
Contributor Author

Chris B (@llvm-beanz) Sorry for the delay. The revised PR should address your review comments. PTAL.

@jimblandy

Copy link
Copy Markdown
Contributor Author

Chris B (@llvm-beanz) Review ping!

@s-perron

Copy link
Copy Markdown
Collaborator

/AzurePipelines run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@s-perron
Steven Perron (s-perron) enabled auto-merge (squash) October 21, 2025 15:32
@s-perron
Steven Perron (s-perron) merged commit d70b568 into microsoft:main Oct 21, 2025
@github-project-automation github-project-automation Bot moved this from New to Done in HLSL Roadmap Oct 21, 2025
@github-project-automation github-project-automation Bot moved this from For MSFT to Triaged in HLSL Triage Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project
Status: Triaged

Development

Successfully merging this pull request may close these issues.

DXC crashes on simple HLSL input

4 participants