Index: include/llvm/BinaryFormat/Wasm.h =================================================================== --- include/llvm/BinaryFormat/Wasm.h +++ include/llvm/BinaryFormat/Wasm.h @@ -85,7 +85,6 @@ struct WasmEventType { // Kind of event. Currently only WASM_EVENT_ATTRIBUTE_EXCEPTION is possible. uint32_t Attribute; - uint32_t SigIndex; }; struct WasmEvent { @@ -98,8 +97,8 @@ StringRef Module; StringRef Field; uint8_t Kind; + uint32_t SigIndex; union { - uint32_t SigIndex; WasmGlobalType Global; WasmTable Table; WasmLimits Memory; @@ -332,7 +331,7 @@ } inline bool operator==(const WasmEventType &LHS, const WasmEventType &RHS) { - return LHS.Attribute == RHS.Attribute && LHS.SigIndex == RHS.SigIndex; + return LHS.Attribute == RHS.Attribute; } inline bool operator!=(const WasmEventType &LHS, const WasmEventType &RHS) { Index: include/llvm/Object/Wasm.h =================================================================== --- include/llvm/Object/Wasm.h +++ include/llvm/Object/Wasm.h @@ -37,16 +37,16 @@ class WasmSymbol { public: WasmSymbol(const wasm::WasmSymbolInfo &Info, - const wasm::WasmSignature *FunctionType, const wasm::WasmGlobalType *GlobalType, - const wasm::WasmEventType *EventType) - : Info(Info), FunctionType(FunctionType), GlobalType(GlobalType), - EventType(EventType) {} + const wasm::WasmEventType *EventType, + const wasm::WasmSignature *Signature) + : Info(Info), GlobalType(GlobalType), EventType(EventType), + Signature(Signature) {} const wasm::WasmSymbolInfo &Info; - const wasm::WasmSignature *FunctionType; const wasm::WasmGlobalType *GlobalType; const wasm::WasmEventType *EventType; + const wasm::WasmSignature *Signature; bool isTypeFunction() const { return Info.Kind == wasm::WASM_SYMBOL_TYPE_FUNCTION; @@ -132,6 +132,7 @@ const wasm::WasmDylinkInfo &dylinkInfo() const { return DylinkInfo; } ArrayRef types() const { return Signatures; } ArrayRef functionTypes() const { return FunctionTypes; } + ArrayRef eventTypes() const { return EventTypes; } ArrayRef imports() const { return Imports; } ArrayRef tables() const { return Tables; } ArrayRef memories() const { return Memories; } @@ -258,6 +259,7 @@ wasm::WasmDylinkInfo DylinkInfo; std::vector Signatures; std::vector FunctionTypes; + std::vector EventTypes; std::vector Tables; std::vector Memories; std::vector Globals; Index: include/llvm/ObjectYAML/WasmYAML.h =================================================================== --- include/llvm/ObjectYAML/WasmYAML.h +++ include/llvm/ObjectYAML/WasmYAML.h @@ -77,15 +77,14 @@ struct Event { uint32_t Index; uint32_t Attribute; - uint32_t SigIndex; }; struct Import { StringRef Module; StringRef Field; ExportKind Kind; + uint32_t SigIndex; union { - uint32_t SigIndex; Global GlobalImport; Table TableImport; Limits Memory; @@ -290,6 +289,7 @@ return S->Type == wasm::WASM_SEC_EVENT; } + std::vector EventTypes; std::vector Events; }; Index: lib/MC/WasmObjectWriter.cpp =================================================================== --- lib/MC/WasmObjectWriter.cpp +++ lib/MC/WasmObjectWriter.cpp @@ -127,6 +127,12 @@ uint64_t InitialValue; }; +// A wasm event to be written into the event section. +struct WasmEvent { + wasm::WasmEventType Type; + uint32_t SigIndex; +}; + // Information about a single item which is part of a COMDAT. For each data // segment or function which is in the COMDAT, there is a corresponding // WasmComdatEntry. @@ -304,7 +310,7 @@ void writeCodeSection(const MCAssembler &Asm, const MCAsmLayout &Layout, ArrayRef Functions); void writeDataSection(); - void writeEventSection(ArrayRef Events); + void writeEventSection(ArrayRef Events); void writeRelocSection(uint32_t SectionIndex, StringRef Name, std::vector &Relocations); void writeLinkingMetaDataSection( @@ -759,7 +765,7 @@ break; case wasm::WASM_EXTERNAL_EVENT: encodeULEB128(Import.Event.Attribute, W.OS); - encodeULEB128(Import.Event.SigIndex, W.OS); + encodeULEB128(Import.SigIndex, W.OS); break; default: llvm_unreachable("unsupported import kind"); @@ -803,7 +809,7 @@ endSection(Section); } -void WasmObjectWriter::writeEventSection(ArrayRef Events) { +void WasmObjectWriter::writeEventSection(ArrayRef Events) { if (Events.empty()) return; @@ -811,8 +817,8 @@ startSection(Section, wasm::WASM_SEC_EVENT); encodeULEB128(Events.size(), W.OS); - for (const wasm::WasmEventType &Event : Events) { - encodeULEB128(Event.Attribute, W.OS); + for (const WasmEvent &Event : Events) { + encodeULEB128(Event.Type.Attribute, W.OS); encodeULEB128(Event.SigIndex, W.OS); } @@ -1150,7 +1156,7 @@ SmallVector TableElems; SmallVector Imports; SmallVector Exports; - SmallVector Events; + SmallVector Events; SmallVector SymbolInfos; SmallVector, 2> InitFuncs; std::map> Comdats; @@ -1226,7 +1232,7 @@ Import.Field = WS.getName(); Import.Kind = wasm::WASM_EXTERNAL_EVENT; Import.Event.Attribute = wasm::WASM_EVENT_ATTRIBUTE_EXCEPTION; - Import.Event.SigIndex = getEventType(WS); + Import.SigIndex = getEventType(WS); Imports.push_back(Import); WasmIndices[&WS] = NumEventImports++; } @@ -1381,9 +1387,9 @@ unsigned Index; if (WS.isDefined()) { Index = NumEventImports + Events.size(); - wasm::WasmEventType Event; + WasmEvent Event; Event.SigIndex = getEventType(WS); - Event.Attribute = wasm::WASM_EVENT_ATTRIBUTE_EXCEPTION; + Event.Type.Attribute = wasm::WASM_EVENT_ATTRIBUTE_EXCEPTION; WasmIndices[&WS] = Index; Events.push_back(Event); } else { Index: lib/Object/WasmObjectFile.cpp =================================================================== --- lib/Object/WasmObjectFile.cpp +++ lib/Object/WasmObjectFile.cpp @@ -468,7 +468,7 @@ while (Count--) { wasm::WasmSymbolInfo Info; - const wasm::WasmSignature *FunctionType = nullptr; + const wasm::WasmSignature *Signature = nullptr; const wasm::WasmGlobalType *GlobalType = nullptr; const wasm::WasmEventType *EventType = nullptr; @@ -486,13 +486,13 @@ if (IsDefined) { Info.Name = readString(Ctx); unsigned FuncIndex = Info.ElementIndex - NumImportedFunctions; - FunctionType = &Signatures[FunctionTypes[FuncIndex]]; + Signature = &Signatures[FunctionTypes[FuncIndex]]; wasm::WasmFunction &Function = Functions[FuncIndex]; if (Function.SymbolName.empty()) Function.SymbolName = Info.Name; } else { wasm::WasmImport &Import = *ImportedFunctions[Info.ElementIndex]; - FunctionType = &Signatures[Import.SigIndex]; + Signature = &Signatures[Import.SigIndex]; Info.Name = Import.Field; Info.Module = Import.Module; } @@ -564,6 +564,7 @@ if (IsDefined) { Info.Name = readString(Ctx); unsigned EventIndex = Info.ElementIndex - NumImportedEvents; + Signature = &Signatures[EventTypes[EventIndex]]; wasm::WasmEvent &Event = Events[EventIndex]; EventType = &Event.Type; if (Event.SymbolName.empty()) @@ -571,6 +572,7 @@ } else { wasm::WasmImport &Import = *ImportedEvents[Info.ElementIndex]; + Signature = &Signatures[Import.SigIndex]; EventType = &Import.Event; Info.Name = Import.Field; } @@ -589,8 +591,8 @@ Twine(Info.Name), object_error::parse_failed); LinkingData.SymbolTable.emplace_back(Info); - Symbols.emplace_back(LinkingData.SymbolTable.back(), FunctionType, - GlobalType, EventType); + Symbols.emplace_back(LinkingData.SymbolTable.back(), + GlobalType, EventType, Signature); LLVM_DEBUG(dbgs() << "Adding symbol: " << Symbols.back() << "\n"); } @@ -811,7 +813,7 @@ case wasm::WASM_EXTERNAL_EVENT: NumImportedEvents++; Im.Event.Attribute = readVarint32(Ctx); - Im.Event.SigIndex = readVarint32(Ctx); + Im.SigIndex = readVarint32(Ctx); break; default: return make_error("Unexpected import kind", @@ -893,12 +895,18 @@ EventSection = Sections.size(); uint32_t Count = readVarint32(Ctx); Events.reserve(Count); + EventTypes.reserve(Count); + uint32_t NumTypes = Signatures.size(); while (Count--) { wasm::WasmEvent Event; Event.Index = NumImportedEvents + Events.size(); Event.Type.Attribute = readVaruint32(Ctx); - Event.Type.SigIndex = readVarint32(Ctx); + uint32_t Type = readVarint32(Ctx); + if (Type >= NumTypes) + return make_error("Invalid function type", + object_error::parse_failed); Events.push_back(Event); + EventTypes.push_back(Type); } if (Ctx.Ptr != Ctx.End) Index: lib/ObjectYAML/WasmYAML.cpp =================================================================== --- lib/ObjectYAML/WasmYAML.cpp +++ lib/ObjectYAML/WasmYAML.cpp @@ -111,6 +111,7 @@ static void sectionMapping(IO &IO, WasmYAML::EventSection &Section) { commonSectionMapping(IO, Section); + IO.mapOptional("EventTypes", Section.EventTypes); IO.mapOptional("Events", Section.Events); } @@ -332,8 +333,8 @@ IO.mapRequired("GlobalType", Import.GlobalImport.Type); IO.mapRequired("GlobalMutable", Import.GlobalImport.Mutable); } else if (Import.Kind == wasm::WASM_EXTERNAL_EVENT) { + IO.mapRequired("SigIndex", Import.SigIndex); IO.mapRequired("EventAttribute", Import.EventImport.Attribute); - IO.mapRequired("EventSigIndex", Import.EventImport.SigIndex); } else if (Import.Kind == wasm::WASM_EXTERNAL_TABLE) { IO.mapRequired("Table", Import.TableImport); } else if (Import.Kind == wasm::WASM_EXTERNAL_MEMORY) { @@ -444,7 +445,6 @@ void MappingTraits::mapping(IO &IO, WasmYAML::Event &Event) { IO.mapRequired("Index", Event.Index); IO.mapRequired("Attribute", Event.Attribute); - IO.mapRequired("SigIndex", Event.SigIndex); } void ScalarBitSetTraits::bitset( Index: lib/Target/WebAssembly/WebAssemblyMCInstLower.cpp =================================================================== --- lib/Target/WebAssembly/WebAssemblyMCInstLower.cpp +++ lib/Target/WebAssembly/WebAssemblyMCInstLower.cpp @@ -92,10 +92,7 @@ SmallVector Params; if (strcmp(Name, "__cpp_exception") == 0) { WasmSym->setType(wasm::WASM_SYMBOL_TYPE_EVENT); - // We can't confirm its signature index for now because there can be - // imported exceptions. Set it to be 0 for now. - WasmSym->setEventType( - {wasm::WASM_EVENT_ATTRIBUTE_EXCEPTION, /* SigIndex */ 0}); + WasmSym->setEventType({wasm::WASM_EVENT_ATTRIBUTE_EXCEPTION}); // We may have multiple C++ compilation units to be linked together, each of // which defines the exception symbol. To resolve them, we declare them as // weak. Index: test/MC/WebAssembly/event-section.ll =================================================================== --- test/MC/WebAssembly/event-section.ll +++ test/MC/WebAssembly/event-section.ll @@ -28,10 +28,10 @@ ; CHECK-NEXT: - I32 ; CHECK: - Type: EVENT +; CHECK-NEXT: EventTypes: [ 1 ] ; CHECK-NEXT: Events: ; CHECK-NEXT: - Index: 0 ; CHECK-NEXT: Attribute: 0 -; CHECK-NEXT: SigIndex: 1 ; CHECK-NEXT: - Type: CODE ; CHECK-NEXT: Relocations: Index: test/ObjectYAML/wasm/event_section.yaml =================================================================== --- test/ObjectYAML/wasm/event_section.yaml +++ test/ObjectYAML/wasm/event_section.yaml @@ -17,10 +17,10 @@ - Type: FUNCTION FunctionTypes: [ 0 ] - Type: EVENT + EventTypes: [ 1 ] Events: - Index: 0 Attribute: 0 - SigIndex: 1 - Type: CODE Relocations: - Type: R_WEBASSEMBLY_EVENT_INDEX_LEB @@ -63,10 +63,10 @@ # CHECK-NEXT: - Type: FUNCTION # CHECK-NEXT: FunctionTypes: [ 0 ] # CHECK-NEXT: - Type: EVENT +# CHECK-NEXT: EventTypes: [ 1 ] # CHECK-NEXT: Events: # CHECK-NEXT: - Index: 0 # CHECK-NEXT: Attribute: 0 -# CHECK-NEXT: SigIndex: 1 # CHECK-NEXT: - Type: CODE # CHECK-NEXT: Relocations: # CHECK-NEXT: - Type: R_WEBASSEMBLY_EVENT_INDEX_LEB Index: tools/obj2yaml/wasm2yaml.cpp =================================================================== --- tools/obj2yaml/wasm2yaml.cpp +++ tools/obj2yaml/wasm2yaml.cpp @@ -194,7 +194,7 @@ break; case wasm::WASM_EXTERNAL_EVENT: Im.EventImport.Attribute = Import.Event.Attribute; - Im.EventImport.SigIndex = Import.Event.SigIndex; + Im.SigIndex = Import.SigIndex; break; case wasm::WASM_EXTERNAL_TABLE: Im.TableImport = make_table(Import.Table); @@ -247,11 +247,13 @@ } case wasm::WASM_SEC_EVENT: { auto EventSec = make_unique(); + for (const auto &Func : Obj.eventTypes()) { + EventSec->EventTypes.push_back(Func); + } for (auto &Event : Obj.events()) { WasmYAML::Event E; E.Index = Event.Index; E.Attribute = Event.Type.Attribute; - E.SigIndex = Event.Type.SigIndex; EventSec->Events.push_back(E); } S = std::move(EventSec); Index: tools/yaml2obj/yaml2wasm.cpp =================================================================== --- tools/yaml2obj/yaml2wasm.cpp +++ tools/yaml2obj/yaml2wasm.cpp @@ -311,7 +311,7 @@ break; case wasm::WASM_EXTERNAL_EVENT: writeUint32(OS, Import.EventImport.Attribute); - writeUint32(OS, Import.EventImport.SigIndex); + writeUint32(OS, Import.SigIndex); NumImportedGlobals++; break; case wasm::WASM_EXTERNAL_MEMORY: @@ -395,14 +395,16 @@ WasmYAML::EventSection &Section) { encodeULEB128(Section.Events.size(), OS); uint32_t ExpectedIndex = NumImportedEvents; - for (auto &Event : Section.Events) { + assert(Section.EventTypes.size() == Section.Events.size() && + "EventType size and Events size are different"); + for (unsigned I = 0; I < Section.Events.size(); ++I) { + const auto &Event = Section.Events[I]; if (Event.Index != ExpectedIndex) { errs() << "Unexpected event index: " << Event.Index << "\n"; return 1; } - ++ExpectedIndex; encodeULEB128(Event.Attribute, OS); - encodeULEB128(Event.SigIndex, OS); + encodeULEB128(Section.EventTypes[I], OS); } return 0; }