Skip to content
← Back

src/syntax/process/compile-expressions/compile_expressions_pipe_fusion.ghul

1
namespace Syntax.Process is
2
use System.Exception
3
4
use IO.Std
5
6
use Logging
7
use Source
8
9
use IR.Values
10
use IR.VALUE_CONVERTER
11
use IR.VALUE_BOXER
12
13
use Semantic.LEAST_UPPER_BOUND_MAP
14
use Semantic.Types.Type
15
16
use Syntax.Trees.Definitions.PRAGMA
17
18
use Ghul.Pipes
19
20
21
// Iterator setup for for loops and the pipe-fusion recognizer and stage builder.
22
partial COMPILE_EXPRESSIONS is
23
// Why the last _resolve_chain_fusion declined, for the
24
// pipe-fusion study. Nothing the compiler decides reads it.
25
_chain_resolve_reason: string?
26
27
// The owner of the Ghul.Pipes free functions, taken from a
28
// combinator fusion already knows, so a call to a Pipes function
29
// fusion does not recognise can still be told from any other
30
// global call. Null when the runtime is unavailable, or before
31
// the first lookup.
32
_pipes_owner: Semantic.Scope?
33
_pipes_owner_known: bool
34
35
_pipes_owner_symbol: Semantic.Scope? is
36
if !_pipes_owner_known then
37
for combinator in _innate_symbol_lookup.get_pipes_fusion_functions().keys do
38
_pipes_owner = combinator.owner
39
40
break
41
od
42
43
_pipes_owner_known = true
44
fi
45
46
return _pipes_owner
47
si
48
49
// Whether `function` is declared in Ghul.Pipes.
50
_is_pipes_function(function: Semantic.Symbols.Function?) -> bool is
51
let owner = _pipes_owner_symbol
52
53
return function? /\ owner? /\ function.root_specialized_from.owner == owner
54
si
55
56
// The name of the Ghul.Pipes function `call` resolved to, or null
57
// for any other call.
58
_pipes_function_name(call: Trees.Expressions.CALL) -> string? is
59
if !isa IR.Values.Call.GLOBAL(call.value) then
60
return null
61
fi
62
63
let function = (cast IR.Values.Call.GLOBAL(call.value)).function
64
65
if !_is_pipes_function(function) then
66
return null
67
fi
68
69
return function.name
70
si
71
72
// Iterable inference: a still-placeholder iterated expression
73
// records an ITERABLE_CONSTRAINT on its origin so the body-retry
74
// loop can filter candidate types to those that actually
75
// iterate. set_iterator_for will still emit "not iterable" on
76
// this iteration - the speculate/roll-back wrapper drops the
77
// error on retry once the placeholder resolves.
78
record_iterable_constraint(expression: Trees.Expressions.Expression?) is
79
if let
80
ev = expression?.value,
81
ev_type = ev.type /\
82
isa Semantic.Types.INFERRED_VARIABLE_TYPE(ev_type)
83
then
84
let placeholder = cast Semantic.Types.INFERRED_VARIABLE_TYPE(ev_type)
85
86
_logger.mark_consumed_any_if(Semantic.INFERENCE_TRACE.add_constraint("pipe_fusion.iterable", placeholder.origin, Semantic.ITERABLE_CONSTRAINT()))
87
fi
88
si
89
90
set_iterator_for(carrier: Trees.IteratorCarrier, type: Type, recursing: bool) -> bool is
91
let expression = carrier.iterated!
92
93
expression.value!.check_is_consumable(_logger, expression.location)
94
95
if type.is_error then
96
return false
97
fi
98
99
// A reference type that is both iterable and its own iterator, such
100
// as a pipe, is asked for an iterator rather than driven directly, so
101
// that each read starts from the beginning. A struct is driven
102
// directly: asking it would copy it, and box the copy.
103
if !recursing /\ !type.is_value_type then
104
if let read_iterator = get_zero_argument_function(type, "$get_iterator") then
105
carrier.read_iterator = read_iterator
106
107
return set_iterator_for(carrier, read_iterator.return_type!, true)
108
fi
109
fi
110
111
let move_next = get_zero_argument_function(type, "move_next")
112
113
if move_next? then
114
let read_current mut = get_zero_argument_function(type, "$get_current")
115
116
if !read_current? then
117
read_current = get_zero_argument_function(type, "$get_Current")
118
fi
119
120
if !read_current? then
121
_logger.error(expression.location, "incomplete iterator type (has move_next method but no current property)")
122
return false
123
fi
124
125
carrier.move_next = move_next
126
carrier.read_current = read_current
127
128
_declare_state_machine_iterator_field(carrier, type)
129
130
return true
131
elif !recursing then
132
let read_iterator = get_zero_argument_function(type, "$get_iterator")
133
134
if read_iterator? then
135
carrier.read_iterator = read_iterator
136
137
return set_iterator_for(carrier, read_iterator.return_type!, true)
138
fi
139
fi
140
141
_logger.error(expression.location, "not iterable")
142
143
return false
144
si
145
146
// Recognise a fusible Pipe[T] chain on the loop expression and,
147
// if found, resolve the source's own iterator members (exactly
148
// as a plain `for x in source` would) so the IL pass can drive
149
// them directly, skipping the pipe objects entirely. Returns
150
// null - fall back to the normal iterator loop - when the chain
151
// isn't fusible or the source doesn't expose a complete iterator
152
// surface.
153
_recognize_pipe_fusion(`for: Trees.Statements.FOR) -> PIPE_FUSION? is
154
// `@suppress("pipe-fusion")` disables fusion for a scope, so the
155
// loop runs the ordinary pipe-object lowering - used where the
156
// real Pipe implementations must be exercised (the Ghul.Pipes
157
// unit tests) rather than the fused equivalent.
158
if _logger.is_suppressed("pipe-fusion", `for.location) then
159
return null
160
fi
161
162
let recognizer = PIPE_FUSION_RECOGNIZER(_innate_symbol_lookup.get_pipes_fusion_functions())
163
let fusion = recognizer.recognize(`for)
164
165
if !_resolve_chain_fusion(fusion) then
166
_note_fusion_site(`for.location, "for", true, recognizer, false)
167
168
return null
169
fi
170
171
_note_fusion_site(`for.location, "for", true, recognizer, true)
172
173
return fusion
174
si
175
176
// Record for the pipe-fusion study what a consumption site
177
// decided, with the reason the recogniser or the resolver gave.
178
// Inert unless the study is enabled.
179
//
180
// A site that fused is always recorded, stages or none: a bare
181
// source is a chain of no stages and fuses like any other. A site
182
// that peeled no stage and did not fuse is recorded only when the
183
// peel stopped at a Ghul.Pipes function - a combinator fusion has
184
// no stage for, which is what teaching the fuser a new stage would
185
// recover. Stopping anywhere else leaves nothing fusion could have
186
// reached, which every ordinary `for` loop does.
187
_note_fusion_site(
188
location: Source.LOCATION,
189
consumer: string,
190
consumer_fusible: bool,
191
recognizer: PIPE_FUSION_RECOGNIZER,
192
fused: bool
193
) is
194
if !PIPE_FUSION_STUDY.enabled then
195
return
196
fi
197
198
let roles = recognizer.peeled_roles ?? ""
199
200
if !fused /\ roles.length == 0 /\ !_is_pipes_function(recognizer.boundary_function) then
201
return
202
fi
203
204
PIPE_FUSION_STUDY.note_site(
205
location,
206
consumer,
207
consumer_fusible,
208
roles,
209
fused,
210
if fused then
211
""
212
else
213
recognizer.decline_reason ?? _chain_resolve_reason ?? "unknown"
214
fi)
215
si
216
217
// Record a call to a Ghul.Pipes function for the study, whether or
218
// not fusion recognises it, so the sites fusion reaches can be read
219
// as a fraction of the whole combinator surface.
220
_note_pipes_call(call: Trees.Expressions.CALL) is
221
if !PIPE_FUSION_STUDY.enabled then
222
return
223
fi
224
225
if let name = _pipes_function_name(call) then
226
PIPE_FUSION_STUDY.note_call(call.location, name)
227
fi
228
si
229
230
// Record a Ghul.Pipes call that consumes a chain but that fusion
231
// has no consumer for - `collect`, `join`, `sum` and the rest -
232
// naming the chain it would have consumed. This is where teaching
233
// the fuser a new consumer pays, so the shapes are worth counting
234
// even though nothing here declined on their account.
235
_note_unfusible_consumer(call: Trees.Expressions.CALL) is
236
if !PIPE_FUSION_STUDY.enabled then
237
return
238
fi
239
240
let name = _pipes_function_name(call)
241
242
if !name? \/ call.arguments.expressions.count == 0 then
243
return
244
fi
245
246
let fusion_functions = _innate_symbol_lookup.get_pipes_fusion_functions()
247
let role: string mut
248
249
if
250
isa IR.Values.Call.GLOBAL(call.value) /\
251
fusion_functions.try_get_value(
252
(cast IR.Values.Call.GLOBAL(call.value)).function.root_specialized_from, role ref) /\
253
PIPE_CONSUMER_KIND.is_stage_role(role)
254
then
255
return
256
fi
257
258
let recognizer = PIPE_FUSION_RECOGNIZER(fusion_functions)
259
260
let _ = recognizer.recognize_any(call.arguments.expressions[0])
261
262
_chain_resolve_reason = "consumer-not-fusible"
263
264
_note_fusion_site(call.location, name, false, recognizer, false)
265
si
266
267
// Finish a recognised chain plan: resolve the source's own iterator
268
// members, and resolve each stage (index constructor, or inline a
269
// literal lambda). Returns false when any of that fails, in which case the
270
// chain must not be fused. Shared by the `for` and consumer paths.
271
_resolve_chain_fusion(fusion: PIPE_FUSION?) -> bool =>
272
_resolve_chain_fusion(fusion, true)
273
274
// `inline_stages` false skips the inline re-walk of map/filter lambda
275
// bodies - consumer fusion calls the stage delegates rather than
276
// inlining them, and the re-walk can disturb a stage lambda whose
277
// body nests a capturing closure.
278
_resolve_chain_fusion(fusion: PIPE_FUSION?, inline_stages: bool) -> bool is
279
_chain_resolve_reason = null
280
281
if !fusion? then
282
return false
283
fi
284
285
let source_value = fusion.source.value
286
287
if !source_value? \/ !source_value.type? then
288
_chain_resolve_reason = "source-not-compiled"
289
290
return false
291
fi
292
293
if !_resolve_fusion_source_iterator(fusion, source_value.type, false) then
294
_chain_resolve_reason = "source-iterator-unresolved"
295
296
return false
297
fi
298
299
let to_inline = Collections.LIST[PIPE_FUSION_STAGE]()
300
301
for stage in fusion.stages_outermost_first do
302
if stage.is_countdown then
303
// take/skip carry an int count evaluated once into the
304
// running counter; nothing to inline or resolve, but the
305
// count must have compiled to a value.
306
let count_value = stage.argument!.value
307
308
if !count_value? \/ !count_value.type? then
309
_chain_resolve_reason = "count-not-compiled"
310
311
return false
312
fi
313
elif stage.is_index then
314
// The INDEXED_VALUE the stage builds is the element
315
// type of the `index` call's own return, so it is read
316
// off the call rather than rebuilt from the element type
317
// flowing into the stage.
318
if !_resolve_index_stage(stage) then
319
_chain_resolve_reason = "index-stage-unresolved"
320
321
return false
322
fi
323
else
324
// A map/filter stage's function argument must have
325
// compiled to a function-typed value. One that failed
326
// to resolve - a bare overloaded group with no
327
// member the formal could pick - carries a group
328
// type with no call shape, and building consumer
329
// stages from it would index past an empty argument
330
// list. Decline fusion; the ordinary call path
331
// reports the argument's own error.
332
let stage_argument = stage.argument!.value
333
334
335
if
336
!stage_argument? \/
337
!stage_argument.type? \/
338
!stage_argument.type.is_function \/
339
stage_argument.type.arguments.count == 0
340
then
341
_chain_resolve_reason = "stage-argument-not-a-function"
342
343
return false
344
fi
345
346
if inline_stages then
347
to_inline.add(stage)
348
fi
349
fi
350
od
351
352
// Inlining takes a stage's body out of its literal, so it waits
353
// until every stage has resolved: a chain that declines part way
354
// runs the literals as they were written.
355
for stage in to_inline do
356
_try_inline_stage(stage)
357
od
358
359
return true
360
si
361
362
// Determine whether `call` is a terminal Pipe consumer: a
363
// `Ghul.Pipes` free-function call, reached via |> thread-first
364
// desugaring (`chain |> count()`) or written directly
365
// (`count(chain)`), the two being indistinguishable by the time
366
// this pass runs. It is identified by comparing the call's
367
// resolved target against the fusible-combinator symbols, so
368
// what fuses follows the function the call bound rather than
369
// how it spells itself. Only a global call can be one: `Pipe[T]`
370
// carries no combinator methods of its own, so a member call
371
// never resolves to a combinator. Returns the consumer's role
372
// and the chain expression feeding it, which is the call's
373
// first argument.
374
_consumer_call_shape(call: Trees.Expressions.CALL) -> (name: string, chain_before: Trees.Expressions.Expression)? is
375
if !isa IR.Values.Call.GLOBAL(call.value) then
376
return null
377
fi
378
379
let function = (cast IR.Values.Call.GLOBAL(call.value)).function
380
let role: string mut
381
382
if !_innate_symbol_lookup.get_pipes_fusion_functions().try_get_value(function.root_specialized_from, role ref) then
383
return null
384
fi
385
386
if call.arguments.expressions.count == 0 then
387
return null
388
fi
389
390
return (role, call.arguments.expressions[0])
391
si
392
393
// Recognise a terminal Pipe consumer (`chain |> count()`, or the
394
// equivalent `count(chain)`) whose chain is fusible, and build the
395
// FUSED_CONSUMER value the call lowers to.
396
// Every stage fuses (a map/filter delegate is called rather than
397
// inlined). A `memo` stage is elided - the consumer is the memo's
398
// only reader, so the cache would never be consulted.
399
_recognize_consumer_fusion(call: Trees.Expressions.CALL) -> IR.Values.FUSED_CONSUMER? is
400
_note_pipes_call(call)
401
402
if _logger.is_suppressed("pipe-fusion", call.location) then
403
return null
404
fi
405
406
let shape = _consumer_call_shape(call)
407
408
if !shape? then
409
_note_unfusible_consumer(call)
410
411
return null
412
fi
413
414
let name = shape.name
415
let chain_before = shape.chain_before
416
let arg_count = call.arguments.expressions.count - 1
417
418
let recognized_kind = PIPE_CONSUMER_KIND.of(name, arg_count)
419
420
if !recognized_kind? then
421
_note_unfusible_consumer(call)
422
423
return null
424
fi
425
426
let consumer_kind = recognized_kind
427
428
if !call.value? \/ !call.value.type? then
429
return null
430
fi
431
432
let recognizer = PIPE_FUSION_RECOGNIZER(_innate_symbol_lookup.get_pipes_fusion_functions())
433
let fusion = recognizer.recognize_any(chain_before)
434
435
if !_resolve_chain_fusion(fusion, false) then
436
_note_fusion_site(call.location, _pipes_function_name(call) ?? consumer_kind, true, recognizer, false)
437
438
return null
439
fi
440
441
let plan = fusion!
442
443
// `count` over a bare source is the one consumer a fused loop
444
// makes slower: the runtime's own `count` returns `source.count`
445
// for a sized source, so fusing turns an O(1) read into a walk
446
// of every element. A chain with stages has nothing sized to
447
// ask - the stages are never materialised - so it keeps fusing.
448
if plan.stages_outermost_first.count == 0 /\ consumer_kind =~ "count" then
449
return _decline_consumer(call, recognizer, consumer_kind, "bare-count-has-a-sized-fast-path")
450
fi
451
452
let id = _next_fused_local_id()
453
454
let source_read_iterator = plan.source_read_iterator
455
456
let iterator_type =
457
if source_read_iterator? then
458
source_read_iterator.return_type!
459
else
460
plan.source.value!.type!
461
fi
462
463
let iterator_init =
464
if source_read_iterator? then
465
source_read_iterator.call(plan.source.location, plan.source.value!, Collections.LIST[IR.Values.Value](0), null, _function_caller)
466
else
467
plan.source.value!
468
fi
469
470
let iterator = IR.LOCAL(".fc_iter.{id}", iterator_type)
471
let iterator_load = iterator.load()
472
473
let move_next = plan.source_move_next!.call(plan.source.location, iterator_load, Collections.LIST[IR.Values.Value](0), null, _function_caller)
474
let read_current = plan.source_read_current!.call(plan.source.location, iterator_load, Collections.LIST[IR.Values.Value](0), null, _function_caller)
475
476
let element_type = plan.source_read_current!.return_type!
477
478
let element = IR.LOCAL(".fc_element.{id}", element_type)
479
480
let built = _build_consumer_stages(plan, element)
481
482
let result = IR.LOCAL(".fc_result.{id}", call.value!.type!)
483
484
let fused = IR.Values.FUSED_CONSUMER(
485
call.value!.type!,
486
iterator_init,
487
source_read_iterator?,
488
iterator,
489
move_next,
490
read_current,
491
element,
492
built.ops,
493
built.final,
494
consumer_kind,
495
result
496
)
497
498
let final_element_load = built.final.load()
499
500
if PIPE_CONSUMER_KIND.takes_function(consumer_kind) then
501
// Hoist the predicate / action delegate and apply it to the
502
// surviving element per iteration. A function argument that
503
// failed to resolve has no call shape to apply - decline
504
// fusion and let the ordinary call path report it.
505
let function = call.arguments.expressions[1].value
506
507
if !function? \/ !function.type? \/ !function.type.is_function then
508
return _decline_consumer(call, recognizer, consumer_kind)
509
fi
510
511
let func_type = function.type
512
513
let arg = IR.LOCAL(".fc_arg.{id}", func_type)
514
515
let result_type =
516
if func_type.is_action then
517
_innate_symbol_lookup.get_void_type()
518
else
519
func_type.arguments[func_type.arguments.count - 1]
520
fi
521
522
// sum_by totals what the selector returns, so the running
523
// total is of the selector's type rather than the element's,
524
// and it is that type whose addition has to be the `add`
525
// instruction.
526
if consumer_kind =~ "sum_by" then
527
if !_is_add_instruction_type(result_type) then
528
return _decline_consumer(call, recognizer, consumer_kind, "sum-element-not-add-instruction")
529
fi
530
531
fused.seed_init = IR.Values.DEFAULT(result_type)
532
fi
533
534
let call_arguments = Collections.LIST[IR.Values.Value]()
535
call_arguments.add(final_element_load)
536
537
fused.consumer_arg = arg
538
fused.consumer_arg_init = function
539
fused.consumer_apply = IR.Values.Call.CLOSURE(
540
arg.load(), result_type, func_type.is_action, func_type, call_arguments)
541
elif consumer_kind =~ "reduce" then
542
// running = accumulator(running, element).
543
let seed = call.arguments.expressions[1].value!
544
let accumulator = call.arguments.expressions[2].value!
545
let func_type = accumulator.type!
546
547
let arg = IR.LOCAL(".fc_arg.{id}", func_type)
548
let running_type = call.value!.type!
549
550
let call_arguments = Collections.LIST[IR.Values.Value]()
551
call_arguments.add(cast IR.Values.Value(IR.Values.Load.TEMP(result, running_type)))
552
call_arguments.add(final_element_load)
553
554
fused.seed_init = seed
555
fused.consumer_arg = arg
556
fused.consumer_arg_init = accumulator
557
fused.consumer_apply = IR.Values.Call.CLOSURE(
558
arg.load(), running_type, false, func_type, call_arguments)
559
elif consumer_kind =~ "sum" then
560
// The running total starts at the element type's zero and
561
// the loop adds each element with one instruction. A type
562
// whose `+` is an operator method - decimal, bigint, an
563
// INumber implementor of its own - cannot be added that
564
// way, and keeps the runtime's sum.
565
if !_is_add_instruction_type(built.final.type) then
566
return _decline_consumer(call, recognizer, consumer_kind, "sum-element-not-add-instruction")
567
fi
568
569
fused.seed_init = IR.Values.DEFAULT(built.final.type)
570
fi
571
572
// find / first return a MAYBE: seed with the empty MAYBE, and
573
// wrap the surviving element with MAYBE(element).
574
if consumer_kind =~ "find" \/ consumer_kind =~ "first" then
575
let maybe_type = call.value!.type!
576
577
let empty_ctor = _resolve_ctor_by_arity(maybe_type, 0)
578
let wrap_ctor = _resolve_ctor_by_arity(maybe_type, 1)
579
580
if !empty_ctor? \/ !wrap_ctor? then
581
return _decline_consumer(call, recognizer, consumer_kind)
582
fi
583
584
fused.seed_init = IR.Values.NEW(maybe_type, empty_ctor, Collections.LIST[IR.Values.Value](0))
585
586
let wrap_args = Collections.LIST[IR.Values.Value]()
587
wrap_args.add(final_element_load)
588
589
fused.wrap_element = IR.Values.NEW(maybe_type, wrap_ctor, wrap_args)
590
fi
591
592
// collect_mutable returns a fresh LIST[element]; seed it empty and
593
// add the surviving element each iteration.
594
if consumer_kind =~ "collect" then
595
let list_type = call.value!.type!
596
597
let ctor = _resolve_ctor_by_arity(list_type, 0)
598
let add_fn = _resolve_member_by_arity(list_type, "add", 1)
599
600
if !ctor? \/ !add_fn? then
601
return _decline_consumer(call, recognizer, consumer_kind)
602
fi
603
604
fused.seed_init = IR.Values.NEW(list_type, ctor, Collections.LIST[IR.Values.Value](0))
605
606
let add_args = Collections.LIST[IR.Values.Value]()
607
add_args.add(final_element_load)
608
609
fused.add_element = add_fn.call(
610
call.location, IR.Values.Load.TEMP(result, list_type), add_args, null, _function_caller)
611
fi
612
613
if consumer_kind =~ "collect_array" then
614
// `collect` is `collect_mutable(source).to_array()`: fill a
615
// LIST over the chain's element type exactly as the fused
616
// collect_mutable does, then hand back its array. The
617
// call's own type is the array; the list is built here.
618
let unspecialized_list = _innate_symbol_lookup.get_unspecialized_mutable_list_type()
619
620
if !isa Semantic.Types.NAMED(unspecialized_list) then
621
return _decline_consumer(call, recognizer, consumer_kind)
622
fi
623
624
// The open generic's symbol is the GENERIC specialization
625
// over its own parameters; the type to specialize is the
626
// Classy it stands for.
627
let list_symbol mut = (cast Semantic.Types.NAMED(unspecialized_list)).symbol
628
629
if isa Semantic.Symbols.GENERIC(list_symbol) then
630
list_symbol = (cast Semantic.Symbols.GENERIC(list_symbol)).symbol
631
fi
632
633
if !isa Semantic.Symbols.Classy(list_symbol) then
634
return _decline_consumer(call, recognizer, consumer_kind)
635
fi
636
637
let list_arguments = Collections.LIST[Type]()
638
list_arguments.add(built.final.type)
639
640
let list_type =
641
cast Type(
642
Semantic.Types.GENERIC(
643
call.location,
644
cast Semantic.Symbols.Classy(list_symbol),
645
list_arguments))
646
647
let ctor = _resolve_ctor_by_arity(list_type, 0)
648
let add_fn = _resolve_member_by_arity(list_type, "add", 1)
649
let to_array = _resolve_member_by_arity(list_type, "to_array", 0)
650
651
if !ctor? \/ !add_fn? \/ !to_array? then
652
return _decline_consumer(call, recognizer, consumer_kind)
653
fi
654
655
fused.result_local_type = list_type
656
657
fused.seed_init = IR.Values.NEW(list_type, ctor, Collections.LIST[IR.Values.Value](0))
658
659
let add_args = Collections.LIST[IR.Values.Value]()
660
add_args.add(final_element_load)
661
662
fused.add_element = add_fn.call(
663
call.location, IR.Values.Load.TEMP(result, list_type), add_args, null, _function_caller)
664
665
fused.finish = to_array.call(
666
call.location,
667
IR.Values.Load.TEMP(result, list_type),
668
Collections.LIST[IR.Values.Value](0),
669
null,
670
_function_caller)
671
fi
672
673
if consumer_kind =~ "join" then
674
// `join(chain)` is its own overload whose body passes a
675
// separator to the two-argument one, and a referenced
676
// runtime's bodies are not there to read it from. Writing
677
// the separator here instead would put the fused text at
678
// the mercy of the two agreeing, silently, so the form
679
// without one is left to the runtime.
680
if call.arguments.expressions.count < 2 then
681
return _decline_consumer(call, recognizer, consumer_kind, "join-separator-not-given")
682
fi
683
684
if !_build_fused_join(fused, call, id, result, final_element_load, built.final.type) then
685
return _decline_consumer(call, recognizer, consumer_kind, "join-handler-unresolved")
686
fi
687
fi
688
689
if consumer_kind =~ "collect" \/ consumer_kind =~ "collect_array" then
690
_try_presize(fused, plan, call, consumer_kind, built.final.type)
691
fi
692
693
_note_fusion_site(call.location, _pipes_function_name(call) ?? consumer_kind, true, recognizer, true)
694
695
return fused
696
si
697
698
// Size a fused collect's result up front where the source's count
699
// is known before the loop and every stage keeps it. The source is
700
// then read twice - for its iterator and for its count - so it is
701
// evaluated once into a local and the iterator rebuilt over that.
702
// A list result is constructed at the count as its capacity; an
703
// array result is allocated at the count and written by index,
704
// with no list and no copy. Nothing changes where the count is not
705
// known or a stage can drop or add elements.
706
_try_presize(
707
fused: IR.Values.FUSED_CONSUMER,
708
plan: PIPE_FUSION,
709
call: Trees.Expressions.CALL,
710
consumer_kind: string,
711
element_type: Type
712
) is
713
if !_stages_keep_count(plan) then
714
return
715
fi
716
717
let source_value = plan.source.value!
718
let source_type = source_value.type!
719
720
let source = IR.LOCAL(".fc_source.{_next_fused_local_id()}", source_type)
721
let source_load = source.load()
722
723
let count = known_counts.count_of(plan.source, source_load, source_type, call.location)
724
725
if !count? then
726
return
727
fi
728
729
fused.source = source
730
fused.source_init = source_value
731
732
fused.iterator_init =
733
if let read_iterator = plan.source_read_iterator then
734
read_iterator.call(plan.source.location, source_load, Collections.LIST[IR.Values.Value](0), null, _function_caller)
735
else
736
source_load
737
fi
738
739
if consumer_kind =~ "collect_array" then
740
fused.presize_array = true
741
fused.presize_count = count
742
fused.presize_index = IR.LOCAL(".fc_fill.{_next_fused_local_id()}", _innate_symbol_lookup.get_int_type())
743
fused.presize_element_type = element_type
744
fused.result_local_type = null
745
fused.seed_init = null
746
fused.add_element = null
747
fused.finish = null
748
else
749
let list_type = fused.result_local_type ?? call.value!.type!
750
let ctor = _resolve_ctor_taking_int(list_type)
751
752
if !ctor? then
753
return
754
fi
755
756
let capacity = Collections.LIST[IR.Values.Value]()
757
capacity.add(count)
758
759
fused.seed_init = IR.Values.NEW(list_type, ctor, capacity)
760
fi
761
si
762
763
// Whether every stage passes each element through: map and index
764
// do, filter, take and skip do not.
765
_stages_keep_count(plan: PIPE_FUSION) -> bool is
766
for stage in plan.stages_outermost_first do
767
if stage.is_filter \/ stage.is_countdown then
768
return false
769
fi
770
od
771
772
return true
773
si
774
775
// How many elements a source will produce, where that is known
776
// before the loop starts.
777
known_counts: KNOWN_COUNT_SOURCE =>
778
KNOWN_COUNT_SOURCE(
779
_innate_symbol_lookup,
780
_function_caller,
781
(type, name) => get_zero_argument_function(type, name),
782
call => _pipes_function_name(call)
783
)
784
785
// The constructor of `type` taking one int - a list's capacity.
786
_resolve_ctor_taking_int(type: Type) -> Semantic.Symbols.Function? is
787
let member = type.find_member("init")
788
789
if !member? \/ !isa Semantic.Symbols.FUNCTION_GROUP(member) then
790
return null
791
fi
792
793
let int_type = _innate_symbol_lookup.get_int_type()
794
795
for f in (cast Semantic.Symbols.FUNCTION_GROUP(member)).functions do
796
if f.arguments.count != 1 then
797
continue
798
fi
799
800
if let parameter_type = f.arguments[0].type then
801
if int_type.is_assignable_from(parameter_type) /\ parameter_type.is_assignable_from(int_type) then
802
return f
803
fi
804
fi
805
od
806
807
return null
808
si
809
810
// Decline a consumer whose chain resolved but whose own shape did
811
// not - an unresolved function argument, list constructor or
812
// member - recording the site as declined for the study.
813
_decline_consumer(call: Trees.Expressions.CALL, recognizer: PIPE_FUSION_RECOGNIZER, consumer_kind: string) -> IR.Values.FUSED_CONSUMER? =>
814
_decline_consumer(call, recognizer, consumer_kind, "consumer-unresolved")
815
816
// `reason` is what the study records, so a decline the fuser could
817
// one day lift is told apart from one it cannot.
818
_decline_consumer(
819
call: Trees.Expressions.CALL,
820
recognizer: PIPE_FUSION_RECOGNIZER,
821
consumer_kind: string,
822
reason: string
823
) -> IR.Values.FUSED_CONSUMER? is
824
_chain_resolve_reason = reason
825
826
_note_fusion_site(call.location, _pipes_function_name(call) ?? consumer_kind, true, recognizer, false)
827
828
return null
829
si
830
831
// A member of `type` named `name` taking `arity` arguments (or null).
832
_resolve_member_by_arity(type: Type, name: string, arity: int) -> Semantic.Symbols.Function? is
833
let member = type.find_member(name)
834
835
if !member? \/ !isa Semantic.Symbols.FUNCTION_GROUP(member) then
836
return null
837
fi
838
839
let group = cast Semantic.Symbols.FUNCTION_GROUP(member)
840
841
for f in group.functions do
842
if f.argument_names.count == arity then
843
return f
844
fi
845
od
846
847
return null
848
si
849
850
// The constructor of `type` taking `arity` arguments (or null).
851
_resolve_ctor_by_arity(type: Type, arity: int) -> Semantic.Symbols.Function? =>
852
_resolve_member_by_arity(type, "init", arity)
853
854
// Pre-build the per-stage application for a consumer loop, threading
855
// the element through freshly-named locals (a map produces a new
856
// typed local the next stage reads). Every stage is an inlinable
857
// lambda: assign the current element to the lambda's parameter local,
858
// then the harvested inline body is the applied value.
859
_build_consumer_stages(fusion: PIPE_FUSION, start: IR.LOCAL) -> (ops: Collections.LIST[IR.Values.FUSED_CONSUMER_STAGE], final: IR.LOCAL) is
860
let ops = Collections.LIST[IR.Values.FUSED_CONSUMER_STAGE]()
861
862
let current mut = start
863
864
let index mut = fusion.stages_outermost_first.count - 1
865
866
while index >= 0 do
867
let stage = fusion.stages_outermost_first[index]
868
869
if stage.is_countdown then
870
// take/skip: a running counter, no delegate and no
871
// output local - the element passes through unchanged.
872
let counter = IR.LOCAL(".fc_countdown.{_next_fused_local_id()}", _innate_symbol_lookup.get_int_type())
873
874
ops.add(IR.Values.FUSED_CONSUMER_STAGE(stage.is_take, stage.is_skip, counter, stage.argument!.value!))
875
876
index = index - 1
877
878
continue
879
fi
880
881
if stage.is_index then
882
// A running counter and an INDEXED_VALUE built per
883
// element from it, in place of an INDEX_PIPE. The
884
// counter is seeded with the caller's start where one
885
// was given, and with zero otherwise.
886
let int_type = _innate_symbol_lookup.get_int_type()
887
let counter = IR.LOCAL(".fc_index.{_next_fused_local_id()}", int_type)
888
let indexed_type = stage.indexed_value_type!
889
let output = IR.LOCAL(".fc_indexed.{_next_fused_local_id()}", indexed_type)
890
891
let start =
892
if let start_expression = stage.argument then
893
start_expression.value!
894
else
895
cast IR.Values.Value(IR.Values.Literal.NUMBER(0, int_type))
896
fi
897
898
let build_arguments = Collections.LIST[IR.Values.Value]()
899
build_arguments.add(
900
cast IR.Values.Value(IR.Values.POST_INCREMENT(counter, int_type)))
901
build_arguments.add(current.load())
902
903
ops.add(
904
IR.Values.FUSED_CONSUMER_STAGE(
905
counter,
906
start,
907
output,
908
IR.Values.NEW(indexed_type, stage.indexed_value_constructor!, build_arguments)))
909
910
current = output
911
912
index = index - 1
913
914
continue
915
fi
916
917
let delegate = stage.argument!.value!
918
let func_type = delegate.type!
919
920
let delegate_local = IR.LOCAL(".fc_stage.{_next_fused_local_id()}", func_type)
921
922
let result_type =
923
if func_type.is_action then
924
_innate_symbol_lookup.get_void_type()
925
else
926
func_type.arguments[func_type.arguments.count - 1]
927
fi
928
929
let call_arguments = Collections.LIST[IR.Values.Value]()
930
call_arguments.add(current.load())
931
932
let apply = cast IR.Values.Value(IR.Values.Call.CLOSURE(
933
delegate_local.load(), result_type, func_type.is_action, func_type, call_arguments))
934
935
if stage.is_filter then
936
ops.add(IR.Values.FUSED_CONSUMER_STAGE(true, delegate_local, delegate, apply, null))
937
else
938
let output = IR.LOCAL(".fc_map.{_next_fused_local_id()}", result_type)
939
940
ops.add(IR.Values.FUSED_CONSUMER_STAGE(false, delegate_local, delegate, apply, output))
941
942
current = output
943
fi
944
945
index = index - 1
946
od
947
948
return (ops, current)
949
si
950
951
// Fill an index stage's INDEXED_VALUE type and constructor, and
952
// check the start it was given compiled. False declines the whole
953
// chain, which then runs through its own INDEX_PIPE unchanged.
954
_resolve_index_stage(stage: PIPE_FUSION_STAGE) -> bool is
955
if let start = stage.argument then
956
let start_value = start.value
957
958
if !start_value? \/ !start_value.type? then
959
return false
960
fi
961
fi
962
963
if !stage.chain_type? then
964
return false
965
fi
966
967
stage.indexed_value_type = Semantic.Symbols.sequence_element_type(stage.chain_type)
968
969
if !stage.indexed_value_type? then
970
return false
971
fi
972
973
stage.indexed_value_constructor = _resolve_index_constructor(stage.indexed_value_type)
974
975
return stage.is_index_ready
976
si
977
978
// The single INDEXED_VALUE[element] constructor `init(int, element)`,
979
// specialized to the stage's element type, that the fused index stage
980
// calls per element in place of building an INDEX_PIPE.
981
_resolve_index_constructor(indexed_value_type: Type) -> Semantic.Symbols.Function? is
982
let init_symbol = indexed_value_type.find_member("init")
983
984
if !init_symbol? \/ !isa Semantic.Symbols.FUNCTION_GROUP(init_symbol) then
985
return null
986
fi
987
988
let group = cast Semantic.Symbols.FUNCTION_GROUP(init_symbol)
989
990
if group.count != 1 then
991
return null
992
fi
993
994
return group.functions[0]
995
si
996
997
_fused_local_id_counter: int static
998
999
_next_fused_local_id() -> int static is
1000
let result = _fused_local_id_counter
1001
_fused_local_id_counter = _fused_local_id_counter + 1
1002
return result
1003
si
1004
1005
// Attempt to inline a stage's `map`/`filter` lambda so the fused
1006
// loop runs its body directly, with no delegate or closure
1007
// frame. Only literal, single-parameter, expression-bodied
1008
// lambdas qualify; anything else keeps the delegate path
1009
// (`stage` left unmodified).
1010
//
1011
// Mechanism: declare a synthetic local, owned by the enclosing
1012
// method, for the parameter, then re-walk the body with that
1013
// local bound. Because the current function is the enclosing
1014
// method (not the closure), the parameter and any captured outer
1015
// locals resolve as ordinary local loads. The re-walk's
1016
// diagnostics and symbol-use records are discarded (the first,
1017
// normal walk already recorded them). The body's nodes keep the
1018
// loop-context IR, and generate-il walks the body where the loop
1019
// wants it, so the closure's own method is never emitted. Only a
1020
// decline after that walk restores the nodes, by walking the whole
1021
// lambda again.
1022
_try_inline_stage(stage: PIPE_FUSION_STAGE) is
1023
// A body whose statements become a state machine keeps its
1024
// loops as pipe objects, so the stage's delegate is still
1025
// built and called. Inlining marks the literal to load its
1026
// names as the enclosing body's locals, which in a state
1027
// machine they are not, so the delegate's own method would be
1028
// emitted reading locals that do not exist there.
1029
let enclosing = _symbol_table.current_function
1030
1031
if
1032
Semantic.Symbols.state_machine_for(enclosing)? \/
1033
Semantic.Symbols.async_state_machine_for(enclosing)?
1034
then
1035
return
1036
fi
1037
1038
if !isa Trees.Expressions.FUNCTION(stage.argument) then
1039
return
1040
fi
1041
1042
let function = cast Trees.Expressions.FUNCTION(stage.argument)
1043
1044
if
1045
function.is_recursive \/
1046
function.contains_let_await \/
1047
function.arguments.expressions.count != 1 \/
1048
!isa Trees.Bodies.EXPRESSION(function.body)
1049
then
1050
return
1051
fi
1052
1053
let param = function.arguments.expressions[0]
1054
1055
if !isa Trees.Expressions.VARIABLE(param) then
1056
return
1057
fi
1058
1059
let variable = cast Trees.Expressions.VARIABLE(param)
1060
1061
// The harvest below declares one enclosing-method local, for
1062
// the formal's own name. A destructure pattern's leaves are
1063
// separate symbols owned by the closure, and nothing here
1064
// rebinds them, so the body would read them against the
1065
// enclosing method. Inlining one needs a local per leaf and
1066
// an unpack in the fused loop; until then the stage keeps
1067
// the delegate.
1068
if variable.is_destructuring then
1069
return
1070
fi
1071
1072
let param_name = variable.name.name
1073
1074
let closure = cast Semantic.Symbols.Closure?(_symbol_table.scope_for(function))
1075
1076
if !closure? then
1077
return
1078
fi
1079
1080
let param_symbol = closure.find_direct(param_name)
1081
1082
if !param_symbol? \/ !isa Semantic.Types.Typed(param_symbol) then
1083
return
1084
fi
1085
1086
let param_type = (cast Semantic.Types.Typed(param_symbol)).type
1087
1088
if !param_type? \/ param_type.is_sentinel \/ param_type.is_error then
1089
return
1090
fi
1091
1092
let body = cast Trees.Bodies.EXPRESSION?(function.body)
1093
1094
if !body? then
1095
return
1096
fi
1097
1098
// The parameter local the fused loop stores each element into.
1099
// It is declared in a scope of its own so its name cannot
1100
// collide with anything the method already has - nothing ever
1101
// looks it up by name, since the loader translates the
1102
// literal's parameter symbol to it directly.
1103
//
1104
// The body is walked again on every body retry, and the local
1105
// has to be the same one each time. A nested literal captures
1106
// it, and a capture is keyed on the symbol, so a second local
1107
// would leave that capture naming a slot the retained walk
1108
// never declares.
1109
let local: Semantic.Symbols.Symbol mut
1110
1111
if let inlined = closure.inlined_into then
1112
local = inlined.local
1113
else
1114
let scope = Semantic.BLOCK_SCOPE(_symbol_table.current_scope)
1115
1116
// The local-id generator is only in a function frame during
1117
// the declare pass; push one so the LOCAL_VARIABLE ctor can
1118
// mint an id, then give the local a globally-unique IL slot
1119
// name so it can't collide with an enclosing local.
1120
let local_id_generator = IoC.CONTAINER.instance.local_id_generator
1121
1122
local_id_generator.enter_function()
1123
1124
let declared = scope.declare_variable(function.location, param_name, false, null)
1125
1126
local_id_generator.leave_function()
1127
1128
if !isa Semantic.Symbols.Variable(declared) \/ !isa Semantic.Types.SettableTyped(declared) then
1129
return
1130
fi
1131
1132
declared.il_name_override = ".fused_param.{_next_fused_local_id()}"
1133
1134
(cast Semantic.Symbols.Variable(declared)).define()
1135
(cast Semantic.Types.SettableTyped(declared)).set_type(param_type)
1136
1137
local = declared
1138
fi
1139
1140
// Compile the body inline: the literal keeps its own scope, so
1141
// every name in it resolves exactly as it does when the literal
1142
// is compiled as a closure, at every nesting depth. The marker
1143
// is what makes those names load as the method's locals.
1144
closure.inlined_into = (_symbol_table.current_function!, param_symbol, cast Semantic.Symbols.Variable(local))
1145
1146
_reset_nested_captures(body)
1147
1148
let harvest_site = RETRY_SITE_STATS.enter("fusion.harvest")
1149
1150
_symbol_table.enter_scope(closure)
1151
1152
_logger.speculate()
1153
_symbol_use_locations.speculate()
1154
1155
body.expression.walk(self)
1156
1157
let harvested = body.expression.value
1158
1159
_logger.roll_back()
1160
_symbol_use_locations.roll_back()
1161
1162
_symbol_table.leave_scope(closure)
1163
1164
harvest_site.dispose()
1165
1166
// The one late decline. The body compiled to nothing, or to an
1167
// error, in loop context. Nothing after the walk may decline
1168
// without putting the closure back as it was, since the nodes
1169
// now hold loop-context IR and the closure is still the only
1170
// thing that could be called instead.
1171
if !harvested? \/ !harvested.type? \/ harvested.type.is_error then
1172
closure.inlined_into = null
1173
1174
if Driver.COMPILER_ENVIRONMENT.fusion_late_decline then
1175
IO.Std.error.write_line(
1176
"fusion: late decline restoring {function.location}")
1177
fi
1178
1179
let restore_site = RETRY_SITE_STATS.enter("fusion.restore")
1180
1181
_reset_nested_captures(body)
1182
1183
_logger.speculate()
1184
_symbol_use_locations.speculate()
1185
1186
function.walk(self)
1187
1188
_logger.roll_back()
1189
_symbol_use_locations.roll_back()
1190
1191
restore_site.dispose()
1192
1193
return
1194
fi
1195
1196
// The body's IL is emitted into this block by generate-il's
1197
// visit of the literal, and the fused loop emits the block
1198
// where it wants the body. The nodes in the body still carry
1199
// their own placeholders, which that visit fills in place.
1200
let block = IR.Values.BLOCK(harvested.type!)
1201
1202
closure.inlined_block = block
1203
closure.inlined_body_node = body.expression
1204
1205
stage.inlined_closure = closure
1206
1207
stage.param_local = cast Semantic.Symbols.Variable(local)
1208
stage.inline_body = block
1209
si
1210
1211
// Fill in what a fused `join` needs: the interpolation handler the
1212
// elements are written into, the separator evaluated once, and the
1213
// two calls the loop makes per element.
1214
//
1215
// The runtime's `append_to` writes each element as `"{element}"`
1216
// into a StringBuilder, so what has to be reproduced is the
1217
// interpolation rather than a `StringBuilder.append` overload -
1218
// which for a char or a struct would not write the same text. One
1219
// handler serves the whole join, since a run of literals and
1220
// formatted values produces the same characters as a string per
1221
// element appended in turn.
1222
//
1223
// Returns false where the handler's members do not resolve, which
1224
// is the runtime being older than the handler shape this expects.
1225
_build_fused_join(
1226
fused: IR.Values.FUSED_CONSUMER,
1227
call: Trees.Expressions.CALL,
1228
id: int,
1229
result: IR.LOCAL,
1230
element_load: IR.Values.Value,
1231
element_type: Type
1232
) -> bool is
1233
let handler_type = _innate_symbol_lookup.get_interpolated_string_handler_type()
1234
let string_type = _innate_symbol_lookup.get_string_type()
1235
1236
let ctor = _resolve_ctor_by_arity(handler_type, 2)
1237
let append_literal = _resolve_member_by_arity(handler_type, "append_literal", 1)
1238
let to_string_and_clear = _resolve_member_by_arity(handler_type, "to_string_and_clear", 0)
1239
let append_formatted = _resolve_generic_member_by_arity(handler_type, "append_formatted", 1)
1240
1241
if !ctor? \/ !append_literal? \/ !to_string_and_clear? \/ !append_formatted? then
1242
return false
1243
fi
1244
1245
// The two counts are the builder's sizing hints, and neither
1246
// is known before the loop runs.
1247
fused.result_local_type = handler_type
1248
fused.seed_init =
1249
IR.Values.RUNTIME(
1250
IR.RuntimeOperation.NEW_STRING_BUILDER,
1251
IR.Values.Literal.NUMBER(0, _innate_symbol_lookup.get_int_type()),
1252
IR.Values.Literal.NUMBER(0, _innate_symbol_lookup.get_int_type()),
1253
handler_type)
1254
1255
let handler_load = cast IR.Values.Value(IR.Values.Load.TEMP(result, handler_type))
1256
1257
// The caller's own separator, which the recogniser has already
1258
// required: the form that supplies none is declined there.
1259
let separator = call.arguments.expressions[1].value
1260
1261
if !separator? then
1262
return false
1263
fi
1264
1265
let separator_local = IR.LOCAL(".fc_separator.{id}", string_type)
1266
1267
fused.join_separator = separator_local
1268
fused.join_separator_init = separator
1269
fused.join_seen = IR.LOCAL(".fc_seen.{id}", _innate_symbol_lookup.get_bool_type())
1270
1271
fused.join_append_separator =
1272
IR.Values.RUNTIME(IR.RuntimeOperation.APPEND_TEXT, handler_load, separator_local.load(), null)
1273
1274
fused.join_append_element =
1275
IR.Values.RUNTIME(
1276
IR.RuntimeOperation.APPEND_VALUE,
1277
[handler_load, element_load]:IR.Values.Value,
1278
null,
1279
element_type)
1280
1281
fused.finish = IR.Values.RUNTIME(IR.RuntimeOperation.BUILD_STRING, handler_load, string_type)
1282
1283
return true
1284
si
1285
1286
// The one generic overload of `name` on `type` taking `arity`
1287
// arguments, or null.
1288
_resolve_generic_member_by_arity(type: Type, name: string, arity: int) -> Semantic.Symbols.Function? is
1289
let member = type.find_member(name)
1290
1291
if !member? \/ !isa Semantic.Symbols.FUNCTION_GROUP(member) then
1292
return null
1293
fi
1294
1295
let found: Semantic.Symbols.Function? mut = null
1296
1297
for f in (cast Semantic.Symbols.FUNCTION_GROUP(member)).functions do
1298
if f.is_generic /\ f.arguments.count == arity then
1299
if found? then
1300
return null
1301
fi
1302
1303
found = f
1304
fi
1305
od
1306
1307
return found
1308
si
1309
1310
// Whether `type`'s addition is the `add` instruction, which is what
1311
// a fused sum emits. It is, for every integer width and both float
1312
// widths: the compiler emits a bare `add` for each of them, narrow
1313
// ones included. `decimal` and `bigint` are not here because their
1314
// addition is a call to the type's own operator, and neither is a
1315
// type parameter, whose instantiation is not known here.
1316
_is_add_instruction_type(type: Type?) -> bool is
1317
if !type? then
1318
return false
1319
fi
1320
1321
for addable in
1322
[
1323
_innate_symbol_lookup.get_byte_type(),
1324
_innate_symbol_lookup.get_ubyte_type(),
1325
_innate_symbol_lookup.get_short_type(),
1326
_innate_symbol_lookup.get_ushort_type(),
1327
_innate_symbol_lookup.get_int_type(),
1328
_innate_symbol_lookup.get_uint_type(),
1329
_innate_symbol_lookup.get_long_type(),
1330
_innate_symbol_lookup.get_ulong_type(),
1331
_innate_symbol_lookup.get_word_type(),
1332
_innate_symbol_lookup.get_uword_type(),
1333
_innate_symbol_lookup.get_single_type(),
1334
_innate_symbol_lookup.get_double_type()
1335
]
1336
do
1337
if type.matches(addable) then
1338
return true
1339
fi
1340
od
1341
1342
return false
1343
si
1344
1345
// Drop what the literals in a stage body captured, so the walk that
1346
// follows records what the body reads under the rule now in force.
1347
// A capture is keyed on the symbol while the frame field it declares
1348
// is keyed on the name, so a walk that resolves the same name to a
1349
// different symbol than the last one adds a second value under a
1350
// name the frame already has one field for, and the frame's
1351
// constructor is then called with one argument too many. Neither
1352
// walk's captures are part of what the logger rolls back, so each
1353
// walk over the body is preceded by this.
1354
_reset_nested_captures(body: Trees.Bodies.EXPRESSION) is
1355
for nested in NESTED_LITERAL_COLLECTOR().collect(body) do
1356
if let nested_closure = cast Semantic.Symbols.Closure?(_symbol_table.scope_for(nested)) then
1357
nested_closure.reset_captures()
1358
fi
1359
od
1360
si
1361
1362
// Fill the fusion plan's iterator slots for `type`, mirroring
1363
// set_iterator_for: follow $get_iterator once and recurse when a
1364
// reference type has one, else bind move_next / read_current
1365
// directly when `type` is itself an iterator. Returns false if no complete iterator surface is
1366
// found (fall back to the normal loop).
1367
_resolve_fusion_source_iterator(fusion: PIPE_FUSION, type: Type, recursing: bool) -> bool is
1368
if !recursing /\ !type.is_value_type then
1369
if let read_iterator = get_zero_argument_function(type, "$get_iterator") then
1370
fusion.source_read_iterator = read_iterator
1371
1372
return _resolve_fusion_source_iterator(fusion, read_iterator.return_type!, true)
1373
fi
1374
fi
1375
1376
let move_next = get_zero_argument_function(type, "move_next")
1377
1378
if move_next? then
1379
let read_current mut = get_zero_argument_function(type, "$get_current")
1380
1381
if !read_current? then
1382
read_current = get_zero_argument_function(type, "$get_Current")
1383
fi
1384
1385
if !read_current? then
1386
return false
1387
fi
1388
1389
fusion.source_move_next = move_next
1390
fusion.source_read_current = read_current
1391
1392
return true
1393
elif !recursing then
1394
let read_iterator = get_zero_argument_function(type, "$get_iterator")
1395
1396
if read_iterator? then
1397
fusion.source_read_iterator = read_iterator
1398
1399
return _resolve_fusion_source_iterator(fusion, read_iterator.return_type!, true)
1400
fi
1401
fi
1402
1403
return false
1404
si
1405
1406
si
1407
si