-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Public API Proposal: Make bool RuntimeHelpers.IsKnownConstant(...) public for all primitive types #67285
Comments
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label. |
I'm in favor of making it generic. |
Tagging subscribers to this area: @dotnet/area-system-runtime-compilerservices Issue DetailsBackground and motivationRecently two internal IsKnownConstant() overloads in class RuntimeHelpers for char and string? were added. Same performance/codegen reasons apply as with the original (internal) proposal. #11484 At least one overload for each primitive type (+ the existing string) would be wellcomed. This request has similarities with GNU GCC int __builtin_constant_p (exp) API Proposalnamespace System.Runtime.CompilerServices
{
public static partial class RuntimeHelpers
{
// The following intrinsics return true if input is a compile-time constant
// Feel free to add more overloads on demand
// CHANGE VISIBILITY
[Intrinsic]
public static bool IsKnownConstant(string? t) => false;
[Intrinsic]
public static bool IsKnownConstant(char t) => false;
// ADD OVERLOADS
[Intrinsic]
public static bool IsKnownConstant(byte t) => false;
[Intrinsic]
public static bool IsKnownConstant(sbyte t) => false;
[Intrinsic]
public static bool IsKnownConstant(short t) => false;
[Intrinsic]
public static bool IsKnownConstant(ushort t) => false;
[Intrinsic]
public static bool IsKnownConstant(int t) => false;
[Intrinsic]
public static bool IsKnownConstant(uint t) => false;
[Intrinsic]
public static bool IsKnownConstant(long t) => false;
[Intrinsic]
public static bool IsKnownConstant(ulong t) => false;
[Intrinsic]
public static bool IsKnownConstant(nint t) => false;
[Intrinsic]
public static bool IsKnownConstant(nuint t) => false;
[Intrinsic]
public static bool IsKnownConstant(float t) => false;
[Intrinsic]
public static bool IsKnownConstant(double t) => false;
}
} API UsageAPI can be public used like the current internal usage inside of the runtime. Alternative DesignsMaybe a true generic Version can be used. namespace System.Runtime.CompilerServices
{
public static partial class RuntimeHelpers
{
[Intrinsic]
public static bool IsKnownConstant<T>(T t) => false;
}
} Risks
|
Will it be able to consume |
void DoWork(int a)
{
if (RuntimeHelpers.IsKnownConstant(a) && a == 42)
{
// a lot of code
}
// a lot of code
} jit (currently) is not able to give it additional inlining boost only when a == 42 (currently it doesn't boost at all) |
Btw. thanks for all the cool coding I found by you in this repository! |
Could restriction unmanaged be dangerous with large structs? |
What about |
Yep, it would be useful to see this API used in libraries in more places to prove that it actually works except for a few trivial carefully crafted cases. #64821 was unsuccessfully in using this API for more complex code. |
Tag @GrabYourPitchforks for triage as he created the internal API proposal |
@jkotas Will all code be inlined or is only the budget increased to inline more (but maybe not all) code? |
Yes, large Example: sharplab.io |
I agree with Jan's assessment at #67285 (comment). This is experimental and very temperamental. We need more evidence that it's widely useful before exposing it publicly. It honestly wouldn't surprise me if the JIT folks somehow come up with some clever scheme which obviates the utility of IsKnownConstant entirely and we end up removing the API as a result. |
This code should have a We have a few examples on our codebase that could use a generic version. |
Background and motivation
Recently two internal IsKnownConstant() overloads in class RuntimeHelpers for char and string? were added.
For a library developer it would be usefull to consume this API like the framework internaly does.
Same performance/codegen reasons apply as with the original (internal) proposal. #11484
At least one overload for each primitive type (+ the existing string) would be wellcomed.
See also: #63734 and #64809
This request has similarities with GNU GCC int __builtin_constant_p (exp)
API Proposal
API Usage
API can be public used like the current internal usage inside of the runtime.
Alternative Designs
Maybe a true generic Version can be used.
Risks
No breaking change. Not an public API today.
Will depend on JIT beyond c# language spec.
The text was updated successfully, but these errors were encountered: