Skip to content

JIT: Transform SELECT(x < 0, C - 1, C) to SAR(x, 31) + C#125549

Draft
BoyBaykiller wants to merge 1 commit intodotnet:mainfrom
BoyBaykiller:transform-select-to-sar-add
Draft

JIT: Transform SELECT(x < 0, C - 1, C) to SAR(x, 31) + C#125549
BoyBaykiller wants to merge 1 commit intodotnet:mainfrom
BoyBaykiller:transform-select-to-sar-add

Conversation

@BoyBaykiller
Copy link
Contributor

Feels a bit clunky in Lower, might move to if-conversion?

@github-actions github-actions bot added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Mar 14, 2026
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Mar 14, 2026
@dotnet-policy-service
Copy link
Contributor

Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.

@jakobbotsch
Copy link
Member

@copilot Use the performance benchmark skill to create micro benchmarks for this pattern

@jakobbotsch
Copy link
Member

@EgorBot -linux_amd -osx_arm64

   using BenchmarkDotNet.Attributes;
   using BenchmarkDotNet.Running;
   using System.Runtime.CompilerServices;
   
   BenchmarkSwitcher.FromAssembly(typeof(Bench).Assembly).Run(args);
   
   public class Bench
   {
       private int[] _values = default!;
   
       [GlobalSetup]
       public void Setup()
       {
           var rng = new Random(42);
           _values = new int[1024];
           for (int i = 0; i < _values.Length; i++)
               _values[i] = rng.Next(-1000, 1000);
       }
   
       [Benchmark]
       public int SelectLtZero_ConstOffset()
       {
           int sum = 0;
           int[] arr = _values;
           for (int i = 0; i < arr.Length; i++)
               sum += LtZero_Offset(arr[i]);
           return sum;
       }
   
       [Benchmark]
       public int SelectGeZero_ConstOffset()
       {
           int sum = 0;
           int[] arr = _values;
           for (int i = 0; i < arr.Length; i++)
               sum += GeZero_Offset(arr[i]);
           return sum;
       }
   
       [Benchmark]
       public int SelectLtZero_ZeroBase()
       {
           int sum = 0;
           int[] arr = _values;
           for (int i = 0; i < arr.Length; i++)
               sum += LtZero_ZeroBase(arr[i]);
           return sum;
       }
   
       [MethodImpl(MethodImplOptions.NoInlining)]
       private static int LtZero_Offset(int x) => x < 0 ? 41 : 42;
   
       [MethodImpl(MethodImplOptions.NoInlining)]
       private static int GeZero_Offset(int x) => x >= 0 ? 42 : 41;
   
       [MethodImpl(MethodImplOptions.NoInlining)]
       private static int LtZero_ZeroBase(int x) => x < 0 ? -1 : 0;
   }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants