Skip to content

Commit f2526c1

Browse files
committed
Add DXIL Bitcode Writer and DXIL testing
This change is a big blob of code that isn't easy to break up. It either comes in all together as a blob, works and has tests, or it doesn't do anything. Logically you can think of this patch as three things: (1) Adding virtual interfaces so the bitcode writer can be overridden (2) Adding a new bitcode writer implementation for DXIL (3) Adding some (optional) crazy CMake goop to build the DirectXShaderCompiler's llvm-dis as dxil-dis for testing Reviewed By: nikic Differential Revision: https://reviews.llvm.org/D122082
1 parent a7b9d75 commit f2526c1

22 files changed

+4911
-5
lines changed

llvm/lib/Target/DirectX/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@ add_llvm_target(DirectXCodeGen
1212
DXILPrepare.cpp
1313

1414
LINK_COMPONENTS
15-
Bitwriter
1615
Core
1716
Support
1817
DirectXInfo
18+
DXILBitWriter
1919

2020
ADD_TO_COMPONENT
2121
DirectX
2222
)
2323

2424
add_subdirectory(MCTargetDesc)
2525
add_subdirectory(TargetInfo)
26+
add_subdirectory(DXILWriter)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
add_llvm_component_library(LLVMDXILBitWriter
2+
DXILBitcodeWriter.cpp
3+
DXILValueEnumerator.cpp
4+
DXILWriterPass.cpp
5+
6+
DEPENDS
7+
intrinsics_gen
8+
9+
LINK_COMPONENTS
10+
Bitwriter
11+
Core
12+
MC
13+
Object
14+
Support
15+
)

llvm/lib/Target/DirectX/DXILWriter/DXILBitcodeWriter.cpp

Lines changed: 2963 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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

0 commit comments

Comments
 (0)