Good full module handling

This commit is contained in:
Jeremy Rifkin
2022-06-19 10:06:22 -04:00
parent 220f191cdd
commit bc78af7421
4 changed files with 117 additions and 27 deletions
+10 -4
View File
@@ -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),
};
}
+93 -16
View File
@@ -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: <attributes>"
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<string, PassDump[]> = {};
// 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['<Full Module>'] = [];
// 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['<Full Module>']].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<string, PassDump[]>) {
// 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);
}
}
+10 -7
View File
@@ -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<monaco.editor.IStandaloneDiffEdi
modifiedModel: any;
options: Toggles;
// todo: can probably get rid of this and just use state?
lastOptions = {'dump-full-module': false};
lastOptions: LLVMOptPipelineBackendOptions = {'dump-full-module': false};
resizeStartX: number;
resizeStartWidth: number;
resizeDragMoveBind: (e: MouseEvent) => void;
@@ -199,7 +204,7 @@ export class LLVMOptPipeline extends MonacoPane<monaco.editor.IStandaloneDiffEdi
const options = this.options.get();
// TODO: Make use of filter-inconsequential-passes on the back end? Maybe provide a specific function arg to
// the backend? Would be a data transfer optimization.
const newOptions = {
const newOptions: LLVMOptPipelineBackendOptions = {
//'filter-inconsequential-passes': options['filter-inconsequential-passes'],
'dump-full-module': options['dump-full-module'],
};
@@ -265,12 +270,10 @@ export class LLVMOptPipeline extends MonacoPane<monaco.editor.IStandaloneDiffEdi
}
this.passesList.empty();
if (keys.length > 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
@@ -34,3 +34,7 @@ export type Pass = {
};
export type LLVMOptPipelineOutput = Record<string, Pass[]>;
export type LLVMOptPipelineBackendOptions = {
'dump-full-module': boolean;
};