[llvm] 659ed24 - [DirectX] Add `only-section` to `llvm-objcopy` for `DXContainer` (#156372)

via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 8 08:45:44 PDT 2025


Author: Finn Plummer
Date: 2025-09-08T09:45:41-06:00
New Revision: 659ed245edfe4c84b6db22768f47854633e067bc

URL: https://github.com/llvm/llvm-project/commit/659ed245edfe4c84b6db22768f47854633e067bc
DIFF: https://github.com/llvm/llvm-project/commit/659ed245edfe4c84b6db22768f47854633e067bc.diff

LOG: [DirectX] Add `only-section` to `llvm-objcopy` for `DXContainer` (#156372)

This pr implements the `only-section` option for a `DXContainer` object
in `llvm-objcopy`.

It extends the base `removeParts` of the minimal object representation
of a `DXContainerObject`.

This a pre-requisite step to implement
https://github.com/llvm/llvm-project/issues/150286 as a way to strip all
other information away.

Added: 
    llvm/test/tools/llvm-objcopy/DXContainer/only-section-headers.test
    llvm/test/tools/llvm-objcopy/DXContainer/only-section-with-remove.test

Modified: 
    llvm/lib/ObjCopy/ConfigManager.cpp
    llvm/lib/ObjCopy/DXContainer/DXContainerObjcopy.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/ObjCopy/ConfigManager.cpp b/llvm/lib/ObjCopy/ConfigManager.cpp
index 2b17d728aad3f..24f3caccf7dc1 100644
--- a/llvm/lib/ObjCopy/ConfigManager.cpp
+++ b/llvm/lib/ObjCopy/ConfigManager.cpp
@@ -116,14 +116,13 @@ ConfigManager::getDXContainerConfig() const {
       !Common.AllocSectionsPrefix.empty() ||
       Common.DiscardMode != DiscardType::None || !Common.AddSection.empty() ||
       !Common.DumpSection.empty() || !Common.KeepSection.empty() ||
-      !Common.OnlySection.empty() || !Common.SectionsToRename.empty() ||
-      !Common.SetSectionAlignment.empty() || !Common.SetSectionFlags.empty() ||
-      !Common.SetSectionType.empty() || Common.ExtractDWO ||
-      Common.OnlyKeepDebug || Common.StripAllGNU || Common.StripDWO ||
-      Common.StripDebug || Common.StripNonAlloc || Common.StripSections ||
-      Common.StripUnneeded || Common.DecompressDebugSections ||
-      Common.GapFill != 0 || Common.PadTo != 0 ||
-      Common.ChangeSectionLMAValAll != 0 ||
+      !Common.SectionsToRename.empty() || !Common.SetSectionAlignment.empty() ||
+      !Common.SetSectionFlags.empty() || !Common.SetSectionType.empty() ||
+      Common.ExtractDWO || Common.OnlyKeepDebug || Common.StripAllGNU ||
+      Common.StripDWO || Common.StripDebug || Common.StripNonAlloc ||
+      Common.StripSections || Common.StripUnneeded ||
+      Common.DecompressDebugSections || Common.GapFill != 0 ||
+      Common.PadTo != 0 || Common.ChangeSectionLMAValAll != 0 ||
       !Common.ChangeSectionAddress.empty()) {
     return createStringError(
         llvm::errc::invalid_argument,

diff  --git a/llvm/lib/ObjCopy/DXContainer/DXContainerObjcopy.cpp b/llvm/lib/ObjCopy/DXContainer/DXContainerObjcopy.cpp
index 375e382ddb044..ac97dc462c142 100644
--- a/llvm/lib/ObjCopy/DXContainer/DXContainerObjcopy.cpp
+++ b/llvm/lib/ObjCopy/DXContainer/DXContainerObjcopy.cpp
@@ -28,6 +28,13 @@ static Error handleArgs(const CommonConfig &Config, Object &Obj) {
       return Config.ToRemove.matches(P.Name);
     };
 
+  if (!Config.OnlySection.empty())
+    RemovePred = [&Config](const Part &P) {
+      // Explicitly keep these sections regardless of previous removes and
+      // remove everything else.
+      return !Config.OnlySection.matches(P.Name);
+    };
+
   if (auto E = Obj.removeParts(RemovePred))
     return E;
 

diff  --git a/llvm/test/tools/llvm-objcopy/DXContainer/only-section-headers.test b/llvm/test/tools/llvm-objcopy/DXContainer/only-section-headers.test
new file mode 100644
index 0000000000000..012ba224654ae
--- /dev/null
+++ b/llvm/test/tools/llvm-objcopy/DXContainer/only-section-headers.test
@@ -0,0 +1,55 @@
+## Tests that the copied DXContainer correctly retains only the specified
+## headers.
+
+# RUN: yaml2obj %s -o %t
+# RUN: llvm-objcopy --only-section=FKE1 --only-section=FKE4 %t %t.out
+# RUN: obj2yaml %t.out | FileCheck %s --implicit-check-not=Name:
+
+## FileSize = 32 (header)
+##          + 8 (FKE1 content) + 1688 (FKE4 content)
+##          + 8 (2 part offsets) + 16 (2 part headers)
+##          = 1752
+# CHECK:      FileSize:    1752
+# CHECK-NEXT: PartCount:   2
+# CHECK-NEXT: PartOffsets: [ 40, 56 ]
+# CHECK-NEXT: Parts
+# CHECK-NEXT: Name: FKE1
+# CHECK-NEXT: Size: 8
+# CHECK-NEXT: Name: FKE4
+# CHECK-NEXT: Size: 1688
+
+--- !dxcontainer
+Header:
+  Hash:        [ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
+                 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 ]
+  Version:
+    Major:     1
+    Minor:     0
+  FileSize:    1996
+  PartCount:   7
+  PartOffsets: [ 60, 76, 92, 108, 236, 1932, 1960 ]
+Parts:
+  - Name: FKE0
+    Size: 8
+  - Name: FKE1
+    Size: 8
+  - Name: FKE2
+    Size: 8
+  - Name: FKE3
+    Size: 120
+  - Name: FKE4
+    Size: 1688
+  - Name: FKE5
+    Size: 20
+  - Name: DXIL
+    Size: 28
+    Program:
+      MajorVersion:     6
+      MinorVersion:     5
+      ShaderKind:       5
+      Size:             8
+      DXILMajorVersion: 1
+      DXILMinorVersion: 5
+      DXILSize:         4
+      DXIL:             [ 0x42, 0x43, 0xC0, 0xDE, ]
+...

diff  --git a/llvm/test/tools/llvm-objcopy/DXContainer/only-section-with-remove.test b/llvm/test/tools/llvm-objcopy/DXContainer/only-section-with-remove.test
new file mode 100644
index 0000000000000..4b1f56ee6f585
--- /dev/null
+++ b/llvm/test/tools/llvm-objcopy/DXContainer/only-section-with-remove.test
@@ -0,0 +1,98 @@
+## Tests that the copied DXContainer correctly retains only the RTS0 part even
+## when it was defined to be removed.
+
+# RUN: yaml2obj %s -o %t
+# RUN: llvm-objcopy --remove-section=RTS0 --only-section=RTS0 %t %t.out
+# RUN: obj2yaml %t.out | FileCheck %s --implicit-check-not=Name:
+
+## The DXContainer described below was generated with:
+
+## `clang-dxc -T cs_6_7 test.hlsl /Fo temp.dxo`
+## `obj2yaml temp.dxo`
+
+## ``` test.hlsl
+## [RootSignature("")]
+## [numthreads(1,1,1)]
+## void main() {}
+## ```
+
+## FileSize = 32 + 24 (RTS0 content) + 4 (1 part offset) + 8 (1 part header)
+##          = 68
+# CHECK:      FileSize:       68
+# CHECK-NEXT: PartCount:      1
+# CHECK-NEXT: PartOffsets:     [ 36 ]
+# CHECK-NEXT: Parts
+# CHECK-NEXT: Name: RTS0
+
+--- !dxcontainer
+Header:
+  Hash:            [ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
+                     0x0, 0x0, 0x0, 0x0, 0x0, 0x0 ]
+  Version:
+    Major:           1
+    Minor:           0
+  FileSize:        1984
+  PartCount:       7
+  PartOffsets:     [ 60, 1792, 1808, 1836, 1852, 1868, 1900 ]
+Parts:
+  - Name:            DXIL
+    Size:            1724
+    Program:
+      MajorVersion:    6
+      MinorVersion:    7
+      ShaderKind:      5
+      Size:            28
+      DXILMajorVersion: 1
+      DXILMinorVersion: 7
+      DXILSize:        4
+      DXIL:            [ 0x42, 0x43, 0xC0, 0xDE, ]
+  - Name:            SFI0
+    Size:            8
+  - Name:            HASH
+    Size:            20
+    Hash:
+      IncludesSource:  false
+      Digest:          [ 0x9F, 0xD1, 0xD9, 0xE2, 0x49, 0xFB, 0x3A, 0x6C,
+                         0x8C, 0x14, 0x8A, 0x96, 0x1C, 0x7D, 0x85, 0xA9 ]
+  - Name:            ISG1
+    Size:            8
+    Signature:
+      Parameters:      []
+  - Name:            OSG1
+    Size:            8
+    Signature:
+      Parameters:      []
+  - Name:            RTS0
+    Size:            24
+    RootSignature:
+      Version:         2
+      NumRootParameters: 0
+      RootParametersOffset: 24
+      NumStaticSamplers: 0
+      StaticSamplersOffset: 24
+      Parameters:      []
+  - Name:            PSV0
+    Size:            76
+    PSVInfo:
+      Version:         3
+      ShaderStage:     5
+      MinimumWaveLaneCount: 0
+      MaximumWaveLaneCount: 4294967295
+      UsesViewID:      0
+      SigInputVectors: 0
+      SigOutputVectors: [ 0, 0, 0, 0 ]
+      NumThreadsX:     1
+      NumThreadsY:     1
+      NumThreadsZ:     1
+      EntryName:       main
+      ResourceStride:  24
+      Resources:       []
+      SigInputElements: []
+      SigOutputElements: []
+      SigPatchOrPrimElements: []
+      InputOutputMap:
+        - [  ]
+        - [  ]
+        - [  ]
+        - [  ]
+...


        


More information about the llvm-commits mailing list