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

feat: Define Const inline by default, and add a parameter to change the parent #1404

Merged
merged 1 commit into from
Aug 8, 2024
Merged
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
29 changes: 20 additions & 9 deletions hugr-py/src/hugr/dfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,25 +42,29 @@ def define_function(
name: str,
input_types: TypeRow,
type_params: list[TypeParam] | None = None,
parent: ToNode | None = None,
) -> Function:
"""Start building a function definition in the graph.

Args:
name: The name of the function.
input_types: The input types for the function.
type_params: The type parameters for the function, if polymorphic.
parent: The parent node of the constant. Defaults to the root node.

Returns:
The new function builder.
"""
parent_node = parent or self.hugr.root
parent_op = ops.FuncDefn(name, input_types, type_params or [])
return Function.new_nested(parent_op, self.hugr)
return Function.new_nested(parent_op, self.hugr, parent_node)

def add_const(self, value: val.Value) -> Node:
def add_const(self, value: val.Value, parent: ToNode | None = None) -> Node:
"""Add a static constant to the graph.

Args:
value: The constant value to add.
parent: The parent node of the constant. Defaults to the root node.

Returns:
The node holding the :class:`Const <hugr.ops.Const>` operation.
Expand All @@ -71,11 +75,13 @@ def add_const(self, value: val.Value) -> Node:
>>> dfg.hugr[const_n].op
Const(TRUE)
"""
return self.hugr.add_node(ops.Const(value), self.hugr.root)
parent_node = parent or self.hugr.root
return self.hugr.add_node(ops.Const(value), parent_node)

def add_alias_defn(self, name: str, ty: Type) -> Node:
def add_alias_defn(self, name: str, ty: Type, parent: ToNode | None = None) -> Node:
"""Add a type alias definition."""
return self.hugr.add_node(ops.AliasDefn(name, ty), self.hugr.root)
parent_node = parent or self.hugr.root
return self.hugr.add_node(ops.AliasDefn(name, ty), parent_node)


DP = TypeVar("DP", bound=ops.DfParentOp)
Expand Down Expand Up @@ -482,13 +488,17 @@ def add_state_order(self, src: Node, dst: Node) -> None:
# adds edge to the right of all existing edges
self.hugr.add_link(src.out(-1), dst.inp(-1))

def load(self, const: ToNode | val.Value) -> Node:
def load(
self, const: ToNode | val.Value, const_parent: ToNode | None = None
) -> Node:
"""Load a constant into the graph as a dataflow value.

Args:
const: The constant to load, either a Value that will be added as a
child Const node then loaded, or a node corresponding to an existing
Const.
child Const node then loaded, or a node corresponding to an
existing Const.
const_parent: If `const` is a Value, the parent node for the new
constant definition. Defaults to the current dataflow container.

Returns:
The node holding the :class:`LoadConst <hugr.ops.LoadConst>`
Expand All @@ -503,7 +513,8 @@ def load(self, const: ToNode | val.Value) -> Node:
LoadConst(Bool)
"""
if isinstance(const, val.Value):
const = self.add_const(const)
const_parent = const_parent or self.parent_node
const = self.add_const(const, parent=const_parent)
const_op = self.hugr._get_typed_op(const, ops.Const)
load_op = ops.LoadConst(const_op.val.type_())

Expand Down
Loading