[NativeAOT] Source to native mapping fix for out-of-order code#123333
Merged
rcj1 merged 2 commits intodotnet:mainfrom Jan 19, 2026
Merged
[NativeAOT] Source to native mapping fix for out-of-order code#123333rcj1 merged 2 commits intodotnet:mainfrom
rcj1 merged 2 commits intodotnet:mainfrom
Conversation
Contributor
|
Tagging subscribers to this area: @agocke, @dotnet/ilc-contrib |
Contributor
There was a problem hiding this comment.
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
approved these changes
Jan 19, 2026
Member
MichalStrehovsky
left a comment
There was a problem hiding this comment.
Thank you! Cc @jakobbotsch
rosebyte
pushed a commit
that referenced
this pull request
Jan 19, 2026
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)
rcj1
added a commit
that referenced
this pull request
Feb 3, 2026
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ý <MichalStrehovsky@users.noreply.github.com> Co-authored-by: Jan Kotas <jkotas@microsoft.com>
lewing
pushed a commit
to lewing/runtime
that referenced
this pull request
Feb 9, 2026
…3831) Testing on dotnet#122722 revealed some issues with dotnet#123333 so I am fixing these here. After dotnet#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 dotnet#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 - dotnet#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ý <MichalStrehovsky@users.noreply.github.com> Co-authored-by: Jan Kotas <jkotas@microsoft.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
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