utilizing precompiled code

I'm using clang embedded in an app and am trying to figure out how I
can precompile a chunk of code and reuse it when compiling other
chunks of code. I'm compiling lots of independent bits of code that
each live in their own llvm::Module. For each independent bit of
code, I have ~4K lines of common C++ code that gets pasted at the top.
Ideally, there would be a way to preprocess these ~4K lines such that
I could just tack it on to each module to save from having to
reprocess the same code over and over.

I've looked into using the PCH infrastructure, but it seems to assume
that a PCH is an include file on disk. Everything I'm doing is
in-memory. I've scanned through the clang source and found
GeneratePCH, which takes a raw_ostream. I was thinking I could just
rass in a raw_string_ostream instead, but I'm not sure where to grab
the clang::Module function argument from and in any case, it seems
that clang::Modules correspond tightly to files, which is not going to
work for me. Is there an alternative?

I'm using the CompilerInstance to drive compilation. I figure there
must be some data structure that results from calling clang::ParseAST.
Here's an outline of what I'd like to do (keep in mind this is all

- Generate ~4K common code
- Parse common code

Repeat forever:
- Generate code to perform task
- Parse task code
- Prepend parsed common code to parsed task code (like you would with a PCH)
- Codegen combined code
- Get entry point function pointers and use them in the app