Skip to content

Commit f137eb7

Browse files
authored
Merge pull request #11 from JITx-Inc/jb/new-profiler
jb/new profiler
2 parents dbb29e2 + 4e3f05b commit f137eb7

35 files changed

+1392
-67
lines changed

ci/build-stanza-version.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77
# like 1.23.45
88
#
99
# Use version 0.17.56 to compile 0.18.0
10-
0.19.6
10+
0.19.5

compiler/codegen.stanza

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ public defstruct AsmStubs :
4949
stack-limit:Int
5050
registers:Int
5151
system-registers-space:Int
52+
profile-flag:Int
53+
profile-buffer:Int
54+
function-counters:Int
55+
function-info:Int
5256
heap-start:Int
5357
heap-top:Int
5458
heap-limit:Int
@@ -72,6 +76,7 @@ public defstruct AsmStubs :
7276
collect-garbage:Int
7377
saved-regs:Tuple<Int>
7478
saved-fregs:Tuple<Int>
79+
heap-statistics:Int
7580
class-table:Int
7681
global-root-table:Int
7782
stackmap-table:Int
@@ -105,6 +110,10 @@ public defn AsmStubs (backend:Backend) :
105110
next(id-counter) ;stack-limit:Int
106111
next(id-counter) ;registers:Int
107112
next(id-counter) ;system-registers-space:Int
113+
next(id-counter) ;profile-flag:Int
114+
next(id-counter) ;profile-buffer:Int
115+
next(id-counter) ;function-counters:Int
116+
next(id-counter) ;function-info:Int
108117
next(id-counter) ;heap-start:Int
109118
next(id-counter) ;heap-top:Int
110119
next(id-counter) ;heap-limit:Int
@@ -128,6 +137,7 @@ public defn AsmStubs (backend:Backend) :
128137
next(id-counter) ;collect-garbage:Int
129138
saved-regs ;saved-regs:Tuple<Int>
130139
saved-fregs ;saved-fregs:Tuple<Int>
140+
next(id-counter) ;heap-statistics:Int
131141
next(id-counter) ;class-table:Int
132142
next(id-counter) ;global-root-table:Int
133143
next(id-counter) ;stackmap-table:Int
@@ -204,6 +214,10 @@ public defn compile-runtime-stubs (emitter:CodeEmitter, stubs:AsmStubs) :
204214
comment("stack-limit = %_" % [stack-limit(stubs)])
205215
comment("registers = %_" % [registers(stubs)])
206216
comment("system-registers-space = %_" % [system-registers-space(stubs)])
217+
comment("profile-flag = %_" % [profile-flag(stubs)])
218+
comment("profile-buffer = %_" % [profile-buffer(stubs)])
219+
comment("function-counters = %_" % [function-counters(stubs)])
220+
comment("function-info = %_" % [function-info(stubs)])
207221
comment("heap-start = %_" % [heap-start(stubs)])
208222
comment("heap-top = %_" % [heap-top(stubs)])
209223
comment("heap-limit = %_" % [heap-limit(stubs)])
@@ -334,6 +348,12 @@ public defn compile-entry-function (emitter:CodeEmitter, stubs:AsmStubs) :
334348
#label(safepoint-table) ;safepoint-table:ptr<?>
335349
#label(debug-table) ;debug-table:ptr<?>
336350
#label(local-var-table) ;local-var-table:ptr<?>
351+
#label(heap-statistics) ;heap-statistics:ptr<?>
352+
#long() ;heap-dominator-tree:ptr<?>
353+
#label(profile-flag) ;profile-flag: ptr<long>
354+
#long() ;profile-buffer: ptr<long>
355+
#label(function-counters) ;function-counters: ptr<long>
356+
#label(function-info) ;function-info: ptr<FunctionInfoTable>
337357
#label(class-table) ;class-table:ptr<?>
338358
#label(global-root-table) ;global-root-table:ptr<GlobalRoots>
339359
#label(stackmap-table) ;stackmap-table:ptr<?>

compiler/compiler-build-settings.stanza

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ public defstruct BuildSettings :
2222
build-from-source?: True|False
2323
optimize?: True|False
2424
debug?: True|False
25+
profile?: True|False
26+
coverage?: True|False
2527
ccfiles: Tuple<String>
2628
ccflags: Tuple<String|Tuple<String>>
2729
flags: Tuple<Symbol>

