xref: /llvm-project/mlir/utils/emacs/mlir-mode.el (revision 5b4abae7630572c96a736faa1f09b1a3c37201a2)
1;;; mlir-mode.el --- Major mode for the MLIR assembler language.
2
3;; Copyright (C) 2019 The MLIR Authors.
4;;
5;; Licensed under the Apache License, Version 2.0 (the "License");
6;; you may not use this file except in compliance with the License.
7;; You may obtain a copy of the License at
8;;
9;;      http://www.apache.org/licenses/LICENSE-2.0
10;;
11;; Unless required by applicable law or agreed to in writing, software
12;; distributed under the License is distributed on an "AS IS" BASIS,
13;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14;; See the License for the specific language governing permissions and
15;; limitations under the License.
16
17;; Version: 0.1.0
18
19;;; Commentary:
20
21;; Major mode for editing MLIR files.
22
23;;; Code:
24
25(defvar mlir-mode-syntax-table
26  (let ((table (make-syntax-table)))
27    (modify-syntax-entry ?% "_" table)
28    (modify-syntax-entry ?@ "_" table)
29    (modify-syntax-entry ?# "_" table)
30    (modify-syntax-entry ?. "_" table)
31    (modify-syntax-entry ?/ ". 12" table)
32    (modify-syntax-entry ?\n "> " table)
33    table)
34  "Syntax table used while in MLIR mode.")
35
36(defvar mlir-font-lock-keywords
37  (list
38   ;; Variables
39   '("%[-a-zA-Z$._0-9]*" . font-lock-variable-name-face)
40   ;; Functions
41   '("@[-a-zA-Z$._0-9]*" . font-lock-function-name-face)
42   ;; Affinemaps
43   '("#[-a-zA-Z$._0-9]*" . font-lock-variable-name-face)
44   ;; Types
45   '("\\b\\(f16\\|bf16\\|f32\\|f64\\|index\\|tf_control\\|i[1-9][0-9]*\\)\\b" . font-lock-type-face)
46   '("\\b\\(tensor\\|vector\\|memref\\)\\b" . font-lock-type-face)
47   ;; Dimension lists
48   '("\\b\\([0-9?]+x\\)*\\(f16\\|bf16\\|f32\\|f64\\|index\\|i[1-9][0-9]*\\)\\b" . font-lock-preprocessor-face)
49   ;; Integer literals
50   '("\\b[-]?[0-9]+\\b" . font-lock-preprocessor-face)
51   ;; Floating point constants
52   '("\\b[-+]?[0-9]+.[0-9]*\\([eE][-+]?[0-9]+\\)?\\b" . font-lock-preprocessor-face)
53   ;; Hex constants
54   '("\\b0x[0-9A-Fa-f]+\\b" . font-lock-preprocessor-face)
55   ;; Keywords
56   `(,(regexp-opt
57       '(;; Toplevel entities
58         "br" "ceildiv" "func" "cond_br" "else" "extfunc" "false" "floordiv" "for" "if" "mod" "return" "size" "step" "to" "true" "??" ) 'symbols) . font-lock-keyword-face))
59  "Syntax highlighting for MLIR.")
60
61;; Emacs 23 compatibility.
62(defalias 'mlir-mode-prog-mode
63  (if (fboundp 'prog-mode)
64      'prog-mode
65    'fundamental-mode))
66
67;;;###autoload
68(define-derived-mode mlir-mode mlir-mode-prog-mode "MLIR"
69  "Major mode for editing MLIR source files.
70\\{mlir-mode-map}
71  Runs `mlir-mode-hook' on startup."
72  (setq font-lock-defaults `(mlir-font-lock-keywords))
73  (setq-local comment-start "//"))
74
75;; Associate .mlir files with mlir-mode
76;;;###autoload
77(add-to-list 'auto-mode-alist (cons "\\.mlir\\'" 'mlir-mode))
78
79(defgroup mlir nil
80  "Major mode for editing MLIR source files."
81  :group 'languages
82  :prefix "mlir-")
83
84;; Set default value of opt-tool to use as mlir-opt.
85(defcustom mlir-opt "mlir-opt"
86  "Commandline MLIR opt tool to use."
87  :type 'string)
88
89;; Enable reading/writing .mlirbc files.
90(require 'jka-compr)
91(add-to-list 'jka-compr-compression-info-list
92  (vector "\\.mlirbc\\'"
93   "mlir-to-bytecode" mlir-opt (vector "--mlir-print-debuginfo" "--emit-bytecode" "-o" "-" "-")
94   "mlir-bytecode-to-text" mlir-opt (vector "--mlir-print-debuginfo")
95   nil nil "ML\357R"))
96(jka-compr-update)
97(auto-compression-mode t)
98(add-to-list 'auto-mode-alist (cons "\\.mlirbc\\'" 'mlir-mode))
99
100(provide 'mlir-mode)
101;;; mlir-mode.el ends here
102