Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lld/test/wasm/Inputs/tags.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.globl __cpp_exception
.tagtype __cpp_exception i32
__cpp_exception:

.globl __c_longjmp
.tagtype __c_longjmp i32
__c_longjmp:
16 changes: 9 additions & 7 deletions lld/test/wasm/tag-section.ll
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
; Static code
; RUN: llvm-mc -filetype=obj -triple=wasm32-unknown-unknown -o %t_tags.o %p/Inputs/tags.s

; Static code, with tags defined in tags.s
; RUN: llc -filetype=obj -wasm-enable-eh -exception-model=wasm -mattr=+exception-handling %p/Inputs/tag-section1.ll -o %t1.o
; RUN: llc -filetype=obj -wasm-enable-eh -exception-model=wasm -mattr=+exception-handling %p/Inputs/tag-section2.ll -o %t2.o
; RUN: llc -filetype=obj -wasm-enable-eh -exception-model=wasm -mattr=+exception-handling %s -o %t.o
; RUN: wasm-ld -o %t.wasm %t.o %t1.o %t2.o
; RUN: wasm-ld --export-all -o %t-export-all.wasm %t.o %t1.o %t2.o
; RUN: wasm-ld -o %t.wasm %t.o %t1.o %t2.o %t_tags.o
; RUN: wasm-ld --export-all -o %t-export-all.wasm %t.o %t1.o %t2.o %t_tags.o
; RUN: obj2yaml %t.wasm | FileCheck %s --check-prefix=NOPIC
; RUN: obj2yaml %t-export-all.wasm | FileCheck %s --check-prefix=NOPIC-EXPORT-ALL

; PIC code
; PIC code with tags imported
; RUN: llc -filetype=obj -wasm-enable-eh -exception-model=wasm -mattr=+exception-handling -relocation-model=pic %p/Inputs/tag-section1.ll -o %t1.o
; RUN: llc -filetype=obj -wasm-enable-eh -exception-model=wasm -mattr=+exception-handling -relocation-model=pic %p/Inputs/tag-section2.ll -o %t2.o
; RUN: llc -filetype=obj -wasm-enable-eh -exception-model=wasm -mattr=+exception-handling -relocation-model=pic %s -o %t.o
; RUN: wasm-ld --import-undefined --experimental-pic --unresolved-symbols=import-dynamic -pie -o %t.wasm %t.o %t1.o %t2.o
; RUN: obj2yaml %t.wasm | FileCheck %s --check-prefix=PIC
; RUN: wasm-ld --import-undefined --experimental-pic --unresolved-symbols=import-dynamic -pie -o %t_pic.wasm %t.o %t1.o %t2.o
; RUN: obj2yaml %t_pic.wasm | FileCheck %s --check-prefix=PIC

target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128"
target triple = "wasm32-unknown-emscripten"
Expand Down Expand Up @@ -49,7 +51,7 @@ define void @_start() {
; NOPIC-EXPORT-ALL: Kind: TAG
; NOPIC-EXPORT-ALL: Index: 0

; In PIC mode, tags are undefined and imported from JS.
; In PIC mode, we leave the tags as undefined and they should be imported
; PIC: Sections:
; PIC: - Type: TYPE
; PIC-NEXT: Signatures:
Expand Down
23 changes: 0 additions & 23 deletions llvm/lib/CodeGen/AsmPrinter/WasmException.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,6 @@
#include "llvm/MC/MCStreamer.h"
using namespace llvm;

void WasmException::endModule() {
// These are symbols used to throw/catch C++ exceptions and C longjmps. These
// symbols have to be emitted somewhere once in the module. Check if each of
// the symbols has already been created, i.e., we have at least one 'throw' or
// 'catch' instruction with the symbol in the module, and emit the symbol only
// if so.
//
// But in dynamic linking, it is in general not possible to come up with a
// module instantiating order in which tag-defining modules are loaded before
// the importing modules. So we make them undefined symbols here, define tags
// in the JS side, and feed them to each importing module.
if (!Asm->isPositionIndependent()) {
for (const char *SymName : {"__cpp_exception", "__c_longjmp"}) {
SmallString<60> NameStr;
Mangler::getNameWithPrefix(NameStr, SymName, Asm->getDataLayout());
if (Asm->OutContext.lookupSymbol(NameStr)) {
MCSymbol *ExceptionSym = Asm->GetExternalSymbolSymbol(SymName);
Asm->OutStreamer->emitLabel(ExceptionSym);
}
}
}
}

void WasmException::endFunction(const MachineFunction *MF) {
bool ShouldEmitExceptionTable = false;
for (const LandingPadInfo &Info : MF->getLandingPads()) {
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/CodeGen/AsmPrinter/WasmException.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class LLVM_LIBRARY_VISIBILITY WasmException : public EHStreamer {
public:
WasmException(AsmPrinter *A) : EHStreamer(A) {}

void endModule() override;
void endModule() override {}
void beginFunction(const MachineFunction *MF) override {}
void endFunction(const MachineFunction *MF) override;

Expand Down
7 changes: 0 additions & 7 deletions llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,13 +249,6 @@ MCSymbol *WebAssemblyAsmPrinter::getOrCreateWasmSymbol(StringRef Name) {
SmallVector<wasm::ValType, 4> Params;
if (Name == "__cpp_exception" || Name == "__c_longjmp") {
WasmSym->setType(wasm::WASM_SYMBOL_TYPE_TAG);
// In static linking we define tag symbols in WasmException::endModule().
// But we may have multiple objects to be linked together, each of which
// defines the tag symbols. To resolve them, we declare them as weak. In
// dynamic linking we make tag symbols undefined in the backend, define it
// in JS, and feed them to each importing module.
if (!isPositionIndependent())
WasmSym->setWeak(true);
WasmSym->setExternal(true);

// Currently both C++ exceptions and C longjmps have a single pointer type
Expand Down
3 changes: 2 additions & 1 deletion llvm/test/CodeGen/WebAssembly/exception-legacy.ll
Original file line number Diff line number Diff line change
Expand Up @@ -573,4 +573,5 @@ attributes #0 = { nounwind }
attributes #1 = { noreturn }
attributes #2 = { noreturn nounwind }

; CHECK: __cpp_exception:
;; The exception tag should not be defined locally
; CHECK-NOT: __cpp_exception:
3 changes: 2 additions & 1 deletion llvm/test/CodeGen/WebAssembly/exception.ll
Original file line number Diff line number Diff line change
Expand Up @@ -670,4 +670,5 @@ attributes #0 = { nounwind }
attributes #1 = { noreturn }
attributes #2 = { noreturn nounwind }

; CHECK: __cpp_exception:
;; The exception tag should not be defined locally
; CHECK-NOT: __cpp_exception: