Skip to content

Commit 641b915

Browse files
Force inclusion of lambdas and local functions as opt candidates
An alternative "fix" would be to import these on demand during inlining discovery, but that would miss early optimizations (like SSA promotion), which wouldn't make much up for inlining. Closes #27
1 parent c8f546a commit 641b915

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/DistIL.Cli/Program.cs

+9-2
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,9 @@ Multiple patterns can be separated with '|'.
179179
public void FilterPassCandidates(List<MethodDef> candidates)
180180
{
181181
if (FilterUnmarked) {
182-
candidates.RemoveAll(m => (GetOptimizeAttr(m) ?? GetOptimizeAttr(m.DeclaringType)) is not true);
182+
candidates.RemoveAll(m => (IsMarkedForOpts(m) ?? IsMarkedForOpts(m.DeclaringType)) is not true && !IsGeneratedInnerMethod(m));
183183

184-
static bool? GetOptimizeAttr(ModuleEntity entity)
184+
static bool? IsMarkedForOpts(ModuleEntity entity)
185185
{
186186
foreach (var attr in entity.GetCustomAttribs()) {
187187
if (attr.Type.Namespace != "DistIL.Attributes") continue;
@@ -190,6 +190,13 @@ public void FilterPassCandidates(List<MethodDef> candidates)
190190
}
191191
return null;
192192
}
193+
// Checks if the given method has a mangled lambda or local function name.
194+
// We'll forcibly import these in order to enable inlining into marked methods. See #27
195+
// - https://github.com/dotnet/roslyn/blob/main/src/Compilers/CSharp/Portable/Symbols/Synthesized/GeneratedNames.cs
196+
static bool IsGeneratedInnerMethod(MethodDef method)
197+
{
198+
return method.Name.StartsWith('<') && (method.Name.Contains(">b__") || method.Name.Contains(">g__"));
199+
}
193200
}
194201
if (MethodFilter != null && MethodFilter.StartsWith('!')) {
195202
var pred = GetMethodFilter()!;

0 commit comments

Comments
 (0)