Skip to content

Commit 531a0b6

Browse files
authored
[DirectX] Reapply Fix DXIL part header version encoding (#91956)
This reapplies 195d8ac [DirectX] Fix DXIL part header version encoding. The endian issue was fixed by f42117c. Move MinorVersion be the lower 8 bit. Set DXIL version in DXContainerObjectWriter::writeObject. Fixes #89952
1 parent 943baf3 commit 531a0b6

File tree

6 files changed

+18
-11
lines changed

6 files changed

+18
-11
lines changed

llvm/include/llvm/BinaryFormat/DXContainer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ struct PartHeader {
103103

104104
struct BitcodeHeader {
105105
uint8_t Magic[4]; // ACSII "DXIL".
106-
uint8_t MajorVersion; // DXIL version.
107106
uint8_t MinorVersion; // DXIL version.
107+
uint8_t MajorVersion; // DXIL version.
108108
uint16_t Unused;
109109
uint32_t Offset; // Offset to LLVM bitcode (from start of header).
110110
uint32_t Size; // Size of LLVM bitcode (in bytes).

llvm/include/llvm/TargetParser/Triple.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ class Triple {
429429
/// (SubArch). This should only be called with Vulkan SPIR-V triples.
430430
VersionTuple getVulkanVersion() const;
431431

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

llvm/lib/MC/MCDXContainerWriter.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@ uint64_t DXContainerObjectWriter::writeObject(MCAssembler &Asm,
129129
// The program header's size field is in 32-bit words.
130130
Header.Size = (SectionSize + sizeof(dxbc::ProgramHeader) + 3) / 4;
131131
memcpy(Header.Bitcode.Magic, "DXIL", 4);
132+
VersionTuple DXILVersion = TT.getDXILVersion();
133+
Header.Bitcode.MajorVersion = DXILVersion.getMajor();
134+
Header.Bitcode.MinorVersion = DXILVersion.getMinor().value_or(0);
132135
Header.Bitcode.Offset = sizeof(dxbc::BitcodeHeader);
133136
Header.Bitcode.Size = SectionSize;
134137
if (sys::IsBigEndianHost)

llvm/lib/TargetParser/Triple.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1510,6 +1510,8 @@ VersionTuple Triple::getDXILVersion() const {
15101510
if (getArch() != dxil || getOS() != ShaderModel)
15111511
llvm_unreachable("invalid DXIL triple");
15121512
StringRef Arch = getArchName();
1513+
if (getSubArch() == NoSubArch)
1514+
Arch = getDXILArchNameFromShaderModel(getOSName());
15131515
Arch.consume_front("dxilv");
15141516
VersionTuple DXILVersion = parseVersionFromName(Arch);
15151517
// FIXME: validate DXIL version against Shader Model version.

llvm/test/CodeGen/DirectX/embed-dxil.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ define i32 @add(i32 %a, i32 %b) {
4242
; DXC-NEXT: MinorVersion: 5
4343
; DXC-NEXT: ShaderKind: 6
4444
; DXC-NEXT: Size: [[#div(SIZE,4)]]
45-
; DXC-NEXT: DXILMajorVersion: [[#]]
46-
; DXC-NEXT: DXILMinorVersion: [[#]]
45+
; DXC-NEXT: DXILMajorVersion: 1
46+
; DXC-NEXT: DXILMinorVersion: 5
4747
; DXC-NEXT: DXILSize: [[#SIZE - 24]]
4848
; DXC-NEXT: DXIL: [ 0x42, 0x43, 0xC0, 0xDE,
4949
; DXC: - Name: SFI0

llvm/unittests/Object/DXContainerTest.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,8 @@ TEST(DXCFile, ParseDXILPart) {
215215
EXPECT_EQ(Header.getMinorVersion(), 5u);
216216
EXPECT_EQ(Header.ShaderKind, 5u);
217217
EXPECT_EQ(Header.Size, 8u);
218+
EXPECT_EQ(Header.Bitcode.MajorVersion, 1u);
219+
EXPECT_EQ(Header.Bitcode.MinorVersion, 5u);
218220
}
219221

220222
static Expected<DXContainer>
@@ -283,8 +285,8 @@ TEST(DXCFile, PSVResourceIterators) {
283285
MinorVersion: 0
284286
ShaderKind: 14
285287
Size: 6
286-
DXILMajorVersion: 0
287-
DXILMinorVersion: 1
288+
DXILMajorVersion: 1
289+
DXILMinorVersion: 0
288290
DXILSize: 0
289291
...
290292
)";
@@ -404,8 +406,8 @@ TEST(DXCFile, PSVResourceIterators) {
404406
// MinorVersion: 0
405407
// ShaderKind: 14
406408
// Size: 6
407-
// DXILMajorVersion: 0
408-
// DXILMinorVersion: 1
409+
// DXILMajorVersion: 1
410+
// DXILMinorVersion: 0
409411
// DXILSize: 0
410412
// - Name: PSV0
411413
// Size: 36
@@ -520,7 +522,7 @@ TEST(DXCFile, MaliciousFiles) {
520522
//
521523
// --- !dxcontainer
522524
// Header:
523-
// Hash: [ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
525+
// Hash: [ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
524526
// 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 ]
525527
// Version:
526528
// Major: 1
@@ -534,8 +536,8 @@ TEST(DXCFile, MaliciousFiles) {
534536
// MinorVersion: 0
535537
// ShaderKind: 14
536538
// Size: 6
537-
// DXILMajorVersion: 0
538-
// DXILMinorVersion: 1
539+
// DXILMajorVersion: 1
540+
// DXILMinorVersion: 0
539541
// DXILSize: 0
540542
// - Name: PSV0
541543
// Size: 100

0 commit comments

Comments
 (0)