Changeset 288758 in webkit


Ignore:
Timestamp:
Jan 28, 2022 12:48:11 PM (6 months ago)
Author:
commit-queue@webkit.org
Message:

[JSC] Add support for WASM branch hinting proposal
https://bugs.webkit.org/show_bug.cgi?id=235581

Patch by Tom Tartarin <tom@leaningtech.com> on 2022-01-28
Reviewed by Yusuke Suzuki.

JSTests:

Add test for branch hinting custom section

  • wasm.yaml:
  • wasm/branch-hints/branchHintsModule.wasm: Added.
  • wasm/branch-hints/branchHintsSection.js: Added.

(const.module):

Source/JavaScriptCore:

See the proposal for a more detailed description: https://github.com/WebAssembly/branch-hinting.

This allows parsing a "code_annotation.branch_hint" custom section,
as per the code annotation proposal: https://github.com/WebAssembly/annotations.
This section provides per function information about how likely a branch at a
given offset is to be taken. It is similar to branch weight metadata in LLVM.

  • JavaScriptCore.xcodeproj/project.pbxproj:
  • Sources.txt:
  • runtime/OptionsList.h:
  • wasm/WasmAirIRGenerator.cpp:

(JSC::Wasm::AirIRGenerator::addIf):
(JSC::Wasm::AirIRGenerator::addBranch):

  • wasm/WasmB3IRGenerator.cpp:

(JSC::Wasm::B3IRGenerator::addIf):
(JSC::Wasm::B3IRGenerator::addBranch):

  • wasm/WasmBranchHints.h: Added.

(JSC::Wasm::BranchHintMap::add):
(JSC::Wasm::BranchHintMap::getBranchHint const):
(JSC::Wasm::BranchHintMap::isValidKey const):
(JSC::Wasm::isValidBranchHint):

  • wasm/WasmBranchHintsSectionParser.cpp: Added.

(JSC::Wasm::BranchHintsSectionParser::parse):

  • wasm/WasmBranchHintsSectionParser.h: Added.

(JSC::Wasm::BranchHintsSectionParser::BranchHintsSectionParser):

  • wasm/WasmModuleInformation.h:

(JSC::Wasm::ModuleInformation::getBranchHint const):

  • wasm/WasmSectionParser.cpp:

(JSC::Wasm::SectionParser::parseCustom):

Location:
trunk
Files:
6 added
10 edited

Legend:

Unmodified
Added
Removed
  • trunk/JSTests/ChangeLog

    r288538 r288758  
     12022-01-28  Tom Tartarin  <tom@leaningtech.com>
     2
     3        [JSC] Add support for WASM branch hinting proposal
     4        https://bugs.webkit.org/show_bug.cgi?id=235581
     5
     6        Reviewed by Yusuke Suzuki.
     7
     8        Add test for branch hinting custom section
     9
     10        * wasm.yaml:
     11        * wasm/branch-hints/branchHintsModule.wasm: Added.
     12        * wasm/branch-hints/branchHintsSection.js: Added.
     13        (const.module):
     14
    1152022-01-24  Yusuke Suzuki  <ysuzuki@apple.com>
    216
  • trunk/JSTests/wasm.yaml

    r285065 r288758  
    4848- path: wasm/v8/
    4949  cmd: runWebAssemblySuite(:no_module, "mjsunit.js") unless parseRunCommands
     50- path: wasm/branch-hints
     51  cmd: runWebAssemblySuite("--useWebAssemblyBranchHints=true")
    5052
    5153- path: wasm/references-spec-tests/elem.wast.js
  • trunk/Source/JavaScriptCore/ChangeLog

    r288748 r288758  
     12022-01-28  Tom Tartarin  <tom@leaningtech.com>
     2
     3        [JSC] Add support for WASM branch hinting proposal
     4        https://bugs.webkit.org/show_bug.cgi?id=235581
     5
     6        Reviewed by Yusuke Suzuki.
     7
     8        See the proposal for a more detailed description: https://github.com/WebAssembly/branch-hinting.
     9
     10        This allows parsing a "code_annotation.branch_hint" custom section,
     11        as per the code annotation proposal: https://github.com/WebAssembly/annotations.
     12        This section provides per function information about how likely a branch at a
     13        given offset is to be taken. It is similar to branch weight metadata in LLVM.
     14
     15
     16        * JavaScriptCore.xcodeproj/project.pbxproj:
     17        * Sources.txt:
     18        * runtime/OptionsList.h:
     19        * wasm/WasmAirIRGenerator.cpp:
     20        (JSC::Wasm::AirIRGenerator::addIf):
     21        (JSC::Wasm::AirIRGenerator::addBranch):
     22        * wasm/WasmB3IRGenerator.cpp:
     23        (JSC::Wasm::B3IRGenerator::addIf):
     24        (JSC::Wasm::B3IRGenerator::addBranch):
     25        * wasm/WasmBranchHints.h: Added.
     26        (JSC::Wasm::BranchHintMap::add):
     27        (JSC::Wasm::BranchHintMap::getBranchHint const):
     28        (JSC::Wasm::BranchHintMap::isValidKey const):
     29        (JSC::Wasm::isValidBranchHint):
     30        * wasm/WasmBranchHintsSectionParser.cpp: Added.
     31        (JSC::Wasm::BranchHintsSectionParser::parse):
     32        * wasm/WasmBranchHintsSectionParser.h: Added.
     33        (JSC::Wasm::BranchHintsSectionParser::BranchHintsSectionParser):
     34        * wasm/WasmModuleInformation.h:
     35        (JSC::Wasm::ModuleInformation::getBranchHint const):
     36        * wasm/WasmSectionParser.cpp:
     37        (JSC::Wasm::SectionParser::parseCustom):
     38
    1392022-01-28  Yusuke Suzuki  <ysuzuki@apple.com>
    240
  • trunk/Source/JavaScriptCore/JavaScriptCore.xcodeproj/project.pbxproj

    r288710 r288758  
    857857                33B2A54722653481005A0F79 /* B3ValueInlines.h in Headers */ = {isa = PBXBuildFile; fileRef = 0FEC84FB1BDACDAC0080FF74 /* B3ValueInlines.h */; };
    858858                33B2A548226543BF005A0F79 /* FTLLowerDFGToB3.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 0FEA0A04170513DB00BB722C /* FTLLowerDFGToB3.cpp */; };
     859                37AAC093279F1BFC00D64842 /* WasmBranchHintsSectionParser.h in Headers */ = {isa = PBXBuildFile; fileRef = 37AAC092279F124200D64842 /* WasmBranchHintsSectionParser.h */; };
     860                37AAC094279F1C0500D64842 /* WasmBranchHints.h in Headers */ = {isa = PBXBuildFile; fileRef = 37AAC091279F124200D64842 /* WasmBranchHints.h */; };
    859861                37C738D21EDB56E4003F2B0B /* ParseInt.h in Headers */ = {isa = PBXBuildFile; fileRef = 37C738D11EDB5672003F2B0B /* ParseInt.h */; settings = {ATTRIBUTES = (Private, ); }; };
    860862                412952771D2CF6BC00E78B89 /* builtins_generate_internals_wrapper_header.py in Headers */ = {isa = PBXBuildFile; fileRef = 412952731D2CF6AC00E78B89 /* builtins_generate_internals_wrapper_header.py */; settings = {ATTRIBUTES = (Private, ); }; };
     
    37273729                37119A7720CCB5DC002C6DC9 /* WebKitTargetConditionals.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = WebKitTargetConditionals.xcconfig; sourceTree = "<group>"; };
    37283730                371D842C17C98B6E00ECF994 /* libz.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libz.dylib; path = usr/lib/libz.dylib; sourceTree = SDKROOT; };
     3731                37AAC090279F124100D64842 /* WasmBranchHintsSectionParser.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = WasmBranchHintsSectionParser.cpp; sourceTree = "<group>"; };
     3732                37AAC091279F124200D64842 /* WasmBranchHints.h */ = {isa = PBXFileReference; explicitFileType = sourcecode.cpp.h; fileEncoding = 4; path = WasmBranchHints.h; sourceTree = "<group>"; };
     3733                37AAC092279F124200D64842 /* WasmBranchHintsSectionParser.h */ = {isa = PBXFileReference; explicitFileType = sourcecode.cpp.h; fileEncoding = 4; path = WasmBranchHintsSectionParser.h; sourceTree = "<group>"; };
    37293734                37C738D11EDB5672003F2B0B /* ParseInt.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ParseInt.h; sourceTree = "<group>"; };
    37303735                412952731D2CF6AC00E78B89 /* builtins_generate_internals_wrapper_header.py */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.script.python; path = builtins_generate_internals_wrapper_header.py; sourceTree = "<group>"; };
     
    73107315                        isa = PBXGroup;
    73117316                        children = (
     7317                                37AAC091279F124200D64842 /* WasmBranchHints.h */,
     7318                                37AAC090279F124100D64842 /* WasmBranchHintsSectionParser.cpp */,
     7319                                37AAC092279F124200D64842 /* WasmBranchHintsSectionParser.h */,
    73127320                                AD2FCB8A1DB5840000B3E736 /* js */,
    73137321                                52847AD921FFB8630061A9DB /* WasmAirIRGenerator.cpp */,
     
    1095710965                                0FE0502B1AA9095600D33B33 /* ScopeOffset.h in Headers */,
    1095810966                                0F24E55217EE274900ABB217 /* ScratchRegisterAllocator.h in Headers */,
     10967                                37AAC094279F1C0500D64842 /* WasmBranchHints.h in Headers */,
    1095910968                                33111B8B2397256500AA34CE /* Scribble.h in Headers */,
    1096010969                                A5FD0068189AFE9C00633231 /* ScriptArguments.h in Headers */,
     
    1108311092                                0F572D4F16879FDD00E57FBD /* ThunkGenerator.h in Headers */,
    1108411093                                A7386556118697B400540279 /* ThunkGenerators.h in Headers */,
     11094                                37AAC093279F1BFC00D64842 /* WasmBranchHintsSectionParser.h in Headers */,
    1108511095                                141448CD13A1783700F5BA1A /* TinyBloomFilter.h in Headers */,
    1108611096                                0F55989817C86C5800A1E543 /* ToNativeFromValue.h in Headers */,
  • trunk/Source/JavaScriptCore/Sources.txt

    r288261 r288758  
    10661066wasm/WasmBBQPlan.cpp
    10671067wasm/WasmBinding.cpp
     1068wasm/WasmBranchHintsSectionParser.cpp
    10681069wasm/WasmCallee.cpp
    10691070wasm/WasmCalleeGroup.cpp
  • trunk/Source/JavaScriptCore/runtime/OptionsList.h

    r288538 r288758  
    551551    v(Bool, useWebAssemblyTypedFunctionReferences, false, Normal, "Allow function types from the wasm typed function references spec.") \
    552552    v(Bool, useWebAssemblyExceptions, true, Normal, "Allow the new section and instructions from the wasm exception handling spec.") \
     553    v(Bool, useWebAssemblyBranchHints, false, Normal, "Allow the new section from the wasm branch hinting spec.") \
    553554
    554555
  • trunk/Source/JavaScriptCore/wasm/WasmAirIRGenerator.cpp

    r288261 r288758  
    4545#include "JSWebAssemblyInstance.h"
    4646#include "ScratchRegisterAllocator.h"
     47#include "WasmBranchHints.h"
    4748#include "WasmCallingConvention.h"
    4849#include "WasmContextInlines.h"
     
    31753176    BasicBlock* notTaken = m_code.addBlock();
    31763177    BasicBlock* continuation = m_code.addBlock();
    3177    
     3178    B3::FrequencyClass takenFrequency = B3::FrequencyClass::Normal;
     3179    B3::FrequencyClass notTakenFrequency= B3::FrequencyClass::Normal;
     3180
     3181    if (Options::useWebAssemblyBranchHints()) {
     3182        BranchHint hint = m_info.getBranchHint(m_functionIndex, m_parser->currentOpcodeStartingOffset());
     3183
     3184        switch (hint) {
     3185        case BranchHint::Unlikely:
     3186            takenFrequency = B3::FrequencyClass::Rare;
     3187            break;
     3188        case BranchHint::Likely:
     3189            notTakenFrequency = B3::FrequencyClass::Rare;
     3190            break;
     3191        case BranchHint::Invalid:
     3192            break;
     3193        }
     3194    }
     3195
    31783196    // Wasm bools are i32.
    31793197    append(BranchTest32, Arg::resCond(MacroAssembler::NonZero), condition, condition);
    3180     m_currentBlock->setSuccessors(taken, notTaken);
     3198    m_currentBlock->setSuccessors(FrequentedBlock(taken, takenFrequency), FrequentedBlock(notTaken, notTakenFrequency));
    31813199
    31823200    m_currentBlock = taken;
     
    34183436
    34193437    BasicBlock* target = data.targetBlockForBranch();
     3438    B3::FrequencyClass targetFrequency = B3::FrequencyClass::Normal;
     3439    B3::FrequencyClass continuationFrequency = B3::FrequencyClass::Normal;
     3440
     3441    if (Options::useWebAssemblyBranchHints()) {
     3442        BranchHint hint = m_info.getBranchHint(m_functionIndex, m_parser->currentOpcodeStartingOffset());
     3443
     3444        switch (hint) {
     3445        case BranchHint::Unlikely:
     3446            targetFrequency = B3::FrequencyClass::Rare;
     3447            break;
     3448        case BranchHint::Likely:
     3449            continuationFrequency = B3::FrequencyClass::Rare;
     3450            break;
     3451        case BranchHint::Invalid:
     3452            break;
     3453        }
     3454    }
     3455
    34203456    if (condition) {
    34213457        BasicBlock* continuation = m_code.addBlock();
    34223458        append(BranchTest32, Arg::resCond(MacroAssembler::NonZero), condition, condition);
    3423         m_currentBlock->setSuccessors(target, continuation);
     3459        m_currentBlock->setSuccessors(FrequentedBlock(target, targetFrequency), FrequentedBlock(continuation, continuationFrequency));
    34243460        m_currentBlock = continuation;
    34253461    } else {
    34263462        append(Jump);
    3427         m_currentBlock->setSuccessors(target);
     3463        m_currentBlock->setSuccessors(FrequentedBlock(target, targetFrequency));
    34283464    }
    34293465
  • trunk/Source/JavaScriptCore/wasm/WasmB3IRGenerator.cpp

    r288261 r288758  
    5353#include "ProbeContext.h"
    5454#include "ScratchRegisterAllocator.h"
    55 #include "ScratchRegisterAllocator.h"
     55#include "WasmBranchHints.h"
    5656#include "WasmCallingConvention.h"
    5757#include "WasmContextInlines.h"
     
    24762476    BasicBlock* notTaken = m_proc.addBlock();
    24772477    BasicBlock* continuation = m_proc.addBlock();
     2478    FrequencyClass takenFrequency = FrequencyClass::Normal;
     2479    FrequencyClass notTakenFrequency = FrequencyClass::Normal;
     2480
     2481    if (Options::useWebAssemblyBranchHints()) {
     2482        BranchHint hint = m_info.getBranchHint(m_functionIndex, m_parser->currentOpcodeStartingOffset());
     2483
     2484        switch (hint) {
     2485        case BranchHint::Unlikely:
     2486            takenFrequency = FrequencyClass::Rare;
     2487            break;
     2488        case BranchHint::Likely:
     2489            notTakenFrequency = FrequencyClass::Rare;
     2490            break;
     2491        case BranchHint::Invalid:
     2492            break;
     2493        }
     2494    }
    24782495
    24792496    m_currentBlock->appendNew<Value>(m_proc, B3::Branch, origin(), get(condition));
    2480     m_currentBlock->setSuccessors(FrequentedBlock(taken), FrequentedBlock(notTaken));
     2497    m_currentBlock->setSuccessors(FrequentedBlock(taken, takenFrequency), FrequentedBlock(notTaken, notTakenFrequency));
    24812498    taken->addPredecessor(m_currentBlock);
    24822499    notTaken->addPredecessor(m_currentBlock);
     
    27222739
    27232740    BasicBlock* target = data.targetBlockForBranch();
     2741    FrequencyClass targetFrequency = FrequencyClass::Normal;
     2742    FrequencyClass continuationFrequency = FrequencyClass::Normal;
     2743
     2744    if (Options::useWebAssemblyBranchHints()) {
     2745        BranchHint hint = m_info.getBranchHint(m_functionIndex, m_parser->currentOpcodeStartingOffset());
     2746
     2747        switch (hint) {
     2748        case BranchHint::Unlikely:
     2749            targetFrequency = FrequencyClass::Rare;
     2750            break;
     2751        case BranchHint::Likely:
     2752            continuationFrequency = FrequencyClass::Rare;
     2753            break;
     2754        case BranchHint::Invalid:
     2755            break;
     2756        }
     2757    }
     2758
    27242759    if (condition) {
    27252760        BasicBlock* continuation = m_proc.addBlock();
    27262761        m_currentBlock->appendNew<Value>(m_proc, B3::Branch, origin(), get(condition));
    2727         m_currentBlock->setSuccessors(FrequentedBlock(target), FrequentedBlock(continuation));
     2762        m_currentBlock->setSuccessors(FrequentedBlock(target, targetFrequency), FrequentedBlock(continuation, continuationFrequency));
    27282763        target->addPredecessor(m_currentBlock);
    27292764        continuation->addPredecessor(m_currentBlock);
    27302765        m_currentBlock = continuation;
    27312766    } else {
    2732         m_currentBlock->appendNewControlValue(m_proc, Jump, origin(), FrequentedBlock(target));
     2767        m_currentBlock->appendNewControlValue(m_proc, Jump, origin(), FrequentedBlock(target, targetFrequency));
    27332768        target->addPredecessor(m_currentBlock);
    27342769    }
  • trunk/Source/JavaScriptCore/wasm/WasmModuleInformation.h

    r288573 r288758  
    2828#if ENABLE(WEBASSEMBLY)
    2929
     30#include "WasmBranchHints.h"
    3031#include "WasmFormat.h"
    3132
    3233#include <wtf/BitVector.h>
     34#include <wtf/HashMap.h>
    3335
    3436namespace JSC { namespace Wasm {
    3537
    3638struct ModuleInformation : public ThreadSafeRefCounted<ModuleInformation> {
     39
     40    using BranchHints = HashMap<uint32_t, BranchHintMap, IntHash<uint32_t>, WTF::UnsignedWithZeroKeyHashTraits<uint32_t>>;
     41
    3742    ModuleInformation();
    3843    ModuleInformation(const ModuleInformation&) = delete;
     
    97102    bool hasMemoryImport() const { return memory.isImport(); }
    98103
     104    BranchHint getBranchHint(uint32_t functionOffset, uint32_t branchOffset) const
     105    {
     106        auto it = branchHints.find(functionOffset);
     107        return it == branchHints.end()
     108            ? BranchHint::Invalid
     109            : it->value.getBranchHint(branchOffset);
     110    }
     111
    99112    Vector<Import> imports;
    100113    Vector<SignatureIndex> importFunctionSignatureIndices;
     
    118131    Vector<CustomSection> customSections;
    119132    Ref<NameSection> nameSection;
     133    BranchHints branchHints;
    120134    uint32_t numberOfDataSegments { 0 };
    121135
  • trunk/Source/JavaScriptCore/wasm/WasmSectionParser.cpp

    r285065 r288758  
    3131
    3232#include "JSCJSValueInlines.h"
     33#include "WasmBranchHintsSectionParser.h"
    3334#include "WasmMemoryInformation.h"
    3435#include "WasmNameSectionParser.h"
     
    897898        if (auto nameSection = nameSectionParser.parse())
    898899            m_info->nameSection = WTFMove(*nameSection);
     900    } else if (Options::useWebAssemblyBranchHints()) {
     901        Name branchHintsName = { 'c', 'o', 'd', 'e', '_', 'a', 'n', 'n', 'o', 't', 'a', 't', 'i', 'o', 'n', '.', 'b', 'r', 'a', 'n', 'c', 'h', '_', 'h', 'i', 'n', 't' };
     902        if (section.name == branchHintsName) {
     903            BranchHintsSectionParser branchHintsSectionParser(section.payload.begin(), section.payload.size(), m_info);
     904            branchHintsSectionParser.parse();
     905        }
    899906    }
    900907
Note: See TracChangeset for help on using the changeset viewer.