Can I suppress F# compiler making copy of function in IL code?
<p>I want to create a JIT GPU compiler. You give an F# function, and we JIT compile it. The key of JIT compiling is to be able to cache the compiling result. I tried to use the <code>MethodInfo</code> as the caching key, but it won't work. It seems that F# compiler will make a copy of the function instead of referencing the origin function. Is there a way to suppress this behavior?</p>

<p>Here is a test code, ideally, it should be just compiled twice, but it did it 4 times.</p>

<pre><code>let compileGpuCode (m:MethodInfo) =
    printfn "JIT compiling..."
    printfn "Type  : %A" m.ReflectedType
    printfn "Method: %A" m
    printfn ""
    "fake gpu code"

let gpuCodeCache = ConcurrentDictionary&lt;MethodInfo, string&gt;()

let launchGpu (func:int -&gt; int -&gt; int) =
    let m = func.GetType().GetMethod("Invoke", [| typeof&lt;int&gt;; typeof&lt;int&gt; |])
    let gpuCode = gpuCodeCache.GetOrAdd(m, compileGpuCode)
    // launch gpuCode
    ()

let myGpuCode (a:int) (b:int) = a + 2 * b

[&lt;Test&gt;]
let testFSFuncReflection() =
    launchGpu (+)
    launchGpu (+)
    launchGpu myGpuCode
    launchGpu myGpuCode
</code></pre>

<p>Here is the output:</p>

<pre><code>JIT compiling...
Type  : AleaTest.FS.Lab.Experiments+testFSFuncReflection@50
Method: Int32 Invoke(Int32, Int32)

JIT compiling...
Type  : AleaTest.FS.Lab.Experiments+testFSFuncReflection@51-1
Method: Int32 Invoke(Int32, Int32)

JIT compiling...
Type  : AleaTest.FS.Lab.Experiments+testFSFuncReflection@52-2
Method: Int32 Invoke(Int32, Int32)

JIT compiling...
Type  : AleaTest.FS.Lab.Experiments+testFSFuncReflection@53-3
Method: Int32 Invoke(Int32, Int32)
</code></pre>