Skip to content

[DirectX] Reapply Fix DXIL part header version encoding #91956

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 13, 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
2 changes: 1 addition & 1 deletion llvm/include/llvm/BinaryFormat/DXContainer.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ struct PartHeader {

struct BitcodeHeader {
uint8_t Magic[4]; // ACSII "DXIL".
uint8_t MajorVersion; // DXIL version.
uint8_t MinorVersion; // DXIL version.
uint8_t MajorVersion; // DXIL version.
uint16_t Unused;
uint32_t Offset; // Offset to LLVM bitcode (from start of header).
uint32_t Size; // Size of LLVM bitcode (in bytes).
Expand Down
2 changes: 1 addition & 1 deletion llvm/include/llvm/TargetParser/Triple.h
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ class Triple {
/// (SubArch). This should only be called with Vulkan SPIR-V triples.
VersionTuple getVulkanVersion() const;

/// Parse the DXIL version number from the DXIL version
/// Parse the DXIL version number from the OSVersion and DXIL version
/// (SubArch). This should only be called with DXIL triples.
VersionTuple getDXILVersion() const;

Expand Down
3 changes: 3 additions & 0 deletions llvm/lib/MC/MCDXContainerWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ uint64_t DXContainerObjectWriter::writeObject(MCAssembler &Asm,
// The program header's size field is in 32-bit words.
Header.Size = (SectionSize + sizeof(dxbc::ProgramHeader) + 3) / 4;
memcpy(Header.Bitcode.Magic, "DXIL", 4);
VersionTuple DXILVersion = TT.getDXILVersion();
Header.Bitcode.MajorVersion = DXILVersion.getMajor();
Header.Bitcode.MinorVersion = DXILVersion.getMinor().value_or(0);
Header.Bitcode.Offset = sizeof(dxbc::BitcodeHeader);
Header.Bitcode.Size = SectionSize;
if (sys::IsBigEndianHost)
Expand Down
2 changes: 2 additions & 0 deletions llvm/lib/TargetParser/Triple.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1510,6 +1510,8 @@ VersionTuple Triple::getDXILVersion() const {
if (getArch() != dxil || getOS() != ShaderModel)
llvm_unreachable("invalid DXIL triple");
StringRef Arch = getArchName();
if (getSubArch() == NoSubArch)
Arch = getDXILArchNameFromShaderModel(getOSName());
Arch.consume_front("dxilv");
VersionTuple DXILVersion = parseVersionFromName(Arch);
// FIXME: validate DXIL version against Shader Model version.
Expand Down
4 changes: 2 additions & 2 deletions llvm/test/CodeGen/DirectX/embed-dxil.ll
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ define i32 @add(i32 %a, i32 %b) {
; DXC-NEXT: MinorVersion: 5
; DXC-NEXT: ShaderKind: 6
; DXC-NEXT: Size: [[#div(SIZE,4)]]
; DXC-NEXT: DXILMajorVersion: [[#]]
; DXC-NEXT: DXILMinorVersion: [[#]]
; DXC-NEXT: DXILMajorVersion: 1
; DXC-NEXT: DXILMinorVersion: 5
; DXC-NEXT: DXILSize: [[#SIZE - 24]]
; DXC-NEXT: DXIL: [ 0x42, 0x43, 0xC0, 0xDE,
; DXC: - Name: SFI0
Expand Down
16 changes: 9 additions & 7 deletions llvm/unittests/Object/DXContainerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,8 @@ TEST(DXCFile, ParseDXILPart) {
EXPECT_EQ(Header.getMinorVersion(), 5u);
EXPECT_EQ(Header.ShaderKind, 5u);
EXPECT_EQ(Header.Size, 8u);
EXPECT_EQ(Header.Bitcode.MajorVersion, 1u);
EXPECT_EQ(Header.Bitcode.MinorVersion, 5u);
}

static Expected<DXContainer>
Expand Down Expand Up @@ -283,8 +285,8 @@ TEST(DXCFile, PSVResourceIterators) {
MinorVersion: 0
ShaderKind: 14
Size: 6
DXILMajorVersion: 0
DXILMinorVersion: 1
DXILMajorVersion: 1
DXILMinorVersion: 0
DXILSize: 0
...
)";
Expand Down Expand Up @@ -404,8 +406,8 @@ TEST(DXCFile, PSVResourceIterators) {
// MinorVersion: 0
// ShaderKind: 14
// Size: 6
// DXILMajorVersion: 0
// DXILMinorVersion: 1
// DXILMajorVersion: 1
// DXILMinorVersion: 0
// DXILSize: 0
// - Name: PSV0
// Size: 36
Expand Down Expand Up @@ -520,7 +522,7 @@ TEST(DXCFile, MaliciousFiles) {
//
// --- !dxcontainer
// Header:
// Hash: [ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
// Hash: [ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
// 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 ]
// Version:
// Major: 1
Expand All @@ -534,8 +536,8 @@ TEST(DXCFile, MaliciousFiles) {
// MinorVersion: 0
// ShaderKind: 14
// Size: 6
// DXILMajorVersion: 0
// DXILMinorVersion: 1
// DXILMajorVersion: 1
// DXILMinorVersion: 0
// DXILSize: 0
// - Name: PSV0
// Size: 100
Expand Down
Loading