Fixes https://github.com/llvm/llvm-project/issues/64665. The included
test case reproduces the stack trace when the if guard is removed.
I would be open to any advice about a better way to fix this issue,
since the if guard seems a little bit hacky (it would be better not to
generate duplicated removal instructions in the first place). From what
I can tell, the call to legalizeWithFold is where this originates, and
it calls ConversionPatternRewriter::replaceOpWithNewOp which inserts an
additional instruction to remove the materialization op after the loop
over unresolvedMaterializations already removes it.
No need to write into a temporary-file, you can just pipe the result of mlir-opt into FileCheck