8

Now I use clang build my .c file to .s file. And I have used the llvm API modify the IR. However, now I can't save my modified IR to a file. I want to use "LLVMWriteBitcodeToFile", but I can't find the struct of "LLVMOpaqueModule"; I want to use "WriteBitcodeToFile", it always show me "type mismatch". And I also want to know how to build an IR file to a executable file.

Next are two methods I use to save a module:

1、First use WriteBitcodeToFile

bool unbuffered = false; 
llvm::raw_ostream ro(unbuffered); 
WriteBitcodeToFile(m, ro); 

2、Second use LLVMWriteBitcodeToFile

const char *Path = "hello2.s"; 
int ans = LLVMWriteBitcodeToFile(m, Path); 

note: m is a point of Module instance

4
  • The correct function to use is indeed WriteBitcodeToFile from Bitcode/ReaderWriter.h. If you fail using it, you should provide the code you are trying to compile in this question. Commented Dec 18, 2012 at 8:27
  • Yes,Follow is the prototype of WriteBitcodeToFilevoid: WriteBitcodeToFile(const Module *M, raw_ostream &Out); Commented Dec 18, 2012 at 11:15
  • Yes,Follow is the prototype of WriteBitcodeToFilevoid: WriteBitcodeToFile(const Module *M, raw_ostream &Out); when I init a raw_stream, I can't afford a type match parameter. which has a defaulted parameter. Commented Dec 18, 2012 at 11:20
  • Next are two methods I use to save a module: bool unbuffered = false; llvm::raw_ostream ro(unbuffered); WriteBitcodeToFile(m, ro); const char *Path = "hello2.s"; int ans = LLVMWriteBitcodeToFile(m, Path); Commented Dec 18, 2012 at 16:12

3 Answers 3

4
  1. For saving the IR into a file, see the answer to this question: writing module to .bc bitcode file
  2. For compiling IR to an object file, look at the llc tool and follow what its main function does.
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, But when I init an instance of raw_ostream which is a parameter of WriteBitcodeToFile, I can't do it.Neither prompt no parameter ,nor type mismatch.
And another I remember that llc just make the IR become assembly, we just can allocate the platform.
Next are two methods I use to save a module: bool unbuffered = false; llvm::raw_ostream ro(unbuffered); WriteBitcodeToFile(m, ro); const char *Path = "hello2.s"; int ans = LLVMWriteBitcodeToFile(m, Path);
@KunLee: could you edit your question to provide this additional information in a structured, well-formatted and well-expressed manner? That would greatly increase the chances of your question being answered
@KunLee: do you want to write to a file? Then use raw_fd_ostream. If you just want to write to stdout, use outs().
0

Check out these functions in llvm-c/TargetMachine.h:

/** Emits an asm or object file for the given module to the filename. This
  wraps several c++ only classes (among them a file stream). Returns any
  error in ErrorMessage. Use LLVMDisposeMessage to dispose the message. */
LLVMBool LLVMTargetMachineEmitToFile(LLVMTargetMachineRef T, LLVMModuleRef M,
  char *Filename, LLVMCodeGenFileType codegen, char **ErrorMessage);

/** Compile the LLVM IR stored in \p M and store the result in \p OutMemBuf. */
LLVMBool LLVMTargetMachineEmitToMemoryBuffer(LLVMTargetMachineRef T, LLVMModuleRef M,
  LLVMCodeGenFileType codegen, char** ErrorMessage, LLVMMemoryBufferRef *OutMemBuf);

See also How to generate machine code with llvm

Comments

0

for writing llvm bitcode to file what I do is :

std::error_code EC;
llvm::raw_fd_ostream OS("module", EC, llvm::sys::fs::F_None);
WriteBitcodeToFile(pBiFModule, OS);
OS.flush();

and then disassemble using llvm-dis.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.