Skip to content

Commit

Permalink
[WebAssembly] call_indirect causes indirect function table import
Browse files Browse the repository at this point in the history
For wasm-ld table linking work to proceed, object files should indicate
if they use an indirect function table.  In the future this will be done
by the usual symbols and relocations mechanism, but until that support
lands in the linker, the presence of an `__indirect_function_table` in
the object file's import section shows that the object file needs an
indirect function table.

Prior to https://reviews.llvm.org/D91637, this condition was met by all
object files residualizing an `__indirect_function_table` import.

Since https://reviews.llvm.org/D91637, the intention has been that only
those object files needing an indirect function table would have the
`__indirect_function_table` import.  However, we missed the case of
object files which use the table via `call_indirect` but which
themselves do not declare any indirect functions.

This changeset makes it so that when we lower a call to `call_indirect`,
that we ensure that a `__indirect_function_table` symbol is present and
that it will be propagated to the linker.

A followup patch will revise this mechanism to make an explicit link
between `call_indirect` and its associated indirect function table; see
https://reviews.llvm.org/D90948.

Differential Revision: https://reviews.llvm.org/D92840
  • Loading branch information
wingo committed Jan 5, 2021
1 parent 53a341a commit 9ad83fd
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 0 deletions.
7 changes: 7 additions & 0 deletions llvm/lib/MC/WasmObjectWriter.cpp
Expand Up @@ -405,6 +405,13 @@ void WasmObjectWriter::writeHeader(const MCAssembler &Asm) {

void WasmObjectWriter::executePostLayoutBinding(MCAssembler &Asm,
const MCAsmLayout &Layout) {
// As a stopgap measure until call_indirect instructions start explicitly
// referencing the indirect function table via TABLE_NUMBER relocs, ensure
// that the indirect function table import makes it to the output if anything
// in the compilation unit has caused it to be present.
if (auto *Sym = Asm.getContext().lookupSymbol("__indirect_function_table"))
Asm.registerSymbol(*Sym);

// Build a map of sections to the function that defines them, for use
// in recordRelocation.
for (const MCSymbol &S : Asm.symbols()) {
Expand Down
27 changes: 27 additions & 0 deletions llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmParser.cpp
Expand Up @@ -160,6 +160,24 @@ struct WebAssemblyOperand : public MCParsedAsmOperand {
}
};

static MCSymbolWasm *GetOrCreateFunctionTableSymbol(MCContext &Ctx,
const StringRef &Name) {
// FIXME: Duplicates functionality from
// MC/WasmObjectWriter::recordRelocation, as well as WebAssemblyCodegen's
// WebAssembly:getOrCreateFunctionTableSymbol.
MCSymbolWasm *Sym = cast_or_null<MCSymbolWasm>(Ctx.lookupSymbol(Name));
if (Sym) {
if (!Sym->isFunctionTable())
Ctx.reportError(SMLoc(), "symbol is not a wasm funcref table");
} else {
Sym = cast<MCSymbolWasm>(Ctx.getOrCreateSymbol(Name));
Sym->setFunctionTable();
// The default function table is synthesized by the linker.
Sym->setUndefined();
}
return Sym;
}

class WebAssemblyAsmParser final : public MCTargetAsmParser {
MCAsmParser &Parser;
MCAsmLexer &Lexer;
Expand Down Expand Up @@ -531,6 +549,15 @@ class WebAssemblyAsmParser final : public MCTargetAsmParser {
return true;
} else if (Name == "call_indirect" || Name == "return_call_indirect") {
ExpectFuncType = true;
// Ensure that the object file has a __indirect_function_table import, as
// we call_indirect against it.
auto &Ctx = getStreamer().getContext();
MCSymbolWasm *Sym =
GetOrCreateFunctionTableSymbol(Ctx, "__indirect_function_table");
// Until call_indirect emits TABLE_NUMBER relocs against this symbol, mark
// it as NO_STRIP so as to ensure that the indirect function table makes
// it to linked output.
Sym->setNoStrip();
} else if (Name == "ref.null") {
ExpectHeapType = true;
}
Expand Down
11 changes: 11 additions & 0 deletions llvm/lib/Target/WebAssembly/WebAssemblyFastISel.cpp
Expand Up @@ -20,12 +20,14 @@
#include "WebAssemblyMachineFunctionInfo.h"
#include "WebAssemblySubtarget.h"
#include "WebAssemblyTargetMachine.h"
#include "WebAssemblyUtilities.h"
#include "llvm/Analysis/BranchProbabilityInfo.h"
#include "llvm/CodeGen/FastISel.h"
#include "llvm/CodeGen/FunctionLoweringInfo.h"
#include "llvm/CodeGen/MachineConstantPool.h"
#include "llvm/CodeGen/MachineFrameInfo.h"
#include "llvm/CodeGen/MachineInstrBuilder.h"
#include "llvm/CodeGen/MachineModuleInfo.h"
#include "llvm/CodeGen/MachineRegisterInfo.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/DerivedTypes.h"
Expand Down Expand Up @@ -878,6 +880,15 @@ bool WebAssemblyFastISel::selectCall(const Instruction *I) {
// Add placeholders for the type index and immediate flags
MIB.addImm(0);
MIB.addImm(0);

// Ensure that the object file has a __indirect_function_table import, as we
// call_indirect against it.
MCSymbolWasm *Sym = WebAssembly::getOrCreateFunctionTableSymbol(
MF->getMMI().getContext(), "__indirect_function_table");
// Until call_indirect emits TABLE_NUMBER relocs against this symbol, mark
// it as NO_STRIP so as to ensure that the indirect function table makes it
// to linked output.
Sym->setNoStrip();
}

for (unsigned ArgReg : Args)
Expand Down
10 changes: 10 additions & 0 deletions llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp
Expand Up @@ -16,6 +16,7 @@
#include "WebAssemblyMachineFunctionInfo.h"
#include "WebAssemblySubtarget.h"
#include "WebAssemblyTargetMachine.h"
#include "WebAssemblyUtilities.h"
#include "llvm/CodeGen/Analysis.h"
#include "llvm/CodeGen/CallingConvLower.h"
#include "llvm/CodeGen/MachineInstrBuilder.h"
Expand Down Expand Up @@ -477,6 +478,15 @@ static MachineBasicBlock *LowerCallResults(MachineInstr &CallResults,
if (IsIndirect) {
MIB.addImm(0);
MIB.addImm(0);

// Ensure that the object file has a __indirect_function_table import, as we
// call_indirect against it.
MCSymbolWasm *Sym = WebAssembly::getOrCreateFunctionTableSymbol(
MF.getContext(), "__indirect_function_table");
// Until call_indirect emits TABLE_NUMBER relocs against this symbol, mark
// it as NO_STRIP so as to ensure that the indirect function table makes it
// to linked output.
Sym->setNoStrip();
}

for (auto Use : CallParams.uses())
Expand Down
19 changes: 19 additions & 0 deletions llvm/lib/Target/WebAssembly/WebAssemblyUtilities.cpp
Expand Up @@ -15,6 +15,7 @@
#include "WebAssemblyMachineFunctionInfo.h"
#include "llvm/CodeGen/MachineInstr.h"
#include "llvm/CodeGen/MachineLoopInfo.h"
#include "llvm/MC/MCContext.h"
using namespace llvm;

const char *const WebAssembly::ClangCallTerminateFn = "__clang_call_terminate";
Expand Down Expand Up @@ -96,3 +97,21 @@ const MachineOperand &WebAssembly::getCalleeOp(const MachineInstr &MI) {
llvm_unreachable("Not a call instruction");
}
}

MCSymbolWasm *
WebAssembly::getOrCreateFunctionTableSymbol(MCContext &Ctx,
const StringRef &Name) {
// FIXME: Duplicates functionality from
// MC/WasmObjectWriter::recordRelocation.
MCSymbolWasm *Sym = cast_or_null<MCSymbolWasm>(Ctx.lookupSymbol(Name));
if (Sym) {
if (!Sym->isFunctionTable())
Ctx.reportError(SMLoc(), "symbol is not a wasm funcref table");
} else {
Sym = cast<MCSymbolWasm>(Ctx.getOrCreateSymbol(Name));
Sym->setFunctionTable();
// The default function table is synthesized by the linker.
Sym->setUndefined();
}
return Sym;
}
8 changes: 8 additions & 0 deletions llvm/lib/Target/WebAssembly/WebAssemblyUtilities.h
Expand Up @@ -19,6 +19,9 @@ namespace llvm {

class MachineInstr;
class MachineOperand;
class MCContext;
class MCSymbolWasm;
class StringRef;
class WebAssemblyFunctionInfo;

namespace WebAssembly {
Expand All @@ -37,6 +40,11 @@ extern const char *const PersonalityWrapperFn;
/// instruction.
const MachineOperand &getCalleeOp(const MachineInstr &MI);

/// Returns the operand number of a callee, assuming the argument is a call
/// instruction.
MCSymbolWasm *getOrCreateFunctionTableSymbol(MCContext &Ctx,
const StringRef &Name);

} // end namespace WebAssembly

} // end namespace llvm
Expand Down
28 changes: 28 additions & 0 deletions llvm/test/CodeGen/WebAssembly/call-indirect.ll
@@ -0,0 +1,28 @@
; RUN: llc < %s -asm-verbose=false -O2 | FileCheck --check-prefix=CHECK %s
; RUN: llc < %s -asm-verbose=false -O2 --filetype=obj | obj2yaml | FileCheck --check-prefix=OBJ %s

; Test that compilation units with call_indirect but without any
; function pointer declarations still get a table.

target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128"
target triple = "wasm32-unknown-unknown"

; CHECK-LABEL: call_indirect_void:
; CHECK-NEXT: .functype call_indirect_void (i32) -> ()
; CHECK-NEXT: local.get 0
; CHECK-NEXT: call_indirect () -> ()
; CHECK-NEXT: end_function
define void @call_indirect_void(void ()* %callee) {
call void %callee()
ret void
}

; OBJ: Imports:
; OBJ-NEXT: - Module: env
; OBJ-NEXT: Field: __linear_memory
; OBJ-NEXT: Kind: MEMORY
; OBJ-NEXT: Memory:
; OBJ-NEXT: Initial: 0x0
; OBJ-NEXT: - Module: env
; OBJ-NEXT: Field: __indirect_function_table
; OBJ-NEXT: Kind: TABLE
8 changes: 8 additions & 0 deletions llvm/test/MC/WebAssembly/type-index.s
Expand Up @@ -38,6 +38,14 @@ test0:
# BIN-NEXT: Kind: MEMORY
# BIN-NEXT: Memory:
# BIN-NEXT: Initial: 0x0
# BIN-NEXT: - Module: env
# BIN-NEXT: Field: __indirect_function_table
# BIN-NEXT: Kind: TABLE
# BIN-NEXT: Table:
# BIN-NEXT: Index: 0
# BIN-NEXT: ElemType: FUNCREF
# BIN-NEXT: Limits:
# BIN-NEXT: Initial: 0x0
# BIN-NEXT: - Type: FUNCTION
# BIN-NEXT: FunctionTypes: [ 0 ]
# BIN-NEXT: - Type: CODE
Expand Down

0 comments on commit 9ad83fd

Please sign in to comment.