diff --git a/llvm/lib/MC/MCParser/WasmAsmParser.cpp b/llvm/lib/MC/MCParser/WasmAsmParser.cpp --- a/llvm/lib/MC/MCParser/WasmAsmParser.cpp +++ b/llvm/lib/MC/MCParser/WasmAsmParser.cpp @@ -90,15 +90,40 @@ return false; } - bool parseSectionFlags(StringRef FlagStr, bool &Passive) { - SmallVector Flags; - // If there are no flags, keep Flags empty - FlagStr.split(Flags, ",", -1, false); - for (auto &Flag : Flags) { - if (Flag == "passive") - Passive = true; - else - return error("Expected section flags, instead got: ", Lexer->getTok()); + bool parseSectionFlags(StringRef FlagStr, bool &Passive, bool &Group) { + for (char c : FlagStr) { + switch(c) { + case 'p': + Passive = true; + break; + case 'G': + Group = true; + break; + default: + return Parser->Error(getTok().getLoc(), + StringRef("Unexepcted section flag: ") + FlagStr); + } + } + return false; + } + + bool parseGroup(StringRef &GroupName) { + if (Lexer->isNot(AsmToken::Comma)) + return TokError("expected group name"); + Lex(); + if (Lexer->is(AsmToken::Integer)) { + GroupName = getTok().getString(); + Lex(); + } else if (Parser->parseIdentifier(GroupName)) { + return TokError("invalid group name"); + } + if (Lexer->is(AsmToken::Comma)) { + Lex(); + StringRef Linkage; + if (Parser->parseIdentifier(Linkage)) + return TokError("invalid linkage"); + if (Linkage != "comdat") + return TokError("Linkage must be 'comdat'"); } return false; } @@ -130,27 +155,39 @@ if (!Kind.hasValue()) return Parser->Error(Lexer->getLoc(), "unknown section kind: " + Name); - MCSectionWasm *Section = getContext().getWasmSection(Name, Kind.getValue()); // Update section flags if present in this .section directive bool Passive = false; - if (parseSectionFlags(getTok().getStringContents(), Passive)) + bool Group = false; + if (parseSectionFlags(getTok().getStringContents(), Passive, Group)) return true; - if (Passive) { - if (!Section->isWasmData()) - return Parser->Error(getTok().getLoc(), - "Only data sections can be passive"); - Section->setPassive(); - } + Lex(); - if (expect(AsmToken::Comma, ",") || expect(AsmToken::At, "@") || - expect(AsmToken::EndOfStatement, "eol")) + if (expect(AsmToken::Comma, ",") || expect(AsmToken::At, "@")) return true; - auto WS = getContext().getWasmSection(Name, Kind.getValue()); + StringRef GroupName; + if (Group) { + if(parseGroup(GroupName)) + return true; + } + + if (expect(AsmToken::EndOfStatement, "eol")) + return true; + + // TODO: Parse UniqueID + MCSectionWasm *WS = getContext().getWasmSection(Name, Kind.getValue(), + GroupName, ~0); + if (Passive) { + if (!WS->isWasmData()) + return Parser->Error(getTok().getLoc(), + "Only data sections can be passive"); + WS->setPassive(); + } + getStreamer().SwitchSection(WS); return false; } @@ -189,9 +226,15 @@ Lexer->is(AsmToken::Identifier))) return error("Expected label,@type declaration, got: ", Lexer->getTok()); auto TypeName = Lexer->getTok().getString(); - if (TypeName == "function") + if (TypeName == "function") { WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION); - else if (TypeName == "global") + auto *Current = + cast(getStreamer().getCurrentSection().first); + errs() << "Current " << Current->getName() << " " << Current->getGroup() + << "\n"; + if (Current->getGroup()) + WasmSym->setComdat(true); + } else if (TypeName == "global") WasmSym->setType(wasm::WASM_SYMBOL_TYPE_GLOBAL); else if (TypeName == "object") WasmSym->setType(wasm::WASM_SYMBOL_TYPE_DATA); diff --git a/llvm/lib/MC/MCSectionWasm.cpp b/llvm/lib/MC/MCSectionWasm.cpp --- a/llvm/lib/MC/MCSectionWasm.cpp +++ b/llvm/lib/MC/MCSectionWasm.cpp @@ -64,7 +64,9 @@ OS << ",\""; if (IsPassive) - OS << "passive"; + OS << "p"; + if (Group) + OS << "G"; OS << '"'; @@ -78,6 +80,12 @@ // TODO: Print section type. + if (Group) { + OS << ","; + printName(OS, Group->getName()); + OS << ",comdat"; + } + if (isUnique()) OS << ",unique," << UniqueID; diff --git a/llvm/lib/MC/WasmObjectWriter.cpp b/llvm/lib/MC/WasmObjectWriter.cpp --- a/llvm/lib/MC/WasmObjectWriter.cpp +++ b/llvm/lib/MC/WasmObjectWriter.cpp @@ -1367,6 +1367,8 @@ if (Mode == DwoMode::DwoOnly && !isDwoSection(Sec)) continue; + LLVM_DEBUG(dbgs() << " Section " << SectionName << " group " + << Section.getGroup() << "\n";); // .init_array sections are handled specially elsewhere. if (SectionName.startswith(".init_array")) continue; diff --git a/llvm/test/MC/WebAssembly/comdat-asm.ll b/llvm/test/MC/WebAssembly/comdat-asm.ll new file mode 100644 --- /dev/null +++ b/llvm/test/MC/WebAssembly/comdat-asm.ll @@ -0,0 +1,40 @@ +; RUN: llc -filetype=asm %s -o - | FileCheck %s + +target triple = "wasm32-unknown-unknown" + +; 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: .section .text.basicInlineFn,"G",@,basicInlineFn,comdat +; CHECK-NEXT: .weak basicInlineFn # -- Begin function basicInlineFn +; CHECK-NEXT: .type basicInlineFn,@function +; CHECK-NEXT: basicInlineFn: # @basicInlineFn + +; CHECK: .section .text.sharedFn,"G",@,sharedComdat,comdat +; CHECK-NEXT: .weak sharedFn # -- Begin function sharedFn +; CHECK-NEXT: .type sharedFn,@function +; CHECK-NEXT: sharedFn: # @sharedFn + +; CHECK: .type constantData,@object # @constantData +; CHECK-NEXT: .section .rodata.constantData,"G",@,sharedComdat,comdat +; CHECK-NEXT: .weak constantData +; CHECK-NEXT: constantData: diff --git a/llvm/test/MC/WebAssembly/comdat.ll b/llvm/test/MC/WebAssembly/comdat.ll --- a/llvm/test/MC/WebAssembly/comdat.ll +++ b/llvm/test/MC/WebAssembly/comdat.ll @@ -1,4 +1,8 @@ ; RUN: llc -filetype=obj %s -o - | obj2yaml | FileCheck %s +; RUN: llc -filetype=asm %s -o - | FileCheck --check-prefix=ASM %s +; RUN: llc -filetype=asm %s -o %t.s | llvm-mc -triple=wasm32 -filetype=obj %t.s -o - | obj2yaml | FileCheck %s +; TODO: It doesn't fully work going through .s yet. basicInlineFn isn't showing +; up as comdat yet target triple = "wasm32-unknown-unknown" @@ -116,3 +120,19 @@ ; CHECK-NEXT: - Kind: DATA ; CHECK-NEXT: Index: 0 ; CHECK-NEXT: ... + + +; ASM: .section .text.basicInlineFn,"G",@,basicInlineFn,comdat +; ASM-NEXT: .weak basicInlineFn # -- Begin function basicInlineFn +; ASM-NEXT: .type basicInlineFn,@function +; ASM-NEXT: basicInlineFn: # @basicInlineFn + +; ASM: .section .text.sharedFn,"G",@,sharedComdat,comdat +; ASM-NEXT: .weak sharedFn # -- Begin function sharedFn +; ASM-NEXT: .type sharedFn,@function +; ASM-NEXT: sharedFn: # @sharedFn + +; ASM: .type constantData,@object # @constantData +; ASM-NEXT: .section .rodata.constantData,"G",@,sharedComdat,comdat +; ASM-NEXT: .weak constantData +; ASM-NEXT: constantData: