[compiler-rt] [llvm] [WebAssembly] Explicitly set `getCmpLibcallReturnType` to i32 (PR #192425)

via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 16 03:34:33 PDT 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-backend-webassembly

Author: Trevor Gross (tgross35)

<details>
<summary>Changes</summary>

LLVM's default for `getCmpLibcallReturnType` is currently set to `i32`, but this is not exactly accurate: the default in GCC is an integer of word size. This means that on wasm64 targets, runtime libraries that are assuming word size are incorrect and instead need to be using a 32-bit integer.

The unintentional i32 default is actually a reasonable choice for Wasm because it may be slightly cheaper to operate on when running 64-bit wasm on 32-bit hosts, and there is no advantage to using a larger integer (the result of comparisons need only be able to represent three possible values). Thus, do the following:

* Make `getCmpLibcallReturnType` explicit as `i32` to prevent against future changes to the default.
* Update compiler-rt to account for this and fix wasm64.
* Note this decision in the changelog so other runtime libraries can update if needed.

GCC does not support wasm64 so there is no compatibility concern at the current time. If added, it would be reasonable for `i32` to be used there as well.

Closes: https://github.com/llvm/llvm-project/issues/75302
Closes: https://github.com/llvm/llvm-project/issues/192416

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


3 Files Affected:

- (modified) compiler-rt/lib/builtins/fp_compare_impl.inc (+3) 
- (modified) llvm/docs/ReleaseNotes.md (+4-1) 
- (modified) llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.h (+4) 


``````````diff
diff --git a/compiler-rt/lib/builtins/fp_compare_impl.inc b/compiler-rt/lib/builtins/fp_compare_impl.inc
index f883338c471d3..129d0136a0b98 100644
--- a/compiler-rt/lib/builtins/fp_compare_impl.inc
+++ b/compiler-rt/lib/builtins/fp_compare_impl.inc
@@ -15,6 +15,9 @@
 #if defined(__aarch64__) || defined(__arm64ec__)
 // AArch64 GCC overrides libgcc_cmp_return to use int instead of long.
 typedef int CMP_RESULT;
+#elif defined(__wasm__)
+// Both wasm32 and wasm64 use i32
+typedef int CMP_RESULT;
 #elif __SIZEOF_POINTER__ == 8 && __SIZEOF_LONG__ == 4
 // LLP64 ABIs use long long instead of long.
 typedef long long CMP_RESULT;
diff --git a/llvm/docs/ReleaseNotes.md b/llvm/docs/ReleaseNotes.md
index 9caeef89d0b1c..14958f5674d46 100644
--- a/llvm/docs/ReleaseNotes.md
+++ b/llvm/docs/ReleaseNotes.md
@@ -106,7 +106,7 @@ Changes to LLVM infrastructure
   ([#177046](https://github.com/llvm/llvm-project/pull/125687)). Note that
   there are still issues with invalid compiler reasoning about some functions
   in bitcode, e.g. `malloc`. Not yet supported on MachO or when using
-  distributed ThinLTO. 
+  distributed ThinLTO.
 
 Changes to building LLVM
 ------------------------
@@ -198,6 +198,9 @@ Changes to the RISC-V Backend
 Changes to the WebAssembly Backend
 ----------------------------------
 
+* The result of runtime library call comparisons is explicitly set to `i32` on
+  both wasm32 and wasm64.
+
 Changes to the Windows Target
 -----------------------------
 
diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.h b/llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.h
index 42f047840e504..5de57615dccd4 100644
--- a/llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.h
+++ b/llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.h
@@ -29,6 +29,10 @@ class WebAssemblyTargetLowering final : public TargetLowering {
   MVT getPointerTy(const DataLayout &DL, uint32_t AS = 0) const override;
   MVT getPointerMemTy(const DataLayout &DL, uint32_t AS = 0) const override;
 
+  MVT::SimpleValueType getCmpLibcallReturnType() const override {
+    // i32 may be more efficient than the default word size for wasm64 targets
+    return MVT::i32;
+  }
 private:
   /// Keep a pointer to the WebAssemblySubtarget around so that we can make the
   /// right decision when generating code for different targets.

``````````

</details>


https://github.com/llvm/llvm-project/pull/192425


More information about the llvm-commits mailing list