From fe369c1dbb4a718bd4d7b39cc0bef307e9998fcc Mon Sep 17 00:00:00 2001 From: badumbatish Date: Wed, 25 Jun 2025 14:29:56 -0700 Subject: [PATCH 1/7] 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 } +@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 Date: Wed, 25 Jun 2025 18:58:55 -0700 Subject: [PATCH 2/7] [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@GOT -; CHECK-NEXT: i32.load $push1=, 40($pop0) +; CHECK-NEXT: i32.const $push0=, 40 +; CHECK-NEXT: i32.load $push1=, g@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@GOT -; CHECK-NEXT: i32.store 40($pop0), $0 +; CHECK-NEXT: i32.const $push0=, 40 +; CHECK-NEXT: i32.store g@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@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@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@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 From 0a00fdaa2b02832067d8cccde8a07d520caa80bc Mon Sep 17 00:00:00 2001 From: badumbatish <--show-origin> Date: Tue, 1 Jul 2025 13:24:23 -0700 Subject: [PATCH 3/7] [WebAssembly] Fix nit from PR 145829 --- .../WebAssembly/WebAssemblyISelDAGToDAG.cpp | 4 +-- .../CodeGen/WebAssembly/exception-legacy.ll | 2 +- llvm/test/CodeGen/WebAssembly/offset.ll | 36 +++++++++---------- 3 files changed, 20 insertions(+), 22 deletions(-) diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyISelDAGToDAG.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyISelDAGToDAG.cpp index 38ce956bf9030..6c4f1337f9290 100644 --- a/llvm/lib/Target/WebAssembly/WebAssemblyISelDAGToDAG.cpp +++ b/llvm/lib/Target/WebAssembly/WebAssemblyISelDAGToDAG.cpp @@ -343,11 +343,11 @@ bool WebAssemblyDAGToDAGISel::SelectAddrAddOperands(MVT OffsetType, SDValue N, if (N.getOpcode() == ISD::ADD && !N.getNode()->getFlags().hasNoUnsignedWrap()) return false; - // Folds constants in an add into the offset. for (size_t i = 0; i < 2; ++i) { SDValue Op = N.getOperand(i); SDValue OtherOp = N.getOperand(i == 0 ? 1 : 0); + // Folds constants in an add into the offset. if (ConstantSDNode *CN = dyn_cast(Op)) { Offset = CurDAG->getTargetConstant(CN->getZExtValue(), SDLoc(N), OffsetType); @@ -355,7 +355,7 @@ bool WebAssemblyDAGToDAGISel::SelectAddrAddOperands(MVT OffsetType, SDValue N, return true; } - // Fold Add of Global Address straight into load + // Fold target global addresses into the offset. if (Op.getOpcode() == WebAssemblyISD::Wrapper) Op = Op.getOperand(0); diff --git a/llvm/test/CodeGen/WebAssembly/exception-legacy.ll b/llvm/test/CodeGen/WebAssembly/exception-legacy.ll index 870ed3971672a..e07b181b5108d 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}} {{.*}} 4 +; CHECK: i32.store __wasm_lpad_context ; 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 293fa6396f4ce..1a9dc30f80a64 100644 --- a/llvm/test/CodeGen/WebAssembly/offset.ll +++ b/llvm/test/CodeGen/WebAssembly/offset.ll @@ -60,28 +60,26 @@ define i32 @load_i32_with_folded_gep_offset_nuw(ptr %p) { ret i32 %t } -@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.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 - ret i8 %0 +@global_data = external global i32 + +define i32 @load_i32_global_with_folded_gep_offset_nonconst_nuw(i32 %idx) { +; CHECK-LABEL: load_i32_global_with_folded_gep_offset_nonconst_nuw: +; CHECK: i32.const $push0=, 2 +; CHECK-NEXT: i32.shl $push1=, $0, $pop0 +; CHECK-NOT: i32.add +; CHECK-NEXT: i32.load $push2=, global_data($pop1) + %s = getelementptr nuw i32, ptr @global_data, i32 %idx + %t = load i32, ptr %s + ret i32 %t } -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 +define i32 @load_i32_global_with_folded_gep_offset_const_nuw() { +; CHECK-LABEL: load_i32_global_with_folded_gep_offset_const_nuw: +; CHECK: i32.const $push0=, 8 +; CHECK-NEXT: i32.load $push1=, global_data($pop0) entry: - %0 = load i8, ptr getelementptr inbounds nuw (i8, ptr @global_data, i32 2), align 1 - ret i8 %0 + %t = load i32, ptr getelementptr nuw (i32, ptr @global_data, i32 2) + ret i32 %t } From 91b42d5b90b9850117481a6072acfbbcdcf55b8c Mon Sep 17 00:00:00 2001 From: badumbatish <--show-origin> Date: Tue, 1 Jul 2025 14:51:23 -0700 Subject: [PATCH 4/7] [WebAssembly] Add guard for global folding --- .../WebAssembly/WebAssemblyISelDAGToDAG.cpp | 16 +++++++++------- llvm/test/CodeGen/WebAssembly/address-offsets.ll | 8 ++++---- llvm/test/CodeGen/WebAssembly/eh-lsda.ll | 3 ++- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyISelDAGToDAG.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyISelDAGToDAG.cpp index 6c4f1337f9290..ac819cf5c1801 100644 --- a/llvm/lib/Target/WebAssembly/WebAssemblyISelDAGToDAG.cpp +++ b/llvm/lib/Target/WebAssembly/WebAssemblyISelDAGToDAG.cpp @@ -356,13 +356,15 @@ bool WebAssemblyDAGToDAGISel::SelectAddrAddOperands(MVT OffsetType, SDValue N, } // Fold target global addresses into the offset. - if (Op.getOpcode() == WebAssemblyISD::Wrapper) - Op = Op.getOperand(0); - - if (Op.getOpcode() == ISD::TargetGlobalAddress) { - Addr = OtherOp; - Offset = Op; - return true; + if (!TM.isPositionIndependent()) { + 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 d4adc75a7b4fc..3f1ee592c8c61 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: i32.const $push0=, 40 -; CHECK-NEXT: i32.load $push1=, g@GOT($pop0) +; CHECK-NEXT: global.get $push0=, g@GOT +; CHECK-NEXT: i32.load $push1=, 40($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: i32.const $push0=, 40 -; CHECK-NEXT: i32.store g@GOT($pop0), $0 +; CHECK-NEXT: global.get $push0=, g@GOT +; CHECK-NEXT: i32.store 40($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 edebbd991f147..aae8bc93afd3a 100644 --- a/llvm/test/CodeGen/WebAssembly/eh-lsda.ll +++ b/llvm/test/CodeGen/WebAssembly/eh-lsda.ll @@ -73,10 +73,11 @@ try.cont: ; preds = %entry, %catch.start ; 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@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@MBREL ; PIC-NEXT: i[[PTR]].add $push[[EXCEPT_TABLE:.*]]=, $pop[[MEMORY_BASE]], $pop[[EXCEPT_TABLE_REL]] -; PIC-NEXT: i[[PTR]].store __wasm_lpad_context@GOT(${{.*}}), $pop[[EXCEPT_TABLE]] +; PIC-NEXT: i[[PTR]].store {{[48]}}($[[CONTEXT_LOCAL]]), $pop[[EXCEPT_TABLE]] ; CHECK: .section .rodata.gcc_except_table,"",@ ; CHECK-NEXT: .p2align 2 From 65f68c3111a0eca3984a7cb7b8b03e9ca4ed4741 Mon Sep 17 00:00:00 2001 From: badumbatish <--show-origin> Date: Wed, 2 Jul 2025 08:58:53 -0700 Subject: [PATCH 5/7] [WebAssembly] Fix nits for PR 145829 Addresses reusing same global data, and realignment of test case. --- llvm/test/CodeGen/WebAssembly/eh-lsda.ll | 18 +++++----- .../CodeGen/WebAssembly/exception-legacy.ll | 2 +- llvm/test/CodeGen/WebAssembly/offset.ll | 34 ++++++------------- 3 files changed, 21 insertions(+), 33 deletions(-) diff --git a/llvm/test/CodeGen/WebAssembly/eh-lsda.ll b/llvm/test/CodeGen/WebAssembly/eh-lsda.ll index aae8bc93afd3a..6ebeb820a9b11 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 -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 +; 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 @_ZTIi = external constant ptr @_ZTIf = external constant ptr @@ -66,18 +66,18 @@ 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:.*]]=, [[ALIGNMENT]] +; NOPIC: i[[PTR]].const $push[[CONTEXT:.*]]=, {{[48]}} ; NOPIC-NEXT: i[[PTR]].const $push[[EXCEPT_TABLE:.*]]=, GCC_except_table1 ; 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@GOT -; PIC-NEXT: local.tee $push{{.*}}=, $[[CONTEXT_LOCAL:.*]]=, $pop[[CONTEXT]] +; 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@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 {{[48]}}($[[CONTEXT_LOCAL]]), $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 e07b181b5108d..149e443903a88 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 __wasm_lpad_context +; CHECK: i32.store __wasm_lpad_context ; 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 1a9dc30f80a64..cd84204a21ea1 100644 --- a/llvm/test/CodeGen/WebAssembly/offset.ll +++ b/llvm/test/CodeGen/WebAssembly/offset.ll @@ -60,29 +60,6 @@ define i32 @load_i32_with_folded_gep_offset_nuw(ptr %p) { ret i32 %t } -@global_data = external global i32 - -define i32 @load_i32_global_with_folded_gep_offset_nonconst_nuw(i32 %idx) { -; CHECK-LABEL: load_i32_global_with_folded_gep_offset_nonconst_nuw: -; CHECK: i32.const $push0=, 2 -; CHECK-NEXT: i32.shl $push1=, $0, $pop0 -; CHECK-NOT: i32.add -; CHECK-NEXT: i32.load $push2=, global_data($pop1) - %s = getelementptr nuw i32, ptr @global_data, i32 %idx - %t = load i32, ptr %s - ret i32 %t -} - -define i32 @load_i32_global_with_folded_gep_offset_const_nuw() { -; CHECK-LABEL: load_i32_global_with_folded_gep_offset_const_nuw: -; CHECK: i32.const $push0=, 8 -; CHECK-NEXT: i32.load $push1=, global_data($pop0) -entry: - %t = load i32, ptr getelementptr nuw (i32, ptr @global_data, i32 2) - ret i32 %t -} - - ; We can't fold a negative offset though, even with an inbounds gep. ; CHECK-LABEL: load_i32_with_unfolded_gep_negative_offset: @@ -141,6 +118,17 @@ define i32 @load_i32_from_global_address() { ret i32 %t } +define i32 @load_i32_global_with_folded_gep_offset_nonconst_nuw(i32 %idx) { +; CHECK-LABEL: load_i32_global_with_folded_gep_offset_nonconst_nuw: +; CHECK: i32.const $push0=, 2 +; CHECK-NEXT: i32.shl $push1=, $0, $pop0 +; CHECK-NOT: i32.add +; CHECK-NEXT: i32.load $push2=, gv($pop1) + %s = getelementptr nuw i32, ptr @gv, i32 %idx + %t = load i32, ptr %s + ret i32 %t +} + ;===---------------------------------------------------------------------------- ; Loads: 64-bit ;===---------------------------------------------------------------------------- From 3e7f049d5bebb3185d6a248c9053c5f74ec3b881 Mon Sep 17 00:00:00 2001 From: jjasmine Date: Wed, 2 Jul 2025 17:02:09 -0700 Subject: [PATCH 6/7] Apply suggestions from nit Co-authored-by: Luke Lau --- llvm/test/CodeGen/WebAssembly/eh-lsda.ll | 12 ++++++------ llvm/test/CodeGen/WebAssembly/offset.ll | 7 +++---- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/llvm/test/CodeGen/WebAssembly/eh-lsda.ll b/llvm/test/CodeGen/WebAssembly/eh-lsda.ll index 6ebeb820a9b11..181dfaa953365 100644 --- a/llvm/test/CodeGen/WebAssembly/eh-lsda.ll +++ b/llvm/test/CodeGen/WebAssembly/eh-lsda.ll @@ -1,7 +1,7 @@ -; 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-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 @@ -73,11 +73,11 @@ try.cont: ; preds = %entry, %catch.start ; 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@GOT -; PIC-NEXT: local.tee $push{{.*}}=, $[[CONTEXT_LOCAL:.*]]=, $pop[[CONTEXT]] +; 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@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 {{[48]}}($[[CONTEXT_LOCAL]]), $pop[[EXCEPT_TABLE]] ; CHECK: .section .rodata.gcc_except_table,"",@ ; CHECK-NEXT: .p2align 2 diff --git a/llvm/test/CodeGen/WebAssembly/offset.ll b/llvm/test/CodeGen/WebAssembly/offset.ll index cd84204a21ea1..109607c914271 100644 --- a/llvm/test/CodeGen/WebAssembly/offset.ll +++ b/llvm/test/CodeGen/WebAssembly/offset.ll @@ -120,10 +120,9 @@ define i32 @load_i32_from_global_address() { define i32 @load_i32_global_with_folded_gep_offset_nonconst_nuw(i32 %idx) { ; CHECK-LABEL: load_i32_global_with_folded_gep_offset_nonconst_nuw: -; CHECK: i32.const $push0=, 2 -; CHECK-NEXT: i32.shl $push1=, $0, $pop0 -; CHECK-NOT: i32.add -; CHECK-NEXT: i32.load $push2=, gv($pop1) +; CHECK: i32.const $push0=, 2 +; CHECK: i32.shl $push1=, $0, $pop0 +; CHECK: i32.load $push2=, gv($pop1) %s = getelementptr nuw i32, ptr @gv, i32 %idx %t = load i32, ptr %s ret i32 %t From 6418cf684620e2a6429ddf2784706bb4da36f1f2 Mon Sep 17 00:00:00 2001 From: Luke Lau Date: Thu, 3 Jul 2025 10:54:41 +0100 Subject: [PATCH 7/7] Fix whitespace --- llvm/test/CodeGen/WebAssembly/eh-lsda.ll | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/llvm/test/CodeGen/WebAssembly/eh-lsda.ll b/llvm/test/CodeGen/WebAssembly/eh-lsda.ll index 181dfaa953365..d5b28b279d419 100644 --- a/llvm/test/CodeGen/WebAssembly/eh-lsda.ll +++ b/llvm/test/CodeGen/WebAssembly/eh-lsda.ll @@ -2,7 +2,7 @@ ; 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=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 @_ZTIi = external constant ptr @@ -73,11 +73,11 @@ try.cont: ; preds = %entry, %catch.start ; 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@GOT -; PIC-NEXT: local.tee $push{{.*}}=, $[[CONTEXT_LOCAL:.*]]=, $pop[[CONTEXT]] +; 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@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 {{[48]}}($[[CONTEXT_LOCAL]]), $pop[[EXCEPT_TABLE]] ; CHECK: .section .rodata.gcc_except_table,"",@ ; CHECK-NEXT: .p2align 2