mirror of
https://github.com/compiler-explorer/compiler-explorer.git
synced 2025-12-27 09:23:52 -05:00
Close #5530. Infra: https://github.com/compiler-explorer/infra/pull/1711. Previous work by @siboehm at #5531 ## Summary This pull request introduces support for the [Triton](https://github.com/triton-lang/triton) language, a Python-based DSL for writing highly efficient GPU kernels. - [x] **New Language Support**: I've added comprehensive support for the Triton programming language, allowing users to compile and inspect Triton kernels within Compiler Explorer. (c.f., `lib/compilers/triton.ts`) - [x] **Python Wrapper for Compilation**: A new Python wrapper script (`triton_wrapper.py`) has been introduced to manage Triton compilation, patching its behavior to dump compiled kernels and intermediate representations without requiring actual execution, and consolidating the output for Compiler Explorer. - [x] **Device Assembly View**: Enables viewing of generated device assembly code (e.g., PTX, AMDGCN) and various intermediate representations (MLIR, LLVM IR) produced by the Triton compiler. - [x] **MLIR Parsing**: New parsers (`asm-parser-mlir.ts` and `mlir-pass-dump-parser.ts`) have been added to correctly interpret and display MLIR assembly and optimization pass dumps, including source location information. - [x] **Multi-Version & Multi-Backend Support**: Painstakingly includes all 8 versions (from 2.2.0 to 3.3.1) of Triton that supports Python 3.12. Supports both CUDA and HIP backend for Triton 3. ## Screenshots Source and assembly: <img width="1354" height="789" alt="image" src="https://github.com/user-attachments/assets/c29650ff-2073-40e0-a9e6-ff8377094b5e" /> Device view for MLIR and LLVM IR: <img width="1402" height="670" alt="image" src="https://github.com/user-attachments/assets/43dd5c68-ca78-41b1-9865-e97ffe3ef73c" /> Opt pipeline viewer: <img width="1408" height="668" alt="image" src="https://github.com/user-attachments/assets/429eef8c-aaac-4781-aafa-39ef0ffc7241" /> Diff of TTIR in Triton 3.3.1 vs 2.3.0: <img width="1580" height="726" alt="image" src="https://github.com/user-attachments/assets/a928c893-dd9a-4c3a-a048-14046e56a14c" /> CUDA & HIP: <img width="1596" height="800" alt="image" src="https://github.com/user-attachments/assets/c18800c3-cfad-4e5e-96de-ba92c9f236ea" /> ## Implementation Details (and Notes for Reviewers) - For Device Assembly View, I Implemented `MlirAsmParser` for parsing MLIR assembly. Technically MLIR is not an assembly language, but there is no better choice to make the source line map work w/ device view. - I Implemented `MlirPassDumpParser` for processing MLIR optimization pass dumps. I tried to subclass `LlvmPassDumpParser`, but they turn out to be too different to worth doing it. - `LlvmPassDumpParser` made some assumptions that do not hold true for MLIR passed. Some effort is put to make sure that the passes are properly diff-ed, since some passes can run multiple times and also sometimes pass can be nested (i.e., some number of `before`s followed by some number of `after`s) - A lot of effort is put into `patch_triton` to make sure that the we only compile the kernel without actually running it, and that needs to work across all the versions we support. ## Steps to Run Locally 1. Clone https://github.com/ShawnZhong/compiler-explorer-infra.git 2. Install Triton to `/opt/compiler-explorer/triton`: ```sh $ cd compiler-explorer-infra $ ./bin/ce_install install triton $ ls /opt/compiler-explorer/triton # v2.2.0 v2.3.0 v2.3.1 v3.0.0 v3.1.0 v3.2.0 v3.3.0 v3.3.1 ``` 3. Clone https://github.com/ShawnZhong/compiler-explorer.git and checkout branch `triton` 4. Run Compiler Explorer ```sh make EXTRA_ARGS='--language triton' dev ``` 5. Enjoy --------- Co-authored-by: Matt Godbolt <matt@godbolt.org>
147 lines
6.0 KiB
TypeScript
147 lines
6.0 KiB
TypeScript
// Copyright (c) 2025, Compiler Explorer Authors
|
|
// All rights reserved.
|
|
//
|
|
// Redistribution and use in source and binary forms, with or without
|
|
// modification, are permitted provided that the following conditions are met:
|
|
//
|
|
// * Redistributions of source code must retain the above copyright notice,
|
|
// this list of conditions and the following disclaimer.
|
|
// * Redistributions in binary form must reproduce the above copyright
|
|
// notice, this list of conditions and the following disclaimer in the
|
|
// documentation and/or other materials provided with the distribution.
|
|
//
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
// POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
import {AsmResultSource, ParsedAsmResult, ParsedAsmResultLine} from '../../types/asmresult/asmresult.interfaces.js';
|
|
import {ParseFiltersAndOutputOptions} from '../../types/features/filters.interfaces.js';
|
|
import * as utils from '../utils.js';
|
|
|
|
import {AsmParser} from './asm-parser.js';
|
|
|
|
export class MlirAsmParser extends AsmParser {
|
|
protected locDefRegex: RegExp;
|
|
protected locDefUnknownRegex: RegExp;
|
|
protected locRefRegex: RegExp;
|
|
protected locRefRegexReplace: RegExp;
|
|
protected inlineLocRegex: RegExp;
|
|
protected inlineLocRegexReplace: RegExp;
|
|
|
|
constructor() {
|
|
super();
|
|
|
|
// Match location definitions like #loc1 = loc("/path/to/file":line:column)
|
|
this.locDefRegex = /^#(\w+)\s*=\s*loc\("([^"]+)":(\d+):(\d+)\)/;
|
|
|
|
// Match location definitions like #loc1 = loc(unknown)
|
|
this.locDefUnknownRegex = /^#(\w+)\s*=\s*loc\(unknown\)/;
|
|
|
|
// Match location references like loc(#loc1)
|
|
this.locRefRegex = /\s*loc\(#(\w+)\)/;
|
|
this.locRefRegexReplace = new RegExp(this.locRefRegex.source, 'g');
|
|
|
|
// Match inline locations like loc("/path/to/file":line:column)
|
|
this.inlineLocRegex = /\s*loc\("([^"]+)":(\d+):(\d+)\)/;
|
|
this.inlineLocRegexReplace = new RegExp(this.inlineLocRegex.source, 'g');
|
|
}
|
|
|
|
override processAsm(asmResult: string, filters: ParseFiltersAndOutputOptions): ParsedAsmResult {
|
|
const startTime = process.hrtime.bigint();
|
|
|
|
const asm: ParsedAsmResultLine[] = [];
|
|
const asmLines = utils.splitLines(asmResult);
|
|
const startingLineCount = asmLines.length;
|
|
|
|
// First pass: extract all location definitions
|
|
const locationMap = new Map<string, AsmResultSource>();
|
|
for (const line of asmLines) {
|
|
const locMatch = line.match(this.locDefRegex);
|
|
if (locMatch) {
|
|
const locId = locMatch[1];
|
|
const file = locMatch[2];
|
|
const lineNum = Number.parseInt(locMatch[3], 10);
|
|
const column = Number.parseInt(locMatch[4], 10);
|
|
|
|
locationMap.set(locId, {
|
|
file: utils.maskRootdir(file),
|
|
line: lineNum,
|
|
column: column,
|
|
mainsource: true,
|
|
});
|
|
}
|
|
}
|
|
|
|
// Second pass: process each line and associate with source information
|
|
for (const line of asmLines) {
|
|
// Skip location definition lines
|
|
if (this.locDefRegex.test(line) || this.locDefUnknownRegex.test(line)) {
|
|
continue;
|
|
}
|
|
|
|
// Apply filters if needed
|
|
let processedLine = line;
|
|
if (filters.trim) {
|
|
processedLine = processedLine.trim();
|
|
}
|
|
|
|
if (filters.commentOnly && processedLine.trim().startsWith('//')) {
|
|
continue;
|
|
}
|
|
|
|
// Find source information from location references
|
|
let source: AsmResultSource | null = null;
|
|
|
|
// Check for location references like loc(#loc1)
|
|
const locRefMatch = line.match(this.locRefRegex);
|
|
if (locRefMatch) {
|
|
const locId = locRefMatch[1];
|
|
source = locationMap.get(locId) || null;
|
|
// Remove location reference from the displayed text
|
|
processedLine = processedLine.replace(this.locRefRegexReplace, '');
|
|
} else {
|
|
// Check for inline locations like loc("/path/to/file":line:column)
|
|
const inlineLocMatch = line.match(this.inlineLocRegex);
|
|
if (inlineLocMatch) {
|
|
const file = inlineLocMatch[1];
|
|
const lineNum = Number.parseInt(inlineLocMatch[2], 10);
|
|
const column = Number.parseInt(inlineLocMatch[3], 10);
|
|
|
|
source = {
|
|
file: utils.maskRootdir(file),
|
|
line: lineNum,
|
|
column: column,
|
|
mainsource: true,
|
|
};
|
|
}
|
|
// Remove inline location from the displayed text
|
|
processedLine = processedLine.replace(this.inlineLocRegexReplace, '');
|
|
}
|
|
|
|
// Add the line to the result
|
|
asm.push({
|
|
text: processedLine,
|
|
source: source,
|
|
labels: [],
|
|
});
|
|
}
|
|
|
|
const endTime = process.hrtime.bigint();
|
|
return {
|
|
asm: asm,
|
|
labelDefinitions: {},
|
|
languageId: 'mlir',
|
|
parsingTime: utils.deltaTimeNanoToMili(startTime, endTime),
|
|
filteredCount: startingLineCount - asm.length,
|
|
};
|
|
}
|
|
}
|