-
Notifications
You must be signed in to change notification settings - Fork 850
Expand file tree
/
Copy pathTaskGeneratedCode.fs
More file actions
1475 lines (1268 loc) · 62.3 KB
/
TaskGeneratedCode.fs
File metadata and controls
1475 lines (1268 loc) · 62.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.
namespace FSharp.Compiler.UnitTests.CodeGen.EmittedIL
open FSharp.Test
open Xunit
open System
open System.Threading.Tasks
// These tests are sensitive to the fact that FSharp.Core is compiled release, not debug
// The code generated changes slightly in debug mode
#if !DEBUG
// Check the exact code produced for tasks
module TaskGeneratedCode =
// This tests the exact optimized code generated for the MoveNext for a trivial task - we expect 'MoveNext' to be there
// because state machine compilation succeeds
//
// The code is not perfect - because the MoveNext is generated late - but the JIT does a good job on it.
//
// The try/catch for the task still exists even though there is no chance of an exception
//
// The crucial code for "return 1" is really just
// IL_000e: ldc.i4.1
// IL_000f: stfld int32 Test/testTask@4::Result
[<Fact>]
let ``check MoveNext of simple task debug``() =
CompilerAssert.CompileLibraryAndVerifyILWithOptions(
[| "/optimize-"; "/debug:portable";"--realsig+"; "/tailcalls-" |],
"""
module Test
let testTask() = task { return 1 }
""",
(fun verifier -> verifier.VerifyIL [
"""
.method public strict virtual instance void MoveNext() cil managed
{
.override [runtime]System.Runtime.CompilerServices.IAsyncStateMachine::MoveNext
.maxstack 4
.locals init (int32 V_0,
class [runtime]System.Exception V_1,
bool V_2,
int32 V_3,
class [runtime]System.Exception V_4,
class [runtime]System.Exception V_5)
IL_0000: ldarg.0
IL_0001: ldfld int32 Test/testTask@4::ResumptionPoint
IL_0006: stloc.0
.try
{
IL_0007: ldc.i4.1
IL_0008: stloc.3
IL_0009: ldarg.0
IL_000a: ldflda valuetype [FSharp.Core]Microsoft.FSharp.Control.TaskStateMachineData`1<int32> Test/testTask@4::Data
IL_000f: ldloc.3
IL_0010: stfld !0 valuetype [FSharp.Core]Microsoft.FSharp.Control.TaskStateMachineData`1<int32>::Result
IL_0015: ldc.i4.1
IL_0016: stloc.2
IL_0017: ldloc.2
IL_0018: brfalse.s IL_0037
IL_001a: ldarg.0
IL_001b: ldflda valuetype [FSharp.Core]Microsoft.FSharp.Control.TaskStateMachineData`1<int32> Test/testTask@4::Data
IL_0020: ldflda valuetype [runtime]System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1<!0> valuetype [FSharp.Core]Microsoft.FSharp.Control.TaskStateMachineData`1<int32>::MethodBuilder
IL_0025: ldarg.0
IL_0026: ldflda valuetype [FSharp.Core]Microsoft.FSharp.Control.TaskStateMachineData`1<int32> Test/testTask@4::Data
IL_002b: ldfld !0 valuetype [FSharp.Core]Microsoft.FSharp.Control.TaskStateMachineData`1<int32>::Result
IL_0030: call instance void valuetype [netstandard]System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1<int32>::SetResult(!0)
IL_0035: leave.s IL_0045
IL_0037: leave.s IL_0045
}
catch [runtime]System.Object
{
IL_0039: castclass [runtime]System.Exception
IL_003e: stloc.s V_4
IL_0040: ldloc.s V_4
IL_0042: stloc.1
IL_0043: leave.s IL_0045
}
IL_0045: ldloc.1
IL_0046: stloc.s V_5
IL_0048: ldloc.s V_5
IL_004a: brtrue.s IL_004d
IL_004c: ret
IL_004d: ldarg.0
IL_004e: ldflda valuetype [FSharp.Core]Microsoft.FSharp.Control.TaskStateMachineData`1<int32> Test/testTask@4::Data
IL_0053: ldflda valuetype [runtime]System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1<!0> valuetype [FSharp.Core]Microsoft.FSharp.Control.TaskStateMachineData`1<int32>::MethodBuilder
IL_0058: ldloc.s V_5
IL_005a: call instance void valuetype [netstandard]System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1<int32>::SetException(class [netstandard]System.Exception)
IL_005f: ret
}
"""
]))
// This tests the exact optimized code generated for the MoveNext for a trivial task - we expect 'MoveNext' to be there
// because state machine compilation succeeds
//
// The code is not perfect - because the MoveNext is generated late - but the JIT does a good job on it.
//
// The try/catch for the task still exists even though there is no chance of an exception
//
// The crucial code for "return 1" is really just
// IL_000e: ldc.i4.1
// IL_000f: stfld int32 Test/testTask@4::Result
[<Fact>]
let ``check MoveNext of simple task optimized``() =
CompilerAssert.CompileLibraryAndVerifyILWithOptions(
[| "/optimize+"; "/debug:portable";"--realsig+"; "/tailcalls+" |],
"""
module Test
let testTask() = task { return 1 }
""",
(fun verifier -> verifier.VerifyIL [
"""
.method public strict virtual instance void
MoveNext() cil managed
{
.override [runtime]System.Runtime.CompilerServices.IAsyncStateMachine::MoveNext
.maxstack 4
.locals init (int32 V_0,
class [runtime]System.Exception V_1,
bool V_2,
class [runtime]System.Exception V_3)
IL_0000: ldarg.0
IL_0001: ldfld int32 Test/testTask@4::ResumptionPoint
IL_0006: stloc.0
.try
{
IL_0007: ldarg.0
IL_0008: ldflda valuetype [FSharp.Core]Microsoft.FSharp.Control.TaskStateMachineData`1<int32> Test/testTask@4::Data
IL_000d: ldc.i4.1
IL_000e: stfld !0 valuetype [FSharp.Core]Microsoft.FSharp.Control.TaskStateMachineData`1<int32>::Result
IL_0013: ldc.i4.1
IL_0014: stloc.2
IL_0015: ldloc.2
IL_0016: brfalse.s IL_0035
IL_0018: ldarg.0
IL_0019: ldflda valuetype [FSharp.Core]Microsoft.FSharp.Control.TaskStateMachineData`1<int32> Test/testTask@4::Data
IL_001e: ldflda valuetype [runtime]System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1<!0> valuetype [FSharp.Core]Microsoft.FSharp.Control.TaskStateMachineData`1<int32>::MethodBuilder
IL_0023: ldarg.0
IL_0024: ldflda valuetype [FSharp.Core]Microsoft.FSharp.Control.TaskStateMachineData`1<int32> Test/testTask@4::Data
IL_0029: ldfld !0 valuetype [FSharp.Core]Microsoft.FSharp.Control.TaskStateMachineData`1<int32>::Result
IL_002e: call instance void valuetype [netstandard]System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1<int32>::SetResult(!0)
IL_0033: leave.s IL_0041
IL_0035: leave.s IL_0041
}
catch [runtime]System.Object
{
IL_0037: castclass [runtime]System.Exception
IL_003c: stloc.3
IL_003d: ldloc.3
IL_003e: stloc.1
IL_003f: leave.s IL_0041
}
IL_0041: ldloc.1
IL_0042: stloc.3
IL_0043: ldloc.3
IL_0044: brtrue.s IL_0047
IL_0046: ret
IL_0047: ldarg.0
IL_0048: ldflda valuetype [FSharp.Core]Microsoft.FSharp.Control.TaskStateMachineData`1<int32> Test/testTask@4::Data
IL_004d: ldflda valuetype [runtime]System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1<!0> valuetype [FSharp.Core]Microsoft.FSharp.Control.TaskStateMachineData`1<int32>::MethodBuilder
IL_0052: ldloc.3
IL_0053: call instance void valuetype [netstandard]System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1<int32>::SetException(class [netstandard]System.Exception)
IL_0058: ret
}
"""
]))
[<Fact>]
let ``check MoveNext of simple binding task debug``() =
CompilerAssert.CompileLibraryAndVerifyILWithOptions(
[| "/debug:portable";"--realsig+"; "/optimize-"; "/tailcalls-" |],
"""
module Test
open System.Threading.Tasks
let testTask(t: Task<int>) = task { let! res = t in return res+1 }
""",
(fun verifier -> verifier.VerifyIL [
"""
.method public strict virtual instance void
MoveNext() cil managed
{
.override [runtime]System.Runtime.CompilerServices.IAsyncStateMachine::MoveNext
.maxstack 5
.locals init (int32 V_0,
class [runtime]System.Exception V_1,
bool V_2,
class [runtime]System.Threading.Tasks.Task`1<int32> V_3,
bool V_4,
bool V_5,
int32 V_6,
int32 V_7,
int32 V_8,
int32 V_9,
valuetype [runtime]System.Runtime.CompilerServices.TaskAwaiter`1<int32> V_10,
class [runtime]System.Exception V_11,
class [runtime]System.Exception V_12)
IL_0000: ldarg.0
IL_0001: ldfld int32 Test/testTask@4::ResumptionPoint
IL_0006: stloc.0
IL_0007: ldloc.0
IL_0008: ldc.i4.1
IL_0009: sub
IL_000a: switch (
IL_0015)
IL_0013: br.s IL_0018
IL_0015: nop
IL_0016: br.s IL_001b
IL_0018: nop
IL_0019: br.s IL_001b
.try
{
IL_001b: ldloc.0
IL_001c: ldc.i4.1
IL_001d: sub
IL_001e: switch (
IL_0029)
IL_0027: br.s IL_002c
IL_0029: nop
IL_002a: br.s IL_0057
IL_002c: nop
IL_002d: br.s IL_002f
IL_002f: ldarg.0
IL_0030: ldfld class [runtime]System.Threading.Tasks.Task`1<int32> Test/testTask@4::t
IL_0035: stloc.3
IL_0036: ldarg.0
IL_0037: ldloc.3
IL_0038: callvirt instance valuetype [netstandard]System.Runtime.CompilerServices.TaskAwaiter`1<!0> class [netstandard]System.Threading.Tasks.Task`1<int32>::GetAwaiter()
IL_003d: stfld valuetype [runtime]System.Runtime.CompilerServices.TaskAwaiter`1<int32> Test/testTask@4::awaiter
IL_0042: ldc.i4.1
IL_0043: stloc.s V_4
IL_0045: ldarg.0
IL_0046: ldflda valuetype [runtime]System.Runtime.CompilerServices.TaskAwaiter`1<int32> Test/testTask@4::awaiter
IL_004b: call instance bool valuetype [netstandard]System.Runtime.CompilerServices.TaskAwaiter`1<int32>::get_IsCompleted()
IL_0050: brfalse.s IL_0054
IL_0052: br.s IL_006d
IL_0054: ldc.i4.0
IL_0055: brfalse.s IL_005b
IL_0057: ldc.i4.1
IL_0058: nop
IL_0059: br.s IL_0064
IL_005b: ldarg.0
IL_005c: ldc.i4.1
IL_005d: stfld int32 Test/testTask@4::ResumptionPoint
IL_0062: ldc.i4.0
IL_0063: nop
IL_0064: stloc.s V_5
IL_0066: ldloc.s V_5
IL_0068: stloc.s V_4
IL_006a: nop
IL_006b: br.s IL_006e
IL_006d: nop
IL_006e: ldloc.s V_4
IL_0070: brfalse.s IL_009e
IL_0072: ldarg.0
IL_0073: ldflda valuetype [runtime]System.Runtime.CompilerServices.TaskAwaiter`1<int32> Test/testTask@4::awaiter
IL_0078: call instance !0 valuetype [netstandard]System.Runtime.CompilerServices.TaskAwaiter`1<int32>::GetResult()
IL_007d: stloc.s V_6
IL_007f: ldloc.s V_6
IL_0081: stloc.s V_7
IL_0083: ldloc.s V_7
IL_0085: stloc.s V_8
IL_0087: ldloc.s V_8
IL_0089: ldc.i4.1
IL_008a: add
IL_008b: stloc.s V_9
IL_008d: ldarg.0
IL_008e: ldflda valuetype [FSharp.Core]Microsoft.FSharp.Control.TaskStateMachineData`1<int32> Test/testTask@4::Data
IL_0093: ldloc.s V_9
IL_0095: stfld !0 valuetype [FSharp.Core]Microsoft.FSharp.Control.TaskStateMachineData`1<int32>::Result
IL_009a: ldc.i4.1
IL_009b: nop
IL_009c: br.s IL_00b7
IL_009e: ldarg.0
IL_009f: ldflda valuetype [FSharp.Core]Microsoft.FSharp.Control.TaskStateMachineData`1<int32> Test/testTask@4::Data
IL_00a4: ldflda valuetype [runtime]System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1<!0> valuetype [FSharp.Core]Microsoft.FSharp.Control.TaskStateMachineData`1<int32>::MethodBuilder
IL_00a9: ldarg.0
IL_00aa: ldflda valuetype [runtime]System.Runtime.CompilerServices.TaskAwaiter`1<int32> Test/testTask@4::awaiter
IL_00af: ldarg.0
IL_00b0: call instance void valuetype [netstandard]System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1<int32>::AwaitUnsafeOnCompleted<valuetype [runtime]System.Runtime.CompilerServices.TaskAwaiter`1<int32>,valuetype Test/testTask@4>(!!0&,
!!1&)
IL_00b5: ldc.i4.0
IL_00b6: nop
IL_00b7: brfalse.s IL_00c5
IL_00b9: ldarg.0
IL_00ba: ldloc.s V_10
IL_00bc: stfld valuetype [runtime]System.Runtime.CompilerServices.TaskAwaiter`1<int32> Test/testTask@4::awaiter
IL_00c1: ldc.i4.1
IL_00c2: nop
IL_00c3: br.s IL_00c7
IL_00c5: ldc.i4.0
IL_00c6: nop
IL_00c7: stloc.2
IL_00c8: ldloc.2
IL_00c9: brfalse.s IL_00e8
IL_00cb: ldarg.0
IL_00cc: ldflda valuetype [FSharp.Core]Microsoft.FSharp.Control.TaskStateMachineData`1<int32> Test/testTask@4::Data
IL_00d1: ldflda valuetype [runtime]System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1<!0> valuetype [FSharp.Core]Microsoft.FSharp.Control.TaskStateMachineData`1<int32>::MethodBuilder
IL_00d6: ldarg.0
IL_00d7: ldflda valuetype [FSharp.Core]Microsoft.FSharp.Control.TaskStateMachineData`1<int32> Test/testTask@4::Data
IL_00dc: ldfld !0 valuetype [FSharp.Core]Microsoft.FSharp.Control.TaskStateMachineData`1<int32>::Result
IL_00e1: call instance void valuetype [netstandard]System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1<int32>::SetResult(!0)
IL_00e6: leave.s IL_00f6
IL_00e8: leave.s IL_00f6
}
catch [runtime]System.Object
{
IL_00ea: castclass [runtime]System.Exception
IL_00ef: stloc.s V_11
IL_00f1: ldloc.s V_11
IL_00f3: stloc.1
IL_00f4: leave.s IL_00f6
}
IL_00f6: ldloc.1
IL_00f7: stloc.s V_12
IL_00f9: ldloc.s V_12
IL_00fb: brtrue.s IL_00fe
IL_00fd: ret
IL_00fe: ldarg.0
IL_00ff: ldflda valuetype [FSharp.Core]Microsoft.FSharp.Control.TaskStateMachineData`1<int32> Test/testTask@4::Data
IL_0104: ldflda valuetype [runtime]System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1<!0> valuetype [FSharp.Core]Microsoft.FSharp.Control.TaskStateMachineData`1<int32>::MethodBuilder
IL_0109: ldloc.s V_12
IL_010b: call instance void valuetype [netstandard]System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1<int32>::SetException(class [netstandard]System.Exception)
IL_0110: ret
}
"""
]))
module TaskTryFinallyGeneration =
[<Fact>]
let ``check MoveNext of task try/finally optimized``() =
CompilerAssert.CompileLibraryAndVerifyILWithOptions(
[| "/optimize+"; "/debug:portable";"--realsig+"; "/tailcalls+" |],
"""
module Test
let testTask() = task { try 1+1 finally System.Console.WriteLine("finally") }
""",
(fun verifier -> verifier.VerifyIL [
"""
.method public strict virtual instance void
MoveNext() cil managed
{
.override [runtime]System.Runtime.CompilerServices.IAsyncStateMachine::MoveNext
.maxstack 4
.locals init (int32 V_0,
class [runtime]System.Exception V_1,
bool V_2,
bool V_3,
bool V_4,
class [runtime]System.Exception V_5)
IL_0000: ldarg.0
IL_0001: ldfld int32 Test/testTask@4::ResumptionPoint
IL_0006: stloc.0
.try
{
IL_0007: ldc.i4.0
IL_0008: stloc.3
.try
{
IL_0009: ldc.i4.1
IL_000a: stloc.s V_4
IL_000c: ldloc.s V_4
IL_000e: stloc.3
IL_000f: leave.s IL_0030
}
catch [runtime]System.Object
{
IL_0011: castclass [runtime]System.Exception
IL_0016: stloc.s V_5
IL_0018: ldstr "finally"
IL_001d: call void [runtime]System.Console::WriteLine(string)
IL_0022: ldc.i4.1
IL_0023: stloc.s V_4
IL_0025: rethrow
IL_0027: ldnull
IL_0028: unbox.any [FSharp.Core]Microsoft.FSharp.Core.Unit
IL_002d: pop
IL_002e: leave.s IL_0030
}
IL_0030: ldloc.3
IL_0031: brfalse.s IL_0043
IL_0033: ldstr "finally"
IL_0038: call void [runtime]System.Console::WriteLine(string)
IL_003d: ldc.i4.1
IL_003e: stloc.s V_4
IL_0040: nop
IL_0041: br.s IL_0044
IL_0043: nop
IL_0044: ldloc.3
IL_0045: stloc.2
IL_0046: ldloc.2
IL_0047: brfalse.s IL_0066
IL_0049: ldarg.0
IL_004a: ldflda valuetype [FSharp.Core]Microsoft.FSharp.Control.TaskStateMachineData`1<class [FSharp.Core]Microsoft.FSharp.Core.Unit> Test/testTask@4::Data
IL_004f: ldflda valuetype [runtime]System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1<!0> valuetype [FSharp.Core]Microsoft.FSharp.Control.TaskStateMachineData`1<class [FSharp.Core]Microsoft.FSharp.Core.Unit>::MethodBuilder
IL_0054: ldarg.0
IL_0055: ldflda valuetype [FSharp.Core]Microsoft.FSharp.Control.TaskStateMachineData`1<class [FSharp.Core]Microsoft.FSharp.Core.Unit> Test/testTask@4::Data
IL_005a: ldfld !0 valuetype [FSharp.Core]Microsoft.FSharp.Control.TaskStateMachineData`1<class [FSharp.Core]Microsoft.FSharp.Core.Unit>::Result
IL_005f: call instance void valuetype [netstandard]System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1<class [FSharp.Core]Microsoft.FSharp.Core.Unit>::SetResult(!0)
IL_0064: leave.s IL_0074
IL_0066: leave.s IL_0074
}
catch [runtime]System.Object
{
IL_0068: castclass [runtime]System.Exception
IL_006d: stloc.s V_5
IL_006f: ldloc.s V_5
IL_0071: stloc.1
IL_0072: leave.s IL_0074
}
IL_0074: ldloc.1
IL_0075: stloc.s V_5
IL_0077: ldloc.s V_5
IL_0079: brtrue.s IL_007c
IL_007b: ret
IL_007c: ldarg.0
IL_007d: ldflda valuetype [FSharp.Core]Microsoft.FSharp.Control.TaskStateMachineData`1<class [FSharp.Core]Microsoft.FSharp.Core.Unit> Test/testTask@4::Data
IL_0082: ldflda valuetype [runtime]System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1<!0> valuetype [FSharp.Core]Microsoft.FSharp.Control.TaskStateMachineData`1<class [FSharp.Core]Microsoft.FSharp.Core.Unit>::MethodBuilder
IL_0087: ldloc.s V_5
IL_0089: call instance void valuetype [netstandard]System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1<class [FSharp.Core]Microsoft.FSharp.Core.Unit>::SetException(class [netstandard]System.Exception)
IL_008e: ret
}
"""
]))
[<Fact>]
let ``check MoveNext of task try/finally debug``() =
CompilerAssert.CompileLibraryAndVerifyILWithOptions(
[| "/optimize-"; "/debug:portable";"--realsig+"; "/tailcalls-" |],
"""
module Test
let testTask() = task { try 1+1 finally System.Console.WriteLine("finally") }
""",
(fun verifier -> verifier.VerifyIL [
"""
.method public strict virtual instance void
MoveNext() cil managed
{
.override [runtime]System.Runtime.CompilerServices.IAsyncStateMachine::MoveNext
.maxstack 4
.locals init (int32 V_0,
class [runtime]System.Exception V_1,
bool V_2,
bool V_3,
bool V_4,
class [runtime]System.Exception V_5,
bool V_6,
bool V_7,
class [runtime]System.Exception V_8,
class [runtime]System.Exception V_9)
IL_0000: ldarg.0
IL_0001: ldfld int32 Test/testTask@4::ResumptionPoint
IL_0006: stloc.0
.try
{
IL_0007: ldc.i4.0
IL_0008: stloc.3
.try
{
IL_0009: nop
IL_000a: ldc.i4.1
IL_000b: stloc.s V_4
IL_000d: ldloc.s V_4
IL_000f: stloc.3
IL_0010: leave.s IL_0032
}
catch [runtime]System.Object
{
IL_0012: castclass [runtime]System.Exception
IL_0017: stloc.s V_5
IL_0019: nop
IL_001a: ldstr "finally"
IL_001f: call void [runtime]System.Console::WriteLine(string)
IL_0024: ldc.i4.1
IL_0025: stloc.s V_6
IL_0027: rethrow
IL_0029: ldnull
IL_002a: unbox.any [FSharp.Core]Microsoft.FSharp.Core.Unit
IL_002f: pop
IL_0030: leave.s IL_0032
}
IL_0032: ldloc.3
IL_0033: brfalse.s IL_0046
IL_0035: nop
IL_0036: ldstr "finally"
IL_003b: call void [runtime]System.Console::WriteLine(string)
IL_0040: ldc.i4.1
IL_0041: stloc.s V_7
IL_0043: nop
IL_0044: br.s IL_0047
IL_0046: nop
IL_0047: ldloc.3
IL_0048: stloc.2
IL_0049: ldloc.2
IL_004a: brfalse.s IL_0069
IL_004c: ldarg.0
IL_004d: ldflda valuetype [FSharp.Core]Microsoft.FSharp.Control.TaskStateMachineData`1<class [FSharp.Core]Microsoft.FSharp.Core.Unit> Test/testTask@4::Data
IL_0052: ldflda valuetype [runtime]System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1<!0> valuetype [FSharp.Core]Microsoft.FSharp.Control.TaskStateMachineData`1<class [FSharp.Core]Microsoft.FSharp.Core.Unit>::MethodBuilder
IL_0057: ldarg.0
IL_0058: ldflda valuetype [FSharp.Core]Microsoft.FSharp.Control.TaskStateMachineData`1<class [FSharp.Core]Microsoft.FSharp.Core.Unit> Test/testTask@4::Data
IL_005d: ldfld !0 valuetype [FSharp.Core]Microsoft.FSharp.Control.TaskStateMachineData`1<class [FSharp.Core]Microsoft.FSharp.Core.Unit>::Result
IL_0062: call instance void valuetype [netstandard]System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1<class [FSharp.Core]Microsoft.FSharp.Core.Unit>::SetResult(!0)
IL_0067: leave.s IL_0077
IL_0069: leave.s IL_0077
}
catch [runtime]System.Object
{
IL_006b: castclass [runtime]System.Exception
IL_0070: stloc.s V_8
IL_0072: ldloc.s V_8
IL_0074: stloc.1
IL_0075: leave.s IL_0077
}
IL_0077: ldloc.1
IL_0078: stloc.s V_9
IL_007a: ldloc.s V_9
IL_007c: brtrue.s IL_007f
IL_007e: ret
IL_007f: ldarg.0
IL_0080: ldflda valuetype [FSharp.Core]Microsoft.FSharp.Control.TaskStateMachineData`1<class [FSharp.Core]Microsoft.FSharp.Core.Unit> Test/testTask@4::Data
IL_0085: ldflda valuetype [runtime]System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1<!0> valuetype [FSharp.Core]Microsoft.FSharp.Control.TaskStateMachineData`1<class [FSharp.Core]Microsoft.FSharp.Core.Unit>::MethodBuilder
IL_008a: ldloc.s V_9
IL_008c: call instance void valuetype [netstandard]System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1<class [FSharp.Core]Microsoft.FSharp.Core.Unit>::SetException(class [netstandard]System.Exception)
IL_0091: ret
}
"""
]))
module TaskTryWithGeneration =
[<Fact>]
let ``check MoveNext of task try/with optimized``() =
CompilerAssert.CompileLibraryAndVerifyILWithOptions(
[| "/optimize+"; "/debug:portable";"--realsig+"; "/tailcalls+" |],
"""
module Test
let testTask() = task { try 1 with e -> System.Console.WriteLine("finally"); 2 }
""",
(fun verifier -> verifier.VerifyIL [
"""
.method public strict virtual instance void
MoveNext() cil managed
{
.override [runtime]System.Runtime.CompilerServices.IAsyncStateMachine::MoveNext
.maxstack 4
.locals init (int32 V_0,
class [runtime]System.Exception V_1,
bool V_2,
bool V_3,
bool V_4,
class [runtime]System.Exception V_5,
bool V_6,
class [runtime]System.Exception V_7)
IL_0000: ldarg.0
IL_0001: ldfld int32 Test/testTask@4::ResumptionPoint
IL_0006: stloc.0
.try
{
IL_0007: ldc.i4.0
IL_0008: stloc.3
IL_0009: ldc.i4.0
IL_000a: stloc.s V_4
IL_000c: ldnull
IL_000d: stloc.s V_5
.try
{
IL_000f: ldc.i4.1
IL_0010: stloc.s V_6
IL_0012: ldloc.s V_6
IL_0014: stloc.3
IL_0015: leave.s IL_0027
}
catch [runtime]System.Object
{
IL_0017: castclass [runtime]System.Exception
IL_001c: stloc.s V_7
IL_001e: ldc.i4.1
IL_001f: stloc.s V_4
IL_0021: ldloc.s V_7
IL_0023: stloc.s V_5
IL_0025: leave.s IL_0027
}
IL_0027: ldloc.s V_4
IL_0029: brfalse.s IL_003e
IL_002b: ldloc.s V_5
IL_002d: stloc.s V_7
IL_002f: nop
IL_0030: ldstr "finally"
IL_0035: call void [runtime]System.Console::WriteLine(string)
IL_003a: ldc.i4.1
IL_003b: nop
IL_003c: br.s IL_0040
IL_003e: ldloc.3
IL_003f: nop
IL_0040: stloc.2
IL_0041: ldloc.2
IL_0042: brfalse.s IL_0061
IL_0044: ldarg.0
IL_0045: ldflda valuetype [FSharp.Core]Microsoft.FSharp.Control.TaskStateMachineData`1<class [FSharp.Core]Microsoft.FSharp.Core.Unit> Test/testTask@4::Data
IL_004a: ldflda valuetype [runtime]System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1<!0> valuetype [FSharp.Core]Microsoft.FSharp.Control.TaskStateMachineData`1<class [FSharp.Core]Microsoft.FSharp.Core.Unit>::MethodBuilder
IL_004f: ldarg.0
IL_0050: ldflda valuetype [FSharp.Core]Microsoft.FSharp.Control.TaskStateMachineData`1<class [FSharp.Core]Microsoft.FSharp.Core.Unit> Test/testTask@4::Data
IL_0055: ldfld !0 valuetype [FSharp.Core]Microsoft.FSharp.Control.TaskStateMachineData`1<class [FSharp.Core]Microsoft.FSharp.Core.Unit>::Result
IL_005a: call instance void valuetype [netstandard]System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1<class [FSharp.Core]Microsoft.FSharp.Core.Unit>::SetResult(!0)
IL_005f: leave.s IL_006f
IL_0061: leave.s IL_006f
}
catch [runtime]System.Object
{
IL_0063: castclass [runtime]System.Exception
IL_0068: stloc.s V_5
IL_006a: ldloc.s V_5
IL_006c: stloc.1
IL_006d: leave.s IL_006f
}
IL_006f: ldloc.1
IL_0070: stloc.s V_5
IL_0072: ldloc.s V_5
IL_0074: brtrue.s IL_0077
IL_0076: ret
IL_0077: ldarg.0
IL_0078: ldflda valuetype [FSharp.Core]Microsoft.FSharp.Control.TaskStateMachineData`1<class [FSharp.Core]Microsoft.FSharp.Core.Unit> Test/testTask@4::Data
IL_007d: ldflda valuetype [runtime]System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1<!0> valuetype [FSharp.Core]Microsoft.FSharp.Control.TaskStateMachineData`1<class [FSharp.Core]Microsoft.FSharp.Core.Unit>::MethodBuilder
IL_0082: ldloc.s V_5
IL_0084: call instance void valuetype [netstandard]System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1<class [FSharp.Core]Microsoft.FSharp.Core.Unit>::SetException(class [netstandard]System.Exception)
IL_0089: ret
}
"""
]))
[<Fact>]
let ``check MoveNext of task try/with debug``() =
CompilerAssert.CompileLibraryAndVerifyILWithOptions(
[| "/optimize-"; "/debug:portable";"--realsig+"; "/tailcalls-" |],
"""
module Test
let testTask() = task { try 1 with e -> System.Console.WriteLine("with"); 2 }
""",
(fun verifier -> verifier.VerifyIL [
"""
.method public strict virtual instance void
MoveNext() cil managed
{
.override [runtime]System.Runtime.CompilerServices.IAsyncStateMachine::MoveNext
.maxstack 4
.locals init (int32 V_0,
class [runtime]System.Exception V_1,
bool V_2,
bool V_3,
bool V_4,
class [runtime]System.Exception V_5,
bool V_6,
class [runtime]System.Exception V_7,
class [runtime]System.Exception V_8,
class [runtime]System.Exception V_9,
class [runtime]System.Exception V_10,
class [runtime]System.Exception V_11)
IL_0000: ldarg.0
IL_0001: ldfld int32 Test/testTask@4::ResumptionPoint
IL_0006: stloc.0
.try
{
IL_0007: ldc.i4.0
IL_0008: stloc.3
IL_0009: ldc.i4.0
IL_000a: stloc.s V_4
IL_000c: ldnull
IL_000d: stloc.s V_5
.try
{
IL_000f: nop
IL_0010: ldc.i4.1
IL_0011: stloc.s V_6
IL_0013: ldloc.s V_6
IL_0015: stloc.3
IL_0016: leave.s IL_0028
}
catch [runtime]System.Object
{
IL_0018: castclass [runtime]System.Exception
IL_001d: stloc.s V_7
IL_001f: ldc.i4.1
IL_0020: stloc.s V_4
IL_0022: ldloc.s V_7
IL_0024: stloc.s V_5
IL_0026: leave.s IL_0028
}
IL_0028: ldloc.s V_4
IL_002a: brfalse.s IL_0042
IL_002c: ldloc.s V_5
IL_002e: stloc.s V_8
IL_0030: ldloc.s V_8
IL_0032: stloc.s V_9
IL_0034: ldstr "with"
IL_0039: call void [runtime]System.Console::WriteLine(string)
IL_003e: ldc.i4.1
IL_003f: nop
IL_0040: br.s IL_0044
IL_0042: ldloc.3
IL_0043: nop
IL_0044: stloc.2
IL_0045: ldloc.2
IL_0046: brfalse.s IL_0065
IL_0048: ldarg.0
IL_0049: ldflda valuetype [FSharp.Core]Microsoft.FSharp.Control.TaskStateMachineData`1<class [FSharp.Core]Microsoft.FSharp.Core.Unit> Test/testTask@4::Data
IL_004e: ldflda valuetype [runtime]System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1<!0> valuetype [FSharp.Core]Microsoft.FSharp.Control.TaskStateMachineData`1<class [FSharp.Core]Microsoft.FSharp.Core.Unit>::MethodBuilder
IL_0053: ldarg.0
IL_0054: ldflda valuetype [FSharp.Core]Microsoft.FSharp.Control.TaskStateMachineData`1<class [FSharp.Core]Microsoft.FSharp.Core.Unit> Test/testTask@4::Data
IL_0059: ldfld !0 valuetype [FSharp.Core]Microsoft.FSharp.Control.TaskStateMachineData`1<class [FSharp.Core]Microsoft.FSharp.Core.Unit>::Result
IL_005e: call instance void valuetype [netstandard]System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1<class [FSharp.Core]Microsoft.FSharp.Core.Unit>::SetResult(!0)
IL_0063: leave.s IL_0073
IL_0065: leave.s IL_0073
}
catch [runtime]System.Object
{
IL_0067: castclass [runtime]System.Exception
IL_006c: stloc.s V_10
IL_006e: ldloc.s V_10
IL_0070: stloc.1
IL_0071: leave.s IL_0073
}
IL_0073: ldloc.1
IL_0074: stloc.s V_11
IL_0076: ldloc.s V_11
IL_0078: brtrue.s IL_007b
IL_007a: ret
IL_007b: ldarg.0
IL_007c: ldflda valuetype [FSharp.Core]Microsoft.FSharp.Control.TaskStateMachineData`1<class [FSharp.Core]Microsoft.FSharp.Core.Unit> Test/testTask@4::Data
IL_0081: ldflda valuetype [runtime]System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1<!0> valuetype [FSharp.Core]Microsoft.FSharp.Control.TaskStateMachineData`1<class [FSharp.Core]Microsoft.FSharp.Core.Unit>::MethodBuilder
IL_0086: ldloc.s V_11
IL_0088: call instance void valuetype [netstandard]System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1<class [FSharp.Core]Microsoft.FSharp.Core.Unit>::SetException(class [netstandard]System.Exception)
IL_008d: ret
}
"""
]))
module TaskWhileLoopGeneration =
[<Fact>]
let ``check MoveNext of task while loop optimized``() =
CompilerAssert.CompileLibraryAndVerifyILWithOptions(
[| "/optimize+"; "/debug:portable";"--realsig+"; "/tailcalls+" |],
"""
module Test
let mutable x = 1
let testTask() = task { while x > 4 do System.Console.WriteLine("loop") }
""",
(fun verifier -> verifier.VerifyIL [
"""
.method public strict virtual instance void
MoveNext() cil managed
{
.override [runtime]System.Runtime.CompilerServices.IAsyncStateMachine::MoveNext
.maxstack 4
.locals init (int32 V_0,
class [runtime]System.Exception V_1,
bool V_2,
bool V_3,
bool V_4,
class [runtime]System.Exception V_5)
IL_0000: ldarg.0
IL_0001: ldfld int32 Test/testTask@5::ResumptionPoint
IL_0006: stloc.0
.try
{
IL_0007: ldc.i4.1
IL_0008: stloc.3
IL_0009: br.s IL_001d
IL_000b: ldstr "loop"
IL_0010: call void [runtime]System.Console::WriteLine(string)
IL_0015: ldc.i4.1
IL_0016: stloc.s V_4
IL_0018: ldloc.s V_4
IL_001a: stloc.3
IL_001b: ldc.i4.0
IL_001c: stloc.0
IL_001d: ldloc.3
IL_001e: brfalse.s IL_002b
IL_0020: call int32 Test::get_x()
IL_0025: ldc.i4.4
IL_0026: cgt
IL_0028: nop
IL_0029: br.s IL_002d
IL_002b: ldc.i4.0
IL_002c: nop
IL_002d: brtrue.s IL_000b
IL_002f: ldloc.3
IL_0030: stloc.2
IL_0031: ldloc.2
IL_0032: brfalse.s IL_0051
IL_0034: ldarg.0
IL_0035: ldflda valuetype [FSharp.Core]Microsoft.FSharp.Control.TaskStateMachineData`1<class [FSharp.Core]Microsoft.FSharp.Core.Unit> Test/testTask@5::Data
IL_003a: ldflda valuetype [runtime]System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1<!0> valuetype [FSharp.Core]Microsoft.FSharp.Control.TaskStateMachineData`1<class [FSharp.Core]Microsoft.FSharp.Core.Unit>::MethodBuilder
IL_003f: ldarg.0
IL_0040: ldflda valuetype [FSharp.Core]Microsoft.FSharp.Control.TaskStateMachineData`1<class [FSharp.Core]Microsoft.FSharp.Core.Unit> Test/testTask@5::Data
IL_0045: ldfld !0 valuetype [FSharp.Core]Microsoft.FSharp.Control.TaskStateMachineData`1<class [FSharp.Core]Microsoft.FSharp.Core.Unit>::Result
IL_004a: call instance void valuetype [netstandard]System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1<class [FSharp.Core]Microsoft.FSharp.Core.Unit>::SetResult(!0)
IL_004f: leave.s IL_005f
IL_0051: leave.s IL_005f
}
catch [runtime]System.Object
{
IL_0053: castclass [runtime]System.Exception
IL_0058: stloc.s V_5
IL_005a: ldloc.s V_5
IL_005c: stloc.1
IL_005d: leave.s IL_005f
}
IL_005f: ldloc.1
IL_0060: stloc.s V_5
IL_0062: ldloc.s V_5
IL_0064: brtrue.s IL_0067
IL_0066: ret
IL_0067: ldarg.0
IL_0068: ldflda valuetype [FSharp.Core]Microsoft.FSharp.Control.TaskStateMachineData`1<class [FSharp.Core]Microsoft.FSharp.Core.Unit> Test/testTask@5::Data
IL_006d: ldflda valuetype [runtime]System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1<!0> valuetype [FSharp.Core]Microsoft.FSharp.Control.TaskStateMachineData`1<class [FSharp.Core]Microsoft.FSharp.Core.Unit>::MethodBuilder
IL_0072: ldloc.s V_5
IL_0074: call instance void valuetype [netstandard]System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1<class [FSharp.Core]Microsoft.FSharp.Core.Unit>::SetException(class [netstandard]System.Exception)
IL_0079: ret
}
"""
]))
[<Fact>]
let ``check MoveNext of task while loop debug``() =
CompilerAssert.CompileLibraryAndVerifyILWithOptions(
[| "/optimize-"; "/debug:portable";"--realsig+"; "/tailcalls-" |],
"""
module Test
let mutable x = 1
let testTask() = task { while x > 4 do System.Console.WriteLine("loop") }
""",
(fun verifier -> verifier.VerifyIL [
"""
.method public strict virtual instance void
MoveNext() cil managed
{
.override [runtime]System.Runtime.CompilerServices.IAsyncStateMachine::MoveNext
.maxstack 4
.locals init (int32 V_0,
class [runtime]System.Exception V_1,
bool V_2,
bool V_3,
bool V_4,
class [runtime]System.Exception V_5,
class [runtime]System.Exception V_6)
IL_0000: ldarg.0
IL_0001: ldfld int32 Test/testTask@5::ResumptionPoint
IL_0006: stloc.0
.try
{
IL_0007: ldc.i4.1
IL_0008: stloc.3
IL_0009: br.s IL_001d
IL_000b: ldstr "loop"
IL_0010: call void [runtime]System.Console::WriteLine(string)
IL_0015: ldc.i4.1
IL_0016: stloc.s V_4
IL_0018: ldloc.s V_4
IL_001a: stloc.3
IL_001b: ldc.i4.0
IL_001c: stloc.0
IL_001d: ldloc.3
IL_001e: brfalse.s IL_002b
IL_0020: call int32 Test::get_x()
IL_0025: ldc.i4.4
IL_0026: cgt
IL_0028: nop
IL_0029: br.s IL_002d
IL_002b: ldc.i4.0
IL_002c: nop
IL_002d: brtrue.s IL_000b
IL_002f: ldloc.3
IL_0030: stloc.2
IL_0031: ldloc.2
IL_0032: brfalse.s IL_0051
IL_0034: ldarg.0
IL_0035: ldflda valuetype [FSharp.Core]Microsoft.FSharp.Control.TaskStateMachineData`1<class [FSharp.Core]Microsoft.FSharp.Core.Unit> Test/testTask@5::Data
IL_003a: ldflda valuetype [runtime]System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1<!0> valuetype [FSharp.Core]Microsoft.FSharp.Control.TaskStateMachineData`1<class [FSharp.Core]Microsoft.FSharp.Core.Unit>::MethodBuilder
IL_003f: ldarg.0
IL_0040: ldflda valuetype [FSharp.Core]Microsoft.FSharp.Control.TaskStateMachineData`1<class [FSharp.Core]Microsoft.FSharp.Core.Unit> Test/testTask@5::Data
IL_0045: ldfld !0 valuetype [FSharp.Core]Microsoft.FSharp.Control.TaskStateMachineData`1<class [FSharp.Core]Microsoft.FSharp.Core.Unit>::Result
IL_004a: call instance void valuetype [netstandard]System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1<class [FSharp.Core]Microsoft.FSharp.Core.Unit>::SetResult(!0)
IL_004f: leave.s IL_005f
IL_0051: leave.s IL_005f