Inserting a new basic block with Terminator of kind InlineAsm into the MIR

Lately I've been reading the rustc development guide . Not because I want to contribute, but out of interest. Now I wanted to play with this knowledge a little bit, but got stuck trying to alter the MIR Body of a function.

What I am trying to accomplish is to add an new Basic Block into the MIR, which has a Terminator of kind TerminatorKind::InlineAsm. Ultimately I want this to have the same effect like having an inline assembly expression asm!(".."); in the source code, but only by changing the MIR.

The Problem I have is that in order to create a Terminator of kind TerminatorKind::InlineAsm for the Basic Block, an argument template: &'tcx [InlineAsmTemplatePiece] is required. And I don't know the right way to create an InlineAsmTemplatePiece with the appropriate lifetime, being 'tcx. I assume that I have to store an instance of InlineAsmTemplatePiece somehow in the Typing Context TyCtxt and then use it, but I don't know how.

I am using a custom MirPass and the visitor pattern to achieve this task.

You can use tcx.arena.alloc_slice() or tcx.arena.alloc_from_iter().

Thanks a lot! I think tcx.arena.alloc_from_iter() did the job for me.