Skip to content

Commit 80fe119

Browse files
committed
wip
1 parent f49bc03 commit 80fe119

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

cranelift/codegen/src/egraph/elaborate.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ impl<'a> Elaborator<'a> {
248248
seen_this_traversal.remove(&x_val);
249249
}
250250
let (best_value_y, new_seen_y) = self.best_value_traversal(y, seen_this_traversal);
251-
// Usually, right will be better. But if not, choose x and restore seen_this_traversal state
251+
// Usually, y will be better. But if not, choose x and restore seen_this_traversal state
252252
if best_value_x.0 < best_value_y.0 {
253253
for x_val in new_seen_x.keys() {
254254
seen_this_traversal.insert(x_val.clone());
@@ -316,12 +316,25 @@ impl<'a> Elaborator<'a> {
316316
let best = BestEntry(cost, value);
317317
union_of_new_seen[value] = best;
318318
trace!(" -> cost of value {} = {:?}", value, cost);
319+
320+
// AVH TODO REMOVE
321+
trace!("size of new {}", union_of_new_seen.capacity());
319322
return (best, union_of_new_seen);
320323
}
321324
}
322325
};
323326
}
324327

328+
// Elaborate the best value (from required/skeleton use). If we have already found a best value,
329+
// we can just return that. Otherwise, we traverse the aegraph from this point, keeping track
330+
// of previously seen values along argument paths to avoid double-counting DAG entries (e.g., to
331+
// have a cost function that does not repeat costs for subexpressions). This involves carefully
332+
// handling union nodes, only counting their values as permanently seen once a choice is made between
333+
// the unioned values.
334+
335+
// Once the `best_value_traversal` helper is complete, we know we have found a best value for the
336+
// current node, and every node newly seen in its chosen traversal. We can thus add all of these
337+
// values to our per-function `value_to_best_value` map.
325338
fn elaborate_best_value(&mut self, value: Value) -> BestEntry {
326339
let best_found = self.value_to_best_value[value];
327340
if !best_found.1.is_reserved_value() {

cranelift/filetests/filetests/egraph/sharing.clif

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@ test optimize
22
set opt_level=speed
33
target x86_64
44

5-
;; iadd has cost 3, imul has cost 10
6-
;; (rule (simplify (ireduce ty (iadd _ x y))) (iadd ty (ireduce ty x) (ireduce ty y)))
7-
85
function %f0(i32, i32) -> i16 {
96
block0(v0: i32, v1: i32):
107
v2 = imul.i32 v0, v1
@@ -31,8 +28,11 @@ block0(v0: i32, v1: i32):
3128
v23 = imul.i32 v22, v22
3229
v24 = imul.i32 v23, v23
3330
v25 = isub.i32 v24, v1
34-
;; Chain of imuls saturate to infinity w/o DAG cost model
35-
;; then do a rewrite to a more expensive thing
31+
;; Chain of imuls saturate to infinity with a tree cost model that ignores
32+
;; sharing. A DAG cost model that attempts to avoid double-counting shared
33+
;; subterms will not saturate. To test, invoke one of the few rewrite rules
34+
;; that rewrites to a high-cost expression:
35+
;; (rule (simplify (ireduce ty (iadd _ x y))) (iadd ty (ireduce ty x) (ireduce ty y)))
3636
v26 = iadd.i32 v24, v25
3737
v27 = ireduce.i16 v26
3838
v28 = ireduce.i16 v0

0 commit comments

Comments
 (0)