MinIR rewriting, operationally

3.5. MinIR rewriting, operationally

The previous section proposed to view minIR rewrites as the result of a (DPO or SqPO) graph transformation. This yields valid rewriting semantics elegantly (and with little effort!). However, the conditions that must be imposed on the transformation to be valid, along with the fact that pushouts may not exist mean that the existence of a rewrite given a transformation rule and a match is not guaranteed.

In this section, we address this by considering a more restricted notion of minIR rewriting, for which the existence of the right-hand side of the rewrite is guaranteed. In addition, in place of the categorical presentation of the last section, we express the rewriting operation operationally, i.e. as data and a procedure on sets that translates directly into an algorithmic implementation.

We find that this rewrite definition is sufficient in practice. We conclude the section with an example of how more complex rewrites can be achieved by composition of simpler rewrites that can be expressed in this framework.

Graph glueings and rewrites #

Throughout, we consider graph glueings on disjoint vertex and (hyper)edge sets. To underline this, we will use the \sqcup symbol to denote disjoint set unions.

As we will be working exclusively with vertex and edge sets in this section (as opposed to the objects in the indexing category), we will drop the bold typeface for sets, writing e.g. VV instead of V\mathbf V for the set of vertices of a hypergraph.

Finally, all minIR graphs in this section are IO free.

We define local graph rewrites in terms of graph glueings. Consider first the case of two arbitrary graphs G1=(V1,E2)G_1 = (V_1, E_2) and G2=(V2,E2)G_2 = (V_2, E_2), along with a relation μ V1×V2\mu\ \subseteq V_1 \times V_2. Let μ (V1V2)2\sim_\mu \ \subseteq (V_1 \sqcup V_2)^2 be the equivalence relation induced by μ\mu, i.e. the smallest relation on V1V2V_1 \sqcup V_2 that is reflexive, symmetric and transitive, and satisifes for all v1V1v_1 \in V_1 and v2V2v_2 \in V_2,

(v1,v2)μv1μv2.(v_1, v_2) \in \mu \Rightarrow v_1 \sim_\mu v_2.

Then, we can define

  • V=(V1V2)/μV = (V_1 \sqcup V_2)/\sim_\mu is the set of all equivalence classes of μ\sim_\mu, and
  • for vV1V2v \in V_1 \sqcup V_2, αμ(v)V\alpha_\mu(v) \in V is the equivalence class of μ\sim_\mu that vv belongs to.
Definition 3.8Graph glueing

The glueing of G1G_1 and G2G_2 according to the glueing relation μ\mu is given by the vertices V=(V1V2)/μV = (V_1 \sqcup V_2)/\sim_\mu and the edges

E={(αμ(u),αμ(v))(u,v)E1E2}V2.E = \{(\alpha_\mu(u), \alpha_\mu(v)) \mid (u,v) \in E_1 \sqcup E_2 \} \subseteq V^2.

We write the glueing graph as (G1G2)/μ(G_1 \sqcup G_2) / \sim_\mu.

In other words, the glueing is the disjoint union of the two graphs, with identification (and merging) of vertices that are related in μ\mu.

This allows us to define a rewrite on a graph GG:

Definition 3.9Graph rewrite

A rewrite rr on a graph G=(V,E)G = (V, E) is given by a tuple r=(GR,V,E,μ)r = (G_R, V^-, E^-, \mu), with

  • GR=(VR,ER)G_R = (V_R, E_R) is a graph called the replacement graph,
  • VVV^- \subseteq V is the vertex deletion set,
  • EEdom(μ)2E^- \subseteq E \cap dom(\mu)^2 is the edge deletion set, and
  • μ:VVR\mu: V^- \rightharpoonup V_R is the glueing relation, a partial function that maps a subset of the deleted vertices of GG to vertices in the replacement graph.

The domain of definition dom(μ)dom(\mu) is known as the boundary values of rr.

A graph rewrite per this definition can always be generated by a single pushout (SPO) transformation Löwe, 1991Michael Löwe. 1991. Extended algebraic graph transformation. Retrieved from http://d-nb.info/910935696.

  • define LL as the graph (V,E)(V^-, E^-). Then the injection LGL \subseteq G is the match morphism LGL \to G;
  • the partial map μ\mu maps a subset of VV^- to vertices in the replacement R=GRR = G_R. By injectivity of the match morphism, it also defines a partial map LRL \rightharpoonup R.

