Commit 28a3e34b authored by Franziska Hinkelmann's avatar Franziska Hinkelmann Committed by Commit Bot

[type-profile] Return type profile object.

Return a structured objet with the type profile
information.

Move the test from message to mjsunit.

BUG=v8:5933

Change-Id: I3e1c592697924d87f82d46b0ddbdb6d82d9c8467
Reviewed-on: https://chromium-review.googlesource.com/464847Reviewed-by: 's avatarMichael Stanton <mvstanton@chromium.org>
Commit-Queue: Franziska Hinkelmann <franzih@chromium.org>
Cr-Commit-Position: refs/heads/master@{#44364}
parent e8c109e2
...@@ -944,54 +944,51 @@ void CollectTypeProfileNexus::Collect(Handle<String> type, int position) { ...@@ -944,54 +944,51 @@ void CollectTypeProfileNexus::Collect(Handle<String> type, int position) {
namespace { namespace {
void PrintTypes(Handle<ArrayList> s) { Handle<JSObject> ConvertToJSObject(Isolate* isolate,
for (int i = 0; i < s->Length(); i++) { Handle<UnseededNumberDictionary> feedback) {
Object* entry = s->Get(i); Handle<JSObject> type_profile =
if (entry->IsString()) { isolate->factory()->NewJSObject(isolate->object_function());
PrintF("%s\n", String::cast(entry)->ToCString().get());
}
}
}
void SortTypes(Isolate* isolate,
Handle<UnseededNumberDictionary> unsorted_types,
std::map<int, Handle<ArrayList>>* types) {
for (int index = UnseededNumberDictionary::kElementsStartIndex; for (int index = UnseededNumberDictionary::kElementsStartIndex;
index < unsorted_types->length(); index < feedback->length();
index += UnseededNumberDictionary::kEntrySize) { index += UnseededNumberDictionary::kEntrySize) {
int key_index = index + UnseededNumberDictionary::kEntryKeyIndex; int key_index = index + UnseededNumberDictionary::kEntryKeyIndex;
Object* key = unsorted_types->get(key_index); Object* key = feedback->get(key_index);
if (key->IsSmi()) { if (key->IsSmi()) {
int value_index = index + UnseededNumberDictionary::kEntryValueIndex; int value_index = index + UnseededNumberDictionary::kEntryValueIndex;
Handle<ArrayList> types_for_position = Handle<ArrayList>(
ArrayList::cast(unsorted_types->get(value_index)), isolate);
(*types)[Smi::cast(key)->value()] = types_for_position; Handle<ArrayList> orig = Handle<ArrayList>(
ArrayList::cast(feedback->get(value_index)), isolate);
// Delete the first entry, i.e., the length.
Handle<FixedArray> storage =
isolate->factory()->NewFixedArray(orig->Length());
orig->CopyTo(1, *storage, 0, orig->Length());
int pos = Smi::cast(key)->value();
JSObject::AddDataElement(
type_profile, pos,
isolate->factory()->NewJSArrayWithElements(storage),
PropertyAttributes::NONE)
.ToHandleChecked();
} }
} }
return type_profile;
} }
} // namespace } // namespace
void CollectTypeProfileNexus::Print() const { JSObject* CollectTypeProfileNexus::GetTypeProfile() const {
Isolate* isolate = GetIsolate(); Isolate* isolate = GetIsolate();
Object* const feedback = GetFeedback(); Object* const feedback = GetFeedback();
if (feedback == *FeedbackVector::UninitializedSentinel(isolate)) { if (feedback == *FeedbackVector::UninitializedSentinel(isolate)) {
return; return *isolate->factory()->NewJSMap();
} }
Handle<UnseededNumberDictionary> unsorted_types = return *ConvertToJSObject(
Handle<UnseededNumberDictionary>(UnseededNumberDictionary::cast(feedback), isolate, Handle<UnseededNumberDictionary>(
isolate); UnseededNumberDictionary::cast(feedback), isolate));
std::map<int, Handle<ArrayList>> types;
SortTypes(isolate, unsorted_types, &types);
for (const auto& entry : types) {
PrintF("%d:\n", entry.first);
PrintTypes(entry.second);
}
} }
} // namespace internal } // namespace internal
......
...@@ -759,13 +759,9 @@ class CollectTypeProfileNexus : public FeedbackNexus { ...@@ -759,13 +759,9 @@ class CollectTypeProfileNexus : public FeedbackNexus {
DCHECK_EQ(FeedbackSlotKind::kTypeProfile, vector->GetKind(slot)); DCHECK_EQ(FeedbackSlotKind::kTypeProfile, vector->GetKind(slot));
} }
// Add a type to the list of types. // Add a type to the list of types for source position <position>.
void Collect(Handle<String> type, int position); void Collect(Handle<String> type, int position);
JSObject* GetTypeProfile() const;
// Dump the types to stdout.
// TODO(franzih): pass this information to the debugger protocol instead of
// stdout.
void Print() const;
InlineCacheState StateFromFeedback() const override; InlineCacheState StateFromFeedback() const override;
}; };
......
...@@ -210,7 +210,7 @@ RUNTIME_FUNCTION(Runtime_IsConcurrentRecompilationSupported) { ...@@ -210,7 +210,7 @@ RUNTIME_FUNCTION(Runtime_IsConcurrentRecompilationSupported) {
isolate->concurrent_recompilation_enabled()); isolate->concurrent_recompilation_enabled());
} }
RUNTIME_FUNCTION(Runtime_PrintTypeProfile) { RUNTIME_FUNCTION(Runtime_TypeProfile) {
HandleScope scope(isolate); HandleScope scope(isolate);
DCHECK_EQ(1, args.length()); DCHECK_EQ(1, args.length());
...@@ -221,18 +221,13 @@ RUNTIME_FUNCTION(Runtime_PrintTypeProfile) { ...@@ -221,18 +221,13 @@ RUNTIME_FUNCTION(Runtime_PrintTypeProfile) {
CONVERT_ARG_HANDLE_CHECKED(JSFunction, function, 0); CONVERT_ARG_HANDLE_CHECKED(JSFunction, function, 0);
if (function->has_feedback_vector()) { if (function->has_feedback_vector()) {
FeedbackVector* vector = function->feedback_vector(); FeedbackVector* vector = function->feedback_vector();
Object* function_name = vector->shared_function_info()->name();
PrintF("Function: %s\n", String::cast(function_name)->ToCString().get());
if (vector->metadata()->HasTypeProfileSlot()) { if (vector->metadata()->HasTypeProfileSlot()) {
FeedbackSlot slot = vector->GetTypeProfileSlot(); FeedbackSlot slot = vector->GetTypeProfileSlot();
CollectTypeProfileNexus nexus(vector, slot); CollectTypeProfileNexus nexus(vector, slot);
nexus.Print(); return nexus.GetTypeProfile();
PrintF("\n");
} }
} }
return isolate->heap()->undefined_value(); return *isolate->factory()->NewJSObject(isolate->object_function());
} }
RUNTIME_FUNCTION(Runtime_OptimizeFunctionOnNextCall) { RUNTIME_FUNCTION(Runtime_OptimizeFunctionOnNextCall) {
......
...@@ -552,7 +552,7 @@ namespace internal { ...@@ -552,7 +552,7 @@ namespace internal {
F(RunningInSimulator, 0, 1) \ F(RunningInSimulator, 0, 1) \
F(IsConcurrentRecompilationSupported, 0, 1) \ F(IsConcurrentRecompilationSupported, 0, 1) \
F(OptimizeFunctionOnNextCall, -1, 1) \ F(OptimizeFunctionOnNextCall, -1, 1) \
F(PrintTypeProfile, 1, 1) \ F(TypeProfile, 1, 1) \
F(OptimizeOsr, -1, 1) \ F(OptimizeOsr, -1, 1) \
F(NeverOptimizeFunction, 1, 1) \ F(NeverOptimizeFunction, 1, 1) \
F(GetOptimizationStatus, -1, 1) \ F(GetOptimizationStatus, -1, 1) \
......
Function: testFunction
247:
Object
number
string
number
254:
undefined
boolean
undefined
undefined
443:
Object
number
string
number
Function: testFunction
247:
Object
number
string
number
undefined
string
Object
Object
MyClass
254:
undefined
boolean
undefined
undefined
undefined
boolean
boolean
undefined
undefined
443:
Object
number
string
number
undefined
string
Object
Object
MyClass
Function: try_finally
1038:
string
Function: fall_off
1122:
undefined
Function: check_param
1272:
number
1275:
string
1280:
Object
1291:
MyClass
1323:
undefined
*%(basename)s:70: end
throw "end";
^
This diff is collapsed.
...@@ -4,6 +4,16 @@ ...@@ -4,6 +4,16 @@
// Flags: --type-profile --turbo --allow-natives-syntax // Flags: --type-profile --turbo --allow-natives-syntax
function check_collect_types(name, expected) {
const type_profile = %TypeProfile(name);
if (type_profile !== undefined) {
const result = JSON.stringify(type_profile);
print(result);
assertEquals(expected, result, name + " failed");
}
}
function testFunction(param, flag) { function testFunction(param, flag) {
// We want to test 2 different return positions in one function. // We want to test 2 different return positions in one function.
if (flag) { if (flag) {
...@@ -18,13 +28,16 @@ class MyClass { ...@@ -18,13 +28,16 @@ class MyClass {
constructor() {} constructor() {}
} }
%PrintTypeProfile(testFunction); var expected = `{}`;
check_collect_types(testFunction, expected);
testFunction({}); testFunction({});
testFunction(123, true); testFunction(123, true);
testFunction('hello'); testFunction('hello');
testFunction(123); testFunction(123);
%PrintTypeProfile(testFunction);
expected = `{\"503\":[\"Object\",\"number\",\"string\",\"number\"],\"510\":[\"undefined\",\"boolean\",\"undefined\",\"undefined\"],\"699\":[\"Object\",\"number\",\"string\",\"number\"]}`;
check_collect_types(testFunction, expected);
testFunction(undefined); testFunction(undefined);
testFunction('hello', true); testFunction('hello', true);
...@@ -32,11 +45,16 @@ testFunction({x: 12}, true); ...@@ -32,11 +45,16 @@ testFunction({x: 12}, true);
testFunction({x: 12}); testFunction({x: 12});
testFunction(new MyClass()); testFunction(new MyClass());
%PrintTypeProfile(testFunction); expected = `{\"503\":[\"Object\",\"number\",\"string\",\"number\",\"undefined\",\"string\",\"Object\",\"Object\",\"MyClass\"],\"510\":[\"undefined\",\"boolean\",\"undefined\",\"undefined\",\"undefined\",\"boolean\",\"boolean\",\"undefined\",\"undefined\"],\"699\":[\"Object\",\"number\",\"string\",\"number\",\"undefined\",\"string\",\"Object\",\"Object\",\"MyClass\"]}`;
check_collect_types(testFunction, expected);
function testReturnOfNonVariable() { function testReturnOfNonVariable() {
return 32; return 32;
} }
testReturnOfNonVariable();
expected = `{\"1732\":[\"number\"]}`;
check_collect_types(testReturnOfNonVariable, expected);
// Return statement is reached but its expression is never really returned. // Return statement is reached but its expression is never really returned.
function try_finally() { function try_finally() {
...@@ -47,24 +65,26 @@ function try_finally() { ...@@ -47,24 +65,26 @@ function try_finally() {
} }
} }
try_finally(); try_finally();
%PrintTypeProfile(try_finally); expected = `{\"2034\":[\"string\"]}`;
check_collect_types(try_finally, expected);
// Fall-off return.
function fall_off() { function fall_off() {
//nothing //nothing
} }
fall_off(); fall_off();
%PrintTypeProfile(fall_off); expected = `{\"2188\":[\"undefined\"]}`;
check_collect_types(fall_off, expected);
testReturnOfNonVariable(); // Do not collect types when the function is never run.
function never_called() {}
expected = `{}`;
check_collect_types(never_called, expected);
function never_call() {}
%PrintTypeProfile(never_call);
function check_param(a, bbb, ccccccccc, dddddddddddddddd) { function several_params(a, b, c, d) {
//nothing //nothing
} }
check_param(2, 'foo', {}, new MyClass()); several_params(2, 'foo', {}, new MyClass());
%PrintTypeProfile(check_param); expected = `{\"2456\":[\"number\"],\"2459\":[\"string\"],\"2462\":[\"Object\"],\"2465\":[\"MyClass\"],\"2482\":[\"undefined\"]}`;
check_collect_types(several_params, expected);
throw "end";
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment