将MethodImplOptions.AggressiveInlining应用于F#函数

时间:2016-10-18 09:37:45

标签: .net f# jit cil inlining

属性System.Runtime.CompilerServices.MethodImplAttribute可用于向JIT编译器提供有关如何处理修饰方法的提示。特别是,选项MethodImplOptions.AggressiveInlining指示编译器在可能的情况下内联受影响的方法。不幸的是,F#编译器在生成IL时似乎只是忽略了这个属性。

示例:以下C#代码

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int Inc(int x) => x + 1;

被翻译为

.method public hidebysig static int32  Inc(int32 x) cil managed aggressiveinlining
{     
    .maxstack  8
    IL_0000:  ldarg.0
    IL_0001:  ldc.i4.1
    IL_0002:  add
    IL_0003:  ret
}

请注意“aggressiveinlining”标志。

这个F#代码

[<MethodImpl(MethodImplOptions.AggressiveInlining)>]
let inc x = x + 1

变为

.method public static int32  inc(int32 x) cil managed
{
    .maxstack  8
    IL_0000:  nop
    IL_0001:  ldarg.0
    IL_0002:  ldc.i4.1
    IL_0003:  add
    IL_0004:  ret
}

没有“积极进取”。我还尝试将该属性应用于适当类(type ...)的静态和非静态方法,但结果是相同的。

但是,如果我将它应用于自定义索引器,就像这样

type Dummy =
    member self.Item
        with [<MethodImpl(MethodImplOptions.AggressiveInlining)>] get x = x + 1

得到的IL是

.method public hidebysig specialname instance int32 get_Item(int32 x) cil managed
{
    .custom instance void [mscorlib]System.Runtime.CompilerServices.MethodImplAttribute::.ctor(valuetype [mscorlib]System.Runtime.CompilerServices.MethodImplOptions) = ( 01 00 00 01 00 00 00 00 ) 
    .maxstack  8
    IL_0000:  nop
    IL_0001:  ldarg.1
    IL_0002:  ldc.i4.1
    IL_0003:  add
    IL_0004:  ret
}

...虽然我不确定这是否等同于C#编译器生成的“aggressiveinling”标志。

这种行为是期望/期望的吗?它是F#编译器中的错误吗?

(注意:我知道F#inline关键字,但这只适用于我的库的F#客户端,而不适用于C#使用者。)

1 个答案:

答案 0 :(得分:6)

@kvb是正确的,似乎F#编译器似乎剥离了MethodImpl

调用IlxGen.fs中的

ComputeMethodImplAttribs来计算方法属性。

and ComputeMethodImplAttribs cenv (_v:Val) attrs =
    let implflags = 
        match TryFindFSharpAttribute cenv.g cenv.g.attrib_MethodImplAttribute attrs with
        | Some (Attrib(_,_,[ AttribInt32Arg flags ],_,_,_,_))  -> flags
        | _ -> 0x0

    let hasPreserveSigAttr = 
        match TryFindFSharpAttributeOpt cenv.g cenv.g.attrib_PreserveSigAttribute attrs with
        | Some _ -> true
        | _ -> false

    // strip the MethodImpl pseudo-custom attribute    
    // The following method implementation flags are used here
    // 0x80 - hasPreserveSigImplFlag
    // 0x20 - synchronize
    // (See ECMA 335, Partition II, section 23.1.11 - Flags for methods [MethodImplAttributes]) 
    let attrs = attrs 
                    |> List.filter (IsMatchingFSharpAttribute cenv.g cenv.g.attrib_MethodImplAttribute >> not) 
                        |> List.filter (IsMatchingFSharpAttributeOpt cenv.g cenv.g.attrib_PreserveSigAttribute >> not)
    let hasPreserveSigImplFlag = ((implflags &&& 0x80) <> 0x0) || hasPreserveSigAttr
    let hasSynchronizedImplFlag = (implflags &&& 0x20) <> 0x0
    let hasNoInliningImplFlag = (implflags &&& 0x08) <> 0x0
    hasPreserveSigImplFlag, hasSynchronizedImplFlag, hasNoInliningImplFlag, attrs

仔细观察行:4990:

    let attrs = attrs 
                    |> List.filter (IsMatchingFSharpAttribute cenv.g cenv.g.attrib_MethodImplAttribute >> not) 
                        |> List.filter (IsMatchingFSharpAttributeOpt cenv.g cenv.g.attrib_PreserveSigAttribute >> not)

第一个filter过滤掉MethodImplAttribute

现在,我看起来有点想找到原理,但这段代码可以追溯到latkin初始提交。我认为剥离MethodImpl可能是错误的,特别是AggressiveInlining,我相信它会影响JIT:因此它需要在集会中。

我建议您注册issue。也许你至少可以得到解释。