Skip to content
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

[LoongArch64] amend the crossgen2/r2rdump for LA64. #85038

Merged
merged 3 commits into from
May 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1732,16 +1732,8 @@ private void ForceSigWalk()
int floatRegOfsInBytes = argOffset - _transitionBlock.OffsetOfFloatArgumentRegisters;
Debug.Assert((floatRegOfsInBytes % _transitionBlock.FloatRegisterSize) == 0);
pLoc.m_idxFloatReg = floatRegOfsInBytes / _transitionBlock.FloatRegisterSize;
pLoc.m_cFloatReg = 1;

if (!_argTypeHandle.IsNull() && _argTypeHandle.IsHomogeneousAggregate())
{
int haElementSize = _argTypeHandle.GetHomogeneousAggregateElementSize();
pLoc.m_cFloatReg = GetArgSize() / haElementSize;
}
else
{
pLoc.m_cFloatReg = 1;
}
return pLoc;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ protected override void EmitCode(NodeFactory factory, ref LoongArch64Emitter ins

if (!relocsOnly)
{
// movz T0=R12, #index
// ori T0=R12, R0, #index
int index = _containingImportSection.IndexFromBeginningOfArray;
instructionEncoder.EmitMOV(Register.R12, checked((ushort)index));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -651,8 +651,8 @@ private class LoongArch64TransitionBlock : TransitionBlock
public override int NumCalleeSavedRegisters => 12;
// Callee-saves, argument registers
public override int SizeOfTransitionBlock => SizeOfCalleeSavedRegisters + SizeOfArgumentRegisters;
public override int OffsetOfArgumentRegisters => SizeOfCalleeSavedRegisters;
public override int OffsetOfFirstGCRefMapSlot => OffsetOfArgumentRegisters;
public override int OffsetOfFirstGCRefMapSlot => SizeOfCalleeSavedRegisters;
public override int OffsetOfArgumentRegisters => OffsetOfFirstGCRefMapSlot;

// F0..F7
public override int OffsetOfFloatArgumentRegisters => 8 * sizeof(double);
Expand All @@ -671,19 +671,11 @@ public override bool IsArgPassedByRef(TypeHandle th)
}
else
{
int numIntroducedFields = 0;
foreach (FieldDesc field in th.GetRuntimeTypeHandle().GetFields())
{
if (!field.IsStatic)
{
numIntroducedFields++;
}
}
return ((numIntroducedFields == 0) || (numIntroducedFields > 2));
return false;
}
}

public sealed override int GetRetBuffArgOffset(bool hasThis) => OffsetOfArgumentRegisters;
public sealed override int GetRetBuffArgOffset(bool hasThis) => OffsetOfFirstGCRefMapSlot + (hasThis ? 8 : 0);

public override int StackElemSize(int parmSize, bool isValueType = false, bool isFloatHfa = false)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,14 @@ public override string ToString()

sb.AppendLine($" Has Tailcalls: {_wantsReportOnlyLeaf}");
}
else if (_machine == Machine.LoongArch64)
{
if (StackBaseRegister != 0xffffffff)
{
sb.AppendLine($" StackBaseRegister: {(LoongArch64.Registers)StackBaseRegister}");
}
sb.AppendLine($" Has Tailcalls: {_wantsReportOnlyLeaf}");
}

sb.AppendLine($" Size of parameter area: 0x{SizeOfStackOutgoingAndScratchArea:X}");
if (SizeOfEditAndContinuePreservedArea != 0xffffffff)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ private static string GetRegisterName(int registerNumber, Machine machine)
case Machine.Arm64:
return ((Arm64.Registers)registerNumber).ToString();

case Machine.LoongArch64:
return ((LoongArch64.Registers)registerNumber).ToString();

default:
throw new NotImplementedException(machine.ToString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ public string GetSlotState(GcSlotTable slotTable, Machine machine)
regType = typeof(Amd64.Registers);
break;

case Machine.LoongArch64:
regType = typeof(LoongArch64.Registers);
break;

default:
throw new NotImplementedException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ public static string GetPlatformSpecificRegister(Machine machine, int regnum)
return ((Arm.Registers)regnum).ToString();
case Machine.Arm64:
return ((Arm64.Registers)regnum).ToString();
case Machine.LoongArch64:
return ((LoongArch64.Registers)regnum).ToString();
default:
throw new NotImplementedException($"No implementation for machine type {machine}.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,11 @@ internal GcInfoTypes(Machine machine)
NUM_UNTRACKED_SLOTS_ENCBASE = 5;
REGISTER_DELTA_ENCBASE = 3;
break;
case Machine.LoongArch64:
SIZE_OF_RETURN_KIND_FAT = 4;
STACK_BASE_REGISTER_ENCBASE = 2;
NUM_REGISTERS_ENCBASE = 3;
break;
}
}

