|
| 1 | +using AsmResolver.DotNet; |
| 2 | +using AsmResolver.PE.DotNet.Metadata.Tables.Rows; |
| 3 | +using Microsoft.Build.Framework; |
| 4 | +using Microsoft.Build.Utilities; |
1 | 5 | using System;
|
2 | 6 | using System.IO;
|
3 | 7 | using System.Linq;
|
4 |
| -using Microsoft.Build.Framework; |
5 |
| -using Microsoft.Build.Utilities; |
6 |
| -using Mono.Cecil; |
7 | 8 |
|
8 | 9 | namespace BenchmarkDotNet.Weaver;
|
9 | 10 |
|
10 |
| -internal class CustomAssemblyResolver : DefaultAssemblyResolver |
11 |
| -{ |
12 |
| - public override AssemblyDefinition Resolve(AssemblyNameReference name, ReaderParameters parameters) |
13 |
| - // NetStandard causes StackOverflow. https://github.com/jbevain/cecil/issues/573 |
14 |
| - // Mscorlib fails to resolve in Visual Studio. https://github.com/jbevain/cecil/issues/966 |
15 |
| - // We don't care about any types from runtime assemblies anyway, so just skip resolving them. |
16 |
| - => name.Name is "netstandard" or "mscorlib" or "System.Runtime" or "System.Private.CoreLib" |
17 |
| - ? null |
18 |
| - : base.Resolve(name, parameters); |
19 |
| -} |
20 |
| - |
21 | 11 | /// <summary>
|
22 | 12 | /// The Task used by MSBuild to weave the assembly.
|
23 | 13 | /// </summary>
|
@@ -45,72 +35,52 @@ public override bool Execute()
|
45 | 35 | {
|
46 | 36 | Log.LogError($"Assembly not found: {TargetAssembly}");
|
47 | 37 | return false;
|
48 |
| - } |
49 |
| - |
50 |
| - var resolver = new CustomAssemblyResolver(); |
51 |
| - resolver.AddSearchDirectory(TargetDir); |
52 |
| - |
53 |
| - // ReaderParameters { ReadWrite = true } is necessary to later write the file. |
54 |
| - // https://stackoverflow.com/questions/41840455/locked-target-assembly-with-mono-cecil-and-pcl-code-injection |
55 |
| - var readerParameters = new ReaderParameters |
56 |
| - { |
57 |
| - ReadWrite = true, |
58 |
| - AssemblyResolver = resolver |
59 |
| - }; |
| 38 | + } |
60 | 39 |
|
61 |
| - bool benchmarkMethodsImplAdjusted = false; |
62 |
| - try |
63 |
| - { |
64 |
| - using var module = ModuleDefinition.ReadModule(TargetAssembly, readerParameters); |
65 |
| - |
66 |
| - foreach (var type in module.Types) |
67 |
| - { |
68 |
| - ProcessType(type, ref benchmarkMethodsImplAdjusted); |
69 |
| - } |
70 |
| - |
71 |
| - // Write the modified assembly to file. |
72 |
| - module.Write(); |
73 |
| - } |
74 |
| - catch (Exception e) |
75 |
| - { |
76 |
| - if (benchmarkMethodsImplAdjusted) |
| 40 | + // Load the assembly using AsmResolver. |
| 41 | + var module = ModuleDefinition.FromFile(TargetAssembly); |
| 42 | + |
| 43 | + bool benchmarkMethodsImplAdjusted = false; |
| 44 | + try |
| 45 | + { |
| 46 | + foreach (var type in module.GetAllTypes()) |
77 | 47 | {
|
78 |
| - Log.LogWarning($"Benchmark methods were found that require NoInlining, and assembly weaving failed.{Environment.NewLine}{e}"); |
79 |
| - } |
80 |
| - } |
| 48 | + // We can skip non-public types as they are not valid for benchmarks. |
| 49 | + if (type.IsNotPublic) |
| 50 | + { |
| 51 | + continue; |
| 52 | + } |
| 53 | + |
| 54 | + foreach (var method in type.Methods) |
| 55 | + { |
| 56 | + if (method.CustomAttributes.Any(IsBenchmarkAttribute)) |
| 57 | + { |
| 58 | + var oldImpl = method.ImplAttributes; |
| 59 | + // Remove AggressiveInlining and add NoInlining. |
| 60 | + const MethodImplAttributes AggressiveInlining = (MethodImplAttributes) 512; |
| 61 | + method.ImplAttributes = (oldImpl & ~AggressiveInlining) | MethodImplAttributes.NoInlining; |
| 62 | + benchmarkMethodsImplAdjusted |= (oldImpl & MethodImplAttributes.NoInlining) == 0; |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + // Write the modified assembly to file. |
| 68 | + module.Write(TargetAssembly); |
| 69 | + } |
| 70 | + catch (Exception e) |
| 71 | + { |
| 72 | + if (benchmarkMethodsImplAdjusted) |
| 73 | + { |
| 74 | + Log.LogWarning($"Benchmark methods were found that require NoInlining, and assembly weaving failed.{Environment.NewLine}{e}"); |
| 75 | + } |
| 76 | + } |
81 | 77 | return true;
|
82 | 78 | }
|
83 | 79 |
|
84 |
| - private static void ProcessType(TypeDefinition type, ref bool benchmarkMethodsImplAdjusted) |
85 |
| - { |
86 |
| - // We can skip non-public types as they are not valid for benchmarks. |
87 |
| - if (type.IsNotPublic) |
88 |
| - { |
89 |
| - return; |
90 |
| - } |
91 |
| - |
92 |
| - // Remove AggressiveInlining and add NoInlining to all [Benchmark] methods. |
93 |
| - foreach (var method in type.Methods) |
94 |
| - { |
95 |
| - if (method.CustomAttributes.Any(IsBenchmarkAttribute)) |
96 |
| - { |
97 |
| - var oldImpl = method.ImplAttributes; |
98 |
| - method.ImplAttributes = (oldImpl & ~MethodImplAttributes.AggressiveInlining) | MethodImplAttributes.NoInlining; |
99 |
| - benchmarkMethodsImplAdjusted |= (oldImpl & MethodImplAttributes.NoInlining) == 0; |
100 |
| - } |
101 |
| - } |
102 |
| - |
103 |
| - // Recursively process nested types |
104 |
| - foreach (var nestedType in type.NestedTypes) |
105 |
| - { |
106 |
| - ProcessType(nestedType, ref benchmarkMethodsImplAdjusted); |
107 |
| - } |
108 |
| - } |
109 |
| - |
110 | 80 | private static bool IsBenchmarkAttribute(CustomAttribute attribute)
|
111 | 81 | {
|
112 | 82 | // BenchmarkAttribute is unsealed, so we need to walk its hierarchy.
|
113 |
| - for (var attr = attribute.AttributeType; attr != null; attr = attr.Resolve()?.BaseType) |
| 83 | + for (var attr = attribute.Constructor.DeclaringType; attr != null; attr = attr.Resolve()?.BaseType) |
114 | 84 | {
|
115 | 85 | if (attr.FullName == "BenchmarkDotNet.Attributes.BenchmarkAttribute")
|
116 | 86 | {
|
|
0 commit comments