Path: blob/main/contrib/llvm-project/llvm/lib/Transforms/IPO/EmbedBitcodePass.cpp
35266 views
//===- EmbedBitcodePass.cpp - Pass that embeds the bitcode into a global---===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "llvm/Transforms/IPO/EmbedBitcodePass.h"9#include "llvm/Bitcode/BitcodeWriter.h"10#include "llvm/Bitcode/BitcodeWriterPass.h"11#include "llvm/IR/PassManager.h"12#include "llvm/Pass.h"13#include "llvm/Support/ErrorHandling.h"14#include "llvm/Support/MemoryBufferRef.h"15#include "llvm/Support/raw_ostream.h"16#include "llvm/TargetParser/Triple.h"17#include "llvm/Transforms/IPO/ThinLTOBitcodeWriter.h"18#include "llvm/Transforms/Utils/ModuleUtils.h"1920#include <memory>21#include <string>2223using namespace llvm;2425PreservedAnalyses EmbedBitcodePass::run(Module &M, ModuleAnalysisManager &AM) {26if (M.getGlobalVariable("llvm.embedded.module", /*AllowInternal=*/true))27report_fatal_error("Can only embed the module once",28/*gen_crash_diag=*/false);2930Triple T(M.getTargetTriple());31if (T.getObjectFormat() != Triple::ELF)32report_fatal_error(33"EmbedBitcode pass currently only supports ELF object format",34/*gen_crash_diag=*/false);3536std::string Data;37raw_string_ostream OS(Data);38if (IsThinLTO)39ThinLTOBitcodeWriterPass(OS, /*ThinLinkOS=*/nullptr).run(M, AM);40else41BitcodeWriterPass(OS, /*ShouldPreserveUseListOrder=*/false, EmitLTOSummary)42.run(M, AM);4344embedBufferInModule(M, MemoryBufferRef(Data, "ModuleData"), ".llvm.lto");4546return PreservedAnalyses::all();47}484950