Skip to content

Commit e646a55

Browse files
rcj1MichalStrehovskyjkotas
authored
[NativeAOT] fix NAOT mappings (follow-up to #123333) (#123831)
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>
1 parent 4fa6991 commit e646a55

File tree

3 files changed

+22
-28
lines changed

3 files changed

+22
-28
lines changed

src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/StackTraceLineNumbersNode.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,9 @@ static Vertex CreateLineNumbersBlob(NativeWriter writer, StackTraceDocumentsNode
103103

104104
foreach (NativeSequencePoint sequencePoint in debugInfoNode.GetNativeSequencePoints())
105105
{
106-
if (currentLineNumber == sequencePoint.LineNumber && currentDocument == sequencePoint.FileName)
107-
continue;
108-
109106
// Make sure a zero native offset delta is not possible because we use it below
110107
// to indicate an update to the current document.
111-
if (currentDocument != null && currentNativeOffset == sequencePoint.NativeOffset)
112-
continue;
113-
108+
Debug.Assert(currentDocument == null || currentNativeOffset != sequencePoint.NativeOffset);
114109
if (currentDocument != sequencePoint.FileName)
115110
{
116111
// We start with currentDocument == null, so the reader knows the first byte of the output

src/coreclr/tools/aot/ILCompiler.RyuJit/Compiler/DependencyAnalysis/MethodCodeNode.cs

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -260,10 +260,9 @@ public IEnumerable<NativeSequencePoint> GetNativeSequencePoints()
260260
if (_debugLocInfos == null)
261261
yield break;
262262

263-
var sequencePoints = new (string Document, int LineNumber, bool IsBackedSequencePoint)[_debugLocInfos.Length * 4 /* chosen empirically */];
263+
var sequencePoints = new (string Document, int LineNumber)[_debugLocInfos.Length * 4 /* chosen empirically */];
264264
try
265265
{
266-
int maxOffset = 0;
267266
foreach (var sequencePoint in _debugInfo.GetSequencePoints())
268267
{
269268
int offset = sequencePoint.Offset;
@@ -272,16 +271,15 @@ public IEnumerable<NativeSequencePoint> GetNativeSequencePoints()
272271
int newLength = Math.Max(2 * sequencePoints.Length, sequencePoint.Offset + 1);
273272
Array.Resize(ref sequencePoints, newLength);
274273
}
275-
sequencePoints[offset] = (sequencePoint.Document, sequencePoint.LineNumber, true);
276-
maxOffset = Math.Max(maxOffset, offset);
274+
sequencePoints[offset] = (sequencePoint.Document, sequencePoint.LineNumber);
277275
}
278276

279277
// Propagate last known document/line number forward to enable correct mapping when IL offsets decrease at higher native offsets
280-
for (int i = 1; i <= maxOffset; i++)
278+
for (int i = 1; i < sequencePoints.Length; i++)
281279
{
282280
if (sequencePoints[i].Document == null && sequencePoints[i - 1].Document != null)
283281
{
284-
sequencePoints[i] = (sequencePoints[i - 1].Document, sequencePoints[i - 1].LineNumber, false);
282+
sequencePoints[i] = (sequencePoints[i - 1].Document, sequencePoints[i - 1].LineNumber);
285283
}
286284
}
287285
}
@@ -294,28 +292,26 @@ public IEnumerable<NativeSequencePoint> GetNativeSequencePoints()
294292
}
295293

296294
int previousNativeOffset = -1;
297-
int previousIlOffset = -1;
295+
string previousDocument = null;
296+
int previousLineNumber = -1;
297+
// OffsetMapping is sorted in order of increasing native offset (but not necessarily by IL offset)
298298
foreach (var nativeMapping in _debugLocInfos)
299299
{
300300
if (nativeMapping.NativeOffset == previousNativeOffset)
301301
continue;
302302

303-
if (nativeMapping.ILOffset < sequencePoints.Length)
303+
var sequencePoint = sequencePoints[Math.Min(nativeMapping.ILOffset, sequencePoints.Length - 1)];
304+
// Emit sequence point if its line number or document differ from the previous one.
305+
// See WalkILOffsetsCallback in src/coreclr/vm/debugdebugger.cpp for more details.
306+
if (sequencePoint.Document != null && (sequencePoint.Document != previousDocument || sequencePoint.LineNumber != previousLineNumber))
304307
{
305-
var sequencePoint = sequencePoints[nativeMapping.ILOffset];
306-
// Emit sequence point if we have it from _debugInfo or if ILOffset decreases.
307-
// This handles the case of IL offsets decreasing at higher native offsets.
308-
// See WalkILOffsetsCallback in src/coreclr/vm/debugdebugger.cpp for more details.
309-
if ((sequencePoint.IsBackedSequencePoint || nativeMapping.ILOffset < previousIlOffset) &&
310-
sequencePoint.Document != null)
311-
{
312-
yield return new NativeSequencePoint(
313-
nativeMapping.NativeOffset,
314-
sequencePoint.Document,
315-
sequencePoint.LineNumber);
316-
previousNativeOffset = nativeMapping.NativeOffset;
317-
previousIlOffset = nativeMapping.ILOffset;
318-
}
308+
yield return new NativeSequencePoint(
309+
nativeMapping.NativeOffset,
310+
sequencePoint.Document,
311+
sequencePoint.LineNumber);
312+
previousNativeOffset = nativeMapping.NativeOffset;
313+
previousDocument = sequencePoint.Document;
314+
previousLineNumber = sequencePoint.LineNumber;
319315
}
320316
}
321317
}

src/coreclr/tools/aot/ILCompiler.RyuJit/JitInterface/CorInfoImpl.RyuJit.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,6 +1048,9 @@ private void setBoundaries(CORINFO_METHOD_STRUCT_* ftn, uint cMap, OffsetMapping
10481048
case (int)MappingTypes.NO_MAPPING:
10491049
continue;
10501050
}
1051+
// Ignore these; see WalkILOffsetsCallback in src/coreclr/vm/debugdebugger.cpp.
1052+
if (nativeToILInfo->source == SourceTypes.CALL_INSTRUCTION)
1053+
continue;
10511054

10521055
debugLocInfos.Add(new DebugLocInfo((int)nativeToILInfo->nativeOffset, ilOffset));
10531056
}

0 commit comments

Comments
 (0)