Skip to content

Commit 3efb547

Browse files
committed
Cancellable: add InterruptibleLazy
1 parent b0f2370 commit 3efb547

33 files changed

+512
-170
lines changed

src/Compiler/AbstractIL/il.fs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
module FSharp.Compiler.AbstractIL.IL
44

55
open FSharp.Compiler.IO
6+
open Internal.Utilities.Library
67

78
#nowarn "49"
89
#nowarn "343" // The type 'ILAssemblyRef' implements 'System.IComparable' explicitly but provides no corresponding override for 'Object.Equals'.
@@ -29,16 +30,14 @@ let _ =
2930
if logging then
3031
dprintn "* warning: Il.logging is on"
3132

32-
let notlazy v = Lazy<_>.CreateFromValue v
33-
3433
/// A little ugly, but the idea is that if a data structure does not
3534
/// contain lazy values then we don't add laziness. So if the thing to map
3635
/// is already evaluated then immediately apply the function.
37-
let lazyMap f (x: Lazy<_>) =
36+
let lazyMap f (x: InterruptibleLazy<_>) =
3837
if x.IsValueCreated then
3938
notlazy (f (x.Force()))
4039
else
41-
lazy (f (x.Force()))
40+
InterruptibleLazy(fun _ -> f (x.Force()))
4241

4342
[<RequireQualifiedAccess>]
4443
type PrimaryAssembly =
@@ -165,7 +164,7 @@ let splitTypeNameRight nm =
165164
// --------------------------------------------------------------------
166165

167166
/// This is used to store event, property and field maps.
168-
type LazyOrderedMultiMap<'Key, 'Data when 'Key: equality>(keyf: 'Data -> 'Key, lazyItems: Lazy<'Data list>) =
167+
type LazyOrderedMultiMap<'Key, 'Data when 'Key: equality>(keyf: 'Data -> 'Key, lazyItems: InterruptibleLazy<'Data list>) =
169168

170169
let quickMap =
171170
lazyItems
@@ -1822,7 +1821,7 @@ type ILMethodVirtualInfo =
18221821

18231822
[<RequireQualifiedAccess>]
18241823
type MethodBody =
1825-
| IL of Lazy<ILMethodBody>
1824+
| IL of InterruptibleLazy<ILMethodBody>
18261825
| PInvoke of Lazy<PInvokeMethod> (* platform invoke to native *)
18271826
| Abstract
18281827
| Native
@@ -1903,7 +1902,7 @@ type ILMethodDef
19031902
callingConv: ILCallingConv,
19041903
parameters: ILParameters,
19051904
ret: ILReturn,
1906-
body: Lazy<MethodBody>,
1905+
body: InterruptibleLazy<MethodBody>,
19071906
isEntryPoint: bool,
19081907
genericParams: ILGenericParameterDefs,
19091908
securityDeclsStored: ILSecurityDeclsStored,
@@ -1962,7 +1961,7 @@ type ILMethodDef
19621961
?callingConv: ILCallingConv,
19631962
?parameters: ILParameters,
19641963
?ret: ILReturn,
1965-
?body: Lazy<MethodBody>,
1964+
?body: InterruptibleLazy<MethodBody>,
19661965
?securityDecls: ILSecurityDecls,
19671966
?isEntryPoint: bool,
19681967
?genericParams: ILGenericParameterDefs,
@@ -2468,7 +2467,7 @@ type ILMethodImplDef =
24682467

24692468
// Index table by name and arity.
24702469
type ILMethodImplDefs =
2471-
| ILMethodImpls of Lazy<MethodImplsMap>
2470+
| ILMethodImpls of InterruptibleLazy<MethodImplsMap>
24722471

24732472
member x.AsList() =
24742473
let (ILMethodImpls ltab) = x in Map.foldBack (fun _x y r -> y @ r) (ltab.Force()) []
@@ -2919,7 +2918,7 @@ type ILNestedExportedType =
29192918
override x.ToString() = "exported type " + x.Name
29202919

29212920
and ILNestedExportedTypes =
2922-
| ILNestedExportedTypes of Lazy<Map<string, ILNestedExportedType>>
2921+
| ILNestedExportedTypes of InterruptibleLazy<Map<string, ILNestedExportedType>>
29232922

29242923
member x.AsList() =
29252924
let (ILNestedExportedTypes ltab) = x in Map.foldBack (fun _x y r -> y :: r) (ltab.Force()) []
@@ -2943,7 +2942,7 @@ and [<NoComparison; NoEquality>] ILExportedTypeOrForwarder =
29432942
override x.ToString() = "exported type " + x.Name
29442943

29452944
and ILExportedTypesAndForwarders =
2946-
| ILExportedTypesAndForwarders of Lazy<Map<string, ILExportedTypeOrForwarder>>
2945+
| ILExportedTypesAndForwarders of InterruptibleLazy<Map<string, ILExportedTypeOrForwarder>>
29472946

29482947
member x.AsList() =
29492948
let (ILExportedTypesAndForwarders ltab) = x in Map.foldBack (fun _x y r -> y :: r) (ltab.Force()) []
@@ -3784,7 +3783,7 @@ let mkILMethodBody (initlocals, locals, maxstack, code, tag, imports) : ILMethod
37843783

37853784
let mkMethodBody (zeroinit, locals, maxstack, code, tag, imports) =
37863785
let ilCode = mkILMethodBody (zeroinit, locals, maxstack, code, tag, imports)
3787-
MethodBody.IL(lazy ilCode)
3786+
MethodBody.IL(InterruptibleLazy.FromValue ilCode)
37883787

37893788
// --------------------------------------------------------------------
37903789
// Make a constructor
@@ -4098,7 +4097,7 @@ let mkILExportedTypes l =
40984097
ILExportedTypesAndForwarders(notlazy (List.foldBack addExportedTypeToTable l Map.empty))
40994098

41004099
let mkILExportedTypesLazy (l: Lazy<_>) =
4101-
ILExportedTypesAndForwarders(lazy (List.foldBack addExportedTypeToTable (l.Force()) Map.empty))
4100+
ILExportedTypesAndForwarders(InterruptibleLazy(fun _ -> List.foldBack addExportedTypeToTable (l.Force()) Map.empty))
41024101

41034102
let addNestedExportedTypeToTable (y: ILNestedExportedType) tab = Map.add y.Name y tab
41044103

@@ -4116,7 +4115,7 @@ let mkILNestedExportedTypes l =
41164115
ILNestedExportedTypes(notlazy (List.foldBack addNestedExportedTypeToTable l Map.empty))
41174116

41184117
let mkILNestedExportedTypesLazy (l: Lazy<_>) =
4119-
ILNestedExportedTypes(lazy (List.foldBack addNestedExportedTypeToTable (l.Force()) Map.empty))
4118+
ILNestedExportedTypes(InterruptibleLazy(fun _ -> List.foldBack addNestedExportedTypeToTable (l.Force()) Map.empty))
41204119

41214120
let mkILResources l = ILResources l
41224121
let emptyILResources = ILResources []
@@ -4130,7 +4129,7 @@ let mkILMethodImpls l =
41304129
ILMethodImpls(notlazy (List.foldBack addMethodImplToTable l Map.empty))
41314130

41324131
let mkILMethodImplsLazy l =
4133-
ILMethodImpls(lazy (List.foldBack addMethodImplToTable (Lazy.force l) Map.empty))
4132+
ILMethodImpls(InterruptibleLazy(fun _ -> List.foldBack addMethodImplToTable (Lazy.force l) Map.empty))
41344133

41354134
let emptyILMethodImpls = mkILMethodImpls []
41364135

src/Compiler/AbstractIL/il.fsi

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -984,7 +984,7 @@ type internal ILOverridesSpec =
984984

985985
[<RequireQualifiedAccess>]
986986
type MethodBody =
987-
| IL of Lazy<ILMethodBody>
987+
| IL of InterruptibleLazy<ILMethodBody>
988988
| PInvoke of Lazy<PInvokeMethod>
989989
| Abstract
990990
| Native
@@ -1033,7 +1033,7 @@ type ILMethodDef =
10331033
callingConv: ILCallingConv *
10341034
parameters: ILParameters *
10351035
ret: ILReturn *
1036-
body: Lazy<MethodBody> *
1036+
body: InterruptibleLazy<MethodBody> *
10371037
isEntryPoint: bool *
10381038
genericParams: ILGenericParameterDefs *
10391039
securityDeclsStored: ILSecurityDeclsStored *
@@ -1049,7 +1049,7 @@ type ILMethodDef =
10491049
callingConv: ILCallingConv *
10501050
parameters: ILParameters *
10511051
ret: ILReturn *
1052-
body: Lazy<MethodBody> *
1052+
body: InterruptibleLazy<MethodBody> *
10531053
isEntryPoint: bool *
10541054
genericParams: ILGenericParameterDefs *
10551055
securityDecls: ILSecurityDecls *
@@ -1140,7 +1140,7 @@ type ILMethodDef =
11401140
?callingConv: ILCallingConv *
11411141
?parameters: ILParameters *
11421142
?ret: ILReturn *
1143-
?body: Lazy<MethodBody> *
1143+
?body: InterruptibleLazy<MethodBody> *
11441144
?securityDecls: ILSecurityDecls *
11451145
?isEntryPoint: bool *
11461146
?genericParams: ILGenericParameterDefs *
@@ -2075,11 +2075,11 @@ val internal mkILMethodBody:
20752075

20762076
val internal mkMethodBody: bool * ILLocals * int * ILCode * ILDebugPoint option * ILDebugImports option -> MethodBody
20772077

2078-
val internal methBodyNotAvailable: Lazy<MethodBody>
2078+
val internal methBodyNotAvailable: InterruptibleLazy<MethodBody>
20792079

2080-
val internal methBodyAbstract: Lazy<MethodBody>
2080+
val internal methBodyAbstract: InterruptibleLazy<MethodBody>
20812081

2082-
val internal methBodyNative: Lazy<MethodBody>
2082+
val internal methBodyNative: InterruptibleLazy<MethodBody>
20832083

20842084
val internal mkILCtor: ILMemberAccess * ILParameter list * MethodBody -> ILMethodDef
20852085

@@ -2217,11 +2217,11 @@ val storeILSecurityDecls: ILSecurityDecls -> ILSecurityDeclsStored
22172217
val internal mkILSecurityDeclsReader: (int32 -> ILSecurityDecl[]) -> ILSecurityDeclsStored
22182218

22192219
val mkILEvents: ILEventDef list -> ILEventDefs
2220-
val mkILEventsLazy: Lazy<ILEventDef list> -> ILEventDefs
2220+
val mkILEventsLazy: InterruptibleLazy<ILEventDef list> -> ILEventDefs
22212221
val emptyILEvents: ILEventDefs
22222222

22232223
val mkILProperties: ILPropertyDef list -> ILPropertyDefs
2224-
val mkILPropertiesLazy: Lazy<ILPropertyDef list> -> ILPropertyDefs
2224+
val mkILPropertiesLazy: InterruptibleLazy<ILPropertyDef list> -> ILPropertyDefs
22252225
val emptyILProperties: ILPropertyDefs
22262226

22272227
val mkILMethods: ILMethodDef list -> ILMethodDefs
@@ -2230,7 +2230,7 @@ val mkILMethodsComputed: (unit -> ILMethodDef[]) -> ILMethodDefs
22302230
val emptyILMethods: ILMethodDefs
22312231

22322232
val mkILFields: ILFieldDef list -> ILFieldDefs
2233-
val mkILFieldsLazy: Lazy<ILFieldDef list> -> ILFieldDefs
2233+
val mkILFieldsLazy: InterruptibleLazy<ILFieldDef list> -> ILFieldDefs
22342234
val emptyILFields: ILFieldDefs
22352235

22362236
val mkILMethodImpls: ILMethodImplDef list -> ILMethodImplDefs

src/Compiler/AbstractIL/ilmorph.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ let morphILMethodBody fMethBody (x: MethodBody) =
305305
match x with
306306
| MethodBody.IL il ->
307307
let ilCode = fMethBody il.Value // Eager
308-
MethodBody.IL(lazy ilCode)
308+
MethodBody.IL(InterruptibleLazy.FromValue ilCode)
309309
| x -> x
310310

311311
let ospec_ty2ty f (OverridesSpec (mref, ty)) = OverridesSpec(mref_ty2ty f mref, f ty)

src/Compiler/AbstractIL/ilread.fs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,7 +1129,7 @@ type ILMetadataReader =
11291129
mdfile: BinaryFile
11301130
pectxtCaptured: PEReader option // only set when reading full PE including code etc. for static linking
11311131
entryPointToken: TableName * int
1132-
dataEndPoints: Lazy<int32 list>
1132+
dataEndPoints: InterruptibleLazy<int32 list>
11331133
fileName: string
11341134
getNumRows: TableName -> int
11351135
userStringsStreamPhysicalLoc: int32
@@ -1766,7 +1766,7 @@ let readNativeResources (pectxt: PEReader) =
17661766
]
17671767

17681768
let getDataEndPointsDelayed (pectxt: PEReader) ctxtH =
1769-
lazy
1769+
InterruptibleLazy(fun _ ->
17701770
let (ctxt: ILMetadataReader) = getHole ctxtH
17711771
let mdv = ctxt.mdfile.GetView()
17721772

@@ -1826,14 +1826,14 @@ let getDataEndPointsDelayed (pectxt: PEReader) ctxtH =
18261826
[ ("managed vtable_fixups", pectxt.vtableFixupsAddr) ])
18271827
@ methodRVAs)))
18281828
|> List.distinct
1829-
|> List.sort
1829+
|> List.sort)
18301830

18311831
let rvaToData (ctxt: ILMetadataReader) (pectxt: PEReader) nm rva =
18321832
if rva = 0x0 then
18331833
failwith "rva is zero"
18341834

18351835
let start = pectxt.anyV2P (nm, rva)
1836-
let endPoints = (Lazy.force ctxt.dataEndPoints)
1836+
let endPoints = ctxt.dataEndPoints.Value
18371837

18381838
let rec look l =
18391839
match l with
@@ -2424,14 +2424,14 @@ and seekReadField ctxt mdv (numTypars, hasLayout) (idx: int) =
24242424

24252425
and seekReadFields (ctxt: ILMetadataReader) (numTypars, hasLayout) fidx1 fidx2 =
24262426
mkILFieldsLazy (
2427-
lazy
2427+
InterruptibleLazy(fun _ ->
24282428
let mdv = ctxt.mdfile.GetView()
24292429

24302430
[
24312431
if fidx1 > 0 then
24322432
for i = fidx1 to fidx2 - 1 do
24332433
yield seekReadField ctxt mdv (numTypars, hasLayout) i
2434-
]
2434+
])
24352435
)
24362436

24372437
and seekReadMethods (ctxt: ILMetadataReader) numTypars midx1 midx2 =
@@ -3064,7 +3064,7 @@ and seekReadEvent ctxt mdv numTypars idx =
30643064
(* REVIEW: can substantially reduce numbers of EventMap and PropertyMap reads by first checking if the whole table mdv sorted according to ILTypeDef tokens and then doing a binary chop *)
30653065
and seekReadEvents (ctxt: ILMetadataReader) numTypars tidx =
30663066
mkILEventsLazy (
3067-
lazy
3067+
InterruptibleLazy(fun _ ->
30683068
let mdv = ctxt.mdfile.GetView()
30693069

30703070
match
@@ -3090,7 +3090,7 @@ and seekReadEvents (ctxt: ILMetadataReader) numTypars tidx =
30903090
if beginEventIdx > 0 then
30913091
for i in beginEventIdx .. endEventIdx - 1 do
30923092
yield seekReadEvent ctxt mdv numTypars i
3093-
]
3093+
])
30943094
)
30953095

30963096
and seekReadProperty ctxt mdv numTypars idx =
@@ -3131,7 +3131,7 @@ and seekReadProperty ctxt mdv numTypars idx =
31313131

31323132
and seekReadProperties (ctxt: ILMetadataReader) numTypars tidx =
31333133
mkILPropertiesLazy (
3134-
lazy
3134+
InterruptibleLazy(fun _ ->
31353135
let mdv = ctxt.mdfile.GetView()
31363136

31373137
match
@@ -3157,7 +3157,7 @@ and seekReadProperties (ctxt: ILMetadataReader) numTypars tidx =
31573157
if beginPropIdx > 0 then
31583158
for i in beginPropIdx .. endPropIdx - 1 do
31593159
yield seekReadProperty ctxt mdv numTypars i
3160-
]
3160+
])
31613161
)
31623162

31633163
and customAttrsReader ctxtH tag : ILAttributesStored =
@@ -3251,7 +3251,7 @@ and seekReadConstant (ctxt: ILMetadataReader) idx =
32513251
| _ -> ILFieldInit.Null
32523252

32533253
and seekReadImplMap (ctxt: ILMetadataReader) nm midx =
3254-
lazy
3254+
InterruptibleLazy(fun _ ->
32553255
MethodBody.PInvoke(
32563256
lazy
32573257
let mdv = ctxt.mdfile.GetView()
@@ -3339,7 +3339,7 @@ and seekReadImplMap (ctxt: ILMetadataReader) nm midx =
33393339
| Some nm2 -> nm2)
33403340
Where = seekReadModuleRef ctxt mdv scopeIdx
33413341
}
3342-
)
3342+
))
33433343

33443344
and seekReadTopCode (ctxt: ILMetadataReader) pev mdv numTypars (sz: int) start =
33453345
let labelsOfRawOffsets = Dictionary<_, _>(sz / 2)
@@ -3633,7 +3633,7 @@ and seekReadTopCode (ctxt: ILMetadataReader) pev mdv numTypars (sz: int) start =
36333633
instrs, rawToLabel, lab2pc
36343634

36353635
and seekReadMethodRVA (pectxt: PEReader) (ctxt: ILMetadataReader) (nm, noinline, aggressiveinline, numTypars) rva =
3636-
lazy
3636+
InterruptibleLazy(fun _ ->
36373637
let pev = pectxt.pefile.GetView()
36383638
let baseRVA = pectxt.anyV2P ("method rva", rva)
36393639
// ": reading body of method "+nm+" at rva "+string rva+", phys "+string baseRVA
@@ -3650,7 +3650,7 @@ and seekReadMethodRVA (pectxt: PEReader) (ctxt: ILMetadataReader) (nm, noinline,
36503650
else
36513651

36523652
MethodBody.IL(
3653-
lazy
3653+
InterruptibleLazy(fun _ ->
36543654
let pev = pectxt.pefile.GetView()
36553655
let mdv = ctxt.mdfile.GetView()
36563656

@@ -3824,8 +3824,8 @@ and seekReadMethodRVA (pectxt: PEReader) (ctxt: ILMetadataReader) (nm, noinline,
38243824
Code = code
38253825
DebugRange = None
38263826
DebugImports = None
3827-
}
3828-
)
3827+
})
3828+
))
38293829

38303830
and int32AsILVariantType (ctxt: ILMetadataReader) (n: int32) =
38313831
if List.memAssoc n (Lazy.force ILVariantTypeRevMap) then

src/Compiler/AbstractIL/ilx.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ type IlxClosureInfo =
167167
{
168168
cloStructure: IlxClosureLambdas
169169
cloFreeVars: IlxClosureFreeVar[]
170-
cloCode: Lazy<ILMethodBody>
170+
cloCode: InterruptibleLazy<ILMethodBody>
171171
cloUseStaticField: bool
172172
}
173173

src/Compiler/AbstractIL/ilx.fsi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
module internal FSharp.Compiler.AbstractIL.ILX.Types
55

66
open FSharp.Compiler.AbstractIL.IL
7+
open Internal.Utilities.Library
78

89
/// Union case field
910
[<Sealed>]
@@ -118,7 +119,7 @@ type IlxClosureApps =
118119
type IlxClosureInfo =
119120
{ cloStructure: IlxClosureLambdas
120121
cloFreeVars: IlxClosureFreeVar[]
121-
cloCode: Lazy<ILMethodBody>
122+
cloCode: InterruptibleLazy<ILMethodBody>
122123
cloUseStaticField: bool }
123124

124125
/// Represents a discriminated union type prior to erasure

0 commit comments

Comments
 (0)