Skip to content

Instantly share code, notes, and snippets.

@RealNeGate
Last active March 4, 2024 04:58
Show Gist options
  • Select an option

  • Save RealNeGate/b2009e693739b772a69a40dd1ad50a8a to your computer and use it in GitHub Desktop.

Select an option

Save RealNeGate/b2009e693739b772a69a40dd1ad50a8a to your computer and use it in GitHub Desktop.

Revisions

  1. RealNeGate revised this gist Mar 4, 2024. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions combined_solver.c
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,6 @@
    // combined "Peeps + ConstProp + GVN" solver (Combining Analyses, Combining Optimizations 7.3.2)
    // returns isomorphic node.
    // combined "Peeps + ConstProp + GVN" solver (extended version of the one in Combining Analyses, Combining Optimizations 7.3.2)
    //
    // returns isomorphic node (might be the same node).
    Node* peephole_node(Optimizer* opt, Node* n) {
    Node* k;
    bool progress = false;
  2. RealNeGate revised this gist Mar 4, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion combined_solver.c
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    // combined "Peeps + ConstProp + GVN" solver.
    // combined "Peeps + ConstProp + GVN" solver (Combining Analyses, Combining Optimizations 7.3.2)
    // returns isomorphic node.
    Node* peephole_node(Optimizer* opt, Node* n) {
    Node* k;
  3. RealNeGate revised this gist Mar 4, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion combined_solver.c
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@ Node* peephole_node(Optimizer* opt, Node* n) {

    // iterative convergence (rewrite peepholes):
    // we'll settle on a fixpoint and be happy.
    while (k = ideal(), k) { n = k; }
    while (k = ideal(n), k) { n = k; }

    // tries to deduce the value of an op based on the
    // types of the inputs, if both inputs were deduced
  4. RealNeGate revised this gist Mar 4, 2024. 1 changed file with 3 additions and 23 deletions.
    26 changes: 3 additions & 23 deletions combined_solver.c
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    // combined "Peeps + ConstProp + GVN" solver.
    // returns NULL if it made no progress
    // returns isomorphic node.
    Node* peephole_node(Optimizer* opt, Node* n) {
    Node* k;
    bool progress = false;
    @@ -9,16 +9,7 @@ Node* peephole_node(Optimizer* opt, Node* n) {

    // iterative convergence (rewrite peepholes):
    // we'll settle on a fixpoint and be happy.
    while (k = ideal(), k) {
    // we can recycle the node which means we'll make progress (k != NULL),
    // but don't need to move the node uses.
    if (n != k) {
    subsume_node(opt, n, k);
    worklist_push_users(ws, k);
    n = k;
    }
    progress = true;
    }
    while (k = ideal(), k) { n = k; }

    // tries to deduce the value of an op based on the
    // types of the inputs, if both inputs were deduced
    @@ -48,16 +39,5 @@ Node* peephole_node(Optimizer* opt, Node* n) {
    return k;
    }

    return progress ? n : NULL;
    }

    void peephole(Optimizer* opt, Worklist* ws) {
    Node* n;
    while (n = worklist_pop(ws), n) {
    Node* k = peephole_node(ws, n);
    if (k) {
    if (n != k) { subsume_node(opt, n, k); }
    worklist_push_users(ws, k);
    }
    }
    return n;
    }
  5. RealNeGate revised this gist Feb 29, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion combined_solver.c
    Original file line number Diff line number Diff line change
    @@ -55,7 +55,7 @@ void peephole(Optimizer* opt, Worklist* ws) {
    Node* n;
    while (n = worklist_pop(ws), n) {
    Node* k = peephole_node(ws, n);
    if (k != NULL) {
    if (k) {
    if (n != k) { subsume_node(opt, n, k); }
    worklist_push_users(ws, k);
    }
  6. RealNeGate revised this gist Feb 29, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion combined_solver.c
    Original file line number Diff line number Diff line change
    @@ -56,7 +56,7 @@ void peephole(Optimizer* opt, Worklist* ws) {
    while (n = worklist_pop(ws), n) {
    Node* k = peephole_node(ws, n);
    if (k != NULL) {
    subsume_node(opt, n, k);
    if (n != k) { subsume_node(opt, n, k); }
    worklist_push_users(ws, k);
    }
    }
  7. RealNeGate revised this gist Feb 29, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion combined_solver.c
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    // combined "Peeps + ConstProp + GVN" solver.
    // returns NULL if it made no progress
    Node* peephole_node(Optimizer* opt, Node* n) {
    Node* k = n;
    Node* k;
    bool progress = false;

    // potential mutations from ideal() means we need to get rehash.
  8. RealNeGate revised this gist Feb 29, 2024. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions combined_solver.c
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,7 @@
    // returns NULL if it made no progress
    Node* peephole_node(Optimizer* opt, Node* n) {
    Node* k = n;
    bool progress = false;

    // potential mutations from ideal() means we need to get rehash.
    hashset_remove(opt->gvn_nodes, n);
    @@ -16,6 +17,7 @@ Node* peephole_node(Optimizer* opt, Node* n) {
    worklist_push_users(ws, k);
    n = k;
    }
    progress = true;
    }

    // tries to deduce the value of an op based on the
    @@ -46,9 +48,7 @@ Node* peephole_node(Optimizer* opt, Node* n) {
    return k;
    }

    // either k is NULL and it's already in it's normal form or
    // it's not and we performed some idealize ops.
    return k;
    return progress ? n : NULL;
    }

    void peephole(Optimizer* opt, Worklist* ws) {
  9. RealNeGate revised this gist Feb 29, 2024. 1 changed file with 7 additions and 3 deletions.
    10 changes: 7 additions & 3 deletions combined_solver.c
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,10 @@
    // combined "Peeps + ConstProp + GVN" solver.
    // returns NULL if it made no progress
    Node* peephole_node(Optimizer* opt, Node* n) {
    Node* k;
    Node* k = n;

    // potential mutations from ideal() means we need to get rehash.
    hashset_remove(opt->gvn_nodes, n);

    // iterative convergence (rewrite peepholes):
    // we'll settle on a fixpoint and be happy.
    @@ -43,8 +46,9 @@ Node* peephole_node(Optimizer* opt, Node* n) {
    return k;
    }

    // it's already in it's normal form (no progress)
    return NULL;
    // either k is NULL and it's already in it's normal form or
    // it's not and we performed some idealize ops.
    return k;
    }

    void peephole(Optimizer* opt, Worklist* ws) {
  10. RealNeGate revised this gist Feb 29, 2024. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion combined_solver.c
    Original file line number Diff line number Diff line change
    @@ -26,7 +26,8 @@ Node* peephole_node(Optimizer* opt, Node* n) {
    // this is ops which replace the node with a direct
    // input (x + 0 => x), because of this we don't need
    // to rerun const prop since they don't change nodes
    // just referenced existing ones.
    // just referenced existing ones (which are either in
    // normal form or waiting on the worklist).
    //
    // identity(opt, n) will always return a node, either n
    // or a direct replacement
  11. RealNeGate revised this gist Feb 29, 2024. 1 changed file with 14 additions and 2 deletions.
    16 changes: 14 additions & 2 deletions combined_solver.c
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,23 @@
    // combined SimpleConstProp + Identity + GVN solver.
    // combined "Peeps + ConstProp + GVN" solver.
    // returns NULL if it made no progress
    Node* peephole_node(Optimizer* opt, Node* n) {
    Node* k;

    // iterative convergence (rewrite peepholes):
    // we'll settle on a fixpoint and be happy.
    while (k = ideal(), k) {
    // we can recycle the node which means we'll make progress (k != NULL),
    // but don't need to move the node uses.
    if (n != k) {
    subsume_node(opt, n, k);
    worklist_push_users(ws, k);
    n = k;
    }
    }

    // tries to deduce the value of an op based on the
    // types of the inputs, if both inputs were deduced
    // constant we'd be constants and if both inputs were
    // constant we might be constant and if both inputs were
    // ranges we might infer extra data if not a constant.
    Type* t = value(opt, n);
    k = try_as_const(opt, n, t);
  12. RealNeGate revised this gist Feb 12, 2024. 1 changed file with 5 additions and 2 deletions.
    7 changes: 5 additions & 2 deletions combined_solver.c
    Original file line number Diff line number Diff line change
    @@ -34,10 +34,13 @@ Node* peephole_node(Optimizer* opt, Node* n) {
    return NULL;
    }

    void peephole(Worklist* ws) {
    void peephole(Optimizer* opt, Worklist* ws) {
    Node* n;
    while (n = worklist_pop(ws), n) {
    Node* k = peephole_node(ws, n);
    if (k != NULL) { worklist_push_users(ws, k); }
    if (k != NULL) {
    subsume_node(opt, n, k);
    worklist_push_users(ws, k);
    }
    }
    }
  13. RealNeGate created this gist Feb 12, 2024.
    43 changes: 43 additions & 0 deletions combined_solver.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    // combined SimpleConstProp + Identity + GVN solver.
    // returns NULL if it made no progress
    Node* peephole_node(Optimizer* opt, Node* n) {
    Node* k;

    // tries to deduce the value of an op based on the
    // types of the inputs, if both inputs were deduced
    // constant we'd be constants and if both inputs were
    // ranges we might infer extra data if not a constant.
    Type* t = value(opt, n);
    k = try_as_const(opt, n, t);
    if (k) { return k; }

    // this is ops which replace the node with a direct
    // input (x + 0 => x), because of this we don't need
    // to rerun const prop since they don't change nodes
    // just referenced existing ones.
    //
    // identity(opt, n) will always return a node, either n
    // or a direct replacement
    k = identity(opt, n);
    if (k != n) { return k; }

    // GVN, we literally just place nodes into a hash table.
    // no need to rerun it's ruleset if it's just referencing
    // an existing node.
    k = hashset_get(opt->gvn_nodes, n);
    if (k) {
    hashset_put(opt->gvn_nodes, k);
    return k;
    }

    // it's already in it's normal form (no progress)
    return NULL;
    }

    void peephole(Worklist* ws) {
    Node* n;
    while (n = worklist_pop(ws), n) {
    Node* k = peephole_node(ws, n);
    if (k != NULL) { worklist_push_users(ws, k); }
    }
    }