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