Skip to content

Compiler Bug: Cross module inlining when it shouldn't #532

Closed

Description

@manofstick

NB. Now I'm at work so haven't tested this on the 4.0 compiler, so I may end up closing this bug before anyone reads it; but anyway...

If you have an assembly with two files which have the same namespace, and one has a record type with private constructor and the second has a factory method for the constructor, and then you have a second assembly that accesses the factory method, the f# compiler inlines the call to the internal type and throws an exception at runtime.

Assembly 1 - File 1

namespace ThisNamespaceHasToBeTheSame
type Hidden = private { x : int } with member this.X = this.x

Assembly 1 - File 2

namespace ThisNamespaceHasToBeTheSame
module Factory = let New () = { x = 0 } 

Assembly 2 - File 3

[<EntryPoint>]
let main argv = 
    let boom = ThisNamespaceHasToBeTheSame.Factory.New ()
    boom.X

The runtime exception:

Unhandled Exception: System.MethodAccessException: Attempt by method 'Program.main(System.String[])' to access method 'ThisNamespaceHasToBeTheSame.Hidden..ctor(Int32)' failed.
   at Program.main(String[] argv) in ...\Program.fs:line 4

The IL

IL_0000: nop
IL_0001: ldc.i4.0
IL_0002: newobj instance void [Library1]ThisNamespaceHasToBeTheSame.Hidden::.ctor(int32) // Shouldn't be accessing this!
IL_0007: tail.
IL_0009: callvirt instance int32 [Library1]ThisNamespaceHasToBeTheSame.Hidden::get_X()
IL_000e: ret

So this error only occurs if the two files share the same namespace, as otherwise you wouldn't be able to construct the record type in the second file. If file1 and file2 are merged then the compiles does not inline the call.

Activity

  1. manofstick commented on Jul 15, 2015

    @manofstick
    ContributorAuthor

    Confirmed that this is a still a problem with F# 4.0 in 2015 RC.

    (because it is to do with inlining, it only occurs in the optimized (release) configuration.)

  2. dsyme commented on Jul 15, 2015

    @dsyme
    Contributor

    Yes, this should be fixed (I can help with the fix, I know the code that prevents this sort of thing well enough).

  3. added this to the F# 4.0 Update 1 milestone on Aug 4, 2015
  4. zealisland commented on Aug 27, 2015

    @zealisland

    We are still on F# 3.1 and also bothered by this bug. We are not sure when we will move to F# 4.0, so cannot take advantage of the fix any time soon (even when it becomes available). Currently, we just try to rewrite the code in different random ways, then look at the IL to see if it's avoided. Is there any better/easier workaround that can be used to prevent a method from being inlined? (such as put a certain attribute on that method)?

  5. dsyme commented on Aug 27, 2015

    @dsyme
    Contributor

    Hi @zealisland, This bug should only impact the (very rare) scenario above - using "private" on the right of record/union type representations, with multiple namespace fragments for the same namespace in the library assembly, and inlinable helpers using the representation. Avoiding that combination should be sufficient. If necessary you can turn off the generation of cross assembly optimization information for the library assembly.

  6. zealisland commented on Aug 27, 2015

    @zealisland

    Hi @dsyme, I have a repro that is not the same as described by above example though has some similarity. https://drive.google.com/open?id=0BzV_3Q9QdVSOeEFKRlo1RGlKRE0. Please take a look. If possible, please share a reliable workaround.

    Essentially, an Library1 assembly has

    namespace Repro.Tools
    
    type internal Foo private () = 
        static member FooMethod() = ()
    

    Also, it is InternalsVisibleTo Library2, then Library2 has

    namespace Repro.Core
    open Repro.Tools
    
    type Bar () = 
    
        member x.BarMethod() =
           Foo.FooMethod()
    

    Now, there is a console app does

    open Repro.Core
    
    [<EntryPoint>]
    let main argv = 
        let x = Bar()
        x.BarMethod()
        0 // return an integer exit code
    

    If they are compiled in Release, the IL for the "main" function has

    IL_0000: nop
    IL_0001: newobj instance void [Library2]Repro.Core.Bar::.ctor()
    IL_0006: stloc.0
    IL_0007: call void [Library1]Repro.Tools.Foo::FooMethod()
    IL_000c: nop
    IL_000d: ldc.i4.0
    IL_000e: ret
    

    As you can see that the internal "Repro.Tools.Foo::FooMethod()" got inlined and it should not be.

  7. zealisland commented on Aug 27, 2015

    @zealisland

    I found that if I write

    member x.BarMethod() =
        let f = Foo.FooMethod
        f()
    

    Then it will work around the issue. However, I have to do this at every caller of Foo.FooMethod and can easily forget about doing it when add new code. It would be nice to have a workaround that modifies FooMethod itself to make it not inline-able.

  8. added a commit that references this issue on Aug 27, 2015
  9. added a commit that references this issue on Oct 8, 2015
  10. added a commit that references this issue on Oct 8, 2015
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

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions