Skip to content

Commit a47975f

Browse files
authored
Add InterruptibleLazy (#16179)
* Cancellable: add InterruptibleLazy
1 parent 84a2dcb commit a47975f

33 files changed

+513
-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
@@ -1794,7 +1794,7 @@ let readNativeResources (pectxt: PEReader) =
17941794
]
17951795

17961796
let getDataEndPointsDelayed (pectxt: PEReader) ctxtH =
1797-
lazy
1797+
InterruptibleLazy(fun _ ->
17981798
let (ctxt: ILMetadataReader) = getHole ctxtH
17991799
let mdv = ctxt.mdfile.GetView()
18001800

@@ -1854,14 +1854,14 @@ let getDataEndPointsDelayed (pectxt: PEReader) ctxtH =
18541854
[ ("managed vtable_fixups", pectxt.vtableFixupsAddr) ])
18551855
@ methodRVAs)))
18561856
|> List.distinct
1857-
|> List.sort
1857+
|> List.sort)
18581858

18591859
let rvaToData (ctxt: ILMetadataReader) (pectxt: PEReader) nm rva =
18601860
if rva = 0x0 then
18611861
failwith "rva is zero"
18621862

18631863
let start = pectxt.anyV2P (nm, rva)
1864-
let endPoints = (Lazy.force ctxt.dataEndPoints)
1864+
let endPoints = ctxt.dataEndPoints.Value
18651865

18661866
let rec look l =
18671867
match l with
@@ -2452,14 +2452,14 @@ and seekReadField ctxt mdv (numTypars, hasLayout) (idx: int) =
24522452

24532453
and seekReadFields (ctxt: ILMetadataReader) (numTypars, hasLayout) fidx1 fidx2 =
24542454
mkILFieldsLazy (
2455-
lazy
2455+
InterruptibleLazy(fun _ ->
24562456
let mdv = ctxt.mdfile.GetView()
24572457

24582458
[
24592459
if fidx1 > 0 then
24602460
for i = fidx1 to fidx2 - 1 do
24612461
yield seekReadField ctxt mdv (numTypars, hasLayout) i
2462-
]
2462+
])
24632463
)
24642464

24652465
and seekReadMethods (ctxt: ILMetadataReader) numTypars midx1 midx2 =
@@ -3092,7 +3092,7 @@ and seekReadEvent ctxt mdv numTypars idx =
30923092
(* 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 *)
30933093
and seekReadEvents (ctxt: ILMetadataReader) numTypars tidx =
30943094
mkILEventsLazy (
3095-
lazy
3095+
InterruptibleLazy(fun _ ->
30963096
let mdv = ctxt.mdfile.GetView()
30973097

30983098
match
@@ -3118,7 +3118,7 @@ and seekReadEvents (ctxt: ILMetadataReader) numTypars tidx =
31183118
if beginEventIdx > 0 then
31193119
for i in beginEventIdx .. endEventIdx - 1 do
31203120
yield seekReadEvent ctxt mdv numTypars i
3121-
]
3121+
])
31223122
)
31233123

31243124
and seekReadProperty ctxt mdv numTypars idx =
@@ -3159,7 +3159,7 @@ and seekReadProperty ctxt mdv numTypars idx =
31593159

31603160
and seekReadProperties (ctxt: ILMetadataReader) numTypars tidx =
31613161
mkILPropertiesLazy (
3162-
lazy
3162+
InterruptibleLazy(fun _ ->
31633163
let mdv = ctxt.mdfile.GetView()
31643164

31653165
match
@@ -3185,7 +3185,7 @@ and seekReadProperties (ctxt: ILMetadataReader) numTypars tidx =
31853185
if beginPropIdx > 0 then
31863186
for i in beginPropIdx .. endPropIdx - 1 do
31873187
yield seekReadProperty ctxt mdv numTypars i
3188-
]
3188+
])
31893189
)
31903190

31913191
and customAttrsReader ctxtH tag : ILAttributesStored =
@@ -3279,7 +3279,7 @@ and seekReadConstant (ctxt: ILMetadataReader) idx =
32793279
| _ -> ILFieldInit.Null
32803280

32813281
and seekReadImplMap (ctxt: ILMetadataReader) nm midx =
3282-
lazy
3282+
InterruptibleLazy(fun _ ->
32833283
MethodBody.PInvoke(
32843284
lazy
32853285
let mdv = ctxt.mdfile.GetView()
@@ -3367,7 +3367,7 @@ and seekReadImplMap (ctxt: ILMetadataReader) nm midx =
33673367
| Some nm2 -> nm2)
33683368
Where = seekReadModuleRef ctxt mdv scopeIdx
33693369
}
3370-
)
3370+
))
33713371

33723372
and seekReadTopCode (ctxt: ILMetadataReader) pev mdv numTypars (sz: int) start =
33733373
let labelsOfRawOffsets = Dictionary<_, _>(sz / 2)
@@ -3661,7 +3661,7 @@ and seekReadTopCode (ctxt: ILMetadataReader) pev mdv numTypars (sz: int) start =
36613661
instrs, rawToLabel, lab2pc
36623662

36633663
and seekReadMethodRVA (pectxt: PEReader) (ctxt: ILMetadataReader) (nm, noinline, aggressiveinline, numTypars) rva =
3664-
lazy
3664+
InterruptibleLazy(fun _ ->
36653665
let pev = pectxt.pefile.GetView()
36663666
let baseRVA = pectxt.anyV2P ("method rva", rva)
36673667
// ": reading body of method "+nm+" at rva "+string rva+", phys "+string baseRVA
@@ -3678,7 +3678,7 @@ and seekReadMethodRVA (pectxt: PEReader) (ctxt: ILMetadataReader) (nm, noinline,
36783678
else
36793679

36803680
MethodBody.IL(
3681-
lazy
3681+
InterruptibleLazy(fun _ ->
36823682
let pev = pectxt.pefile.GetView()
36833683
let mdv = ctxt.mdfile.GetView()
36843684

@@ -3852,8 +3852,8 @@ and seekReadMethodRVA (pectxt: PEReader) (ctxt: ILMetadataReader) (nm, noinline,
38523852
Code = code
38533853
DebugRange = None
38543854
DebugImports = None
3855-
}
3856-
)
3855+
})
3856+
))
38573857

38583858
and int32AsILVariantType (ctxt: ILMetadataReader) (n: int32) =
38593859
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)