Skip to content

Commit

Permalink
first try at cherry picking profiling/coverage changes
Browse files Browse the repository at this point in the history
  • Loading branch information
jackbackrack committed Jan 6, 2025
1 parent 3987fc0 commit 4b20a67
Show file tree
Hide file tree
Showing 18 changed files with 623 additions and 17 deletions.
12 changes: 12 additions & 0 deletions compiler/codegen.stanza
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ public defstruct AsmStubs :
stack-limit:Int
registers:Int
system-registers-space:Int
profile-flag:Int
profile-buffer:Int
function-counters:Int
function-info:Int
heap-start:Int
heap-top:Int
heap-limit:Int
Expand Down Expand Up @@ -105,6 +109,10 @@ public defn AsmStubs (backend:Backend) :
next(id-counter) ;stack-limit:Int
next(id-counter) ;registers:Int
next(id-counter) ;system-registers-space:Int
next(id-counter) ;profile-flag:Int
next(id-counter) ;profile-buffer:Int
next(id-counter) ;function-counters:Int
next(id-counter) ;function-info:Int
next(id-counter) ;heap-start:Int
next(id-counter) ;heap-top:Int
next(id-counter) ;heap-limit:Int
Expand Down Expand Up @@ -204,6 +212,10 @@ public defn compile-runtime-stubs (emitter:CodeEmitter, stubs:AsmStubs) :
comment("stack-limit = %_" % [stack-limit(stubs)])
comment("registers = %_" % [registers(stubs)])
comment("system-registers-space = %_" % [system-registers-space(stubs)])
comment("profile-flag = %_" % [profile-flag(stubs)])
comment("profile-buffer = %_" % [profile-buffer(stubs)])
comment("function-counters = %_" % [function-counters(stubs)])
comment("function-info = %_" % [function-info(stubs)])
comment("heap-start = %_" % [heap-start(stubs)])
comment("heap-top = %_" % [heap-top(stubs)])
comment("heap-limit = %_" % [heap-limit(stubs)])
Expand Down
2 changes: 2 additions & 0 deletions compiler/compiler-build-settings.stanza
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public defstruct BuildSettings :
build-from-source?: True|False
optimize?: True|False
debug?: True|False
profile?: True|False
coverage?: True|False
ccfiles: Tuple<String>
ccflags: Tuple<String|Tuple<String>>
flags: Tuple<Symbol>
Expand Down
8 changes: 5 additions & 3 deletions compiler/compiler-main.stanza
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ public defn compile (proj-manager:ProjManager,
backend:Backend,
optimize?:True|False,
debug?:True|False,
profile?:True|False,
coverage?:True|False,
macro-plugins:Tuple<String>,
force-build-macros?:True|False) -> CompilationResult :
defn driver () :
Expand Down Expand Up @@ -224,7 +226,7 @@ public defn compile (proj-manager:ProjManager,
`unoptimized-asm :
val packages = for p in /packages(result) map :
match(p) :
(p:EPackage) : compile(lower-unoptimized(p), debug?)
(p:EPackage) : compile(lower-unoptimized(p, profile?, coverage?), debug?)
(p:StdPkg) : p
compile-vmpackages(pkgsaver when write-out-pkgs?,
packages,
Expand All @@ -244,7 +246,7 @@ public defn compile (proj-manager:ProjManager,
val epackages = for p in packages map :
match(p:FastPkg) : EPackage(packageio(p), exps(p))
else : p as EPackage
compile(lower-optimized(epackages), debug?)
compile(lower-optimized(epackages, profile?, coverage?), debug?)

;- pkgsaver: False if we not need to write out any .pkg files to disk (either
; to pkg dir or to pkg cache.).
Expand Down Expand Up @@ -309,7 +311,7 @@ public defn compile (proj-manager:ProjManager,
for package in packages do :
match(package) :
(epackage:EPackage) :
val vmpackage = compile(lower-unoptimized(epackage), debug?)
val vmpackage = compile(lower-unoptimized(epackage, profile?, coverage?), debug?)
val npkg = normalize(vmpackage, backend)
val buffer = Vector<Ins>()
emit-normalized-package(npkg, buffer-emitter(buffer, stubs), stubs)
Expand Down
8 changes: 7 additions & 1 deletion compiler/compiler.stanza
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ defn compute-build-settings (projenv:StandardProjEnv,
val build-optimization = optimize?(settings) or optimize(s)
;Compute build optimization level
val build-debug = debug?(settings)
;Compute build profile level
val build-profile = profile?(settings) or profile(s)
val build-coverage = coverage?(settings) or coverage(s)
;Compute assembly
val build-asm = string-or?(original-asm(settings), value?(assembly(s)))
;Compute output
Expand Down Expand Up @@ -110,6 +113,8 @@ defn compute-build-settings (projenv:StandardProjEnv,
build-from-source?(settings)
build-optimization,
build-debug,
build-profile,
build-coverage,
build-ccfiles,
build-ccflags,
to-tuple(build-flags),
Expand Down Expand Up @@ -246,7 +251,8 @@ public defn compile (settings:BuildSettings, system:System) :
val proj-params = ProjParams(compiler-flags(), optimize?(settings*), debug?(settings*), false, build-from-source?(settings), pkg-cache-dir(settings*))
val proj-manager = ProjManager(proj, proj-params, auxfile)
val comp-result = compile(proj-manager, auxfile, build-inputs!(settings*), vm-packages(settings*), asm?(settings*), pkg-dir(settings*),
backend(platform(settings*) as Symbol), optimize?(settings*), debug?(settings*),
backend(platform(settings*) as Symbol), optimize?(settings*), debug?(settings*),
profile?(settings*), coverage?(settings*),
macro-plugins(settings*), inputs(settings) is BuildTarget)
save(auxfile)
within delete-temporary-file-on-finish(settings*) :
Expand Down
1 change: 1 addition & 0 deletions compiler/dl-ir.stanza
Original file line number Diff line number Diff line change
Expand Up @@ -1192,6 +1192,7 @@ public val CORE-COLLECT-STACK-TRACE-ID = register $ core-fnid(`collect-stack-tra
public val CORE-COLLECT-GARBAGE-ID = register $ core-fnid(`collect-garbage, [`long])
public val CORE-MAKE-STRING-ID = register $ core-fnid(`String, [DPtrT(DByte())])
public val CORE-EXECUTE-TOPLEVEL-COMMAND-ID = register $ core-fnid(`execute-toplevel-command, [DArrow([], core-type(`False))])
public val CORE-PROFILE-STACK-TRACE-ID = register $ core-fnid(`profile-stack-trace)

;============================================================
;====================== ID Environment ======================
Expand Down
11 changes: 11 additions & 0 deletions compiler/el-ir.stanza
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,10 @@ public defstruct DetupleContext <: CastErrorContext
public defstruct GetVarContext <: CastErrorContext
public defstruct BranchContext <: CastErrorContext

public defstruct EProfile <: EIns :
id: Int
kind: Int

;============================================================
;======================= Primitives =========================
;============================================================
Expand Down Expand Up @@ -761,6 +765,7 @@ defmethod print (o:OutputStream, e:EIns) :
(e:ETypeof) : P $ "goto L%_ when typeof(%_, %_) else L%_" % [n1(e), y(e), type(e), n2(e)]
(e:ECheckFail) : P $ "check-fail(%_) %_ : %_" % [ctxt(e), y(e), type(e)]
(e:ESafepoint) : P $ "safepoint %_ ;%_" % [group-id(e), info?(e)]
(e:EProfile) : P $ "profile(%_,%_)" % [id(e), kind(e)]

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

defproduction calltype : CallType
defrule calltype = (guarded((?a1:#etype ...) -> ?a2:#etype)) : CallGuarded(to-tuple(a1), a2)
Expand Down Expand Up @@ -1420,6 +1426,9 @@ defmethod map<?T> (f:ELItem -> ELItem, item:ELItem&?T) -> ELItem&T :
;Closures
(e:EClosure) : EClosure(n(e), h(targs(e)), h(ys(e)))

;Profile
(e:EProfile) : EProfile(id(e), kind(e))

;============================================================
;===================== Definitions ==========================
;============================================================
Expand Down Expand Up @@ -1478,6 +1487,7 @@ public defn varlocs (e:EIns) -> Tuple<EVarLoc> :
(e:ETypeof) : []
(e:ECheckFail) : []
(e:ESafepoint) : []
(e:EProfile) : []

;Return the list of immediates that the instruction
;uses to perform its action.
Expand Down Expand Up @@ -1532,6 +1542,7 @@ public defn uses (e:EIns) -> Seqable<EImm> :
(e:ETypeof) : [y(e)]
(e:ECheckFail) : [y(e)]
(e:ESafepoint) : []
(e:EProfile) : []

;Return the list of immediates that the instruction
;uses to retrieve its location.
Expand Down
68 changes: 63 additions & 5 deletions compiler/el.stanza
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,17 @@ defpackage stz/el :
;==================== Drivers ===============================
;============================================================

public defn lower-optimized (epackages:Tuple<EPackage>) -> EPackage :
public defn lower-optimized (epackages:Tuple<EPackage>, profile?:True|False, coverage?:True|False) -> EPackage :
vprintln("EL: Lower optimized packages: %," % [seq(name, epackages)])
val epackages* = map(fill-stack-trace-entries, epackages)
;do(dump{_, "logs", "input"}, epackages*)
val collapsed = collapse(epackages*)
;dump(collapsed, "logs", "collapsed")
lower(collapsed, true)
lower(collapsed, true, profile?, coverage?)

public defn lower-unoptimized (epackage:EPackage) -> EPackage :
public defn lower-unoptimized (epackage:EPackage, profile?:True|False, coverage?:True|False) -> EPackage :
vprintln("EL: Lower unoptimized package: %_" % [name(epackage)])
lower(fill-stack-trace-entries(epackage), false)
lower(fill-stack-trace-entries(epackage), false, profile?, coverage?)

;============================================================
;===================== Timers ===============================
Expand All @@ -56,7 +56,7 @@ val EL-TIMERS = within pass-name = HashTable-init<String,TimerLabel>() :
;========================= Lowering =========================
;============================================================

defn lower (epackage:EPackage, optimize?:True|False) -> EPackage :
defn lower (epackage:EPackage, optimize?:True|False, profile?:True|False, coverage?:True|False) -> EPackage :
val lower-timer-name = to-string("EL-LOWER -- %_" % [name(epackage)])
within log-time(EL-LOWER, lower-timer-name) :

Expand Down Expand Up @@ -105,6 +105,8 @@ defn lower (epackage:EPackage, optimize?:True|False) -> EPackage :
run-pass("Box Mutables", box-mutables, "boxed", false)
run-pass("Detect Loops", detect-loops, "looped", false)
run-pass("Simple Inline", simple-inline, "inlined0", false)
if profile? or coverage? :
run-pass("Profile", insert-profiling{_, coverage?}, "profile", true)
run-pass("Within Package Inline", within-package-inline{_, true}, "wp-inlined0", false)
run-pass("Cleanup Labels", cleanup-labels, "cleanup-labels", false)
if optimize? :
Expand Down Expand Up @@ -1754,6 +1756,8 @@ defn inline-call (buffer:BodyBuffer,
emit(buffer, ESafepoint(group-id(i), info))
(call-info, instruction-info):
emit(buffer, i)
(i:EProfile) :
false
(i) :
emit(buffer, i)

Expand Down Expand Up @@ -2129,6 +2133,60 @@ defn rename-fn (f:EFn) -> EFn :
create-definitions(f)
rename(f) as EFn

;============================================================
;=================== Insert Profiling =======================
;============================================================

defn insert-profiling (epackage:EPackage, coverage?:True|False) :
val infos = Vector<FunctionInfo>()
val iotable = IOTable(packageio(epackage))
val num-uses = IntTable<Int>(0)
defn get-info (sinfo:False|TraceInfo, body:EBody) -> False|StackTraceEntry :
match(sinfo) :
(sinfo: StackTraceEntry) : sinfo
(sinfo) :
label<False|StackTraceEntry> return :
for i in ins(body) do :
match(info?(i)) :
(info:StackTraceEntry) : return(info)
(info) : false

defn insert (e:EBody, sinfo:StackTraceEntry, insert?:True|False) -> EBody :
val buffer = BodyBuffer(e)
val id = length(infos)
emit(buffer, EProfile(id, 1 when coverage? else 0)) when insert?
for i in ins(e) do :
val i* =
match(info?(i)) :
(info:StackTraceEntry) : sub-info(i, sub-function(info, id))
(info) : i
emit(buffer, i*)
if insert? and not coverage? :
match(i:ELabel) :
emit(buffer, EProfile(id, 0)) when (num-uses[n(i)] == 0)
else :
for v in label-uses(i) do :
update(num-uses, {_ + 1}, v)
add(infos, FunctionInfo(sinfo))
to-body(buffer)

defn insert-texp (t:ELBigItem, insert?:True|False) -> ELBigItem :
match(map(insert-texp{_, insert?}, t)) :
(e:EFn) :
; println("%_ INFO %_" % [length(infos), get-info(info(e), body(e))])
match(get-info(info(e), body(e))) :
(i:StackTraceEntry) : sub-body(e, insert(body(e), i, insert?))
(i:False) : e
(e) : e

;Inline all top-level expressions
val new-exps = for exp in exps(epackage) map :
match(exp) :
(exp:EDefn) : insert-texp(exp, n(exp) != n(iotable,CORE-PROFILE-STACK-TRACE-ID)) as ETExp
(exp:EDefClosure|EDefmethod) : insert-texp(exp, true) as ETExp
(exp) : exp
sub-function-info(sub-exps(epackage, new-exps), to-tuple $ infos)

;============================================================
;==================== Label Cleanup =========================
;============================================================
Expand Down
21 changes: 17 additions & 4 deletions compiler/main.stanza
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,10 @@ val COMMON-STANZA-FLAGS = [
"Requests the compiler to compile in optimized mode.")
Flag("debug", ZeroFlag, OptionalFlag,
"Requests the compiler to compile for debugging.")
Flag("profile", ZeroFlag, OptionalFlag,
"Requests the compiler to add profile counters.")
Flag("coverage", ZeroFlag, OptionalFlag,
"Requests the compiler to add coverage counters.")
Flag("link", OneFlag, OptionalFlag,
"Provide the type of linking to use.")
Flag("ccfiles", ZeroOrMoreFlag, OptionalFlag,
Expand Down Expand Up @@ -333,6 +337,8 @@ defn compile-command () :
flag?(cmd-args, "build-from-source")
flag?(cmd-args, "optimize")
flag?(cmd-args, "debug")
flag?(cmd-args, "profile")
flag?(cmd-args, "coverage")
get?(cmd-args, "ccfiles", [])
ccflags(cmd-args)
map(to-symbol, get?(cmd-args, "flags", []))
Expand All @@ -348,7 +354,8 @@ defn compile-command () :
Command("compile",
AtLeastOneArg, "the .stanza/.proj input files or Stanza package names.",
common-stanza-flags(["o" "s" "pkg" "pkg-cache" "build-from-source" "optimize" "debug" "ccfiles" "ccflags" "flags"
"verbose" "supported-vm-packages" "platform" "external-dependencies" "macros" "link" "timing-log"]),
"verbose" "supported-vm-packages" "platform" "external-dependencies" "macros" "link" "timing-log"
"profile" "coverage"]),
compile-msg, false, verify-args, intercept-no-match-exceptions(compile-action))


Expand Down Expand Up @@ -383,6 +390,8 @@ defn build-command () :
flag?(cmd-args, "build-from-source")
flag?(cmd-args, "optimize")
flag?(cmd-args, "debug")
flag?(cmd-args, "profile")
flag?(cmd-args, "coverage")
[]
ccflags(cmd-args)
map(to-symbol, get?(cmd-args, "flags", []))
Expand All @@ -397,7 +406,7 @@ defn build-command () :
;Command definition
Command("build",
ZeroOrOneArg, "the name of the build target. If not supplied, the default build target is 'main'.",
common-stanza-flags(["s" "o" "external-dependencies" "pkg" "pkg-cache" "build-from-source" "flags" "optimize" "debug" "verbose" "ccflags" "macros" "link" "timing-log"]),
common-stanza-flags(["s" "o" "external-dependencies" "pkg" "pkg-cache" "build-from-source" "flags" "optimize" "debug" "verbose" "ccflags" "macros" "link" "timing-log" "profile" "coverage"]),
build-msg, false, verify-args, intercept-no-match-exceptions(build))

;============================================================
Expand Down Expand Up @@ -449,6 +458,8 @@ defn extend-command () :
false
flag?(cmd-args, "optimize")
false
flag?(cmd-args, "profile")
flag?(cmd-args, "coverage")
get?(cmd-args, "ccfiles", [])
ccflags(cmd-args)
map(to-symbol, get?(cmd-args, "flags", []))
Expand All @@ -462,7 +473,7 @@ defn extend-command () :
Command("extend",
ZeroOrMoreArg, "the .stanza/.proj input files or Stanza packages to use to extend the current compiler with.",
common-stanza-flags(["s" "o" "external-dependencies" "ccfiles" "ccflags" "flags" "supported-vm-packages" "optimize"
"verbose" "macros" "timing-log"])
"verbose" "macros" "timing-log" "profile" "coverage"])
extend-msg, false, verify-args, intercept-no-match-exceptions(extend))

;============================================================
Expand Down Expand Up @@ -518,6 +529,8 @@ defn compile-test-command () :
flag?(cmd-args, "build-from-source")
flag?(cmd-args, "optimize")
flag?(cmd-args, "debug")
flag?(cmd-args, "profile")
flag?(cmd-args, "coverage")
get?(cmd-args, "ccfiles", [])
ccflags(cmd-args)
new-flags
Expand All @@ -531,7 +544,7 @@ defn compile-test-command () :
Command("compile-test",
AtLeastOneArg, "the .stanza/.proj input files or Stanza package names containing tests.",
common-stanza-flags(["platform" "s" "o" "external-dependencies" "pkg" "pkg-cache" "build-from-source" "ccfiles" "ccflags" "flags" "optimize" "debug" "verbose"
"macros" "link" "timing-log"])
"macros" "link" "timing-log" "profile" "coverage"])
compile-test-msg, false, verify-args, intercept-no-match-exceptions(compile-test))

;============================================================
Expand Down
2 changes: 2 additions & 0 deletions compiler/proj-ir.stanza
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ public defstruct BuildStmt <: ProjStmt :
ccflags: Tuple<String|Tuple<String>>
flags: Tuple<Symbol>
optimize: True|False
profile: True|False
coverage: True|False
with:
printer => true

Expand Down
7 changes: 6 additions & 1 deletion compiler/proj-reader.stanza
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,10 @@ defsyntax stanza-projfile :
entry?(bs, `ccfiles)
entry?(bs, `ccflags)
entry?(bs, `flags)
entry?(bs, `optimize, false))
entry?(bs, `optimize, false)
entry?(bs, `profile, false)
entry?(bs, `coverage, false)
)

defrule projstmt = (var ?name:#symbol! = ?v:#projvalue!) :
VarStmtS0(closest-info(), name, v)
Expand Down Expand Up @@ -201,6 +204,8 @@ defsyntax stanza-projfile :
defrule build-option! = (ccflags #:! ?v:#projvalue!) : `ccflags => v
defrule build-option! = (flags #:! ?v:#projvalue!) : `flags => v
defrule build-option! = (optimize) : `optimize => true
defrule build-option! = (profile) : `profile => true
defrule build-option! = (coverage) : `coverage => true

;----------------------------------------------------------
;--------------------- Error Productions ------------------
Expand Down
Loading

0 comments on commit 4b20a67

Please sign in to comment.