sway-ir 0.71.0

Sway intermediate representation.
Documentation
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
//! Lowering of the `init_aggr` instruction.
//!
//! The lowering replaces `init_aggr` with an optimal sequence of
//! instructions like `store`, `mem_clear_val`, `mem_copy_val`, etc.

use std::{collections::HashSet, vec};

use rustc_hash::FxHashMap;

use crate::{
    dominator::{self},
    AnalysisResults, BinaryOpKind, Context, Function, InitAggrInitializer, InsertionPosition,
    InstOp, Instruction, InstructionInserter, IrError, MetadataIndex, Pass, PassMutability,
    Predicate, ScopedPass, Type, TypeContent, Value,
};

pub const INIT_AGGR_LOWERING_NAME: &str = "lower-init-aggr";

pub fn create_init_aggr_lowering_pass() -> Pass {
    Pass {
        name: INIT_AGGR_LOWERING_NAME,
        descr: "Lowering of `init_aggr` instructions",
        deps: vec![],
        runner: ScopedPass::FunctionPass(PassMutability::Transform(init_aggr_lowering)),
    }
}

pub fn init_aggr_lowering<'a, 'b>(
    context: &'a mut Context<'b>,
    _analyses: &AnalysisResults,
    function: Function,
) -> Result<bool, IrError> {
    let root_init_aggrs = find_root_init_aggrs(context, function);
    if root_init_aggrs.is_empty() {
        return Ok(false);
    }

    // Lower each `root_init_aggr` in a most optimized way.
    // Lowering does not remove the `root_init_aggr` instruction yet, nor replaces its uses.
    // This is done after all lowerings are complete, and for that we build the `replace_map`
    // which maps each `root_init_aggr` to the aggregate that it initializes.
    let mut replace_map = FxHashMap::<Value, Value>::default();
    for root_init_aggr in root_init_aggrs.iter() {
        let (root_aggr_ptr, initializers) = deconstruct_init_aggr(context, *root_init_aggr);

        replace_map.insert(*root_init_aggr, root_aggr_ptr);

        let aggr_type = root_aggr_ptr
            .match_ptr_type(context)
            .expect("`root_aggr_ptr` must be a pointer");

        // TODO: (INIT-AGGR) Think of other possible optimizations that bring benefits, if any.
        // Try mostly optimized lowerings first.
        let _ = lower_mostly_zeroed_aggregate()
            || lower_to_stores(
                context,
                *root_init_aggr,
                aggr_type,
                root_aggr_ptr,
                &mut Vec::new(),
                &initializers,
            );
    }

    // Replace all usages of `root_init_aggr`s with the pointers to the aggregates they initialize.
    function.replace_values(context, &replace_map, None);

    // Finally, remove all root `root_init_aggr` instructions.
    function.remove_instructions(context, |inst| root_init_aggrs.contains(&inst));

    Ok(true)
}

/// Deconstructs `init_aggr` into `aggr_ptr` and `initializers`.
fn deconstruct_init_aggr(context: &Context, init_aggr: Value) -> (Value, Vec<InitAggrInitializer>) {
    let Some(Instruction {
        parent: _,
        op: InstOp::InitAggr(init_aggr),
    }) = init_aggr.get_instruction(context).cloned()
    else {
        panic!("`init_aggr` must be an `Instruction` with `op` of variant `InstOp::InitAggr`");
    };

    (
        init_aggr.aggr_ptr,
        init_aggr.initializers(context).collect(),
    )
}

/// This lowering checks whether the aggregate being initialized is mostly zeroed,
/// i.e., whether most of its fields are initialized to zero values.
/// If so, it lowers the `init_aggr` to a `mem_clear_val` for the entire aggregate,
/// followed by `store`s for the non-zero fields.
///
/// E.g., a very common case is initializing tuples like `(0, 0, 0, some_variable)`.
///
/// Returns `true` if the lowering was performed, `false` otherwise.
fn lower_mostly_zeroed_aggregate() -> bool {
    // TODO: (INIT-AGGR) Implement lowering of mostly zeroed aggregates.
    false
}

/// This is the default lowering, run if there are no any optimizations that we can perform.
/// It will flatten the aggregate structure and `store` initial values into individual fields.
/// Array fields might be an exception, depending on the size and the way the array is declared,
/// they might be lowered to `memcpy`s or even loops.
///
/// This function is called recursively for nested `init_aggr`s, starting from a `root_init_aggr`
/// whose aggregate pointer is `root_aggr_ptr`.
///
/// - `init_aggr`: The current `init_aggr` instruction to lower into the root aggregate at the position specified by `gep_indices`.
/// - `aggr_type`: The type of the aggregate initialized by the current `init_aggr`.
/// - `root_aggr_ptr`: The pointer to the root aggregate that is being initialized.
/// - `gep_indices`: The GEP indices to reach the position in the root aggregate where `init_aggr` initializes.
///
/// Returns `true` if the lowering was performed, `false` otherwise.
fn lower_to_stores<'a, 'b>(
    context: &'a mut Context<'b>,
    init_aggr: Value,
    aggr_type: Type,
    root_aggr_ptr: Value,
    gep_indices: &mut Vec<u64>,
    initializers: &[InitAggrInitializer],
) -> bool {
    let init_aggr_metadata = init_aggr.get_metadata(context);
    match aggr_type.get_content(context).clone() {
        TypeContent::Array(arr_elem_type, length) => {
            assert_eq!(
                length as usize,
                initializers.len(),
                "`init_aggr` initializers must match the length of the array type"
            );

            // If all initializers are the same value, we can treat the array as a repeat array.
            // Returns the repeated element and the number of those elements in the array: `[repeated_value; size]`.
            fn as_repeat_array(
                initializers: &[InitAggrInitializer],
            ) -> Option<(InitAggrInitializer, u64)> {
                initializers.split_first().and_then(|(first_init, rest)| {
                    if rest.iter().all(|init| init == first_init) {
                        Some((first_init.clone(), initializers.len() as u64))
                    } else {
                        None
                    }
                })
            }

            match as_repeat_array(initializers) {
                Some((initializer, length)) => {
                    let repeated_value = match initializer {
                        InitAggrInitializer::Value(value) => value,
                        InitAggrInitializer::NestedInitAggr {
                            load: nested_ia_load,
                            init_aggr: nested_init_aggr,
                        } => {
                            // The repeated initializer's value comes from an `init_aggr`.
                            // Note that we could store the entire nested aggregate into the first array element
                            // and then load it from there to initialize the rest of the array elements,
                            // thus eliminating the need for a temporary for the nested aggregate.
                            //
                            // But this actually harm optimization opportunities later on, unlike the case
                            // where we store the initializer into a temporary and then load it from there
                            // to initialize all array elements, which is what we do here.

                            let (nested_aggr_ptr, nested_ia_initializers) =
                                deconstruct_init_aggr(context, nested_init_aggr);

                            // Store the nested aggregate into its original temporary, and not into the root aggregate.
                            // Essentially, we are treating the nested `init_aggr` as a root for the rest of the lowering.
                            let mut gep_indices: Vec<u64> = vec![];

                            let nested_aggr_type = nested_aggr_ptr
                                .match_ptr_type(context)
                                .expect("`nested_aggr_ptr` must be a pointer");

                            lower_to_stores(
                                context,
                                nested_init_aggr,
                                nested_aggr_type,
                                nested_aggr_ptr,
                                &mut gep_indices,
                                &nested_ia_initializers,
                            );

                            // Remove the `nested_init_aggr` and adapt its associated `load`
                            // to load from the `nested_aggr_ptr`.
                            // Note that we do not need to replace uses of the `nested_init_aggr`,
                            // because they are only used in their corresponding `load`,
                            // which we are adapting.
                            let nested_ia_block = nested_init_aggr
                                .get_parent_block(context)
                                .expect(
                                "`nested_init_aggr` is an instruction and must have a parent block",
                            );
                            nested_ia_block.remove_instruction(context, nested_init_aggr);
                            nested_ia_load.replace_instruction_value(
                                context,
                                nested_init_aggr,
                                nested_aggr_ptr,
                            );

                            // The value to use in the array initialization is the load from the nested aggregate.
                            nested_ia_load
                        }
                    };

                    // For large repeating arrays, initialize them in a loop.
                    if length > 5 {
                        let array_ptr = if gep_indices.is_empty() {
                            // The array is the root aggregate, not nested in an other aggregate.
                            root_aggr_ptr
                        } else {
                            // The array is nested in an other aggregate. Calculate its pointer.
                            let inserter =
                                get_inst_inserter_for_before_init_aggr(context, init_aggr);
                            inserter
                                .get_elem_ptr_with_idcs(root_aggr_ptr, aggr_type, gep_indices)
                                .add_metadatum(context, init_aggr_metadata)
                        };

                        generate_array_init_loop(
                            context,
                            array_ptr,
                            arr_elem_type,
                            repeated_value,
                            length,
                            init_aggr,
                            init_aggr_metadata,
                        );
                    } else {
                        // For small repeating arrays, store the `repeated_value` into each element individually.
                        for insert_idx in 0..length {
                            gep_indices.push(insert_idx);

                            let inserter =
                                get_inst_inserter_for_before_init_aggr(context, init_aggr);
                            let gep_val = inserter
                                .get_elem_ptr_with_idcs(root_aggr_ptr, arr_elem_type, gep_indices)
                                .add_metadatum(context, init_aggr_metadata);

                            let inserter =
                                get_inst_inserter_for_before_init_aggr(context, init_aggr);
                            inserter
                                .store(gep_val, repeated_value)
                                .add_metadatum(context, init_aggr_metadata);

                            gep_indices.pop();
                        }
                    }
                }
                None => {
                    // Non-repeating array initializers. Initialize each element individually.
                    for (insert_idx, initializer) in initializers.iter().enumerate() {
                        gep_indices.push(insert_idx as u64);

                        lower_single_initializer_to_stores(
                            context,
                            init_aggr,
                            root_aggr_ptr,
                            gep_indices,
                            init_aggr_metadata,
                            initializer,
                            arr_elem_type,
                        );

                        gep_indices.pop();
                    }
                }
            }
        }
        TypeContent::Struct(field_types) => {
            assert_eq!(
                field_types.len(),
                initializers.len(),
                "`init_aggr` initializers must match the number of fields in the struct type"
            );
            for (insert_idx, (initializer, field_type)) in
                initializers.iter().zip(field_types).enumerate()
            {
                gep_indices.push(insert_idx as u64);

                lower_single_initializer_to_stores(
                    context,
                    init_aggr,
                    root_aggr_ptr,
                    gep_indices,
                    init_aggr_metadata,
                    initializer,
                    field_type,
                );

                gep_indices.pop();
            }
        }
        _ => unreachable!("`aggr_ptr` must point to an array or struct IR type"),
    }

    true
}

