Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 18 additions & 15 deletions src/Compiler/Optimize/DetupleArgs.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

module internal FSharp.Compiler.Detuple

open System.Collections.Generic
open Internal.Utilities.Collections
open Internal.Utilities.Library
open FSharp.Compiler.DiagnosticsLogger
Expand Down Expand Up @@ -173,6 +174,9 @@ module GlobalUsageAnalysis =

type accessor = TupleGet of int * TType list

let valStampEquality =
HashIdentity.FromFunctions (fun (v: Val) -> int v.Stamp) (fun (v1: Val) v2 -> v1.Stamp = v2.Stamp)

/// Expr information.
/// For each v,
/// (a) log it's usage site context = accessors // APP type-inst args
Expand All @@ -181,7 +185,7 @@ module GlobalUsageAnalysis =
type Results =
{
/// v -> context / APP inst args
Uses: Zmap<Val, (accessor list * TType list * Expr list) list>
Uses: Dictionary<Val, (accessor list * TType list * Expr list) list>

/// v -> binding repr
Defns: Zmap<Val, Expr>
Expand All @@ -197,8 +201,9 @@ module GlobalUsageAnalysis =
IterationIsAtTopLevel: bool
}

let z0 =
{ Uses = Zmap.empty valOrder
/// New instance per file: Uses is mutable and implementation files are optimized in parallel.
let mkInitialResults () =
{ Uses = Dictionary<Val, _>(valStampEquality)
Defns = Zmap.empty valOrder
RecursiveBindings = Zmap.empty valOrder
DecisionTreeBindings = Zset.empty valOrder
Expand All @@ -208,11 +213,8 @@ module GlobalUsageAnalysis =
/// Log the use of a value with a particular tuple shape at a callsite
/// Note: this routine is called very frequently
let logUse (f: Val) tup z =
{ z with
Uses =
match Zmap.tryFind f z.Uses with
| Some sites -> Zmap.add f (tup :: sites) z.Uses
| None -> Zmap.add f [ tup ] z.Uses }
z.Uses.BagAdd(f, tup)
z

/// Log the definition of a binding
let logBinding z (isInDTree, v) =
Expand Down Expand Up @@ -342,7 +344,7 @@ module GlobalUsageAnalysis =

let GetUsageInfoOfImplFile g expr =
let folder = UsageFolders g
let z = FoldImplFile folder z0 expr
let z = FoldImplFile folder (mkInitialResults ()) expr
z

let internalError str = raise (Failure(str))
Expand Down Expand Up @@ -610,9 +612,9 @@ let decideFormalSuggestedCP g z tys vss =
TupleTS tss

let trimTsByVal z ts v =
match Zmap.tryFind v z.Uses with
| None -> UnknownTS (* formal has no usage info, it is unused *)
| Some sites ->
match z.Uses.TryGetValue v with
| false, _ -> UnknownTS (* formal has no usage info, it is unused *)
| true, sites ->
let trim ts (accessors, _inst, _args) = trimTsByAccess accessors ts
List.fold trim ts sites

Expand Down Expand Up @@ -703,9 +705,10 @@ let determineTransforms (scope: PerFileNamingScope) g (z: Results) =
decideTransform scope g z f callPatterns (m, tps, vss, retTy) // make transform (if required)

let vtransforms =
Zmap.toList z.Uses
|> List.sortBy (fst >> valSourceOrderKey)
|> List.choose (fun (f, sites) -> selectTransform f sites)
z.Uses
|> Seq.sortBy (fun (KeyValue(f, _)) -> struct (valSourceOrderKey f, f.Stamp))
|> Seq.choose (fun (KeyValue(f, sites)) -> selectTransform f sites)
|> List.ofSeq
let vtransforms = Zmap.ofList valOrder vtransforms
vtransforms

Expand Down
3 changes: 2 additions & 1 deletion src/Compiler/Optimize/DetupleArgs.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
module internal FSharp.Compiler.Detuple

open Internal.Utilities.Collections
open System.Collections.Generic
open FSharp.Compiler.CompilerGlobalState
open FSharp.Compiler.TcGlobals
open FSharp.Compiler.TypedTree
Expand All @@ -17,7 +18,7 @@ module GlobalUsageAnalysis =
type Results =
{
/// v -> context / APP inst args
Uses: Zmap<Val, (accessor list * TType list * Expr list) list>
Uses: Dictionary<Val, (accessor list * TType list * Expr list) list>

/// v -> binding repr
Defns: Zmap<Val, Expr>
Expand Down
6 changes: 3 additions & 3 deletions src/Compiler/Optimize/InnerLambdasToTopLevelFuncs.fs
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,9 @@ let IsMandatoryNonTopLevel g (f: Val) =
module Pass1_DetermineTLRAndArities =

let GetMaxNumArgsAtUses xinfo f =
match Zmap.tryFind f xinfo.Uses with
| None -> 0 (* no call sites *)
| Some sites ->
match xinfo.Uses.TryGetValue f with
| false, _ -> 0 (* no call sites *)
| true, sites ->
sites |> List.map (fun (_accessors, _tinst, args) -> List.length args) |> List.max

let SelectTLRVals amap g xinfo f e =
Expand Down
Loading