The Legacy Partial Inlining pass has an ownership issue related to the OptimizationRemarkEmitter objects it creates. The call back used to create an ORE caches the allocated object in a unique_ptr in the runOnModule function, and returns a reference to that object. In the case we find a multi-region outline candidate that fails to get inlined into any of its users we end up with a dangling reference. This occurs because we get a reference to an ORE in unswitchFunction which gets destructed when tryPartialInline calls the callback to create other ORE objects. After we fail to inline in the multi-region case, we continue on with the single-region case which tries to emit a remark with the now deleted emitter.
This patch moves the ORE creation into a scope which avoids the problem, but there is still a potential pitfall here. Its not apparent that the ORE is invalidated by the call to tryPartialInline. I have toyed with a follow on patch that changes the GetORE callbacks to return a unique_ptr to an ORE instead, but thought I would post this change first to start the discussion of what the proper fix should look like.