c3a6ca68d0
SourcePosition::InliningId() refers to a the new table DeoptimizationInputData::InliningPositions(), which provides the following data for every inlining id: - The inlined SharedFunctionInfo as an offset into DeoptimizationInfo::LiteralArray - The SourcePosition of the inlining. Recursively, this yields the full inlining stack. Before the Code object is created, the same information can be found in CompilationInfo::inlined_functions(). If SourcePosition::InliningId() is SourcePosition::kNotInlined, it refers to the outer (non-inlined) function. So every SourcePosition has full information about its inlining stack, as long as the corresponding Code object is known. The internal represenation of a source position is a positive 64bit integer. All compilers create now appropriate source positions for inlined functions. In the case of Turbofan, this required using AstGraphBuilderWithPositions for inlined functions too. So this class is now moved to a header file. At the moment, the additional information in source positions is only used in --trace-deopt and --code-comments. The profiler needs to be updated, at the moment it gets the correct script offsets from the deopt info, but the wrong script id from the reconstructed deopt stack, which can lead to wrong outputs. This should be resolved by making the profiler use the new inlining information for deopts. I activated the inlined deoptimization tests in test-cpu-profiler.cc for Turbofan, changing them to a case where the deopt stack and the inlining position agree. It is currently still broken for other cases. The following additional changes were necessary: - The source position table (internal::SourcePositionTableBuilder etc.) supports now 64bit source positions. Encoding source positions in a single 64bit int together with the difference encoding in the source position table results in very little overhead for the inlining id, since only 12% of the source positions in Octane have a changed inlining id. - The class HPositionInfo was effectively dead code and is now removed. - SourcePosition has new printing and information facilities, including computing a full inlining stack. - I had to rename compiler/source-position.{h,cc} to compiler/compiler-source-position-table.{h,cc} to avoid clashes with the new src/source-position.cc file. - I wrote the new wrapper PodArray for ByteArray. It is a template working with any POD-type. This is used in DeoptimizationInputData::InliningPositions(). - I removed HInlinedFunctionInfo and HGraph::inlined_function_infos, because they were only used for the now obsolete Crankshaft inlining ids. - Crankshaft managed a list of inlined functions in Lithium: LChunk::inlined_functions. This is an analog structure to CompilationInfo::inlined_functions. So I removed LChunk::inlined_functions and made Crankshaft use CompilationInfo::inlined_functions instead, because this was necessary to register the offsets into the literal array in a uniform way. This is a safe change because LChunk::inlined_functions has no other uses and the functions in CompilationInfo::inlined_functions have a strictly longer lifespan, being created earlier (in Hydrogen already). BUG=v8:5432 Review-Url: https://codereview.chromium.org/2451853002 Cr-Commit-Position: refs/heads/master@{#40975}
226 lines
7.6 KiB
C++
226 lines
7.6 KiB
C++
// Copyright 2016 the V8 project authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
// found in the LICENSE file.
|
|
|
|
#include "test/cctest/interpreter/source-position-matcher.h"
|
|
|
|
#include "src/objects-inl.h"
|
|
#include "src/objects.h"
|
|
|
|
namespace v8 {
|
|
namespace internal {
|
|
namespace interpreter {
|
|
|
|
// Comparer for PositionTableEntry instances.
|
|
struct PositionTableEntryComparer {
|
|
bool operator()(const PositionTableEntry& lhs,
|
|
const PositionTableEntry& rhs) const {
|
|
int lhs_type_score = type_score(lhs);
|
|
int rhs_type_score = type_score(rhs);
|
|
if (lhs_type_score == rhs_type_score) {
|
|
return lhs.source_position < rhs.source_position;
|
|
} else {
|
|
return lhs_type_score < rhs_type_score;
|
|
}
|
|
}
|
|
|
|
int type_score(const PositionTableEntry& entry) const {
|
|
return entry.is_statement ? 1 : 0;
|
|
}
|
|
};
|
|
|
|
//
|
|
// The principles for comparing source positions in bytecode arrays
|
|
// are:
|
|
//
|
|
// 1. The number of statement positions must be the same in both.
|
|
//
|
|
// 2. Statement positions may be moved provide they do not affect the
|
|
// debuggers causal view of the v8 heap and local state. This means
|
|
// statement positions may be moved when their initial position is
|
|
// on bytecodes that manipulate the accumulator and temporary
|
|
// registers.
|
|
//
|
|
// 3. When duplicate expression positions are present, either may
|
|
// be dropped.
|
|
//
|
|
// 4. Expression positions may be applied to later bytecodes in the
|
|
// bytecode array if the current bytecode does not throw.
|
|
//
|
|
// 5. Expression positions may be dropped when they are applied to
|
|
// bytecodes that manipulate local frame state and immediately
|
|
// proceeded by another source position.
|
|
//
|
|
// 6. The relative ordering of source positions must be preserved.
|
|
//
|
|
bool SourcePositionMatcher::Match(Handle<BytecodeArray> original_bytecode,
|
|
Handle<BytecodeArray> optimized_bytecode) {
|
|
SourcePositionTableIterator original(
|
|
original_bytecode->source_position_table());
|
|
SourcePositionTableIterator optimized(
|
|
optimized_bytecode->source_position_table());
|
|
|
|
int last_original_bytecode_offset = 0;
|
|
int last_optimized_bytecode_offset = 0;
|
|
|
|
// Ordered lists of expression positions immediately before the
|
|
// latest statements in each bytecode array.
|
|
std::vector<PositionTableEntry> original_expression_entries;
|
|
std::vector<PositionTableEntry> optimized_expression_entries;
|
|
|
|
while (true) {
|
|
MoveToNextStatement(&original, &original_expression_entries);
|
|
MoveToNextStatement(&optimized, &optimized_expression_entries);
|
|
|
|
if (original.done() && optimized.done()) {
|
|
return true;
|
|
} else if (original.done()) {
|
|
return false;
|
|
} else if (optimized.done()) {
|
|
return false;
|
|
}
|
|
|
|
if (HasNewExpressionPositionsInOptimized(&original_expression_entries,
|
|
&optimized_expression_entries)) {
|
|
return false;
|
|
}
|
|
|
|
StripUnneededExpressionPositions(original_bytecode,
|
|
&original_expression_entries,
|
|
original.code_offset());
|
|
StripUnneededExpressionPositions(optimized_bytecode,
|
|
&optimized_expression_entries,
|
|
optimized.code_offset());
|
|
|
|
if (!CompareExpressionPositions(&original_expression_entries,
|
|
&optimized_expression_entries)) {
|
|
// Message logged in CompareExpressionPositions().
|
|
return false;
|
|
}
|
|
|
|
// Check original and optimized have matching source positions.
|
|
if (original.source_position() != optimized.source_position()) {
|
|
return false;
|
|
}
|
|
|
|
if (original.code_offset() < last_original_bytecode_offset) {
|
|
return false;
|
|
}
|
|
last_original_bytecode_offset = original.code_offset();
|
|
|
|
if (optimized.code_offset() < last_optimized_bytecode_offset) {
|
|
return false;
|
|
}
|
|
last_optimized_bytecode_offset = optimized.code_offset();
|
|
|
|
// TODO(oth): Can we compare statement positions are semantically
|
|
// equivalent? e.g. before a bytecode that has debugger observable
|
|
// effects. This is likely non-trivial.
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
bool SourcePositionMatcher::HasNewExpressionPositionsInOptimized(
|
|
const std::vector<PositionTableEntry>* const original_positions,
|
|
const std::vector<PositionTableEntry>* const optimized_positions) {
|
|
std::set<PositionTableEntry, PositionTableEntryComparer> original_set(
|
|
original_positions->begin(), original_positions->end());
|
|
|
|
bool retval = false;
|
|
for (auto optimized_position : *optimized_positions) {
|
|
if (original_set.find(optimized_position) == original_set.end()) {
|
|
retval = true;
|
|
}
|
|
}
|
|
return retval;
|
|
}
|
|
|
|
bool SourcePositionMatcher::CompareExpressionPositions(
|
|
const std::vector<PositionTableEntry>* const original_positions,
|
|
const std::vector<PositionTableEntry>* const optimized_positions) {
|
|
if (original_positions->size() != optimized_positions->size()) {
|
|
return false;
|
|
}
|
|
|
|
if (original_positions->size() == 0) {
|
|
return true;
|
|
}
|
|
|
|
for (size_t i = 0; i < original_positions->size(); ++i) {
|
|
PositionTableEntry original = original_positions->at(i);
|
|
PositionTableEntry optimized = original_positions->at(i);
|
|
CHECK(original.source_position > 0);
|
|
if ((original.is_statement || optimized.is_statement) ||
|
|
(original.source_position != optimized.source_position) ||
|
|
(original.source_position < 0)) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
void SourcePositionMatcher::StripUnneededExpressionPositions(
|
|
Handle<BytecodeArray> bytecode_array,
|
|
std::vector<PositionTableEntry>* expression_positions,
|
|
int next_statement_bytecode_offset) {
|
|
size_t j = 0;
|
|
for (size_t i = 0; i < expression_positions->size(); ++i) {
|
|
CHECK(expression_positions->at(i).source_position > 0 &&
|
|
!expression_positions->at(i).is_statement);
|
|
int bytecode_end = (i == expression_positions->size() - 1)
|
|
? next_statement_bytecode_offset
|
|
: expression_positions->at(i + 1).code_offset;
|
|
if (ExpressionPositionIsNeeded(bytecode_array,
|
|
expression_positions->at(i).code_offset,
|
|
bytecode_end)) {
|
|
expression_positions->at(j++) = expression_positions->at(i);
|
|
}
|
|
}
|
|
expression_positions->resize(j);
|
|
}
|
|
|
|
void SourcePositionMatcher::AdvanceBytecodeIterator(
|
|
BytecodeArrayIterator* iterator, int bytecode_offset) {
|
|
while (iterator->current_offset() != bytecode_offset) {
|
|
iterator->Advance();
|
|
}
|
|
}
|
|
|
|
bool SourcePositionMatcher::ExpressionPositionIsNeeded(
|
|
Handle<BytecodeArray> bytecode_array, int start_offset, int end_offset) {
|
|
CHECK_GT(end_offset, start_offset);
|
|
BytecodeArrayIterator iterator(bytecode_array);
|
|
AdvanceBytecodeIterator(&iterator, start_offset);
|
|
|
|
while (iterator.current_offset() != end_offset) {
|
|
if (Bytecodes::IsWithoutExternalSideEffects(iterator.current_bytecode())) {
|
|
iterator.Advance();
|
|
} else {
|
|
// Bytecode could throw so need an expression position.
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
void SourcePositionMatcher::MoveToNextStatement(
|
|
SourcePositionTableIterator* iterator,
|
|
std::vector<PositionTableEntry>* positions) {
|
|
iterator->Advance();
|
|
positions->clear();
|
|
while (!iterator->done()) {
|
|
if (iterator->is_statement()) {
|
|
break;
|
|
}
|
|
positions->push_back({iterator->code_offset(),
|
|
iterator->source_position().raw(),
|
|
iterator->is_statement()});
|
|
iterator->Advance();
|
|
}
|
|
}
|
|
|
|
} // namespace interpreter
|
|
} // namespace internal
|
|
} // namespace v8
|