Skip to content

[DirectX] Replace bitfield version in ProgramHeader. #91797

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions llvm/include/llvm/BinaryFormat/DXContainer.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,7 @@ struct BitcodeHeader {
};

struct ProgramHeader {
uint8_t MinorVersion : 4;
uint8_t MajorVersion : 4;
uint8_t Version;
uint8_t Unused;
uint16_t ShaderKind;
uint32_t Size; // Size in uint32_t words including this header.
Expand All @@ -131,6 +130,11 @@ struct ProgramHeader {
sys::swapByteOrder(Size);
Bitcode.swapBytes();
}
uint8_t getMajorVersion() { return Version >> 4; }
uint8_t getMinorVersion() { return Version & 0xF; }
static uint8_t getVersion(uint8_t Major, uint8_t Minor) {
return (Major << 4) | Minor;
}
};

static_assert(sizeof(ProgramHeader) == 24, "ProgramHeader Size incorrect!");
Expand Down
8 changes: 5 additions & 3 deletions llvm/lib/MC/MCDXContainerWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,11 @@ uint64_t DXContainerObjectWriter::writeObject(MCAssembler &Asm,

const Triple &TT = Asm.getContext().getTargetTriple();
VersionTuple Version = TT.getOSVersion();
Header.MajorVersion = static_cast<uint8_t>(Version.getMajor());
if (Version.getMinor())
Header.MinorVersion = static_cast<uint8_t>(*Version.getMinor());
uint8_t MajorVersion = static_cast<uint8_t>(Version.getMajor());
uint8_t MinorVersion =
static_cast<uint8_t>(Version.getMinor().value_or(0));
Header.Version =
dxbc::ProgramHeader::getVersion(MajorVersion, MinorVersion);
if (TT.hasEnvironment())
Header.ShaderKind =
static_cast<uint16_t>(TT.getEnvironment() - Triple::Pixel);
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/ObjectYAML/DXContainerEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ void DXContainerWriter::writeParts(raw_ostream &OS) {
if (!P.Program)
continue;
dxbc::ProgramHeader Header;
Header.MajorVersion = P.Program->MajorVersion;
Header.MinorVersion = P.Program->MinorVersion;
Header.Version = dxbc::ProgramHeader::getVersion(P.Program->MajorVersion,
P.Program->MinorVersion);
Header.Unused = 0;
Header.ShaderKind = P.Program->ShaderKind;
memcpy(Header.Bitcode.Magic, "DXIL", 4);
Expand Down
4 changes: 2 additions & 2 deletions llvm/tools/obj2yaml/dxcontainer2yaml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ dumpDXContainer(MemoryBufferRef Source) {
assert(DXIL && "Since we are iterating and found a DXIL part, "
"this should never not have a value");
NewPart.Program = DXContainerYAML::DXILProgram{
DXIL->first.MajorVersion,
DXIL->first.MinorVersion,
DXIL->first.getMajorVersion(),
DXIL->first.getMinorVersion(),
DXIL->first.ShaderKind,
DXIL->first.Size,
DXIL->first.Bitcode.MajorVersion,
Expand Down
43 changes: 43 additions & 0 deletions llvm/unittests/Object/DXContainerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,49 @@ TEST(DXCFile, ParseEmptyParts) {
}
}

// This test verify DXIL part are correctly parsed.
// This test is based on the binary output constructed from this yaml.
// --- !dxcontainer
// Header:
// Hash: [ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
// 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 ]
// Version:
// Major: 1
// Minor: 0
// PartCount: 1
// Parts:
// - Name: DXIL
// Size: 28
// Program:
// MajorVersion: 6
// MinorVersion: 5
// ShaderKind: 5
// Size: 8
// DXILMajorVersion: 1
// DXILMinorVersion: 5
// DXILSize: 4
// DXIL: [ 0x42, 0x43, 0xC0, 0xDE, ]
// ...
TEST(DXCFile, ParseDXILPart) {
uint8_t Buffer[] = {
0x44, 0x58, 0x42, 0x43, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
0x48, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x24, 0x00, 0x00, 0x00,
0x44, 0x58, 0x49, 0x4c, 0x1c, 0x00, 0x00, 0x00, 0x65, 0x00, 0x05, 0x00,
0x08, 0x00, 0x00, 0x00, 0x44, 0x58, 0x49, 0x4c, 0x05, 0x01, 0x00, 0x00,
0x10, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x42, 0x43, 0xc0, 0xde};
DXContainer C =
llvm::cantFail(DXContainer::create(getMemoryBuffer<116>(Buffer)));
EXPECT_EQ(C.getHeader().PartCount, 1u);
const std::optional<object::DXContainer::DXILData> &DXIL = C.getDXIL();
EXPECT_TRUE(DXIL.has_value());
dxbc::ProgramHeader Header = DXIL->first;
EXPECT_EQ(Header.getMajorVersion(), 6u);
EXPECT_EQ(Header.getMinorVersion(), 5u);
EXPECT_EQ(Header.ShaderKind, 5u);
EXPECT_EQ(Header.Size, 8u);
}

static Expected<DXContainer>
generateDXContainer(StringRef Yaml, SmallVectorImpl<char> &BinaryData) {
DXContainerYAML::Object Obj;
Expand Down