CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
136 lines
5.3 KiB
Python
136 lines
5.3 KiB
Python
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
# See https://llvm.org/LICENSE.txt for license information.
|
|
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
from pygments.lexer import RegexLexer, bygroups, include, using
|
|
from pygments.token import *
|
|
import re
|
|
|
|
__all__ = ["MlirLexer"]
|
|
|
|
|
|
class MlirLexer(RegexLexer):
|
|
"""Pygments lexer for MLIR.
|
|
|
|
This lexer focuses on accurate tokenization of common MLIR constructs:
|
|
- SSA values (%%... / %...)
|
|
- attribute and type aliases (#name =, !name =)
|
|
- types (builtin and dialect types, parametric types)
|
|
- attribute dictionaries and nested containers to a reasonable depth
|
|
- numbers (ints, floats with exponents, hex)
|
|
- strings with common escapes
|
|
- line comments (// ...)
|
|
- block labels (^foo) and operations
|
|
"""
|
|
|
|
name = "MLIR"
|
|
aliases = ["mlir"]
|
|
filenames = ["*.mlir"]
|
|
|
|
flags = re.MULTILINE
|
|
|
|
class VariableList(RegexLexer):
|
|
"""Lexer for lists of SSA variables separated by commas."""
|
|
|
|
tokens = {
|
|
"root": [
|
|
(r"\s+", Text),
|
|
(r",", Punctuation),
|
|
(r"%[_A-Za-z0-9\.\$\-:#]+", Name.Variable),
|
|
]
|
|
}
|
|
|
|
tokens = {
|
|
"root": [
|
|
# Comments
|
|
(r"//.*?$", Comment.Single),
|
|
# operation name with assignment: %... = op.name
|
|
(
|
|
r"^(\s*)(%[\%_A-Za-z0-9\:#\,\s]+)(=)(\s*)([A-Za-z0-9_\.\$\-]+)\b",
|
|
bygroups(Text, using(VariableList), Operator, Text, Name.Builtin),
|
|
),
|
|
# operation name without result
|
|
(r"^(\s*)([A-Za-z0-9_\.\$\-]+)\b(?=[^<:])", bygroups(Text, Name.Builtin)),
|
|
# Attribute alias definition: #name =
|
|
(
|
|
r"^(\s*)(#[_A-Za-z0-9\$\-\.]+)(\b)(\s*=)",
|
|
bygroups(Text, Name.Constant, Text, Operator),
|
|
),
|
|
# Type alias definition: !name =
|
|
(
|
|
r"^(\s*)(![_A-Za-z0-9\$\-\.]+)(\b)(\s*=)",
|
|
bygroups(Text, Keyword.Type, Text, Operator),
|
|
),
|
|
# SSA values (uses)
|
|
(r"%[_A-Za-z0-9\.\$\-:#]+", Name.Variable),
|
|
# attribute refs, constants and named attributes
|
|
(r"#[_A-Za-z0-9\$\-\.]+\b", Name.Constant),
|
|
# symbol refs / function-like names
|
|
(r"@[_A-Za-z][_A-Za-z0-9\$\-\.]*\b", Name.Function),
|
|
# blocks
|
|
(r"\^[A-Za-z0-9_\$\.\-]+", Name.Label),
|
|
# types by exclamation or builtin names
|
|
(r"![_A-Za-z0-9\$\-\.]+\b", Keyword.Type),
|
|
# NOTE: please sync changes to corresponding builtin type rule in "angled-type"
|
|
(r"\b(bf16|f16|f32|f64|f80|f128|index|none|(u|s)?i[0-9]+)\b", Keyword.Type),
|
|
# container-like dialect types (tensor<...>, memref<...>, vector<...>)
|
|
(
|
|
r"\b(complex|memref|tensor|tuple|vector)\s*(<)",
|
|
bygroups(Keyword.Type, Punctuation),
|
|
"angled-type",
|
|
),
|
|
# affine constructs
|
|
(r"\b(affine_map|affine_set)\b", Keyword.Reserved),
|
|
# common builtin operators / functions inside affine_map
|
|
(r"\b(ceildiv|floordiv|mod|symbol)\b", Name.Other),
|
|
# identifiers / bare words
|
|
(r"\b[_A-Za-z][_A-Za-z0-9\.-]*\b", Name.Other),
|
|
# numbers: hex, float (with exponent), integer
|
|
(r"\b0x[0-9A-Fa-f]+\b", Number.Hex),
|
|
(r"\b([0-9]+(\.[0-9]*)?|\.[0-9]+)([eE][+-]?[0-9]+)?\b", Number.Float),
|
|
(r"\b[0-9]+\b", Number.Integer),
|
|
# strings
|
|
(r'"', String.Double, "string"),
|
|
# punctuation and arrow-like tokens
|
|
(r"->|>=|<=|\>=|\<=|\->|\=>", Operator),
|
|
(r"[()\[\]{}<>,.:=]", Punctuation),
|
|
# operators
|
|
(r"[-+*/%]", Operator),
|
|
],
|
|
# string state with common escapes
|
|
"string": [
|
|
(r'\\[ntr"\\]', String.Escape),
|
|
(r'[^"\\]+', String.Double),
|
|
(r'"', String.Double, "#pop"),
|
|
],
|
|
# angled-type content
|
|
"angled-type": [
|
|
# match nested '<' and '>'
|
|
(r"<", Punctuation, "#push"),
|
|
(r">", Punctuation, "#pop"),
|
|
# dimensions like 3x or 3x3x... and standalone numbers:
|
|
# - match numbers that are followed by an 'x' (dimension separator)
|
|
(r"([0-9]+)(?=(?:x))", Number.Integer),
|
|
# - match bare numbers (sizes)
|
|
(r"[0-9]+", Number.Integer),
|
|
# dynamic dimension '?'
|
|
(r"\?", Name.Integer),
|
|
# the 'x' dimension separator (treat as punctuation)
|
|
(r"x", Punctuation),
|
|
# element / builtin types inside angle brackets (no word-boundary)
|
|
# NOTE: please sync changes to corresponding builtin type rule in "root"
|
|
(
|
|
r"(?:bf16|f16|f32|f64|f80|f128|index|none|(?:[us]?i[0-9]+))",
|
|
Keyword.Type,
|
|
),
|
|
# also allow nested container-like types to be recognized
|
|
(
|
|
r"\b(complex|memref|tensor|tuple|vector)\s*(<)",
|
|
bygroups(Keyword.Type, Punctuation),
|
|
"angled-type",
|
|
),
|
|
# fall back to root rules for anything else
|
|
include("root"),
|
|
],
|
|
}
|