[llvm] [WebAssembly] Fix trunc in FastISel (PR #138479)

Pavel Verigo via llvm-commits llvm-commits at lists.llvm.org
Sun May 4 17:42:57 PDT 2025


https://github.com/pavelverigo updated https://github.com/llvm/llvm-project/pull/138479

>From 25b3059f4e4e3fc221f0ff876e9f5b0934f9bf7a 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 ++++-
 .../CodeGen/WebAssembly/fast-isel-pr138479.ll     | 15 +++++++++++++++
 2 files changed, 19 insertions(+), 1 deletion(-)
 create mode 100644 llvm/test/CodeGen/WebAssembly/fast-isel-pr138479.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-pr138479.ll b/llvm/test/CodeGen/WebAssembly/fast-isel-pr138479.ll
new file mode 100644
index 0000000000000..2676000b968c3
--- /dev/null
+++ b/llvm/test/CodeGen/WebAssembly/fast-isel-pr138479.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