diff --git a/llvm/bindings/go/llvm/bitwriter.go b/llvm/bindings/go/llvm/bitwriter.go --- a/llvm/bindings/go/llvm/bitwriter.go +++ b/llvm/bindings/go/llvm/bitwriter.go @@ -35,4 +35,9 @@ return MemoryBuffer{mb} } +func WriteThinLTOBitcodeToMemoryBuffer(m Module) MemoryBuffer { + mb := C.LLVMWriteThinLTOBitcodeToMemoryBuffer(m.C) + return MemoryBuffer{mb} +} + // TODO(nsf): Figure out way how to make it work with io.Writer diff --git a/llvm/include/llvm-c/BitWriter.h b/llvm/include/llvm-c/BitWriter.h --- a/llvm/include/llvm-c/BitWriter.h +++ b/llvm/include/llvm-c/BitWriter.h @@ -47,6 +47,9 @@ /** Writes a module to a new memory buffer and returns it. */ LLVMMemoryBufferRef LLVMWriteBitcodeToMemoryBuffer(LLVMModuleRef M); +/** Writes a module with ThinLTO summary to a new memory buffer and returns it. */ +LLVMMemoryBufferRef LLVMWriteThinLTOBitcodeToMemoryBuffer(LLVMModuleRef M); + /** * @} */ diff --git a/llvm/lib/Bitcode/Writer/BitWriter.cpp b/llvm/lib/Bitcode/Writer/BitWriter.cpp --- a/llvm/lib/Bitcode/Writer/BitWriter.cpp +++ b/llvm/lib/Bitcode/Writer/BitWriter.cpp @@ -8,10 +8,12 @@ #include "llvm-c/BitWriter.h" #include "llvm/Bitcode/BitcodeWriter.h" +#include "llvm/IR/LegacyPassManager.h" #include "llvm/IR/Module.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/raw_ostream.h" +#include "llvm/Transforms/IPO.h" using namespace llvm; @@ -47,3 +49,12 @@ WriteBitcodeToFile(*unwrap(M), OS); return wrap(MemoryBuffer::getMemBufferCopy(OS.str()).release()); } + +LLVMMemoryBufferRef LLVMWriteThinLTOBitcodeToMemoryBuffer(LLVMModuleRef M) { + std::string Data; + llvm::raw_string_ostream OS(Data); + llvm::legacy::PassManager PM; + PM.add(createWriteThinLTOBitcodePass(OS)); + PM.run(*llvm::unwrap(M)); + return llvm::wrap(llvm::MemoryBuffer::getMemBufferCopy(OS.str()).release()); +}