Skip to content

Commit 4f08056

Browse files
authored
[CIR] Provide syntax highlighting support of clang ir for vim/nvim users (#1854)
Hi there, I'm opening the PR to add syntax highlighting support for vim and neovim users. The PR borrows a lot from MLIR but also add - Dedicated CIR types like bytes - Dedicated CIR keywords(?!) like coroutine, resume, suspend, etc etc. - Blanket operation highlighting for CIR operations (No need to edit syntax/cir.vim again for new operations) Below is the before and after of applying syntax highlighting. Before: <img width="1532" height="1003" alt="Screenshot 2025-08-22 at 3 30 44 PM" src="https://github.com/user-attachments/assets/b94f8965-99db-41b0-a3e2-c004b4dfd6b8" /> After: <img width="1532" height="1003" alt="Screenshot 2025-08-22 at 3 30 21 PM" src="https://github.com/user-attachments/assets/e25448de-8dc3-4df3-88e3-35143a06c53a" />
1 parent 5c69e08 commit 4f08056

File tree

2 files changed

+129
-0
lines changed

2 files changed

+129
-0
lines changed

mlir/utils/vim/ftdetect/cir.vim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
au BufRead,BufNewFile *.cir set filetype=cir

mlir/utils/vim/syntax/cir.vim

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
" Vim syntax file
2+
" Language: cir
3+
" Maintainer: The CIR team
4+
" Version: $Revision$
5+
" Some parts adapted from the LLVM and MLIR vim syntax file.
6+
7+
if version < 600
8+
syntax clear
9+
elseif exists("b:current_syntax")
10+
finish
11+
endif
12+
13+
syn case match
14+
15+
" Types.
16+
"
17+
syn keyword mlirType index
18+
syn match mlirType /\<f\d\+\>/
19+
syn match mlirType /\<bf\d\+\>/
20+
" Signless integer types.
21+
syn match mlirType /\<i\d\+\>/
22+
" Unsigned integer types.
23+
syn match mlirType /\<ui\d\+\>/
24+
" Signed integer types.
25+
syn match mlirType /\<si\d\+\>/
26+
27+
" Elemental types inside memref, tensor, or vector types.
28+
syn match mlirType /x\s*\zs\(bf16|f16\|f32\|f64\|i\d\+\|ui\d\+\|si\d\+\)/
29+
30+
" Shaped types.
31+
syn match mlirType /\<memref\ze\s*<.*>/
32+
syn match mlirType /\<tensor\ze\s*<.*>/
33+
syn match mlirType /\<vector\ze\s*<.*>/
34+
35+
" vector types inside memref or tensor.
36+
syn match mlirType /x\s*\zsvector/
37+
38+
" Misc syntax.
39+
40+
syn match mlirNumber /-\?\<\d\+\>/
41+
" Match numbers even in shaped types.
42+
syn match mlirNumber /-\?\<\d\+\ze\s*x/
43+
syn match mlirNumber /x\s*\zs-\?\d\+\ze\s*x/
44+
45+
syn match mlirFloat /-\?\<\d\+\.\d*\(e[+-]\d\+\)\?\>/
46+
syn match mlirFloat /\<0x\x\+\>/
47+
syn keyword mlirBoolean true false
48+
" Spell checking is enabled only in comments by default.
49+
syn match mlirComment /\/\/.*$/ contains=@Spell
50+
syn region mlirString start=/"/ skip=/\\"/ end=/"/
51+
syn match mlirLabel /[-a-zA-Z$._][-a-zA-Z$._0-9]*:/
52+
" Prefixed identifiers usually used for ssa values and symbols.
53+
syn match mlirIdentifier /[%@][a-zA-Z$._-][a-zA-Z0-9$._-]*/
54+
syn match mlirIdentifier /[%@]\d\+\>/
55+
" Prefixed identifiers usually used for blocks.
56+
syn match mlirBlockIdentifier /\^[a-zA-Z$._-][a-zA-Z0-9$._-]*/
57+
syn match mlirBlockIdentifier /\^\d\+\>/
58+
" Prefixed identifiers usually used for types.
59+
syn match mlirTypeIdentifier /![a-zA-Z$._-][a-zA-Z0-9$._-]*/
60+
syn match mlirTypeIdentifier /!\d\+\>/
61+
" Prefixed identifiers usually used for attribute aliases and result numbers.
62+
syn match mlirAttrIdentifier /#[a-zA-Z$._-][a-zA-Z0-9$._-]*/
63+
syn match mlirAttrIdentifier /#\d\+\>/
64+
65+
" Syntax-highlight lit test commands and bug numbers.
66+
" Old highlighting version of MLIR is faulty, any misamount of whitespace before or after on a line
67+
" will not highlight the special comments anymore.
68+
syn match mlirSpecialComment /^\s*\/\/\s*RUN:.*$/
69+
syn match mlirSpecialComment /^\s*\/\/\s*\(CHECK\|CIR\|MLIR\|LLVM\|OGCG\):.*$/
70+
syn match mlirSpecialComment /^\s*\/\/\s*\(CHECK\|CIR\|MLIR\|LLVM\|OGCG\)-\(NEXT\|NOT\|DAG\|SAME\|LABEL\):.*$/
71+
syn match mlirSpecialComment /^\s*\/\/\s*expected-error.*$/
72+
syn match mlirSpecialComment /^\s*\/\/\s*expected-remark.*$/
73+
syn match mlirSpecialComment /^\s*;\s*XFAIL:.*$/
74+
syn match mlirSpecialComment /^\s*\/\/\s*PR\d*\s*$/
75+
syn match mlirSpecialComment /^\s*\/\/\s*REQUIRES:.*$/
76+
77+
"""""""""""" CIR SECTION """"""""""""
78+
" CIR blanket operations
79+
syn match cirOps /\vcir(\.\w+)+/
80+
81+
syn keyword cirKeyword
82+
\ module attributes
83+
\ from to cond body step
84+
\ align alignment init
85+
\ cond exception
86+
\ null
87+
\ coroutine suspend resume cleanup
88+
\ class union struct
89+
\ do while
90+
\ if then else
91+
\ nsw nuw
92+
\ constant
93+
\ equal default anyof
94+
\ internal external available_externally private linkonce linkonce_odr
95+
\ weak weak_odr extern_weak common cir_private
96+
97+
98+
syn keyword cirType bytes
99+
100+
101+
if version >= 508 || !exists("did_c_syn_inits")
102+
if version < 508
103+
let did_c_syn_inits = 1
104+
command -nargs=+ HiLink hi link <args>
105+
else
106+
command -nargs=+ HiLink hi def link <args>
107+
endif
108+
109+
HiLink mlirType Type
110+
HiLink mlirNumber Number
111+
HiLink mlirComment Comment
112+
HiLink mlirString String
113+
HiLink mlirLabel Label
114+
HiLink mlirBoolean Boolean
115+
HiLink mlirFloat Float
116+
HiLink mlirSpecialComment SpecialComment
117+
HiLink mlirIdentifier Identifier
118+
HiLink mlirBlockIdentifier Label
119+
HiLink mlirTypeIdentifier Type
120+
HiLink mlirAttrIdentifier PreProc
121+
122+
HiLink cirType Type
123+
HiLink cirOps Statement
124+
HiLink cirKeyword Keyword
125+
delcommand HiLink
126+
endif
127+
128+
let b:current_syntax = "cir"

0 commit comments

Comments
 (0)