compiler/compiler-main.stanza

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ public defn compile (proj-manager:ProjManager,
158158
backend:Backend,
159159
optimize?:True|False,
160160
debug?:True|False,
161+
profile?:True|False,
162+
coverage?:True|False,
161163
macro-plugins:Tuple<String>,
162164
force-build-macros?:True|False) -> CompilationResult :
163165
defn driver () :
@@ -224,7 +226,7 @@ public defn compile (proj-manager:ProjManager,
224226
`unoptimized-asm :
225227
val packages = for p in /packages(result) map :
226228
match(p) :
227-
(p:EPackage) : compile(lower-unoptimized(p), debug?)
229+
(p:EPackage) : compile(lower-unoptimized(p, profile?, coverage?), debug?)
228230
(p:StdPkg) : p
229231
compile-vmpackages(pkgsaver when write-out-pkgs?,
230232
packages,
@@ -244,7 +246,7 @@ public defn compile (proj-manager:ProjManager,
244246
val epackages = for p in packages map :
245247
match(p:FastPkg) : EPackage(packageio(p), exps(p))
246248
else : p as EPackage
247-
compile(lower-optimized(epackages), debug?)
249+
compile(lower-optimized(epackages, profile?, coverage?), debug?)
248250

249251
;- pkgsaver: False if we not need to write out any .pkg files to disk (either
250252
; to pkg dir or to pkg cache.).
@@ -309,7 +311,7 @@ public defn compile (proj-manager:ProjManager,
309311
for package in packages do :
310312
match(package) :
311313
(epackage:EPackage) :
312-
val vmpackage = compile(lower-unoptimized(epackage), debug?)
314+
val vmpackage = compile(lower-unoptimized(epackage, profile?, coverage?), debug?)
313315
val npkg = normalize(vmpackage, backend)
314316
val buffer = Vector<Ins>()
315317
emit-normalized-package(npkg, buffer-emitter(buffer, stubs), stubs)
@@ -399,3 +401,4 @@ defn collapse (p:NormVMPackage|StdPkg) :
399401
defmethod debug-table (this) : debug-table(vmp)
400402
defmethod safepoint-table (this) : safepoint-table(vmp)
401403
defmethod debug-name-table (this): debug-name-table(vmp)
404+
defmethod function-info (this) : function-info(vmp)

compiler/compiler.stanza

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ defn compute-build-settings (projenv:StandardProjEnv,
6969
val build-optimization = optimize?(settings) or optimize(s)
7070
;Compute build optimization level
7171
val build-debug = debug?(settings)
72+
;Compute build profile level
73+
val build-profile = profile?(settings) or profile(s)
74+
val build-coverage = coverage?(settings) or coverage(s)
7275
;Compute assembly
7376
val build-asm = string-or?(original-asm(settings), value?(assembly(s)))
7477
;Compute output
@@ -110,6 +113,8 @@ defn compute-build-settings (projenv:StandardProjEnv,
110113
build-from-source?(settings)
111114
build-optimization,
112115
build-debug,
116+
build-profile,
117+
build-coverage,
113118
build-ccfiles,
114119
build-ccflags,
115120
to-tuple(build-flags),
@@ -243,10 +248,12 @@ public defn compile (settings:BuildSettings, system:System) :
243248
println("Build target %~ is already up-to-date." % [build-target?(settings*)])
244249
else :
245250
setup-system-flags(settings*)
251+
add-flag(`HEAP-ANALYZER)
246252
val proj-params = ProjParams(compiler-flags(), optimize?(settings*), debug?(settings*), false, build-from-source?(settings), pkg-cache-dir(settings*))
247253
val proj-manager = ProjManager(proj, proj-params, auxfile)
248254
val comp-result = compile(proj-manager, auxfile, build-inputs!(settings*), vm-packages(settings*), asm?(settings*), pkg-dir(settings*),
249-
backend(platform(settings*) as Symbol), optimize?(settings*), debug?(settings*),
255+
backend(platform(settings*) as Symbol), optimize?(settings*), debug?(settings*),
256+
profile?(settings*), coverage?(settings*),
250257
macro-plugins(settings*), inputs(settings) is BuildTarget)
251258
save(auxfile)
252259
within delete-temporary-file-on-finish(settings*) :

compiler/defs-db.stanza

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ protected-when(TESTING) defn analyze-input (input:DefsDbInput) -> DependencyResu
9999
true, ;build-from-source?
100100
optimize?(input), ;optimize?
101101
false, ;debug?
102+
false, ;profile?
103+
false, ;coverage?
102104
[], ;ccfiles
103105
[], ;ccflags
104106
flags(input) ;flags

