Skip to content

[WebAssembly] Fix trunc in FastISel #138479

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

Merged
merged 1 commit into from
May 6, 2025
Merged

Conversation

pavelverigo
Copy link
Contributor

Previous logic did not handle the case where the result bit size was between 32 and 64 bits inclusive. I updated the if-statements for more precise handling.

An alternative solution would have been to abort FastISel in case the result type is not legal for FastISel.

Resolves: #64222.

This PR began as an investigation into the root cause of ziglang/zig#20966.

Godbolt link showing incorrect codegen on 20.1.0: https://godbolt.org/z/cEr4vY7d4.

Copy link

github-actions bot commented May 5, 2025

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot
Copy link
Member

llvmbot commented May 5, 2025

@llvm/pr-subscribers-backend-webassembly

Author: Pavel Verigo (pavelverigo)

Changes

Previous logic did not handle the case where the result bit size was between 32 and 64 bits inclusive. I updated the if-statements for more precise handling.

An alternative solution would have been to abort FastISel in case the result type is not legal for FastISel.

Resolves: #64222.

This PR began as an investigation into the root cause of ziglang/zig#20966.

Godbolt link showing incorrect codegen on 20.1.0: https://godbolt.org/z/cEr4vY7d4.


Full diff: https://github.com/llvm/llvm-project/pull/138479.diff

2 Files Affected:

  • (modified) llvm/lib/Target/WebAssembly/WebAssemblyFastISel.cpp (+4-1)
  • (added) llvm/test/CodeGen/WebAssembly/fast-isel-trunc.ll (+15)
diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyFastISel.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyFastISel.cpp
index 78c6a41624291..b2ea784057780 100644
--- a/llvm/lib/Target/WebAssembly/WebAssemblyFastISel.cpp
+++ b/llvm/lib/Target/WebAssembly/WebAssemblyFastISel.cpp
@@ -992,7 +992,10 @@ bool WebAssemblyFastISel::selectTrunc(const Instruction *I) {
   if (Reg == 0)
     return false;
 
-  if (Trunc->getOperand(0)->getType()->isIntegerTy(64)) {
+  unsigned FromBitWidth = Trunc->getOperand(0)->getType()->getIntegerBitWidth();
+  unsigned ToBitWidth = Trunc->getType()->getIntegerBitWidth();
+
+  if (ToBitWidth <= 32 && (32 < FromBitWidth && FromBitWidth <= 64)) {
     Register Result = createResultReg(&WebAssembly::I32RegClass);
     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
             TII.get(WebAssembly::I32_WRAP_I64), Result)
diff --git a/llvm/test/CodeGen/WebAssembly/fast-isel-trunc.ll b/llvm/test/CodeGen/WebAssembly/fast-isel-trunc.ll
new file mode 100644
index 0000000000000..2676000b968c3
--- /dev/null
+++ b/llvm/test/CodeGen/WebAssembly/fast-isel-trunc.ll
@@ -0,0 +1,15 @@
+; RUN: llc < %s -asm-verbose=false -fast-isel -fast-isel-abort=1 -verify-machineinstrs | FileCheck %s
+
+target triple = "wasm32-unknown-unknown"
+
+declare void @extern48(i48)
+
+; CHECK-LABEL: call_trunc_i64_to_i48:
+; CHECK:       local.get 0
+; CHECK-NEXT:  call extern48
+; CHECK-NEXT:  end_function
+define void @call_trunc_i64_to_i48(i64 %x) {
+  %x48 = trunc i64 %x to i48
+  call void @extern48(i48 %x48)
+  ret void
+}

Previous logic did not handle the case where the result bit size was between 32 and 64 bits inclusive.
I updated the if-statements for more precise handling.

An alternative solution would have been to abort FastISel in case the result type is not legal for FastISel.

Resolves: llvm#64222.
@pavelverigo
Copy link
Contributor Author

@aheejin @dschuff Would you be able to review and assign someone if needed? Thanks, Pavel.

@dschuff
Copy link
Member

dschuff commented May 5, 2025

So my first thought was that this should be emitting some sort of bitmask to zero out the top 16 bits, but I can see that the DAG isel doesn't do that either. Do you know what the calling convention is in this case? I assume you've checked that the final behavior is correct?
@sunfishcode do you recall whether fast-isel handles these odd-sized bitwidth, or those get expanded out somewhere before?

@sunfishcode
Copy link
Member

I haven't looked at FastISel in quite a while, but last I knew, it bailed out on odd-sized bitwidths.

@pavelverigo
Copy link
Contributor Author

@dschuff
The trunc instruction doesn't perform any zero/sign extension, it simply changes the operand's local type if needed. Any zero/sign extension behavior occurs during lowering of specific instructions (like icmp), not at the trunc itself.

Also, note that the call instruction here isn't lowered by FastISel, it fails (and therefore being lowered by SelectionDAG) out due to i48 not being in the supported set of "simple types" (i1, i8, i16, i32, i64, etc). However, in SelectionDAGISel.cpp, if FastISel fails on a CallInst, it still proceeds with selection rather than aborting whole block, which is why my test doesn't just trunc to i48 and return it (this would not make FastISel doing wrong codegen on trunc).

As for correctness: I believe SelectionDAG gets it right. You can confirm by removing -fast-isel in the Godbolt link I included. The added test fails without the patch, which I’ve verified.

@pavelverigo
Copy link
Contributor Author

pavelverigo commented May 6, 2025

@sunfishcode
Yes, that's correct. FastISel does bail out on "odd-sized" bitwidths. Specifically, getRegForValue returns 0 for unsupported types, which causes selectTrunc to fail early.

That said, the resulting type being non-simple (like i48) is still fine from FastISel's perspective, as we're just returning a register for it. The updated logic only correctly applies needed wrapping if the result is <= 32 bits.

Though, I'm fine with updating the PR to bail out on non-legal result types in trunc if that's the preferred solution.

@dschuff
Copy link
Member

dschuff commented May 6, 2025

I think this change is actually fine, with that explanation. Mostly (aside from the obvious question of "are we sure it's correct" which you answered), I just wanted to make sure that this would be useful; i.e. we wouldn't just immediately run into some other case that caused a bailout for the block, or if we would fall into some indefinite cascade of other things we'd need to add that might be beyond the scope we want. If this change fixes the problem (and it's not just equivalent to directly bailing out), then I'd rather do it than bail out. In general I suspect our fast isel could stand to be maybe a little more complete. I haven't actually looked at stats about how often it bails out recently, but as @sunfishcode said, nobody has looked at it in a long time and it hasn't really been updated with new proposals. So all that to say, I'd rather have this simple addition than a bailout, all else being equal.

Copy link
Member

@dschuff dschuff left a comment

Choose a reason for hiding this comment

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

I'll merge this after the CI finishes (unless you want to do it yourself).

@dschuff dschuff merged commit 9d89b05 into llvm:main May 6, 2025
11 checks passed
Copy link

github-actions bot commented May 6, 2025

@pavelverigo Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

GeorgeARM pushed a commit to GeorgeARM/llvm-project that referenced this pull request May 7, 2025
Previous logic did not handle the case where the result bit size was
between 32 and 64 bits inclusive. I updated the if-statements for more
precise handling.

An alternative solution would have been to abort FastISel in case the
result type is not legal for FastISel.

Resolves: llvm#64222.

This PR began as an investigation into the root cause of
ziglang/zig#20966.

Godbolt link showing incorrect codegen on 20.1.0:
https://godbolt.org/z/cEr4vY7d4.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[wasm] trunc + sadd.with.overflow produces output that does not validate
4 participants