aboutsummaryrefslogtreecommitdiff
path: root/lib/parsers/racket-pass-dump-parser.ts
blob: 0fa7e73fc3f02fb2b34fa2715c96709f46060097 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// Copyright (c) 2023, Compiler Explorer Authors
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
//     * Redistributions of source code must retain the above copyright notice,
//       this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above copyright
//       notice, this list of conditions and the following disclaimer in the
//       documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.

import type {
    OptPipelineBackendOptions,
    OptPipelineResults,
    Pass,
} from '../../types/compilation/opt-pipeline-output.interfaces.js';
import type {ParseFiltersAndOutputOptions} from '../../types/features/filters.interfaces.js';
import type {ResultLine} from '../../types/resultline/resultline.interfaces.js';
import {assert} from '../assert.js';

type PassDump = {
    header: string;
    group: string | undefined;
    machine: boolean;
    lines: ResultLine[];
};

export class RacketPassDumpParser {
    filters: RegExp[];
    lineFilters: RegExp[];
    debugInfoFilters: RegExp[];
    debugInfoLineFilters: RegExp[];
    metadataLineFilters: RegExp[];
    moduleHeader: RegExp;
    linkletPhaseHeader: RegExp;
    linkletHeader: RegExp;
    stepHeader: RegExp;
    passHeader: RegExp;
    mainModule: RegExp;

    constructor(compilerProps) {
        // Filters that are always enabled
        this.filters = [];
        this.lineFilters = [];

        // Additional filters conditionally enabled by `filterDebugInfo`
        this.debugInfoFilters = [];
        this.debugInfoLineFilters = [];

        // Conditionally enabled by `filterIRMetadata`
        this.metadataLineFilters = [];

        // Racket's compilation pipeline (and thus its pass output)
        // works on one module at a time.
        this.moduleHeader = /^[ ;]*?compile-linklet: module: (.+)$/;

        // For each module Racket compiles, several linklets are produced that
        // cover different aspects of the module. See Racket's
        // `compile/module.rkt` for more details. The various kinds of linklets
        // for each module include:
        //   - Body linklets for each phase (keyed by phase number)
        //   - `decl`: declaration linklet (shared across module instances)
        //   - `data`: data linklet (shared across module instances)
        //   - `stx`: syntax literals linklet
        //   - `stx-data`: syntax literals data linklet (shared across module
        //     instances)
        this.linkletHeader = /^[ ;]*?compile-linklet: name: (.+)$/;
        this.linkletPhaseHeader = /^[ ;]*?compile-linklet: phase: (\d+)$/;

        // Each linklet moves through various compilation steps.
        this.stepHeader = /^[ ;]*?compile-linklet: step: (.+)$/;
        this.passHeader = /^[ ;]*?output of (.+):$/;

        // Modules with `main` at the top-level are actually showing the
        // compilation process (bits of `raco make`), so we filter those out.
        this.mainModule = /^\(?main/;
    }

    breakdownOutputIntoPassDumps(logLines: ResultLine[]) {
        // Collect output from each compilation step
        const rawPasses: PassDump[] = [];
        let pass: PassDump | null = null;

        // Progressively assemble a group name by cobbling together the
        // module and linklet names.
        let mod: string | null = null;
        let linklet: string | null = null;
        let linkletPhase: number | null = null;

        let lastWasBlank = false; // skip duplicate blank lines

        for (const line of logLines) {
            const moduleMatch = line.text.match(this.moduleHeader);
            if (moduleMatch) {
                mod = moduleMatch[1];
                continue;
            }
            const linkletMatch = line.text.match(this.linkletHeader);
            if (linkletMatch) {
                linklet = linkletMatch[1];
                continue;
            }
            const linkletPhaseMatch = line.text.match(this.linkletPhaseHeader);
            if (linkletPhaseMatch) {
                linkletPhase = parseInt(linkletPhaseMatch[1]);
                continue;
            }
            const stepMatch = line.text.match(this.stepHeader);
            const passMatch = line.text.match(this.passHeader);
            if (stepMatch || passMatch) {
                const name = stepMatch?.[1] || passMatch?.[1];
                assert(name);
                if (pass !== null) {
                    rawPasses.push(pass);
                    pass = null;
                }
                if (mod?.match(this.mainModule)) {
                    // Skip modules with `main` at the root
                    continue;
                }
                let moduleAndLinkletName = `module: ${mod}, linklet: ${linklet}`;
                if (linklet === 'module' && linkletPhase !== null) {
                    moduleAndLinkletName += `, phase: ${linkletPhase}`;
                }
                pass = {
                    header: name,
                    group: moduleAndLinkletName,
                    machine: false,
                    lines: [],
                };
                lastWasBlank = true; // skip leading newlines after the header
            } else {
                if (!pass) {
                    continue;
                }
                if (line.text.startsWith(';;')) {
                    // Skip any other header lines
                    continue;
                }
                if (line.text.trim() === '') {
                    if (!lastWasBlank) {
                        pass.lines.push(line);
                    }
                    lastWasBlank = true;
                } else {
                    pass.lines.push(line);
                    lastWasBlank = false;
                }
                if (line.text === 'done') {
                    // The last step just emits "done", so stop once we've seen
                    // it. This conveniently drops any trailing logs we don't
                    // want as well.
                    rawPasses.push(pass);
                    pass = null;
                }
            }
        }
        if (pass !== null) {
            rawPasses.push(pass);
        }
        return rawPasses;
    }

    associatePassDumpsWithGroups(passDumps: PassDump[]) {
        const passDumpsByGroup: Record<string, PassDump[]> = {};
        // First figure out what all the groups are
        for (const pass of passDumps) {
            if (pass.group) {
                passDumpsByGroup[pass.group] = [];
            }
        }
        for (const pass of passDumps) {
            const {header, group, machine, lines} = pass;
            if (group) {
                assert(group in passDumpsByGroup);
                [passDumpsByGroup[group]].map(entry =>
                    entry.push({
                        header,
                        group,
                        machine,
                        lines,
                    }),
                );
            } else {
                // applies to everything
                for (const [_, entry] of Object.entries(passDumpsByGroup)) {
                    entry.push({
                        header,
                        group: undefined,
                        machine,
                        lines,
                    });
                }
            }
        }
        return passDumpsByGroup;
    }

    matchPassDumps(passDumpsByGroup: Record<string, PassDump[]>) {
        // We have collected output for each step
        // grouped by module and linklet name
        // We now assemble them into before / after pairs
        const finalOutput: OptPipelineResults = {};
        for (const [group, passDumps] of Object.entries(passDumpsByGroup)) {
            const passes: Pass[] = [];
            for (let i = 0; i < passDumps.length; i++) {
                const pass: Pass = {
                    name: '',
                    machine: false,
                    before: [],
                    after: [],
                    irChanged: true,
                };
                const previousDump = i > 0 ? passDumps[i - 1] : null;
                const currentDump = passDumps[i];

                pass.name = currentDump.header;
                if (previousDump) {
                    pass.before = previousDump.lines;
                }
                pass.after = currentDump.lines;

                // check for equality
                pass.irChanged = pass.before.map(x => x.text).join('\n') !== pass.after.map(x => x.text).join('\n');
                passes.push(pass);
            }
            finalOutput[group] = passes;
        }
        return finalOutput;
    }

    breakdownOutput(ir: ResultLine[], llvmOptPipelineOptions: OptPipelineBackendOptions) {
        const rawPasses = this.breakdownOutputIntoPassDumps(ir);
        const passDumpsByGroup = this.associatePassDumpsWithGroups(rawPasses);
        // Match before / after pass dumps and we're done
        return this.matchPassDumps(passDumpsByGroup);
    }

    applyIrFilters(ir: ResultLine[], optPipelineOptions: OptPipelineBackendOptions) {
        // Additional filters conditionally enabled by `filterDebugInfo`/`filterIRMetadata`
        let filters = this.filters;
        let lineFilters = this.lineFilters;
        if (optPipelineOptions.filterDebugInfo) {
            filters = filters.concat(this.debugInfoFilters);
            lineFilters = lineFilters.concat(this.debugInfoLineFilters);
        }
        if (optPipelineOptions.filterIRMetadata) {
            lineFilters = lineFilters.concat(this.metadataLineFilters);
        }

        return (
            ir
                // whole-line filters
                .filter(line => filters.every(re => line.text.match(re) === null))
                // intra-line filters
                .map(resultLine => {
                    let line = resultLine.text;
                    // eslint-disable-next-line no-constant-condition
                    while (true) {
                        let newLine = line;
                        for (const re of lineFilters) {
                            newLine = newLine.replace(re, '');
                        }
                        if (newLine === line) {
                            break;
                        } else {
                            line = newLine;
                        }
                    }
                    resultLine.text = line;
                    return resultLine;
                })
        );
    }

    process(output: ResultLine[], _: ParseFiltersAndOutputOptions, optPipelineOptions: OptPipelineBackendOptions) {
        const preprocessedLines = this.applyIrFilters(output, optPipelineOptions);
        return this.breakdownOutput(preprocessedLines, optPipelineOptions);
    }
}