-
Notifications
You must be signed in to change notification settings - Fork 1.1k
const parameters feature proposal - based on Issue #744 and Design Review: Hardware Intrinsics for Intel #886
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
…w: Hardware Intrinsics for Intel Description of the proposal contains specifics about implementation details and formal syntax proposed to support Constant Parameters feature. It is accompanied by early prototype which supports const parameters declaration, invocation of methods with constant arguments with value expressed as constant expression, overload resolution with support for 'const' parameters modifiers. Still no support for code generation as there is no final decision how to represent 'const' parameters at the CLI level. No working implementation for Roslyn Constant Parameter feature conditional support, however, everything is stubbed to support it. Very limited tests.
|
I expect that PR may be to some extend incomplete (sorry for that), however, wanted to help with speeding up hardware SIMD intrinsics feature implementation. PTAL |
|
Thanks for comments. Fixed errors and style. |
|
Dumb question: The motivating scenario for this language feature seems to be:
It's unclear to me why this needs language support. Wouldn't it be far quicker and easier to just make the api like this: public static Vector256<float> Shuffle(
Vector256<float> value, Vector256<float> right, [Const] byte control);And then have an analyzer that validates that the code compiled against it passes a constant? Such a solution could be provided today, and would satisfy the use case well without needing any language changes and without gating the features on the compiler then implementing those changes. -- Actually, i can see you wrote something about this in the doc:
But this seems really tenuous. First:
Why is this an inferior user experience? It addresses the core need, and does so in a way that is natural and familiar to C# devs.
This seems like a good thing. Would we expect to have methods like "Foo(int i)" and "Foo(const int i)"? That seems like something that would be dramatically easy to get wrong. Someone could easily forget to put the "const" at the callsite and would then call the former method instead of the latter. It seems like you would not want overloading based on const/non-constness. And, as such, an attribute is utterly fine for this sort of thing.
This just seems wrong. There's a totally reasonable alternative on the table. It's cheap and simple and can be done today. -- In other words, even if i posit that the attribute+analyzer isn't perfect and isn't as awesome as having a keyword, it's still literally thousands of times cheaper to get up and running today. It also seems to provide a more than good enough solution for the problem space. You could write this up and have it shipped in a day and give more than good enough results for this use case. Whereas trying to make a language change here could take years, and still never get done. Even if you did, years from now, get it through the language, it would only be marginally better at best than the solution that exists today :) |
Yes. There are quite a bit of SIMD instructions taking 2, 3, 4 arguments which have versions supporting both The difference between C++ and C# would be that C++
In C++ world it is up to the developer to write correct code and the same could be expected of C# developers I think.
I agree that doing it via |
Having FooImmediate and Foo seems like a totally reasonable approach.
If this is the case... then why need the language change in the first place? :) If you can just depend on C# developers writing correct code, you don't need a language feature that enforces that they're doing things correctly.
Actually, they were. This is the entire idea behind analyzers. Note: you wouldn't even need the attribute. You could just say: Here is an analyzer that just knows about this vector instruction API. When it sees you using it improperly, it lets you know. This was a core goal behind analyzers. They were so people could write an API and supply an extra set of rules about how that API should be used properly. It was entirely so API usage could be checked without needing extra language/compiler rules. That's entirely what's happening here. You have an API you want called in very specific ways. And you want warnings/errors if the user doesn't call it in that way. That's 100% in the realm of analyzers. |
Being cheap to implement is definitely good. But it's not a reason to do something. It needs to be the rigth choice for the language. We make language changes considering a multitude of factors, including (but not limited to):
etc. etc. Another way to think about it that your 10 hours multiple out by 100x when you consider needing to have the language design team design and consider all the implications here. When they have to make sure it works properly in all possible language/feature permutations. When the compiler team has to implement and verify across all the necessary permutations. Making sure the IDE works properly. Getting things doc'ed. Getting things spec'ed. etc. etc. And that's time that will be compared against the 'zero cost', supported and encouraged path of using analyzers. --
That's not a good reason :) Making something 100x more costly for exceptionally marginal gain isn't good language design or engineering. Purism definitely falls way to pragmatism in how we do things. |
Examples?
C++ doesn't support this kind of overloading. If there really intrinsics that do support this then that's really a special case, not something that the standard language supports.
Not really. C++ compilers actually produce errors if you provide non-constants in such cases. Though not because the parameter is |
|
I logged an issue against CoreFX (see https://github.com/dotnet/corefx/issues/26174) suggesting that a repo or project specifically for CoreFX related analyzers be created. There is also a bit of sample code (more of a prototype really: https://github.com/dotnet/corefx/issues/26174#issuecomment-355476154) that shows how trivial it is to implement a diagnostic analyzer that enforces a given parameter is |
|
Closing this as proposals should not be checked-in until after a given feature is championed. CC. @gafter |
Description of the proposal contains specifics about implementation details and formal syntax
proposed to support Constant Parameters C# feature. It is accompanied by early Roslyn prototype which
supports const parameters declaration, invocation of methods with constant arguments with value
expressed as constant expression, overload resolution with support for 'const' parameters modifiers.
Still no support for assembly code generation as there is no final decision how to represent 'const' parameters
at the CLI level. Roslyn Constant Parameter feature has conditional support. Very limited tests.