-
Notifications
You must be signed in to change notification settings - Fork 1k
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
[Proposal] Expand const keyword usage to enable compile time enforcement of const semantics #744
Comments
I think the two proposals ( Especially considering that |
@svick Agree - i will edit my proposal and create a new one to reflect your remarks. |
slightly related: dotnet/roslyn#15079 int x = 1;
var z = v << x; would be accepted, as The issue dotnet/roslyn#15079 is in fact an 'umbrella' issue for the following ones: dotnet/roslyn#12238 dotnet/roslyn#14665 dotnet/roslyn#10506 dotnet/roslyn#11259 dotnet/roslyn#9627 dotnet/coreclr#3633 #504 (and a few more) |
Similar proposal but with scoped runtime const semantics is ref readonly feature which is currently in implementation phase. |
IMO, the C# language should not be concerned with whether the underlying hardware requires (or is most performant) when passed a constant/literal value. For some cases, the IL doesn't even expose a way for the compiler to guarantee a value is constant. The IL is almost entirely stack based (except for a few scenarios where it can take immediate operands). The That being said, there is a proposal on CoreFX/CoreCLR to expose hardware intrinsics directly (https://github.com/dotnet/coreclr/issues/6906#issuecomment-307164495). If that were to happen, then the runtime would need to either:
If the latter were to happen, I believe it would be a compiler feature, rather than a language feature. |
@tannergooding I think the most straightforward implementation of this proposal would be pretty much that magic attribute, except it would be exposed at the C# level as the The compiler would have to ensure that it produces IL that's recognized by the CLR as "constant", but that's probably mostly already the case. I don't see why the lack of support for immediate values in IL should be a problem.
Why? I thought the C# language/compiler is generally against attributes that affect how the C# compiler behaves. (And I'm talking about C# attributes, not IL attributes.) |
@svick, What I'm trying to envision here is:
I'm having trouble envisioning either of these. From my viewpoint,
The first is already being provided for The second is likely its own feature with its own validation. I imagine parameter The third is a very limited scenario (the primary scenario raised by this) and is likely not worth a language keyword (IMO). Providing a magic attribute that compiles down to a modreq is likely sufficient and the few cases where it is required, users can manually list it. |
@tannergooding Yeah, I think you're right. This feature has very limited use cases, so it makes sense to limit its cost and impact. And making it an attribute instead of a keyword does achieve that. |
Directly related to https://github.com/dotnet/corefx/issues/22940 |
Compile-time const parameter syntax
Rationale
In many situations it is necessary to create objects, parametrize types (generics) or execute functions using parameters passed as compile time constants. It directly relates to x86 and other ISA requirements for instruction encoding where one of the operands has to be compile time constant (in jitted code the definition of compile time constant is somehow blurred by the fact that one can in theory supply runtime value before jitting given method which could be used as a compile time constant for the lifetime of jitted code and later replaced with another runtime value used for rejitting the very same method).
Situations in which this syntax would be required are i.e.:
where contract requirement is that
int n
passed to shift operator is a "compile time constant" or in some cases, depending on underlying ISA instruction, it could be an int variable. This situation arises in several cases when intrinsics based on Vector class could be used with some group of SIMD instructions i.e.PSLLD/PSLLQ xmm1, imm8
for SSE2 ISA extensions. For all sets of SSE and SSSE instructions there are no instructions equivalent toPSLLD/PSLLQ xmm1, imm8
which would acceptxmm/m128
second operand but such instructions exists in AVX/AVX2/AVX512. C# syntax does not provide any methods to prevent passing non-constant int to shift operator in the above example.Proposal
Extend usage of
const keyword
to method/constructor/property parameters while maintaining it's semantics - enforcement of compile time constant usage.const keyword
should participate in overload resolution allowing for overloads containing identical types of parameters at the same position in the method signature differing only byconst keyword
Examples
Alternatives
Usage of different indirect methods which do not fit very well to overall C# semantics. For details see issue # 16835 in corefx repo.
The text was updated successfully, but these errors were encountered: