From 13decfd048e7dcafa36a81b429a34ee20d94e55a Mon Sep 17 00:00:00 2001 From: Jasmine Tang Date: Fri, 22 Aug 2025 15:34:31 -0700 Subject: [PATCH 1/4] Provide syntax highlighting support of clang ir for vim/nvim users --- mlir/utils/vim/ftdetect/cir.vim | 1 + mlir/utils/vim/syntax/cir.vim | 165 ++++++++++++++++++++++++++++++++ 2 files changed, 166 insertions(+) create mode 100644 mlir/utils/vim/ftdetect/cir.vim create mode 100644 mlir/utils/vim/syntax/cir.vim diff --git a/mlir/utils/vim/ftdetect/cir.vim b/mlir/utils/vim/ftdetect/cir.vim new file mode 100644 index 000000000000..fc53cf3e4b7c --- /dev/null +++ b/mlir/utils/vim/ftdetect/cir.vim @@ -0,0 +1 @@ +au BufRead,BufNewFile *.cir set filetype=cir diff --git a/mlir/utils/vim/syntax/cir.vim b/mlir/utils/vim/syntax/cir.vim new file mode 100644 index 000000000000..23cd3af8e258 --- /dev/null +++ b/mlir/utils/vim/syntax/cir.vim @@ -0,0 +1,165 @@ +" Vim syntax file +" Language: cir +" Maintainer: The CIR team +" Version: $Revision$ +" Some parts adapted from the LLVM and MLIR vim syntax file. + +if version < 600 + syntax clear +elseif exists("b:current_syntax") + finish +endif + +syn case match + +" Types. +" +syn keyword mlirType index f16 f32 f64 bf16 +" Signless integer types. +syn match mlirType /\/ +" Unsigned integer types. +syn match mlirType /\/ +" Signed integer types. +syn match mlirType /\/ + +" Elemental types inside memref, tensor, or vector types. +syn match mlirType /x\s*\zs\(bf16|f16\|f32\|f64\|i\d\+\|ui\d\+\|si\d\+\)/ + +" Shaped types. +syn match mlirType /\/ +syn match mlirType /\/ +syn match mlirType /\/ + +" vector types inside memref or tensor. +syn match mlirType /x\s*\zsvector/ + +" Operations. +" TODO: this list is not exhaustive. +syn keyword mlirOps alloc alloca addf addi and call call_indirect cmpf cmpi +syn keyword mlirOps constant dealloc divf dma_start dma_wait dim exp +syn keyword mlirOps getTensor index_cast load log memref_cast +syn keyword mlirOps memref_shape_cast mulf muli negf powf prefetch rsqrt sitofp +syn keyword mlirOps splat store select sqrt subf subi subview tanh +syn keyword mlirOps view + +" Math ops. +syn match mlirOps /\/ +syn match mlirOps /\/ + +" Affine ops. +syn match mlirOps /\/ +syn match mlirOps /\/ +syn match mlirOps /\/ +syn match mlirOps /\/ +syn match mlirOps /\/ +syn match mlirOps /\/ +syn match mlirOps /\/ +syn match mlirOps /\/ +syn match mlirOps /\/ +syn match mlirOps /\/ +syn match mlirOps /\/ +syn match mlirOps /\/ +syn match mlirOps /\/ + + +" TODO: dialect name prefixed ops (llvm or std). + +" Keywords. +syn keyword mlirKeyword + \ affine_map + \ affine_set + \ dense + \ else + \ func + \ module + \ return + \ step + \ to + +" Misc syntax. + +syn match mlirNumber /-\?\<\d\+\>/ +" Match numbers even in shaped types. +syn match mlirNumber /-\?\<\d\+\ze\s*x/ +syn match mlirNumber /x\s*\zs-\?\d\+\ze\s*x/ + +syn match mlirFloat /-\?\<\d\+\.\d*\(e[+-]\d\+\)\?\>/ +syn match mlirFloat /\<0x\x\+\>/ +syn keyword mlirBoolean true false +" Spell checking is enabled only in comments by default. +syn match mlirComment /\/\/.*$/ contains=@Spell +syn region mlirString start=/"/ skip=/\\"/ end=/"/ +syn match mlirLabel /[-a-zA-Z$._][-a-zA-Z$._0-9]*:/ +" Prefixed identifiers usually used for ssa values and symbols. +syn match mlirIdentifier /[%@][a-zA-Z$._-][a-zA-Z0-9$._-]*/ +syn match mlirIdentifier /[%@]\d\+\>/ +" Prefixed identifiers usually used for blocks. +syn match mlirBlockIdentifier /\^[a-zA-Z$._-][a-zA-Z0-9$._-]*/ +syn match mlirBlockIdentifier /\^\d\+\>/ +" Prefixed identifiers usually used for types. +syn match mlirTypeIdentifier /![a-zA-Z$._-][a-zA-Z0-9$._-]*/ +syn match mlirTypeIdentifier /!\d\+\>/ +" Prefixed identifiers usually used for attribute aliases and result numbers. +syn match mlirAttrIdentifier /#[a-zA-Z$._-][a-zA-Z0-9$._-]*/ +syn match mlirAttrIdentifier /#\d\+\>/ + +" Syntax-highlight lit test commands and bug numbers. +syn match mlirSpecialComment /\/\/\s*RUN:.*$/ +syn match mlirSpecialComment /\/\/\s*CHECK:.*$/ +syn match mlirSpecialComment "\v\/\/\s*CHECK-(NEXT|NOT|DAG|SAME|LABEL):.*$" +syn match mlirSpecialComment /\/\/\s*expected-error.*$/ +syn match mlirSpecialComment /\/\/\s*expected-remark.*$/ +syn match mlirSpecialComment /;\s*XFAIL:.*$/ +syn match mlirSpecialComment /\/\/\s*PR\d*\s*$/ +syn match mlirSpecialComment /\/\/\s*REQUIRES:.*$/ + +"""""""""""" CIR SECTION """""""""""" +" CIR blanket operations +syn match cirOps /\vcir(\.\w+)+/ + +syn keyword cirKeyword + \ from to cond body step + \ align alignment init + \ cond exception + \ null + \ coroutine suspend resume cleanup + \ class union struct + \ do while + \ if then else + \ nsw nuw + \ equal default anyof + \ internal external private linkonce_odr + +syn keyword cirType bytes + +if version >= 508 || !exists("did_c_syn_inits") + if version < 508 + let did_c_syn_inits = 1 + command -nargs=+ HiLink hi link + else + command -nargs=+ HiLink hi def link + endif + + HiLink mlirType Type + HiLink mlirOps Statement + HiLink mlirNumber Number + HiLink mlirComment Comment + HiLink mlirString String + HiLink mlirLabel Label + HiLink mlirKeyword Keyword + HiLink mlirBoolean Boolean + HiLink mlirFloat Float + HiLink mlirConstant Constant + HiLink mlirSpecialComment SpecialComment + HiLink mlirIdentifier Identifier + HiLink mlirBlockIdentifier Label + HiLink mlirTypeIdentifier Type + HiLink mlirAttrIdentifier PreProc + + HiLink cirType Type + HiLink cirOps Statement + HiLink cirKeyword Keyword + delcommand HiLink +endif + +let b:current_syntax = "cir" From 1db325c29748d3bea11995159a7b446469a6bb14 Mon Sep 17 00:00:00 2001 From: Jasmine Tang Date: Mon, 25 Aug 2025 11:18:58 -0700 Subject: [PATCH 2/4] Improve highlighting for comments, add constant keyword, remove redundant mlir highlighting --- mlir/utils/vim/syntax/cir.vim | 65 +++++++---------------------------- 1 file changed, 13 insertions(+), 52 deletions(-) diff --git a/mlir/utils/vim/syntax/cir.vim b/mlir/utils/vim/syntax/cir.vim index 23cd3af8e258..d98143d62b61 100644 --- a/mlir/utils/vim/syntax/cir.vim +++ b/mlir/utils/vim/syntax/cir.vim @@ -33,49 +33,6 @@ syn match mlirType /\/ " vector types inside memref or tensor. syn match mlirType /x\s*\zsvector/ -" Operations. -" TODO: this list is not exhaustive. -syn keyword mlirOps alloc alloca addf addi and call call_indirect cmpf cmpi -syn keyword mlirOps constant dealloc divf dma_start dma_wait dim exp -syn keyword mlirOps getTensor index_cast load log memref_cast -syn keyword mlirOps memref_shape_cast mulf muli negf powf prefetch rsqrt sitofp -syn keyword mlirOps splat store select sqrt subf subi subview tanh -syn keyword mlirOps view - -" Math ops. -syn match mlirOps /\/ -syn match mlirOps /\/ - -" Affine ops. -syn match mlirOps /\/ -syn match mlirOps /\/ -syn match mlirOps /\/ -syn match mlirOps /\/ -syn match mlirOps /\/ -syn match mlirOps /\/ -syn match mlirOps /\/ -syn match mlirOps /\/ -syn match mlirOps /\/ -syn match mlirOps /\/ -syn match mlirOps /\/ -syn match mlirOps /\/ -syn match mlirOps /\/ - - -" TODO: dialect name prefixed ops (llvm or std). - -" Keywords. -syn keyword mlirKeyword - \ affine_map - \ affine_set - \ dense - \ else - \ func - \ module - \ return - \ step - \ to - " Misc syntax. syn match mlirNumber /-\?\<\d\+\>/ @@ -104,14 +61,16 @@ syn match mlirAttrIdentifier /#[a-zA-Z$._-][a-zA-Z0-9$._-]*/ syn match mlirAttrIdentifier /#\d\+\>/ " Syntax-highlight lit test commands and bug numbers. -syn match mlirSpecialComment /\/\/\s*RUN:.*$/ -syn match mlirSpecialComment /\/\/\s*CHECK:.*$/ -syn match mlirSpecialComment "\v\/\/\s*CHECK-(NEXT|NOT|DAG|SAME|LABEL):.*$" -syn match mlirSpecialComment /\/\/\s*expected-error.*$/ -syn match mlirSpecialComment /\/\/\s*expected-remark.*$/ -syn match mlirSpecialComment /;\s*XFAIL:.*$/ -syn match mlirSpecialComment /\/\/\s*PR\d*\s*$/ -syn match mlirSpecialComment /\/\/\s*REQUIRES:.*$/ +" Old highlighting version of MLIR is faulty, any misamount of whitespace before or after on a line +" will not highlight the special comments anymore. +syn match mlirSpecialComment /^\s*\/\/\s*RUN:.*$/ +syn match mlirSpecialComment /^\s*\/\/\s*\(CHECK\|MLIR\|LLVM\|OGCG\):.*$/ +syn match mlirSpecialComment /^\s*\/\/\s*\(CHECK\|MLIR\|LLVM\|OGCG\)-\(NEXT\|NOT\|DAG\|SAME\|LABEL\):.*$/ +syn match mlirSpecialComment /^\s*\/\/\s*expected-error.*$/ +syn match mlirSpecialComment /^\s*\/\/\s*expected-remark.*$/ +syn match mlirSpecialComment /^\s*;\s*XFAIL:.*$/ +syn match mlirSpecialComment /^\s*\/\/\s*PR\d*\s*$/ +syn match mlirSpecialComment /^\s*\/\/\s*REQUIRES:.*$/ """""""""""" CIR SECTION """""""""""" " CIR blanket operations @@ -127,11 +86,14 @@ syn keyword cirKeyword \ do while \ if then else \ nsw nuw + \ constant \ equal default anyof \ internal external private linkonce_odr + syn keyword cirType bytes + if version >= 508 || !exists("did_c_syn_inits") if version < 508 let did_c_syn_inits = 1 @@ -141,7 +103,6 @@ if version >= 508 || !exists("did_c_syn_inits") endif HiLink mlirType Type - HiLink mlirOps Statement HiLink mlirNumber Number HiLink mlirComment Comment HiLink mlirString String From bfd744655cecc5b51f0e385bd5e20daf9cfbe757 Mon Sep 17 00:00:00 2001 From: Jasmine Tang Date: Mon, 25 Aug 2025 11:23:06 -0700 Subject: [PATCH 3/4] Remove redundant highlighting group --- mlir/utils/vim/syntax/cir.vim | 2 -- 1 file changed, 2 deletions(-) diff --git a/mlir/utils/vim/syntax/cir.vim b/mlir/utils/vim/syntax/cir.vim index d98143d62b61..e10fb8eadc70 100644 --- a/mlir/utils/vim/syntax/cir.vim +++ b/mlir/utils/vim/syntax/cir.vim @@ -107,10 +107,8 @@ if version >= 508 || !exists("did_c_syn_inits") HiLink mlirComment Comment HiLink mlirString String HiLink mlirLabel Label - HiLink mlirKeyword Keyword HiLink mlirBoolean Boolean HiLink mlirFloat Float - HiLink mlirConstant Constant HiLink mlirSpecialComment SpecialComment HiLink mlirIdentifier Identifier HiLink mlirBlockIdentifier Label From 5dd4198a40185e148ef83415d80ab9ae1876c5a5 Mon Sep 17 00:00:00 2001 From: Jasmine Tang Date: Tue, 26 Aug 2025 15:53:37 -0700 Subject: [PATCH 4/4] Update float coloring, added extra keywords --- mlir/utils/vim/syntax/cir.vim | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/mlir/utils/vim/syntax/cir.vim b/mlir/utils/vim/syntax/cir.vim index e10fb8eadc70..4540bce244b9 100644 --- a/mlir/utils/vim/syntax/cir.vim +++ b/mlir/utils/vim/syntax/cir.vim @@ -14,7 +14,9 @@ syn case match " Types. " -syn keyword mlirType index f16 f32 f64 bf16 +syn keyword mlirType index +syn match mlirType /\/ +syn match mlirType /\/ " Signless integer types. syn match mlirType /\/ " Unsigned integer types. @@ -64,8 +66,8 @@ syn match mlirAttrIdentifier /#\d\+\>/ " Old highlighting version of MLIR is faulty, any misamount of whitespace before or after on a line " will not highlight the special comments anymore. syn match mlirSpecialComment /^\s*\/\/\s*RUN:.*$/ -syn match mlirSpecialComment /^\s*\/\/\s*\(CHECK\|MLIR\|LLVM\|OGCG\):.*$/ -syn match mlirSpecialComment /^\s*\/\/\s*\(CHECK\|MLIR\|LLVM\|OGCG\)-\(NEXT\|NOT\|DAG\|SAME\|LABEL\):.*$/ +syn match mlirSpecialComment /^\s*\/\/\s*\(CHECK\|CIR\|MLIR\|LLVM\|OGCG\):.*$/ +syn match mlirSpecialComment /^\s*\/\/\s*\(CHECK\|CIR\|MLIR\|LLVM\|OGCG\)-\(NEXT\|NOT\|DAG\|SAME\|LABEL\):.*$/ syn match mlirSpecialComment /^\s*\/\/\s*expected-error.*$/ syn match mlirSpecialComment /^\s*\/\/\s*expected-remark.*$/ syn match mlirSpecialComment /^\s*;\s*XFAIL:.*$/ @@ -77,6 +79,7 @@ syn match mlirSpecialComment /^\s*\/\/\s*REQUIRES:.*$/ syn match cirOps /\vcir(\.\w+)+/ syn keyword cirKeyword + \ module attributes \ from to cond body step \ align alignment init \ cond exception @@ -88,7 +91,8 @@ syn keyword cirKeyword \ nsw nuw \ constant \ equal default anyof - \ internal external private linkonce_odr + \ internal external available_externally private linkonce linkonce_odr + \ weak weak_odr extern_weak common cir_private syn keyword cirType bytes