MLIR  22.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"
14 #include "llvm/Support/CommandLine.h"
15 #include "llvm/Support/Program.h"
16 
17 using namespace mlir;
18 using namespace mlir::lsp;
19 
20 LogicalResult mlir::MlirLspServerMain(int argc, char **argv,
21  DialectRegistryFn registry_fn) {
22  llvm::cl::opt<JSONStreamStyle> inputStyle{
23  "input-style",
24  llvm::cl::desc("Input JSON stream encoding"),
25  llvm::cl::values(clEnumValN(JSONStreamStyle::Standard, "standard",
26  "usual LSP protocol"),
27  clEnumValN(JSONStreamStyle::Delimited, "delimited",
28  "messages delimited by `// -----` lines, "
29  "with // comment support")),
30  llvm::cl::init(JSONStreamStyle::Standard),
31  llvm::cl::Hidden,
32  };
33  llvm::cl::opt<bool> litTest{
34  "lit-test",
35  llvm::cl::desc(
36  "Abbreviation for -input-style=delimited -pretty -log=verbose. "
37  "Intended to simplify lit tests"),
38  llvm::cl::init(false),
39  };
40  llvm::cl::opt<Logger::Level> logLevel{
41  "log",
42  llvm::cl::desc("Verbosity of log messages written to stderr"),
43  llvm::cl::values(
44  clEnumValN(Logger::Level::Error, "error", "Error messages only"),
45  clEnumValN(Logger::Level::Info, "info",
46  "High level execution tracing"),
47  clEnumValN(Logger::Level::Debug, "verbose", "Low level details")),
48  llvm::cl::init(Logger::Level::Info),
49  };
50  llvm::cl::opt<bool> prettyPrint{
51  "pretty",
52  llvm::cl::desc("Pretty-print JSON output"),
53  llvm::cl::init(false),
54  };
55  llvm::cl::ParseCommandLineOptions(argc, argv, "MLIR LSP Language Server");
56 
57  if (litTest) {
58  inputStyle = JSONStreamStyle::Delimited;
59  logLevel = Logger::Level::Debug;
60  prettyPrint = true;
61  }
62 
63  // Configure the logger.
64  Logger::setLogLevel(logLevel);
65 
66  // Configure the transport used for communication.
67  llvm::sys::ChangeStdinToBinary();
68  JSONTransport transport(stdin, llvm::outs(), inputStyle, prettyPrint);
69 
70  // Register the additionally supported URI schemes for the MLIR server.
71  URIForFile::registerSupportedScheme("mlir.bytecode-mlir");
72 
73  // Configure the servers and start the main language server.
74  MLIRServer server(registry_fn);
75  return runMlirLSPServer(server, transport);
76 }
77 
78 llvm::LogicalResult mlir::MlirLspServerMain(int argc, char **argv,
79  DialectRegistry &registry) {
80  auto registry_fn =
81  [&registry](const lsp::URIForFile &uri) -> DialectRegistry & {
82  return registry;
83  };
84  return MlirLspServerMain(argc, argv, registry_fn);
85 }
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:88
static void setLogLevel(Level logLevel)
Set the severity level of the logger.
Definition: Logging.cpp:16
This class implements all of the MLIR related functionality necessary for a language server.
Definition: MLIRServer.h:36
URI in "file" scheme for a file.
Definition: Protocol.h:97
static void registerSupportedScheme(StringRef scheme)
Register a supported URI scheme.
Definition: Protocol.cpp:238
@ 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:292
Include the generated interface declarations.
llvm::LogicalResult MlirLspServerMain(int argc, char **argv, DialectRegistry &registry)
Implementation for tools like mlir-lsp-server.