diff --git a/bricks/brick-llvm b/bricks/brick-llvm index 7f46a5b29a10272c3f62fc2402e3891667172c8a..bbee27868448a04fe0e940664f98c8c6d5bd1eca 100644 --- a/bricks/brick-llvm +++ b/bricks/brick-llvm @@ -33,11 +33,15 @@ DIVINE_RELAX_WARNINGS #endif +#include <llvm/IR/LLVMContext.h> #include <llvm/IR/Module.h> #include <llvm/IR/Verifier.h> #include <llvm/IR/Constants.h> #include <llvm/IR/Intrinsics.h> #include <llvm/Bitcode/BitcodeWriter.h> +#include <llvm/Bitcode/BitcodeReader.h> +#include <llvm/Object/IRObjectFile.h> +#include <llvm/Support/MemoryBuffer.h> #include <llvm/Support/raw_ostream.h> #include <llvm/Support/raw_os_ostream.h> #include <llvm/Support/Error.h> @@ -48,6 +52,8 @@ DIVINE_RELAX_WARNINGS DIVINE_UNRELAX_WARNINGS #endif +#include <memory> + #include <brick-assert> #include <brick-string> #include <brick-types> @@ -244,6 +250,25 @@ inline void writeModule( ::llvm::Module *m, std::string out ) { WriteBitcodeToFile( *m, fs ); } +inline std::unique_ptr< ::llvm::Module > load_bc( const std::string &str, + ::llvm::LLVMContext *ctx ) +{ + std::unique_ptr< ::llvm::MemoryBuffer > input = + std::move( ::llvm::MemoryBuffer::getFile( str ).get() ); + auto bc_input = + ::llvm::object::IRObjectFile::findBitcodeInMemBuffer( input->getMemBufferRef() ); + + if ( !bc_input ) + UNREACHABLE( "Could not load bitcode file" ); + auto module = ::llvm::parseBitcodeFile( bc_input.get(), *ctx ); + + if ( !module ) + UNREACHABLE( "Error parsing input model; probably not a valid bc file." ); + + return std::move( module.get() ); +} + + inline std::string getModuleBytes( ::llvm::Module *m ) { std::string s; verifyModule( *m );