[PATCH] D119431: [C API] Add LLVMWriteThinLTOBitcodeToMemoryBuffer to C API

Ayke via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 10 05:19:47 PST 2022


aykevl created this revision.
aykevl added reviewers: deadalnix, echristo.
Herald added subscribers: JDevlieghere, hiraditya, inglorion.
aykevl requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

A similar function already exists in Rust as `LLVMRustThinLTOBufferCreate`. I needed it in the C API for my own project so I wrote this patch to add it.

I tried using the new pass manager, but somehow that didn't work: no module summary was created.

This didn't work:

  std::string Data;
  raw_string_ostream OS(Data);
  ModulePassManager MPM;
  ModuleAnalysisManager MAM;
  MPM.addPass(ThinLTOBitcodeWriterPass(OS, nullptr));
  MPM.run(*unwrap(M), MAM);
  return wrap(MemoryBuffer::getMemBufferCopy(OS.str()).release());


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D119431

Files:
  llvm/include/llvm-c/BitWriter.h
  llvm/lib/Bitcode/Writer/BitWriter.cpp


Index: llvm/lib/Bitcode/Writer/BitWriter.cpp
===================================================================
--- llvm/lib/Bitcode/Writer/BitWriter.cpp
+++ 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());
+}
Index: llvm/include/llvm-c/BitWriter.h
===================================================================
--- llvm/include/llvm-c/BitWriter.h
+++ 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);
+
 /**
  * @}
  */


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D119431.407485.patch
Type: text/x-patch
Size: 1507 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220210/8cf83b5f/attachment.bin>


More information about the llvm-commits mailing list