| 1 |
// |jit-test| skip-if: !isAsmJSCompilationAvailable() || (!getBuildConfiguration("arm-simulator") && !getBuildConfiguration("arm64-simulator")) |
| 2 |
// Single-step profiling currently only works in the ARM simulator |
| 3 |
|
| 4 |
load(libdir + "asm.js"); |
| 5 |
load(libdir + "asserts.js"); |
| 6 |
|
| 7 |
function checkSubSequence(got, expect) |
| 8 |
{ |
| 9 |
var got_i = 0; |
| 10 |
EXP: for (var exp_i = 0; exp_i < expect.length; exp_i++) { |
| 11 |
var item = expect[exp_i]; |
| 12 |
// Scan for next match in got. |
| 13 |
while (got_i < got.length) { |
| 14 |
if (got[got_i++] == expect[exp_i]) |
| 15 |
continue EXP; |
| 16 |
} |
| 17 |
print("MISMATCH: " + got.join(",") + "\n" + |
| 18 |
" VS " + expect.join(",")); |
| 19 |
return false; |
| 20 |
} |
| 21 |
return true; |
| 22 |
} |
| 23 |
|
| 24 |
function assertStackContainsSeq(got, expect) |
| 25 |
{ |
| 26 |
var normalized = []; |
| 27 |
|
| 28 |
for (var i = 0; i < got.length; i++) { |
| 29 |
if (got[i].length == 0) |
| 30 |
continue; |
| 31 |
var parts = got[i].split(','); |
| 32 |
for (var j = 0; j < parts.length; j++) { |
| 33 |
var frame = parts[j]; |
| 34 |
frame = frame.replace(/ \([^\)]*\)/g, ""); |
| 35 |
frame = frame.replace(/fast exit trampoline to native/g, "N"); |
| 36 |
frame = frame.replace(/^call to( asm.js)? native .*\(in wasm\)$/g, "N"); |
| 37 |
frame = frame.replace(/(fast|slow) exit trampoline/g, "<"); |
| 38 |
frame = frame.replace(/(fast|slow) entry trampoline/g, ">"); |
| 39 |
frame = frame.replace(/(\/[^\/,<]+)*\/testProfiling.js/g, ""); |
| 40 |
frame = frame.replace(/testBuiltinD2D/g, ""); |
| 41 |
frame = frame.replace(/testBuiltinF2F/g, ""); |
| 42 |
frame = frame.replace(/testBuiltinDD2D/g, ""); |
| 43 |
frame = frame.replace(/assertThrowsInstanceOf/g, ""); |
| 44 |
frame = frame.replace(/^ffi[12]?/g, ""); |
| 45 |
normalized.push(frame); |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
var gotNorm = normalized.join(',').replace(/,+/g, ","); |
| 50 |
gotNorm = gotNorm.replace(/^,/, "").replace(/,$/, ""); |
| 51 |
|
| 52 |
assertEq(checkSubSequence(gotNorm.split(','), expect.split(',')), true); |
| 53 |
} |
| 54 |
|
| 55 |
// Test profiling enablement while asm.js is running. |
| 56 |
var stacks; |
| 57 |
var ffi = function(enable) { |
| 58 |
if (enable == +1) |
| 59 |
enableGeckoProfiling(); |
| 60 |
enableSingleStepProfiling(); |
| 61 |
stacks = disableSingleStepProfiling(); |
| 62 |
if (enable == -1) |
| 63 |
disableGeckoProfiling(); |
| 64 |
} |
| 65 |
var f = asmLink(asmCompile('global','ffis',USE_ASM + "var ffi=ffis.ffi; function g(i) { i=i|0; ffi(i|0) } function f(i) { i=i|0; g(i|0) } return f"), null, {ffi}); |
| 66 |
f(0); |
| 67 |
assertStackContainsSeq(stacks, ""); |
| 68 |
f(+1); |
| 69 |
assertStackContainsSeq(stacks, "<,g,f,>"); |
| 70 |
f(0); |
| 71 |
assertStackContainsSeq(stacks, "<,g,f,>"); |
| 72 |
f(-1); |
| 73 |
assertStackContainsSeq(stacks, "<,g,f,>"); |
| 74 |
f(0); |
| 75 |
assertStackContainsSeq(stacks, ""); |
| 76 |
|
| 77 |
// Enable profiling for the rest of the tests. |
| 78 |
enableGeckoProfiling(); |
| 79 |
|
| 80 |
var f = asmLink(asmCompile(USE_ASM + "function f() { return 42 } return f")); |
| 81 |
enableSingleStepProfiling(); |
| 82 |
assertEq(f(), 42); |
| 83 |
var stacks = disableSingleStepProfiling(); |
| 84 |
assertStackContainsSeq(stacks, ">,f,>,>"); |
| 85 |
|
| 86 |
var m = asmCompile(USE_ASM + "function g(i) { i=i|0; return (i+1)|0 } function f() { return g(42)|0 } return f"); |
| 87 |
for (var i = 0; i < 3; i++) { |
| 88 |
var f = asmLink(m); |
| 89 |
enableSingleStepProfiling(); |
| 90 |
assertEq(f(), 43); |
| 91 |
var stacks = disableSingleStepProfiling(); |
| 92 |
assertStackContainsSeq(stacks, ">,f,>,g,f,>,f,>,>"); |
| 93 |
} |
| 94 |
|
| 95 |
var m = asmCompile(USE_ASM + "function g1() { return 1 } function g2() { return 2 } function f(i) { i=i|0; return TBL[i&1]()|0 } var TBL=[g1,g2]; return f"); |
| 96 |
for (var i = 0; i < 3; i++) { |
| 97 |
var f = asmLink(m); |
| 98 |
enableSingleStepProfiling(); |
| 99 |
assertEq(f(0), 1); |
| 100 |
assertEq(f(1), 2); |
| 101 |
var stacks = disableSingleStepProfiling(); |
| 102 |
assertStackContainsSeq(stacks, ">,f,>,g1,f,>,f,>,>,>,f,>,g2,f,>,f,>,>"); |
| 103 |
} |
| 104 |
|
| 105 |
function testBuiltinD2D(name) { |
| 106 |
var m = asmCompile('g', USE_ASM + "var fun=g.Math." + name + "; function f(d) { d=+d; return +fun(d) } return f"); |
| 107 |
for (var i = 0; i < 3; i++) { |
| 108 |
var f = asmLink(m, this); |
| 109 |
enableSingleStepProfiling(); |
| 110 |
assertEq(f(.1), eval("Math." + name + "(.1)")); |
| 111 |
var stacks = disableSingleStepProfiling(); |
| 112 |
assertStackContainsSeq(stacks, ">,f,>,N,f,>,f,>,>"); |
| 113 |
} |
| 114 |
} |
| 115 |
for (name of ['sin', 'cos', 'tan', 'asin', 'acos', 'atan', 'ceil', 'floor', 'exp', 'log']) |
| 116 |
testBuiltinD2D(name); |
| 117 |
|
| 118 |
function testBuiltinF2F(name) { |
| 119 |
var m = asmCompile('g', USE_ASM + "var tof=g.Math.fround; var fun=g.Math." + name + "; function f(d) { d=tof(d); return tof(fun(d)) } return f"); |
| 120 |
for (var i = 0; i < 3; i++) { |
| 121 |
var f = asmLink(m, this); |
| 122 |
enableSingleStepProfiling(); |
| 123 |
assertEq(f(.1), eval("Math.fround(Math." + name + "(Math.fround(.1)))")); |
| 124 |
var stacks = disableSingleStepProfiling(); |
| 125 |
assertStackContainsSeq(stacks, ">,f,>,N,f,>,f,>,>"); |
| 126 |
} |
| 127 |
} |
| 128 |
for (name of ['ceil', 'floor']) |
| 129 |
testBuiltinF2F(name); |
| 130 |
|
| 131 |
function testBuiltinDD2D(name) { |
| 132 |
var m = asmCompile('g', USE_ASM + "var fun=g.Math." + name + "; function f(d, e) { d=+d; e=+e; return +fun(d,e) } return f"); |
| 133 |
for (var i = 0; i < 3; i++) { |
| 134 |
var f = asmLink(m, this); |
| 135 |
enableSingleStepProfiling(); |
| 136 |
assertEq(f(.1, .2), eval("Math." + name + "(.1, .2)")); |
| 137 |
var stacks = disableSingleStepProfiling(); |
| 138 |
assertStackContainsSeq(stacks, ">,f,>,N,f,>,f,>,>"); |
| 139 |
} |
| 140 |
} |
| 141 |
for (name of ['atan2', 'pow']) |
| 142 |
testBuiltinDD2D(name); |
| 143 |
|
| 144 |
// FFI tests: |
| 145 |
setJitCompilerOption("ion.warmup.trigger", 10); |
| 146 |
setJitCompilerOption("baseline.warmup.trigger", 0); |
| 147 |
setJitCompilerOption("offthread-compilation.enable", 0); |
| 148 |
|
| 149 |
var m = asmCompile('g','ffis', USE_ASM + "var ffi1=ffis.ffi1, ffi2=ffis.ffi2; function f() { return ((ffi1()|0) + (ffi2()|0))|0 } return f"); |
| 150 |
|
| 151 |
var ffi1 = function() { return 10 } |
| 152 |
var ffi2 = function() { return 73 } |
| 153 |
var f = asmLink(m, null, {ffi1,ffi2}); |
| 154 |
|
| 155 |
// Interp FFI exit |
| 156 |
enableSingleStepProfiling(); |
| 157 |
assertEq(f(), 83); |
| 158 |
var stacks = disableSingleStepProfiling(); |
| 159 |
assertStackContainsSeq(stacks, ">,f,>,<,f,>,f,>,<,f,>,f,>,>"); |
| 160 |
|
| 161 |
// Ion FFI exit |
| 162 |
for (var i = 0; i < 20; i++) |
| 163 |
assertEq(f(), 83); |
| 164 |
enableSingleStepProfiling(); |
| 165 |
assertEq(f(), 83); |
| 166 |
var stacks = disableSingleStepProfiling(); |
| 167 |
assertStackContainsSeq(stacks, ">,f,>,<,f,>,f,>,<,f,>,f,>,>"); |
| 168 |
|
| 169 |
var ffi1 = function() { return { valueOf() { return 20 } } } |
| 170 |
var ffi2 = function() { return { valueOf() { return 74 } } } |
| 171 |
var f = asmLink(m, null, {ffi1,ffi2}); |
| 172 |
|
| 173 |
// Interp FFI exit |
| 174 |
enableSingleStepProfiling(); |
| 175 |
assertEq(f(), 94); |
| 176 |
var stacks = disableSingleStepProfiling(); |
| 177 |
assertStackContainsSeq(stacks, ">,f,>,<,f,>,f,>,<,f,>,f,>,>"); // TODO: add 'valueOf' once interp shows up |
| 178 |
|
| 179 |
// Ion FFI exit |
| 180 |
for (var i = 0; i < 20; i++) |
| 181 |
assertEq(f(), 94); |
| 182 |
enableSingleStepProfiling(); |
| 183 |
assertEq(f(), 94); |
| 184 |
var stacks = disableSingleStepProfiling(); |
| 185 |
assertStackContainsSeq(stacks, ">,f,>,<,f,>,f,>,<,f,>,f,>,>"); // TODO: add 'valueOf' once interp shows up |
| 186 |
|
| 187 |
var ffi1 = function() { return 15 } |
| 188 |
var ffi2 = function() { return f2() + 17 } |
| 189 |
var {f1,f2} = asmLink(asmCompile('g','ffis', USE_ASM + "var ffi1=ffis.ffi1, ffi2=ffis.ffi2; function f2() { return ffi1()|0 } function f1() { return ffi2()|0 } return {f1:f1, f2:f2}"), null, {ffi1, ffi2}); |
| 190 |
// Interpreter FFI exit |
| 191 |
enableSingleStepProfiling(); |
| 192 |
assertEq(f1(), 32); |
| 193 |
var stacks = disableSingleStepProfiling(); |
| 194 |
assertStackContainsSeq(stacks, ">,f1,>,<,f1,>,>,<,f1,>,f2,>,<,f1,>,<,f2,>,<,f1,>,f2,>,<,f1,>,>,<,f1,>,<,f1,>,f1,>,>"); |
| 195 |
|
| 196 |
|
| 197 |
// Ion FFI exit |
| 198 |
var jitOptions = getJitCompilerOptions(); |
| 199 |
if (jitOptions['baseline.enable']) { |
| 200 |
for (var i = 0; i < 20; i++) |
| 201 |
assertEq(f1(), 32); |
| 202 |
enableSingleStepProfiling(); |
| 203 |
assertEq(f1(), 32); |
| 204 |
var stacks = disableSingleStepProfiling(); |
| 205 |
assertStackContainsSeq(stacks, ">,f1,>,<,f1,>,>,<,f1,>,f2,>,<,f1,>,<,f2,>,<,f1,>,f2,>,<,f1,>,>,<,f1,>,<,f1,>,f1,>,>"); |
| 206 |
} |
| 207 |
|
| 208 |
// Thunks |
| 209 |
setJitCompilerOption("jump-threshold", 0); |
| 210 |
var h = asmLink(asmCompile(USE_ASM + 'function f() {} function g() { f() } function h() { g() } return h')); |
| 211 |
enableSingleStepProfiling(); |
| 212 |
h(); |
| 213 |
var stacks = disableSingleStepProfiling(); |
| 214 |
assertStackContainsSeq(stacks, ">,h,>,g,h,>,f,g,h,>,g,h,>,h,>,>"); |
| 215 |
setJitCompilerOption("jump-threshold", -1); |
| 216 |
|
| 217 |
// This takes forever to run. |
| 218 |
// Stack-overflow exit test |
| 219 |
//var limit = -1; |
| 220 |
//var maxct = 0; |
| 221 |
//function ffi(ct) { if (ct == limit) { enableSingleStepProfiling(); print("enabled"); } maxct = ct; } |
| 222 |
//var f = asmLink(asmCompile('g', 'ffis',USE_ASM + "var ffi=ffis.ffi; var ct=0; function rec(){ ct=(ct+1)|0; ffi(ct|0); rec() } function f() { ct=0; rec() } return f"), null, {ffi}); |
| 223 |
//// First find the stack limit: |
| 224 |
//var caught = false; |
| 225 |
//try { f() } catch (e) { caught = true; assertEq(String(e).indexOf("too much recursion") >= 0, true) } |
| 226 |
//assertEq(caught, true); |
| 227 |
//limit = maxct; |
| 228 |
//print("Setting limit"); |
| 229 |
//var caught = false; |
| 230 |
//try { f() } catch (e) { caught = true; print("caught"); assertEq(String(e).indexOf("too much recursion") >= 0, true) } |
| 231 |
//var stacks = disableSingleStepProfiling(); |
| 232 |
//assertEq(String(stacks).indexOf("rec") >= 0, true); |