From f92b6f388a85b577fc41bd8682f301bf0d027f5b Mon Sep 17 00:00:00 2001 From: Shawn Zhong Date: Wed, 30 Jul 2025 08:40:00 -0700 Subject: [PATCH] Remove dangling empty braces in PTX (#7960) After directive filtering, there will be dangling `{}` spanning two lines. This PR attempts to remove them. For example, ``` .section .debug_abbrev { .b8 1 } .section .debug_info { .b32 70 .b32 .debug_abbrev } ``` The above assembly used to become the following text after filtering directive ``` { } { } ``` --- lib/parsers/asm-parser-ptx.ts | 9 +++++++++ test/asm-parser-tests.ts | 22 ++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/lib/parsers/asm-parser-ptx.ts b/lib/parsers/asm-parser-ptx.ts index 698907758..0e242e539 100644 --- a/lib/parsers/asm-parser-ptx.ts +++ b/lib/parsers/asm-parser-ptx.ts @@ -102,6 +102,8 @@ export class PTXAsmParser extends AsmParser { let inFunctionCall = false; let callBaseIndent = ''; let braceDepth = 0; + let lineNumber = 0; + let openBraceLineNumber = 0; for (let line of asmLines) { const newSource = this.processSourceLine(line, files); @@ -183,8 +185,14 @@ export class PTXAsmParser extends AsmParser { if (this.functionStart.test(line)) { braceDepth++; + openBraceLineNumber = lineNumber; } else if (this.functionEnd.test(line)) { braceDepth--; + // Remove paired braces with no content + if (openBraceLineNumber + 1 === lineNumber) { + asm.pop(); + continue; + } } if (filters.trim) { @@ -196,6 +204,7 @@ export class PTXAsmParser extends AsmParser { source: this.hasOpcode(line) ? currentSource : null, labels: [], }); + lineNumber++; } const endTime = process.hrtime.bigint(); diff --git a/test/asm-parser-tests.ts b/test/asm-parser-tests.ts index 23152b6da..1d9afb918 100644 --- a/test/asm-parser-tests.ts +++ b/test/asm-parser-tests.ts @@ -209,6 +209,28 @@ vprintf, expect(lines[7]).toBe('}'); }); }); + + describe('Directive filter', () => { + it('should remove directives when directive filter is enabled', () => { + const input = ` + .file 1 "/tmp/compiler-explorer-compilerk2brih/example.py" + .section .debug_abbrev + { + .b8 1 + } + .section .debug_info + { + .b32 70 + .b32 .debug_abbrev + } + .section .debug_macinfo { } +`; + const result = parser.processAsm(input, {directives: true}); + const lines = result.asm.map(line => line.text); + expect(lines.length).toBe(1); + expect(lines[0]).toBe(''); + }); + }); }); describe('MlirAsmParser tests', () => {