Skip to content

Commit

Permalink
Spanify ILReader
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
PaulusParssinen committed Jan 31, 2024
1 parent f545368 commit a5087ed
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 226 deletions.
69 changes: 30 additions & 39 deletions src/coreclr/tools/Common/TypeSystem/IL/ILReader.cs
Original file line number Diff line number Diff line change
@@ -1,42 +1,27 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Buffers.Binary;

using Internal.TypeSystem;

using Debug = System.Diagnostics.Debug;

namespace Internal.IL
{
internal struct ILReader
internal ref struct ILReader
{
private int _currentOffset;
private readonly byte[] _ilBytes;
private readonly ReadOnlySpan<byte> _ilBytes;

public int Offset
{
get
{
return _currentOffset;
}
}
public readonly int Offset => _currentOffset;

public int Size
{
get
{
return _ilBytes.Length;
}
}
public readonly int Size => _ilBytes.Length;

public bool HasNext
{
get
{
return _currentOffset < _ilBytes.Length;
}
}
public readonly bool HasNext => _currentOffset < _ilBytes.Length;

public ILReader(byte[] ilBytes, int currentOffset = 0)
public ILReader(ReadOnlySpan<byte> ilBytes, int currentOffset = 0)
{
_ilBytes = ilBytes;
_currentOffset = currentOffset;
Expand All @@ -56,22 +41,20 @@ public byte ReadILByte()

public ushort ReadILUInt16()
{
if (_currentOffset + 2 > _ilBytes.Length)
if (!BinaryPrimitives.TryReadUInt16LittleEndian(_ilBytes.Slice(_currentOffset), out ushort value))
ThrowHelper.ThrowInvalidProgramException();

ushort val = (ushort)(_ilBytes[_currentOffset] + (_ilBytes[_currentOffset + 1] << 8));
_currentOffset += 2;
return val;
_currentOffset += sizeof(ushort);
return value;
}

public uint ReadILUInt32()
{
if (_currentOffset + 4 > _ilBytes.Length)
if (!BinaryPrimitives.TryReadUInt32LittleEndian(_ilBytes.Slice(_currentOffset), out uint value))
ThrowHelper.ThrowInvalidProgramException();

uint val = (uint)(_ilBytes[_currentOffset] + (_ilBytes[_currentOffset + 1] << 8) + (_ilBytes[_currentOffset + 2] << 16) + (_ilBytes[_currentOffset + 3] << 24));
_currentOffset += 4;
return val;
_currentOffset += sizeof(uint);
return value;
}

public int ReadILToken()
Expand All @@ -81,21 +64,29 @@ public int ReadILToken()

public ulong ReadILUInt64()
{
ulong value = ReadILUInt32();
value |= (((ulong)ReadILUInt32()) << 32);
if (!BinaryPrimitives.TryReadUInt64LittleEndian(_ilBytes.Slice(_currentOffset), out ulong value))
ThrowHelper.ThrowInvalidProgramException();

_currentOffset += sizeof(ulong);
return value;
}

public unsafe float ReadILFloat()
public float ReadILFloat()
{
uint value = ReadILUInt32();
return *(float*)(&value);
if (!BinaryPrimitives.TryReadSingleLittleEndian(_ilBytes.Slice(_currentOffset), out float value))
ThrowHelper.ThrowInvalidProgramException();

_currentOffset += sizeof(float);
return value;
}

public unsafe double ReadILDouble()
{
ulong value = ReadILUInt64();
return *(double*)(&value);
if (!BinaryPrimitives.TryReadDoubleLittleEndian(_ilBytes.Slice(_currentOffset), out double value))
ThrowHelper.ThrowInvalidProgramException();

_currentOffset += sizeof(double);
return value;
}

public ILOpcode ReadILOpcode()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,7 @@ public int MoveNext(int offset)
_foundEndOfPrevBlock = false;
}

var reader = new ILReader(_methodBody.GetILBytes());
reader.Seek(offset);
var reader = new ILReader(_methodBody.GetILBytes(), offset);
ILOpcode opcode = reader.ReadILOpcode();
if (opcode.IsControlFlowInstruction())
{
Expand Down
181 changes: 0 additions & 181 deletions src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/ILStreamReader.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,6 @@
<Compile Include="Compiler\DependencyAnalysis\ShadowConcreteUnboxingThunkNode.cs" />
<Compile Include="Compiler\ILScanner.cs" />
<Compile Include="Compiler\ILScannerBuilder.cs" />
<Compile Include="Compiler\ILStreamReader.cs" />
<Compile Include="Compiler\LibraryInitializers.cs" />
<Compile Include="Compiler\Compilation.cs" />
<Compile Include="Compiler\CompilerMetadataFieldLayoutAlgorithm.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ private enum MibcGroupParseState
///
/// This format is designed to be extensible to hold more data as we add new per method profile data without breaking existing parsers.
/// </summary>
private static IEnumerable<MethodProfileData> ReadMIbcGroup(EcmaMethod method)
private static List<MethodProfileData> ReadMIbcGroup(EcmaMethod method)
{
EcmaMethodIL ilBody = EcmaMethodIL.Create(method);
MetadataLoaderForPgoData metadataLoader = new MetadataLoaderForPgoData(ilBody);
Expand All @@ -369,6 +369,7 @@ private static IEnumerable<MethodProfileData> ReadMIbcGroup(EcmaMethod method)
Dictionary<MethodDesc, int> weights = null;
List<long> instrumentationDataLongs = null;
PgoSchemaElem[] pgoSchemaData = null;
var methodProfileData = new List<MethodProfileData>();

while (ilReader.HasNext)
{
Expand Down Expand Up @@ -552,8 +553,7 @@ private static IEnumerable<MethodProfileData> ReadMIbcGroup(EcmaMethod method)
if (methodInProgress != null)
{
// If the method being loaded didn't have meaningful input, skip
MethodProfileData mibcData = new MethodProfileData((MethodDesc)methodInProgress, MethodProfilingDataFlags.ReadMethodCode, exclusiveWeight, weights, 0xFFFFFFFF, pgoSchemaData);
yield return mibcData;
methodProfileData.Add(new MethodProfileData((MethodDesc)methodInProgress, MethodProfilingDataFlags.ReadMethodCode, exclusiveWeight, weights, 0xFFFFFFFF, pgoSchemaData));
}
state = MibcGroupParseState.LookingForNextMethod;
exclusiveWeight = 0;
Expand Down Expand Up @@ -608,6 +608,8 @@ private static IEnumerable<MethodProfileData> ReadMIbcGroup(EcmaMethod method)
}
}
}

return methodProfileData;
}

/// <summary>
Expand Down

0 comments on commit a5087ed

Please sign in to comment.