runtime: JIT: nullcheck is not folded for PHI

Seems like global assertprop bails out on the following snippet

bool Test(bool cond)
{
    object tmp;
    if (cond)
        tmp = "1111";
    else
        tmp = "2222";
    return tmp != null;
}

Current codegen:

; Assembly listing for method Proga:Test(ubyte):ubyte (FullOpts)
G_M26246_IG01:  ;; offset=0x0000
G_M26246_IG02:  ;; offset=0x0000
       mov      rax, 0x20000209798      ; '2222'
       mov      rdx, 0x200002097B8      ; '1111'
       test     cl, cl
       cmovne   rax, rdx
       test     rax, rax
       setne    al
       movzx    rax, al
G_M26246_IG03:  ;; offset=0x0023
       ret      

Expected codegen:

; Assembly listing for method Proga:Test(ubyte):ubyte (FullOpts)
G_M26246_IG01:  ;; offset=0x0000
G_M26246_IG02:  ;; offset=0x0000
       mov      eax, 1
G_M26246_IG03:  ;; offset=0x0023
       ret      

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 22 (11 by maintainers)

Most upvoted comments

@EgorBot -arm64 -intel -profiler -commit main

using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;

BenchmarkRunner.Run<PgoBenchmarks>(args: args);

public class PgoBenchmarks
{
    [Benchmark]
    public async Task<bool> Task1() => await Task2();

    [MethodImpl(MethodImplOptions.NoInlining)]
    public async Task<bool> Task2() => true;
}

@EgorBot -arm64 -profiler -commit main

using BenchmarkDotNet.Attributes;

public class Bench
{
        static object obj = new object();
        
        [Benchmark]
        public Type GetType_() => obj.GetType();
}

@EgorBot -intel -arm64 -profiler -commit main

using BenchmarkDotNet.Attributes;

public class Bench
{
        static object obj = new object();
        
        [Benchmark]
        public Type GetType_() => obj.GetType();
}

@EgorBot -intel -profiler -keep -commit main --envvars “DOTNET_JitDisasm:GetType_”

using BenchmarkDotNet.Attributes;

public class Bench
{
        static object obj = new object();
        
        [Benchmark]
        public Type GetType_() => obj.GetType();
}

@EgorBot -intel -profiler -keep -commit main --envvars “DOTNET_JitDisasm:GetType_”

using BenchmarkDotNet.Attributes;

public class Bench
{
        static object obj = new object();
        
        [Benchmark]
        public Type GetType_() => obj.GetType();
}

@EgorBot -intel -arm64 -profiler -commit main

using BenchmarkDotNet.Attributes;

public class Bench
{
        static object obj = new object();
        
        [Benchmark]
        public Type GetType_() => obj.GetType();
}

@EgorBot -intel -profiler -keep -commit main --envvars “DOTNET_JitDisasm:GetType_”

using BenchmarkDotNet.Attributes;

public class Bench
{
        static object obj = new object();
        
        [Benchmark]
        public Type GetType_() => obj.GetType();
}

@EgorBot -intel -arm64 -profiler -commit main

using BenchmarkDotNet.Attributes;

public class Bench
{
        static object obj = new object();
        
        [Benchmark]
        public Type GetType_() => obj.GetType();
}

@EgorBot -intel -arm

using System.Collections.Generic;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Environments;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Running;

BenchmarkRunner.Run<PgoBenchmarks>(args: args);

[Config(typeof(MyEnvVars))]
[HideColumns("EnvironmentVariables", "Runtime")]
[DisassemblyDiagnoser]
public class PgoBenchmarks
{
    class MyEnvVars : ManualConfig
    {
        public MyEnvVars()
        {
            AddJob(Job.Default.WithId("8.0").WithRuntime(CoreRuntime.Core80));
            AddJob(Job.Default.WithId("8.0 R2R=0").WithRuntime(CoreRuntime.Core80)
                .WithEnvironmentVariables(
                    new EnvironmentVariable("DOTNET_ReadyToRun", "0")));

            AddJob(Job.Default.WithId("9.0").WithRuntime(CoreRuntime.Core90));
            AddJob(Job.Default.WithId("9.0 R2R=0").WithRuntime(CoreRuntime.Core90)
                .WithEnvironmentVariables(
                    new EnvironmentVariable("DOTNET_ReadyToRun", "0")));
        }
    }

    public static IEnumerable<object[]> TestData()
    {
        yield return new object[] { "1111", "1122" };
    }

    [Benchmark]
    [ArgumentsSource(nameof(TestData))]
    public bool StringEquals(string a, string b) => a == b;
}

@EgorBot -intel --runtimes net9.0 net8.0 net7.0 --disasm --envvars DOTNET_ReadyToRun:0

using BenchmarkDotNet.Attributes;

public class Bench
{
    string _str1 = "1234";
    string _str2 = "1235";

    [Benchmark]
    public bool StringEquals() => _str1 == _str2;
}