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

optimizer: deduplicate fixed columns #2102

Merged
merged 5 commits into from
Nov 22, 2024
Merged

Conversation

Schaeff
Copy link
Collaborator

@Schaeff Schaeff commented Nov 16, 2024

With bus lookups, we insert is_first once per lookup.
Only keep a single column per syntactic value.
Depends on #2103

@Schaeff Schaeff requested a review from chriseth November 16, 2024 15:07
@@ -215,6 +217,39 @@ fn constant_value(function: &FunctionValueDefinition) -> Option<BigUint> {
}
}

/// Deduplicate fixed columns which share the same value.
/// This uses the `Display` implementation of the function value, so `|i| i` is different from `|j| j`
Copy link
Member

@chriseth chriseth Nov 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it that much more complicated to check for syntactic equality? I think Display is very expensive.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can have a go at it, seemed annoying because of sourcemaps.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done!

@Schaeff Schaeff force-pushed the deduplicate-fixed-columns branch 2 times, most recently from 02f041a to d0a95ff Compare November 17, 2024 17:28
@Schaeff Schaeff changed the base branch from main to make-all-source-ref-equal November 17, 2024 17:33
Base automatically changed from make-all-source-ref-equal to main November 21, 2024 10:17
@Schaeff Schaeff force-pushed the deduplicate-fixed-columns branch from eca4b56 to 19e31ac Compare November 21, 2024 11:11
@@ -215,6 +217,44 @@ fn constant_value(function: &FunctionValueDefinition) -> Option<BigUint> {
}
}

/// Deduplicate fixed columns of the same namespace which share the same value.
/// This compares the function values, so `|i| i` is different from `|j| j`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could actually quite easily support |i| i being equal to |j| j, because all references are already the same. The only thing we need to change is the comparison and hash function for Pattern to ignore the variable name.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh great, I was hoping for something like this

Copy link
Collaborator Author

@Schaeff Schaeff Nov 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I'm looking into this, I don't think we should make all Pattern::Variable equal, right?
One level up I was looking at

#[derive(
    Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, JsonSchema, Hash,
)]
pub struct LambdaExpression<E = Expression<NamespacedPolynomialReference>> {
    pub kind: FunctionKind,
    pub params: Vec<Pattern>,
    pub body: Box<E>,
    /// Type of the parameters, filled in during type inference.
    pub param_types: Vec<Type>,
}

Here I would expect body to refer to the parameters by index, in which case it would be the same no matter what the parameters are called. Then we could ignore them in comparisons.
Unless I'm missing something obvious, this seems like it should be a different PR?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean the question is if we want to do that in the "default" comparison function. Is there an easy way to provide a different comparison - probably quite cumbersome to do that recursively, right?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opened #2128 for this

// group symbols by common namespace and function value
.into_group_map_by(|(symbol, value)| {
(
symbol.absolute_name.split("::").next().unwrap(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use SymbolPath::name or at least introduce a helper function somewhere?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah sorry, this is the namespace, not the name.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added function

})
.collect();

// substitute all occurences in expressions.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you also have to substitute in definitions. This has some overlap with #2086.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now substituting also in definitions, however I'm struggling to think of an example where that would apply, the ones I came up with were being optimised away at a previous step.

.into_group_map_by(|(symbol, value)| {
(
symbol.absolute_name.split("::").next().unwrap(),
value.as_ref().unwrap(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the parser also allows fixed columns without value, but I don't know how far we advance for those.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does not seem like this is something we expect, so it would be fine to fail if it does not fail before?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should remove that option from the parser.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chriseth chriseth added this pull request to the merge queue Nov 22, 2024
Merged via the queue into main with commit c45a4d5 Nov 22, 2024
14 checks passed
@chriseth chriseth deleted the deduplicate-fixed-columns branch November 22, 2024 09:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants