[LLVMdev] How to emit a simple stream of machine code.

Eli Friedman eli.friedman at gmail.com
Wed Aug 10 11:52:53 PDT 2011


On Wed, Aug 10, 2011 at 11:26 AM, Tom Stellard <tstellar at gmail.com> wrote:
> Hi,
>
> I am working on an LLVM backend for GPUs, and I would like to be able
> to emit a very simple byte stream of machine code, so that it can be
> fed directly into the GPU.  I don't need anything fancy, just the raw
> machine code, one instruction after another.  I think I've already
> implemented everything in the backend that is required to do this, but
> I'm not sure how to get a pointer to the emitted machine code stream
> so I can send it to the GPU.  I'm guessing that I need to use the
> function addPassesToEmitMachineCode(PassManagerBase &PM,JITCodeEmitter
> &JCE, CodeGenOpt::Level OptLevel, bool DisableVerify), but I'm not
> sure how to extract the code from the JITCodeEmitter, or if this is
> even the right place to start.  What is the best way for me to
> accomplish this?

The normal way targets emit machine code directly is with the MC
infrastructure; x86 and ARM really have complete implementations.  The
MC infrastructure is probably overkill for what you are doing (it
sounds like you don't need relocations and things like that), but it
might be worth taking a quick look.

You can override addPassesToEmitFile in your target, which will give
you complete control over how exactly your target emits code.  Take a
look at LLVMTargetMachine::addPassesToEmitFile to see what most
targets do, and CTargetMachine::addPassesToEmitFile in the C backend
to see what an unusual target can do.  You might also want to look at
the PTX backend, which is also a GPU backend (but only emits textual
assembly.)

I would strongly suggest not touching any of the JIT infrastructure
for what you are doing.

-Eli




More information about the llvm-dev mailing list