Skip to content

Compiler optimization for ScopedValues #51352

@vchuravy

Description

@vchuravy

It might be beneficial to perform some optimization over scoped values.

In particular it would be beneficial for the value lookup to be coalesced within a dynamical scope.
This would require them to be CSE'd or for GVN (#51120) to be aware of their semantics.

We would need to annotate in the IR the extent of a dynamical scope, since
since ScopedValue are only constant within that region.

token = Expr(:dynamical_scope_enter, args::Pair{ScopedValue, Any}...)
...
Expr(:dynamical_scope_exit, token)

This might be a bit frustrating since currently we have:

current_scope = current_task().scope
current_task().scope = Scope(...)
try
finally
      current_task().scope = current_scope
end

and thus we would introduce a "silent" new form of a try...finally block which seems not ideal.

But maybe a representation like:

old_scope = Expr(:dynamical_scope_enter, args::Pair{ScopedValue, Any}...)
try
finally
    Expr(:dynamical_scope_exit, old_scope)
end

Could still work?

Of course this is predicated on how folks are using ScopedValues in the wild to determine if this would be useful at all.
Java seems to do this kind of optimization + a very small cache.

Metadata

Metadata

Assignees

Labels

compiler:optimizerOptimization passes (mostly in base/compiler/ssair/)help wantedIndicates that a maintainer wants help on an issue or pull request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions