Skip to content

Commit

Permalink
Fix 16105
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinRansom committed Feb 20, 2024
1 parent 9d36d64 commit 7019919
Show file tree
Hide file tree
Showing 4 changed files with 224 additions and 8 deletions.
20 changes: 16 additions & 4 deletions src/Compiler/Optimize/Optimizer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3062,11 +3062,14 @@ and TryOptimizeVal cenv env (vOpt: ValRef option, mustInline, inlineIfLambda, va
failwith "tuple, union and record values cannot be marked 'inline'"

| UnknownValue when mustInline ->
warning(Error(FSComp.SR.optValueMarkedInlineHasUnexpectedValue(), m)); None
warning(Error(FSComp.SR.optValueMarkedInlineHasUnexpectedValue(), m))
None

| _ when mustInline ->
warning(Error(FSComp.SR.optValueMarkedInlineCouldNotBeInlined(), m)); None
| _ -> None
warning(Error(FSComp.SR.optValueMarkedInlineCouldNotBeInlined(), m))
None

| _ -> None

and TryOptimizeValInfo cenv env m vinfo =
if vinfo.HasEffect then None else TryOptimizeVal cenv env (None, false, false, vinfo.Info, m)
Expand Down Expand Up @@ -3402,7 +3405,7 @@ and TryInlineApplication cenv env finfo (tyargs: TType list, args: Expr list, m)
| _ -> false
| _ -> false
| _ -> false
| _ -> false
| _ -> false

if isValFromLazyExtensions then None else

Expand All @@ -3421,6 +3424,15 @@ and TryInlineApplication cenv env finfo (tyargs: TType list, args: Expr list, m)

if isGetHashCode then None else

let isApplicationComplete =
match finfo.Info with
| ValValue (_, CurriedLambdaValue (_, _, _, Expr.Lambda (_, _, _, _, Expr.App (Expr.Val(vr,_,_), _, _, _, _), _, _), _) ) ->
IsPartialExprVal (GetInfoForVal cenv env m vr).ValExprInfo
| _ ->
false

if isApplicationComplete then None else

// Inlining lambda
let f2R = CopyExprForInlining cenv false f2 m

Expand Down
201 changes: 200 additions & 1 deletion tests/FSharp.Compiler.ComponentTests/EmittedIL/NoCompilerInlining.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,206 @@ namespace EmittedIL
open Xunit
open FSharp.Test.Compiler

module ``NoCompilerInlining`` =
module NoCompilerInlining =

[<Fact>]
let ``Methods marked internal not available for cross module inlining``() =

let outerModule =
FSharpWithFileName
"outerModule.fs"
"""
module internal OuterModule
open System.Runtime.CompilerServices
[<assembly:InternalsVisibleTo("middleModule")>]
do ()
let sayOuterModuleHello () = System.Console.WriteLine("Hello World") """
|> withOptimize
|> asLibrary
|> withName "outerLibrary"

let middleModule =
FSharpWithFileName
"middleModule.fs"
"""
module MiddleModule
let sayMiddleModuleHello () = OuterModule.sayOuterModuleHello()"""
|> withOptimize
|> withReferences [outerModule]
|> asLibrary
|> withName "middleModule"

FSharpWithFileName
"program.fs"
"""MiddleModule.sayMiddleModuleHello()"""
|> withOptimize
|> withReferences [middleModule; outerModule]
|> withName "Program"
|> compileExeAndRun
|> shouldSucceed
|> verifyIL [ """
.method public static void main@() cil managed
{
.entrypoint
.maxstack 8
IL_0000: call void [middleModule]MiddleModule::sayMiddleModuleHello()
IL_0005: ret
}
""" ]

[<Fact>]
let ``Methods marked public available for cross module inlining``() =

let outerModule =
FSharpWithFileName
"outerModule.fs"
"""
module OuterModule
open System.Runtime.CompilerServices
let sayOuterModuleHello () = System.Console.WriteLine("Hello World") """
|> withOptimize
|> asLibrary
|> withName "outerLibrary"

let middleModule =
FSharpWithFileName
"middleModule.fs"
"""
module MiddleModule
let sayMiddleModuleHello () = OuterModule.sayOuterModuleHello()"""
|> withOptimize
|> withReferences [outerModule]
|> asLibrary
|> withName "middleModule"

FSharpWithFileName
"program.fs"
"""MiddleModule.sayMiddleModuleHello()"""
|> withOptimize
|> withReferences [middleModule; outerModule]
|> withName "Program"
|> compileExeAndRun
|> shouldSucceed
|> verifyIL [ """
.method public static void main@() cil managed
{
.entrypoint
.maxstack 8
IL_0000: ldstr "Hello World"
IL_0005: call void [runtime]System.Console::WriteLine(string)
IL_000a: ret
}
""" ]


[<Fact>]
let ``Nested Module marked internal not available for cross module inlining``() =

let outerModule =
FSharpWithFileName
"outerModule.fs"
"""
module OuterModule
open System.Runtime.CompilerServices
[<assembly:InternalsVisibleTo("middleModule")>]
do ()
module internal nestedModule =
let sayNestedModuleHello () = System.Console.WriteLine("Hello World")
let sayOuterModuleHello () = nestedModule.sayNestedModuleHello () """
|> withOptimize
|> asLibrary
|> withName "outerLibrary"

let middleModule =
FSharpWithFileName
"middleModule.fs"
"""
module MiddleModule
let sayMiddleModuleHello () = OuterModule.sayOuterModuleHello()"""
|> withOptimize
|> withReferences [outerModule]
|> asLibrary
|> withName "middleModule"

FSharpWithFileName
"program.fs"
"""MiddleModule.sayMiddleModuleHello()"""
|> withOptimize
|> withReferences [middleModule; outerModule]
|> withName "Program"
|> compileExeAndRun
|> shouldSucceed
|> verifyIL [ """
.method public static void main@() cil managed
{
.entrypoint
.maxstack 8
IL_0000: ldstr "Hello World"
IL_0005: call void [runtime]System.Console::WriteLine(string)
IL_000a: ret
}
""" ]

[<Fact>]
let ``Nested Module marked public available for cross module inlining``() =

let outerModule =
FSharpWithFileName
"outerModule.fs"
"""
module OuterModule
open System.Runtime.CompilerServices
module nestedModule =
let sayNestedModuleHello () = System.Console.WriteLine("Hello World")
let sayOuterModuleHello () = nestedModule.sayNestedModuleHello () """
|> withOptimize
|> asLibrary
|> withName "outerLibrary"

let middleModule =
FSharpWithFileName
"middleModule.fs"
"""
module MiddleModule
let sayMiddleModuleHello () = OuterModule.sayOuterModuleHello()"""
|> withOptimize
|> withReferences [outerModule]
|> asLibrary
|> withName "middleModule"

FSharpWithFileName
"program.fs"
"""MiddleModule.sayMiddleModuleHello()"""
|> withOptimize
|> withReferences [middleModule; outerModule]
|> withName "Program"
|> compileExeAndRun
|> shouldSucceed
|> verifyIL [ """
.method public static void main@() cil managed
{
.entrypoint
.maxstack 8
IL_0000: ldstr "Hello World"
IL_0005: call void [runtime]System.Console::WriteLine(string)
IL_000a: ret
}
""" ]



[<Fact>]
let ``Function marked with NoCompilerInlining is not inlined by the compiler``() =
FSharp """
Expand Down
4 changes: 4 additions & 0 deletions tests/FSharp.Test.Utilities/Compiler.fs
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,10 @@ module rec Compiler =
let FSharp (source: string) : CompilationUnit =
Fs source

let FSharpWithFileName name (source: string) : CompilationUnit =
fsFromString (SourceCodeFileKind.Fs({FileName=name; SourceText=Some source }))
|> FS

let FsFromPath (path: string) : CompilationUnit =
fsFromString (SourceFromPath path)
|> FS
Expand Down
7 changes: 4 additions & 3 deletions tests/FSharp.Test.Utilities/CompilerAssert.fs
Original file line number Diff line number Diff line change
Expand Up @@ -439,9 +439,10 @@ module rec CompilerAssertHelpers =
| Some text ->
// In memory source file copy it to the build directory
let source = item.ChangeExtension
File.WriteAllText (source.GetSourceFileName, text)
disposals.Add(disposeFile source.GetSourceFileName)
yield source
let destFileName = Path.Combine(outputDirectory.FullName, Path.GetFileName(source.GetSourceFileName))
File.WriteAllText (destFileName, text)
disposals.Add(disposeFile destFileName)
yield source.WithFileName(destFileName)
| None ->
// On Disk file
let sourceFileName = item.GetSourceFileName
Expand Down

0 comments on commit 7019919

Please sign in to comment.