Click here to monitor SSC

Subterranean IL: Custom modifiers

Published 26 November 2010 5:04 pm

In IL, volatile is an instruction prefix used to set a memory barrier at that instruction. However, in C#, volatile is applied to a field to indicate that all accesses on that field should be prefixed with volatile. As I mentioned in my previous post, this means that the field definition needs to store this information somehow, as such a field could be accessed from another assembly. However, IL does not have a concept of a ‘volatile field’. How is this information stored?

Attributes

The standard way of solving this is to apply a VolatileAttribute or similar to the field; this extra metadata notifies the C# compiler that all loads and stores to that field should use the volatile prefix. However, there is a problem with this approach, namely, the .NET C++ compiler. C++ allows methods to be overloaded using properties, like volatile or const, on the parameters; this is perfectly legal C++:

public ref class VolatileMethods {
    void Method(int *i) {}
    void Method(volatile int *i) {}
}
If volatile was specified using a custom attribute, then the VolatileMethods class wouldn’t be compilable to IL, as there is nothing to differentiate the two methods from each other. This is where custom modifiers come in.

Custom modifiers

Custom modifiers are similar to custom attributes, but instead of being applied to an IL element separately to its declaration, they are embedded within the field or parameter’s type signature itself. The VolatileMethods class would be compiled to the following IL:

.class public VolatileMethods {
    .method public instance void Method(int32* i) {}
    .method public instance void Method(
        int32 modreq(
[mscorlib]System.Runtime.CompilerServices.IsVolatile)* i) {} }
The modreq([mscorlib]System.Runtime.CompilerServices.IsVolatile) is the custom modifier. This adds a TypeDef or TypeRef token to the signature of the field or parameter, and even though they are mostly ignored by the CLR when it’s executing the program, this allows methods and fields to be overloaded in ways that wouldn’t be allowed using attributes. Because the modifiers are part of the signature, they need to be fully specified when calling such a method in IL:
call instance void Method(
    int32 modreq([mscorlib]System.Runtime.CompilerServices.IsVolatile)*)

There are two ways of applying modifiers; modreq specifies required modifiers (like IsVolatile), and modopt specifies optional modifiers that can be ignored by compilers (like IsLong or IsConst). The type specified as the modifier argument are simple placeholders; if you have a look at the definitions of IsVolatile and IsLong they are completely empty. They exist solely to be referenced by a modifier.

Custom modifiers are used extensively by the C++ compiler to specify concepts that aren’t expressible in IL, but still need to be taken into account when calling method overloads.

C++ and C#

That’s all very well and good, but how does this affect C#? Well, the C++ compiler uses modreq(IsVolatile) to specify volatility on both method parameters and fields, as it would be slightly odd to have the same concept represented using a modifier or attribute depending on what it was applied to. Once you’ve compiled your C++ project, it can then be referenced and used from C#, so the C# compiler has to recognise the modreq(IsVolatile) custom modifier applied to fields, and vice versa.

So, even though you can’t overload fields or parameters with volatile using C#, volatile needs to be expressed using a custom modifier rather than an attribute to guarentee correct interoperability and behaviour with any C++ dlls that happen to come along.

Next up: a closer look at attributes, and how certain attributes compile in unexpected ways.

2 Responses to “Subterranean IL: Custom modifiers”

  1. qwertie says:

    So you can overload on return value AND custom modifiers? This blows open my stackoverflow question about “after creating a dynamic assembly, how do I reliably get a delegate to call the correct version of an overloaded method?” http://stackoverflow.com/questions/3031183/reflection-emit-how-to-convert-methodbuilder-to-runtimemethodinfo-reliably

  2. Simon Cooper says:

    Most of the time you don’t need to worry about modifiers (C# and VB does not use them). Only when you’ve got a language that needs to overload methods using concepts that the CLR does not natively know about do you need to use them. So when you’re dealing with C++/CLI, then you do need to check the custom modifiers when searching method overloads. Otherwise, don’t worry about it.

Leave a Reply