-
Notifications
You must be signed in to change notification settings - Fork 710
Add JitAsm tool to be able to analyse the Jit output #10432
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
aff70ce
Add JitAsm tool to be able to analyse the Jit output
benaadams 4b8c54e
Spelling
benaadams afdaed5
Address AGENTS.md LINQ guideline feedback on JitAsm PR (#10433)
Copilot 4c7ba80
Spell
benaadams a23f8b9
feedback
benaadams 50aa107
Feedback
benaadams 2e66cd4
Improve initialization
benaadams File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| // SPDX-FileCopyrightText: 2025 Demerzel Solutions Limited | ||
| // SPDX-License-Identifier: LGPL-3.0-only | ||
|
|
||
| using System.Text; | ||
| using System.Text.RegularExpressions; | ||
|
|
||
| namespace JitAsm; | ||
|
|
||
| internal static partial class DisassemblyParser | ||
| { | ||
| // Pattern to detect the start of a method's disassembly | ||
| // Example: ; Assembly listing for method Namespace.Type:Method(args) | ||
| [GeneratedRegex(@"^; Assembly listing for method (?<method>.+)$", RegexOptions.Compiled | RegexOptions.Multiline)] | ||
| private static partial Regex MethodHeaderPattern(); | ||
|
|
||
| // Pattern to detect end of method disassembly (next method or end) | ||
| [GeneratedRegex(@"^; Total bytes of code", RegexOptions.Compiled | RegexOptions.Multiline)] | ||
| private static partial Regex MethodEndPattern(); | ||
|
|
||
| public static string Parse(string jitOutput, bool lastOnly = false) | ||
| { | ||
| if (string.IsNullOrWhiteSpace(jitOutput)) | ||
| { | ||
| return string.Empty; | ||
| } | ||
|
|
||
| var result = new StringBuilder(); | ||
| var matches = MethodHeaderPattern().Matches(jitOutput); | ||
|
|
||
| if (matches.Count == 0) | ||
| { | ||
| // No method headers found, return raw output if it looks like assembly | ||
| if (jitOutput.Contains("mov") || jitOutput.Contains("call") || jitOutput.Contains("ret")) | ||
| { | ||
| return jitOutput.Trim(); | ||
| } | ||
| return string.Empty; | ||
| } | ||
|
|
||
| // In tier1 mode, JitDisasm captures both Tier-0 and Tier-1 compilations. | ||
| // We want the LAST compilation (Tier-1 with full optimizations). | ||
| int startIdx = lastOnly ? matches.Count - 1 : 0; | ||
|
|
||
| for (int i = startIdx; i < matches.Count; i++) | ||
| { | ||
| var match = matches[i]; | ||
| var startIndex = match.Index; | ||
|
|
||
| // Find the end of this method's disassembly | ||
| int endIndex = (i + 1 < matches.Count) ? matches[i + 1].Index : jitOutput.Length; | ||
|
|
||
| // Extract this method's disassembly | ||
| var methodAsm = jitOutput[startIndex..endIndex].TrimEnd(); | ||
|
|
||
| // Find "Total bytes of code" line and include it | ||
| var totalBytesMatch = MethodEndPattern().Match(methodAsm); | ||
| if (totalBytesMatch.Success) | ||
| { | ||
| // Find end of line after "Total bytes of code" | ||
| var lineEnd = methodAsm.IndexOf('\n', totalBytesMatch.Index); | ||
| if (lineEnd > 0) | ||
| { | ||
| methodAsm = methodAsm[..(lineEnd + 1)].TrimEnd(); | ||
| } | ||
| } | ||
|
|
||
| if (result.Length > 0) | ||
| { | ||
| result.AppendLine(); | ||
| result.AppendLine(new string('-', 80)); | ||
| result.AppendLine(); | ||
| } | ||
|
|
||
| result.AppendLine(methodAsm); | ||
| } | ||
|
|
||
| return result.ToString().Trim(); | ||
| } | ||
|
|
||
| public static IEnumerable<MethodDisassembly> ParseMethods(string jitOutput) | ||
| { | ||
| if (string.IsNullOrWhiteSpace(jitOutput)) | ||
| { | ||
| yield break; | ||
| } | ||
|
|
||
| var matches = MethodHeaderPattern().Matches(jitOutput); | ||
|
|
||
| for (int i = 0; i < matches.Count; i++) | ||
| { | ||
| var match = matches[i]; | ||
| var methodName = match.Groups["method"].Value; | ||
| var startIndex = match.Index; | ||
|
|
||
| int endIndex = (i + 1 < matches.Count) ? matches[i + 1].Index : jitOutput.Length; | ||
|
|
||
| var methodAsm = jitOutput[startIndex..endIndex].TrimEnd(); | ||
|
|
||
| yield return new MethodDisassembly | ||
| { | ||
| MethodName = methodName, | ||
| Assembly = methodAsm | ||
| }; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| internal sealed class MethodDisassembly | ||
| { | ||
| public required string MethodName { get; init; } | ||
| public required string Assembly { get; init; } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.