LambdaExpression.CanCompileToIL should respect IsDynamicCodeSupported#80759
LambdaExpression.CanCompileToIL should respect IsDynamicCodeSupported#80759eerhardt merged 2 commits intodotnet:mainfrom
Conversation
With the new feature switch added in dotnet#39806, calling LambdaExpression.Compile is throwing a PlatformNotSupportedException. Instead, LambdaExpression should respect IsDynamicCodeSupported and switch to using the interpreter when IsDynamicCodeSupported is false.
|
Tagging subscribers to this area: @cston Issue DetailsWith the new feature switch added in #39806, calling LambdaExpression.Compile is throwing a PlatformNotSupportedException. Instead, LambdaExpression should respect IsDynamicCodeSupported and switch to using the interpreter when IsDynamicCodeSupported is false. This causes issues when Adding new tests is currently in progress. Opening as draft to get CI and initial feedback
|
|
Would |
No. Just because the generated code isn’t compiled (i.e. JIT’d), it doesn’t mean we should use the Linq.Expressions interpreter. The Mono Interpreter can probably do a better job at executing the generated IL than the Linq.Expressions interpreter can. Note that the Mono interpreter is currently the only environment where these values will be different that affects this code. This is mainly for WASM. On iOS, this value is already false. See #38693 (comment) for an example of a case where generating IL and then interpreting it is preferred. |
| public Delegate Compile(bool preferInterpretation) | ||
| { | ||
| if (CanCompileToIL && CanInterpret && preferInterpretation) | ||
| if (CanInterpret && preferInterpretation) |
There was a problem hiding this comment.
Can you check if this impacts size with NativeAOT?
The dead branch elimination in NativeAOT only considers things that have feature switches. It doesn't attempt to inline things.
I think this is changing things from if (false && CanInterpret && preferInterpretation) to if (CanInterpret && preferInterpretation) (no longer a known constant).
Could be fixed by creating a substitution rule for CanInterpret as well.
(Either that or beefing up the dead branch elimination in NativeAOT. The dead branch elimination in NativeAOT is simpler than in linker because: 1. linker's one is super scary and I don't want to have to deal with the bugs in it that I would get by porting it, and 2. longer term, we should have RyuJIT do the dead branch elimination. RyuJIT is much better at it than anything we would write specifically for trimming.)
There was a problem hiding this comment.
Or maybe this is fine now that I look at it for longer?
There was a problem hiding this comment.
There are 2 reasons why I'm proposing this change.
- Logically, the condition doesn't make sense. Why would
CanCompileToILneed to betruein order forpreferInterpretationbe respected? - Looking at what it functionally does:
IfCanCompileToIL == false, the same code (return new Interpreter.LightCompiler().CompileTop(this).CreateDelegate();) is going to be executed regardless.
There was a problem hiding this comment.
Yes, I think it makes sense to do. I'm responsible for introducing this in #61952.
That pull request was a mechanical replacement of #defines. The code originally looked like:
public Delegate Compile(bool preferInterpretation)
{
#if FEATURE_COMPILE && FEATURE_INTERPRET
if (preferInterpretation)
{
return new Interpreter.LightCompiler().CompileTop(this).CreateDelegate();
}
#endif
return Compile();
}There was a problem hiding this comment.
On a second look, the original code doesn't make that much more sense either...
…dotnet#80759) * LambdaExpression.CanCompileToIL should respect IsDynamicCodeSupported With the new feature switch added in dotnet#39806, calling LambdaExpression.Compile is throwing a PlatformNotSupportedException. Instead, LambdaExpression should respect IsDynamicCodeSupported and switch to using the interpreter when IsDynamicCodeSupported is false. * Add tests
With the new feature switch added in #39806, calling LambdaExpression.Compile is throwing a PlatformNotSupportedException. Instead, LambdaExpression should respect IsDynamicCodeSupported and switch to using the interpreter when IsDynamicCodeSupported is false.
This causes issues when
dotnet runon the newdotnet new api -aottemplate from ASP.NET.Notes
DynamicMethod..ctorthat throws when IsDynamicCodeSupported == false.cc @DamianEdwards