Add [Immediate] attribute for parameters which should be constants #33814
Labels
api-suggestion
Early API idea and discussion, it is NOT ready for implementation
area-System.Runtime.CompilerServices
code-analyzer
Marks an issue that suggests a Roslyn analyzer
(Copied from #30740 (comment).)
We have scenarios (particularly in the Intrinsics namespace) where certain APIs expect constant values as parameters. For instance,
Sse2.ShiftLeftLogical(Vector, byte)
expects a constant value for its last argument, as the constant value needs to be encoded as part of the instruction. You can pass a non-constant, but if you do this you'll get redirected to a jump table, which could kill your performance. And presumably you were using intrinsics because you wanted bare-metal performance. :)As a strawman for an analyzer that could detect this, we could consider an
[Immediate]
attribute that could be placed on parameters and an analyzer that would flag call sites where a non-const is passed in for these parameters. Bruce had further suggested we could extend it with min and max properties if needed.So, using the SSE2 example mentioned earlier, the annotation would appear as follows.
Category: Performance (possibly also correctness?)
The text was updated successfully, but these errors were encountered: