Skip to content

Commit a5087ed

Browse files
Spanify ILReader
* Make ILReader ref struct, only holding IL body bytes and a offset * Use BinaryPrimitives instead of manual bit-shifting * Remove unused ILStreamReader.cs * Make ReadMIbcGroup return List instead of using enumerator
1 parent f545368 commit a5087ed

File tree

5 files changed

+36
-226
lines changed

5 files changed

+36
-226
lines changed

src/coreclr/tools/Common/TypeSystem/IL/ILReader.cs

Lines changed: 30 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,27 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System;
5+
using System.Buffers.Binary;
6+
47
using Internal.TypeSystem;
58

69
using Debug = System.Diagnostics.Debug;
710

811
namespace Internal.IL
912
{
10-
internal struct ILReader
13+
internal ref struct ILReader
1114
{
1215
private int _currentOffset;
13-
private readonly byte[] _ilBytes;
16+
private readonly ReadOnlySpan<byte> _ilBytes;
1417

15-
public int Offset
16-
{
17-
get
18-
{
19-
return _currentOffset;
20-
}
21-
}
18+
public readonly int Offset => _currentOffset;
2219

23-
public int Size
24-
{
25-
get
26-
{
27-
return _ilBytes.Length;
28-
}
29-
}
20+
public readonly int Size => _ilBytes.Length;
3021

31-
public bool HasNext
32-
{
33-
get
34-
{
35-
return _currentOffset < _ilBytes.Length;
36-
}
37-
}
22+
public readonly bool HasNext => _currentOffset < _ilBytes.Length;
3823

39-
public ILReader(byte[] ilBytes, int currentOffset = 0)
24+
public ILReader(ReadOnlySpan<byte> ilBytes, int currentOffset = 0)
4025
{
4126
_ilBytes = ilBytes;
4227
_currentOffset = currentOffset;
@@ -56,22 +41,20 @@ public byte ReadILByte()
5641

5742
public ushort ReadILUInt16()
5843
{
59-
if (_currentOffset + 2 > _ilBytes.Length)
44+
if (!BinaryPrimitives.TryReadUInt16LittleEndian(_ilBytes.Slice(_currentOffset), out ushort value))
6045
ThrowHelper.ThrowInvalidProgramException();
6146

62-
ushort val = (ushort)(_ilBytes[_currentOffset] + (_ilBytes[_currentOffset + 1] << 8));
63-
_currentOffset += 2;
64-
return val;
47+
_currentOffset += sizeof(ushort);
48+
return value;
6549
}
6650

6751
public uint ReadILUInt32()
6852
{
69-
if (_currentOffset + 4 > _ilBytes.Length)
53+
if (!BinaryPrimitives.TryReadUInt32LittleEndian(_ilBytes.Slice(_currentOffset), out uint value))
7054
ThrowHelper.ThrowInvalidProgramException();
7155

72-
uint val = (uint)(_ilBytes[_currentOffset] + (_ilBytes[_currentOffset + 1] << 8) + (_ilBytes[_currentOffset + 2] << 16) + (_ilBytes[_currentOffset + 3] << 24));
73-
_currentOffset += 4;
74-
return val;
56+
_currentOffset += sizeof(uint);
57+
return value;
7558
}
7659

7760
public int ReadILToken()
@@ -81,21 +64,29 @@ public int ReadILToken()
8164

8265
public ulong ReadILUInt64()
8366
{
84-
ulong value = ReadILUInt32();
85-
value |= (((ulong)ReadILUInt32()) << 32);
67+
if (!BinaryPrimitives.TryReadUInt64LittleEndian(_ilBytes.Slice(_currentOffset), out ulong value))
68+
ThrowHelper.ThrowInvalidProgramException();
69+
70+
_currentOffset += sizeof(ulong);
8671
return value;
8772
}
8873

89-
public unsafe float ReadILFloat()
74+
public float ReadILFloat()
9075
{
91-
uint value = ReadILUInt32();
92-
return *(float*)(&value);
76+
if (!BinaryPrimitives.TryReadSingleLittleEndian(_ilBytes.Slice(_currentOffset), out float value))
77+
ThrowHelper.ThrowInvalidProgramException();
78+
79+
_currentOffset += sizeof(float);
80+
return value;
9381
}
9482

9583
public unsafe double ReadILDouble()
9684
{
97-
ulong value = ReadILUInt64();
98-
return *(double*)(&value);
85+
if (!BinaryPrimitives.TryReadDoubleLittleEndian(_ilBytes.Slice(_currentOffset), out double value))
86+
ThrowHelper.ThrowInvalidProgramException();
87+
88+
_currentOffset += sizeof(double);
89+
return value;
9990
}
10091

10192
public ILOpcode ReadILOpcode()

src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/Dataflow/MethodBodyScanner.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,7 @@ public int MoveNext(int offset)
188188
_foundEndOfPrevBlock = false;
189189
}
190190

191-
var reader = new ILReader(_methodBody.GetILBytes());
192-
reader.Seek(offset);
191+
var reader = new ILReader(_methodBody.GetILBytes(), offset);
193192
ILOpcode opcode = reader.ReadILOpcode();
194193
if (opcode.IsControlFlowInstruction())
195194
{

src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/ILStreamReader.cs

Lines changed: 0 additions & 181 deletions
This file was deleted.

src/coreclr/tools/aot/ILCompiler.Compiler/ILCompiler.Compiler.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,6 @@
478478
<Compile Include="Compiler\DependencyAnalysis\ShadowConcreteUnboxingThunkNode.cs" />
479479
<Compile Include="Compiler\ILScanner.cs" />
480480
<Compile Include="Compiler\ILScannerBuilder.cs" />
481-
<Compile Include="Compiler\ILStreamReader.cs" />
482481
<Compile Include="Compiler\LibraryInitializers.cs" />
483482
<Compile Include="Compiler\Compilation.cs" />
484483
<Compile Include="Compiler\CompilerMetadataFieldLayoutAlgorithm.cs" />

src/coreclr/tools/aot/ILCompiler.ReadyToRun/IBC/MIbcProfileParser.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ private enum MibcGroupParseState
353353
///
354354
/// This format is designed to be extensible to hold more data as we add new per method profile data without breaking existing parsers.
355355
/// </summary>
356-
private static IEnumerable<MethodProfileData> ReadMIbcGroup(EcmaMethod method)
356+
private static List<MethodProfileData> ReadMIbcGroup(EcmaMethod method)
357357
{
358358
EcmaMethodIL ilBody = EcmaMethodIL.Create(method);
359359
MetadataLoaderForPgoData metadataLoader = new MetadataLoaderForPgoData(ilBody);
@@ -369,6 +369,7 @@ private static IEnumerable<MethodProfileData> ReadMIbcGroup(EcmaMethod method)
369369
Dictionary<MethodDesc, int> weights = null;
370370
List<long> instrumentationDataLongs = null;
371371
PgoSchemaElem[] pgoSchemaData = null;
372+
var methodProfileData = new List<MethodProfileData>();
372373

373374
while (ilReader.HasNext)
374375
{
@@ -552,8 +553,7 @@ private static IEnumerable<MethodProfileData> ReadMIbcGroup(EcmaMethod method)
552553
if (methodInProgress != null)
553554
{
554555
// If the method being loaded didn't have meaningful input, skip
555-
MethodProfileData mibcData = new MethodProfileData((MethodDesc)methodInProgress, MethodProfilingDataFlags.ReadMethodCode, exclusiveWeight, weights, 0xFFFFFFFF, pgoSchemaData);
556-
yield return mibcData;
556+
methodProfileData.Add(new MethodProfileData((MethodDesc)methodInProgress, MethodProfilingDataFlags.ReadMethodCode, exclusiveWeight, weights, 0xFFFFFFFF, pgoSchemaData));
557557
}
558558
state = MibcGroupParseState.LookingForNextMethod;
559559
exclusiveWeight = 0;
@@ -608,6 +608,8 @@ private static IEnumerable<MethodProfileData> ReadMIbcGroup(EcmaMethod method)
608608
}
609609
}
610610
}
611+
612+
return methodProfileData;
611613
}
612614

613615
/// <summary>

0 commit comments

Comments
 (0)