1*fe013be4SDimitry Andric //===- EmbedBitcodePass.cpp - Pass that embeds the bitcode into a global---===//
2*fe013be4SDimitry Andric //
3*fe013be4SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*fe013be4SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*fe013be4SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*fe013be4SDimitry Andric //
7*fe013be4SDimitry Andric //===----------------------------------------------------------------------===//
8*fe013be4SDimitry Andric 
9*fe013be4SDimitry Andric #include "llvm/Transforms/IPO/EmbedBitcodePass.h"
10*fe013be4SDimitry Andric #include "llvm/Bitcode/BitcodeWriter.h"
11*fe013be4SDimitry Andric #include "llvm/Bitcode/BitcodeWriterPass.h"
12*fe013be4SDimitry Andric #include "llvm/IR/PassManager.h"
13*fe013be4SDimitry Andric #include "llvm/Pass.h"
14*fe013be4SDimitry Andric #include "llvm/Support/ErrorHandling.h"
15*fe013be4SDimitry Andric #include "llvm/Support/MemoryBufferRef.h"
16*fe013be4SDimitry Andric #include "llvm/Support/raw_ostream.h"
17*fe013be4SDimitry Andric #include "llvm/TargetParser/Triple.h"
18*fe013be4SDimitry Andric #include "llvm/Transforms/IPO/ThinLTOBitcodeWriter.h"
19*fe013be4SDimitry Andric #include "llvm/Transforms/Utils/Cloning.h"
20*fe013be4SDimitry Andric #include "llvm/Transforms/Utils/ModuleUtils.h"
21*fe013be4SDimitry Andric 
22*fe013be4SDimitry Andric #include <memory>
23*fe013be4SDimitry Andric #include <string>
24*fe013be4SDimitry Andric 
25*fe013be4SDimitry Andric using namespace llvm;
26*fe013be4SDimitry Andric 
27*fe013be4SDimitry Andric PreservedAnalyses EmbedBitcodePass::run(Module &M, ModuleAnalysisManager &AM) {
28*fe013be4SDimitry Andric   if (M.getGlobalVariable("llvm.embedded.module", /*AllowInternal=*/true))
29*fe013be4SDimitry Andric     report_fatal_error("Can only embed the module once",
30*fe013be4SDimitry Andric                        /*gen_crash_diag=*/false);
31*fe013be4SDimitry Andric 
32*fe013be4SDimitry Andric   Triple T(M.getTargetTriple());
33*fe013be4SDimitry Andric   if (T.getObjectFormat() != Triple::ELF)
34*fe013be4SDimitry Andric     report_fatal_error(
35*fe013be4SDimitry Andric         "EmbedBitcode pass currently only supports ELF object format",
36*fe013be4SDimitry Andric         /*gen_crash_diag=*/false);
37*fe013be4SDimitry Andric 
38*fe013be4SDimitry Andric   std::unique_ptr<Module> NewModule = CloneModule(M);
39*fe013be4SDimitry Andric   MPM.run(*NewModule, AM);
40*fe013be4SDimitry Andric 
41*fe013be4SDimitry Andric   std::string Data;
42*fe013be4SDimitry Andric   raw_string_ostream OS(Data);
43*fe013be4SDimitry Andric   if (IsThinLTO)
44*fe013be4SDimitry Andric     ThinLTOBitcodeWriterPass(OS, /*ThinLinkOS=*/nullptr).run(*NewModule, AM);
45*fe013be4SDimitry Andric   else
46*fe013be4SDimitry Andric     BitcodeWriterPass(OS, /*ShouldPreserveUseListOrder=*/false, EmitLTOSummary)
47*fe013be4SDimitry Andric         .run(*NewModule, AM);
48*fe013be4SDimitry Andric 
49*fe013be4SDimitry Andric   embedBufferInModule(M, MemoryBufferRef(Data, "ModuleData"), ".llvm.lto");
50*fe013be4SDimitry Andric 
51*fe013be4SDimitry Andric   return PreservedAnalyses::all();
52*fe013be4SDimitry Andric }
53