Skip to content
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
aef14d2
[JSC] The control flow profiler's basic blocks and function ranges ma…
dylan-conway Oct 17, 2026
dd69957
Give a basic block that is part of an expression its own end instead …
dylan-conway Oct 17, 2026
88185ad
Test each control flow profiler change on its own and leave the funct…
dylan-conway Oct 17, 2026
eede189
Make a part of an expression a gap in the basic blocks around it, kee…
dylan-conway Oct 17, 2026
6caea36
Declare the basic block of a static field when the class is evaluated…
dylan-conway Oct 17, 2026
f6ba728
Look up the basic block of the computed field, not the one around it,…
dylan-conway Oct 17, 2026
c2c53d4
Keep the parse mode of a function range in FunctionHasExecutedCache, …
dylan-conway Oct 17, 2026
368280a
Give FunctionHasExecutedCache the parse mode of every range, not only…
dylan-conway Oct 17, 2026
5426125
Give the empty finally of a try with a return in a derived constructo…
dylan-conway Oct 17, 2026
e0cf9ae
Have the parse mode in the key of FunctionHasExecutedCache, so that a…
dylan-conway Oct 17, 2026
bd30236
Say which ranges have a basic block before they run where it is decid…
dylan-conway Oct 17, 2026
3b1ea22
Nest the basic blocks of statements as the source does, with nothing …
dylan-conway Oct 18, 2026
2e56a0b
Give the block the parser has around a function declaration in an if …
dylan-conway Oct 18, 2026
f59bbe0
Test what is after each kind of loop, a label, a finally and an else …
dylan-conway Oct 18, 2026
5302573
Test what is between the last clause of a switch and its brace, after…
dylan-conway Oct 18, 2026
984d4a2
Have what is after a statement go up to the end of its list, with tha…
dylan-conway Oct 18, 2026
0d1cda9
Have the end offset of a block be after its brace, as that of any sta…
dylan-conway Oct 18, 2026
2607907
Have a do while statement start at its do, and test the end offset of…
dylan-conway Oct 18, 2026
f00d5af
Find the gaps of a basic block in one sorted list of basic blocks and…
dylan-conway Oct 18, 2026
bd957c3
Have the basic block of a program with no token start at its end, as …
dylan-conway Oct 18, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions JSTests/controlFlowProfiler/arrowfunction-expression-body.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
var basicBlockExecutionCount = $vm.basicBlockExecutionCount;

load("./driver/driver.js");

function foo() { return 1; }

// An expression that is the body of an arrow function is the end of it.
function testLastCharacter() {
let f = () => foo();
f();
f();
}
testLastCharacter();
assert(basicBlockExecutionCount(testLastCharacter, "foo()") === 2, "the body runs per call");
assert(basicBlockExecutionCount(testLastCharacter, ");\n f();") === 2, "and so does its last character");
50 changes: 50 additions & 0 deletions JSTests/controlFlowProfiler/break-continue.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
var basicBlockExecutionCount = $vm.basicBlockExecutionCount;

load("./driver/driver.js");

function foo() { }

function testBreakNotTaken(n) {
for (let i = 0; i < n; i++) {
if (i > n)
break;
foo();
}
}
testBreakNotTaken(3);
checkBasicBlock(testBreakNotTaken, "break", ShouldNotHaveExecuted);
assert(basicBlockExecutionCount(testBreakNotTaken, "foo()") === 3, "what follows the if runs each time");

function testBreakTaken(n) {
for (let i = 0; i < n; i++) {
if (i === 1)
break;
foo();
}
}
testBreakTaken(3);
assert(basicBlockExecutionCount(testBreakTaken, "break") === 1, "break runs once");
assert(basicBlockExecutionCount(testBreakTaken, "foo()") === 1, "what follows the if runs until then");

