Skip to content

Commit

Permalink
Allow passing target to codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnnyMorganz committed Dec 14, 2024
1 parent 8b9409f commit f3f12e2
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 20 deletions.
5 changes: 4 additions & 1 deletion editors/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ A custom LSP request message is implemented:

- `luau-lsp/bytecode`: `{ textDocument: TextDocumentIdentifier, optimizationLevel: number }`, returns `string` - textual bytecode output
- `luau-lsp/compilerRemarks`: `{ textDocument: TextDocumentIdentifier, optimizationLevel: number }`, returns `string` - source code with inline remarks as comments
- `luau-lsp/codeGen`: `{ textDocument: TextDocumentIdentifier, optimizationLevel: number }`, returns `string` - annotated codegen instructions
- `luau-lsp/codeGen`: `{ textDocument: TextDocumentIdentifier, optimizationLevel: number, codeGenTarget: string }`,
returns `string` - annotated codegen instructions

`codeGenTarget` can be one of: `"host" | "a64" | "a64_nofeatures" | "x64_windows" | "x64_systemv"`

You can implement this request via a custom command to surface this information in your editor
77 changes: 63 additions & 14 deletions editors/code/src/bytecode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export type BytecodeParams = {
};

export const BytecodeRequest = new RequestType<BytecodeParams, string, void>(
"luau-lsp/bytecode",
"luau-lsp/bytecode"
);

export type CompilerRemarksParams = {
Expand All @@ -36,9 +36,17 @@ export const ComputeCompilerRemarksRequest = new RequestType<
void
>("luau-lsp/compilerRemarks");

export type CodeGenTarget =
| "host"
| "a64"
| "a64_nofeatures"
| "x64_windows"
| "x64_systemv";

export type CodeGenParams = {
textDocument: TextDocumentIdentifier;
optimizationLevel: OptimizationLevel;
codeGenTarget: CodeGenTarget;
};

export const ComputeCodeGenRequest = new RequestType<
Expand Down Expand Up @@ -70,7 +78,7 @@ export const getOptimizationLevel = async (): Promise<OptimizationLevel> => {
{
title: "Select Optimization Level",
placeHolder: "Select optimization level",
},
}
);

return optimizationLevel?.label === "None"
Expand All @@ -82,7 +90,43 @@ export const getOptimizationLevel = async (): Promise<OptimizationLevel> => {
: OptimizationLevel.O1;
};

export const getCodeGenTarget = async (): Promise<CodeGenTarget> => {
const optimizationLevel = await vscode.window.showQuickPick(
[
{
label: "host",
detail: "Host target",
picked: true,
},

{
label: "a64",
detail: "Arm64",
},
{
label: "a64_nofeatures",
detail: "Arm64 (No Features)",
},
{
label: "x64_windows",
detail: "x64 Windows",
},
{
label: "x64_systemv",
detail: "x64 SystemV",
},
],
{
title: "Select CodeGen target",
placeHolder: "Select CodeGen target",
}
);

return (optimizationLevel?.label as CodeGenTarget) ?? "host";
};

let optimizationLevel = OptimizationLevel.O2;
let codeGenTarget: CodeGenTarget = "host";

// Based off https://github.com/rust-lang/rust-analyzer/blob/a24ede2066778f66b5b5e5aa7aa57a6d1be2063a/editors/code/src/commands.ts
// Licensed under MIT
Expand Down Expand Up @@ -110,10 +154,10 @@ const getBytecodeInfo = (
scheme: string,
fileName: string,
requestType: RequestType<
CompilerRemarksParams | BytecodeParams,
CompilerRemarksParams | BytecodeParams | CodeGenParams,
string,
void
>,
>
) => {
const tdcp = new (class implements vscode.TextDocumentContentProvider {
readonly uri = vscode.Uri.parse(`${scheme}://bytecode/${fileName}.luau`);
Expand All @@ -123,12 +167,12 @@ const getBytecodeInfo = (
vscode.workspace.onDidChangeTextDocument(
this.onDidChangeTextDocument,
this,
context.subscriptions,
context.subscriptions
);
vscode.window.onDidChangeActiveTextEditor(
this.onDidChangeActiveTextEditor,
this,
context.subscriptions,
context.subscriptions
);
}

Expand All @@ -153,9 +197,10 @@ const getBytecodeInfo = (

const params = {
textDocument: client.code2ProtocolConverter.asTextDocumentIdentifier(
editor.document,
editor.document
),
optimizationLevel,
codeGenTarget,
};

return client.sendRequest(requestType, params);
Expand All @@ -174,56 +219,60 @@ const getBytecodeInfo = (
if (isLuauEditor(textEditor)) {
optimizationLevel = await getOptimizationLevel();

if (command === "luau-lsp.computeCodeGen") {
codeGenTarget = await getCodeGenTarget();
}

const doc = await vscode.workspace.openTextDocument(tdcp.uri);
tdcp.eventEmitter.fire(tdcp.uri);
await vscode.window.showTextDocument(doc, {
viewColumn: vscode.ViewColumn.Two,
preserveFocus: true,
});
}
},
}
),
];
};

export const registerComputeBytecode = (
context: vscode.ExtensionContext,
client: LanguageClient,
client: LanguageClient
): vscode.Disposable[] => {
return getBytecodeInfo(
context,
client,
"luau-lsp.computeBytecode",
BYTECODE_SCHEME,
"bytecode",
BytecodeRequest,
BytecodeRequest
);
};

