[PSA] New MLIR-LSP features

I don’t generally send PSAs for stuff I add to the MLIR language servers, but some newer stuff is semi-obscure and maybe of use to others.

The first of which is code completion. The server now has some rudimentary support for code completing dialect/operation names, SSA/Block names, keywords, attribute/types, etc. Code completion is kind of finnicky (especially for an IR), so please let me know if you have annoyances/suggestions for more completions/etc.

The second of which is that the language server can now auto-insert exected-* checks for parser and verifier diagnostics. I’ve given it some rudimentary use, but it’d be nice if others can try it out (and break it).

As always, I generally just add features that seem cool or useful to myself, but I’m always happy to field requests/suggestions of interesting things to add. I should also note that the list of features is always up to date on the mlir website, and everything is supported via the MLIR VSCode extension.

– River

10 Likes

Is it planned to have a standalone binary version of the LSP server?

There has been one from the beginning, the docs mention how you can build your own (if you have custom dialects/etc.): MLIR : Language Server Protocol - MLIR
The one for just upstream dialects is at: https://github.com/llvm/llvm-project/tree/main/mlir/tools/mlir-lsp-server

Separate server and client is generally how LSP works, VSCode is just the only client I care about (given that it’s the only one I personally use).

– River

I had no idea that this was already existing.

This make sense and most people use VSCode unlike me.
Thank you for your awnser.

Feel free to send patches for clients that aren’t VSCode, we would accept most clients really (VSCode is just the one I focus on the most, but LSP features generally transfer easily across clients). See the client that was added a few months ago for emacs: https://github.com/llvm/llvm-project/blob/main/mlir/utils/emacs/mlir-lsp-client.el

– River

@okkwon uses it with Emacs if I’m not mistaken (I jump between Emacs, Vim, Vscode and another editor, but mine are at the “works on my desktop” level of hackiness, so contributions definitely welcome :slight_smile: )

Really cool enhancements! Thanks. I should give it a try. Emacs has its own issue like a hang during processing a large MLIR file. It is unfortunate since I have to kill the daemon or an instance outside (using the kill command). There might be some time-out feature though. (I didn’t dig it deeper).

I am using Neovim with the lsp builtin server and the support out of the box is nearly non existent.
I have random errors in all mlir test files like on the picture below.

In my lsp log file i have errors each time i’m trying some lsp language server’s features.


So it’s definitely not working on Neovim right now.
I’ll maybe try to investigate when i got the time.

I’ve been using the LSP server from Neovim (builtin LSP support via the nvim-lspconfig plugin) for the last month or so without issue.

This is the relevant part of my config if it helps anyone:

local configs = require('lspconfig.configs')
local lspconfig = require('lspconfig')
local lsputil = require('lspconfig.util')

configs.mlir = {
  default_config = {
    filetypes = {'mlir'},
    root_dir = lsputil.find_git_ancestor,
    single_file_support = true,
  }
}

lspconfig.mlir.setup {
  cmd = {'<path-to-lsp-server-binary>'},
  on_attach = on_attach, -- attach your key bindings
}

That’s strange, i have done the the same thing and i have tried your snippet but i still have errors that shouldn’t appear.

What your Neovim version (mine is v0.7.2)?
Do you need to do some extra things like clangd with the compile_commands.json file?

Only the language servers for PDLL and TableGen require extra command files, the one for MLIR is standalone.

I have zero experience with neovim, but that looks to me as if the func dialect isn’t registered by the server. Not sure if that’s an out-of-date server, or some weird neovim issue, but that’s generally what that error indicates.

I figured out that you need to use the MLIR LSP server of the LLVM git not the one of the LLVM 14.0.6.

For information Neovim users: i’ve added the 3 MLIR LSP servers (mlir, pdll and tablegen) default configuration on nvim-lspconfig, my PR has been merged today.

2 Likes

PSA to anyone with non-vscode setups, please feel free to add the necessary setup/etc. to the main LSP doc (we have a section for editor setups): MLIR : Language Server Protocol - MLIR

(LSP doc in-tree is at: llvm-project/MLIRLSP.md at main · llvm/llvm-project · GitHub)

It’d be nice to collect the different setups so that it’s easier for users moving forward.

– River

I agree, i’ll add the required configuration for Neovim sometime soon.
Thanks for these details.