You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
IndexOutOfRangeException in System.Numerics.Vector<T>(array) Where array Size < 32 Bytes #24174
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.
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
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.
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.
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 -
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.
@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.
@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!
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.
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...
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.
@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!
Hi @jkotas, resuming after considerable time. I did the following,
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.
Built corefx using private clr binaries with command
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...?
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.
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).
Activity
benaadams commentedon Nov 19, 2017
System.Numerics.Vector<byte>.Countshould tell you the size it supports (which varies based on CPU) it always chooses the largest supported vector sizezcanann commentedon Nov 20, 2017
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.
terrajobst commentedon Dec 11, 2017
Normally I'd expect the constructor to throw an
ArgumentExceptionif the array's length is less than thanVector<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.
eerhardt commentedon Dec 11, 2017
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?
zcanann commentedon Dec 11, 2017
Adding a message seems like a sufficient solution to me
CarolEidt commentedon Dec 11, 2017
Agreed.
WinCPP commentedon Jan 20, 2018
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
IndexOutOfRangeExceptionas 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.
clarkis117 commentedon Feb 15, 2018
@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.
WinCPP commentedon Mar 3, 2018
@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
IndexOutOfRangeExceptionwas not getting overridden. It has been sometime that I worked on it. Will redo everything. Thanks!Fix review comments
jkotas commentedon Mar 4, 2018
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.WinCPP commentedon Mar 4, 2018
Is this something that I can do or is it offlimits for me...? I am interested...
jkotas commentedon Mar 4, 2018
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.
fiigii commentedon Mar 4, 2018
@WinCPP JIT uses a
GenTreeBoundsChkIR (withGT_SIMD_CHKoper) 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 SpecialCodeKindbecause the currentSCK_RNGCHK_FAILhas the fixed error message.4creators commentedon Mar 4, 2018
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_FAILandSCK_ARG_EXCPN25 remaining items
WinCPP commentedon Apr 11, 2018
@jkotas Thanks for the inputs. Need some guidance around this line.
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!
jkotas commentedon Apr 11, 2018
I meant generated native code. It is where any missing optimizations are going to show up. Here is what you can try to do:
Vector(T[] values)constructor in a loop over an arrayset COMPlus_JitDisasm=...name of the test method...in environmentAlternatively, you can use tools like http://adamsitnik.com/Disassembly-Diagnoser/ . Or just look at the native code using native debugger.
WinCPP commentedon Jun 7, 2018
Hi @jkotas, resuming after considerable time. I did the following,
Vector(T[])constructor (with removal of Intrinsic attribute on the constructor). CLR code changes are in this commit.ComPlus_JitDisasmset 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_RNGCHKFAILis 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.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...?
jkotas commentedon Jun 7, 2018
Try generating the jit log for the method using
ComPlus_JitDumpand 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.WinCPP commentedon Aug 1, 2018
Hi all. Want to keep you updated that I'm not getting cycles to do this one at this point in time. Apologies.
Gnbrkm41 commentedon Oct 12, 2019
I wonder what is the design decision here, whether
IndexOutOfRangeExceptionis okay for perf reasons or to eventually change toArgumentException? 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 thanCount.jkotas commentedon Oct 12, 2019
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).tannergooding commentedon Jul 9, 2025
As per the above, the behavior here is "by design".
We treat these more like primitive types and so they throw
IndexOutOfRangeExceptionlikeT[]orSpan<T>might.The
Vector<T>constructor expects and is documented to takeCountelements in the input span or array.