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) {
namespace {
void PrintTypes(Handle<ArrayList> s) {
for (int i = 0; i < s->Length(); i++) {
Object* entry = s->Get(i);
if (entry->IsString()) {
PrintF("%s\n", String::cast(entry)->ToCString().get());
}
}
}
Handle<JSObject> ConvertToJSObject(Isolate* isolate,
Handle<UnseededNumberDictionary> feedback) {
Handle<JSObject> type_profile =
isolate->factory()->NewJSObject(isolate->object_function());
void SortTypes(Isolate* isolate,
Handle<UnseededNumberDictionary> unsorted_types,
std::map<int, Handle<ArrayList>>* types) {
for (int index = UnseededNumberDictionary::kElementsStartIndex;
index < unsorted_types->length();
index < feedback->length();
index += UnseededNumberDictionary::kEntrySize) {
int key_index = index + UnseededNumberDictionary::kEntryKeyIndex;
Object* key = unsorted_types->get(key_index);
Object* key = feedback->get(key_index);
if (key->IsSmi()) {
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
void CollectTypeProfileNexus::Print() const {
JSObject* CollectTypeProfileNexus::GetTypeProfile() const {
Isolate* isolate = GetIsolate();
Object* const feedback = GetFeedback();
if (feedback == *FeedbackVector::UninitializedSentinel(isolate)) {
return;
return *isolate->factory()->NewJSMap();
}
Handle<UnseededNumberDictionary> unsorted_types =
Handle<UnseededNumberDictionary>(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);
}
return *ConvertToJSObject(
isolate, Handle<UnseededNumberDictionary>(
UnseededNumberDictionary::cast(feedback), isolate));
}
} // namespace internal
......
......@@ -759,13 +759,9 @@ class CollectTypeProfileNexus : public FeedbackNexus {
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);
// Dump the types to stdout.
// TODO(franzih): pass this information to the debugger protocol instead of
// stdout.
void Print() const;
JSObject* GetTypeProfile() const;
InlineCacheState StateFromFeedback() const override;
};
......
......@@ -210,7 +210,7 @@ RUNTIME_FUNCTION(Runtime_IsConcurrentRecompilationSupported) {
isolate->concurrent_recompilation_enabled());
}
RUNTIME_FUNCTION(Runtime_PrintTypeProfile) {
RUNTIME_FUNCTION(Runtime_TypeProfile) {
HandleScope scope(isolate);
DCHECK_EQ(1, args.length());
......@@ -221,18 +221,13 @@ RUNTIME_FUNCTION(Runtime_PrintTypeProfile) {
CONVERT_ARG_HANDLE_CHECKED(JSFunction, function, 0);
if (function->has_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()) {
FeedbackSlot slot = vector->GetTypeProfileSlot();
CollectTypeProfileNexus nexus(vector, slot);
nexus.Print();
PrintF("\n");
return nexus.GetTypeProfile();
}
}
return isolate->heap()->undefined_value();
return *isolate->factory()->NewJSObject(isolate->object_function());
}
RUNTIME_FUNCTION(Runtime_OptimizeFunctionOnNextCall) {
......
......@@ -552,7 +552,7 @@ namespace internal {
F(RunningInSimulator, 0, 1) \
F(IsConcurrentRecompilationSupported, 0, 1) \
F(OptimizeFunctionOnNextCall, -1, 1) \
F(PrintTypeProfile, 1, 1) \
F(TypeProfile, 1, 1) \
F(OptimizeOsr, -1, 1) \
F(NeverOptimizeFunction, 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 @@
// 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) {
// We want to test 2 different return positions in one function.
if (flag) {
......@@ -18,13 +28,16 @@ class MyClass {
constructor() {}
}
%PrintTypeProfile(testFunction);
var expected = `{}`;
check_collect_types(testFunction, expected);
testFunction({});
testFunction(123, true);
testFunction('hello');
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('hello', true);
......@@ -32,11 +45,16 @@ testFunction({x: 12}, true);
testFunction({x: 12});
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() {
return 32;
}
testReturnOfNonVariable();
expected = `{\"1732\":[\"number\"]}`;
check_collect_types(testReturnOfNonVariable, expected);
// Return statement is reached but its expression is never really returned.
function try_finally() {
......@@ -47,24 +65,26 @@ function try_finally() {
}
}
try_finally();
%PrintTypeProfile(try_finally);
expected = `{\"2034\":[\"string\"]}`;
check_collect_types(try_finally, expected);
// Fall-off return.
function fall_off() {
//nothing
}
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
}
check_param(2, 'foo', {}, new MyClass());
%PrintTypeProfile(check_param);
throw "end";
several_params(2, 'foo', {}, new MyClass());
expected = `{\"2456\":[\"number\"],\"2459\":[\"string\"],\"2462\":[\"Object\"],\"2465\":[\"MyClass\"],\"2482\":[\"undefined\"]}`;
check_collect_types(several_params, expected);
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