DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Mercurial (b6d82b1a6b02)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 * vim: set ts=8 sts=2 et sw=2 tw=80:
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "jit/IonOptimizationLevels.h"

#include "jit/Ion.h"
#include "vm/JSScript.h"

using namespace js;
using namespace js::jit;

namespace js {
namespace jit {

const OptimizationLevelInfo IonOptimizations;

void OptimizationInfo::initNormalOptimizationInfo() {
  level_ = OptimizationLevel::Normal;

  autoTruncate_ = true;
  eaa_ = true;
  edgeCaseAnalysis_ = true;
  eliminateRedundantChecks_ = true;
  inlineInterpreted_ = true;
  inlineNative_ = true;
  licm_ = true;
  gvn_ = true;
  rangeAnalysis_ = true;
  reordering_ = true;
  scalarReplacement_ = true;
  sink_ = true;

  registerAllocator_ = RegisterAllocator_Backtracking;

  inlineMaxBytecodePerCallSiteMainThread_ = 200;
  inlineMaxBytecodePerCallSiteHelperThread_ = 400;
  inlineMaxCalleeInlinedBytecodeLength_ = 3550;
  inlineMaxTotalBytecodeLength_ = 85000;
  inliningMaxCallerBytecodeLength_ = 1600;
  maxInlineDepth_ = 0;
  smallFunctionMaxInlineDepth_ = 1;
  inliningWarmUpThresholdFactor_ = 0.5;
  inliningRecompileThresholdFactor_ = 4;
}

void OptimizationInfo::initFullOptimizationInfo() {
  initNormalOptimizationInfo();

  level_ = OptimizationLevel::Full;

  inlineMaxBytecodePerCallSiteMainThread_ = 550;
  inlineMaxBytecodePerCallSiteHelperThread_ = 1100;
  maxInlineDepth_ = 3;
  smallFunctionMaxInlineDepth_ = 10;
  inliningWarmUpThresholdFactor_ = 0.125;
}

void OptimizationInfo::initWasmOptimizationInfo() {
  // The Wasm optimization level
  // Disables some passes that don't work well with wasm.

  // Take normal option values for not specified values.
  initNormalOptimizationInfo();

  level_ = OptimizationLevel::Wasm;

  ama_ = true;
  autoTruncate_ = false;
  edgeCaseAnalysis_ = false;
  eliminateRedundantChecks_ = false;
  scalarReplacement_ = false;  // wasm has no objects.
  sink_ = false;
}

uint32_t OptimizationInfo::compilerWarmUpThreshold(JSScript* script,
                                                   jsbytecode* pc) const {
  MOZ_ASSERT(pc == nullptr || pc == script->code() ||
             JSOp(*pc) == JSOP_LOOPENTRY);

  if (pc == script->code()) {
    pc = nullptr;
  }

  uint32_t warmUpThreshold = baseCompilerWarmUpThreshold();

  // If the script is too large to compile on the main thread, we can still
  // compile it off thread. In these cases, increase the warm-up counter
  // threshold to improve the compilation's type information and hopefully
  // avoid later recompilation.

  if (script->length() > JitOptions.ionMaxScriptSizeMainThread) {
    warmUpThreshold *=
        (script->length() / double(JitOptions.ionMaxScriptSizeMainThread));
  }

  uint32_t numLocalsAndArgs = NumLocalsAndArgs(script);
  if (numLocalsAndArgs > JitOptions.ionMaxLocalsAndArgsMainThread) {
    warmUpThreshold *=
        (numLocalsAndArgs / double(JitOptions.ionMaxLocalsAndArgsMainThread));
  }

  if (!pc || JitOptions.eagerIonCompilation()) {
    return warmUpThreshold;
  }

  // It's more efficient to enter outer loops, rather than inner loops, via OSR.
  // To accomplish this, we use a slightly higher threshold for inner loops.
  // Note that the loop depth is always > 0 so we will prefer non-OSR over OSR.
  uint32_t loopDepth = LoopEntryDepthHint(pc);
  MOZ_ASSERT(loopDepth > 0);
  return warmUpThreshold + loopDepth * (baseCompilerWarmUpThreshold() / 10);
}

uint32_t OptimizationInfo::recompileWarmUpThreshold(JSScript* script,
                                                    jsbytecode* pc) const {
  MOZ_ASSERT(pc == script->code() || *pc == JSOP_LOOPENTRY);

  uint32_t threshold = compilerWarmUpThreshold(script, pc);
  if (*pc != JSOP_LOOPENTRY || JitOptions.eagerIonCompilation()) {
    return threshold;
  }

  // If we're stuck in a long-running loop at a low optimization level, we have
  // to invalidate to be able to tier up. This is worse than recompiling at
  // function entry (because in that case we can use the lazy link mechanism and
  // avoid invalidation completely). Use a very high recompilation threshold for
  // loop edges so that this only affects very long-running loops.

  uint32_t loopDepth = LoopEntryDepthHint(pc);
  MOZ_ASSERT(loopDepth > 0);
  return threshold + loopDepth * (baseCompilerWarmUpThreshold() / 10);
}

OptimizationLevelInfo::OptimizationLevelInfo() {
  infos_[OptimizationLevel::Normal].initNormalOptimizationInfo();
  infos_[OptimizationLevel::Full].initFullOptimizationInfo();
  infos_[OptimizationLevel::Wasm].initWasmOptimizationInfo();

#ifdef DEBUG
  OptimizationLevel level = firstLevel();
  while (!isLastLevel(level)) {
    OptimizationLevel next = nextLevel(level);
    MOZ_ASSERT_IF(level != OptimizationLevel::DontCompile, level < next);
    level = next;
  }
#endif
}

OptimizationLevel OptimizationLevelInfo::nextLevel(
    OptimizationLevel level) const {
  MOZ_ASSERT(!isLastLevel(level));
  switch (level) {
    case OptimizationLevel::DontCompile:
      return OptimizationLevel::Normal;
    case OptimizationLevel::Normal:
      return OptimizationLevel::Full;
    case OptimizationLevel::Full:
    case OptimizationLevel::Wasm:
    case OptimizationLevel::Count:
      break;
  }
  MOZ_CRASH("Unknown optimization level.");
}

OptimizationLevel OptimizationLevelInfo::firstLevel() const {
  return nextLevel(OptimizationLevel::DontCompile);
}

bool OptimizationLevelInfo::isLastLevel(OptimizationLevel level) const {
  return level == OptimizationLevel::Full;
}

OptimizationLevel OptimizationLevelInfo::levelForScript(JSScript* script,
                                                        jsbytecode* pc) const {
  OptimizationLevel prev = OptimizationLevel::DontCompile;

  while (!isLastLevel(prev)) {
    OptimizationLevel level = nextLevel(prev);
    const OptimizationInfo* info = get(level);
    if (script->getWarmUpCount() < info->compilerWarmUpThreshold(script, pc)) {
      return prev;
    }

    prev = level;
  }

  return prev;
}

}  // namespace jit
}  // namespace js