-
Notifications
You must be signed in to change notification settings - Fork 5.3k
[NativeAOT] Source to native mapping fix for out-of-order code #123333
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
Conversation
|
Tagging subscribers to this area: @agocke, @dotnet/ilc-contrib |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR fixes source-to-native line mapping in NativeAOT for scenarios where code generation results in out-of-order placement—specifically, when code at lower IL offsets appears at higher native offsets. The fix implements a mechanism similar to CoreCLR's debugger algorithm by tracking when IL offsets decrease during native mapping traversal and inserting sequence points accordingly.
Changes:
- Modified
GetNativeSequencePoints()to forward-fill sequence point information across all IL offsets between real sequence points - Added tracking to detect when IL offsets decrease during native mapping traversal, emitting additional sequence points to maintain correct source line attribution
- Introduced
IsBackedSequencePointflag to distinguish between real and propagated sequence points
src/coreclr/tools/aot/ILCompiler.RyuJit/Compiler/DependencyAnalysis/MethodCodeNode.cs
Outdated
Show resolved
Hide resolved
src/coreclr/tools/aot/ILCompiler.RyuJit/Compiler/DependencyAnalysis/MethodCodeNode.cs
Show resolved
Hide resolved
src/coreclr/tools/aot/ILCompiler.RyuJit/Compiler/DependencyAnalysis/MethodCodeNode.cs
Show resolved
Hide resolved
MichalStrehovsky
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you! Cc @jakobbotsch
Sometimes code is placed at a higher native offset than the code at IL offsets adjacent to it. Our debugger algorithm deals with this by, during traversal of mappings, [setting the IL offset back](https://github.com/dotnet/runtime/blob/6afecd4adc64210e5b46767d02a6495af1a08c46/src/coreclr/vm/debugdebugger.cpp#L1321) if one is encountered that is lower than the current IL offset in the traversal. Native AOT does not have this mechanism, which leads to such code being misattributed to higher-than-actual source lines. This change reproduces this mechanism in NativeAOT by inserting a native sequence point whenever the IL offset drops below the previous offset. Repro issue (line attribution of DoWork): ```csharp using System.Runtime.CompilerServices; static class Program { #line 53 [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)] public async static Task Main() { await DoWork(0); } [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)] public async static Task BackgroundWorker1() { // await Task.Delay(10000); // await Task.Yield(); throw new InvalidOperationException("BackgroundWorker1 exception."); } [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)] public async static Task BackgroundWorker2() { await Task.Delay(10); Console.WriteLine("BackgroundWorker2 completed."); } [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)] public async static Task BackgroundWorker3() { await Task.Delay(10); Console.WriteLine("BackgroundWorker3 completed."); } [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)] static async Task DoWork(int i) { Task worker1 = BackgroundWorker1(); Task worker2 = BackgroundWorker2(); Task worker3 = BackgroundWorker3(); await Task.WhenAll(worker1, worker2, worker3); Console.WriteLine("All background workers completed."); } } ``` Config: Windows x64 Debug, .NET 11.0.100-alpha.1.25618.104, runtime-async on <table> <tr> <th>NativeOffset</th> <td>0</td> <td>106</td> <td>107</td> <td>107</td> <td>116</td> <td>124</td> <td>124</td> <td>133</td> <td>141</td> <td>141</td> <td>150</td> <td>158</td> <td>169</td> <td>183</td> <td>192</td> <td>205</td> <td>222</td> <td>231</td> <td>244</td> <td>261</td> <td>270</td> <td>283</td> <td>307</td> <td>312</td> <td>357</td> <td>379</td> <td>380</td> <td>387</td> <td>392</td> <td>393</td> <td>420</td> <td>431</td> <td>867</td> <td>878</td> <td>1067</td> <td>1096</td> </tr> <tr> <th>ILOffset</th> <td>0</td> <td>0</td> <td>1</td> <td>1</td> <td>6</td> <td>7</td> <td>7</td> <td>12</td> <td>13</td> <td>13</td> <td>18</td> <td>19</td> <td>27</td> <td>30</td> <td>35</td> <td>37</td> <td>40</td> <td>45</td> <td>47</td> <td>50</td> <td>55</td> <td>57</td> <td>60</td> <td>65</td> <td>70</td> <td>75</td> <td>76</td> <td>81</td> <td>86</td> <td>87</td> <td>87</td> <td>70</td> <td>87</td> <td>70</td> <td>0</td> <td>87</td> </tr> </table> <table> <tr> <th>ILOffset</th> <td>0</td> <td>1</td> <td>7</td> <td>13</td> <td>19</td> <td>76</td> </tr> <tr> <th>LineNumber</th> <td>80</td> <td>81</td> <td>82</td> <td>83</td> <td>84</td> <td>85</td> </tr> </table> Before: <table> <tr> <th>NativeOffset</th> <td>0</td> <td>106</td> <td>107</td> <td>124</td> <td>141</td> <td>158</td> <td>380</td> <td>1067</td> </tr> <tr> <th>LineNumber</th> <td>80</td> <td>80</td> <td>81</td> <td>82</td> <td>83</td> <td>84</td> <td>85</td> <td>80</td> </tr> </table> Notice in particular that code after native offset 431, the suspension code for DoWork, is mistakenly attributed to line 85 (Console.WriteLine). After: <table> <tr> <th>NativeOffset</th> <td>0</td> <td>106</td> <td>107</td> <td>124</td> <td>141</td> <td>158</td> <td>380</td> <td>431</td> <td>878</td> <td>1067</td> </tr> <tr> <th>LineNumber</th> <td>80</td> <td>80</td> <td>81</td> <td>82</td> <td>83</td> <td>84</td> <td>85</td> <td>84</td> <td>84</td> <td>80</td> </tr> </table> Here we see that 380-431 is attributed to Console.WriteLine, while the suspension code that follows is correctly attributed to the previous line of await. [aot.txt](https://github.com/user-attachments/files/24697543/aot.txt)
Testing on #122722 revealed some issues with #123333 so I am fixing these here. After #123333 there remain two error cases: 1. Our IL offset is greater than the previously found offset, but lower than the max found offset. In this case, we would like to advance the line number in accordance with the new IL offset, which was omitted after #123333. 2. Our IL offset is greater than the highest IL offset for which there exists a sequence point. In this case, we want to ensure we can create a native <-> line mapping - #123333 had left these IL offsets with a Document of null, meaning no mappings were possible. This fixes both of these issues. Before: <img width="1298" height="268" alt="Screenshot 2026-01-30 192451" src="https://github.com/user-attachments/assets/ab46947f-ea52-4530-949b-3e36bf3b3df6" /> <img width="1312" height="359" alt="Screenshot 2026-01-30 192408" src="https://github.com/user-attachments/assets/b08a8f45-0af2-484a-8392-8d12c3694361" /> After: <img width="1020" height="311" alt="Screenshot 2026-01-30 190332" src="https://github.com/user-attachments/assets/ac8bca54-6466-4436-9fac-1f6f685e7bd0" /> <img width="1030" height="400" alt="Screenshot 2026-01-30 190613" src="https://github.com/user-attachments/assets/321c8160-cf06-4f43-8c36-352d2bd6908a" /> [foo.txt](https://github.com/user-attachments/files/24984632/foo.txt) [foo2.txt](https://github.com/user-attachments/files/24984633/foo2.txt) [New Compressed (zipped) Folder.zip](https://github.com/user-attachments/files/24976959/New.Compressed.zipped.Folder.zip) --------- Co-authored-by: Michal Strehovský <[email protected]> Co-authored-by: Jan Kotas <[email protected]>
Sometimes code is placed at a higher native offset than the code at IL offsets adjacent to it. Our debugger algorithm deals with this by, during traversal of mappings, setting the IL offset back if one is encountered that is lower than the current IL offset in the traversal. Native AOT does not have this mechanism, which leads to such code being misattributed to higher-than-actual source lines.
This change reproduces this mechanism in NativeAOT by inserting a native sequence point whenever the IL offset drops below the previous offset.
Repro issue (line attribution of DoWork):
Config: Windows x64 Debug, .NET 11.0.100-alpha.1.25618.104, runtime-async on
Before:
After:
aot.txt