Computed collections: optimize simple mappings with preludes (#17067) · dotnet/fsharp@fc42ec9 · GitHub
Skip to content

Commit fc42ec9

Browse files
Computed collections: optimize simple mappings with preludes (#17067)
* Gather up any prelude & reapply after lowering * E.g., ```fsharp [let y = f () in for … in … -> …] ``` and ```fsharp [f (); g (); for … in … -> …] ``` are transformed into something like ```fsharp let y = f () in [for … in … -> …] ``` and ```fsharp f (); g (); [for … in … -> …] ``` so that the existing pattern recognition of simple mappings and integral ranges may succeed.
1 parent 46aa5a9 commit fc42ec9

19 files changed

Lines changed: 5032 additions & 686 deletions

‎docs/release-notes/.FSharp.Compiler.Service/8.0.400.md‎

Lines changed: 1 addition & 0 deletions

‎src/Compiler/Optimize/LowerComputedCollections.fs‎

Lines changed: 211 additions & 54 deletions
Large diffs are not rendered by default.

‎tests/FSharp.Compiler.ComponentTests/EmittedIL/ComputedCollections/ForNInRangeArrays.fs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,8 @@ let f25 f g h = [|for n in f ()..g ()..h () -> n|]
3030
let f26 start step finish = [|for n in start..step..finish -> n, float n|]
3131
let f27 start step finish = [|for n in start..step..finish -> struct (n, float n)|]
3232
let f28 start step finish = [|for n in start..step..finish -> let x = n + 1 in n * n|]
33+
34+
let f29 f g = [|let y = f () in let z = g () in for x in 1..2..10 -> x + y + z|]
35+
let f30 f g = [|let y = f () in g (); for x in 1..2..10 -> x + y|]
36+
let f31 f g = [|f (); g (); for x in 1..2..10 -> x|]
37+
let f32 f g = [|f (); let y = g () in for x in 1..2..10 -> x + y|]

‎tests/FSharp.Compiler.ComponentTests/EmittedIL/ComputedCollections/ForNInRangeArrays.fs.il.bsl‎

Lines changed: 236 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2431,6 +2431,242 @@
24312431
IL_007a: ret
24322432
}
24332433

2434+
.method public static int32[] f29(class [FSharp.Core]Microsoft.FSharp.Core.FSharpFunc`2<class [FSharp.Core]Microsoft.FSharp.Core.Unit,int32> f,
2435+
class [FSharp.Core]Microsoft.FSharp.Core.FSharpFunc`2<class [FSharp.Core]Microsoft.FSharp.Core.Unit,int32> g) cil managed
2436+
{
2437+
.custom instance void [FSharp.Core]Microsoft.FSharp.Core.CompilationArgumentCountsAttribute::.ctor(int32[]) = ( 01 00 02 00 00 00 01 00 00 00 01 00 00 00 00 00 )
2438+
2439+
.maxstack 6
2440+
.locals init (int32 V_0,
2441+
int32 V_1,
2442+
int32[] V_2,
2443+
uint64 V_3,
2444+
int32 V_4,
2445+
int32 V_5)
2446+
IL_0000: nop
2447+
IL_0001: ldarg.0
2448+
IL_0002: ldnull
2449+
IL_0003: callvirt instance !1 class [FSharp.Core]Microsoft.FSharp.Core.FSharpFunc`2<class [FSharp.Core]Microsoft.FSharp.Core.Unit,int32>::Invoke(!0)
2450+
IL_0008: stloc.0
2451+
IL_0009: ldarg.1
2452+
IL_000a: ldnull
2453+
IL_000b: callvirt instance !1 class [FSharp.Core]Microsoft.FSharp.Core.FSharpFunc`2<class [FSharp.Core]Microsoft.FSharp.Core.Unit,int32>::Invoke(!0)
2454+
IL_0010: stloc.1
2455+
IL_0011: ldc.i4.5
2456+
IL_0012: conv.i8
2457+
IL_0013: conv.ovf.i.un
2458+
IL_0014: newarr [runtime]System.Int32
2459+
IL_0019: stloc.2
2460+
IL_001a: ldc.i4.0
2461+
IL_001b: conv.i8
2462+
IL_001c: stloc.3
2463+
IL_001d: ldc.i4.1
2464+
IL_001e: stloc.s V_4
2465+
IL_0020: br.s IL_003b
2466+
2467+
IL_0022: ldloc.2
2468+
IL_0023: ldloc.3
2469+
IL_0024: conv.i
2470+
IL_0025: ldloc.s V_4
2471+
IL_0027: stloc.s V_5
2472+
IL_0029: ldloc.s V_5
2473+
IL_002b: ldloc.0
2474+
IL_002c: add
2475+
IL_002d: ldloc.1
2476+
IL_002e: add
2477+
IL_002f: stelem.i4
2478+
IL_0030: ldloc.s V_4
2479+
IL_0032: ldc.i4.2
2480+
IL_0033: add
2481+
IL_0034: stloc.s V_4
2482+
IL_0036: ldloc.3
2483+
IL_0037: ldc.i4.1
2484+
IL_0038: conv.i8
2485+
IL_0039: add
2486+
IL_003a: stloc.3
2487+
IL_003b: ldloc.3
2488+
IL_003c: ldc.i4.5
2489+
IL_003d: conv.i8
2490+
IL_003e: blt.un.s IL_0022
2491+
2492+
IL_0040: ldloc.2
2493+
IL_0041: ret
2494+
}
2495+
2496+
.method public static int32[] f30(class [FSharp.Core]Microsoft.FSharp.Core.FSharpFunc`2<class [FSharp.Core]Microsoft.FSharp.Core.Unit,int32> f,
2497+
class [FSharp.Core]Microsoft.FSharp.Core.FSharpFunc`2<class [FSharp.Core]Microsoft.FSharp.Core.Unit,class [FSharp.Core]Microsoft.FSharp.Core.Unit> g) cil managed
2498+
{
2499+
.custom instance void [FSharp.Core]Microsoft.FSharp.Core.CompilationArgumentCountsAttribute::.ctor(int32[]) = ( 01 00 02 00 00 00 01 00 00 00 01 00 00 00 00 00 )
2500+
2501+
.maxstack 6
2502+
.locals init (int32 V_0,
2503+
int32[] V_1,
2504+
uint64 V_2,
2505+
int32 V_3,
2506+
int32 V_4)
2507+
IL_0000: nop
2508+
IL_0001: ldarg.0
2509+
IL_0002: ldnull
2510+
IL_0003: callvirt instance !1 class [FSharp.Core]Microsoft.FSharp.Core.FSharpFunc`2<class [FSharp.Core]Microsoft.FSharp.Core.Unit,int32>::Invoke(!0)
2511+
IL_0008: stloc.0
2512+
IL_0009: ldarg.1
2513+
IL_000a: ldnull
2514+
IL_000b: callvirt instance !1 class [FSharp.Core]Microsoft.FSharp.Core.FSharpFunc`2<class [FSharp.Core]Microsoft.FSharp.Core.Unit,class [FSharp.Core]Microsoft.FSharp.Core.Unit>::Invoke(!0)
2515+
IL_0010: pop
2516+
IL_0011: ldc.i4.5
2517+
IL_0012: conv.i8
2518+
IL_0013: conv.ovf.i.un
2519+
IL_0014: newarr [runtime]System.Int32
2520+
IL_0019: stloc.1
2521+
IL_001a: ldc.i4.0
2522+
IL_001b: conv.i8
2523+
IL_001c: stloc.2
2524+
IL_001d: ldc.i4.1
2525+
IL_001e: stloc.3
2526+
IL_001f: br.s IL_0035
2527+
2528+
IL_0021: ldloc.1
2529+
IL_0022: ldloc.2
2530+
IL_0023: conv.i
2531+
IL_0024: ldloc.3
2532+
IL_0025: stloc.s V_4
2533+
IL_0027: ldloc.s V_4
2534+
IL_0029: ldloc.0
2535+
IL_002a: add
2536+
IL_002b: stelem.i4
2537+
IL_002c: ldloc.3
2538+
IL_002d: ldc.i4.2
2539+
IL_002e: add
2540+
IL_002f: stloc.3
2541+
IL_0030: ldloc.2
2542+
IL_0031: ldc.i4.1
2543+
IL_0032: conv.i8
2544+
IL_0033: add
2545+
IL_0034: stloc.2
2546+
IL_0035: ldloc.2
2547+
IL_0036: ldc.i4.5
2548+
IL_0037: conv.i8
2549+
IL_0038: blt.un.s IL_0021
2550+
2551+
IL_003a: ldloc.1
2552+
IL_003b: ret
2553+
}
2554+
2555+
.method public static int32[] f31(class [FSharp.Core]Microsoft.FSharp.Core.FSharpFunc`2<class [FSharp.Core]Microsoft.FSharp.Core.Unit,class [FSharp.Core]Microsoft.FSharp.Core.Unit> f,
2556+
class [FSharp.Core]Microsoft.FSharp.Core.FSharpFunc`2<class [FSharp.Core]Microsoft.FSharp.Core.Unit,class [FSharp.Core]Microsoft.FSharp.Core.Unit> g) cil managed
2557+
{
2558+
.custom instance void [FSharp.Core]Microsoft.FSharp.Core.CompilationArgumentCountsAttribute::.ctor(int32[]) = ( 01 00 02 00 00 00 01 00 00 00 01 00 00 00 00 00 )
2559+
2560+
.maxstack 5
2561+
.locals init (int32[] V_0,
2562+
uint64 V_1,
2563+
int32 V_2,
2564+
int32 V_3)
2565+
IL_0000: nop
2566+
IL_0001: ldarg.0
2567+
IL_0002: ldnull
2568+
IL_0003: callvirt instance !1 class [FSharp.Core]Microsoft.FSharp.Core.FSharpFunc`2<class [FSharp.Core]Microsoft.FSharp.Core.Unit,class [FSharp.Core]Microsoft.FSharp.Core.Unit>::Invoke(!0)
2569+
IL_0008: pop
2570+
IL_0009: ldarg.1
2571+
IL_000a: ldnull
2572+
IL_000b: callvirt instance !1 class [FSharp.Core]Microsoft.FSharp.Core.FSharpFunc`2<class [FSharp.Core]Microsoft.FSharp.Core.Unit,class [FSharp.Core]Microsoft.FSharp.Core.Unit>::Invoke(!0)
2573+
IL_0010: pop
2574+
IL_0011: ldc.i4.5
2575+
IL_0012: conv.i8
2576+
IL_0013: conv.ovf.i.un
2577+
IL_0014: newarr [runtime]System.Int32
2578+
IL_0019: stloc.0
2579+
IL_001a: ldc.i4.0
2580+
IL_001b: conv.i8
2581+
IL_001c: stloc.1
2582+
IL_001d: ldc.i4.1
2583+
IL_001e: stloc.2
2584+
IL_001f: br.s IL_0031
2585+
2586+
IL_0021: ldloc.0
2587+
IL_0022: ldloc.1
2588+
IL_0023: conv.i
2589+
IL_0024: ldloc.2
2590+
IL_0025: stloc.3
2591+
IL_0026: ldloc.3
2592+
IL_0027: stelem.i4
2593+
IL_0028: ldloc.2
2594+
IL_0029: ldc.i4.2
2595+
IL_002a: add
2596+
IL_002b: stloc.2
2597+
IL_002c: ldloc.1
2598+
IL_002d: ldc.i4.1
2599+
IL_002e: conv.i8
2600+
IL_002f: add
2601+
IL_0030: stloc.1
2602+
IL_0031: ldloc.1
2603+
IL_0032: ldc.i4.5
2604+
IL_0033: conv.i8
2605+
IL_0034: blt.un.s IL_0021
2606+
2607+
IL_0036: ldloc.0
2608+
IL_0037: ret
2609+
}
2610+
2611+
.method public static int32[] f32(class [FSharp.Core]Microsoft.FSharp.Core.FSharpFunc`2<class [FSharp.Core]Microsoft.FSharp.Core.Unit,class [FSharp.Core]Microsoft.FSharp.Core.Unit> f,
2612+
class [FSharp.Core]Microsoft.FSharp.Core.FSharpFunc`2<class [FSharp.Core]Microsoft.FSharp.Core.Unit,int32> g) cil managed
2613+
{
2614+
.custom instance void [FSharp.Core]Microsoft.FSharp.Core.CompilationArgumentCountsAttribute::.ctor(int32[]) = ( 01 00 02 00 00 00 01 00 00 00 01 00 00 00 00 00 )
2615+
2616+
.maxstack 6
2617+
.locals init (int32 V_0,
2618+
int32[] V_1,
2619+
uint64 V_2,
2620+
int32 V_3,
2621+
int32 V_4)
2622+
IL_0000: nop
2623+
IL_0001: ldarg.0
2624+
IL_0002: ldnull
2625+
IL_0003: callvirt instance !1 class [FSharp.Core]Microsoft.FSharp.Core.FSharpFunc`2<class [FSharp.Core]Microsoft.FSharp.Core.Unit,class [FSharp.Core]Microsoft.FSharp.Core.Unit>::Invoke(!0)
2626+
IL_0008: pop
2627+
IL_0009: ldarg.1
2628+
IL_000a: ldnull
2629+
IL_000b: callvirt instance !1 class [FSharp.Core]Microsoft.FSharp.Core.FSharpFunc`2<class [FSharp.Core]Microsoft.FSharp.Core.Unit,int32>::Invoke(!0)
2630+
IL_0010: stloc.0
2631+
IL_0011: ldc.i4.5
2632+
IL_0012: conv.i8
2633+
IL_0013: conv.ovf.i.un
2634+
IL_0014: newarr [runtime]System.Int32
2635+
IL_0019: stloc.1
2636+
IL_001a: ldc.i4.0
2637+
IL_001b: conv.i8
2638+
IL_001c: stloc.2
2639+
IL_001d: ldc.i4.1
2640+
IL_001e: stloc.3
2641+
IL_001f: br.s IL_0035
2642+
2643+
IL_0021: ldloc.1
2644+
IL_0022: ldloc.2
2645+
IL_0023: conv.i
2646+
IL_0024: ldloc.3
2647+
IL_0025: stloc.s V_4
2648+
IL_0027: ldloc.s V_4
2649+
IL_0029: ldloc.0
2650+
IL_002a: add
2651+
IL_002b: stelem.i4
2652+
IL_002c: ldloc.3
2653+
IL_002d: ldc.i4.2
2654+
IL_002e: add
2655+
IL_002f: stloc.3
2656+
IL_0030: ldloc.2
2657+
IL_0031: ldc.i4.1
2658+
IL_0032: conv.i8
2659+
IL_0033: add
2660+
IL_0034: stloc.2
2661+
IL_0035: ldloc.2
2662+
IL_0036: ldc.i4.5
2663+
IL_0037: conv.i8
2664+
IL_0038: blt.un.s IL_0021
2665+
2666+
IL_003a: ldloc.1
2667+
IL_003b: ret
2668+
}
2669+
24342670
}
24352671

24362672
.class private abstract auto ansi sealed '<StartupCode$assembly>'.$assembly

‎tests/FSharp.Compiler.ComponentTests/EmittedIL/ComputedCollections/ForNInRangeLists.fs‎

Lines changed: 5 additions & 0 deletions

0 commit comments

Comments
 (0)