[llvm] [WebAssembly] Fix trunc in FastISel (PR #138479)
Pavel Verigo via llvm-commits
llvm-commits at lists.llvm.org
Sun May 4 17:39:51 PDT 2025
https://github.com/pavelverigo created https://github.com/llvm/llvm-project/pull/138479
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 https://github.com/ziglang/zig/issues/20966.
Godbolt link showing incorrect codegen on 20.1.0: https://godbolt.org/z/cEr4vY7d4.
>From cb3b6590def2cd0dae6b985330427aa6f88b7e0b Mon Sep 17 00:00:00 2001
From: Pavel Verigo <paul.verigo at gmail.com>
Date: Mon, 5 May 2025 02:25:47 +0200
Subject: [PATCH] [WebAssembly] Fix trunc in FastISel
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.
---
.../Target/WebAssembly/WebAssemblyFastISel.cpp | 5 ++++-
llvm/test/CodeGen/WebAssembly/fast-isel-trunc.ll | 15 +++++++++++++++
2 files changed, 19 insertions(+), 1 deletion(-)
create mode 100644 llvm/test/CodeGen/WebAssembly/fast-isel-trunc.ll
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
+}
More information about the llvm-commits
mailing list