175a17970SPaul Kirth //===- EmbedBitcodePass.cpp - Pass that embeds the bitcode into a global---===// 275a17970SPaul Kirth // 375a17970SPaul Kirth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 475a17970SPaul Kirth // See https://llvm.org/LICENSE.txt for license information. 575a17970SPaul Kirth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 675a17970SPaul Kirth // 775a17970SPaul Kirth //===----------------------------------------------------------------------===// 875a17970SPaul Kirth 975a17970SPaul Kirth #include "llvm/Transforms/IPO/EmbedBitcodePass.h" 10*9d476e1eSPaul Kirth #include "llvm/Bitcode/BitcodeWriter.h" 11*9d476e1eSPaul Kirth #include "llvm/Bitcode/BitcodeWriterPass.h" 1275a17970SPaul Kirth #include "llvm/IR/PassManager.h" 1375a17970SPaul Kirth #include "llvm/Pass.h" 1475a17970SPaul Kirth #include "llvm/Support/ErrorHandling.h" 1575a17970SPaul Kirth #include "llvm/Support/MemoryBufferRef.h" 1675a17970SPaul Kirth #include "llvm/Support/raw_ostream.h" 1775a17970SPaul Kirth #include "llvm/TargetParser/Triple.h" 1875a17970SPaul Kirth #include "llvm/Transforms/IPO/ThinLTOBitcodeWriter.h" 1975a17970SPaul Kirth #include "llvm/Transforms/Utils/ModuleUtils.h" 2075a17970SPaul Kirth 2175a17970SPaul Kirth #include <string> 2275a17970SPaul Kirth 2375a17970SPaul Kirth using namespace llvm; 2475a17970SPaul Kirth 2575a17970SPaul Kirth PreservedAnalyses EmbedBitcodePass::run(Module &M, ModuleAnalysisManager &AM) { 2675a17970SPaul Kirth if (M.getGlobalVariable("llvm.embedded.module", /*AllowInternal=*/true)) 2775a17970SPaul Kirth report_fatal_error("Can only embed the module once", 2875a17970SPaul Kirth /*gen_crash_diag=*/false); 2975a17970SPaul Kirth 3075a17970SPaul Kirth Triple T(M.getTargetTriple()); 3175a17970SPaul Kirth if (T.getObjectFormat() != Triple::ELF) 3275a17970SPaul Kirth report_fatal_error( 3375a17970SPaul Kirth "EmbedBitcode pass currently only supports ELF object format", 3475a17970SPaul Kirth /*gen_crash_diag=*/false); 35*9d476e1eSPaul Kirth 3675a17970SPaul Kirth std::string Data; 3775a17970SPaul Kirth raw_string_ostream OS(Data); 38*9d476e1eSPaul Kirth if (IsThinLTO) 39cfe1ece8SPaul Kirth ThinLTOBitcodeWriterPass(OS, /*ThinLinkOS=*/nullptr).run(M, AM); 40*9d476e1eSPaul Kirth else 41*9d476e1eSPaul Kirth BitcodeWriterPass(OS, /*ShouldPreserveUseListOrder=*/false, EmitLTOSummary) 42*9d476e1eSPaul Kirth .run(M, AM); 43*9d476e1eSPaul Kirth 4475a17970SPaul Kirth embedBufferInModule(M, MemoryBufferRef(Data, "ModuleData"), ".llvm.lto"); 45*9d476e1eSPaul Kirth 4675a17970SPaul Kirth return PreservedAnalyses::all(); 4775a17970SPaul Kirth } 48