On Wednesday 09 July 2008 12:16, Eli Friedman wrote:> On Wed, Jul 9, 2008 at 9:56 AM, David Greene <dag at cray.com> wrote: > > I seem to recall havbing asked this before but I can't find it by > > searching. > > > > What's the right way to clone a Function? I need to save off the text at > > a certain point and spit it out later, after other optimizations have > > run. But I need to spit out the original text, not the optimized > > version. > > You can use CloneFunction (in llvm/Transforms/Utils/Cloning.h) to > duplicate a function within a module. > > That said, I'm not completely sure what you're trying to do, so I > don't know if that's what you're looking for.Ok, I've looked at the cloning stuff and it's on the right track. Here's what I need to do: I need to implement functionality similar to llvm-gcc's -emit-llvm in our compiler. I need to emit the llvm before any optimizations on the IR have been performed. But here's the catch. Our compiler processes one function at a time, so the LLVM IR is generated and fully optimized before the next function appears. This also means the global symbol table changes as we process functions. So what I need to do is somehow clone the final module (so I capture all of the needed global symbols) and then replace the functions in the cloned module (which have been optimized) with the unoptimized LLVM IR. So it seems I need to do something like this: 1. Call CloneFunction for each function as it comes across to LLVM. This captures the unoptimized LLVM IR. 2. Call CloneModule at the end of all processing to capture all globals. 3. ??!? 4. Profit! The ??!? is something like, replace the cloned optimized Functions in the cloned Module with the cloned unoptimized Functions that were saved off in step 1. But how do I correctly remap the values? I would have a ValueMap for each function from step 1 but I somehow have to map those Values onto the Values in the cloned module. And map the arguments as well (what CloneFunctionInto does). Can anyone with more familiarity that I have about the cloning mechanism provide guidance? Thanks! -Dave -Dave
On Jul 9, 2008, at 10:43 AM, David Greene wrote:> On Wednesday 09 July 2008 12:16, Eli Friedman wrote: >> On Wed, Jul 9, 2008 at 9:56 AM, David Greene <dag at cray.com> wrote: >>> I seem to recall havbing asked this before but I can't find it by >>> searching. >>> >>> What's the right way to clone a Function? I need to save off the >>> text at >>> a certain point and spit it out later, after other optimizations >>> have >>> run. But I need to spit out the original text, not the optimized >>> version.Is it possible to explain intended use of original unoptimized version ?>>> You can use CloneFunction (in llvm/Transforms/Utils/Cloning.h) to >> duplicate a function within a module. >> >> That said, I'm not completely sure what you're trying to do, so I >> don't know if that's what you're looking for. > > Ok, I've looked at the cloning stuff and it's on the right track. > Here's > what I need to do: > > I need to implement functionality similar to llvm-gcc's -emit-llvm in > our compiler. I need to emit the llvm before any optimizations on the > IR have been performed. > > But here's the catch. Our compiler processes one function at a time, > so the LLVM IR is generated and fully optimized before the next > function > appears.You do not need to optimize LLVM IR before processing next function. If your compiler works like for each function generate_ir optimize then it seems you're doing for each function generate_ir convert_to_llvm_ir optimize_llvm_ir If you're doing this then you may try for each function generate_ir convert_to_llvm_ir optimize_llvm_ir_module Now, you have access to un-optimized functions before you do optimize_llvm_ir_module. Would that work ?> This also means the global symbol table changes as we > process functions. > > So what I need to do is somehow clone the final module (so I capture > all > of the needed global symbols) and then replace the functions in the > cloned > module (which have been optimized) with the unoptimized LLVM IR.> > > So it seems I need to do something like this: > > 1. Call CloneFunction for each function as it comes across to LLVM. > This > captures the unoptimized LLVM IR. > > 2. Call CloneModule at the end of all processing to capture all > globals. > > 3. ??!? > > 4. Profit! > > The ??!? is something like, replace the cloned optimized Functions > in the > cloned Module with the cloned unoptimized Functions that were saved > off in step 1. But how do I correctly remap the values? I would > have a > ValueMap for each function from step 1 but I somehow have to map those > Values onto the Values in the cloned module. And map the arguments as > well (what CloneFunctionInto does). > > Can anyone with more familiarity that I have about the cloning > mechanism > provide guidance? > > Thanks! > > -Dave > > -Dave > _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev- Devang
On Wednesday 09 July 2008 13:24, Devang Patel wrote:> Is it possible to explain intended use of original unoptimized version ?bugpoint. I want to run it on the IR produced by our frontend. This will help us generate new LLVM tests we can send upstream. We've fixed bugs that aren't caught by the upstream tests and it would be nice to capture the problem and make the test available to everyone.> You do not need to optimize LLVM IR before processing next function.You do if the functions you compile are mega-humongous.> If your compiler works like > > for each function > generate_ir > optimizeYep.> then it seems you're doing > > for each function > generate_ir > convert_to_llvm_ir > optimize_llvm_irYep.> If you're doing this then you may try > > for each function > generate_ir > convert_to_llvm_ir > optimize_llvm_ir_moduleWe in fact did used to do it this way. Then we ran out of memory. And there are other issues to. We'd added things in our copy of LLVM that looks to our middle-end for various bits of information. Some of that information goes away after we get each function so delaying optimization means we don't have all of the useful information we'd like. And BTW, there are many things we had to fix in LLVM to allow us to do this. It seems LLVM keeps around a lot of global state that needs to be reset if you do function-at-a-time processing. I'm hoping to send these fixes back upstream as soon as I deal with the red tape that's recently been put in place here.> Now, you have access to un-optimized functions before you do > optimize_llvm_ir_module. Would that work ?It will work for this particular code I'm looking at but in general it won't. And I'm loathe to change how our compiler flow works just for this. It's one more difference we'd have to deal with when tracking down bugs. -Dave