Repository navigation
Description
Description
A NativeAOT version of our project fails at runtime while the related managed version works. There are not publishing warnings and static analysis warnings.
The root cause is that the publishing removes some required code. The code is simple without reflection or any similar dynamic techniques.
Reproduction Steps
Program.cs
namespace AotIssue
{
internal class Program
{
static void Main(string[] args)
{
string s = "12151";
int i = 0;
char? a = null;
while (true)
{
string? res = get(s, ref i, ref a);
if (res != null)
{
Console.Write(res);
a = null; // !!! this line is removed from the published version
// Console.Write(a); // uncomment the line for the workaround
continue;
}
if (i >= s.Length)
break;
a = '.';
}
}
static string? get(string s, ref int index, ref char? a)
{
if (index >= s.Length)
return null;
if (a == '.')
return ".";
a ??= s[index++];
return (a == '1') ? "1" : null;
}
}
}AotIssue.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PublishAot>true</PublishAot>
<InvariantGlobalization>true</InvariantGlobalization>
<IsAotCompatible>true</IsAotCompatible>
</PropertyGroup>
</Project>To publish the project, you can use the dotnet publish -r win-x64 -c Release command. Or similar publish profile:
<?xml version="1.0" encoding="utf-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project>
<PropertyGroup>
<Configuration>Release</Configuration>
<Platform>Any CPU</Platform>
<PublishDir>bin\Release\net8.0\publish\win-x64\</PublishDir>
<PublishProtocol>FileSystem</PublishProtocol>
<_TargetId>Folder</_TargetId>
<TargetFramework>net8.0</TargetFramework>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<SelfContained>true</SelfContained>
<PublishSingleFile>false</PublishSingleFile>
<PublishReadyToRun>false</PublishReadyToRun>
</PropertyGroup>
</Project>Expected behavior
The AOT version prints "1.1.1" like regular .NET version.
Actual behavior
The AOT version prints an infinite sequence of "1111111111111...". The loop never ends.
Regression?
No response
Known Workarounds
Uncomment the line Console.Write(a);. In this case, the code works as expected after publishing.
Configuration
I reproduced the issue in the .NET 8 project with win-x64 target platform on Windows 10.
Other information
The line a = null; is removed from the published version.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsNo status
Activity
am11 commentedon Jun 14, 2024
Repros with 9.0.100-preview.6.24313.50 as well (osx-arm64).
dotnet-policy-service commentedon Jun 14, 2024
Tagging subscribers to this area: @agocke, @MichalStrehovsky, @jkotas
See info in area-owners.md if you want to be subscribed.
dotnet-policy-service commentedon Jun 14, 2024
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.
jkotas commentedon Jun 14, 2024
This repros with
DOTNET_TieredCompilation=0with a JIT as well.[-]NativeAOT publishing removes necessary code[/-][+]Optimization removes necessary code[/+]EgorBo commentedon Jun 17, 2024
Looks to be
optRemoveRedundantZeroInitsissuejakobbotsch commentedon Jun 17, 2024
More specifically,
BB02is not marked withBBF_BACKWARD_JUMPthatoptRemoveRedundantZeroInitsis relying on to check whether some zero inits are necessary. Tail merging managed to get rid of thea = nullinside the loop by rewriting the loop as follows:The
a = nullat the beginning of the loop is in a block that tail merging created but did not mark withBBF_BACKWARD_JUMP, and thenoptRemoveRedundantZeroInitsremoves it.I wonder if we can just reuse some of our flow graph analysis to make this determination during
optRemoveRedundantZeroInits. We have both the DFS tree and loops available at this point, though we still need to consider whether the block is part of any irreducible cycles. It seems very fragile to try to rely on something likeBBF_BACKWARD_JUMPthis late.cc @AndyAyersMS
EgorBo commentedon Jun 17, 2024
@jakobbotsch Yes, I also diagnosed lack of
BBF_BACKWARD_JUMPyesterday due to tail merge - I didn't put up a fix because I was not sure how to fix it in place in fgTailMerge (aroundfgRedirectTargetEdgecall) since I couldn't just rely on, potentially, stale bbNum data. I was wondering iffgRenumberBlocksshould just set this flag3 remaining items
AndyAyersMS commentedon Jun 20, 2024
Have a fix for this implemented for main, should have a PR up soon. It will need to be expressed differently in 8.0.
JIT: rely on DFS for cycle entry detection in optRemoveRedundantZeroI…
JIT: rely on DFS for cycle entry detection in optRemoveRedundantZeroI…
JIT: rely on DFS for cycle entry detection in optRemoveRedundantZeroI…
JIT: fix redundant zero init issue (.NET 8 port)
JIT: fix redundant zero init issue (.NET 8 port)
JIT: fix redundant zero init issue (.NET 8 port)
JIT: fix redundant zero init issue (.NET 8 port)
JIT: fix redundant zero init issue (.NET 8 port) (#103918)