Skip to content

Commit

Permalink
Allows CustomAttribute table to be unsorted in EnC delta (#93524)
Browse files Browse the repository at this point in the history
* Allows CustomAttribute table to be unsorted in EnC delta

* Sorted flags and test
  • Loading branch information
tmat authored Oct 17, 2023
1 parent 82c936b commit 185ef72
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
Microsoft Visual Studio Solution File, Format Version 12.00

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.9.34213.114
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestUtilities", "..\Common\tests\TestUtilities\TestUtilities.csproj", "{2231787B-18C9-493C-A102-1E0E6A3D2CD3}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "System.Collections.Immutable", "..\System.Collections.Immutable\ref\System.Collections.Immutable.csproj", "{282C76D4-54C5-44BF-9F15-1A4302234DBB}"
Expand Down Expand Up @@ -39,11 +43,11 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{5E8418DF-67A
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "gen", "gen", "{3FDA41E5-7DFA-48DC-856B-09E5747E3C9C}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "gen", "tools\gen", "{FA7C2C03-A446-4807-A092-9A3ABD5E5FD7}"
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "gen", "gen", "{FA7C2C03-A446-4807-A092-9A3ABD5E5FD7}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "tools\src", "{19CD37AB-E6A2-431C-8893-5ED903B847BA}"
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{19CD37AB-E6A2-431C-8893-5ED903B847BA}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "ref", "tools\ref", "{B80A55DB-C1C4-4A99-A1C8-BFD6606C28AF}"
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "ref", "ref", "{B80A55DB-C1C4-4A99-A1C8-BFD6606C28AF}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tools", "tools", "{5224A519-F2DB-487E-AB04-CB1C45853202}"
EndProject
Expand Down Expand Up @@ -123,26 +127,30 @@ Global
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{2231787B-18C9-493C-A102-1E0E6A3D2CD3} = {57E0E2C2-76DD-4219-B164-E513C4F8BE01}
{7EE935DD-2F8B-4C72-BACF-5DB95DE080BE} = {57E0E2C2-76DD-4219-B164-E513C4F8BE01}
{282C76D4-54C5-44BF-9F15-1A4302234DBB} = {0829DF11-6008-4678-B608-D754E87EBC70}
{A69B0EE0-BE0C-4D53-A16F-5465028D975D} = {5E8418DF-67A1-4FA6-9241-215415C4D50A}
{2A92C2EE-358D-4DFE-8AA4-4D480623F3BF} = {0829DF11-6008-4678-B608-D754E87EBC70}
{587255BE-DC22-4B85-9E3F-02325E7B4FF7} = {0829DF11-6008-4678-B608-D754E87EBC70}
{4D7BBEC2-8A8D-4BAA-AD6B-55905F107317} = {0829DF11-6008-4678-B608-D754E87EBC70}
{A69B0EE0-BE0C-4D53-A16F-5465028D975D} = {5E8418DF-67A1-4FA6-9241-215415C4D50A}
{B905521A-FE25-4D35-9929-B2622F590263} = {5E8418DF-67A1-4FA6-9241-215415C4D50A}
{7EE935DD-2F8B-4C72-BACF-5DB95DE080BE} = {57E0E2C2-76DD-4219-B164-E513C4F8BE01}
{D4293B41-CA47-46DB-8E11-C532E8E8DAF1} = {3FDA41E5-7DFA-48DC-856B-09E5747E3C9C}
{1092AC3C-B01C-4AC4-9F05-6FDB23C699B3} = {3FDA41E5-7DFA-48DC-856B-09E5747E3C9C}
{930B1167-83BE-42CC-BFED-096CB2BA19F1} = {3FDA41E5-7DFA-48DC-856B-09E5747E3C9C}
{4D7BBEC2-8A8D-4BAA-AD6B-55905F107317} = {0829DF11-6008-4678-B608-D754E87EBC70}
{8E173D5F-6011-4471-8514-FF9C7D4417B5} = {FA7C2C03-A446-4807-A092-9A3ABD5E5FD7}
{064431FF-EE86-4C61-8859-0F824AD2B69A} = {FA7C2C03-A446-4807-A092-9A3ABD5E5FD7}
{FA7C2C03-A446-4807-A092-9A3ABD5E5FD7} = {5224A519-F2DB-487E-AB04-CB1C45853202}
{35B39665-CBB5-449F-B51D-91B1B021266F} = {19CD37AB-E6A2-431C-8893-5ED903B847BA}
{98445B72-AFD2-462C-94F2-65E5C1A25B22} = {19CD37AB-E6A2-431C-8893-5ED903B847BA}
{19CD37AB-E6A2-431C-8893-5ED903B847BA} = {5224A519-F2DB-487E-AB04-CB1C45853202}
{CDFCF052-9073-42E3-BC4F-5283E9BFAD1B} = {B80A55DB-C1C4-4A99-A1C8-BFD6606C28AF}
{FA7C2C03-A446-4807-A092-9A3ABD5E5FD7} = {5224A519-F2DB-487E-AB04-CB1C45853202}
{19CD37AB-E6A2-431C-8893-5ED903B847BA} = {5224A519-F2DB-487E-AB04-CB1C45853202}
{B80A55DB-C1C4-4A99-A1C8-BFD6606C28AF} = {5224A519-F2DB-487E-AB04-CB1C45853202}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {64BB97AB-FD23-40BA-B638-FE4756AE6452}
EndGlobalSection
GlobalSection(SharedMSBuildProjectFiles) = preSolution
..\..\tools\illink\src\ILLink.Shared\ILLink.Shared.projitems*{064431ff-ee86-4c61-8859-0f824ad2b69a}*SharedItemsImports = 5
..\..\tools\illink\src\ILLink.Shared\ILLink.Shared.projitems*{98445b72-afd2-462c-94f2-65e5c1a25b22}*SharedItemsImports = 5
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
Expand Up @@ -1894,13 +1894,17 @@ private static void SerializeTablesHeader(BlobBuilder writer, MetadataSizes meta

if (metadataSizes.IsEncDelta)
{
heapSizes |= (HeapSizeFlag.EncDeltas | HeapSizeFlag.DeletedMarks);
heapSizes |= HeapSizeFlag.EncDeltas | HeapSizeFlag.DeletedMarks;
}

ulong sortedDebugTables = metadataSizes.PresentTablesMask & MetadataSizes.SortedDebugTables;
// Custom Attribute table is not sorted in delta metadata:
ulong sortedTables =
metadataSizes.IsEncDelta ? MetadataSizes.SortedTypeSystemTables & ~(1UL << (int)TableIndex.CustomAttribute) :
metadataSizes.IsStandaloneDebugMetadata ? 0 :
MetadataSizes.SortedTypeSystemTables;

// Consider filtering out type system tables that are not present:
ulong sortedTables = sortedDebugTables | (metadataSizes.IsStandaloneDebugMetadata ? 0UL : 0x16003301fa00);
sortedTables |= metadataSizes.PresentTablesMask & MetadataSizes.SortedDebugTables;

writer.WriteUInt32(0); // reserved
writer.WriteByte(MetadataFormatMajorVersion);
Expand Down Expand Up @@ -2048,7 +2052,9 @@ private void SerializeCustomAttributeTable(BlobBuilder writer, MetadataSizes met
{
// Note: we can sort the table at this point since no other table can reference its rows via RowId or CodedIndex (which would need updating otherwise).
// OrderBy performs a stable sort, so multiple attributes with the same parent will be sorted in the order they were added to the table.
var ordered = _customAttributeTableNeedsSorting ? _customAttributeTable.OrderBy((x, y) => x.Parent - y.Parent) : _customAttributeTable;
// Avoid sorting the table when emitting EnC delta. Deleted attributes are represented in the table as rows with nil Parent field.
// Sorting the table would move them to the beginning of the table and break mapping specified in EncMap table.
var ordered = _customAttributeTableNeedsSorting && !metadataSizes.IsEncDelta ? _customAttributeTable.OrderBy((x, y) => x.Parent - y.Parent) : _customAttributeTable;

foreach (CustomAttributeRow customAttribute in ordered)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,22 @@ public sealed class MetadataSizes

internal readonly int MetadataVersionPaddedLength;

internal const ulong SortedTypeSystemTables =
1UL << (int)TableIndex.InterfaceImpl |
1UL << (int)TableIndex.Constant |
1UL << (int)TableIndex.CustomAttribute |
1UL << (int)TableIndex.FieldMarshal |
1UL << (int)TableIndex.DeclSecurity |
1UL << (int)TableIndex.ClassLayout |
1UL << (int)TableIndex.FieldLayout |
1UL << (int)TableIndex.MethodSemantics |
1UL << (int)TableIndex.MethodImpl |
1UL << (int)TableIndex.ImplMap |
1UL << (int)TableIndex.FieldRva |
1UL << (int)TableIndex.NestedClass |
1UL << (int)TableIndex.GenericParam |
1UL << (int)TableIndex.GenericParamConstraint;

internal const ulong SortedDebugTables =
1UL << (int)TableIndex.LocalScope |
1UL << (int)TableIndex.StateMachineMethod |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ public void EncHeaders()
{
var mdBuilder = new MetadataBuilder();
mdBuilder.AddEncLogEntry(MetadataTokens.MethodDefinitionHandle(1), EditAndContinueOperation.AddMethod);
mdBuilder.AddCustomAttribute(MetadataTokens.MethodDefinitionHandle(1), MetadataTokens.MethodDefinitionHandle(1), default);
mdBuilder.AddCustomAttribute(MetadataTokens.MethodDefinitionHandle(0), MetadataTokens.MethodDefinitionHandle(1), default);

var rootBuilder = new MetadataRootBuilder(mdBuilder);

Expand Down Expand Up @@ -181,26 +183,26 @@ public void EncHeaders()

// stream headers:
0x7C, 0x00, 0x00, 0x00,
0x28, 0x00, 0x00, 0x00,
0x44, 0x00, 0x00, 0x00,
(byte)'#', (byte)'-', 0x00, 0x00,

0xA4, 0x00, 0x00, 0x00,
0xC0, 0x00, 0x00, 0x00,
0x04, 0x00, 0x00, 0x00,
(byte)'#', (byte)'S', (byte)'t', (byte)'r', (byte)'i', (byte)'n', (byte)'g', (byte)'s', 0x00, 0x00, 0x00, 0x00,

0xA8, 0x00, 0x00, 0x00,
0xC4, 0x00, 0x00, 0x00,
0x04, 0x00, 0x00, 0x00,
(byte)'#', (byte)'U', (byte)'S', 0x00,

0xAC, 0x00, 0x00, 0x00,
0xC8, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
(byte)'#', (byte)'G', (byte)'U', (byte)'I', (byte)'D', 0x00, 0x00, 0x00,

0xAC, 0x00, 0x00, 0x00,
0xC8, 0x00, 0x00, 0x00,
0x04, 0x00, 0x00, 0x00,
(byte)'#', (byte)'B', (byte)'l', (byte)'o', (byte)'b', 0x00, 0x00, 0x00,

0xB0, 0x00, 0x00, 0x00,
0xCC, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
(byte)'#', (byte)'J', (byte)'T', (byte)'D', 0x00, 0x00, 0x00, 0x00,

Expand All @@ -224,20 +226,27 @@ public void EncHeaders()
0x01,

// Present tables
0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00,
0x00, 0x10, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00,

// Sorted tables
0x00, 0xFA, 0x01, 0x33, 0x00, 0x16, 0x00, 0x00,
0x00, 0xEA, 0x01, 0x33, 0x00, 0x16, 0x00, 0x00,

// Rows
0x02, 0x00, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00,

//
// Custom Attribute table (token, token, token)
//

0x20, 0x00, 0x00, 0x00, 0x0A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x0A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,

//
// EncLog Table (token, operation)
//

0x01, 0x00, 0x00, 0x06,
0x01, 0x00, 0x00, 0x00,
0x01, 0x00, 0x00, 0x06, 0x01, 0x00, 0x00, 0x00,

// Padding and alignment
0x00, 0x00, 0x00, 0x00,
Expand Down

0 comments on commit 185ef72

Please sign in to comment.