Extracting loop body

Hi,

I’ve seen this question asked before but I haven’t seen an answer…

I would like to extract the body of a (for) loop into a new function so I can do some transformations. The function ExtractCodeRegion seemed like a good candidate and I’ve got it to sort of work.

My problem is that ExtractCodeRegion creates a function that still contains a loop. I was able to eliminate the loop by manually splitting the blocks of the original loop body after and before induction variable uses (hard code the instructions to split on)

My question is, how can I reliably/more generally split the blocks of the loop body to avoid a loop in the extracted code region? Also, I should mention that in the presence of nested loops I would like to leave the inner loops intact.

Has anyone done anything similar or perhaps there is something obvious I am missing?

Thanks,

paul

Hi Paul,

FWIW, in KernelGen [1] we first use the loop extractor pass (which
extracts loops into functions) and afterwards substitute the loop
boundaries. If loop boundary values are small or fictive, optimization
passes will automatically remove the loop header for you.

So, replace loop boundary with 1, and it will be gone after opt
passes. This approach should be more safe & faster than manual
cleanup, which may involve dealing with things like phi nodes.

[1] http://kernelgen.org

- D.