compiler/dl-ir.stanza

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1192,6 +1192,7 @@ public val CORE-COLLECT-STACK-TRACE-ID = register $ core-fnid(`collect-stack-tra
11921192
public val CORE-COLLECT-GARBAGE-ID = register $ core-fnid(`collect-garbage, [`long])
11931193
public val CORE-MAKE-STRING-ID = register $ core-fnid(`String, [DPtrT(DByte())])
11941194
public val CORE-EXECUTE-TOPLEVEL-COMMAND-ID = register $ core-fnid(`execute-toplevel-command, [DArrow([], core-type(`False))])
1195+
public val CORE-PROFILE-STACK-TRACE-ID = register $ core-fnid(`profile-stack-trace)
11951196

11961197
;============================================================
11971198
;====================== ID Environment ======================

compiler/el-infer.stanza

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ defn analyze-single-block (emit:Ins -> ?,
360360
(ins:ETupleSet|EStruct|EPtr|ELabel|ETCall|
361361
EDump|EConv|EGoto|ECheckLength|ECheckSet|
362362
EBoxSet|EReturn|EObjectTGet|EClosureTGet|
363-
ETDef|ETypeObject|EEnd|ELive|ECheckFail|ESafepoint) : gen-trivial-commands(ins)
363+
ETDef|ETypeObject|EEnd|ELive|ECheckFail|ESafepoint|EProfile) : gen-trivial-commands(ins)
364364

365365
;Utility: For any uses of inferred variables generate
366366
;them here.
@@ -551,7 +551,7 @@ defn analyze-single-block (emit:Ins -> ?,
551551
defn gen-trivial-commands (ins:ETupleSet|EStruct|EPtr|ELabel|ETCall|
552552
EDump|EConv|EGoto|ECheckLength|ECheckSet|
553553
EBoxSet|EReturn|EObjectTGet|EClosureTGet|
554-
ETDef|ETypeObject|EEnd|ELive|ECheckFail|ESafepoint) :
554+
ETDef|ETypeObject|EEnd|ELive|ECheckFail|ESafepoint|EProfile) :
555555
gen-uses(ins)
556556
emit(Wrapped<EIns>(ins))
557557
for x in varlocs(ins) do :

compiler/el-ir.stanza

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public defstruct EBot <: HiEType
4646
public defstruct EPackage :
4747
packageio: PackageIO with: (updater => sub-packageio)
4848
exps: Tuple<ETExp> with: (updater => sub-exps)
49+
function-info: False|Tuple<FunctionInfo> with: (default => false, updater => sub-function-info)
4950

5051
public defn name (e:EPackage) :
5152
package(packageio(e))
@@ -544,6 +545,10 @@ public defstruct DetupleContext <: CastErrorContext
544545
public defstruct GetVarContext <: CastErrorContext
545546
public defstruct BranchContext <: CastErrorContext
546547

548+
public defstruct EProfile <: EIns :
549+
id: Int
550+
kind: Int
551+
547552
;============================================================
548553
;======================= Primitives =========================
549554
;============================================================
@@ -761,6 +766,7 @@ defmethod print (o:OutputStream, e:EIns) :
761766
(e:ETypeof) : P $ "goto L%_ when typeof(%_, %_) else L%_" % [n1(e), y(e), type(e), n2(e)]
762767
(e:ECheckFail) : P $ "check-fail(%_) %_ : %_" % [ctxt(e), y(e), type(e)]
763768
(e:ESafepoint) : P $ "safepoint %_ ;%_" % [group-id(e), info?(e)]
769+
(e:EProfile) : P $ "profile(%_,%_)" % [id(e), kind(e)]
764770

765771
defmethod print (o:OutputStream, t:CallType) :
766772
print{o, _} $ match(t) :
@@ -1001,6 +1007,7 @@ defsyntax el-ir :
10011007
defrule eins = (?x:#vloc : ?xt:#etype = load ?loc:#eloc) : ELoad(x, xt, loc)
10021008
defrule eins = (?x:#vloc = call ?t:#calltype ?f:#eimm(?ys:#eimm ...)) : ECall(x, f, to-tuple(ys), t, add-base-dir(closest-info()))
10031009
defrule eins = (?x:#vloc = ?y:#eimm) : EDef(x, y)
1010+
defrule eins = (profile(?id:#int, ?arg:#int)) : EProfile(id, arg)
10041011

10051012
defproduction calltype : CallType
10061013
defrule calltype = (guarded((?a1:#etype ...) -> ?a2:#etype)) : CallGuarded(to-tuple(a1), a2)
@@ -1305,7 +1312,7 @@ defmethod map<?T> (f:ELItem -> ELItem, item:ELItem&?T) -> ELItem&T :
13051312
(t:EBot) : t
13061313

13071314
;EPackage
1308-
(e:EPackage) : EPackage(packageio(e), h(exps(e)))
1315+
(e:EPackage) : EPackage(packageio(e), h(exps(e)), function-info(e))
13091316

13101317
;ETExps
13111318
(e:EDefGlobal) : EDefGlobal(n(e), h(type(e)), mutable?(e), lostanza?(e))
@@ -1420,6 +1427,9 @@ defmethod map<?T> (f:ELItem -> ELItem, item:ELItem&?T) -> ELItem&T :
14201427
;Closures
14211428
(e:EClosure) : EClosure(n(e), h(targs(e)), h(ys(e)))
14221429

1430+
;Profile
1431+
(e:EProfile) : EProfile(id(e), kind(e))
1432+
14231433
;============================================================
14241434
;===================== Definitions ==========================
14251435
;============================================================
@@ -1478,6 +1488,7 @@ public defn varlocs (e:EIns) -> Tuple<EVarLoc> :
14781488
(e:ETypeof) : []
14791489
(e:ECheckFail) : []
14801490
(e:ESafepoint) : []
1491+
(e:EProfile) : []
14811492

14821493
;Return the list of immediates that the instruction
14831494
;uses to perform its action.
@@ -1532,6 +1543,7 @@ public defn uses (e:EIns) -> Seqable<EImm> :
15321543
(e:ETypeof) : [y(e)]
15331544
(e:ECheckFail) : [y(e)]
15341545
(e:ESafepoint) : []
1546+
(e:EProfile) : []
15351547

15361548
;Return the list of immediates that the instruction
15371549
;uses to retrieve its location.

compiler/el-to-vm.stanza

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public defn compile (epackage:EPackage, debug?:True|False) -> VMPackage :
6161
val init = compile-init(init-compiler, iotable, progbuffer)
6262
init
6363

64-
val vmp = to-vmpackage(progbuffer, io, init)
64+
val vmp = to-vmpackage(progbuffer, io, init, function-info(epackage))
6565
;dump(vmp, "logs", "pre-analyze")
6666
val vmp* = within time-ms!("VM Analyze") : analyze(vmp)
6767
;dump(vmp*, "logs", false)
@@ -565,7 +565,7 @@ defmulti add (b:ProgBuffer, e:VMMethod) -> False
565565
defmulti add (b:ProgBuffer, e:VMDebugInfo) -> False
566566
defmulti add (b:ProgBuffer, e:SafepointEntry) -> SafepointIns
567567
defmulti add (b:ProgBuffer, e:VMDebugName) -> False
568-
defmulti to-vmpackage (b:ProgBuffer, io:PackageIO, init:Int|False) -> VMPackage
568+
defmulti to-vmpackage (b:ProgBuffer, io:PackageIO, init:Int|False, function-info:False|Tuple<FunctionInfo>) -> VMPackage
569569

570570
defn ProgBuffer () :
571571
val globals = Vector<VMGlobal>()
@@ -591,7 +591,7 @@ defn ProgBuffer () :
591591
defmethod add (this, e:VMDebugInfo) : add(debug, e)
592592
defmethod add (this, e:SafepointEntry) : add(sptable, e)
593593
defmethod add (this, e:VMDebugName) : add(debugnames, e)
594-
defmethod to-vmpackage (this, io:PackageIO, init:Int|False) :
594+
defmethod to-vmpackage (this, io:PackageIO, init:Int|False, function-info:False|Tuple<FunctionInfo>) :
595595
VMPackage(io, init,
596596
to-tuple(globals),
597597
to-tuple(datas),
@@ -603,6 +603,7 @@ defn ProgBuffer () :
603603
to-tuple(extern-defns),
604604
VMDebugNameTable(to-tuple(debugnames)),
605605
VMDebugInfoTable(to-tuple(debug)),
606+
function-info
606607
compile(sptable))
607608

608609
;============================================================
@@ -1544,6 +1545,8 @@ defn Compiler (gt:GlobalTable,
15441545
(info:StackTraceInfo) :
15451546
make-safepoint(info) when /info(info) is-not False
15461547
(f:False) : false
1548+
(ins:EProfile) :
1549+
emit(ProfileIns(id(ins), kind(ins)))
15471550

15481551
;---------------------------------------------------------
15491552
;--------------- Create the Compiler Type ----------------

0 commit comments

Comments
 (0)