[M147] Fix crash when rewriting reshape(reshape(x))
This rewrite reused pointers across a call that could have reallocated the nodes array. In this case, I think we should simply not add the node? It's unclear why the reshape fusion does this, but reshape(expand_dims(x)) or other rewrites do not. The existing reshape was already being modified/invalidated anyways (by the `xnn_shape_fill_gaps` call).
(Cherry-picked from commit 474163f8aec45c77b48009eef6bb62886c3f25bd.)
PiperOrigin-RevId: 884666280
Bug: 492450480
Change-Id: I493e2c400574044840c171361f2688da76f1fe1c
diff --git a/src/subgraph.c b/src/subgraph.c
index 6c2908d..2d785ec 100644
--- a/src/subgraph.c
+++ b/src/subgraph.c
@@ -2712,12 +2712,11 @@
// Check that we are the only consumer of the input node.
const uint32_t input_id = node->inputs[0];
struct xnn_value* input_value = &subgraph->values[input_id];
- const uint32_t input_producer_id = input_value->producer;
- if (input_producer_id == XNN_INVALID_NODE_ID ||
+ if (input_value->producer == XNN_INVALID_NODE_ID ||
input_value->num_consumers != 1) {
return xnn_status_success;
}
- struct xnn_node* input_producer = &subgraph->nodes[input_producer_id];
+ struct xnn_node* input_producer = &subgraph->nodes[input_value->producer];
// Check all the interesting combinations.
@@ -2727,23 +2726,16 @@
XNN_RETURN_IF_ERROR(
xnn_shape_fill_gaps(&input_producer->params.static_reshape.new_shape,
&node->params.static_reshape.new_shape));
- XNN_RETURN_IF_ERROR(
- xnn_define_static_reshape(
- subgraph, node->params.static_reshape.new_shape.num_dims,
- node->params.static_reshape.new_shape.dim,
- input_producer->inputs[0], node->outputs[0],
- node->flags | XNN_NODE_FLAG_DONT_ELIDE),
- "Failed to create Binary Addition node.");
- node = move_last_node_to(subgraph, node_id);
node->inputs[0] = input_producer->inputs[0];
+ node->flags |= XNN_NODE_FLAG_DONT_ELIDE;
input_value = &subgraph->values[node->inputs[0]];
- if (input_value->first_consumer == input_producer_id) {
+ if (input_value->first_consumer == input_producer->id) {
input_value->first_consumer = node->id;
}
xnn_log_info(
"Replaced static_reshape[#%u](static_reshape[#%u](v%03u)) with "
"static_reshape[#%u](v%03u).",
- node_id, input_producer_id, node->inputs[0], node_id, node->inputs[0]);
+ node_id, input_producer->id, node->inputs[0], node_id, node->inputs[0]);
xnn_node_clear(input_producer);
(*changes)++;
}