fn get_inst_inserter_for_before_init_aggr<'a, 'b>(
    context: &'a mut Context<'b>,
    init_aggr: Value,
) -> InstructionInserter<'a, 'b> {
    let block = init_aggr
        .get_parent_block(context)
        .expect("`init_aggr` is an instruction and must have a parent block");
    InstructionInserter::new(context, block, InsertionPosition::Before(init_aggr))
}

fn lower_single_initializer_to_stores(
    context: &mut Context<'_>,
    init_aggr: Value,
    root_aggr_ptr: Value,
    gep_indices: &mut Vec<u64>,
    init_aggr_metadata: Option<MetadataIndex>,
    initializer: &InitAggrInitializer,
    elem_ty: Type,
) {
    match initializer {
        InitAggrInitializer::Value(value) => {
            // The initializer's value does not come from a nested `init_aggr`.
            // Store the initializer's value directly into the field.
            let inserter = get_inst_inserter_for_before_init_aggr(context, init_aggr);
            let gep_val = inserter
                .get_elem_ptr_with_idcs(root_aggr_ptr, elem_ty, gep_indices)
                .add_metadatum(context, init_aggr_metadata);

            let inserter = get_inst_inserter_for_before_init_aggr(context, init_aggr);
            inserter
                .store(gep_val, *value)
                .add_metadatum(context, init_aggr_metadata);
        }
        InitAggrInitializer::NestedInitAggr {
            load: nested_ia_load,
            init_aggr: nested_init_aggr,
        } => {
            // The initializer's value comes from an `init_aggr` which we want to lower
            // to stores into the root aggregate pointed by `root_aggr_ptr`.

            // We want to write nested `init_aggr`'s fields directly into the root aggregate's field.
            // This means completely removing the need for temporary storage of the nested aggregate,
            // and later `memcpy`ing it into the root aggregate.

            let (nested_aggr_ptr, nested_ia_initializers) =
                deconstruct_init_aggr(context, *nested_init_aggr);

            let inserter = get_inst_inserter_for_before_init_aggr(context, *nested_init_aggr);
            let gep_val = inserter
                .get_elem_ptr_with_idcs(root_aggr_ptr, elem_ty, gep_indices)
                .add_metadatum(context, init_aggr_metadata);

            let nested_aggr_type = nested_aggr_ptr
                .match_ptr_type(context)
                .expect("`nested_aggr_ptr` must be a pointer");

            lower_to_stores(
                context,
                *nested_init_aggr,
                nested_aggr_type,
                root_aggr_ptr,
                gep_indices,
                &nested_ia_initializers,
            );

            // Remove the `nested_init_aggr` and adapt its associated `load`
            // to load from the root aggregate's field pointer.
            // Note that we do not need to replace uses of the `nested_init_aggr`,
            // because they are only used in their corresponding `load`,
            // which we are adapting.
            let nested_ia_block = nested_init_aggr
                .get_parent_block(context)
                .expect("`nested_init_aggr` is an instruction and must have a parent block");
            nested_ia_block.remove_instruction(context, *nested_init_aggr);
            nested_ia_load.replace_instruction_value(context, *nested_init_aggr, gep_val);

            // The original local aggregate will after the lowering be unused and removed
            // later during DCE.
        }
    }
}