We opted for SPO-like semantics in this definition, as they are the simplest to write in set-theoretic terms and coincide with DPO and SqPO in our restricted domain of interest.

The result of the rewrite is computed by gluing the right-hand side GRG_R to the context subgraph GC=(VC,EC)G_C = (V_C, E_C) of GG given by

VC=(VV)  dom(μ)EC=(EE)  VC2.\begin{aligned}V_C &= (V \smallsetminus V^-) \ \cup\ dom(\mu)\\E_C &= (E \smallsetminus E^-)\ \cap\ V_C^2.\end{aligned}

The partial function μ\mu is a special case of a glueing relation μVC×VR\mu \subseteq V_C \times V_R, and thus defines a glueing of GCG_C with GRG_R. The rewritten graph resulting from applying rr to GG is r(G)=(GCGR)/μ.r(G) = (G_C \sqcup G_R) / \sim_\mu.

An example of a graph rewrite is given in the next figure. This is equivalent to an SPO transformation with the graph induced by VV^- on the left-hand side, the graph GRG_R on the right-hand side and the partial map LRL \hookrightarrow R given by μ\mu.

Application of a graph rewrite. On the left, the original graph GGG along with the replacement graph GRG_RGR​ (grey box). On the right, the rewritten graph r(G)r(G)r(G). Only the vertex ggg has been deleted, as other vertices in V−V^-V− are in the boundary dom(μ)dom(\mu)dom(μ) (in orange). The (singleton) edge deletion set is red. The blue edge connects a vertex of V∖V−V \smallsetminus V^-V∖V− to a boundary vertex, and is thus also present on the right-hand side. The purple edge, on the other hand, connects a vertex of V∖V−V \smallsetminus V^-V∖V− to a non-boundary vertex of V−V^-V−, and is thus deleted.

Application of a graph rewrite. On the left, the original graph GG along with the replacement graph GRG_R (grey box). On the right, the rewritten graph r(G)r(G). Only the vertex gg has been deleted, as other vertices in VV^- are in the boundary dom(μ)dom(\mu) (in orange). The (singleton) edge deletion set is red. The blue edge connects a vertex of VVV \smallsetminus V^- to a boundary vertex, and is thus also present on the right-hand side. The purple edge, on the other hand, connects a vertex of VVV \smallsetminus V^- to a non-boundary vertex of VV^-, and is thus deleted.

When there are no edges between VVV \smallsetminus V^- and Vdom(μ)V^- \smallsetminus dom(\mu) (purple in the example above), this definition corresponds to graph rewrites that can be produced using DPO transformations (see discussion in section 3.4). Otherwise, such edges are deleted.

The notions of graph glueing and graph rewrite can straightforwardly be lifted to hypergraphs and, by extension, to minIR graphs. Notice that in this case, values are glued together, not operations (the former were defined as the graph’s vertices, the latter as its hyperedges).

However, the glueing of two valid minIR graphs – and the result of applying a valid rewrite – may not be a valid minIR graph. Glueing two values of a linear type, for instance, is a sure way to introduce multiple uses (or definitions) of it. Thus, we must be careful to only consider glueings and rewrites of minIR graphs that preserve all the constraints we have imposed in Definition 3.4.

Ensuring rewrite validity: interfaces #

As a sufficient condition for valid minIR rewrites, we introduce minIR interfaces, a concept closely related to the “hypergraph with interfaces” construction of Bonchi, 2017Filippo Bonchi, Fabio Gadducci, Aleks Kissinger, Paweł Sobociński and Fabio Zanasi. 2017. Confluence of Graph Rewriting with Interfaces or the supermaps of quantum causality Hefford, 2024James Hefford and Matt Wilson. 2024. A Profunctorial Semantics for Quantum Supermaps. In Proceedings of the 39th Annual ACM/IEEE Symposium on Logic in Computer Science, July 2024. ACM, 1--15. doi: 10.1145/3661814.3662123. We eschew the presentation of holes as a slice category in favour of a definition that fits naturally within minIR and is sufficient for our purposes.

Let GG be a Σ\Sigma-typed minIR graph with data types TT and linear types TLTT_L \subseteq T. Consider type strings S,STS, S' \in T^\ast. We define the index sets

Idx(S)={iN1iS}IdxL(S)={iIdx(S)SiTL}Idx(S)\begin{aligned}\mathrm{Idx}(S) &= \{i \in \mathbb{N} \mid 1 \leq i \leq |S|\}\\\mathrm{Idx}_L(S) &= \{i \in \mathrm{Idx}(S) \mid S_i \in T_L\} \subseteq \mathrm{Idx}(S)\end{aligned}

corresponding respectively to the set of all indices into SS and the subset of indices of linear types. For any iIdx(S)i \in \mathrm{Idx}(S), we denote by SiS_i the type at position i in SS.

We define a partial order \preccurlyeq1 on TT^\ast where SSS \preccurlyeq S' and say that SS' can be coerced into SS if there exists an index map ρ:Idx(S)Idx(S)\rho: \mathrm{Idx}(S) \to \mathrm{Idx}(S') such that

  • types are preserved: Si=Sρ(i)S_i = S'_{\rho(i)}, and
  • ρ\rho is well-defined and bijective on the restriction to indices of linear types
    ρIdxL(S):IdxL(S)IdxL(S).\left.\rho\right|_{\mathrm{Idx}_L(S)}: \mathrm{Idx}_L(S) \to \mathrm{Idx}_L(S').
Definition 3.10Interface

Let TT be a set of data types. An interface I=(U,D)I = (U, D) is a pair of type strings U,DTU, D \in T^\ast.

We say that an interface I=(U,D)I' = (U', D') can be coerced into an interface I=(U,D)I = (U, D), written III \triangleleft I', if UUU \succcurlyeq U' and DDD \preccurlyeq D'.

We can define the interface associated with an operation oo in a minIR graph GG by considering the values used and defined by oo. Calling τ\tau the type morphism on GG and assuming oEsto \in E_{st} to be an operation in GG with ss inputs and tt outputs, we define the interface of oo in GG as the pair of strings in TT^\ast

I(o)=(τ(src1(o))τ(srcs(o)),τ(tgt1(o))τ(tgtt(o))).I(o) = (\tau(\textit{src}_1(o))\cdots\tau(\textit{src}_s(o)), \tau(\mathit{tgt}_1(o))\cdots\tau(\mathit{tgt}_t(o))).

Similarly, we can assign interfaces to subgraphs of minIR graphs:

Definition 3.11MinIR subgraph

Consider a subset of values and operations VHVV_H \subseteq V and EHEE_H \subseteq E. Define the use and define boundary sets

BU={vVHdef(v)EEH},BD={vVHuse(v)EEH}.\begin{aligned} B_U &= \{v \in V_H \mid \mathit{def}\,(v) \in E \smallsetminus E_H \},\\B_D &= \{v \in V_H \mid use(v) \in E \smallsetminus E_H \}.\end{aligned}

The tuple H=(VH,EH)H = (V_H, E_H) of GG is called a minIR subgraph of GG if there exists a region RR of GG such that all boundary values of HH are in RR:

B=BUBDR. B= B_U \cup B_D \subseteq R.

We write HGH \subseteq G to indicate that HH is a minIR subgraph of GG.

Note that BUB_U is exactly the set of inputs II in the non-IO free minIR graph given by the subgraph (VH,EH)(V_H, E_H) of the minIR graph. BDB_D is a superset of the outputs OO of HH: it includes all linear values in HH that do not have a use in HH, but also any non-linear value that has a use outside of HH.

Unlike interfaces, subgraph boundary values are not ordered. An ordering of BVB \subseteq V is a string SVS \in V^\ast along with a bijective map

ord:BIdx(S)such thatv=Sord(v).\mathrm{ord}: B \to \mathrm{Idx}(S) \quad\textrm{such that}\quad v = S_{\mathrm{ord}(v)}.

If there are strings SU,SDVS_U, S_D \in V^\ast and orderings of BUB_U and BDB_D

ordU: BUIdx(SU)ordD: BDIdx(SD),\begin{aligned}\textrm{ord}_U:\ &B_U \to \mathrm{Idx}(S_U)&\quad\textrm{ord}_D:\ &B_D \to \mathrm{Idx}(S_D),\end{aligned}

then we can set srci(H)=(SU)i\textit{src}_i\,(H) = (S_U)_i and tgti(H)=(SD)i\textit{tgt}_i\,(H) = (S_D)_i in complete analogy to operations. We will write src(H)\textit{src}(H) and tgt(H)\textit{tgt}(H) for the strings src1(H)srcSU(H)\textit{src}_1(H)\cdots\textit{src}_{|S_U|}(H) and tgt1(H)tgtSD(H)\textit{tgt}_1(H)\cdots\textit{tgt}_{|S_D|}(H) respectively. We say that the subgraph HH implements the interface

IH=(τ(src(H)),τ(tgt(H)),I_H = (\tau(\textit{src}(H)), \tau(\mathit{tgt}(H)),

where the type morphism τ\tau was extended element-wise to strings VV^\ast.

Remark, though, that unlike operations, the same subgraph may implement more than one interface as a result of various choices of orderings ordU\textrm{ord}_U and ordD\textrm{ord}_D.

As mentioned, the subgraph HH forms a non-IO free minIR graph. We can always construct an IO-free minIR graph from HH by adding two operations oino_{in} and oouto_{out} in the root region respectively in E0,SUE_{0, |S_U|} and ESD,0E_{|S_D|, 0} inputs-outputs, defined by

tgti(oin)=srci(H),srci(oout)=tgti(H). \textit{tgt}_i\,(o_{in}) = \textit{src}_i(H),\quad\quad \textit{src}_i(o_{out}) = \textit{tgt}_i\,(H).

We call the resulting graph Hˉ\bar{H} an interface graph. It implements the interface IHI_H if HH implements IHI_H. Calling to mind the illustrations of section 3.3, Hˉ\bar{H} looks like one of the nested regions within regiondef operations that we were considering.

MinIR operation rewrite #

Consider

  • an operation oo in a minIR graph GG with values V,V,
  • an interface graph Hˉ\bar{H} with values VHV_H and its associated subgraph HHˉH \subseteq \bar{H}, such that HH implements an interface I(o)IH,I(o) \triangleleft I_H,
  • the index maps ρ:Idx(src(H))Idx(src(o))\rho: \mathrm{Idx}(\textit{src}(H)) \to \mathrm{Idx}(\textit{src}(o)) and σ:Idx(tgt(o))Idx(tgt(H))\sigma: \mathrm{Idx}(\textit{tgt}\,(o)) \to \mathrm{Idx}(\textit{tgt}\,(H)) that define the generalisation I(o)IHI(o) \triangleleft I_H (per Definition 3.10).

We can define a glueing relation μoV×VH\mu_o \subseteq V \times V_H

μo= {(srcρ(i)(o),srci(H))iIdx(src(H))} {(tgti(o),tgtσ(i)(H))iIdx(tgt(o))}.\begin{aligned}\mu_o =\ & \{ \left(\textit{src}_{\rho(i)}(o), \textit{src}_{i}(H)\right) \mid i \in \mathrm{Idx}(\textit{src}(H)) \}\ \cup \\& \{ \left(\mathit{tgt}_{i}\,(o), \mathit{tgt}_{\sigma(i)}(H)\right) \mid i \in \mathrm{Idx}(\textit{tgt}\,(o)) \}.\end{aligned}

This is almost enough to define a rewrite that replaces the operation oo in GG with the values and operations of HH – the interface compatibility constraint I(o)IHI(o) \triangleleft I_H that we have imposed ensures that the resulting minIR graph is valid. Unfortunately, μo\mu_o is not a partial function as required by Definition 3.4.

This is resolved in the following proposition:

Proposition 3.5MinIR operation rewrite

Let GG, oo and HH such that I(o)IHI(o) \triangleleft I_H, as defined above. Then

((GH)/μo ⁣){o},\big((G \sqcup H) / \sim_{\mu_o}\!\big) \smallsetminus \{o\},

i.e. the graph obtained by removing the operation oo from the glueing of GG and HH along μo\mu_o, is a valid minIR graph.

There is a graph GRG_R with values VRV_R and a partial function μo:VVR\mu_o': V \rightharpoonup V_R such that the graph (5) is the graph ro(G)r_o(G), obtained from the rewrite

ro=(GR,dom(μo),{o},μo).r_o = (G_R, dom(\mu_o), \{o\}, \mu_o').

We call ror_o the rewrite of oo into HH.

The definition of the rewrite of oo into a graph HH behaves as one would expect – the only subtleties relate to handling non-linear (i.e. copyable) values at the boundary of the rewrite. The following example illustrates some of these edge cases.

Rewriting operation ooo in the graph GGG (top left) into the operations o1o_1o1​ and o2o_2o2​ of the graph Hˉ\bar{H}Hˉ (bottom left). Coloured dots indicate the index maps ρ\rhoρ and σ\sigmaσ from inputs BUB_UBU​ of Hˉ\bar{H}Hˉ to inputs of ooo, respectively from outputs of ooo to outputs BDB_DBD​ of Hˉ\bar{H}Hˉ.

Rewriting operation oo in the graph GG (top left) into the operations o1o_1 and o2o_2 of the graph Hˉ\bar{H} (bottom left). Coloured dots indicate the index maps ρ\rho and σ\sigma from inputs BUB_U of Hˉ\bar{H} to inputs of oo, respectively from outputs of oo to outputs BDB_D of Hˉ\bar{H}.

When the index maps ρ\rho and σ\sigma are not injective (yellow and green dots), values are merged, resulting in multiple uses of the value (i.e. copies). This is why the index maps must be injective on linear values (dots in shades of blue). Value merging also happens when a value is used multiple times in oo (yellow and red dots). This will never happen with linear values (as they can never have more than one use in oo), nor with any value definitions (the same value can never be defined more than once). Finally, values not in the image of ρ\rho or σ\sigma (purple dot) are discarded. This case is also excluded for linear values by requiring surjectivity.

Proof

We start this proof with the explicit construction of GRG_R and μo\mu_o'. Define R(VH)2\sim_R \subseteq (V_H)^2 as the smallest equivalence relation such that

srcρ(i)(o)=srcρ(j)(o)tgti(oin)Rtgtj(oin).\textit{src}_{\rho(i)}(o) = \textit{src}_{\rho(j)}(o) \Rightarrow \textit{tgt}_i\,(o_{in}) \sim_R \textit{tgt}_j\,(o_{in}).

Then we define GˉR=Hˉ/R\bar{G}_R = \bar{H} / \sim_R, the graph obtained by glueing together values within the same equivalence class of R\sim_R.

Claim 1: GˉR\bar{G}_R is a valid minIR graph.

Claim 1 follows from the observation that only values of non-linear types are glued together. If vRvv \sim_R v', then either v=vv = v' or there exist iji \neq j such that

tgti(oin)Rtgtj(oin).\textit{tgt}_i\,(o_{in}) \sim_R \textit{tgt}_j\,(o_{in}).
If ρ(i)=ρ(j)\rho(i) = \rho(j), then ρ\rho is not injective on ii and jj, and by the definition of ρ\rho, τ(v)TL\tau(v)\notin T_L and τ(v)TL\tau(v') \notin T_L. Otherwise, there are i=ρ(i)srcρ(j)(o)=ji' = \rho(i) \neq \textit{src}_{\rho(j)}(o) = j' such that srci(o)=srcj(o)\textit{src}_{i'}(o) = \textit{src}_{j'}(o). The same value is used twice, which is only a valid minIR graph if vv and vv' are not linear, thus proving Claim 1.

Define GRG_R as the subgraph obtained from GˉR\bar{G}_R by removing the operations {oin,oout}\{o_{in}, o_{out}\}. Let VR=VH/RV_R = V_H / \sim_R be the set of values of GRG_R (and of GˉR\bar{G}_R). Writing αR(v)VR\alpha_R(v) \in V_R for the equivalence class of R\sim_R that vVHv \in V_H belongs to, we can define μoV×VR\mu_o' \in V \times V_R as:

(v,w)μo(v,αR(w))μo.(v, w) \in \mu_o \Leftrightarrow (v, \alpha_R(w)) \in \mu_o'.

Claim 2: μo\mu_o' is a partial function VVRV \rightharpoonup V_R.

In other words, for all (v,α1),(v,α2)μo(v, \alpha_1), (v, \alpha_2) \in \mu_o', then α1=α2\alpha_1 = \alpha_2. Let w1α1w_1 \in \alpha_1 and w2α2w_2 \in \alpha_2 be values in VHV_H. First of all, srci(o)tgtj(o)\textit{src}_i(o) \neq \textit{tgt}_j\,(o) for all i,ji, j, otherwise GG is not acyclic. So either use(v)=ouse(v) = o, or def(v)=o\textit{def}(v) = o, but not both.

The simpler case: if def(v)=o\textit{def}\,(v) = o, then there exists ii such that tgt(o)i=v\textit{tgt}\,(o)_i = v. Furthermore ii is unique because by minIR definition, vv has a unique definition in GG. It follows from (4) that w1=srcρ(i)(oout)=w2w_1 = \textit{src}_{\rho(i)}(o_{out}) = w_2 and hence α1=α2\alpha_1 = \alpha_2.

Otherwise, there exists ii and jj such that v=srcρ(i)(o)=srcρ(j)(o)v = \textit{src}_{\rho(i)}(o) = \textit{src}_{\rho(j)}(o) and tgti(oin)=w1\textit{tgt}_i\,(o_{in}) = w_1 as well as tgtj(oin)=w2\textit{tgt}_j\,(o_{in}) = w_2. By definition of R\sim_R, we have wRww \sim_R w', and thus

α1=αR(w1)=αR(w2)=α2,\alpha_1 = \alpha_R(w_1) = \alpha_R(w_2) = \alpha_2,

proving Claim 2.

Claim 3: ro(G)r_o(G) is given by ((GH)/μo){o}((G \sqcup H) / \sim_{\mu_o}) \smallsetminus \{o\}.

It follows directly from our construction of R\sim_R and μo\mu_o' that the equivalence classes of (the smallest equivalence relation closure of) μoαR\mu_o' \circ \alpha_R is equal to the equivalence classes of (the smallest equivalence relation closure of) μo\mu_o. The claim follows by Definition 3.8 and the definition of ror_o.

And finally, Claim 4: ro(G)r_o(G) is a valid minIR graph.

Per Definition 3.4, We must check four properties: (i) every value is defined exactly once, (ii) every linear value is used exactly once, (iii) the graph is acyclic, and (iv) every region has (at most) one parent.

(iii) follows from the fact that GG and HH are acyclic and a single operation oo in GG is replaced: any cycle across GG and HH would also be a cycle in GG by replacing the subpath in HH with oo. (iv) follows from the fact that oino_{in} and oouto_{out} are in the root region of Hˉ\bar{H}, by definition of interface implementation. (i): removing oo from GG removes the unique definitions of all values that are targets of oo. Each such value vv is glued to a unique value srci(oout)\mathit{src}_i\,(o_{out}) in HH – the new and unique definition of vv in ro(G).r_o(G). (ii) follows from the same argument as in (i), but relying on injectivity of ρ\rho on linear values to establish uniqueness.

Arbitrary minIR rewrites #

We have so far defined rewrites of single operations into graphs HH. We can generalise these rewrites to rewrite subgraphs PGP \subseteq G, provided the minIR subgraphs satisfy some constraints. We require for this a notion of convexity, as discussed in Bonchi, 2022Filippo Bonchi, Fabio Gadducci, Aleks Kissinger, Pawel Sobocinski and Fabio Zanasi. 2022. String diagram rewrite theory II: Rewriting with symmetric monoidal structure. Mathematical Structures in Computer Science 32, 4 (April 2022, 511--541). doi: 10.1017/s0960129522000317.

As usual, let us consider a minIR graph GG with values VV, linear values VLVV_L \subseteq V, edges EE, the incidence maps srci\textit{src}_i and tgtj\textit{tgt}_j as well as their inverses use\textit{use} and def\textit{def}. Consider further a subgraph of GG that we will now call P=(VP,EP)GP = (V_P, E_P) \subseteq G, to distinguish from HH.

Let us further define the partial parentparent morphism that maps a value vVv \in V to the parent of the region of vv.

Definition 3.12Convex minIR subgraph

A minIR subgraph PGP \subseteq G is convex if the following conditions hold:

  • for all v1,v2VPv_1, v_2 \in V_P, any path along \leadsto from v1v_1 to v2v_2 contains only vertices in VPV_P,
  • parent-child relations are contained within the subgraph, i.e.
    vVPdom(parent)parent(v)VP.v \in V_P \cap dom(\mathit{parent}) \Leftrightarrow \mathit{parent}(v) \in V_P.

Define the sets of boundary values BU,BDB_U, B_D and B=BUBDB = B_U \cup B_D, as in (2); then fix the boundary orderings src(P)\textit{src}(P) and tgt(P)\textit{tgt}\,(P) as in (3). The subgraph PP implements the interface

Consider an interface graph Hˉ\bar{H} that implements IHI_H such that IPIHI_P \triangleleft I_H. Instead of defining a gluing relation from values of an operation oo to values of HH, we replace the interface I(o)I(o) with IPI_P. This generalises the definition of μo\mu_o from (4) to a glueing μB×VH\mu\subseteq B \times V_H defined as

μ= {((srcρ(i)(P)),srci(H))iIdx(src(H))} {((tgti(P)),tgtσ(i)(H))iIdx(tgt(P))},\begin{aligned}\mu =\ & \{ \left((\textit{src}_{\rho(i)}(P)), \textit{src}_{i}\,(H)\right) \mid i \in \mathrm{Idx}(\textit{src}(H)) \}\ \cup \\& \{ \left((\textit{tgt}_{i}\,(P)), \textit{tgt}_{\sigma(i)}\,(H)\right) \mid i \in \mathrm{Idx}(\textit{tgt}\,(P)) \},\end{aligned}

With the set of boundary operations defined as2

EB={oEP(tgt(o)src(o))B},E_B = \left\{o \in E_P \mid \left(\mathit{tgt}\,(o) \cup \textit{src}(o)\right) \subseteq B\right\},

we are able to define minIR rewrites in their most general form.

Proposition 3.6MinIR subgraph rewrite

Let PGP \subseteq G and HH such that IPIHI_P \triangleleft I_H and PP is convex, as defined above. Then,

((GH)/μ ⁣)(VPB,EB),\big((G \sqcup H) / \sim_{\mu}\!\big) \smallsetminus (V_P \smallsetminus B, E_B),

i.e. the graph obtained by removing the values VPBV_P \smallsetminus B and operations EBE_B from the glueing of GG and HH along μ\mu, is a valid minIR graph.

There is a graph GRG_R with values VRV_R and a partial function μ:VPVR\mu': V_P \rightharpoonup V_R such that the graph (8) is the graph rP(G)r_P(G), obtained from the rewrite

rP=(GR,VP,EB,μ).r_P = (G_R, V_P, E_B, \mu').

We call rPr_P the rewrite of PP into HH.

Proof

Consider an operation oo that implements IP=(UP,DP)I_P = (U_P, D_P). We can define the interface graph Hˉo\bar{H}_o given by three operations oino_{in}, oouto_{out} and oo. Its associated subgraph HoHˉoH_o \subseteq \bar{H}_o only includes oo. Let μ~\tilde \mu be the glueing relation

μ~= {(srci(P),srci(o))iIdx(UP)} {(tgti(P),tgti(o))iIdx(DP)}.\begin{aligned}\tilde \mu =\ &\{ (\textit{src}_i(P), \textit{src}_i(o)) \mid i \in \mathrm{Idx}(U_P) \}\ \cup \\& \{ (\textit{tgt}_i\,(P), \textit{tgt}_i\,(o)) \mid i \in \mathrm{Idx}(D_P) \}.\end{aligned}

Consider the rewrite r=(Ho,VPB,EB,μ~)r = (H_o, V_P \smallsetminus B, E_B, \tilde\mu). If we write G=(V,E)G' = (V', E') for the subgraph of GG given by

V=(V(VPB))E=(EEB)(V),\begin{aligned}V' &= (V \smallsetminus (V_P \smallsetminus B)) \\E' &= (E \smallsetminus E_B) \cap (V')^\ast,\end{aligned}
then according to (1), the graph resulting from applying rr to GG can be expressed as the glueing

Go=r(G)=(GHo)/μ~.G_o = r(G) = (G' \sqcup H_o) / \sim_{\tilde \mu}.

Our claim is that GoG_o is a valid minIR graph.

The graph (8) is then obtained by applying the rewrite ror_o as given by (6) to GoG_o. Defining the rewrite rPr_P as the composition of rr followed by ror_o, the result follows from our claim and Proposition 3.5.

We now prove the claim, by showing the four properties of minIR graphs as per Definition 3.4. Property i) requires showing that every value is defined exactly once. As GG' is obtained by removing values and operations from a valid minIR graph GG, no value in VV' can be defined more than once. A value vVv \in V' that is not defined in GG' must be in the boundary vBv \in B of PP. By the boundary definitions of (2), vv cannot be in BUB_U and thus must be in BDB_D. It follows by the definition of the glueing μ~\tilde \mu that in GoG_o, vv will be in the definitions of oo: def(v)=o\textit{def}(v) = o. The glueing μ~\tilde \mu is bijective between the values of PP and oo and thus we can conclude that vv has a unique definition in GoG_o.

The same argument applies to property ii). Property iii) follows from the convexity requirement of PP. Finally, property iv) (every region has at most one parent) follows from two observations. First, by convexity of PP, no deleted value or operation could be the parent of any value not in PP, and thus the parentparent relation is well-defined on GG': im(parent)Eim(parent) \subseteq E'. Secondly, all new values and operations added to the boundary region of GG' are from the root region of HH, and thus do not have a parent, ensuring that parent uniqueness is preserved.

This simple and limited graph transformation framework captures a remarkably large set of minIR program transformations. It may seem at first that the restriction to boundary values within a single region of Definition 3.11, as well as the convexity requirements of Definition 3.12 represent significant limitations on the expressivity of the rewrites. In practice, however, the semantics of minIR operations can be used to decompose more complex rewrites into a sequence of simple rewrites to which Proposition 3.6 applies.

Consider minIR graphs with a type system that includes regiondef and call operations as discussed in examples of the previous section – respectively defining a code block by a nested region and redirecting control flow to a code block defined using a regiondef. Then all constraints that we impose on rewriting can be effectively side-stepped using the region outlining and value hoisting transformations.

Region outlining moves a valid minIR subgraph into its own separate region, and replaces the hole left by the subgraph in the computation by a call operation to the newly outlined region.

Value hoisting moves a value definition within a region to its parent region and passes the value down to the nested region through an additional input. In case of linear values, we can similarly hoist the unique use of the value to the parent region.

Using these transformations, non-convex subgraphs can always be made convex by taking the convex hull and outlining any parts within it that are not part of the subgraph. Outlined regions can then be passed as additional inputs to the subgraph. Step 1 of the figure below illustrates this transformation. Similarly, a subgraph that includes operations without their parent can be extended to cover the entire region and its parent, outlining any parts of the region that are not part of the subgraph.

Finally, whenever a boundary value vv belongs to a region that is not the top level region of the subgraph3, we can repeatedley hoist vv to its parent region until it is in the top level region. The value is then recursively passed as argument to descendant regions until the region that it is required in. Subgraphs can thus always be transformed to only have input and output boundary values at the top level region. Step 2 of the figure below illustrates this transformation.

A non-convex minIR graph rewrite, obtained by decomposition into valid convex rewrites, using outlining and hoisting. For simplicity, regiondef operations were made implicit and represented by nested boxes: a region within an operation corresponds to a region definition that is passed as an argument to the operation. Edge colours correspond to value types. Step 1 outlines the ... operations into a dedicated region, which step 2 hoists outside of the region being rewritten. Step 3 and 4 together correspond to a minIR sugraph rewrite. They have been split into two steps following the proof strategy. Step 4 is an instance of a minIR operation rewrite.

A non-convex minIR graph rewrite, obtained by decomposition into valid convex rewrites, using outlining and hoisting. For simplicity, regiondef operations were made implicit and represented by nested boxes: a region within an operation corresponds to a region definition that is passed as an argument to the operation. Edge colours correspond to value types. Step 1 outlines the ... operations into a dedicated region, which step 2 hoists outside of the region being rewritten. Step 3 and 4 together correspond to a minIR sugraph rewrite. They have been split into two steps following the proof strategy. Step 4 is an instance of a minIR operation rewrite.


  1. To be precise, \preccurlyeq is a partial order on the type strings up to isomorphism↩︎

  2. The set operations \subseteq and \cup are again understood to apply to the unordered set of elements contained in the lists tgt(o)\mathit\,{tgt}(o) and src(o)\mathit{src}\,(o)↩︎

  3. We can always extend a subgraph to contain more ancestor regions, until there is indeed a unique top-level region in the subgraph. ↩︎