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

THG's substitution applier #334

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
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
20 changes: 15 additions & 5 deletions pytato/target/loopy/codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@
from pytato.transform import Mapper


from loopy.symbolic import IdentityMapper as LoopyIdentityMapper
from pymbolic.mapper.subst_applier import SubstitutionApplier

# set in doc/conf.py
if getattr(sys, "_BUILDING_SPHINX_DOCS", False):
# Avoid import unless building docs to avoid creating a hard
Expand Down Expand Up @@ -108,10 +111,16 @@
"""


def loopy_substitute(expression: Any, variable_assignments: Mapping[str, Any]) -> Any:
from loopy.symbolic import SubstitutionMapper
from pymbolic.mapper.substitutor import make_subst_func
# type-ignore-reason: superclasses have no type information
class LoopySubstitutionApplier(
SubstitutionApplier, LoopyIdentityMapper): # type: ignore
def get_cache_key(self, expr: ScalarExpression,
current_substs: Dict[ScalarExpression, ScalarExpression])\
-> Tuple[Any, ScalarExpression, Any]:
return (type(expr), expr, tuple(sorted(current_substs.items())))


def loopy_substitute(expression: Any, variable_assignments: Mapping[str, Any]) -> Any:
# {{{ early exit for identity substitution

if all(isinstance(v, prim.Variable) and v.name == k
Expand All @@ -121,7 +130,7 @@ def loopy_substitute(expression: Any, variable_assignments: Mapping[str, Any]) -

# }}}

return SubstitutionMapper(make_subst_func(variable_assignments))(expression)
return prim.Substitution(expression, *zip(*variable_assignments.items()))


# SymbolicIndex and ShapeType are semantically distinct but identical at the
Expand Down Expand Up @@ -876,7 +885,8 @@ def add_store(name: str, expr: Array, result: ImplementedResult,
for d in range(expr.ndim))
indices = tuple(prim.Variable(iname) for iname in inames)
loopy_expr_context = PersistentExpressionContext(state)
loopy_expr = result.to_loopy_expression(indices, loopy_expr_context)
loopy_expr = LoopySubstitutionApplier()(
result.to_loopy_expression(indices, loopy_expr_context))

# Make the instruction
from loopy.kernel.instruction import make_assignment
Expand Down
Loading