diff --git a/lib/base-compiler.ts b/lib/base-compiler.ts index 31cab55fb..b1f039a5d 100644 --- a/lib/base-compiler.ts +++ b/lib/base-compiler.ts @@ -62,6 +62,7 @@ import {IAsmParser} from './parsers/asm-parser.interfaces'; import {LlvmPassDumpParser} from './parsers/llvm-pass-dump-parser'; import {getToolchainPath} from './toolchain-utils'; import * as utils from './utils'; +import {LLVMOptPipelineBackendOptions} from '../types/compilation/llvm-opt-pipeline-output.interfaces'; export class BaseCompiler { public compiler: any; @@ -968,7 +969,12 @@ export class BaseCompiler { }; } - async generateLLVMOptPipeline(inputFilename, options, filters: ParseFilters, llvmOptPipelineOptions) { + async generateLLVMOptPipeline( + inputFilename, + options, + filters: ParseFilters, + llvmOptPipelineOptions: LLVMOptPipelineBackendOptions, + ) { // These options make Clang produce an IR const newOptions = _.filter(options, option => option !== '-fcolor-diagnostics') .concat(this.compiler.llvmOptArg) @@ -982,11 +988,11 @@ export class BaseCompiler { if (output.code !== 0) { return [{text: 'Failed to get the transformation pipeline output'}]; } - const llvmOptPipeline = await this.processLLVMOptPipeline(output, filters); + const llvmOptPipeline = await this.processLLVMOptPipeline(output, filters, llvmOptPipelineOptions); return llvmOptPipeline.asm; } - async processLLVMOptPipeline(output, filters: ParseFilters) { + async processLLVMOptPipeline(output, filters: ParseFilters, llvmOptPipelineOptions: LLVMOptPipelineBackendOptions) { //const irPath = this.getIrOutputFilename(output.inputFilename); //if (await fs.pathExists(irPath)) { // const output = await fs.readFile(irPath, 'utf-8'); @@ -998,7 +1004,7 @@ export class BaseCompiler { // labelDefinitions: {}, //}; return { - asm: this.llvmPassDumpParser.process(output.stderr, filters), + asm: this.llvmPassDumpParser.process(output.stderr, filters, llvmOptPipelineOptions), }; } diff --git a/lib/parsers/llvm-pass-dump-parser.ts b/lib/parsers/llvm-pass-dump-parser.ts index 3cffe26cd..d6f0af585 100644 --- a/lib/parsers/llvm-pass-dump-parser.ts +++ b/lib/parsers/llvm-pass-dump-parser.ts @@ -24,7 +24,12 @@ import _ from 'underscore'; -import {LLVMOptPipelineOutput, OutputLine, Pass} from '../../types/compilation/llvm-opt-pipeline-output.interfaces'; +import { + LLVMOptPipelineBackendOptions, + LLVMOptPipelineOutput, + OutputLine, + Pass, +} from '../../types/compilation/llvm-opt-pipeline-output.interfaces'; import {ParseFilters} from '../../types/features/filters.interfaces'; import * as utils from '../utils'; @@ -49,6 +54,7 @@ function assert(condition: boolean, message?: string, ...args: any[]): asserts c // Ir Dump for a pass with raw lines type PassDump = { header: string; + affectedFunction: string | undefined; machine: boolean; lines: OutputLine[]; }; @@ -96,7 +102,7 @@ export class LlvmPassDumpParser { // Ir dump headers look like "*** IR Dump After XYZ ***" // Machine dump headers look like "# *** IR Dump After XYZ ***:", possibly with a comment or "(function: xyz)" // or "(loop: %x)" at the end - this.irDumpAfterHeader = /^\*{3} (.+) \*{3}(\s+\((function: \w+|loop: %\w+)\))?(;.+)?$/; + this.irDumpAfterHeader = /^\*{3} (.+) \*{3}(?:\s+\((?:function: |loop: )(%?\w+)\))?(?:;.+)?$/; this.machineCodeDumpHeader = /^# \*{3} (.+) \*{3}:$/; // Ir dumps are "define T @_Z3fooi(...) . .. {" or "# Machine code for function _Z3fooi: " this.functionDefine = /^define .+ @(\w+)\(.+$/; @@ -112,6 +118,7 @@ export class LlvmPassDumpParser { // break down output by "*** IR Dump After XYZ ***" markers const raw_passes: PassDump[] = []; let pass: PassDump | null = null; + let lastWasBlank = false; // skip duplicate blank lines for (const line of ir) { // stop once the machine code passes start, can't handle these yet //if (this.machineCodeDumpHeader.test(line.text)) { @@ -119,21 +126,34 @@ export class LlvmPassDumpParser { //} const irMatch = line.text.match(this.irDumpAfterHeader); const machineMatch = line.text.match(this.machineCodeDumpHeader); - if (irMatch || machineMatch) { + const header = irMatch || machineMatch; + if (header) { if (pass !== null) { raw_passes.push(pass); } - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion pass = { - header: (irMatch || machineMatch)![1], + header: header[1], + // in dump full module mode some headers are annotated for what function (or loop) they operate on + // if we aren't in full module mode or this is a header that isn't function/loop specific this will + // be undefined + affectedFunction: header[2], machine: !!machineMatch, lines: [], }; + lastWasBlank = true; // skip leading newlines after the header } else { if (pass === null) { throw 'Internal error during breakdownOutput (1)'; } - pass.lines.push(line); + if (line.text.trim() === '') { + if (!lastWasBlank) { + pass.lines.push(line); + } + lastWasBlank = true; + } else { + pass.lines.push(line); + lastWasBlank = false; + } } } if (pass !== null) { @@ -248,6 +268,7 @@ export class LlvmPassDumpParser { } passDumpsByFunction[name].push({ header, + affectedFunction: undefined, machine, lines, }); @@ -267,6 +288,56 @@ export class LlvmPassDumpParser { return passDumpsByFunction; } + // used for full module dump mode + associateFullDumpsWithFunctions(passDumps: PassDump[]) { + // Currently we have an array of passes that'll have target annotations + const passDumpsByFunction: Record = {}; + // First figure out what all the functions are + for (const pass of passDumps) { + // If it starts with a % it's a loop + if (pass.affectedFunction && !pass.affectedFunction.startsWith('%')) { + passDumpsByFunction[pass.affectedFunction] = []; + } + } + passDumpsByFunction[''] = []; + // I'm assuming loop dumps should correspond to the previous function dumped + //const functions = Object.keys(passDumpsByFunction); + let previousFunction: string | null = null; + for (const pass of passDumps) { + const {header, affectedFunction, machine, lines} = pass; + if (affectedFunction) { + let fn = affectedFunction; + if (affectedFunction.startsWith('%')) { + assert(previousFunction !== null); + fn = previousFunction; + } + console.log(fn); + assert(fn in passDumpsByFunction); + [passDumpsByFunction[fn], passDumpsByFunction['']].map(entry => + entry.push({ + header: `${header} (${fn})`, + affectedFunction: fn, + machine, + lines, + }), + ); + previousFunction = fn; + } else { + // applies to everything + for (const [_, entry] of Object.entries(passDumpsByFunction)) { + entry.push({ + header, + affectedFunction: undefined, + machine, + lines, + }); + } + previousFunction = null; + } + } + return passDumpsByFunction; + } + matchPassDumps(passDumpsByFunction: Record) { // We have all the passes for each function, now we will go through each function and match the before/after // dumps @@ -329,19 +400,25 @@ export class LlvmPassDumpParser { return final_output; } - breakdownOutput(ir: OutputLine[]) { + breakdownOutput(ir: OutputLine[], llvmOptPipelineOptions: LLVMOptPipelineBackendOptions) { // break down output by "*** IR Dump After XYZ ***" markers const raw_passes = this.breakdownOutputIntoPassDumps(ir); - // Further break down by functions in each dump - const passDumps = raw_passes.map(this.breakdownPassDumpsIntoFunctions.bind(this)); - // Transform array of passes containing multiple functions into a map from functions to arrays of passes on - // those functions - const passDumpsByFunction = this.breakdownIntoPassDumpsByFunction(passDumps); - // Match before / after pass dumps and we're done - return this.matchPassDumps(passDumpsByFunction); + if (llvmOptPipelineOptions['dump-full-module']) { + const passDumpsByFunction = this.associateFullDumpsWithFunctions(raw_passes); + // Match before / after pass dumps and we're done + return this.matchPassDumps(passDumpsByFunction); + } else { + // Further break down by functions in each dump + const passDumps = raw_passes.map(this.breakdownPassDumpsIntoFunctions.bind(this)); + // Transform array of passes containing multiple functions into a map from functions to arrays of passes on + // those functions + const passDumpsByFunction = this.breakdownIntoPassDumpsByFunction(passDumps); + // Match before / after pass dumps and we're done + return this.matchPassDumps(passDumpsByFunction); + } } - process(ir: OutputLine[], _: ParseFilters) { + process(ir: OutputLine[], _: ParseFilters, llvmOptPipelineOptions: LLVMOptPipelineBackendOptions) { // Filter a lot of junk before processing const preprocessed_lines = ir .filter(line => this.filters.every(re => line.text.match(re) === null)) // apply filters @@ -363,6 +440,6 @@ export class LlvmPassDumpParser { return _line; }); - return this.breakdownOutput(preprocessed_lines); + return this.breakdownOutput(preprocessed_lines, llvmOptPipelineOptions); } } diff --git a/static/panes/llvm-opt-pipeline.ts b/static/panes/llvm-opt-pipeline.ts index bb6e26cc4..c90683504 100644 --- a/static/panes/llvm-opt-pipeline.ts +++ b/static/panes/llvm-opt-pipeline.ts @@ -38,7 +38,12 @@ import {Hub} from '../hub'; import * as utils from '../utils'; -import {LLVMOptPipelineOutput, OutputLine, Pass} from '../../types/compilation/llvm-opt-pipeline-output.interfaces'; +import { + LLVMOptPipelineBackendOptions, + LLVMOptPipelineOutput, + OutputLine, + Pass, +} from '../../types/compilation/llvm-opt-pipeline-output.interfaces'; import TomSelect from 'tom-select'; import scrollIntoView from 'scroll-into-view-if-needed'; @@ -61,7 +66,7 @@ export class LLVMOptPipeline extends MonacoPane void; @@ -199,7 +204,7 @@ export class LLVMOptPipeline extends MonacoPane 0) { - if (selectedFunction === '') { + if (selectedFunction === '' || !(selectedFunction in results)) { selectedFunction = keys[0]; } - if (selectedFunction in results) { - this.functionSelector.setValue(selectedFunction); - } + this.functionSelector.setValue(selectedFunction); } else { // restore this.selectedFunction, next time the compilation results aren't errors the selected function will // still be the same diff --git a/types/compilation/llvm-opt-pipeline-output.interfaces.ts b/types/compilation/llvm-opt-pipeline-output.interfaces.ts index 6bb2d7f35..bbeac45e4 100644 --- a/types/compilation/llvm-opt-pipeline-output.interfaces.ts +++ b/types/compilation/llvm-opt-pipeline-output.interfaces.ts @@ -34,3 +34,7 @@ export type Pass = { }; export type LLVMOptPipelineOutput = Record; + +export type LLVMOptPipelineBackendOptions = { + 'dump-full-module': boolean; +};