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