MLIR  20.0.0git
MlirLspServerMain.cpp
Go to the documentation of this file.
1 //===- MlirLspServerMain.cpp - MLIR Language Server main ------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
10 #include "LSPServer.h"
11 #include "MLIRServer.h"
12 #include "mlir/IR/Dialect.h"
15 #include "llvm/Support/CommandLine.h"
16 #include "llvm/Support/Program.h"
17 
18 using namespace mlir;
19 using namespace mlir::lsp;
20 
21 LogicalResult mlir::MlirLspServerMain(int argc, char **argv,
22  DialectRegistry &registry) {
23  llvm::cl::opt<JSONStreamStyle> inputStyle{
24  "input-style",
25  llvm::cl::desc("Input JSON stream encoding"),
26  llvm::cl::values(clEnumValN(JSONStreamStyle::Standard, "standard",
27  "usual LSP protocol"),
28  clEnumValN(JSONStreamStyle::Delimited, "delimited",
29  "messages delimited by `// -----` lines, "
30  "with // comment support")),
31  llvm::cl::init(JSONStreamStyle::Standard),
32  llvm::cl::Hidden,
33  };
34  llvm::cl::opt<bool> litTest{
35  "lit-test",
36  llvm::cl::desc(
37  "Abbreviation for -input-style=delimited -pretty -log=verbose. "
38  "Intended to simplify lit tests"),
39  llvm::cl::init(false),
40  };
41  llvm::cl::opt<Logger::Level> logLevel{
42  "log",
43  llvm::cl::desc("Verbosity of log messages written to stderr"),
44  llvm::cl::values(
45  clEnumValN(Logger::Level::Error, "error", "Error messages only"),
46  clEnumValN(Logger::Level::Info, "info",
47  "High level execution tracing"),
48  clEnumValN(Logger::Level::Debug, "verbose", "Low level details")),
49  llvm::cl::init(Logger::Level::Info),
50  };
51  llvm::cl::opt<bool> prettyPrint{
52  "pretty",
53  llvm::cl::desc("Pretty-print JSON output"),
54  llvm::cl::init(false),
55  };
56  llvm::cl::ParseCommandLineOptions(argc, argv, "MLIR LSP Language Server");
57 
58  if (litTest) {
59  inputStyle = JSONStreamStyle::Delimited;
60  logLevel = Logger::Level::Debug;
61  prettyPrint = true;
62  }
63 
64  // Configure the logger.
65  Logger::setLogLevel(logLevel);
66 
67  // Configure the transport used for communication.
68  llvm::sys::ChangeStdinToBinary();
69  JSONTransport transport(stdin, llvm::outs(), inputStyle, prettyPrint);
70 
71  // Register the additionally supported URI schemes for the MLIR server.
72  URIForFile::registerSupportedScheme("mlir.bytecode-mlir");
73 
74  // Configure the servers and start the main language server.
75  MLIRServer server(registry);
76  return runMlirLSPServer(server, transport);
77 }
The DialectRegistry maps a dialect namespace to a constructor for the matching dialect.
A transport class that performs the JSON-RPC communication with the LSP client.
Definition: Transport.h:48
static void setLogLevel(Level logLevel)
Set the severity level of the logger.
Definition: Logging.cpp:17
This class implements all of the MLIR related functionality necessary for a language server.
Definition: MLIRServer.h:36
static void registerSupportedScheme(StringRef scheme)
Register a supported URI scheme.
Definition: Protocol.cpp:243
@ Delimited
Messages are delimited by a '// --—' line. Comment lines start with //.
Definition: Transport.h:43
@ Standard
Encoding per the LSP specification, with mandatory Content-Length header.
Definition: Transport.h:41
llvm::LogicalResult runMlirLSPServer(MLIRServer &server, JSONTransport &transport)
Run the main loop of the LSP server using the given MLIR server and transport.
Definition: LSPServer.cpp:286
Include the generated interface declarations.
llvm::LogicalResult MlirLspServerMain(int argc, char **argv, DialectRegistry &registry)
Implementation for tools like mlir-lsp-server.