Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use vec.extract_if(…) for constant lifting #786

Merged
merged 3 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/compiler.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ jobs:
benchmark:
name: Benchmark
runs-on: ubuntu-latest
# Benchmarks on pushes to main should run sequentially.
concurrency: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' && format('{0}-{1}-{2}', github.workflow, github.job, github.ref) || github.run_id }}
steps:
- uses: actions/checkout@v4
- uses: dsherret/rust-toolchain-file@v1
Expand Down
1 change: 1 addition & 0 deletions compiler/frontend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
anonymous_lifetime_in_impl_trait,
box_patterns,
entry_insert,
extract_if,
hasher_prefixfree_extras,
io_error_more,
let_chains,
Expand Down
54 changes: 30 additions & 24 deletions compiler/frontend/src/mir_optimize/constant_lifting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,41 +37,47 @@

use super::current_expression::{Context, CurrentExpression};
use crate::mir::Expression;
use itertools::Itertools;

pub fn lift_constants(context: &mut Context, expression: &mut CurrentExpression) {
let Expression::Function { body, .. } = &mut **expression else {
return;
};

let mut constants = vec![];
let return_value = body.return_value();
let mut new_return_reference_target = None;
let constants = body
.expressions
.extract_if(|(id, expression)| {
let id = *id;

let mut index = 0;
while index < body.expressions.len() {
let (id, expression) = &body.expressions[index];
let id = *id;
if !context.pureness.is_definition_const(expression) {
return false;
}

if !context.pureness.is_definition_const(expression) {
index += 1;
continue;
}
let is_return_value = id == return_value;
if is_return_value && let Expression::Reference(_) = expression {
// Returned references shouldn't be lifted. If we would lift one,
// we'd have to add a reference anyway.
return false;
}

let is_return_value = id == body.return_value();
if is_return_value && let Expression::Reference(_) = expression {
// Returned references shouldn't be lifted. If we would lift one,
// we'd have to add a reference anyway.
index += 1;
continue;
}
// This is a constant and should be lifted.

// This is a constant and should be lifted.
if is_return_value {
// The return value was removed. Add a reference to the lifted
// constant.
new_return_reference_target = Some(id);
}
true
})
.collect_vec();

constants.push(body.expressions.remove(index));

if is_return_value {
// The return value was removed. Add a reference to the lifted
// constant.
body.push(context.id_generator.generate(), Expression::Reference(id));
}
if let Some(new_return_reference_target) = new_return_reference_target {
body.push(
context.id_generator.generate(),
Expression::Reference(new_return_reference_target),
);
}

expression.prepend_optimized(context.visible, constants);
Expand Down