diff --git a/lib/parsers/llvm-print-after-all-parser.ts b/lib/parsers/llvm-print-after-all-parser.ts index 13b12757e..08ddce5f3 100644 --- a/lib/parsers/llvm-print-after-all-parser.ts +++ b/lib/parsers/llvm-print-after-all-parser.ts @@ -1,4 +1,4 @@ -// Copyright (c) 2018, Compiler Explorer Authors +// Copyright (c) 2022, Compiler Explorer Authors // All rights reserved. // // Redistribution and use in source and binary forms, with or without @@ -24,6 +24,7 @@ import _ from 'underscore'; +import {LLVMOptPipelineOutput, OutputLine, Pass} from '../../types/compilation/llvm-opt-pipeline-output.interfaces'; import {ParseFilters} from '../../types/features/filters.interfaces'; import * as utils from '../utils'; @@ -45,7 +46,6 @@ function assert(condition: boolean, message?: string, ...args: any[]): asserts c } } -type OutputLine = {text: string}; // Ir Dump for a pass with raw lines type PassDump = { header: string; @@ -56,12 +56,6 @@ type SplitPassDump = { header: string; functions: Record; }; -// Pass name with before / after dump -type Pass = { - name: string; - after: OutputLine[]; - before: OutputLine[]; -}; export class LlvmPrintAfterAllParser { filters: RegExp[]; @@ -229,7 +223,7 @@ export class LlvmPrintAfterAllParser { lines, }); } - if (functionEntries.length > 0) { + if (functionEntries.length === 0) { throw 'Internal error during breakdownOutput (2)'; } else if (functionEntries.length === 1) { const name = functionEntries[0][0]; @@ -245,7 +239,7 @@ export class LlvmPrintAfterAllParser { //}); // We have all the passes for each function, now we will go through each function and match the before/after // dumps - const final_output: Record = {}; + const final_output: LLVMOptPipelineOutput = {}; for (const [function_name, passDumps] of Object.entries(passDumpsByFunction)) { // I had a fantastic chunk2 method to iterate the passes in chunks of 2 but I've been foiled by an edge // case: At least the "Delete dead loops" may only print a before dump and no after dump @@ -256,6 +250,7 @@ export class LlvmPrintAfterAllParser { name: '', after: [], before: [], + irChanged: true, }; assert(i < passDumps.length - 1); // make sure there's another item const current_dump = passDumps[i]; @@ -286,6 +281,8 @@ export class LlvmPrintAfterAllParser { } else { assert(false, 'Unexpected pass header', current_dump.header); } + // check for equality + pass.irChanged = pass.before.map(x => x.text).join('\n') !== pass.after.map(x => x.text).join('\n'); passes.push(pass); } assert(!(function_name in final_output), 'xxx'); @@ -297,7 +294,7 @@ export class LlvmPrintAfterAllParser { return final_output; } - process(ir: {text: string}[], _: ParseFilters) { + process(ir: OutputLine[], _: ParseFilters) { // Filter a lot of junk before processing const preprocessed_lines = ir .filter(line => this.filters.every(re => line.text.match(re) === null)) // apply filters diff --git a/package-lock.json b/package-lock.json index 2e6153e57..e31335bbe 100644 --- a/package-lock.json +++ b/package-lock.json @@ -54,6 +54,7 @@ "request": "^2.88.2", "response-time": "^2.3.2", "sanitize-filename": "^1.6.3", + "scroll-into-view-if-needed": "^2.2.29", "semver": "^7.3.5", "serve-favicon": "^2.4.5", "shell-quote": "^1.7.3", @@ -3857,6 +3858,11 @@ "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=" }, + "node_modules/compute-scroll-into-view": { + "version": "1.0.17", + "resolved": "https://registry.npmjs.org/compute-scroll-into-view/-/compute-scroll-into-view-1.0.17.tgz", + "integrity": "sha512-j4dx+Fb0URmzbwwMUrhqWM2BEWHdFGx+qZ9qqASHRPqvTYdqvWnHg0H1hIbcyLnvgnoNAVMlwkepyqM3DaIFUg==" + }, "node_modules/concat-map": { "version": "0.0.1", "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", @@ -12111,6 +12117,14 @@ "resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-0.4.1.tgz", "integrity": "sha512-xbbCH5dCYU5T8LcEhhuh7HJ88HXuW3qsI3Y0zOZFKfZEHcpWiHU/Jxzk629Brsab/mMiHQti9wMP+845RPe3Vg==" }, + "node_modules/scroll-into-view-if-needed": { + "version": "2.2.29", + "resolved": "https://registry.npmjs.org/scroll-into-view-if-needed/-/scroll-into-view-if-needed-2.2.29.tgz", + "integrity": "sha512-hxpAR6AN+Gh53AdAimHM6C8oTN1ppwVZITihix+WqalywBeFcQ6LdQP5ABNl26nX8GTEL7VT+b8lKpdqq65wXg==", + "dependencies": { + "compute-scroll-into-view": "^1.0.17" + } + }, "node_modules/select": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/select/-/select-1.1.2.tgz", @@ -17817,6 +17831,11 @@ } } }, + "compute-scroll-into-view": { + "version": "1.0.17", + "resolved": "https://registry.npmjs.org/compute-scroll-into-view/-/compute-scroll-into-view-1.0.17.tgz", + "integrity": "sha512-j4dx+Fb0URmzbwwMUrhqWM2BEWHdFGx+qZ9qqASHRPqvTYdqvWnHg0H1hIbcyLnvgnoNAVMlwkepyqM3DaIFUg==" + }, "concat-map": { "version": "0.0.1", "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", @@ -23866,6 +23885,14 @@ } } }, + "scroll-into-view-if-needed": { + "version": "2.2.29", + "resolved": "https://registry.npmjs.org/scroll-into-view-if-needed/-/scroll-into-view-if-needed-2.2.29.tgz", + "integrity": "sha512-hxpAR6AN+Gh53AdAimHM6C8oTN1ppwVZITihix+WqalywBeFcQ6LdQP5ABNl26nX8GTEL7VT+b8lKpdqq65wXg==", + "requires": { + "compute-scroll-into-view": "^1.0.17" + } + }, "select": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/select/-/select-1.1.2.tgz", diff --git a/package.json b/package.json index 35ea99c36..5967aad6a 100644 --- a/package.json +++ b/package.json @@ -65,6 +65,7 @@ "request": "^2.88.2", "response-time": "^2.3.2", "sanitize-filename": "^1.6.3", + "scroll-into-view-if-needed": "^2.2.29", "semver": "^7.3.5", "serve-favicon": "^2.4.5", "shell-quote": "^1.7.3", diff --git a/static/explorer.scss b/static/explorer.scss index 08af5a407..73e69d3aa 100644 --- a/static/explorer.scss +++ b/static/explorer.scss @@ -134,6 +134,10 @@ li.tweet { margin: 0 10px 0 0; } +.llvm-opt-function-picker { + min-width: 10em; +} + #root { width: 100%; margin: 0; @@ -952,3 +956,30 @@ html[data-theme='dark'] { } } } + +.llvm-opt-pipeline-body { + display: flex; + overflow: hidden; + align-items: stretch; + height: 100%; + .passes-column { + color: white; + min-width: 200px; + max-width: 30%; + overflow-y: scroll; + overflow-x: hidden; + height: 100%; + .passes-list div { + background: rgba(255, 255, 255, 0.1); + padding: 2px 3px; + border: 1px solid black; + cursor: pointer; + &:hover, &.active { + background: rgba(255, 255, 255, 0.2); + } + &.changed { + color: #36ac46; + } + } + } +} diff --git a/static/panes/llvm-opt-pipeline.ts b/static/panes/llvm-opt-pipeline.ts index 0fccb4301..28f215e67 100644 --- a/static/panes/llvm-opt-pipeline.ts +++ b/static/panes/llvm-opt-pipeline.ts @@ -38,21 +38,100 @@ import {Hub} from '../hub'; import * as utils from '../utils'; -export class LLVMOptPipeline extends MonacoPane { +import {LLVMOptPipelineOutput, OutputLine, Pass} from '../../types/compilation/llvm-opt-pipeline-output.interfaces'; +import TomSelect from 'tom-select'; + +import scrollIntoView from 'scroll-into-view-if-needed'; + +export class LLVMOptPipeline extends MonacoPane { + results: LLVMOptPipelineOutput = {}; irCode: any[] = []; passesColumn: JQuery; + passesList: JQuery; + body: JQuery; + clickCallback: (e: JQuery.ClickEvent) => void; + keydownCallback: (e: JQuery.KeyDownEvent) => void; + isPassListSelected = false; + functionSelector: TomSelect; + selectedFunction = ''; + originalModel: any; + modifiedModel: any; constructor(hub: Hub, container: Container, state: IrState & MonacoPaneState) { super(hub, container, state); this.passesColumn = this.domRoot.find('.passes-column'); + this.passesList = this.domRoot.find('.passes-list'); + this.body = this.domRoot.find('.llvm-opt-pipeline-body'); + const selector = this.domRoot.get()[0].getElementsByClassName('function-selector')[0]; + if (!(selector instanceof HTMLSelectElement)) { + throw new Error('.function-selector is not an HTMLSelectElement'); + } + /*const instance = new TomSelect(selector, { + sortField: 'name', + valueField: 'id', + labelField: 'name', + searchField: ['name'], + options: [ + {id: 1, name: 'Assembly'}, + {id: 2, name: 'Compiler stdout'}, + {id: 3, name: 'Compiler stderr'}, + {id: 4, name: 'Execution stdout'}, + {id: 5, name: 'Execution stderr'}, + {id: 6, name: 'GNAT Expanded Code'}, + {id: 7, name: 'GNAT Tree Code'}, + ], + items: [], + render: { + option: (item, escape) => { + return `
${escape(item.name)}
`; + }, + }, + dropdownParent: 'body', + plugins: ['input_autogrow'], + onChange: value => { + //if (picker.classList.contains('lhsdifftype')) { + // this.lhs.difftype = parseInt(value as any as string); + // this.lhs.refresh(); + //} else { + // this.rhs.difftype = parseInt(value as any as string); + // this.rhs.refresh(); + //} + //this.updateState(); + }, + });*/ + this.functionSelector = new TomSelect(selector, { + valueField: 'value', + labelField: 'title', + searchField: ['title'], + dropdownParent: 'body', + plugins: ['input_autogrow'], + sortField: 'title', + onChange: e => this.onFnChange(e as any as string), + }); + //this.functionSelector.addOption({ + // title: "Test", + // value: "x" + //}); + //this.functionSelector.addOption({ + // title: "TestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTest", + // value: "y" + //}); + //this.functionSelector.addOption({ + // title: "alpha", + // value: "a" + //}); + this.clickCallback = this.onClickCallback.bind(this); + this.keydownCallback = this.onKeydownCallback.bind(this); + $(document).on('click', this.clickCallback); + $(document).on('keydown', this.keydownCallback); } override getInitialHTML(): string { return $('#llvm-opt-pipeline').html(); } - override createEditor(editorRoot: HTMLElement): monaco.editor.IStandaloneCodeEditor { - return monaco.editor.create( + override createEditor(editorRoot: HTMLElement): monaco.editor.IStandaloneDiffEditor { + const editor = monaco.editor.createDiffEditor( editorRoot, extendConfig({ language: 'llvm-ir', @@ -62,6 +141,10 @@ export class LLVMOptPipeline extends MonacoPane'}]); + //this.showIrResults([{text: ''}]); // TODO } } @@ -128,13 +211,31 @@ export class LLVMOptPipeline extends MonacoPane'); + //this.editor.setValue(''); } } - showIrResults(result: any[]): void { - this.irCode = result; - this.editor.getModel()?.setValue(result.length ? _.pluck(result, 'text').join('\n') : ''); + updateResults(results: LLVMOptPipelineOutput): void { + this.results = results; + //const functions = Object.keys(result); + this.functionSelector.clearOptions(); + this.functionSelector.clearActiveOption(); + const keys = Object.keys(results); + if (keys.length === 0) { + this.functionSelector.addOption({ + title: '', + value: '', + }); + } + for (const fn of keys) { + this.functionSelector.addOption({ + title: fn, + value: fn, + }); + } + + //this.irCode = result; + /*this.editor.getModel()?.setValue(result.length ? _.pluck(result, 'text').join('\n') : ''); if (!this.isAwaitingInitialResults) { if (this.selection) { @@ -142,6 +243,74 @@ export class LLVMOptPipeline extends MonacoPane y.text).join("\n") + x.after.map(y => y.text).join("\n")); + } + + onFnChange(name: string) { + this.selectedFunction = name; + const passes = this.results[name]; + this.passesList.empty(); + for (const [i, pass] of passes.entries()) { + const changedClass = pass.irChanged ? 'changed' : ''; + this.passesList.append(`
${_.escape(pass.name)}
`); + } + const passDivs = this.passesList.find('.pass'); + passDivs.on('click', e => { + const target = e.target; + this.passesList.find('.active').removeClass('active'); + $(target).addClass('active'); + this.displayPass(parseInt(target.getAttribute('data-i') as string)); + }); + this.resize(); // pass list width may change + } + + displayPass(i: number) { + const pass = this.results[this.selectedFunction][i]; + console.log(pass); + const before = pass.before.map(x => x.text).join('\n'); + const after = pass.after.map(x => x.text).join('\n'); + console.log(this.editor.getModel()); + console.log(this.editor.getModel()?.original); + this.editor.getModel()?.original.setValue(before); + this.editor.getModel()?.modified.setValue(after); + } + + onClickCallback(e: JQuery.ClickEvent) { + this.isPassListSelected = (this.passesList.get(0) as HTMLElement).contains(e.target); + } + + onKeydownCallback(e: JQuery.KeyDownEvent) { + if (this.isPassListSelected) { + if (e.key === 'ArrowUp') { + e.preventDefault(); + const active = this.passesList.find('.active'); + const prev = active.prev().get(0); + if (prev) { + active.removeClass('active'); + $(prev).addClass('active'); + scrollIntoView(prev, { + scrollMode: 'if-needed', + block: 'nearest', + }); + this.displayPass(parseInt(prev.getAttribute('data-i') as string)); + } + } + if (e.key === 'ArrowDown') { + e.preventDefault(); + const active = this.passesList.find('.active'); + const next = active.next().get(0); + if (next) { + active.removeClass('active'); + $(next).addClass('active'); + scrollIntoView(next, { + scrollMode: 'if-needed', + block: 'nearest', + }); + this.displayPass(parseInt(next.getAttribute('data-i') as string)); + } + } } } @@ -152,10 +321,13 @@ export class LLVMOptPipeline extends MonacoPane; diff --git a/views/templates.pug b/views/templates.pug index 998452775..5462e0a95 100644 --- a/views/templates.pug +++ b/views/templates.pug @@ -329,8 +329,14 @@ #llvm-opt-pipeline .top-bar.btn-toolbar.bg-light(role="toolbar") include font-size.pug - div(style="display: flex; overflow: hidden;") - .passes-column(style="color: white; min-width: 100px; max-width: 20%;") Passes: + .btn-group.btn-group-sm + .input-group.input-group-sm.mb-auto + .input-group-prepend + label.input-group-text + | Function:  + select.llvm-opt-function-picker.function-selector(placeholder="Select function") + div.llvm-opt-pipeline-body + .passes-column Passes: .passes-list .monaco-placeholder