Index: docs/LangRef.rst =================================================================== --- docs/LangRef.rst +++ docs/LangRef.rst @@ -883,8 +883,8 @@ The linker may choose any COMDAT key but the sections must contain the same amount of data. -Note that the Mach-O platform doesn't support COMDATs and ELF only supports -``any`` as a selection kind. +Note that the Mach-O platform doesn't support COMDATs, and ELF and WebAssembly +only support ``any`` as a selection kind. Here is an example of a COMDAT group where a function will only be selected if the COMDAT key's section is the largest: Index: include/llvm/ADT/Triple.h =================================================================== --- include/llvm/ADT/Triple.h +++ include/llvm/ADT/Triple.h @@ -660,9 +660,9 @@ return getArch() == Triple::aarch64 || getArch() == Triple::aarch64_be; } - /// Tests wether the target supports comdat + /// Tests whether the target supports comdat bool supportsCOMDAT() const { - return !isOSBinFormatMachO() && !isOSBinFormatWasm(); + return !isOSBinFormatMachO(); } /// @} Index: include/llvm/BinaryFormat/Wasm.h =================================================================== --- include/llvm/BinaryFormat/Wasm.h +++ include/llvm/BinaryFormat/Wasm.h @@ -93,6 +93,7 @@ ArrayRef Body; uint32_t CodeSectionOffset; uint32_t Size; + StringRef Comdat; }; struct WasmDataSegment { @@ -102,6 +103,7 @@ StringRef Name; uint32_t Alignment; uint32_t Flags; + StringRef Comdat; }; struct WasmElemSegment { @@ -171,11 +173,6 @@ WASM_OPCODE_F64_CONST = 0x44, }; -enum : unsigned { - WASM_NAMES_FUNCTION = 0x1, - WASM_NAMES_LOCAL = 0x2, -}; - enum : unsigned { WASM_LIMITS_FLAG_HAS_MAX = 0x1, }; @@ -188,12 +185,25 @@ F64 = WASM_TYPE_F64, }; -// Linking metadata kinds. +// Kind codes used in the custom "name" section +enum : unsigned { + WASM_NAMES_FUNCTION = 0x1, + WASM_NAMES_LOCAL = 0x2, +}; + +// Kind codes used in the custom "linking" section enum : unsigned { WASM_SYMBOL_INFO = 0x2, WASM_DATA_SIZE = 0x3, WASM_SEGMENT_INFO = 0x5, WASM_INIT_FUNCS = 0x6, + WASM_COMDAT_INFO = 0x7, +}; + +// Kind codes used in the custom "linking" section in the WASM_COMDAT_INFO +enum : unsigned { + WASM_COMDAT_DATA = 0x0, + WASM_COMDAT_FUNCTION = 0x1, }; const unsigned WASM_SYMBOL_BINDING_MASK = 0x3; Index: include/llvm/Object/Wasm.h =================================================================== --- include/llvm/Object/Wasm.h +++ include/llvm/Object/Wasm.h @@ -140,6 +140,7 @@ ArrayRef elements() const { return ElemSegments; } ArrayRef dataSegments() const { return DataSegments; } ArrayRef functions() const { return Functions; } + ArrayRef comdats() const { return Comdats; } uint32_t startFunction() const { return StartFunction; } void moveSymbolNext(DataRefImpl &Symb) const override; @@ -223,6 +224,7 @@ // Custom section types Error parseNameSection(const uint8_t *Ptr, const uint8_t *End); Error parseLinkingSection(const uint8_t *Ptr, const uint8_t *End); + Error parseLinkingSectionComdat(const uint8_t *&Ptr, const uint8_t *End); Error parseRelocSection(StringRef Name, const uint8_t *Ptr, const uint8_t *End); @@ -241,6 +243,7 @@ std::vector DataSegments; std::vector Functions; std::vector Symbols; + std::vector Comdats; uint32_t StartFunction = -1; bool HasLinkingSection = false; wasm::WasmLinkingData LinkingData; Index: include/llvm/ObjectYAML/WasmYAML.h =================================================================== --- include/llvm/ObjectYAML/WasmYAML.h +++ include/llvm/ObjectYAML/WasmYAML.h @@ -37,6 +37,7 @@ LLVM_YAML_STRONG_TYPEDEF(uint32_t, SymbolFlags) LLVM_YAML_STRONG_TYPEDEF(uint32_t, SegmentFlags) LLVM_YAML_STRONG_TYPEDEF(uint32_t, LimitFlags) +LLVM_YAML_STRONG_TYPEDEF(uint32_t, ComdatKind) struct FileHeader { yaml::Hex32 Version; @@ -136,6 +137,16 @@ uint32_t FunctionIndex; }; +struct ComdatEntry { + ComdatKind Kind; + uint32_t Index; +}; + +struct Comdat { + StringRef Name; + std::vector Entries; +}; + struct Section { explicit Section(SectionType SecType) : Type(SecType) {} virtual ~Section(); @@ -179,6 +190,7 @@ std::vector SymbolInfos; std::vector SegmentInfos; std::vector InitFunctions; + std::vector Comdats; }; struct TypeSection : Section { @@ -316,6 +328,8 @@ LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::SegmentInfo) LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::SymbolInfo) LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::InitFunction) +LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::ComdatEntry) +LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Comdat) namespace llvm { namespace yaml { @@ -412,6 +426,18 @@ static void mapping(IO &IO, WasmYAML::InitFunction &Init); }; +template <> struct ScalarEnumerationTraits { + static void enumeration(IO &IO, WasmYAML::ComdatKind &Kind); +}; + +template <> struct MappingTraits { + static void mapping(IO &IO, WasmYAML::ComdatEntry &ComdatEntry); +}; + +template <> struct MappingTraits { + static void mapping(IO &IO, WasmYAML::Comdat &Comdat); +}; + template <> struct ScalarEnumerationTraits { static void enumeration(IO &IO, WasmYAML::ValueType &Type); }; Index: lib/CodeGen/TargetLoweringObjectFileImpl.cpp =================================================================== --- lib/CodeGen/TargetLoweringObjectFileImpl.cpp +++ lib/CodeGen/TargetLoweringObjectFileImpl.cpp @@ -1254,15 +1254,17 @@ // Wasm //===----------------------------------------------------------------------===// -static void checkWasmComdat(const GlobalValue *GV) { +static const Comdat *getWasmComdat(const GlobalValue *GV) { const Comdat *C = GV->getComdat(); if (!C) - return; + return nullptr; - // TODO(sbc): At some point we may need COMDAT support but currently - // they are not supported. - report_fatal_error("WebAssembly doesn't support COMDATs, '" + C->getName() + - "' cannot be lowered."); + if (C->getSelectionKind() != Comdat::Any) + report_fatal_error("WebAssembly COMDATs only support " + "SelectionKind::Any, '" + C->getName() + "' cannot be " + "lowered."); + + return C; } static SectionKind getWasmKindForNamedSection(StringRef Name, SectionKind K) { @@ -1278,16 +1280,25 @@ MCSection *TargetLoweringObjectFileWasm::getExplicitSectionGlobal( const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { StringRef Name = GO->getSection(); - checkWasmComdat(GO); + Kind = getWasmKindForNamedSection(Name, Kind); - return getContext().getWasmSection(Name, Kind); + + StringRef Group = ""; + if (const Comdat *C = getWasmComdat(GO)) { + Group = C->getName(); + } + + return getContext().getWasmSection(Name, Kind, Group, + MCContext::GenericSectionID); } static MCSectionWasm *selectWasmSectionForGlobal( MCContext &Ctx, const GlobalObject *GO, SectionKind Kind, Mangler &Mang, const TargetMachine &TM, bool EmitUniqueSection, unsigned *NextUniqueID) { StringRef Group = ""; - checkWasmComdat(GO); + if (const Comdat *C = getWasmComdat(GO)) { + Group = C->getName(); + } bool UniqueSectionNames = TM.getUniqueSectionNames(); SmallString<128> Name = getSectionPrefixForGlobal(Kind); Index: lib/MC/WasmObjectWriter.cpp =================================================================== --- lib/MC/WasmObjectWriter.cpp +++ lib/MC/WasmObjectWriter.cpp @@ -138,6 +138,14 @@ uint32_t ImportIndex; }; +// 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. +struct WasmComdatEntry { + unsigned Kind; + uint32_t Index; +}; + // Information about a single relocation. struct WasmRelocationEntry { uint64_t Offset; // Where is the relocation. @@ -284,8 +292,9 @@ void writeDataRelocSection(); void writeLinkingMetaDataSection( ArrayRef Segments, uint32_t DataSize, - const SmallVector, 4> &SymbolFlags, - const SmallVector, 2> &InitFuncs); + ArrayRef> SymbolFlags, + ArrayRef> InitFuncs, + const std::map>& Comdats); uint32_t getProvisionalValue(const WasmRelocationEntry &RelEntry); void applyRelocations(ArrayRef Relocations, @@ -910,8 +919,9 @@ void WasmObjectWriter::writeLinkingMetaDataSection( ArrayRef Segments, uint32_t DataSize, - const SmallVector, 4> &SymbolFlags, - const SmallVector, 2> &InitFuncs) { + ArrayRef> SymbolFlags, + ArrayRef> InitFuncs, + const std::map>& Comdats) { SectionBookkeeping Section; startSection(Section, wasm::WASM_SEC_CUSTOM, "linking"); SectionBookkeeping SubSection; @@ -953,6 +963,21 @@ endSection(SubSection); } + if (Comdats.size()) { + startSection(SubSection, wasm::WASM_COMDAT_INFO); + encodeULEB128(Comdats.size(), getStream()); + for (const auto &C : Comdats) { + writeString(C.first); + encodeULEB128(0, getStream()); // flags for future use + encodeULEB128(C.second.size(), getStream()); + for (const WasmComdatEntry &Entry : C.second) { + encodeULEB128(Entry.Kind, getStream()); + encodeULEB128(Entry.Index, getStream()); + } + } + endSection(SubSection); + } + endSection(Section); } @@ -994,6 +1019,7 @@ SmallVector Exports; SmallVector, 4> SymbolFlags; SmallVector, 2> InitFuncs; + std::map> Comdats; SmallPtrSet IsAddressTaken; unsigned NumFuncImports = 0; SmallVector DataSegments; @@ -1164,6 +1190,12 @@ Segment.Flags = 0; DataSize += Segment.Data.size(); Section.setMemoryOffset(Segment.Offset); + + if (const MCSymbolWasm *C = Section.getGroup()) { + Comdats[C->getName()].emplace_back( + WasmComdatEntry{wasm::WASM_COMDAT_DATA, + static_cast(DataSegments.size()) - 1}); + } } // Handle regular defined and undefined symbols. @@ -1244,6 +1276,7 @@ // address. For externals these will also be named exports. Index = NumGlobalImports + Globals.size(); auto &DataSection = static_cast(WS.getSection()); + assert(DataSection.isWasmData()); WasmGlobal Global; Global.Type = PtrType; @@ -1267,8 +1300,16 @@ Export.Kind = wasm::WASM_EXTERNAL_GLOBAL; DEBUG(dbgs() << " -> export " << Exports.size() << "\n"); Exports.push_back(Export); + if (!WS.isExternal()) SymbolFlags.emplace_back(WS.getName(), wasm::WASM_SYMBOL_BINDING_LOCAL); + + if (WS.isFunction()) { + auto &Section = static_cast(WS.getSection(false)); + if (const MCSymbolWasm *C = Section.getGroup()) + Comdats[C->getName()].emplace_back( + WasmComdatEntry{wasm::WASM_COMDAT_FUNCTION, Index}); + } } } @@ -1379,7 +1420,7 @@ writeCodeRelocSection(); writeDataRelocSection(); writeLinkingMetaDataSection(DataSegments, DataSize, SymbolFlags, - InitFuncs); + InitFuncs, Comdats); // TODO: Translate the .comment section to the output. // TODO: Translate debug sections to the output. Index: lib/Object/WasmObjectFile.cpp =================================================================== --- lib/Object/WasmObjectFile.cpp +++ lib/Object/WasmObjectFile.cpp @@ -10,6 +10,7 @@ #include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/STLExtras.h" #include "llvm/ADT/StringRef.h" +#include "llvm/ADT/StringSet.h" #include "llvm/ADT/Triple.h" #include "llvm/BinaryFormat/Wasm.h" #include "llvm/MC/SubtargetFeature.h" @@ -413,6 +414,10 @@ } break; } + case wasm::WASM_COMDAT_INFO: + if (Error Err = parseLinkingSectionComdat(Ptr, SubSectionEnd)) + return Err; + break; default: Ptr += Size; break; @@ -427,6 +432,55 @@ return Error::success(); } +Error WasmObjectFile::parseLinkingSectionComdat(const uint8_t *&Ptr, + const uint8_t *End) +{ + uint32_t ComdatCount = readVaruint32(Ptr); + StringSet<> ComdatSet; + while (ComdatCount--) { + StringRef Name = readString(Ptr); + if (Name.empty() || !ComdatSet.insert(Name).second) + return make_error("Bad/duplicate COMDAT name " + Twine(Name), + object_error::parse_failed); + Comdats.emplace_back(Name); + uint32_t Flags = readVaruint32(Ptr); + if (Flags != 0) + return make_error("Unsupported COMDAT flags", + object_error::parse_failed); + + uint32_t EntryCount = readVaruint32(Ptr); + while (EntryCount--) { + unsigned Kind = readVaruint32(Ptr); + unsigned Index = readVaruint32(Ptr); + switch (Kind) { + default: + return make_error("Invalid COMDAT entry type", + object_error::parse_failed); + case wasm::WASM_COMDAT_DATA: + if (Index >= DataSegments.size()) + return make_error("COMDAT data index out of range", + object_error::parse_failed); + if (!DataSegments[Index].Data.Comdat.empty()) + return make_error("Data segment in two COMDATs", + object_error::parse_failed); + DataSegments[Index].Data.Comdat = Name; + break; + case wasm::WASM_COMDAT_FUNCTION: + if (Index < NumImportedFunctions || !isValidFunctionIndex(Index)) + return make_error("COMDAT function index out of range", + object_error::parse_failed); + Index -= NumImportedFunctions; + if (!Functions[Index].Comdat.empty()) + return make_error("Function in two COMDATs", + object_error::parse_failed); + Functions[Index].Comdat = Name; + break; + } + } + } + return Error::success(); +} + WasmSection* WasmObjectFile::findCustomSectionByName(StringRef Name) { for (WasmSection& Section : Sections) { if (Section.Type == wasm::WASM_SEC_CUSTOM && Section.Name == Name) Index: lib/ObjectYAML/WasmYAML.cpp =================================================================== --- lib/ObjectYAML/WasmYAML.cpp +++ lib/ObjectYAML/WasmYAML.cpp @@ -61,6 +61,7 @@ IO.mapOptional("SymbolInfo", Section.SymbolInfos); IO.mapOptional("SegmentInfo", Section.SegmentInfos); IO.mapOptional("InitFunctions", Section.InitFunctions); + IO.mapOptional("Comdats", Section.Comdats); } static void sectionMapping(IO &IO, WasmYAML::CustomSection &Section) { @@ -366,6 +367,26 @@ IO.mapRequired("FunctionIndex", Init.FunctionIndex); } +void ScalarEnumerationTraits::enumeration( + IO &IO, WasmYAML::ComdatKind &Kind) { +#define ECase(X) IO.enumCase(Kind, #X, wasm::WASM_COMDAT_##X); + ECase(FUNCTION); + ECase(DATA); +#undef ECase +} + +void MappingTraits::mapping( + IO &IO, WasmYAML::ComdatEntry &ComdatEntry) { + IO.mapRequired("Kind", ComdatEntry.Kind); + IO.mapRequired("Index", ComdatEntry.Index); +} + +void MappingTraits::mapping( + IO &IO, WasmYAML::Comdat &Comdat) { + IO.mapRequired("Name", Comdat.Name); + IO.mapRequired("Entries", Comdat.Entries); +} + void MappingTraits::mapping(IO &IO, WasmYAML::SymbolInfo &Info) { IO.mapRequired("Name", Info.Name); Index: test/MC/WebAssembly/comdat.ll =================================================================== --- /dev/null +++ test/MC/WebAssembly/comdat.ll @@ -0,0 +1,96 @@ +; RUN: llc -mtriple wasm32-unknown-unknown-wasm -filetype=obj %s -o - | obj2yaml | FileCheck %s + +; Import a function just so we can check the index arithmetic for +; WASM_COMDAT_FUNCTION entries is performed correctly +declare i32 @funcImport() +define i32 @callImport() { +entry: + %call = call i32 @funcImport() + ret i32 %call +} + +; Function in its own COMDAT +$basicInlineFn = comdat any +define linkonce_odr i32 @basicInlineFn() #1 comdat { + ret i32 0 +} + +; Global, data, and function in same COMDAT +$sharedComdat = comdat any +@constantData = weak_odr constant [3 x i8] c"abc", comdat($sharedComdat) +define linkonce_odr i32 @sharedFn() #1 comdat($sharedComdat) { + ret i32 0 +} + +; CHECK: - Type: EXPORT +; CHECK-NEXT: Exports: +; CHECK-NEXT: - Name: callImport +; CHECK-NEXT: Kind: FUNCTION +; CHECK-NEXT: Index: 1 +; CHECK-NEXT: - Name: basicInlineFn +; CHECK-NEXT: Kind: FUNCTION +; CHECK-NEXT: Index: 2 +; CHECK-NEXT: - Name: sharedFn +; CHECK-NEXT: Kind: FUNCTION +; CHECK-NEXT: Index: 3 +; CHECK-NEXT: - Name: constantData +; CHECK-NEXT: Kind: GLOBAL +; CHECK-NEXT: Index: 1 +; CHECK-NEXT: - Type: CODE +; CHECK-NEXT: Relocations: +; CHECK-NEXT: - Type: R_WEBASSEMBLY_FUNCTION_INDEX_LEB +; CHECK-NEXT: Index: 0 +; CHECK-NEXT: Offset: 0x00000004 +; CHECK-NEXT: Functions: +; CHECK-NEXT: - Locals: +; CHECK-NEXT: Body: 1080808080000B +; CHECK-NEXT: - Locals: +; CHECK-NEXT: Body: 41000B +; CHECK-NEXT: - Locals: +; CHECK-NEXT: Body: 41000B +; CHECK-NEXT: - Type: DATA +; CHECK-NEXT: Segments: +; CHECK-NEXT: - SectionOffset: 6 +; CHECK-NEXT: MemoryIndex: 0 +; CHECK-NEXT: Offset: +; CHECK-NEXT: Opcode: I32_CONST +; CHECK-NEXT: Value: 0 +; CHECK-NEXT: Content: '616263' +; CHECK-NEXT: - Type: CUSTOM +; CHECK-NEXT: Name: name +; CHECK-NEXT: FunctionNames: +; CHECK-NEXT: - Index: 0 +; CHECK-NEXT: Name: funcImport +; CHECK-NEXT: - Index: 1 +; CHECK-NEXT: Name: callImport +; CHECK-NEXT: - Index: 2 +; CHECK-NEXT: Name: basicInlineFn +; CHECK-NEXT: - Index: 3 +; CHECK-NEXT: Name: sharedFn +; CHECK-NEXT: - Type: CUSTOM +; CHECK-NEXT: Name: linking +; CHECK-NEXT: DataSize: 3 +; CHECK-NEXT: SymbolInfo: +; CHECK-NEXT: - Name: basicInlineFn +; CHECK-NEXT: Flags: [ BINDING_WEAK ] +; CHECK-NEXT: - Name: sharedFn +; CHECK-NEXT: Flags: [ BINDING_WEAK ] +; CHECK-NEXT: - Name: constantData +; CHECK-NEXT: Flags: [ BINDING_WEAK ] +; CHECK-NEXT: SegmentInfo: +; CHECK-NEXT: - Index: 0 +; CHECK-NEXT: Name: .rodata.constantData +; CHECK-NEXT: Alignment: 1 +; CHECK-NEXT: Flags: [ ] +; CHECK-NEXT: Comdats: +; CHECK-NEXT: - Name: basicInlineFn +; CHECK-NEXT: Entries: +; CHECK-NEXT: - Kind: FUNCTION +; CHECK-NEXT: Index: 2 +; CHECK-NEXT: - Name: sharedComdat +; CHECK-NEXT: Entries: +; CHECK-NEXT: - Kind: FUNCTION +; CHECK-NEXT: Index: 3 +; CHECK-NEXT: - Kind: DATA +; CHECK-NEXT: Index: 0 +; CHECK-NEXT: ... Index: tools/obj2yaml/wasm2yaml.cpp =================================================================== --- tools/obj2yaml/wasm2yaml.cpp +++ tools/obj2yaml/wasm2yaml.cpp @@ -49,6 +49,14 @@ return L; } +static uint32_t getNumFunctionImports(ArrayRef Imports) { + uint32_t NumFunctionImports = 0; + for (const wasm::WasmImport &I : Imports) + if (I.Kind == wasm::WASM_EXTERNAL_FUNCTION) + ++NumFunctionImports; + return NumFunctionImports; +} + std::unique_ptr WasmDumper::dumpCustomSection(const WasmSection &WasmSec) { std::unique_ptr CustomSec; if (WasmSec.Name == "name") { @@ -65,7 +73,21 @@ CustomSec = std::move(NameSec); } else if (WasmSec.Name == "linking") { std::unique_ptr LinkingSec = make_unique(); - size_t Index = 0; + std::map ComdatIndexes; + for (StringRef ComdatName : Obj.comdats()) { + ComdatIndexes[ComdatName] = LinkingSec->Comdats.size(); + LinkingSec->Comdats.emplace_back(WasmYAML::Comdat{ComdatName, {}}); + } + uint32_t Index = getNumFunctionImports(Obj.imports()); + for (auto &Func : Obj.functions()) { + if (!Func.Comdat.empty()) { + auto &Comdat = LinkingSec->Comdats[ComdatIndexes[Func.Comdat]]; + Comdat.Entries.emplace_back( + WasmYAML::ComdatEntry{wasm::WASM_COMDAT_FUNCTION, Index}); + } + Index++; + } + Index = 0; for (const object::WasmSegment &Segment : Obj.dataSegments()) { if (!Segment.Data.Name.empty()) { WasmYAML::SegmentInfo SegmentInfo; @@ -75,6 +97,11 @@ SegmentInfo.Flags = Segment.Data.Flags; LinkingSec->SegmentInfos.push_back(SegmentInfo); } + if (!Segment.Data.Comdat.empty()) { + auto &Comdat = LinkingSec->Comdats[ComdatIndexes[Segment.Data.Comdat]]; + Comdat.Entries.emplace_back( + WasmYAML::ComdatEntry{wasm::WASM_COMDAT_DATA, Index}); + } Index++; } for (const object::SymbolRef& Sym: Obj.symbols()) { Index: tools/yaml2obj/yaml2wasm.cpp =================================================================== --- tools/yaml2obj/yaml2wasm.cpp +++ tools/yaml2obj/yaml2wasm.cpp @@ -173,6 +173,23 @@ } SubSection.Done(); } + + // COMDAT_INFO subsection + if (Section.Comdats.size()) { + encodeULEB128(wasm::WASM_COMDAT_INFO, OS); + encodeULEB128(Section.Comdats.size(), SubSection.GetStream()); + for (const auto &C : Section.Comdats) { + writeStringRef(C.Name, SubSection.GetStream()); + encodeULEB128(0, SubSection.GetStream()); // flags for future use + encodeULEB128(C.Entries.size(), SubSection.GetStream()); + for (const WasmYAML::ComdatEntry &Entry : C.Entries) { + encodeULEB128(Entry.Kind, SubSection.GetStream()); + encodeULEB128(Entry.Index, SubSection.GetStream()); + } + } + SubSection.Done(); + } + return 0; }