Add vim scripts for indent/syntax
authorUday Bondhugula <udayb@iisc.ac.in>
Thu, 29 Aug 2019 06:22:39 +0000 (23:22 -0700)
committerA. Unique TensorFlower <gardener@tensorflow.org>
Thu, 29 Aug 2019 06:23:19 +0000 (23:23 -0700)
- some of it has been adapted from LLVM's vim utils

Signed-off-by: Uday Bondhugula <uday@polymagelabs.com>
Closes tensorflow/mlir#90

COPYBARA_INTEGRATE_REVIEW=https://github.com/tensorflow/mlir/pull/90 from bondhugula:vim 22b1c958818c4b09de0ec8e1d7a4893171a03dbf
PiperOrigin-RevId: 266071752

mlir/utils/vim/README [new file with mode: 0644]
mlir/utils/vim/ftdetect/mlir.vim [new file with mode: 0644]
mlir/utils/vim/ftplugin/mlir.vim [new file with mode: 0644]
mlir/utils/vim/indent/mlir.vim [new file with mode: 0644]
mlir/utils/vim/mlir.vim [deleted file]
mlir/utils/vim/syntax/mlir.vim [new file with mode: 0644]

diff --git a/mlir/utils/vim/README b/mlir/utils/vim/README
new file mode 100644 (file)
index 0000000..d9a1f7f
--- /dev/null
@@ -0,0 +1,9 @@
+-*- mlir/utils/vim/README -*-
+
+This directory contains settings for the vim editor to work on MLIR *.mlir 
+files.  It comes with filetype detection rules in the (ftdetect),
+syntax highlighting (syntax), some minimal sensible default settings (ftplugin)
+and indentation plugins (indent).
+
+To install, copy all subdirectories to your $HOME/.vim/, or if you
+prefer, create symlinks to the files here.
diff --git a/mlir/utils/vim/ftdetect/mlir.vim b/mlir/utils/vim/ftdetect/mlir.vim
new file mode 100644 (file)
index 0000000..f8b1de4
--- /dev/null
@@ -0,0 +1 @@
+au BufRead,BufNewFile *.mlir set filetype=mlir
diff --git a/mlir/utils/vim/ftplugin/mlir.vim b/mlir/utils/vim/ftplugin/mlir.vim
new file mode 100644 (file)
index 0000000..83b07f5
--- /dev/null
@@ -0,0 +1,12 @@
+" Vim filetype plugin file
+" Language: MLIR Assembly
+" Maintainer: The MLIR team
+
+if exists("b:did_ftplugin")
+  finish
+endif
+let b:did_ftplugin = 1
+
+setlocal softtabstop=2 shiftwidth=2
+setlocal expandtab
+setlocal comments+=://
diff --git a/mlir/utils/vim/indent/mlir.vim b/mlir/utils/vim/indent/mlir.vim
new file mode 100644 (file)
index 0000000..2c5b4ae
--- /dev/null
@@ -0,0 +1,75 @@
+" Vim indent file
+" Language:   mlir
+" Maintainer: The MLIR team
+" Adapted from the LLVM vim indent file
+" What this indent plugin currently does:
+"  - If no other rule matches copy indent from previous non-empty,
+"    non-commented line.
+"  - On '}' align the same as the line containing the matching '{'.
+"  - If previous line starts with a block label, increase indentation.
+"  - If the current line is a block label and ends with ':' indent at the same
+"    level as the enclosing '{'/'}' block.
+" Stuff that would be nice to add:
+"  - Continue comments on next line.
+"  - If there is an opening+unclosed parenthesis on previous line indent to
+"    that.
+if exists("b:did_indent")
+  finish
+endif
+let b:did_indent = 1
+
+setlocal shiftwidth=2 expandtab
+
+setlocal indentkeys=0{,0},<:>,!^F,o,O,e
+setlocal indentexpr=GetMLIRIndent()
+
+if exists("*GetMLIRIndent")
+  finish
+endif
+
+function! FindOpenBrace(lnum)
+  call cursor(a:lnum, 1)
+  return searchpair('{', '', '}', 'bW')
+endfun
+
+function! GetMLIRIndent()
+  " On '}' align the same as the line containing the matching '{'
+  let thisline = getline(v:lnum)
+  if thisline =~ '^\s*}'
+    call cursor(v:lnum, 1)
+    silent normal %
+    let opening_lnum = line('.')
+    if opening_lnum != v:lnum
+      return indent(opening_lnum)
+    endif
+  endif
+
+  " Indent labels the same as the current opening block
+  if thisline =~ '\^\h\+.*:\s*$'
+    let blockbegin = FindOpenBrace(v:lnum)
+    if blockbegin > 0
+      return indent(blockbegin)
+    endif
+  endif
+
+  " Find a non-blank not-completely commented line above the current line.
+  let prev_lnum = prevnonblank(v:lnum - 1)
+  while prev_lnum > 0 && synIDattr(synID(prev_lnum, 1 + indent(prev_lnum), 0), "name") == "mlirComment"
+    let prev_lnum = prevnonblank(prev_lnum-1)
+  endwhile
+  " Hit the start of the file, use zero indent.
+  if prev_lnum == 0
+    return 0
+  endif
+
+  let ind = indent(prev_lnum)
+  let prevline = getline(prev_lnum)
+
+  " Add a 'shiftwidth' after lines that start a function, block/labels, or a
+  " region.
+  if prevline =~ '{\s*$' || prevline =~ '\^\h\+.*:\s*$'
+    let ind = ind + &shiftwidth
+  endif
+
+  return ind
+endfunction
diff --git a/mlir/utils/vim/mlir.vim b/mlir/utils/vim/mlir.vim
deleted file mode 100644 (file)
index 18ff6fe..0000000
+++ /dev/null
@@ -1,51 +0,0 @@
-" Copyright 2019 The MLIR Authors.
-"
-" Licensed under the Apache License, Version 2.0 (the "License");
-" you may not use this file except in compliance with the License.
-" You may obtain a copy of the License at
-"
-"     http://www.apache.org/licenses/LICENSE-2.0
-"
-" Unless required by applicable law or agreed to in writing, software
-" distributed under the License is distributed on an "AS IS" BASIS,
-" WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-" See the License for the specific language governing permissions and
-" limitations under the License.
-
-" Vim syntax file
-" Language: MLIR
-
-" quit when a syntax file was already loaded
-if exists("b:current_syntax")
-  finish
-endif
-
-syn keyword mlirType index i1 i2 i4 i8 i13 i16 i32 i64
-      \ f16 f32 tf_control
-syn keyword mlirType memref tensor vector
-
-syntax keyword mlirKeywords extfunc cfgfunc mlfunc for to step return
-syntax keyword mlirConditional if else
-syntax keyword mlirCoreOps dim addf addi subf subi mulf muli cmpi select constant affine.apply call call_indirect extract_element getTensor memref_cast tensor_cast load store alloc dealloc dma_start dma_wait
-
-syn match mlirInt "-\=\<\d\+\>"
-syn match mlirFloat "-\=\<\d\+\.\d\+\>"
-syn match mlirMapOutline "#.*$"
-syn match mlirOperator      "[+\-*=]"
-
-syn region mlirComment start="//" skip="\\$" end="$"
-syn region mlirString matchgroup=mlirString start=+"+ end=+"+
-
-hi def link mlirComment      Comment
-hi def link mlirKeywords     Instruction
-hi def link mlirCoreOps      Instruction
-hi def link mlirInt          Constant
-hi def link mlirType         Type
-hi def link mlirMapOutline   PreProc
-hi def link mlirConditional  Conditional
-hi def link mlirString       String
-hi def link mlirOperator     Operator
-hi def link mlirInstruction  Operator
-hi def link mlirAffineOp     Operator
-
-let b:current_syntax = "mlir"
diff --git a/mlir/utils/vim/syntax/mlir.vim b/mlir/utils/vim/syntax/mlir.vim
new file mode 100644 (file)
index 0000000..41d277a
--- /dev/null
@@ -0,0 +1,95 @@
+" Vim syntax file
+" Language:   mlir
+" Maintainer: The MLIR team, http://github.com/tensorflow/mlir/
+" Version:      $Revision$
+" Adapted from the LLVM vim indent file
+
+if version < 600
+  syntax clear
+elseif exists("b:current_syntax")
+  finish
+endif
+
+syn case match
+
+" Types.
+syn keyword mlirType index f16 f32 f64
+" Integer type.
+syn match mlirType /\<i\d\+\>/
+" Primitive types inside memref, tensor, or vector.
+syn match mlirType /<.*x\s*\zs\(f16\|f32\|f64\|i\d\+\)/
+syn match mlirType /\<memref\ze<.*>/
+syn match mlirType /\<tensor\ze<.*>/
+syn match mlirType /\<vector\ze<.*>/
+
+" Operations.
+" Core ops (not exhaustive yet).
+" TODO: the list is not exhaustive.
+syn keyword mlirOps alloc addf addi call call_indirect cmpi constant dealloc dma_start dma_wait dim extract_element getTensor load memref_cast mulf muli store select subf subi tensor_cast
+
+" Affine ops.
+syn match mlirOps /\<affine\.apply\>/
+syn match mlirOps /\<affine\.dma_start\>/
+syn match mlirOps /\<affine\.dma_wait\>/
+syn match mlirOps /\<affine\.for\>/
+syn match mlirOps /\<affine\.if\>/
+syn match mlirOps /\<affine\.load\>/
+syn match mlirOps /\<affine\.store\>/
+
+" Keywords.
+syn keyword mlirKeyword
+      \ else
+      \ func
+      \ return
+      \ step
+      \ to
+
+" Misc syntax.
+syn match   mlirNoName /[%@!]\d\+\>/
+syn match   mlirNumber /-\?\<\d\+\>/
+syn match   mlirFloat  /-\?\<\d\+\.\d*\(e[+-]\d\+\)\?\>/
+syn match   mlirFloat  /\<0x\x\+\>/
+syn keyword mlirBoolean true false
+syn match   mlirComment /\/\/.*$/
+syn region  mlirString start=/"/ skip=/\\"/ end=/"/
+syn match   mlirLabel /[-a-zA-Z$._][-a-zA-Z$._0-9]*:/
+syn match   mlirIdentifier /[%@][-a-zA-Z$._][-a-zA-Z$._0-9]*/
+syn match mlirMapSetOutline "#.*$"
+
+" 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:.*$/
+
+if version >= 508 || !exists("did_c_syn_inits")
+  if version < 508
+    let did_c_syn_inits = 1
+    command -nargs=+ HiLink hi link <args>
+  else
+    command -nargs=+ HiLink hi def link <args>
+  endif
+
+  HiLink mlirType Type
+  HiLink mlirOps Statement
+  HiLink mlirMapSetOutline PreProc
+  HiLink mlirNumber Number
+  HiLink mlirComment Comment
+  HiLink mlirString String
+  HiLink mlirLabel Label
+  HiLink mlirKeyword Keyword
+  HiLink mlirBoolean Boolean
+  HiLink mlirFloat Float
+  HiLink mlirNoName Identifier
+  HiLink mlirConstant Constant
+  HiLink mlirSpecialComment SpecialComment
+  HiLink mlirIdentifier Identifier
+
+  delcommand HiLink
+endif
+
+let b:current_syntax = "mlir"