Repository navigation
Description
Const Generics
Feature branch: feature/const-generics
Diff for changes: main...hez2010:runtime:feature/const-generics
This work is to bring the support for const generics to CoreCLR.
It involves below changes.
IL keyword changes
Addition
-
const: indicate the type argument contains a const value.
Particularly, we use the keyword const to differentiate a const type argument from a type argument.
IL opcode changes
Modification
-
ldtoken: thie opcode is also responsible for loading the const type argument (the value) to the stack.
Element type changes
Addition
-
ELEMENT_TYPE_CTARG: const type argument, i.e. an actual const value
Encoding rules
ELEMENT_TYPE_CTARGwill be followed by aCorElementTypeto indicate the type of the const argument, and then the const value after the type. The size of the const value is determined byCorElementType.
Encoding changes
Addition
- Adding a column to
GenericParameterRecto save amdTokenwhich represents the type of a const generic parameter.
Modification
- Changing the reserved
DWORDtomdTokento save the type of const generic parameter.
Syntax changes
Metadata version
- Bumping the metadata version to v3.0
- Back-compatibility handling for v2.0
Type system changes
We need a TypeDesc to represent const value as a type, so here we added a ConstValueTypeDesc to achieve this. We use a uint64_t to represent const value in the ConstValueTypeDesc directly, and save its type seperately. This allows us to represent up to 8 bytes const value in the type system.
class ConstValueTypeDesc : TypeDesc {
TypeHandle m_type;
uint64_t m_type;
};A IsConstValue has been added for checking whether a TypeDesc is const value or not. To get the type and the value of a const value, GetConstValueType and GetConstValue can be used.
IL code example
.assembly _ {}
.class public auto ansi beforefieldinit Foo`2<T, const int32 N>
extends [System.Runtime]System.Object
{
.method public hidebysig specialname rtspecialname
instance void .ctor () cil managed
{
.maxstack 8
ldarg.0
call instance void [System.Runtime]System.Object::.ctor()
ret
}
.method public hidebysig newslot virtual
instance void M<const int32 V, const int32 W> () cil managed
{
.maxstack 1
.locals init (
[0] int32 v
)
newobj instance void class Foo`2<string, const int32 (42)>::.ctor()
call instance void class Foo`2<string, const int32 (42)>::M<!!V, !!V>()
newobj instance void class Foo`2<string, !!V>::.ctor()
call instance void class Foo`2<string, const !!V>::M<!N, const int32 (42)>()
newobj instance void class Foo`2<string, !N>::.ctor()
call instance void class Foo`2<string, !N>::M<!!V, !!W>()
ldtoken !!V
call void [System.Console]System.Console::WriteLine(int32)
ldtoken !!W
call void [System.Console]System.Console::WriteLine(int32)
ldtoken !N
call void [System.Console]System.Console::WriteLine(int32)
ret
}
}This can be interpreted to the following dummy C# code:
class Foo<T, int N>
{
public void M<int V, int W>()
{
new Foo<string, 42>().M<V, V>();
new Foo<string, V>().M<N, 42>();
new Foo<string, N>().M<V, W>();
Console.WriteLine(V);
Console.WriteLine(W);
Console.WriteLine(N);
}
}TODO List
- Add support for assembling IL with const generics to binary
- Add support for const generics in IL disassembler
- Add support for loading the assembly, tracking the metadata information and etc. in VM
- Add support for const generics in type system
- Add support for const generics in JIT
- Tests if virtual const generic method works as expected
- New reflection and emitting APIs in BCL?
- Managed type system changes and ilc support?
- Support for arithmetic operations on const generic parameters? E.g.,
call instance void Foo::Bar<const int32 (!!M + !!N)>().
Open questions
- Should we introduce those new element types to support const generic parameter?
Conclusion: No. They behave no difference than the current type parameters, so we can reuse the existingELEMENT_TYPE_VARandELEMENT_TYPE_MVAR. - Should we allow const string and const bytearray to be used in const generic parameters?
Conclusion: Not for now. We can expand the support without breaking changes in the future. - Should we reuse
ldargfor loading const type arguments?
Conclusion: No. This would makes theldarghandling complex and inefficient. - Should we use
ldtokenfor loading const type arguments?
Conclusion: Yes. We don't allowtypeof(42)so it doesn't make sense to allowtypeof(T)whereTis a const generic parameter. - Should we bump the metadata version to v3.0?
Conclusion: Yes. Let's do it and also handle the back-compat for v2.0. - Should we introduce a
System.ConstValue<T>type to represent the type of a const value? If yes, should this type inheritSystem.Object?
Conclusion: No. It's totally redundant and useless. - Does const value type need a MethodTable?
Conclusion: No. AConstValueTypeDescshould be enough.
Activity
[mono][arm64] Handle sysctlbyname failing to find system options. (do…
kant2002 commentedon Jul 22, 2023
What about const arguments to methods? Does this change account for that? I think naming to Il opcodes not reflects that
hez2010 commentedon Jul 22, 2023
The existingldarg.*instructions can be extended to support loading const value from const type parameters.I will add an instruction
ldtargto support loading a const type parameter to a local.UPDATE: Done. I have added new IL instructions
ldtarg.sandldtarg.[-]Design to const generics support in CoreCLR[/-][+]WIP: Design to const generics support in CoreCLR[/+]hez2010 commentedon Jul 23, 2023
UPDATE 07/23: Remove all
ldtc.*variants and renameldtargtoldctarg.kant2002 commentedon Jul 23, 2023
What about constructs like this?
How that combinations would be expressed in NewIL ?
nike4613 commentedon Jul 23, 2023
I would imagine that the fixed buffer would be emitted as a runtime-blessed
ValueArraytype.hez2010 commentedon Jul 24, 2023
@kant2002
Fixed buffer will be lowered to a
ValueArray<T, const int32 V>type.hez2010 commentedon Jul 28, 2023
I updated the design to remove the
constbefore a type parameter reference.You should use
!Tand!!Tinstead ofconst !Tandconst !!T[-]WIP: Design to const generics support in CoreCLR[/-][+]Design to const generics support in CoreCLR[/+]Improve profiled casts #2 (dotnet#90735)
[Mono] Fix all the issues related to enabling WasmStripILAfterAOT (do…
Clear IconHandle when we fold compares (dotnet#95383)
Allow specifying IndentCharacter and IndentSize when writing JSON (do…
Late cast expansion: more improvements #2 (dotnet#97480)
[wasm] emscripten bump 3.1.56 (dotnet#100334)
[NRBF] Fix bugs discovered by the fuzzer (dotnet#107368)
JIT: Fix fgFoldCondToReturnBlock for multi-statement return blocks (d…
Fix createdump SIGSEGV on Heap dumps with interpreter active (dotnet#…
[Android] Handle X509ChainContext creation failures (dotnet#128651)
[browser] Wasm boot JSON: touch a stamp, not the boot JSON itself (fix …
Keep negative cast results in R2R when they can't change across versi…