/// Find root `init_aggr` instructions in a `function`.
/// These are `init_aggr` instructions that are not nested in other `init_aggr` instructions.
///
/// Returns a vector of [Value]s representing the root `init_aggr` instructions, in post-order.
fn find_root_init_aggrs(context: &Context, function: Function) -> Vec<Value> {
    fn visit_nested_init_aggrs(
        context: &Context,
        parent_initializers: impl Iterator<Item = InitAggrInitializer>,
        nested_init_aggrs: &mut HashSet<Value>,
    ) {
        for initializer in parent_initializers {
            if let InitAggrInitializer::NestedInitAggr {
                load: _,
                init_aggr: init_aggr_val,
            } = initializer
            {
                let Some(Instruction {
                    parent: _,
                    op: InstOp::InitAggr(init_aggr),
                }) = init_aggr_val.get_instruction(context)
                else {
                    unreachable!("`init_aggr` is an `InstOp::InitAggr`");
                };
                nested_init_aggrs.insert(init_aggr_val);
                visit_nested_init_aggrs(
                    context,
                    init_aggr.initializers(context),
                    nested_init_aggrs,
                );
            }
        }
    }

    let mut result = vec![];
    let mut nested_init_aggrs = HashSet::new();

    // Traverse blocks in post-order and their instructions in reverse order.
    let po = dominator::compute_post_order(context, &function);
    for block in po.po_to_block.iter() {
        for inst in block.instruction_iter(context).rev() {
            if let Some(Instruction {
                parent: _,
                op: InstOp::InitAggr(init_aggr),
            }) = inst.get_instruction(context)
            {
                if !nested_init_aggrs.contains(&inst) {
                    // `inst` is a root `init_aggr`. Visit its nested `init_aggr`s.
                    result.push(inst);
                    visit_nested_init_aggrs(
                        context,
                        init_aggr.initializers(context),
                        &mut nested_init_aggrs,
                    );
                }
            }
        }
    }

    result
}

fn generate_array_init_loop(
    context: &mut Context,
    array_ptr: Value,
    elem_type: Type,
    repeated_value: Value,
    length: u64,
    init_aggr: Value,
    md_idx: Option<MetadataIndex>,
) {
    let block = init_aggr
        .get_parent_block(context)
        .expect("`init_aggr` is an instruction and must have a parent block");

    let init_aggr_idx = block
        .instruction_iter(context)
        .position(|v| v == init_aggr)
        .expect("`init_aggr` must be in its parent block");

    let (pre_block, exit_block) = block.split_at(context, init_aggr_idx + 1);

    exit_block.set_label(context, Some("array_init_loop_exit".into()));

    // Create the loop block before the exit block, with a single argument for the loop index.
    let loop_block = pre_block
        .get_function(context)
        .create_block_before(context, &exit_block, Some("array_init_loop".into()))
        .expect("`exit_block` exists in the `pre_block`'s function");
    let index_var_index = loop_block.new_arg(context, Type::get_uint64(context));
    let index = loop_block.get_arg(context, index_var_index).unwrap();

    // Start the loop by branching from the pre_block to the loop_block with index 0.
    let zero = Value::new_u64_constant(context, 0);
    pre_block.append(context).branch(loop_block, vec![zero]);

    // Build the loop block body.

    // 1. Store `repeated_value` into `array_ptr[index]`.
    let gep_val = loop_block
        .append(context)
        .get_elem_ptr(array_ptr, elem_type, vec![index]);
    loop_block
        .append(context)
        .store(gep_val, repeated_value)
        .add_metadatum(context, md_idx);

    // 2. Increment index by one.
    let one = Value::new_u64_constant(context, 1);
    let index_inc = loop_block
        .append(context)
        .binary_op(BinaryOpKind::Add, index, one);

    // 3. Compare index_inc with length to decide whether to continue the loop.
    //    continue = index_inc < length
    let len = Value::new_u64_constant(context, length);
    let r#continue = loop_block
        .append(context)
        .cmp(Predicate::LessThan, index_inc, len);

    // 4. If `continue` then `loop_block(index_inc)` else `exit_block()`.
    loop_block.append(context).conditional_branch(
        r#continue,
        loop_block,
        exit_block,
        vec![index_inc],
        vec![],
    );
}