Fra*_*ank 14 .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;
Run Code Online (Sandbox Code Playgroud)
被翻译成
.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
}
Run Code Online (Sandbox Code Playgroud)
请注意"aggressiveinlining"标志.
但是这个F#代码
[<MethodImpl(MethodImplOptions.AggressiveInlining)>]
let inc x = x + 1
Run Code Online (Sandbox Code Playgroud)
变
.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
}
Run Code Online (Sandbox Code Playgroud)
没有"积极主动".我还尝试将该属性应用于适当类(type ...)的静态和非静态方法,但结果是相同的.
但是,如果我将它应用于自定义索引器,就像这样
type Dummy =
member self.Item
with [<MethodImpl(MethodImplOptions.AggressiveInlining)>] get x = x + 1
Run Code Online (Sandbox Code Playgroud)
得到的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
}
Run Code Online (Sandbox Code Playgroud)
...虽然我不确定这是否等同于C#编译器生成的"aggressiveinling"标志.
这种行为是期望/预期的吗?它是F#编译器中的错误吗?
(注意:我知道F#inline关键字,但这只适用于我的库的F#客户端,而不适用于C#使用者.)
@kvb是正确的,似乎F#编译器似乎剥离了MethodImpl.
ComputeMethodImplAttribs在IlxGen.fs中调用以计算方法属性.
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
Run Code Online (Sandbox Code Playgroud)
仔细观察行:4990:
let attrs = attrs
|> List.filter (IsMatchingFSharpAttribute cenv.g cenv.g.attrib_MethodImplAttribute >> not)
|> List.filter (IsMatchingFSharpAttributeOpt cenv.g cenv.g.attrib_PreserveSigAttribute >> not)
Run Code Online (Sandbox Code Playgroud)
第一个filter过滤掉了MethodImplAttribute.
现在,我看起来有点试图找到基本原理,但这段代码可以追溯到latkin初始提交.我认为剥离MethodImpl特别是AggressiveInlining我认为影响JIT 的可能是错误的:因此它需要在组装中.
我建议注册一个问题.也许你至少可以得到一个解释.
| 归档时间: |
|
| 查看次数: |
390 次 |
| 最近记录: |