The sentinel value used is the number of post-split dims in the default specialization, which is not a good sentinel for the specialization definition. I'd switch to a different sentinel (e.g. -1), but you still have #8316 to deal with. I think this code should do the whole find_if dance per specialization, and not once out here. That would be in build_provide_loop_nest.
The cause is https://github.com/halide/Halide/blob/main/src/ScheduleFunctions.cpp#L1514
The sentinel value used is the number of post-split dims in the default specialization, which is not a good sentinel for the specialization definition. I'd switch to a different sentinel (e.g. -1), but you still have #8316 to deal with. I think this code should do the whole find_if dance per specialization, and not once out here. That would be in build_provide_loop_nest.