diff --git a/llvm/include/llvm/ADT/Triple.h b/llvm/include/llvm/ADT/Triple.h index c2dd5f6fb739..74fc8eb8ccbf 100644 --- a/llvm/include/llvm/ADT/Triple.h +++ b/llvm/include/llvm/ADT/Triple.h @@ -661,7 +661,9 @@ public: } /// Tests wether the target supports comdat - bool supportsCOMDAT() const { return !isOSBinFormatMachO(); } + bool supportsCOMDAT() const { + return !isOSBinFormatMachO() && !isOSBinFormatWasm(); + } /// @} /// @name Mutators diff --git a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp index 3e0a39c099b2..0b03d0062d98 100644 --- a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp +++ b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp @@ -1825,10 +1825,10 @@ Expected BitcodeReader::recordValue(SmallVectorImpl &Record, 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; diff --git a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp index e45cdee43680..f23e41b8bb40 100644 --- a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp +++ b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp @@ -1233,21 +1233,21 @@ void TargetLoweringObjectFileCOFF::emitLinkerFlagsForGlobal( // Wasm //===----------------------------------------------------------------------===// -static const Comdat *getWasmComdat(const GlobalValue *GV) { +static void checkWasmComdat(const GlobalValue *GV) { const Comdat *C = GV->getComdat(); if (!C) - return nullptr; + return; - if (C->getSelectionKind() != Comdat::Any) - report_fatal_error("Wasm COMDATs only support SelectionKind::Any, '" + - C->getName() + "' cannot be lowered."); - - 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 @@ static MCSectionWasm *selectWasmSectionForGlobal( 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); diff --git a/llvm/test/CodeGen/WebAssembly/comdat.ll b/llvm/test/CodeGen/WebAssembly/comdat.ll new file mode 100644 index 000000000000..6a83ae87e143 --- /dev/null +++ b/llvm/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.