This is an archive of the discontinued LLVM Phabricator instance.

[WebAssembly] Lower global syms representing tables with .tabletype
ClosedPublic

Authored by pmatos on Dec 10 2021, 5:22 AM.

Details

Summary

This patch implements a fix to recognize global symbols that represent
WebAssembly appropriately and generate the necessary .tabletype
directives.

Diff Detail

Event Timeline

pmatos created this revision.Dec 10 2021, 5:22 AM
pmatos requested review of this revision.Dec 10 2021, 5:22 AM
Herald added a project: Restricted Project. · View Herald TranscriptDec 10 2021, 5:22 AM
pmatos planned changes to this revision.Dec 10 2021, 5:24 AM

@sbc100 @tlively This is still not working properly... but I would like to understand if this is the correct way to go about it.

sbc100 added inline comments.Dec 10 2021, 7:09 AM
llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyMCTargetDesc.h
118 ↗(On Diff #393451)

I'm not sure this enumeration is best place to declare that type of a symbols. These other values all refer to how the symbol is used, but what type the symbol is. For example that distinction between function and data symbols does not appear here.

llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp
1540 ↗(On Diff #393451)

Why MO_TABLE_BASE_REL here?

llvm/lib/Target/WebAssembly/WebAssemblyMCInstLower.cpp
172

If you do want to set the symbol type at this point why do it inside this if (MO.getOffset() != 0) { condition?

However, it seems like for other symbol types the type is already previously set elsewhere so this maybe is not the place to do it?

Looking at all the places where wasm symbols get their types set it looks like a rather mixed bag:

$ git grep 'setType(wasm::'
llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp:        SPSym->setType(wasm::WASM_SYMBOL_TYPE_GLOBAL);
llvm/lib/MC/MCContext.cpp:  cast<MCSymbolWasm>(Begin)->setType(wasm::WASM_SYMBOL_TYPE_SECTION);
llvm/lib/MC/MCParser/WasmAsmParser.cpp:      WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
llvm/lib/MC/MCParser/WasmAsmParser.cpp:      WasmSym->setType(wasm::WASM_SYMBOL_TYPE_GLOBAL);
llvm/lib/MC/MCParser/WasmAsmParser.cpp:      WasmSym->setType(wasm::WASM_SYMBOL_TYPE_DATA);
llvm/lib/MC/MCWasmStreamer.cpp:    Symbol->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmParser.cpp:      WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmParser.cpp:      WasmSym->setType(wasm::WASM_SYMBOL_TYPE_GLOBAL);
llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmParser.cpp:      WasmSym->setType(wasm::WASM_SYMBOL_TYPE_TABLE);
llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmParser.cpp:      WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmParser.cpp:      WasmSym->setType(wasm::WASM_SYMBOL_TYPE_TAG);
llvm/lib/Target/WebAssembly/Disassembler/WebAssemblyDisassembler.cpp:        WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
llvm/lib/Target/WebAssembly/Utils/WebAssemblyUtilities.cpp:    Sym->setType(wasm::WASM_SYMBOL_TYPE_TABLE);
llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp:    Sym->setType(wasm::WASM_SYMBOL_TYPE_GLOBAL);
llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp:    WasmSym->setType(wasm::WASM_SYMBOL_TYPE_GLOBAL);
llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp:    WasmSym->setType(wasm::WASM_SYMBOL_TYPE_DATA);
llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp:    WasmSym->setType(wasm::WASM_SYMBOL_TYPE_TAG);
llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp:    WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp:      Sym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp:  WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
llvm/lib/Target/WebAssembly/WebAssemblyMCInstLower.cpp:      WasmSym->setType(wasm::WASM_SYMBOL_TYPE_GLOBAL);
llvm/lib/Target/WebAssembly/WebAssemblyMCInstLower.cpp:  WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
llvm/lib/Target/WebAssembly/WebAssemblyMCInstLower.cpp:  WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);

Do you have any information to do this t WebAssemblyMCInstLower::GetGlobalAddressSymbol?

pmatos added inline comments.Dec 11 2021, 4:45 AM
llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp
1540 ↗(On Diff #393451)

That's a mistake. It should be MO_SYM_IS_..._TABLE.

llvm/lib/Target/WebAssembly/WebAssemblyMCInstLower.cpp
172

Yeah - I got the same feeling. Rather confusing about the best place to set this information. I will take a look at WebAssemblyMCInstLower::GetGlobalAddressSymbol. Thanks for the suggestion.

pmatos added inline comments.Dec 11 2021, 4:46 AM
llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyMCTargetDesc.h
118 ↗(On Diff #393451)

This is the place to set TargetFlags, and we cannot set the type of the symbol directly here because we don't have access to the MCSymbol, so as far as I can understand we need to set a TargetFlag and later on set the right MCSymbol type based on the address TargetFlags. Do you have a suggestion on where to set this information if not as a TargetFlag?

sbc100 added inline comments.Dec 11 2021, 9:21 AM
llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyMCTargetDesc.h
118 ↗(On Diff #393451)

I'm afraid its been a while since I touched this code.. so I'm not sure I have any useful suggestions other than looking at existing code and practices.

pmatos updated this revision to Diff 393835.Dec 13 2021, 3:30 AM

Fixes the issues found with previous patch.

We needed to ensure that GlobalVariable emissions supports table type
symbols as well as proper lowering.

@sbc100, I didn't ignore your comment regarding MO_SYM_IS_..._TABLE but
at this point I really don't know where to place this information for the
MC layer to act on it.

pmatos added inline comments.Dec 13 2021, 3:32 AM
llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyMCTargetDesc.h
118 ↗(On Diff #393451)

Thanks, I have been looking and didn't find a better place to tag a symbol with a property so that the MC layer can 'fix' the symbol type. I will keep looking, if you think this is an improper place for the symbol type tag.

pmatos added inline comments.Dec 13 2021, 4:18 AM
llvm/lib/Target/WebAssembly/WebAssemblyMCInstLower.cpp
172

Me: Who was the fool who set the type of a table GetGlobalAddressSymbol to WASM_SYMBOL_TYPE_GLOBAL?

git blame: That was you...

Me: Doh!

pmatos updated this revision to Diff 393870.Dec 13 2021, 6:29 AM

Patch simplification.

@sbc100 Thanks for the pointer to GetGlobalAddressSymbol (even though I should have
recalled my own change). I hope this patch is easier to accept.

pmatos updated this revision to Diff 393871.Dec 13 2021, 6:30 AM

Making the linter happy

sbc100 accepted this revision.Dec 13 2021, 8:15 AM

Oh nice! This looks way better now. Glad you figured it out, I'm not sure if I would have been much help!

This revision is now accepted and ready to land.Dec 13 2021, 8:15 AM

Oh nice! This looks way better now. Glad you figured it out, I'm not sure if I would have been much help!

Thanks for the review - happy that we manage to find a minimal way to fix this.

This revision was landed with ongoing or failed builds.Dec 13 2021, 9:18 AM
This revision was automatically updated to reflect the committed changes.