function testContinue(n) {
for (let i = 0; i < n; i++) {
if (i % 2)
continue;
foo();
}
}
testContinue(5);
assert(basicBlockExecutionCount(testContinue, "continue") === 2, "continue runs for the odd ones");
assert(basicBlockExecutionCount(testContinue, "foo()") === 3, "what follows the if runs for the even ones");

function testContinueNotTaken(n) {
for (let i = 0; i < n; i++) {
if (i > n) {
continue;
}
foo();
}
}
testContinueNotTaken(2);
checkBasicBlock(testContinueNotTaken, "continue", ShouldNotHaveExecuted);
checkBasicBlock(testContinueNotTaken, "foo()", ShouldHaveExecuted);
175 changes: 175 additions & 0 deletions JSTests/controlFlowProfiler/class-fields.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
var basicBlockExecutionCount = $vm.basicBlockExecutionCount;

load("./driver/driver.js");

function foo() { return 1; }
function bar() { return 2; }
function key() { return "k"; }

function testNeverConstructed() {
class A {
a = foo();
static b = bar();
}
return A;
}
testNeverConstructed();
checkBasicBlock(testNeverConstructed, "class A", ShouldHaveExecuted);
checkBasicBlock(testNeverConstructed, "a = foo", ShouldNotHaveExecuted);
checkBasicBlock(testNeverConstructed, "foo()", ShouldNotHaveExecuted);
checkBasicBlock(testNeverConstructed, "static b", ShouldHaveExecuted);
checkBasicBlock(testNeverConstructed, "bar()", ShouldHaveExecuted);
checkBasicBlock(testNeverConstructed, "return A", ShouldHaveExecuted);

function testCounts(times) {
class A {
a = foo();
[key()] = bar();
static c = foo();
}
for (let i = 0; i < times; i++)
new A;
}
testCounts(3);
assert(basicBlockExecutionCount(testCounts, "class A") === 1, "the class is evaluated once");
assert(basicBlockExecutionCount(testCounts, "a = foo") === 3, "a field is initialized once per instance");
assert(basicBlockExecutionCount(testCounts, "key()") === 1, "a computed key is evaluated once");
assert(basicBlockExecutionCount(testCounts, "bar()") === 3, "the value of a computed field is evaluated once per instance");
assert(basicBlockExecutionCount(testCounts, "static c") === 1, "a static field is initialized once");
assert(basicBlockExecutionCount(testCounts, "for (") === 1, "what follows the class runs once");

// All of it is one basic block, whose range is the one the function that initializes the fields has.
function testScopeOfOneBlock() {
class A {
a = foo();
}
new A;
new A;
new A;
}
testScopeOfOneBlock();
testScopeOfOneBlock();
assert(basicBlockExecutionCount(testScopeOfOneBlock, "class A") === 2, "the class is evaluated once per call");
assert(basicBlockExecutionCount(testScopeOfOneBlock, "a = foo") === 6, "a field is initialized once per instance");
assert(basicBlockExecutionCount(testScopeOfOneBlock, "new A") === 2, "what follows the class runs once per call");

function testParenthesizedField() {
class A {
a = (foo());
}
new A;
}
testParenthesizedField();
checkBasicBlock(testParenthesizedField, "foo()", ShouldHaveExecuted);
checkBasicBlock(testParenthesizedField, ");", ShouldHaveExecuted);

function testFunctionInComputedField() {
class A {
[key()] = () => foo();
}
new A;
let a = new A;
a.k();
a.k();
a.k();
}
testFunctionInComputedField();
assert(basicBlockExecutionCount(testFunctionInComputedField, "foo()") === 3, "the function runs as often as it is called");
assert(basicBlockExecutionCount(testFunctionInComputedField, " () =>") === 2, "the field is initialized once per instance");
assert(basicBlockExecutionCount(testFunctionInComputedField, "key()") === 1, "the key is evaluated once");

function testBranchInField(x) {
class A {
a = x ? foo() : bar();
}
return new A;
}
testBranchInField(true);
checkBasicBlock(testBranchInField, "foo()", ShouldHaveExecuted);
checkBasicBlock(testBranchInField, "bar()", ShouldNotHaveExecuted);
checkBasicBlock(testBranchInField, "return new A", ShouldHaveExecuted);

