| 31 | import {BaseTool} from './base-tool.js'; |
| 32 | |
| 33 | export class LLVMMcaTool extends BaseTool { |
| 34 | static get key() { |
| 35 | return 'llvm-mca-tool'; |
| 36 | } |
| 37 | |
| 38 | public rewriteAsm(asm: string) { |
| 39 | return asm |
| 40 | .replaceAll(/.hword\s/gim, '.short ') |
| 41 | .replaceAll(/offset flat:/gim, '') |
| 42 | .replaceAll(/ptr\s%fs/gim, 'PTR fs') |
| 43 | .replaceAll(/^\s*\.(fnstart|eabi_attribute|fpu).*/gim, '') // https://github.com/compiler-explorer/compiler-explorer/issues/1270 |
| 44 | .replaceAll(/^\s*\.(loc|file).*/gim, ''); // Remove debug directives that can cause "unassigned file number" errors |
| 45 | } |
| 46 | |
| 47 | writeAsmFile(data: string, destination: string) { |
| 48 | return fs.writeFile(destination, this.rewriteAsm(data)); |
| 49 | } |
| 50 | |
| 51 | override async runTool(compilationInfo: CompilationInfo, _inputFilepath?: string, args?: string[]) { |
| 52 | const isets = new InstructionSets(); |
| 53 | let target = isets.getInstructionSetTarget(compilationInfo.compiler.instructionSet); |
| 54 | const prependArgs: string[] = []; |
| 55 | |
| 56 | // Check if compiler target is overridden with --target=<foo> or -target <foo>. |
| 57 | let argIsTarget = false; |
| 58 | for (const arg of compilationInfo.options) { |
| 59 | if (arg.startsWith('--target=')) target = arg.replace(/^--target=/, ''); |
| 60 | if (argIsTarget) { |
| 61 | target = arg; |
| 62 | argIsTarget = false; |
| 63 | } |
| 64 | if (arg === '-target') argIsTarget = true; |
| 65 | } |
| 66 | |
| 67 | if (target) prependArgs.push('-mtriple=' + target); |
| 68 | |
| 69 | let haveCPU = false; |
| 70 | let haveM32 = false; |
| 71 | for (const arg of compilationInfo.options) { |
| 72 | if (arg.startsWith('-march') && (target?.startsWith('x86_64') || target?.startsWith('i386'))) { |
| 73 | prependArgs.push(arg.replace(/^-march=/, '-mcpu=')); |
| 74 | haveCPU = true; |
| 75 | } |
| 76 | if (arg === '-m32') haveM32 = true; |
| 77 | if (arg.startsWith('-mcpu')) { |
| 78 | prependArgs.push(arg); |
| 79 | haveCPU = true; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | if (compilationInfo.filters.binary || compilationInfo.filters.binaryObject) { |
| 84 | return this.createErrorResponse('<cannot run analysis on binary>'); |
| 85 | } |
| 86 | |
| 87 | // If no other CPU specified, default to mcpu=generic to |
| 88 | // override llvm-mca's default of -mcpu=native, otherwise MCA's |
| 89 | // CPU analysis varies according to the machine MCA happens to |
| 90 | // run on which can change over time. |
nothing calls this directly
no outgoing calls
no test coverage detected