Appearance
| 1 | namespace Syntax.Process is | |
| 2 | use Collections | |
| 3 | ||
| 4 | // Env-gated measurement reporting for pipe fusion. | |
| 5 | // | |
| 6 | // GHUL_PIPE_FUSION_STUDY names the file the report is written to; | |
| 7 | // unset, nothing here runs and nothing is recorded. Reporting-only: | |
| 8 | // nothing in this class alters what the compiler concludes, reports | |
| 9 | // or emits. | |
| 10 | // | |
| 11 | // The report answers two questions a corpus sweep asks of fusion. | |
| 12 | // One record per site where a chain of Ghul.Pipes combinators is | |
| 13 | // consumed says whether that chain fused and, when it did not, the | |
| 14 | // reason the recogniser or the resolver gave - so the reasons can be | |
| 15 | // counted and the shapes that lose most often ranked. One record per | |
| 16 | // Ghul.Pipes call gives the total combinator surface the site | |
| 17 | // records are a fraction of. | |
| 18 | // | |
| 19 | // Both are keyed by source position, and a later record for a | |
| 20 | // position replaces the one before it: compile-expressions walks a | |
| 21 | // body more than once - speculatively, on overload retry, and again | |
| 22 | // in the combined-solve rounds - and what a site finally decided is | |
| 23 | // what the last walk decided. | |
| 24 | class PIPE_FUSION_STUDY is | |
| 25 | ||
| 26 | _path: string? static | |
| 27 | _enabled: bool static | |
| 28 | _enabled_known: bool static | |
| 29 | ||
| 30 | enabled: bool static is | |
| 31 | if !_enabled_known then | |
| 32 | _path = Driver.COMPILER_ENVIRONMENT.pipe_fusion_study | |
| 33 | _enabled = _path? /\ _path.length > 0 | |
| 34 | _enabled_known = true | |
| 35 | fi | |
| 36 | ||
| 37 | return _enabled | |
| 38 | si | |
| 39 | ||
| 40 | _sites: MutableMap[string, string]? static | |
| 41 | _calls: MutableMap[string, string]? static | |
| 42 | ||
| 43 | _site_records: MutableMap[string, string] static is | |
| 44 | if !_sites? then | |
| 45 | _sites = MAP[string, string]() | |
| 46 | fi | |
| 47 | ||
| 48 | return _sites | |
| 49 | si | |
| 50 | ||
| 51 | _call_records: MutableMap[string, string] static is | |
| 52 | if !_calls? then | |
| 53 | _calls = MAP[string, string]() | |
| 54 | fi | |
| 55 | ||
| 56 | return _calls | |
| 57 | si | |
| 58 | ||
| 59 | _position(location: Source.LOCATION) -> string static => | |
| 60 | "{location.file_name}:{location.start_line}:{location.start_column}" | |
| 61 | ||
| 62 | // Record what a consumption site decided. `consumer` names what | |
| 63 | // was going to read the chain - `for`, or the Ghul.Pipes function | |
| 64 | // the chain was an argument of - and `consumer_fusible` whether | |
| 65 | // fusion has a consumer for it, which decides half of whether | |
| 66 | // the site could fuse whatever the chain looks like. `stages` is | |
| 67 | // the chain's roles outermost first, comma separated. `reason` is | |
| 68 | // empty for a site that fused. | |
| 69 | note_site( | |
| 70 | location: Source.LOCATION, | |
| 71 | consumer: string, | |
| 72 | consumer_fusible: bool, | |
| 73 | stages: string, | |
| 74 | fused: bool, | |
| 75 | reason: string | |
| 76 | ) static is | |
| 77 | if !enabled then | |
| 78 | return | |
| 79 | fi | |
| 80 | ||
| 81 | _site_records[_position(location)] = | |
| 82 | "{consumer}\t{if consumer_fusible then "consumer-fusible" else "consumer-unfusible" fi}\t" | |
| 83 | "{if stages.length == 0 then "-" else stages fi}\t" | |
| 84 | "{if fused then "fused" else "declined" fi}\t" | |
| 85 | "{if reason.length == 0 then "-" else reason fi}" | |
| 86 | si | |
| 87 | ||
| 88 | // Record a call to a Ghul.Pipes function, whether or not fusion | |
| 89 | // ever looked at it. | |
| 90 | note_call(location: Source.LOCATION, name: string) static is | |
| 91 | if !enabled then | |
| 92 | return | |
| 93 | fi | |
| 94 | ||
| 95 | _call_records[_position(location)] = name | |
| 96 | si | |
| 97 | ||
| 98 | report() static is | |
| 99 | if !enabled \/ !_path? then | |
| 100 | return | |
| 101 | fi | |
| 102 | ||
| 103 | try | |
| 104 | let use writer = IO.StreamWriter(_path) | |
| 105 | ||
| 106 | for position in _site_records.keys do | |
| 107 | writer.write_line("site\t{position}\t{_site_records[position]}") | |
| 108 | od | |
| 109 | ||
| 110 | for position in _call_records.keys do | |
| 111 | writer.write_line("call\t{position}\t{_call_records[position]}") | |
| 112 | od | |
| 113 | ||
| 114 | writer.flush() | |
| 115 | catch ex: System.Exception | |
| 116 | IO.Std.error.write_line("pipe fusion study: report failed: {ex}") | |
| 117 | yrt | |
| 118 | si | |
| 119 | si | |
| 120 | si |