function testStaticBlock(x) {
class A {
static {
if (x)
foo();
else
bar();
}
}
return A;
}
testStaticBlock(true);
checkBasicBlock(testStaticBlock, "static {", ShouldHaveExecuted);
checkBasicBlock(testStaticBlock, "if (x)", ShouldHaveExecuted);
checkBasicBlock(testStaticBlock, "foo()", ShouldHaveExecuted);
checkBasicBlock(testStaticBlock, "bar()", ShouldNotHaveExecuted);
checkBasicBlock(testStaticBlock, "return A", ShouldHaveExecuted);

// What follows a field in the file is not the field, however far it is.
function testStaticElementsAfterThrow() {
try {
class A {
static a = (() => { throw new Error; })();
static b = bar();
static {
foo();
}
}
} catch { }
}
testStaticElementsAfterThrow();
checkBasicBlock(testStaticElementsAfterThrow, "throw new", ShouldHaveExecuted);
checkBasicBlock(testStaticElementsAfterThrow, "bar()", ShouldNotHaveExecuted);
checkBasicBlock(testStaticElementsAfterThrow, "foo()", ShouldNotHaveExecuted);

function testFarFromField() {
class A {
a = foo();
}
new A;
new A;
function never() {
// ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
bar();
}
return A;
}
testFarFromField();
checkBasicBlock(testFarFromField, "bar()", ShouldNotHaveExecuted);
assert(basicBlockExecutionCount(testFarFromField, "return A") === 1, "what follows the class runs once");

function testFieldAfterAccessor() {
class A {
get b() { return 1; }
a = foo();
}
return A;
}
testFieldAfterAccessor();
checkBasicBlock(testFieldAfterAccessor, "foo()", ShouldNotHaveExecuted);
checkBasicBlock(testFieldAfterAccessor, "return A", ShouldHaveExecuted);

function testSkippedClass(x) {
x ??= class { a = foo(); };
bar();
}
testSkippedClass(1);
checkBasicBlock(testSkippedClass, "bar()", ShouldHaveExecuted);

function testStaticFieldOfSkippedClass(x) {
x ??= class { static a = foo(); };
bar();
}
testStaticFieldOfSkippedClass(1);
checkBasicBlock(testStaticFieldOfSkippedClass, "foo()", ShouldNotHaveExecuted);
checkBasicBlock(testStaticFieldOfSkippedClass, "bar()", ShouldHaveExecuted);

function testStaticFieldAfterAccessorOfSkippedClass(x) {
x ??= class { get a() { return 1; } static b = foo(); };
bar();
}
testStaticFieldAfterAccessorOfSkippedClass(1);
checkBasicBlock(testStaticFieldAfterAccessorOfSkippedClass, "foo()", ShouldNotHaveExecuted);
checkBasicBlock(testStaticFieldAfterAccessorOfSkippedClass, "bar()", ShouldHaveExecuted);
13 changes: 13 additions & 0 deletions JSTests/controlFlowProfiler/default-constructor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function f(){g();g();g();g();g();g();g();g();}
function g() { }

load("./driver/driver.js");

// Its constructor is in a text of the engine's own, and the offsets of that text are not offsets into this file.
class NeverConstructed { }

checkBasicBlock(f, "g()", ShouldNotHaveExecuted);
checkBasicBlock(f, "g();}", ShouldNotHaveExecuted);
f();
checkBasicBlock(f, "g()", ShouldHaveExecuted);
Comment thread
dylan-conway marked this conversation as resolved.
Resolved
checkBasicBlock(f, "g();}", ShouldHaveExecuted);
105 changes: 105 additions & 0 deletions JSTests/controlFlowProfiler/do-while-and-labels.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
var basicBlockExecutionCount = $vm.basicBlockExecutionCount;

load("./driver/driver.js");

