Skip to content

Design to const generics support in CoreCLR #2

Closed

Description

@hez2010

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_CTARG will be followed by a CorElementType to indicate the type of the const argument, and then the const value after the type. The size of the const value is determined by CorElementType.

Encoding changes

Addition

  • Adding a column to GenericParameterRec to save a mdToken which represents the type of a const generic parameter.

Modification

  • Changing the reserved DWORD to mdToken to save the type of const generic parameter.

Syntax changes

See https://github.com/hez2010/runtime/blob/feature/const-generics/src/coreclr/ilasm/asmparse.y

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

  1. 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 existing ELEMENT_TYPE_VAR and ELEMENT_TYPE_MVAR.
  2. 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.
  3. Should we reuse ldarg for loading const type arguments?
    Conclusion: No. This would makes the ldarg handling complex and inefficient.
  4. Should we use ldtoken for loading const type arguments?
    Conclusion: Yes. We don't allow typeof(42) so it doesn't make sense to allow typeof(T) where T is a const generic parameter.
  5. Should we bump the metadata version to v3.0?
    Conclusion: Yes. Let's do it and also handle the back-compat for v2.0.
  6. Should we introduce a System.ConstValue<T> type to represent the type of a const value? If yes, should this type inherit System.Object?
    Conclusion: No. It's totally redundant and useless.
  7. Does const value type need a MethodTable?
    Conclusion: No. A ConstValueTypeDesc should be enough.

Activity

  1. kant2002 commented on Jul 22, 2023

    @kant2002

    What about const arguments to methods? Does this change account for that? I think naming to Il opcodes not reflects that

  2. hez2010 commented on Jul 22, 2023

    @hez2010
    OwnerAuthor

    What about const arguments to methods?

    The existing ldarg.* instructions can be extended to support loading const value from const type parameters.

    I will add an instruction ldtarg to support loading a const type parameter to a local.

    UPDATE: Done. I have added new IL instructions ldtarg.s and ldtarg.

  3. changed the title [-]Design to const generics support in CoreCLR[/-] [+]WIP: Design to const generics support in CoreCLR[/+] on Jul 22, 2023
  4. hez2010 commented on Jul 23, 2023

    @hez2010
    OwnerAuthor

    UPDATE 07/23: Remove all ldtc.* variants and rename ldtarg to ldctarg.

  5. kant2002 commented on Jul 23, 2023

    @kant2002

    What about constructs like this?

    class FixedArray<T, const int V> {
       fixed T[V] Items;
    }
    
    class Hero {}
    
    class ArrayOfHeroes<const int V> : FixedArray<Hero, V> {}
    
    class RingBuffer<T> : FixedArray<T, 10> {}
    

    How that combinations would be expressed in NewIL ?

  6. nike4613 commented on Jul 23, 2023

    @nike4613

    I would imagine that the fixed buffer would be emitted as a runtime-blessed ValueArray type.

  7. hez2010 commented on Jul 24, 2023

    @hez2010
    OwnerAuthor

    @kant2002

    Fixed buffer will be lowered to a ValueArray<T, const int32 V> type.

    .class private auto ansi beforefieldinit FixedArray`2<T, const int32 V>
        extends [System.Runtime]System.Object
    {
        .field private ValueArray<T, !V> Items
        .method public hidebysig specialname rtspecialname 
            instance void .ctor () cil managed 
        {
            .maxstack 8
    
            ldarg.0
            call instance void [System.Runtime]System.Object::.ctor()
            ret
        }
    }
    
    .class private auto ansi beforefieldinit Hero
        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
        }
    }
    
    .class private auto ansi beforefieldinit ArrayOfHeroes`1<const int32 V>
        extends class FixedArray`2<class Hero, !V>
    {
        .method public hidebysig specialname rtspecialname 
            instance void .ctor () cil managed 
        {
            .maxtack 8
    
            ldarg.0
            call instance void class FixedArray`2<class Hero, !V>::.ctor()
            ret
        }
    }
    
    .class private auto ansi beforefieldinit RingBuffer`1<T>
        extends class FixedArray`2<!T, const int32(10)>
    {
        .method public hidebysig specialname rtspecialname 
            instance void .ctor () cil managed 
        {
            .maxtack 8
    
            ldarg.0
            call instance void class FixedArray`2<!T, const int32(10)>::.ctor()
            ret
        }
    }
    
  8. hez2010 commented on Jul 28, 2023

    @hez2010
    OwnerAuthor

    I updated the design to remove theconst before a type parameter reference.
    You should use !T and !!T instead of const !T and const !!T

  9. changed the title [-]WIP: Design to const generics support in CoreCLR[/-] [+]Design to const generics support in CoreCLR[/+] on Jul 28, 2023
  10. added a commit that references this issue on Aug 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions