Commit e25c26d2 authored by Ryan Hunt's avatar Ryan Hunt
Browse files

Bug 1790626 - wasm: Move 'immediate' type representation to FuncType and only...

Bug 1790626 - wasm: Move 'immediate' type representation to FuncType and only use with call_indirect. r=yury

TypeIdDesc describes how to load the 'type id' for any type. Right now the
'type id' is an immediate integer for small function types, a *FuncType for
larger function types, and *RttValue for everything else.

The immediate integer case is an optimization for signature checks in
call_indirect. We can simplify our code by adding an 'immediateType_'
field to `FuncType` which stores an alternate representation of the
function type (if any) that can be used with call_indirect. Then
`TypeIdDesc`/`TypeDefWithId` are not needed during instantiation
anymore and we can just pass `TypeDefVector` from compilation to
runtime.

As a drive-by fix, some code for setting up the global data are of
Instance was simplified to allocate whole contiguous areas, instead
of looping, in some cases.

Some uses of TypeIdDesc still remain after this commit, but they are
only in codegen for figuring out how to emit signature checks. The
class is renamed and simplified to CallIndirectId to represent this.

Differential Revision: https://phabricator.services.mozilla.com/D157385
parent 59151266
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
@@ -12814,7 +12814,7 @@ static bool CreateStackMapFromLSafepoint(LSafepoint& safepoint,
}
bool CodeGenerator::generateWasm(
    wasm::TypeIdDesc funcTypeId, wasm::BytecodeOffset trapOffset,
    wasm::CallIndirectId callIndirectId, wasm::BytecodeOffset trapOffset,
    const wasm::ArgTypeVector& argTypes, const RegisterOffsets& trapExitLayout,
    size_t trapExitLayoutNumWords, wasm::FuncOffsets* offsets,
    wasm::StackMaps* stackMaps, wasm::Decoder* decoder) {
@@ -12824,7 +12824,8 @@ bool CodeGenerator::generateWasm(
  size_t nInboundStackArgBytes = StackArgAreaSizeUnaligned(argTypes);
  wasm::GenerateFunctionPrologue(masm, funcTypeId, mozilla::Nothing(), offsets);
  wasm::GenerateFunctionPrologue(masm, callIndirectId, mozilla::Nothing(),
                                 offsets);
  MOZ_ASSERT(masm.framePushed() == 0);
+1 −1
Original line number Diff line number Diff line
@@ -106,7 +106,7 @@ class CodeGenerator final : public CodeGeneratorSpecific {

  [[nodiscard]] bool generate();
  [[nodiscard]] bool generateWasm(
      wasm::TypeIdDesc funcTypeId, wasm::BytecodeOffset trapOffset,
      wasm::CallIndirectId callIndirectId, wasm::BytecodeOffset trapOffset,
      const wasm::ArgTypeVector& argTys, const RegisterOffsets& trapExitLayout,
      size_t trapExitLayoutNumWords, wasm::FuncOffsets* offsets,
      wasm::StackMaps* stackMaps, wasm::Decoder* decoder);
+7 −7
Original line number Diff line number Diff line
@@ -3958,15 +3958,15 @@ void MacroAssembler::wasmCallIndirect(const wasm::CallSiteDesc& desc,

  // Write the functype-id into the ABI functype-id register.

  const wasm::TypeIdDesc funcTypeId = callee.wasmTableSigId();
  switch (funcTypeId.kind()) {
    case wasm::TypeIdDescKind::Global:
      loadWasmGlobalPtr(funcTypeId.globalDataOffset(), WasmTableCallSigReg);
  const wasm::CallIndirectId callIndirectId = callee.wasmTableSigId();
  switch (callIndirectId.kind()) {
    case wasm::CallIndirectIdKind::Global:
      loadWasmGlobalPtr(callIndirectId.globalDataOffset(), WasmTableCallSigReg);
      break;
    case wasm::TypeIdDescKind::Immediate:
      move32(Imm32(funcTypeId.immediate()), WasmTableCallSigReg);
    case wasm::CallIndirectIdKind::Immediate:
      move32(Imm32(callIndirectId.immediate()), WasmTableCallSigReg);
      break;
    case wasm::TypeIdDescKind::None:
    case wasm::CallIndirectIdKind::None:
      break;
  }

+8 −13
Original line number Diff line number Diff line
@@ -1941,8 +1941,7 @@ class MOZ_STACK_CLASS ModuleValidator : public ModuleValidatorShared {
    }

    *sigIndex = moduleEnv_.types->length();
    return moduleEnv_.types->append(std::move(sig)) &&
           moduleEnv_.typeIds.append(TypeIdDesc());
    return moduleEnv_.types->append(std::move(sig));
  }
  bool declareSig(FuncType&& sig, uint32_t* sigIndex) {
    SigSet::AddPtr p = sigSet_.lookupForAdd(sig);
@@ -2130,16 +2129,14 @@ class MOZ_STACK_CLASS ModuleValidator : public ModuleValidatorShared {
      uint32_t funcTypeIndex = r.front().key().sigIndex();
      MOZ_ASSERT(!moduleEnv_.funcs[funcIndex].type);
      moduleEnv_.funcs[funcIndex] =
          FuncDesc(&moduleEnv_.types->funcType(funcTypeIndex),
                   &moduleEnv_.typeIds[funcTypeIndex], funcTypeIndex);
          FuncDesc(&moduleEnv_.types->funcType(funcTypeIndex), funcTypeIndex);
    }
    for (const Func& func : funcDefs_) {
      uint32_t funcIndex = funcImportMap_.count() + func.funcDefIndex();
      uint32_t funcTypeIndex = func.sigIndex();
      MOZ_ASSERT(!moduleEnv_.funcs[funcIndex].type);
      moduleEnv_.funcs[funcIndex] =
          FuncDesc(&moduleEnv_.types->funcType(funcTypeIndex),
                   &moduleEnv_.typeIds[funcTypeIndex], funcTypeIndex);
          FuncDesc(&moduleEnv_.types->funcType(funcTypeIndex), funcTypeIndex);
    }
    for (const Export& exp : moduleEnv_.exports) {
      if (exp.kind() != DefinitionKind::Function) {
@@ -2150,10 +2147,7 @@ class MOZ_STACK_CLASS ModuleValidator : public ModuleValidatorShared {
                                     /* canRefFunc */ false);
    }

    if (!moduleEnv_.funcImportGlobalDataOffsets.resize(
            funcImportMap_.count())) {
      return nullptr;
    }
    moduleEnv_.numFuncImports = funcImportMap_.count();

    MOZ_ASSERT(asmJSMetadata_->asmJSFuncNames.empty());
    if (!asmJSMetadata_->asmJSFuncNames.resize(funcImportMap_.count())) {
@@ -6185,10 +6179,11 @@ static bool CheckFunction(ModuleValidator<Unit>& m) {
    }
  }

  FuncType sig(std::move(args), std::move(results));

  ModuleValidatorShared::Func* func = nullptr;
  if (!CheckFunctionSignature(m, funNode,
                              FuncType(std::move(args), std::move(results)),
                              FunctionName(funNode), &func)) {
  if (!CheckFunctionSignature(m, funNode, std::move(sig), FunctionName(funNode),
                              &func)) {
    return false;
  }

+0 −4
Original line number Diff line number Diff line
@@ -29,10 +29,6 @@ const FuncType& BaseCompiler::funcType() const {
  return *moduleEnv_.funcs[func_.index].type;
}

const TypeIdDesc& BaseCompiler::funcTypeId() const {
  return *moduleEnv_.funcs[func_.index].typeId;
}

bool BaseCompiler::usesMemory() const { return moduleEnv_.usesMemory(); }

bool BaseCompiler::usesSharedMemory() const {
Loading