You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Compiler Bug: Cross module inlining when it shouldn't #532
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
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.
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)?
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.
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.
Activity
manofstick commentedon Jul 15, 2015
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.)
dsyme commentedon Jul 15, 2015
Yes, this should be fixed (I can help with the fix, I know the code that prevents this sort of thing well enough).
zealisland commentedon Aug 27, 2015
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)?
dsyme commentedon Aug 27, 2015
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.
zealisland commentedon Aug 27, 2015
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
Also, it is InternalsVisibleTo Library2, then Library2 has
Now, there is a console app does
If they are compiled in Release, the IL for the "main" function has
As you can see that the internal "Repro.Tools.Foo::FooMethod()" got inlined and it should not be.
zealisland commentedon Aug 27, 2015
I found that if I write
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.
Merge pull request #26 from jinlmsft/master
ad test cases for dotnet#532
ad test cases for #532