Our first attempt to fix the issue is using memref.alloca_scope. However, it does NOT solve the issue as scf.for has the AutomaticAllocationScope trait. As the result, AllocaScopeHoister hoists memref::alloca from the alloca_scope into scf.for, leading to incorrect code.
(or any op like scf.for having AutomaticAllocationScope)
should be:
scf.for {
memref.alloca
xxx
}
That is the semantics of memref.alloca_scope should already be implied here.
Of course there is a lowering problem, where every op that has the AutomaticAllocationScope should lower the exact same way as memref.alloca_scope…
You are actually right! There seems to be another AllocaScopeInliner (MLIR: AllocaScopeInliner Struct Reference). Though, it seems that the Inliner is not applied after Hoister in a single canonicalization pass when I tested it.
I do think that the loop should have the trait, it was added exactly to prevent the kind of situation you observed. The lowering is likely incorrect in this case. I vaguely remember a discussion about llvm.intr.stacksave/restore being unsupported for some targets, but we can always try and see what happens.