Skip to content

Commit 87a1d42

Browse files
authored
[DirectX] Add support for remove-section of DXContainer for llvm-objcopy (#153246)
This pr implements the `remove-section` option for a `DXContainer` object in `llvm-objcopy`. It implements a base `removeParts` to the minimal `object` representation of a `DXContainerObject`. This is the second step to implement #150275 as a compiler actions that invokes llvm-objcopy for functionality.
1 parent a9de1ab commit 87a1d42

File tree

7 files changed

+409
-8
lines changed

7 files changed

+409
-8
lines changed

llvm/lib/ObjCopy/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ source_group("Source Files\\DXContainer" REGULAR_EXPRESSION
4444
add_llvm_component_library(LLVMObjCopy
4545
Archive.cpp
4646
DXContainer/DXContainerObjcopy.cpp
47+
DXContainer/DXContainerObject.cpp
4748
DXContainer/DXContainerReader.cpp
4849
DXContainer/DXContainerWriter.cpp
4950
CommonConfig.cpp

llvm/lib/ObjCopy/ConfigManager.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,14 @@ ConfigManager::getDXContainerConfig() const {
116116
!Common.AllocSectionsPrefix.empty() ||
117117
Common.DiscardMode != DiscardType::None || !Common.AddSection.empty() ||
118118
!Common.DumpSection.empty() || !Common.KeepSection.empty() ||
119-
!Common.OnlySection.empty() || !Common.ToRemove.empty() ||
120-
!Common.SectionsToRename.empty() || !Common.SetSectionAlignment.empty() ||
121-
!Common.SetSectionFlags.empty() || !Common.SetSectionType.empty() ||
122-
Common.ExtractDWO || Common.OnlyKeepDebug || Common.StripAllGNU ||
123-
Common.StripDWO || Common.StripDebug || Common.StripNonAlloc ||
124-
Common.StripSections || Common.StripUnneeded ||
125-
Common.DecompressDebugSections || Common.GapFill != 0 ||
126-
Common.PadTo != 0 || Common.ChangeSectionLMAValAll != 0 ||
119+
!Common.OnlySection.empty() || !Common.SectionsToRename.empty() ||
120+
!Common.SetSectionAlignment.empty() || !Common.SetSectionFlags.empty() ||
121+
!Common.SetSectionType.empty() || Common.ExtractDWO ||
122+
Common.OnlyKeepDebug || Common.StripAllGNU || Common.StripDWO ||
123+
Common.StripDebug || Common.StripNonAlloc || Common.StripSections ||
124+
Common.StripUnneeded || Common.DecompressDebugSections ||
125+
Common.GapFill != 0 || Common.PadTo != 0 ||
126+
Common.ChangeSectionLMAValAll != 0 ||
127127
!Common.ChangeSectionAddress.empty()) {
128128
return createStringError(
129129
llvm::errc::invalid_argument,

llvm/lib/ObjCopy/DXContainer/DXContainerObjcopy.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,19 @@ namespace dxbc {
1919
using namespace object;
2020

2121
static Error handleArgs(const CommonConfig &Config, Object &Obj) {
22+
std::function<bool(const Part &)> RemovePred = [](const Part &) {
23+
return false;
24+
};
25+
26+
if (!Config.ToRemove.empty())
27+
RemovePred = [&Config](const Part &P) {
28+
return Config.ToRemove.matches(P.Name);
29+
};
30+
31+
if (auto E = Obj.removeParts(RemovePred))
32+
return E;
33+
34+
Obj.recomputeHeader();
2235
return Error::success();
2336
}
2437

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//===- DXContainerObject.cpp ----------------------------------------------===//
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+
#include "DXContainerObject.h"
10+
11+
namespace llvm {
12+
namespace objcopy {
13+
namespace dxbc {
14+
15+
Error Object::removeParts(PartPred ToRemove) {
16+
erase_if(Parts, ToRemove);
17+
return Error::success();
18+
}
19+
20+
void Object::recomputeHeader() {
21+
Header.FileSize = headerSize();
22+
Header.PartCount = Parts.size();
23+
for (const Part &P : Parts)
24+
Header.FileSize += P.size();
25+
}
26+
27+
} // end namespace dxbc
28+
} // end namespace objcopy
29+
} // end namespace llvm

llvm/lib/ObjCopy/DXContainer/DXContainerObject.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ struct Part {
3030
}
3131
};
3232

33+
using PartPred = llvm::function_ref<bool(const Part &)>;
34+
3335
struct Object {
3436
::llvm::dxbc::Header Header;
3537
SmallVector<Part> Parts;
@@ -38,6 +40,9 @@ struct Object {
3840
return sizeof(::llvm::dxbc::Header) // base header
3941
+ sizeof(uint32_t) * Parts.size(); // part offset values
4042
}
43+
44+
Error removeParts(PartPred ToRemove);
45+
void recomputeHeader();
4146
};
4247

4348
} // end namespace dxbc
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
## Tests that the copied DXContainer correctly has the specified headers
2+
## removed.
3+
4+
# RUN: yaml2obj %s -o %t
5+
# RUN: llvm-objcopy --remove-section=FKE1 --remove-section=FKE4 %t %t.out
6+
# RUN: obj2yaml %t.out | FileCheck %s
7+
8+
--- !dxcontainer
9+
Header:
10+
Hash: [ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
11+
0x0, 0x0, 0x0, 0x0, 0x0, 0x0 ]
12+
Version:
13+
Major: 1
14+
Minor: 0
15+
## FileSize = 1996 - 8 (FKE1 content) - 1688 (FKE4 content)
16+
## - 8 (2 part offsets) - 16 (2 part headers)
17+
## = 276
18+
# CHECK: FileSize: 276
19+
FileSize: 1996
20+
# CHECK-NEXT: PartCount: 5
21+
PartCount: 7
22+
# CHECK-NEXT: PartOffsets: [ 52, 68, 84, 212, 240 ]
23+
PartOffsets: [ 60, 76, 92, 108, 236, 1932, 1960 ]
24+
Parts:
25+
# CHECK-NEXT: Parts
26+
# CHECK-NOT: FKE1
27+
# CHECK-NOT: FKE4
28+
- Name: FKE0
29+
Size: 8
30+
- Name: FKE1
31+
Size: 8
32+
- Name: FKE2
33+
Size: 8
34+
- Name: FKE3
35+
Size: 120
36+
- Name: FKE4
37+
Size: 1688
38+
- Name: FKE5
39+
Size: 20
40+
- Name: DXIL
41+
Size: 28
42+
Program:
43+
MajorVersion: 6
44+
MinorVersion: 5
45+
ShaderKind: 5
46+
Size: 8
47+
DXILMajorVersion: 1
48+
DXILMinorVersion: 5
49+
DXILSize: 4
50+
DXIL: [ 0x42, 0x43, 0xC0, 0xDE, ]
51+
...

0 commit comments

Comments
 (0)