function foo() { }
function bar() { }

function testDoWhile(n) {
let i = 0;
do {
foo();
i++;
} while (i < n);
bar();
}
testDoWhile(3);
assert(basicBlockExecutionCount(testDoWhile, "let i") === 1, "what precedes the loop runs once");
assert(basicBlockExecutionCount(testDoWhile, "foo()") === 3, "the body runs each time");
assert(basicBlockExecutionCount(testDoWhile, "i < n") === 3, "the condition runs each time");
assert(basicBlockExecutionCount(testDoWhile, "bar()") === 1, "what follows the loop runs once");

function testDoWhileInIf(x, n) {
let i = 0;
if (x)
do {
foo();
i++;
} while (i < n);
bar();
}
testDoWhileInIf(true, 3);
testDoWhileInIf(false, 3);
assert(basicBlockExecutionCount(testDoWhileInIf, "do {") === 1, "the loop starts when the if runs it");
assert(basicBlockExecutionCount(testDoWhileInIf, "foo()") === 3, "the body runs each time");
assert(basicBlockExecutionCount(testDoWhileInIf, "i < n") === 3, "the condition runs each time");
assert(basicBlockExecutionCount(testDoWhileInIf, "bar()") === 2, "what follows the if runs each time");

function testDoWhileInLoop() {
for (let i = 0; i < 2; i++)
do
foo();
while (false);
}
testDoWhileInLoop();
assert(basicBlockExecutionCount(testDoWhileInLoop, "do") === 2, "the inner loop starts each time");
assert(basicBlockExecutionCount(testDoWhileInLoop, "false") === 2, "the condition runs each time");

function testDoWhileBreak() {
do {
foo();
if (true)
break;
bar();
} while (true);
foo();
}
testDoWhileBreak();
checkBasicBlock(testDoWhileBreak, "bar()", ShouldNotHaveExecuted);
checkBasicBlock(testDoWhileBreak, "while (true)", ShouldNotHaveExecuted);
checkBasicBlock(testDoWhileBreak, "foo();\n}", ShouldHaveExecuted);

function testDoWhileContinue(x) {
do {
if (x)
continue;
foo();
} while (bar());
}
testDoWhileContinue(true);
checkBasicBlock(testDoWhileContinue, "foo()", ShouldNotHaveExecuted);
checkBasicBlock(testDoWhileContinue, "bar()", ShouldHaveExecuted);

function testLabeledBlock(x) {
label: {
if (x)
break label;
foo();
}
bar();
}
testLabeledBlock(true);
checkBasicBlock(testLabeledBlock, "break label", ShouldHaveExecuted);
checkBasicBlock(testLabeledBlock, "foo()", ShouldNotHaveExecuted);
checkBasicBlock(testLabeledBlock, "bar()", ShouldHaveExecuted);

function testNestedLabels(x) {
outer: inner: for (;;) {
if (x)
break outer;
foo();
break inner;
}
bar();
}
testNestedLabels(true);
checkBasicBlock(testNestedLabels, "break outer", ShouldHaveExecuted);
checkBasicBlock(testNestedLabels, "foo()", ShouldNotHaveExecuted);
checkBasicBlock(testNestedLabels, "bar()", ShouldHaveExecuted);

function testLabeledFunction() {
label: function labeled() { }
bar();
}
testLabeledFunction();
checkBasicBlock(testLabeledFunction, "bar()", ShouldHaveExecuted);
16 changes: 16 additions & 0 deletions JSTests/controlFlowProfiler/generator-body.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
load("./driver/driver.js");

function foo() { }

// Calling a generator does not run its body.
function* neverResumed() {
foo();
}
neverResumed();
checkBasicBlock(neverResumed, "foo()", ShouldNotHaveExecuted);

function* resumed() {
foo();
}
resumed().next();
checkBasicBlock(resumed, "foo()", ShouldHaveExecuted);
Comment thread
dylan-conway marked this conversation as resolved.
Resolved
Loading
Loading