export const registerComputeCompilerRemarks = (
context: vscode.ExtensionContext,
client: LanguageClient,
client: LanguageClient
): vscode.Disposable[] => {
return getBytecodeInfo(
context,
client,
"luau-lsp.computeCompilerRemarks",
COMPILER_REMARKS_SCHEME,
"compiler-remarks",
ComputeCompilerRemarksRequest,
ComputeCompilerRemarksRequest
);
};

export const registerComputeCodeGen = (
context: vscode.ExtensionContext,
client: LanguageClient,
client: LanguageClient
): vscode.Disposable[] => {
return getBytecodeInfo(
context,
client,
"luau-lsp.computeCodeGen",
CODEGEN_SCHEME,
"codeGen",
ComputeCodeGenRequest,
ComputeCodeGenRequest
);
};
2 changes: 1 addition & 1 deletion src/LanguageServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ void LanguageServer::onRequest(const id_type& id, const std::string& method, std
auto workspace = findWorkspace(params.textDocument.uri);
response = workspace->compilerRemarks(params);
}
else if (method == "luau-lsp/codegen")
else if (method == "luau-lsp/codeGen")
{
ASSERT_PARAMS(baseParams, "luau-lsp/codeGen")
auto params = baseParams->get<lsp::CodegenParams>();
Expand Down
22 changes: 21 additions & 1 deletion src/include/Protocol/Extensions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,32 @@ NLOHMANN_DEFINE_OPTIONAL(CompilerRemarksParams, textDocument, optimizationLevel)

using CompilerRemarksResult = std::string;

// Based off Luau::CodeGen::AssemblyOptions::Target
enum struct CodeGenTarget
{
Host,
A64,
A64_NoFeatures,
X64_Windows,
X64_SystemV
};

NLOHMANN_JSON_SERIALIZE_ENUM(CodeGenTarget, {
{CodeGenTarget::Host, "host"},
{CodeGenTarget::A64, "a64"},
{CodeGenTarget::A64_NoFeatures, "a64_nofeatures"},
{CodeGenTarget::X64_Windows, "x64_windows"},
{CodeGenTarget::X64_SystemV, "x64_systemv"},
})


struct CodegenParams
{
TextDocumentIdentifier textDocument;
CompilerRemarksOptimizationLevel optimizationLevel = CompilerRemarksOptimizationLevel::O1;
CodeGenTarget codeGenTarget = CodeGenTarget::Host;
};
NLOHMANN_DEFINE_OPTIONAL(CodegenParams, textDocument, optimizationLevel)
NLOHMANN_DEFINE_OPTIONAL(CodegenParams, textDocument, optimizationLevel, codeGenTarget)

using CodegenResult = std::string;
} // namespace lsp
25 changes: 22 additions & 3 deletions src/operations/Bytecode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,25 @@ static uint32_t flagsForType(BytecodeOutputType type)
return 0;
}

static std::string computeBytecodeOutput(const Luau::ModuleName& moduleName, const std::string& source, const ClientConfiguration& config, int optimizationLevel, BytecodeOutputType type)
static Luau::CodeGen::AssemblyOptions::Target getCodeGenTarget(const lsp::CodeGenTarget& codeGenTarget)
{
switch (codeGenTarget)
{
case lsp::CodeGenTarget::Host:
return Luau::CodeGen::AssemblyOptions::Target::Host;
case lsp::CodeGenTarget::A64:
return Luau::CodeGen::AssemblyOptions::Target::A64;
case lsp::CodeGenTarget::A64_NoFeatures:
return Luau::CodeGen::AssemblyOptions::Target::A64_NoFeatures;
case lsp::CodeGenTarget::X64_Windows:
return Luau::CodeGen::AssemblyOptions::Target::X64_Windows;
case lsp::CodeGenTarget::X64_SystemV:
return Luau::CodeGen::AssemblyOptions::Target::X64_SystemV;
}
}

static std::string computeBytecodeOutput(const Luau::ModuleName& moduleName, const std::string& source, const ClientConfiguration& config,
int optimizationLevel, BytecodeOutputType type, lsp::CodeGenTarget codeGenTarget = lsp::CodeGenTarget::Host)
{
try
{
Expand Down Expand Up @@ -85,7 +103,7 @@ static std::string computeBytecodeOutput(const Luau::ModuleName& moduleName, con
if (type == BytecodeOutputType::CodeGen)
{
Luau::CodeGen::AssemblyOptions assemblyOptions;
// TODO: assemblyOptions target
assemblyOptions.target = getCodeGenTarget(codeGenTarget);
assemblyOptions.outputBinary = false;
assemblyOptions.includeAssembly = true;
assemblyOptions.includeIr = true;
Expand Down Expand Up @@ -143,5 +161,6 @@ lsp::CompilerRemarksResult WorkspaceFolder::codeGen(const lsp::CodegenParams& pa
throw JsonRpcException(lsp::ErrorCode::RequestFailed, "No managed text document for " + params.textDocument.uri.toString());

auto config = client->getConfiguration(rootUri);
return computeBytecodeOutput(moduleName, textDocument->getText(), config, params.optimizationLevel, BytecodeOutputType::CodeGen);
return computeBytecodeOutput(
moduleName, textDocument->getText(), config, params.optimizationLevel, BytecodeOutputType::CodeGen, params.codeGenTarget);
}

0 comments on commit f3f12e2

Please sign in to comment.