Skip to content

IsInfinity can drop throwing side effects #132902

Closed

Description

@dhartglassMSFT

Following code correctly throws exception in TieredCompilation, does not throw with TieredCompilation=0.

gtNewSimdIsInfinityNode probably needs to respect side effects before folding.

using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.Arm;
public class TestClass
{
    public static int Main(string[] args)
    {
        Vector64<int> a = Vector64<int>.Zero;
        byte idx = 2;

        Vector64.IsInfinity(AdvSimd.Arm64.MultiplyDoublingScalarBySelectedScalarSaturateHigh(a, a, idx));
        return 1;
    }
}

Activity

  1. added
    area-CodeGen-coreclr
    on Aug 28, 2026
  2. added
    untriaged
    on Aug 28, 2026
  3. dotnet-policy-service commented on Aug 28, 2026

    @dotnet-policy-service
    Contributor

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

  4. dhartglassMSFT commented on Aug 28, 2026

    @dhartglassMSFT
    ContributorAuthor

    These may have the same issue

    gtNewSimdIsFiniteNode
    gtNewSimdIsInfinityNode
    gtNewSimdIsIntegerNode
    gtNewSimdIsNaNNode
    gtNewSimdIsNegativeNode
    gtNewSimdIsNegativeInfinityNode
    gtNewSimdIsPositiveNode
    gtNewSimdIsPositiveInfinityNode
    gtNewSimdIsSubnormalNode

  5. tannergooding commented on Aug 28, 2026

    @tannergooding
    Member

    @dhartglassMSFT looks like these are just generally missing a gtWrapWithSideEffects on some paths. It's a relatively simple fix and I can handle it if needed.

  6. dhartglassMSFT commented on Aug 28, 2026

    @dhartglassMSFT
    ContributorAuthor

    @dhartglassMSFT looks like these are just generally missing a gtWrapWithSideEffects on some paths. It's a relatively simple fix and I can handle it if needed.

    yeah feel free, I'm updating Antigen (fuzzer) currently and filing issues, this was one

  7. AndyAyersMS commented on Aug 29, 2026

    @AndyAyersMS
    Member

    I hit this independently while building a fuzzer generator for the class, and it
    looks broader than the original report in three ways.

    It is not arm64-specific and needs no exotic intrinsic. Portable repro, x64,
    70 lines with no test framework:

    private static int Counter;
    
    [MethodImpl(MethodImplOptions.NoInlining)]
    private static int Bump() { Counter++; return 3; }
    
    Vector128.IsInfinity(Vector128.Create(Bump()));
    case operand evaluated
    Vector64.IsInfinity, discarded 1
    Vector128.IsInfinity, discarded 0
    Vector128.IsInfinity, consumed 0
    Vector128.IsZero, consumed (control) 1

    It is not tiering-dependent. The report contrasts tiered against
    TieredCompilation=0; the table above is identical under fullopts and under
    DOTNET_JITMinOpts=1. That points at intrinsic expansion during importation
    rather than a later phase.

    Consuming the result does not save the operand. The original example
    discards it.

    The IsZero control is the useful part: it is value-dependent, so it cannot be
    folded, and its operand survives. That isolates the behaviour to the
    constant-fold path for predicates that are trivially decidable on integer
    element types.

    By that reasoning these Vector128 predicates are all fold candidates on
    integer vectors — I have only confirmed IsInfinity drops the operand, the rest
    are candidates by construction:

    IsInfinity, IsNaN, IsFinite, IsNegativeInfinity, IsPositiveInfinity,
    IsSubnormal, IsInteger

    Tested on d2ea726dd3d.

    Full self-contained repro (prints the table above)
    // Definitive: is the operand evaluated at all?
    //
    // An exception proves a side effect was skipped, but a counter proves it more
    // directly and cannot be confused with exception-handling behaviour.
    //
    // Vector128.IsInfinity on an INTEGER vector is trivially false, so the JIT
    // folds it. dotnet/runtime#132902 says gtNewSimdIsInfinityNode needs to respect
    // side effects before folding. This checks whether the operand runs.
    
    using System;
    using System.Runtime.CompilerServices;
    using System.Runtime.Intrinsics;
    
    public static class Program
    {
        private static int Counter;
    
        [MethodImpl(MethodImplOptions.NoInlining)]
        private static int Bump()
        {
            Counter++;
            return 3;
        }
    
        [MethodImpl(MethodImplOptions.NoInlining)]
        private static void V64_Discard() => Vector64.IsInfinity(Vector64.Create(Bump()));
    
        [MethodImpl(MethodImplOptions.NoInlining)]
        private static void V128_Discard() => Vector128.IsInfinity(Vector128.Create(Bump()));
    
        [MethodImpl(MethodImplOptions.NoInlining)]
        private static void V128_Consume()
        {
            var r = Vector128.IsInfinity(Vector128.Create(Bump()));
            if (!r.Equals(default))
            {
                Console.WriteLine("unexpected: IsInfinity true for an int vector");
            }
        }
    
        // Control: a predicate that is NOT constant for integers cannot be folded,
        // so its operand must survive.
        [MethodImpl(MethodImplOptions.NoInlining)]
        private static void V128_Control_IsZero()
        {
            var r = Vector128.IsZero(Vector128.Create(Bump()));
            if (!r.Equals(default))
            {
                Console.WriteLine("(IsZero true, fine)");
            }
        }
    
        private static void Check(string name, Action a)
        {
            Counter = 0;
            a();
            Console.WriteLine($"{name}: operand evaluated {Counter} time(s)" +
                              (Counter == 0 ? "   <-- SIDE EFFECT DROPPED" : ""));
        }
    
        public static int Main()
        {
            Check("V64  IsInfinity discard ", V64_Discard);
            Check("V128 IsInfinity discard ", V128_Discard);
            Check("V128 IsInfinity consume ", V128_Consume);
            Check("V128 IsZero     consume ", V128_Control_IsZero);
            return 100;
        }
    }
  8. removed
    untriaged
    on Aug 31, 2026
  9. added this to the 12.0.0 milestone on Aug 31, 2026
  10. added a commit that references this issue on Aug 31, 2026
    23c1225
  11. added a commit that references this issue on Sep 1, 2026
    7ab4d96
  12. locked and limited conversation to collaborators on Oct 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-CodeGen-coreclr

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions