Skip to content

Commit 6d85f07

Browse files
ptersilievext01
authored andcommitted
Add flag to embed final bitcode.
The `embed-bitcode` flag serialises the IR after only architecture agnostic optimisations have been run, but then proceeds to apply other optimisations afterwards. Sometimes, this final version is precisely what we are interested in. The `embed-bitcode-final` flag waits until all optimisations have been run before embedding the IR.
1 parent 8809cdf commit 6d85f07

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include "llvm/BinaryFormat/COFF.h"
3737
#include "llvm/BinaryFormat/Dwarf.h"
3838
#include "llvm/BinaryFormat/ELF.h"
39+
#include "llvm/Bitcode/BitcodeWriter.h"
3940
#include "llvm/CodeGen/GCMetadata.h"
4041
#include "llvm/CodeGen/GCMetadataPrinter.h"
4142
#include "llvm/CodeGen/MachineBasicBlock.h"
@@ -141,6 +142,10 @@ static cl::opt<bool>
141142
DisableDebugInfoPrinting("disable-debug-info-print", cl::Hidden,
142143
cl::desc("Disable debug info printing"));
143144

145+
static cl::opt<bool>
146+
EmbedBitcodeFinal("embed-bitcode-final", cl::NotHidden,
147+
cl::desc("Embed final IR as bitcode after all optimisations and transformations have run."));
148+
144149
const char DWARFGroupName[] = "dwarf";
145150
const char DWARFGroupDescription[] = "DWARF Emission";
146151
const char DbgTimerName[] = "emit";
@@ -1778,6 +1783,18 @@ void AsmPrinter::emitRemarksSection(remarks::RemarkStreamer &RS) {
17781783
}
17791784

17801785
bool AsmPrinter::doFinalization(Module &M) {
1786+
// The `embed-bitcode` flag serialises the IR after only architecture
1787+
// agnostic optimisations have been run, but then proceeds to apply other
1788+
// optimisations and transformations afterwards. Sometimes this final version
1789+
// is precisely what we are interested in. The `embed-bitcode-final` flag
1790+
// waits until all optimisations/transformations have been run before
1791+
// embedding the IR.
1792+
if (EmbedBitcodeFinal)
1793+
llvm::EmbedBitcodeInModule(M, llvm::MemoryBufferRef(),
1794+
/*EmbedBitcode*/ true,
1795+
/*EmbedCmdline*/ false,
1796+
/*CmdArgs*/ std::vector<uint8_t>());
1797+
17811798
// Set the MachineFunction to nullptr so that we can catch attempted
17821799
// accesses to MF specific features at the module level and so that
17831800
// we can conditionalize accesses based on whether or not it is nullptr.

0 commit comments

Comments
 (0)