Index: llvm/trunk/include/llvm/ADT/Triple.h =================================================================== --- llvm/trunk/include/llvm/ADT/Triple.h +++ llvm/trunk/include/llvm/ADT/Triple.h @@ -661,7 +661,9 @@ } /// Tests wether the target supports comdat - bool supportsCOMDAT() const { return !isOSBinFormatMachO(); } + bool supportsCOMDAT() const { + return !isOSBinFormatMachO() && !isOSBinFormatWasm(); + } /// @} /// @name Mutators Index: llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp =================================================================== --- llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp +++ llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp @@ -1825,10 +1825,10 @@ auto *GO = dyn_cast(V); if (GO) { if (GO->getComdat() == reinterpret_cast(1)) { - if (TT.isOSBinFormatMachO()) - GO->setComdat(nullptr); - else + if (TT.supportsCOMDAT()) GO->setComdat(TheModule->getOrInsertComdat(V->getName())); + else + GO->setComdat(nullptr); } } return V; Index: llvm/trunk/lib/CodeGen/TargetLoweringObjectFileImpl.cpp =================================================================== --- llvm/trunk/lib/CodeGen/TargetLoweringObjectFileImpl.cpp +++ llvm/trunk/lib/CodeGen/TargetLoweringObjectFileImpl.cpp @@ -1233,21 +1233,21 @@ // Wasm //===----------------------------------------------------------------------===// -static const Comdat *getWasmComdat(const GlobalValue *GV) { +static void checkWasmComdat(const GlobalValue *GV) { const Comdat *C = GV->getComdat(); if (!C) - return nullptr; - - if (C->getSelectionKind() != Comdat::Any) - report_fatal_error("Wasm COMDATs only support SelectionKind::Any, '" + - C->getName() + "' cannot be lowered."); + return; - return C; + // 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."); } MCSection *TargetLoweringObjectFileWasm::getExplicitSectionGlobal( const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { StringRef Name = GO->getSection(); + checkWasmComdat(GO); return getContext().getWasmSection(Name, SectionKind::getData()); } @@ -1255,8 +1255,7 @@ MCContext &Ctx, const GlobalObject *GO, SectionKind Kind, Mangler &Mang, const TargetMachine &TM, bool EmitUniqueSection, unsigned *NextUniqueID) { StringRef Group = ""; - if (getWasmComdat(GO)) - llvm_unreachable("comdat not yet supported for wasm"); + checkWasmComdat(GO); bool UniqueSectionNames = TM.getUniqueSectionNames(); SmallString<128> Name = getSectionPrefixForGlobal(Kind); Index: llvm/trunk/test/CodeGen/WebAssembly/comdat.ll =================================================================== --- llvm/trunk/test/CodeGen/WebAssembly/comdat.ll +++ llvm/trunk/test/CodeGen/WebAssembly/comdat.ll @@ -0,0 +1,5 @@ +; RUN: not llc -mtriple wasm32-unknown-unknown-wasm %s 2>&1 | FileCheck %s + +$f = comdat any +@f = global i32 0, comdat +; CHECK: LLVM ERROR: WebAssembly doesn't support COMDATs, 'f' cannot be lowered.