Expand All @@ -159,6 +164,7 @@ internal int DenormalizeCodeLength(int x)
case Machine.ArmThumb2:
return (x << 1);
case Machine.Arm64:
case Machine.LoongArch64:
return (x << 2);
}
return x;
Expand All @@ -173,6 +179,7 @@ internal int DenormalizeStackSlot(int x)
case Machine.ArmThumb2:
return (x << 2);
case Machine.Arm64:
case Machine.LoongArch64:
return (x << 3);
}
return x;
Expand All @@ -188,6 +195,8 @@ internal uint DenormalizeStackBaseRegister(uint x)
return ((x ^ 7) + 4);
case Machine.Arm64:
return (x ^ 29);
case Machine.LoongArch64:
return ((x ^ 22) & 0x3);
}
return x;
}
Expand All @@ -201,6 +210,7 @@ internal uint DenormalizeSizeOfStackArea(uint x)
case Machine.ArmThumb2:
return (x << 2);
case Machine.Arm64:
case Machine.LoongArch64:
return (x << 3);
}
return x;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// 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.Collections.Generic;
using System.Text;

namespace ILCompiler.Reflection.ReadyToRun.LoongArch64
{
public enum Registers
{
R0,
Ra,
Tp,
Sp,
A0,
A1,
A2,
A3,
A4,
A5,
A6,
A7,
T0,
T1,
T2,
T3,
T4,
T5,
T6,
T7,
T8,
X0,
Fp,
S0,
S1,
S2,
S3,
S4,
S5,
S6,
S7,
S8
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Text;

namespace ILCompiler.Reflection.ReadyToRun.LoongArch64
{
public class Epilog
{
public int Index { get; set; }

public uint EpilogStartOffset { get; set; }
public uint Res { get; set; }
public uint Condition { get; set; }
public uint EpilogStartIndex { get; set; }
public uint EpilogStartOffsetFromMainFunctionBegin { get; set; }

public Epilog() { }

public Epilog(int index, int dw, uint startOffset)
{
Index = index;

EpilogStartOffset = UnwindInfo.ExtractBits(dw, 0, 18);
Res = UnwindInfo.ExtractBits(dw, 18, 4);
Condition = UnwindInfo.ExtractBits(dw, 20, 4);
EpilogStartIndex = UnwindInfo.ExtractBits(dw, 22, 10);

// Note that epilogStartOffset for a funclet is the offset from the beginning
// of the current funclet, not the offset from the beginning of the main function.
// To help find it when looking through JitDump output, also show the offset from
// the beginning of the main function.
EpilogStartOffsetFromMainFunctionBegin = EpilogStartOffset * 4 + startOffset;
}

public override string ToString()
{
StringBuilder sb = new StringBuilder();
sb.AppendLine($" Epilog Start Offset: 0x{EpilogStartOffset:X5} Actual offset = 0x{EpilogStartOffset * 4:X5} Offset from main function begin = 0x{EpilogStartOffsetFromMainFunctionBegin:X6}");
sb.AppendLine($" Condition: {Condition} (0x{Condition:X})" + ((Condition == 0xE) ? " (always)" : ""));
sb.Append($" Epilog Start Index: {EpilogStartIndex} (0x{EpilogStartIndex:X})");
return sb.ToString();
}
}

public class UnwindCode
{
public int Index { get; set; }

public UnwindCode() { }

public UnwindCode(int index)
{
Index = index;

}
}

/// <summary>
/// based on <a href="https://github.com/dotnet/runtime/src/coreclr/jit/unwindloongarch64.cpp">src/jit/unwindloongarch64.cpp</a> DumpUnwindInfo
/// </summary>
public class UnwindInfo : BaseUnwindInfo
{
public uint CodeWords { get; set; }
public uint EpilogCount { get; set; }
public uint EBit { get; set; }
public uint XBit { get; set; }
public uint Vers { get; set; }
public uint FunctionLength { get; set; }

public uint ExtendedCodeWords { get; set; }
public uint ExtendedEpilogCount { get; set; }

public Epilog[] Epilogs { get; set; }

public UnwindInfo() { }

public UnwindInfo(byte[] image, int offset)
{
uint startOffset = (uint)offset;

int dw = NativeReader.ReadInt32(image, ref offset);
CodeWords = ExtractBits(dw, 27, 5);
EpilogCount = ExtractBits(dw, 22, 5);
EBit = ExtractBits(dw, 21, 1);
XBit = ExtractBits(dw, 20, 1);
Vers = ExtractBits(dw, 18, 2);
FunctionLength = ExtractBits(dw, 0, 18) * 4;

if (CodeWords == 0 && EpilogCount == 0)
{
// We have an extension word specifying a larger number of Code Words or Epilog Counts
// than can be specified in the header word.
dw = NativeReader.ReadInt32(image, ref offset);
ExtendedCodeWords = ExtractBits(dw, 16, 8);
ExtendedEpilogCount = ExtractBits(dw, 0, 16);
}

bool[] epilogStartAt = new bool[1024]; // One byte per possible epilog start index; initialized to false

if (EBit == 0)
{
Epilogs = new Epilog[EpilogCount];
if (EpilogCount != 0)
{
for (int scope = 0; scope < EpilogCount; scope++)
{
dw = NativeReader.ReadInt32(image, ref offset);
Epilogs[scope] = new Epilog(scope, dw, startOffset);
epilogStartAt[Epilogs[scope].EpilogStartIndex] = true; // an epilog starts at this offset in the unwind codes
}
}
}
else
{
Epilogs = new Epilog[0];
epilogStartAt[EpilogCount] = true; // the one and only epilog starts its unwind codes at this offset
}



Size = offset - (int)startOffset + (int)CodeWords * 4;
int alignmentPad = ((Size + sizeof(int) - 1) & ~(sizeof(int) - 1)) - Size;
Size += (alignmentPad + sizeof(uint));
}

public override string ToString()
{
StringBuilder sb = new StringBuilder();
sb.AppendLine($" CodeWords: {CodeWords}");
sb.AppendLine($" EpilogCount: {EpilogCount}");
sb.AppendLine($" EBit: {EBit}");
sb.AppendLine($" XBit: {XBit}");
sb.AppendLine($" Vers: {Vers}");
sb.AppendLine($" FunctionLength: {FunctionLength}");
if (CodeWords == 0 && EpilogCount == 0)
{
sb.AppendLine(" ---- Extension word ----");
sb.AppendLine($" Extended Code Words: {CodeWords}");
sb.AppendLine($" Extended Epilog Count: {EpilogCount}");
}
if (EpilogCount == 0)
{
sb.AppendLine(" No epilogs");
}
else
{
for (int i = 0; i < Epilogs.Length; i++)
{
sb.AppendLine(" -------------------------");
sb.AppendLine(Epilogs[i].ToString());
sb.AppendLine(" -------------------------");
}
}
return sb.ToString();
}

internal static uint ExtractBits(int dw, int start, int length)
{
return (uint)((dw >> start) & ((1 << length) - 1));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,10 @@ private int GetSize()
{
return (int)arm64Info.FunctionLength;
}
else if (UnwindInfo is LoongArch64.UnwindInfo loongarch64Info)
{
return (int)loongarch64Info.FunctionLength;
}
else if (Method.GcInfo != null)
{
return Method.GcInfo.CodeLength;
Expand Down Expand Up @@ -488,7 +492,7 @@ private void EnsureInitialized()
}
else
{
// Arm and Arm64 use the same GcInfo format as Amd64
// Arm, Arm64 and LoongArch64 use the same GcInfo format as Amd64
_gcInfo = new Amd64.GcInfo(_readyToRunReader.Image, gcInfoOffset, _readyToRunReader.Machine, _readyToRunReader.ReadyToRunHeader.MajorVersion);
}
}
Expand Down Expand Up @@ -604,6 +608,10 @@ private void ParseRuntimeFunctions(bool partial)
{
unwindInfo = new Arm64.UnwindInfo(_readyToRunReader.Image, unwindOffset);
}
else if (_readyToRunReader.Machine == Machine.LoongArch64)
{
unwindInfo = new LoongArch64.UnwindInfo(_readyToRunReader.Image, unwindOffset);
}

if (i == 0 && unwindInfo != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1415,6 +1415,7 @@ private void EnsureImportSections()

case Machine.Amd64:
case Machine.Arm64:
case Machine.LoongArch64:
entrySize = 8;
break;

Expand Down
Loading