Skip to content

IndexOutOfRangeException in System.Numerics.Vector<T>(array) Where array Size < 32 Bytes #24174

Closed as not planned

Description

@zcanann

System.Numerics.Vector<byte> vec = new System.Numerics.Vector<byte>(new byte[] { 4, 3, 2, 1, 6, 4, 2, 4 });

This call will crash with an IndexOutOfRangeException exception. I'm pulling in this library as a NuGet package. If I pass in >= 32 bytes, this code works.

The same logic applies to other data types if they do not fill enough at least 32 bytes.

Is there a reason for this behavior? My use case involves passing variable length bytes (anywhere from 1 to millions). The constructor does not make this behavior clear, and I'd prefer to not have to pad my byte arrays to use this.

If the library is not supposed to support values less than the size of the XMM/YMM register, then it would be ideal to at least throw a more specific exception.

Activity

  1. benaadams commented on Nov 19, 2017

    @benaadams
    Member

    System.Numerics.Vector<byte>.Count should tell you the size it supports (which varies based on CPU) it always chooses the largest supported vector size

  2. zcanann commented on Nov 20, 2017

    @zcanann
    Author

    Perhaps an exception with more information would be useful then (ie expecting x bytes)

    IndexOutOfRangeException is a bit vague for first-time users of this library.

    EDIT: Alternatively, address https://github.com/dotnet/corefx/issues/23448 to improve the clarity of what constructor calls do.

  3. terrajobst commented on Dec 11, 2017

    @terrajobst
    Contributor

    Normally I'd expect the constructor to throw an ArgumentException if the array's length is less than than Vector<T>.Count. I don't recall whether we explicitly don't do length validation for perf reasons. Also, I don't recall whether the constructor is an intrinsic or not.

    @CarolEidt, do you have any thoughts here?

    EDIT: @eerhardt just pointed me to the code. Looks intentional, but we do have validation so we could easily change the exception. Changing the exception type is a binary breaking change, not sure it's worth it. We should, however, update the message to explain the error condition.

  4. eerhardt commented on Dec 11, 2017

    @eerhardt
    Member

    The issue is that the constructor that takes only an array calls into the constructor that takes an array and an index. So that's where the "index" in "IndexOutOfRangeException" comes from -

    https://github.com/dotnet/corefx/blob/075df17be2057be1d47f11597f0ee735cb39f60b/src/System.Numerics.Vectors/src/System/Numerics/Vector.tt#L186-L189

    Perhaps just adding an appropriate message to this exception would suffice?

  5. zcanann commented on Dec 11, 2017

    @zcanann
    Author

    Adding a message seems like a sufficient solution to me

  6. CarolEidt commented on Dec 11, 2017

    @CarolEidt
    Contributor

    Adding a message seems like a sufficient solution to me

    Agreed.

  7. WinCPP commented on Jan 20, 2018

    @WinCPP
    Contributor

    May I pick this up for the changes?

    To test how it works, I did some change to pass a string "myexception" to the constructor of IndexOutOfRangeException as mentioned in this note by @eerhardt on the thread. However the change doesn't seem to have effect. It still puts out the string "Index was outside the bounds of the array." as exception message; i.e. the default exception message. Seems like my changes to the 'tt' file and the corresponding auto-generated file that has compiled into the assembly aren't being picked up.

    I am not sure if I have followed correct step... Help appreciated.

  8. clarkis117 commented on Feb 15, 2018

    @clarkis117
    Contributor

    @WinCPP You need to build the T4 templates in Visual Studio, so it generates a new .cs file with the changes you made. For the tests, this may involve copying the ...includes.tt file to same directory as the test.tt file. Otherwise, Visual Studio will give you a build error when attempting to build the T4 templates in the solution.

  9. WinCPP commented on Mar 3, 2018

    @WinCPP
    Contributor

    @clarkis117 Thanks. I am working on #23688 at this time and would like to take this up next.

    @clarkis117 Now I remember, that time I had done the changes to the tt file itself but found that the somehow the generic message in the IndexOutOfRangeException was not getting overridden. It has been sometime that I worked on it. Will redo everything. Thanks!

  10. added a commit that references this issue on Mar 4, 2018
  11. jkotas commented on Mar 4, 2018

    @jkotas
    Member

    Seems like my changes to the 'tt' file and the corresponding auto-generated file that has compiled into the assembly aren't being picked up.

    The implementation of [Intrinsics] methods is duplicated in the JIT. The code in .cs files is just a fallback for cases where the JIT intrinsic expansion does not kick in (e.g. when the method is called via reflection, or on platforms where the SIMD intrinsics are not implemented yet). It means that the change of exception message has to be done in the JIT to make it actually kick in.

  12. WinCPP commented on Mar 4, 2018

    @WinCPP
    Contributor

    The implementation of [Intrinsics] methods is duplicated in the JIT. The code in .cs files is just a fallback for cases where the JIT intrinsic expansion does not kick in (e.g. when the method is called via reflection, or on platforms where the SIMD intrinsics are not implemented yet). It means that the change of exception message has to be done in the JIT to make it actually kick in.

    Is this something that I can do or is it offlimits for me...? I am interested...

  13. jkotas commented on Mar 4, 2018

    @jkotas
    Member

    It is not one liner change, but I am sure that you would be able to do it if you dive into it. There is pretty active community around the JIT intrinsics ( @4creators @CarolEidt @eerhardt @fiigii @sdmaclea @tannergooding ) that I am sure will be happy to give you guidance as necessary.

  14. fiigii commented on Mar 4, 2018

    @fiigii
    Contributor

    @WinCPP JIT uses a GenTreeBoundsChk IR (with GT_SIMD_CHK oper) to throw the exception.

    https://github.com/dotnet/coreclr/blob/aaafa705f6aa930343061a0753928839b1e75bf2/src/jit/simd.cpp#L2886-L2906

    I guess this change may need to add a new value in enum SpecialCodeKind because the current SCK_RNGCHK_FAIL has the fixed error message.

  15. 4creators commented on Mar 4, 2018

    @4creators
    Contributor

    Another piece of code to dive into is here:

    https://github.com/dotnet/coreclr/blob/aaafa705f6aa930343061a0753928839b1e75bf2/src/jit/simd.cpp#L2647-L2664

    What is interesting this are types of checks as above SCK_RNGCHK_FAIL and SCK_ARG_EXCPN

  16. 25 remaining items

  17. WinCPP commented on Apr 11, 2018

    @WinCPP
    Contributor

    @jkotas Thanks for the inputs. Need some guidance around this line.

    And then see whether we are losing any optimizations if you do this. If we are losing any optimizations with non-intrinsic implementation like this, look at what needs to be done to get them back.

    Do you mean performance data or you mean the generated IL code, etc.? I haven't done that earlier and hence some inputs around what I have to inspect (and perhaps the tools) will help me a lot and would give me an opportunity to learn something new. Thanks!

  18. jkotas commented on Apr 11, 2018

    @jkotas
    Member

    Do you mean performance data or you mean the generated IL code, etc.?

    I meant generated native code. It is where any missing optimizations are going to show up. Here is what you can try to do:

    • Build checked CoreCLR build
    • Find or write a simple test that calls Vector(T[] values) constructor in a loop over an array
    • set COMPlus_JitDisasm=...name of the test method... in environment
    • Run the test. It should print the native disassembly to the console. Compare the disassembly before/after the change.

    Alternatively, you can use tools like http://adamsitnik.com/Disassembly-Diagnoser/ . Or just look at the native code using native debugger.

  19. WinCPP commented on Jun 7, 2018

    @WinCPP
    Contributor

    Hi @jkotas, resuming after considerable time. I did the following,

    1. Built coreclr as checked build with the changes you had suggested here for the Vector(T[]) constructor (with removal of Intrinsic attribute on the constructor). CLR code changes are in this commit.
    2. Built corefx using private clr binaries with command
    build -release -- /p:CoreCLROverridePath=D:\WinCPP\coreclr\bin\Product\Windows_NT.x64.Checked
    
    1. Wrote a test case that invoked the constructor in loop and using the ComPlus_JitDisasm set for specific method got the dump of the assembly. Please see this commit for the test case.

    But still the call to the JIT exception throw method CORINFO_HELP_RNGCHKFAIL is generated with overall increase in byte code size by 9 bytes. Please refer to here and here for assembly dump before and after the coreclr changes.

             8379081F             cmp      dword ptr [rcx+8], 31
             7705                 ja       SHORT G_M22984_IG05
             E8DBCB0B5E           call     CORINFO_HELP_RNGCHKFAIL
    

    Not sure if this is correct way. Appreciate your advice.


    EDIT 1: I am working on Windows 10 x64 machine. Is this some issue of netcoreapp vs some other type of build that I should be using...?

  20. jkotas commented on Jun 7, 2018

    @jkotas
    Member

    Try generating the jit log for the method using ComPlus_JitDump and then try to figure out why the optimization did not happen. https://github.com/dotnet/coreclr/blob/master/Documentation/botr/ryujit-tutorial.md has some documentation about the internal organization of the JIT.

  21. WinCPP commented on Aug 1, 2018

    @WinCPP
    Contributor

    Hi all. Want to keep you updated that I'm not getting cycles to do this one at this point in time. Apologies.

  22. Gnbrkm41 commented on Oct 12, 2019

    @Gnbrkm41
    Contributor

    Looks intentional, but we do have validation so we could easily change the exception. Changing the exception type is a binary breaking change, not sure it's worth it. We should, however, update the message to explain the error condition.

    I wonder what is the design decision here, whether IndexOutOfRangeException is okay for perf reasons or to eventually change to ArgumentException? I was editing the documents for the constructors and I'm stuck whether to document the exception thrown when the length of the array is less than Count.

  23. jkotas commented on Oct 12, 2019

    @jkotas
    Member

    I'm stuck whether to document the exception thrown when the length of the array is less than Count.

    Yes, it is fine to document the current behavior for exception thrown by Vector constructors. Vector<T> constructors do not follow the framework design guidelines for performance reasons. Another difference from the standard is that they throw NullReferenceException (vs. the ArgumentNullException recommended by the framework design guidelines).

  24. transferred this issue fromdotnet/corefxon Jan 31, 2020
  25. added this to the Future milestone on Jan 31, 2020
  26. added
    untriaged
    on Feb 23, 2020
  27. removed
    untriaged
    on Mar 4, 2020
  28. added
    Cost:S
    on Jan 15, 2021
  29. tannergooding commented on Jul 9, 2025

    @tannergooding
    Member

    As per the above, the behavior here is "by design".

    We treat these more like primitive types and so they throw IndexOutOfRangeException like T[] or Span<T> might.

    The Vector<T> constructor expects and is documented to take Count elements in the input span or array.

  30. locked and limited conversation to collaborators on Aug 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Cost:Sarea-System.Numericsbughelp wanted

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions