[llvm] [WebAssembly] Fold unsigned offset (PR #145829)
via llvm-commits
llvm-commits at lists.llvm.org
Tue Jul 1 10:51:59 PDT 2025
https://github.com/badumbatish updated https://github.com/llvm/llvm-project/pull/145829
>From fe369c1dbb4a718bd4d7b39cc0bef307e9998fcc Mon Sep 17 00:00:00 2001
From: badumbatish <jjasmine at igalia.com>
Date: Wed, 25 Jun 2025 14:29:56 -0700
Subject: [PATCH 1/2] Precommit test for folding a load of unsigned offset
---
llvm/test/CodeGen/WebAssembly/offset.ll | 27 +++++++++++++++++++++++++
1 file changed, 27 insertions(+)
diff --git a/llvm/test/CodeGen/WebAssembly/offset.ll b/llvm/test/CodeGen/WebAssembly/offset.ll
index 130508424f630..47e10c1b47d42 100644
--- a/llvm/test/CodeGen/WebAssembly/offset.ll
+++ b/llvm/test/CodeGen/WebAssembly/offset.ll
@@ -60,6 +60,33 @@ define i32 @load_i32_with_folded_gep_offset_nuw(ptr %p) {
ret i32 %t
}
+ at global_data = hidden local_unnamed_addr global [12 x i8] c"Hello world\00", align 1
+
+define hidden signext i8 @global_load_i32_with_folded_gep_offset_nonconst_nuw(i32 noundef %idx) local_unnamed_addr {
+; CHECK-LABEL: global_load_i32_with_folded_gep_offset_nonconst_nuw:
+; CHECK: .functype global_load_i32_with_folded_gep_offset_nonconst_nuw (i32) -> (i32)
+; CHECK: i32.const $push0=, global_data
+; CHECK: i32.add $push1=, $0, $pop0
+; CHECK: i32.load8_s $push2=, 0($pop1)
+; CHECK: return $pop2
+entry:
+ %arrayidx = getelementptr inbounds nuw [12 x i8], ptr @global_data, i32 0, i32 %idx
+ %0 = load i8, ptr %arrayidx, align 1
+ ret i8 %0
+}
+
+define hidden signext i8 @global_load_i32_with_folded_gep_offset_const_nuw() local_unnamed_addr {
+; CHECK-LABEL: global_load_i32_with_folded_gep_offset_const_nuw:
+; CHECK: .functype global_load_i32_with_folded_gep_offset_const_nuw () -> (i32)
+; CHECK: i32.const $push0=, 0
+; CHECK: i32.load8_s $push1=, global_data+2($pop0)
+; CHECK: return $pop1
+entry:
+ %0 = load i8, ptr getelementptr inbounds nuw (i8, ptr @global_data, i32 2), align 1
+ ret i8 %0
+}
+
+
; We can't fold a negative offset though, even with an inbounds gep.
; CHECK-LABEL: load_i32_with_unfolded_gep_negative_offset:
>From 3dfe71a7fd830b497760c0b28771e69b44661636 Mon Sep 17 00:00:00 2001
From: badumbatish <jjasmine at igalia.com>
Date: Wed, 25 Jun 2025 18:58:55 -0700
Subject: [PATCH 2/2] [WebAssembly] Fold nuw global add to load offset
Fold nuw global add to a load.
Fix extra tests that involves loads in exception-legacy, eh-lsda, and address-offsets.
---
.../WebAssembly/WebAssemblyISelDAGToDAG.cpp | 10 ++++++++++
.../CodeGen/WebAssembly/address-offsets.ll | 8 ++++----
llvm/test/CodeGen/WebAssembly/eh-lsda.ll | 19 +++++++++----------
.../CodeGen/WebAssembly/exception-legacy.ll | 2 +-
llvm/test/CodeGen/WebAssembly/offset.ll | 6 ++----
5 files changed, 26 insertions(+), 19 deletions(-)
diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyISelDAGToDAG.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyISelDAGToDAG.cpp
index 48c0b7e50f080..38ce956bf9030 100644
--- a/llvm/lib/Target/WebAssembly/WebAssemblyISelDAGToDAG.cpp
+++ b/llvm/lib/Target/WebAssembly/WebAssemblyISelDAGToDAG.cpp
@@ -354,6 +354,16 @@ bool WebAssemblyDAGToDAGISel::SelectAddrAddOperands(MVT OffsetType, SDValue N,
Addr = OtherOp;
return true;
}
+
+ // Fold Add of Global Address straight into load
+ if (Op.getOpcode() == WebAssemblyISD::Wrapper)
+ Op = Op.getOperand(0);
+
+ if (Op.getOpcode() == ISD::TargetGlobalAddress) {
+ Addr = OtherOp;
+ Offset = Op;
+ return true;
+ }
}
return false;
}
diff --git a/llvm/test/CodeGen/WebAssembly/address-offsets.ll b/llvm/test/CodeGen/WebAssembly/address-offsets.ll
index 3f1ee592c8c61..d4adc75a7b4fc 100644
--- a/llvm/test/CodeGen/WebAssembly/address-offsets.ll
+++ b/llvm/test/CodeGen/WebAssembly/address-offsets.ll
@@ -12,8 +12,8 @@ define i32 @load_test0() {
; CHECK-LABEL: load_test0:
; CHECK: .functype load_test0 () -> (i32)
; CHECK-NEXT: # %bb.0:
-; CHECK-NEXT: global.get $push0=, g at GOT
-; CHECK-NEXT: i32.load $push1=, 40($pop0)
+; CHECK-NEXT: i32.const $push0=, 40
+; CHECK-NEXT: i32.load $push1=, g at GOT($pop0)
; CHECK-NEXT: return $pop1
%t = load i32, ptr getelementptr inbounds ([0 x i32], ptr @g, i32 0, i32 10), align 4
ret i32 %t
@@ -395,8 +395,8 @@ define void @store_test0(i32 %i) {
; CHECK-LABEL: store_test0:
; CHECK: .functype store_test0 (i32) -> ()
; CHECK-NEXT: # %bb.0:
-; CHECK-NEXT: global.get $push0=, g at GOT
-; CHECK-NEXT: i32.store 40($pop0), $0
+; CHECK-NEXT: i32.const $push0=, 40
+; CHECK-NEXT: i32.store g at GOT($pop0), $0
; CHECK-NEXT: return
store i32 %i, ptr getelementptr inbounds ([0 x i32], ptr @g, i32 0, i32 10), align 4
ret void
diff --git a/llvm/test/CodeGen/WebAssembly/eh-lsda.ll b/llvm/test/CodeGen/WebAssembly/eh-lsda.ll
index ce55f7620e14a..edebbd991f147 100644
--- a/llvm/test/CodeGen/WebAssembly/eh-lsda.ll
+++ b/llvm/test/CodeGen/WebAssembly/eh-lsda.ll
@@ -1,9 +1,9 @@
-; RUN: llc < %s --mtriple=wasm32-unknown-unknown -wasm-disable-explicit-locals -wasm-keep-registers -wasm-enable-eh -exception-model=wasm -mattr=+exception-handling | FileCheck %s -check-prefixes=CHECK,NOPIC -DPTR=32
-; RUN: llc < %s --mtriple=wasm64-unknown-unknown -wasm-disable-explicit-locals -wasm-keep-registers -wasm-enable-eh -exception-model=wasm -mattr=+exception-handling | FileCheck %s -check-prefixes=CHECK,NOPIC -DPTR=64
-; RUN: llc < %s --mtriple=wasm32-unknown-emscripten -wasm-disable-explicit-locals -wasm-keep-registers -wasm-enable-eh -exception-model=wasm -mattr=+exception-handling | FileCheck %s -check-prefixes=CHECK,NOPIC -DPTR=32
-; RUN: llc < %s --mtriple=wasm64-unknown-emscripten -wasm-disable-explicit-locals -wasm-keep-registers -wasm-enable-eh -exception-model=wasm -mattr=+exception-handling | FileCheck %s -check-prefixes=CHECK,NOPIC -DPTR=64
-; RUN: llc < %s --mtriple=wasm32-unknown-emscripten -wasm-disable-explicit-locals -wasm-keep-registers -wasm-enable-eh -exception-model=wasm -mattr=+exception-handling -relocation-model=pic | FileCheck %s -check-prefixes=CHECK,PIC -DPTR=32
-; RUN: llc < %s --mtriple=wasm64-unknown-emscripten -wasm-disable-explicit-locals -wasm-keep-registers -wasm-enable-eh -exception-model=wasm -mattr=+exception-handling -relocation-model=pic | FileCheck %s -check-prefixes=CHECK,PIC -DPTR=64
+; RUN: llc < %s --mtriple=wasm32-unknown-unknown -wasm-disable-explicit-locals -wasm-keep-registers -wasm-enable-eh -exception-model=wasm -mattr=+exception-handling | FileCheck %s -check-prefixes=CHECK,NOPIC -DPTR=32 -DALIGNMENT=4
+; RUN: llc < %s --mtriple=wasm64-unknown-unknown -wasm-disable-explicit-locals -wasm-keep-registers -wasm-enable-eh -exception-model=wasm -mattr=+exception-handling | FileCheck %s -check-prefixes=CHECK,NOPIC -DPTR=64 -DALIGNMENT=8
+; RUN: llc < %s --mtriple=wasm32-unknown-emscripten -wasm-disable-explicit-locals -wasm-keep-registers -wasm-enable-eh -exception-model=wasm -mattr=+exception-handling | FileCheck %s -check-prefixes=CHECK,NOPIC -DPTR=32 -DALIGNMENT=4
+; RUN: llc < %s --mtriple=wasm64-unknown-emscripten -wasm-disable-explicit-locals -wasm-keep-registers -wasm-enable-eh -exception-model=wasm -mattr=+exception-handling | FileCheck %s -check-prefixes=CHECK,NOPIC -DPTR=64 -DALIGNMENT=8
+; RUN: llc < %s --mtriple=wasm32-unknown-emscripten -wasm-disable-explicit-locals -wasm-keep-registers -wasm-enable-eh -exception-model=wasm -mattr=+exception-handling -relocation-model=pic | FileCheck %s -check-prefixes=CHECK,PIC -DPTR=32 -DALIGNMENT=4
+; RUN: llc < %s --mtriple=wasm64-unknown-emscripten -wasm-disable-explicit-locals -wasm-keep-registers -wasm-enable-eh -exception-model=wasm -mattr=+exception-handling -relocation-model=pic | FileCheck %s -check-prefixes=CHECK,PIC -DPTR=64 -DALIGNMENT=8
@_ZTIi = external constant ptr
@_ZTIf = external constant ptr
@@ -66,18 +66,17 @@ try.cont: ; preds = %entry, %catch.start
; CHECK-LABEL: test1:
; In static linking, we load GCC_except_table as a constant directly.
-; NOPIC: i[[PTR]].const $push[[CONTEXT:.*]]=, __wasm_lpad_context
+; NOPIC: i[[PTR]].const $push[[CONTEXT:.*]]=, [[ALIGNMENT]]
; NOPIC-NEXT: i[[PTR]].const $push[[EXCEPT_TABLE:.*]]=, GCC_except_table1
-; NOPIC-NEXT: i[[PTR]].store {{[48]}}($pop[[CONTEXT]]), $pop[[EXCEPT_TABLE]]
+; NOPIC-NEXT: i[[PTR]].store __wasm_lpad_context($pop[[CONTEXT]]), $pop[[EXCEPT_TABLE]]
; In case of PIC, we make GCC_except_table symbols a relative on based on
; __memory_base.
; PIC: global.get $push[[CONTEXT:.*]]=, __wasm_lpad_context at GOT
-; PIC-NEXT: local.tee $push{{.*}}=, $[[CONTEXT_LOCAL:.*]]=, $pop[[CONTEXT]]
; PIC: global.get $push[[MEMORY_BASE:.*]]=, __memory_base
; PIC-NEXT: i[[PTR]].const $push[[EXCEPT_TABLE_REL:.*]]=, GCC_except_table1 at MBREL
; PIC-NEXT: i[[PTR]].add $push[[EXCEPT_TABLE:.*]]=, $pop[[MEMORY_BASE]], $pop[[EXCEPT_TABLE_REL]]
-; PIC-NEXT: i[[PTR]].store {{[48]}}($[[CONTEXT_LOCAL]]), $pop[[EXCEPT_TABLE]]
+; PIC-NEXT: i[[PTR]].store __wasm_lpad_context at GOT(${{.*}}), $pop[[EXCEPT_TABLE]]
; CHECK: .section .rodata.gcc_except_table,"",@
; CHECK-NEXT: .p2align 2
diff --git a/llvm/test/CodeGen/WebAssembly/exception-legacy.ll b/llvm/test/CodeGen/WebAssembly/exception-legacy.ll
index b4ffd185e3ca8..870ed3971672a 100644
--- a/llvm/test/CodeGen/WebAssembly/exception-legacy.ll
+++ b/llvm/test/CodeGen/WebAssembly/exception-legacy.ll
@@ -34,7 +34,7 @@ define void @throw(ptr %p) {
; CHECK: call foo
; CHECK: catch $[[EXN:[0-9]+]]=, __cpp_exception
; CHECK: global.set __stack_pointer
-; CHECK: i32.{{store|const}} {{.*}} __wasm_lpad_context
+; CHECK: i32.{{store|const}} {{.*}} 4
; CHECK: call $drop=, _Unwind_CallPersonality, $[[EXN]]
; CHECK: block
; CHECK: br_if 0
diff --git a/llvm/test/CodeGen/WebAssembly/offset.ll b/llvm/test/CodeGen/WebAssembly/offset.ll
index 47e10c1b47d42..293fa6396f4ce 100644
--- a/llvm/test/CodeGen/WebAssembly/offset.ll
+++ b/llvm/test/CodeGen/WebAssembly/offset.ll
@@ -65,10 +65,8 @@ define i32 @load_i32_with_folded_gep_offset_nuw(ptr %p) {
define hidden signext i8 @global_load_i32_with_folded_gep_offset_nonconst_nuw(i32 noundef %idx) local_unnamed_addr {
; CHECK-LABEL: global_load_i32_with_folded_gep_offset_nonconst_nuw:
; CHECK: .functype global_load_i32_with_folded_gep_offset_nonconst_nuw (i32) -> (i32)
-; CHECK: i32.const $push0=, global_data
-; CHECK: i32.add $push1=, $0, $pop0
-; CHECK: i32.load8_s $push2=, 0($pop1)
-; CHECK: return $pop2
+; CHECK: i32.load8_s $push0=, global_data($0)
+; CHECK: return $pop0
entry:
%arrayidx = getelementptr inbounds nuw [12 x i8], ptr @global_data, i32 0, i32 %idx
%0 = load i8, ptr %arrayidx, align 1
More information about the llvm-commits
mailing list