-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[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
Conversation
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 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. |
@llvm/pr-subscribers-backend-webassembly Author: Pavel Verigo (pavelverigo) ChangesPrevious 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:
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.
cb3b659
to
25b3059
Compare
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? |
I haven't looked at FastISel in quite a while, but last I knew, it bailed out on odd-sized bitwidths. |
@dschuff Also, note that the 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. |
@sunfishcode 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 |
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. |
There was a problem hiding this 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).
@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! |
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.
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.