1*f2526c1aSChris Bieneman //===- Bitcode/Writer/DXILBitcodeWriter.cpp - DXIL Bitcode Writer ---------===//
2*f2526c1aSChris Bieneman //
3*f2526c1aSChris Bieneman // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*f2526c1aSChris Bieneman // See https://llvm.org/LICENSE.txt for license information.
5*f2526c1aSChris Bieneman // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*f2526c1aSChris Bieneman //
7*f2526c1aSChris Bieneman //===----------------------------------------------------------------------===//
8*f2526c1aSChris Bieneman //
9*f2526c1aSChris Bieneman // Bitcode writer implementation.
10*f2526c1aSChris Bieneman //
11*f2526c1aSChris Bieneman //===----------------------------------------------------------------------===//
12*f2526c1aSChris Bieneman 
13*f2526c1aSChris Bieneman #include "llvm/ADT/StringRef.h"
14*f2526c1aSChris Bieneman #include "llvm/IR/ModuleSummaryIndex.h"
15*f2526c1aSChris Bieneman #include "llvm/MC/StringTableBuilder.h"
16*f2526c1aSChris Bieneman #include "llvm/Support/Allocator.h"
17*f2526c1aSChris Bieneman #include "llvm/Support/MemoryBufferRef.h"
18*f2526c1aSChris Bieneman #include <map>
19*f2526c1aSChris Bieneman #include <memory>
20*f2526c1aSChris Bieneman #include <string>
21*f2526c1aSChris Bieneman #include <vector>
22*f2526c1aSChris Bieneman 
23*f2526c1aSChris Bieneman namespace llvm {
24*f2526c1aSChris Bieneman 
25*f2526c1aSChris Bieneman class BitstreamWriter;
26*f2526c1aSChris Bieneman class Module;
27*f2526c1aSChris Bieneman class raw_ostream;
28*f2526c1aSChris Bieneman 
29*f2526c1aSChris Bieneman namespace dxil {
30*f2526c1aSChris Bieneman 
31*f2526c1aSChris Bieneman class BitcodeWriter {
32*f2526c1aSChris Bieneman   SmallVectorImpl<char> &Buffer;
33*f2526c1aSChris Bieneman   std::unique_ptr<BitstreamWriter> Stream;
34*f2526c1aSChris Bieneman 
35*f2526c1aSChris Bieneman   StringTableBuilder StrtabBuilder{StringTableBuilder::RAW};
36*f2526c1aSChris Bieneman 
37*f2526c1aSChris Bieneman   // Owns any strings created by the irsymtab writer until we create the
38*f2526c1aSChris Bieneman   // string table.
39*f2526c1aSChris Bieneman   BumpPtrAllocator Alloc;
40*f2526c1aSChris Bieneman 
41*f2526c1aSChris Bieneman   bool WroteStrtab = false, WroteSymtab = false;
42*f2526c1aSChris Bieneman 
43*f2526c1aSChris Bieneman   void writeBlob(unsigned Block, unsigned Record, StringRef Blob);
44*f2526c1aSChris Bieneman 
45*f2526c1aSChris Bieneman   std::vector<Module *> Mods;
46*f2526c1aSChris Bieneman 
47*f2526c1aSChris Bieneman public:
48*f2526c1aSChris Bieneman   /// Create a BitcodeWriter that writes to Buffer.
49*f2526c1aSChris Bieneman   BitcodeWriter(SmallVectorImpl<char> &Buffer, raw_fd_stream *FS = nullptr);
50*f2526c1aSChris Bieneman 
51*f2526c1aSChris Bieneman   ~BitcodeWriter();
52*f2526c1aSChris Bieneman 
53*f2526c1aSChris Bieneman   /// Attempt to write a symbol table to the bitcode file. This must be called
54*f2526c1aSChris Bieneman   /// at most once after all modules have been written.
55*f2526c1aSChris Bieneman   ///
56*f2526c1aSChris Bieneman   /// A reader does not require a symbol table to interpret a bitcode file;
57*f2526c1aSChris Bieneman   /// the symbol table is needed only to improve link-time performance. So
58*f2526c1aSChris Bieneman   /// this function may decide not to write a symbol table. It may so decide
59*f2526c1aSChris Bieneman   /// if, for example, the target is unregistered or the IR is malformed.
60*f2526c1aSChris Bieneman   void writeSymtab();
61*f2526c1aSChris Bieneman 
62*f2526c1aSChris Bieneman   /// Write the bitcode file's string table. This must be called exactly once
63*f2526c1aSChris Bieneman   /// after all modules and the optional symbol table have been written.
64*f2526c1aSChris Bieneman   void writeStrtab();
65*f2526c1aSChris Bieneman 
66*f2526c1aSChris Bieneman   /// Copy the string table for another module into this bitcode file. This
67*f2526c1aSChris Bieneman   /// should be called after copying the module itself into the bitcode file.
68*f2526c1aSChris Bieneman   void copyStrtab(StringRef Strtab);
69*f2526c1aSChris Bieneman 
70*f2526c1aSChris Bieneman   /// Write the specified module to the buffer specified at construction time.
71*f2526c1aSChris Bieneman   void writeModule(const Module &M);
72*f2526c1aSChris Bieneman };
73*f2526c1aSChris Bieneman 
74*f2526c1aSChris Bieneman /// Write the specified module to the specified raw output stream.
75*f2526c1aSChris Bieneman ///
76*f2526c1aSChris Bieneman /// For streams where it matters, the given stream should be in "binary"
77*f2526c1aSChris Bieneman /// mode.
78*f2526c1aSChris Bieneman void WriteDXILToFile(const Module &M, raw_ostream &Out);
79*f2526c1aSChris Bieneman 
80*f2526c1aSChris Bieneman } // namespace dxil
81*f2526c1aSChris Bieneman 
82*f2526c1aSChris Bieneman } // namespace llvm
83