From c0039de2953d15815448b4b3c3bafb45607781e0 Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Mon, 4 Oct 2021 16:47:59 -0700 Subject: [PATCH] [Object][WebAssemlby] Report function types (signatures). NFC This simplifies the code in a number of ways and avoids having to track functions and their types separately. Differential Revision: https://reviews.llvm.org/D111104 --- lld/wasm/InputFiles.cpp | 7 +++---- lld/wasm/InputFiles.h | 2 +- llvm/include/llvm/BinaryFormat/Wasm.h | 1 + llvm/include/llvm/Object/Wasm.h | 1 - llvm/lib/Object/WasmObjectFile.cpp | 17 +++++++++-------- llvm/tools/obj2yaml/wasm2yaml.cpp | 4 ++-- 6 files changed, 16 insertions(+), 16 deletions(-) diff --git a/lld/wasm/InputFiles.cpp b/lld/wasm/InputFiles.cpp index 664e657..5775258 100644 --- a/lld/wasm/InputFiles.cpp +++ b/lld/wasm/InputFiles.cpp @@ -496,12 +496,11 @@ void ObjFile::parse(bool ignoreComdats) { // Populate `Functions`. ArrayRef funcs = wasmObj->functions(); - ArrayRef funcTypes = wasmObj->functionTypes(); ArrayRef types = wasmObj->types(); functions.reserve(funcs.size()); - for (size_t i = 0, e = funcs.size(); i != e; ++i) { - auto* func = make(types[funcTypes[i]], &funcs[i], this); + for (auto &f : funcs) { + auto *func = make(types[f.SigIndex], &f, this); func->discarded = isExcludedByComdat(func); functions.emplace_back(func); } @@ -541,7 +540,7 @@ void ObjFile::parse(bool ignoreComdats) { addLegacyIndirectFunctionTableIfNeeded(tableSymbolCount); } -bool ObjFile::isExcludedByComdat(InputChunk *chunk) const { +bool ObjFile::isExcludedByComdat(const InputChunk *chunk) const { uint32_t c = chunk->getComdat(); if (c == UINT32_MAX) return false; diff --git a/lld/wasm/InputFiles.h b/lld/wasm/InputFiles.h index f67b7ce..7c5305c 100644 --- a/lld/wasm/InputFiles.h +++ b/lld/wasm/InputFiles.h @@ -154,7 +154,7 @@ private: Symbol *createDefined(const WasmSymbol &sym); Symbol *createUndefined(const WasmSymbol &sym, bool isCalledDirectly); - bool isExcludedByComdat(InputChunk *chunk) const; + bool isExcludedByComdat(const InputChunk *chunk) const; void addLegacyIndirectFunctionTableIfNeeded(uint32_t tableSymbolCount); std::unique_ptr wasmObj; diff --git a/llvm/include/llvm/BinaryFormat/Wasm.h b/llvm/include/llvm/BinaryFormat/Wasm.h index 736280c..1fd3f9a 100644 --- a/llvm/include/llvm/BinaryFormat/Wasm.h +++ b/llvm/include/llvm/BinaryFormat/Wasm.h @@ -139,6 +139,7 @@ struct WasmLocalDecl { struct WasmFunction { uint32_t Index; + uint32_t SigIndex; std::vector Locals; ArrayRef Body; uint32_t CodeSectionOffset; diff --git a/llvm/include/llvm/Object/Wasm.h b/llvm/include/llvm/Object/Wasm.h index d15722d..7b26c42 100644 --- a/llvm/include/llvm/Object/Wasm.h +++ b/llvm/include/llvm/Object/Wasm.h @@ -138,7 +138,6 @@ public: return TargetFeatures; } ArrayRef types() const { return Signatures; } - ArrayRef functionTypes() const { return FunctionTypes; } ArrayRef imports() const { return Imports; } ArrayRef tables() const { return Tables; } ArrayRef memories() const { return Memories; } diff --git a/llvm/lib/Object/WasmObjectFile.cpp b/llvm/lib/Object/WasmObjectFile.cpp index 524a691..8dcd9af 100644 --- a/llvm/lib/Object/WasmObjectFile.cpp +++ b/llvm/lib/Object/WasmObjectFile.cpp @@ -412,7 +412,7 @@ Error WasmObjectFile::parseNameSection(ReadContext &Ctx) { llvm::DenseSet SeenFunctions; llvm::DenseSet SeenGlobals; llvm::DenseSet SeenSegments; - if (FunctionTypes.size() && !SeenCodeSection) { + if (Functions.size() && !SeenCodeSection) { return make_error("names must come after code section", object_error::parse_failed); } @@ -480,7 +480,7 @@ Error WasmObjectFile::parseNameSection(ReadContext &Ctx) { Error WasmObjectFile::parseLinkingSection(ReadContext &Ctx) { HasLinkingSection = true; - if (FunctionTypes.size() && !SeenCodeSection) { + if (Functions.size() && !SeenCodeSection) { return make_error( "linking data must come after code section", object_error::parse_failed); @@ -598,8 +598,8 @@ Error WasmObjectFile::parseLinkingSectionSymtab(ReadContext &Ctx) { if (IsDefined) { Info.Name = readString(Ctx); unsigned FuncIndex = Info.ElementIndex - NumImportedFunctions; - Signature = &Signatures[FunctionTypes[FuncIndex]]; wasm::WasmFunction &Function = Functions[FuncIndex]; + Signature = &Signatures[Function.SigIndex]; if (Function.SymbolName.empty()) Function.SymbolName = Info.Name; } else { @@ -1140,15 +1140,16 @@ Error WasmObjectFile::parseImportSection(ReadContext &Ctx) { Error WasmObjectFile::parseFunctionSection(ReadContext &Ctx) { uint32_t Count = readVaruint32(Ctx); - FunctionTypes.reserve(Count); - Functions.resize(Count); + Functions.reserve(Count); uint32_t NumTypes = Signatures.size(); while (Count--) { uint32_t Type = readVaruint32(Ctx); if (Type >= NumTypes) return make_error("invalid function type", object_error::parse_failed); - FunctionTypes.push_back(Type); + wasm::WasmFunction F; + F.SigIndex = Type; + Functions.push_back(F); } if (Ctx.Ptr != Ctx.End) return make_error("function section ended prematurely", @@ -1272,7 +1273,7 @@ Error WasmObjectFile::parseExportSection(ReadContext &Ctx) { } bool WasmObjectFile::isValidFunctionIndex(uint32_t Index) const { - return Index < NumImportedFunctions + FunctionTypes.size(); + return Index < NumImportedFunctions + Functions.size(); } bool WasmObjectFile::isDefinedFunctionIndex(uint32_t Index) const { @@ -1360,7 +1361,7 @@ Error WasmObjectFile::parseCodeSection(ReadContext &Ctx) { SeenCodeSection = true; CodeSection = Sections.size(); uint32_t FunctionCount = readVaruint32(Ctx); - if (FunctionCount != FunctionTypes.size()) { + if (FunctionCount != Functions.size()) { return make_error("invalid function count", object_error::parse_failed); } diff --git a/llvm/tools/obj2yaml/wasm2yaml.cpp b/llvm/tools/obj2yaml/wasm2yaml.cpp index 0cbf021..61a48c5 100644 --- a/llvm/tools/obj2yaml/wasm2yaml.cpp +++ b/llvm/tools/obj2yaml/wasm2yaml.cpp @@ -260,8 +260,8 @@ ErrorOr WasmDumper::dump() { } case wasm::WASM_SEC_FUNCTION: { auto FuncSec = std::make_unique(); - for (const auto &Func : Obj.functionTypes()) { - FuncSec->FunctionTypes.push_back(Func); + for (const auto &Func : Obj.functions()) { + FuncSec->FunctionTypes.push_back(Func.SigIndex); } S = std::move(FuncSec); break; -- 2.7.4