-
Notifications
You must be signed in to change notification settings - Fork 14.4k
[WebAssembly] Fix catch block type in wasm64 #124381
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
`try_table`'s `catch` or `catch_ref`'s target block's return type should be `i64` and `(i64, exnref)` in case of wasm64.
@llvm/pr-subscribers-backend-webassembly Author: Heejin Ahn (aheejin) Changes
Full diff: https://github.com/llvm/llvm-project/pull/124381.diff 3 Files Affected:
diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyCFGStackify.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyCFGStackify.cpp
index bdc1cc6d652ac6..277d353d1db100 100644
--- a/llvm/lib/Target/WebAssembly/WebAssemblyCFGStackify.cpp
+++ b/llvm/lib/Target/WebAssembly/WebAssemblyCFGStackify.cpp
@@ -852,12 +852,19 @@ void WebAssemblyCFGStackify::placeTryTableMarker(MachineBasicBlock &MBB) {
// Add a CATCH_*** clause to the TRY_TABLE. These are pseudo instructions
// following the destination END_BLOCK to simulate block return values,
// because we currently don't support them.
+ const auto &TLI =
+ *MF.getSubtarget<WebAssemblySubtarget>().getTargetLowering();
+ WebAssembly::BlockType PtrTy =
+ TLI.getPointerTy(MF.getDataLayout()) == MVT::i32
+ ? WebAssembly::BlockType::I32
+ : WebAssembly::BlockType::I64;
auto *Catch = WebAssembly::findCatch(&MBB);
switch (Catch->getOpcode()) {
case WebAssembly::CATCH:
// CATCH's destination block's return type is the extracted value type,
- // which is currently i32 for all supported tags.
- BlockMIB.addImm(int64_t(WebAssembly::BlockType::I32));
+ // which is currently the thrown value's pointer type for all supported
+ // tags.
+ BlockMIB.addImm(int64_t(PtrTy));
TryTableMIB.addImm(wasm::WASM_OPCODE_CATCH);
for (const auto &Use : Catch->uses()) {
// The only use operand a CATCH can have is the tag symbol.
diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyMCInstLower.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyMCInstLower.cpp
index f09b29472cb6b2..c633a8b437ee93 100644
--- a/llvm/lib/Target/WebAssembly/WebAssemblyMCInstLower.cpp
+++ b/llvm/lib/Target/WebAssembly/WebAssemblyMCInstLower.cpp
@@ -169,6 +169,13 @@ void WebAssemblyMCInstLower::lower(const MachineInstr *MI,
const MCInstrDesc &Desc = MI->getDesc();
unsigned NumVariadicDefs = MI->getNumExplicitDefs() - Desc.getNumDefs();
+ const MachineFunction *MF = MI->getMF();
+ const auto &TLI =
+ *MF->getSubtarget<WebAssemblySubtarget>().getTargetLowering();
+ wasm::ValType PtrTy = TLI.getPointerTy(MF->getDataLayout()) == MVT::i32
+ ? wasm::ValType::I32
+ : wasm::ValType::I64;
+
for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
const MachineOperand &MO = MI->getOperand(I);
@@ -234,11 +241,12 @@ void WebAssemblyMCInstLower::lower(const MachineInstr *MI,
// return type of the parent function.
// 2. (catch_ref ...) clause in try_table instruction. Currently all
// tags we support (cpp_exception and c_longjmp) throws a single
- // i32, so the multivalue signature for this case will be (i32,
- // exnref). Having MO_CATCH_BLOCK_SIG target flags means this is
- // a destination of a catch_ref.
- if (MO.getTargetFlags() == WebAssemblyII::MO_CATCH_BLOCK_SIG)
- Returns = {wasm::ValType::I32, wasm::ValType::EXNREF};
+ // pointer, so the multivalue signature for this case will be
+ // (ptr, exnref). Having MO_CATCH_BLOCK_SIG target flags means
+ // this is a destination of a catch_ref.
+ if (MO.getTargetFlags() == WebAssemblyII::MO_CATCH_BLOCK_SIG) {
+ Returns = {PtrTy, wasm::ValType::EXNREF};
+ }
else
getFunctionReturns(MI, Returns);
MCOp = lowerTypeIndexOperand(std::move(Returns),
diff --git a/llvm/test/CodeGen/WebAssembly/exception.ll b/llvm/test/CodeGen/WebAssembly/exception.ll
index 304664b622e800..febab822a6a9e3 100644
--- a/llvm/test/CodeGen/WebAssembly/exception.ll
+++ b/llvm/test/CodeGen/WebAssembly/exception.ll
@@ -2,6 +2,7 @@
; RUN: llc < %s -asm-verbose=false -wasm-enable-eh -wasm-use-legacy-eh=false -exception-model=wasm -mattr=+exception-handling -verify-machineinstrs -O0
; RUN: llc < %s -wasm-enable-eh -wasm-use-legacy-eh=false -exception-model=wasm -mattr=+exception-handling
; RUN: llc < %s -wasm-enable-eh -wasm-use-legacy-eh=false -exception-model=wasm -mattr=+exception-handling -filetype=obj
+; RUN: llc < %s -mtriple=wasm64-unknown-unknown -wasm-enable-eh -wasm-use-legacy-eh=false -exception-model=wasm -mattr=+exception-handling -verify-machineinstrs | FileCheck --implicit-check-not=ehgcr -allow-deprecated-dag-overlap %s --check-prefix=WASM64
target triple = "wasm32-unknown-unknown"
@@ -30,11 +31,13 @@ define void @throw(ptr %p) {
; }
; CHECK-LABEL: catch:
+; WASM64-LABEL: catch:
; CHECK: global.get __stack_pointer
; CHECK: local.set 0
; CHECK: block
; CHECK: block () -> (i32, exnref)
; CHECK: try_table (catch_ref __cpp_exception 0)
+; WASM64: block () -> (i64, exnref)
; CHECK: call foo
; CHECK: br 2
; CHECK: end_try_table
@@ -138,8 +141,10 @@ ehcleanup: ; preds = %entry
; }
; CHECK-LABEL: terminatepad
+; WASM64-LABEL: terminatepad
; CHECK: block
; CHECK: block i32
+; WASM64: block i64
; CHECK: try_table (catch __cpp_exception 0)
; CHECK: call foo
; CHECK: br 2
|
dschuff
approved these changes
Jan 27, 2025
The CI failures are irrelevant. Merging. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
try_table
'scatch
orcatch_ref
's target block's return type should bei64
and(i64, exnref)
in case of wasm64.