From c7056b31ebbf5a176f49e9609aded8a4a4b89fb0 Mon Sep 17 00:00:00 2001 From: Jacob Panov <84049465+jacobpanov@users.noreply.github.com> Date: Wed, 30 Jul 2025 11:00:09 -0400 Subject: [PATCH] Fix llvm-mca tool preprocessing of debug directives (#7964) This PR fixes the "unassigned file number" error in llvm-mca when processing GCC-generated assembly with debug information #6795 . **Problem**: GCC with `-g` flag generates `.loc` and `.file` debug directives that cause llvm-mca to fail with "unassigned file number" errors. **Solution**: Filter out `.loc` and `.file` directives during assembly preprocessing, similar to how other problematic directives are already handled. **Changes**: - Add regex filtering for `.loc` and `.file` directives in `rewriteAsm()` - Make `rewriteAsm()` method public for testability - Add comprehensive test coverage for debug directive removal The fix is minimal, targeted, and follows the existing pattern of directive filtering. --- lib/tooling/llvm-mca-tool.ts | 5 +++-- test/analysis-tests.ts | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/tooling/llvm-mca-tool.ts b/lib/tooling/llvm-mca-tool.ts index 3d24c1596..9528f7bc0 100644 --- a/lib/tooling/llvm-mca-tool.ts +++ b/lib/tooling/llvm-mca-tool.ts @@ -36,12 +36,13 @@ export class LLVMMcaTool extends BaseTool { return 'llvm-mca-tool'; } - rewriteAsm(asm: string) { + public rewriteAsm(asm: string) { return asm .replaceAll(/.hword\s/gim, '.short ') .replaceAll(/offset flat:/gim, '') .replaceAll(/ptr\s%fs/gim, 'PTR fs') - .replaceAll(/^\s*\.(fnstart|eabi_attribute|fpu).*/gim, ''); // https://github.com/compiler-explorer/compiler-explorer/issues/1270 + .replaceAll(/^\s*\.(fnstart|eabi_attribute|fpu).*/gim, '') // https://github.com/compiler-explorer/compiler-explorer/issues/1270 + .replaceAll(/^\s*\.(loc|file).*/gim, ''); // Remove debug directives that can cause "unassigned file number" errors } writeAsmFile(data: string, destination: string) { diff --git a/test/analysis-tests.ts b/test/analysis-tests.ts index 9dc22bd8b..42f56cdae 100644 --- a/test/analysis-tests.ts +++ b/test/analysis-tests.ts @@ -26,6 +26,9 @@ import {beforeAll, describe, expect, it} from 'vitest'; import {CompilationEnvironment} from '../lib/compilation-env.js'; import {AnalysisTool, LLVMmcaTool} from '../lib/compilers/index.js'; +import {ToolEnv} from '../lib/tooling/base-tool.interface.js'; +import {LLVMMcaTool as LLVMMcaTooling} from '../lib/tooling/llvm-mca-tool.js'; +import {ToolInfo} from '../types/tool.interfaces.js'; import { makeCompilationEnvironment, @@ -100,4 +103,17 @@ describe('LLVM-mca tool definition', () => { }); expect(new AnalysisTool(info, ce).getInfo().disabledFilters).toEqual(['labels', 'directives', 'debugCalls']); }); + + it('should remove .loc and .file directives from assembly', () => { + const mcaTool = new LLVMMcaTooling({} as ToolInfo, {} as ToolEnv); + + const asmWithDebugInfo = '.file "test.c"\n.loc 1 1 12\nmovl $2, %eax\n.file 0 "/path" "test.c"\nret\n'; + const result = mcaTool.rewriteAsm(asmWithDebugInfo); + + // Verify that .loc and .file directives are removed + expect(result).not.toContain('.loc'); + expect(result).not.toContain('.file'); + expect(result).toContain('movl $2, %eax'); + expect(result).toContain('ret'); + }); });