Skip to content

Commit acfd4d5

Browse files
committed
[HLSL] Add the Qstrip-rootsignature DXC driver option
1 parent 0f11eb0 commit acfd4d5

File tree

6 files changed

+83
-2
lines changed

6 files changed

+83
-2
lines changed

clang/include/clang/Basic/CodeGenOptions.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,9 @@ CODEGENOPT(StaticClosure, 1, 0, Benign)
479479
/// Assume that UAVs/SRVs may alias
480480
CODEGENOPT(ResMayAlias, 1, 0, Benign)
481481

482+
/// Omit the root signature from produced DXContainer
483+
CODEGENOPT(HLSLRootSigStrip, 1, 0, Benign)
484+
482485
/// Controls how unwind v2 (epilog) information should be generated for x64
483486
/// Windows.
484487
ENUM_CODEGENOPT(WinX64EHUnwindV2, WinX64EHUnwindV2Mode,

clang/include/clang/Driver/Options.td

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9404,6 +9404,12 @@ def res_may_alias : Option<["/", "-"], "res-may-alias", KIND_FLAG>,
94049404
Visibility<[DXCOption, ClangOption, CC1Option]>,
94059405
HelpText<"Assume that UAVs/SRVs may alias">,
94069406
MarshallingInfoFlag<CodeGenOpts<"ResMayAlias">>;
9407+
def dxc_strip_rootsignature :
9408+
Option<["/", "-"], "Qstrip-rootsignature", KIND_FLAG>,
9409+
Group<dxc_Group>,
9410+
Visibility<[DXCOption]>,
9411+
HelpText<"Omit the root signature from produced DXContainer">,
9412+
MarshallingInfoFlag<CodeGenOpts<"HLSLRootSigStrip">>;
94079413
def target_profile : DXCJoinedOrSeparate<"T">, MetaVarName<"<profile>">,
94089414
HelpText<"Set target profile">,
94099415
Values<"ps_6_0, ps_6_1, ps_6_2, ps_6_3, ps_6_4, ps_6_5, ps_6_6, ps_6_7,"

clang/lib/Driver/Driver.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4662,6 +4662,16 @@ void Driver::BuildDefaultActions(Compilation &C, DerivedArgList &Args,
46624662
Actions.push_back(C.MakeAction<BinaryTranslatorJobAction>(
46634663
LastAction, types::TY_DX_CONTAINER));
46644664
}
4665+
if (TC.requiresObjcopy(Args)) {
4666+
Action *LastAction = Actions.back();
4667+
// llvm-objcopy expects a DXIL container, which can either be
4668+
// validated (in which case they are TY_DX_CONTAINER), or unvalidated
4669+
// (TY_OBJECT).
4670+
if (LastAction->getType() == types::TY_DX_CONTAINER ||
4671+
LastAction->getType() == types::TY_Object)
4672+
Actions.push_back(C.MakeAction<BinaryModifyJobAction>(
4673+
LastAction, types::TY_DX_CONTAINER));
4674+
}
46654675
}
46664676

46674677
// Claim ignored clang-cl options.

clang/lib/Driver/ToolChains/HLSL.cpp

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,30 @@ void tools::hlsl::MetalConverter::ConstructJob(
295295
Exec, CmdArgs, Inputs, Input));
296296
}
297297

298+
void tools::hlsl::LLVMObjcopy::ConstructJob(Compilation &C, const JobAction &JA,
299+
const InputInfo &Output,
300+
const InputInfoList &Inputs,
301+
const ArgList &Args,
302+
const char *LinkingOutput) const {
303+
304+
std::string ObjcopyPath = getToolChain().GetProgramPath("llvm-objcopy");
305+
const char *Exec = Args.MakeArgString(ObjcopyPath);
306+
307+
ArgStringList CmdArgs;
308+
assert(Inputs.size() == 1 && "Unable to handle multiple inputs.");
309+
const InputInfo &Input = Inputs[0];
310+
CmdArgs.push_back(Input.getFilename());
311+
CmdArgs.push_back(Output.getFilename());
312+
313+
if (Args.hasArg(options::OPT_dxc_strip_rootsignature)) {
314+
const char *Frs = Args.MakeArgString("--remove-section=RTS0");
315+
CmdArgs.push_back(Frs);
316+
}
317+
318+
C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(),
319+
Exec, CmdArgs, Inputs, Input));
320+
}
321+
298322
/// DirectX Toolchain
299323
HLSLToolChain::HLSLToolChain(const Driver &D, const llvm::Triple &Triple,
300324
const ArgList &Args)
@@ -315,6 +339,10 @@ Tool *clang::driver::toolchains::HLSLToolChain::getTool(
315339
if (!MetalConverter)
316340
MetalConverter.reset(new tools::hlsl::MetalConverter(*this));
317341
return MetalConverter.get();
342+
case Action::BinaryModifyJobClass:
343+
if (!LLVMObjcopy)
344+
LLVMObjcopy.reset(new tools::hlsl::LLVMObjcopy(*this));
345+
return LLVMObjcopy.get();
318346
default:
319347
return ToolChain::getTool(AC);
320348
}
@@ -452,16 +480,25 @@ bool HLSLToolChain::requiresBinaryTranslation(DerivedArgList &Args) const {
452480
return Args.hasArg(options::OPT_metal) && Args.hasArg(options::OPT_dxc_Fo);
453481
}
454482

483+
bool HLSLToolChain::requiresObjcopy(DerivedArgList &Args) const {
484+
return Args.hasArg(options::OPT_dxc_Fo) &&
485+
Args.hasArg(options::OPT_dxc_strip_rootsignature);
486+
}
487+
455488
bool HLSLToolChain::isLastJob(DerivedArgList &Args,
456489
Action::ActionClass AC) const {
457490
bool HasTranslation = requiresBinaryTranslation(Args);
458491
bool HasValidation = requiresValidation(Args);
492+
bool HasObjcopy = requiresObjcopy(Args);
459493
// If translation and validation are not required, we should only have one
460494
// action.
461-
if (!HasTranslation && !HasValidation)
495+
if (!HasTranslation && !HasValidation && !HasObjcopy)
462496
return true;
463497
if ((HasTranslation && AC == Action::BinaryTranslatorJobClass) ||
464-
(!HasTranslation && HasValidation && AC == Action::BinaryAnalyzeJobClass))
498+
(!HasTranslation && HasValidation &&
499+
AC == Action::BinaryAnalyzeJobClass) ||
500+
(!HasTranslation && !HasValidation && HasObjcopy &&
501+
AC == Action::BinaryModifyJobClass))
465502
return true;
466503
return false;
467504
}

clang/lib/Driver/ToolChains/HLSL.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,19 @@ class LLVM_LIBRARY_VISIBILITY MetalConverter : public Tool {
4242
const llvm::opt::ArgList &TCArgs,
4343
const char *LinkingOutput) const override;
4444
};
45+
46+
class LLVM_LIBRARY_VISIBILITY LLVMObjcopy : public Tool {
47+
public:
48+
LLVMObjcopy(const ToolChain &TC)
49+
: Tool("hlsl::LLVMObjcopy", "llvm-objcopy", TC) {}
50+
51+
bool hasIntegratedCPP() const override { return false; }
52+
53+
void ConstructJob(Compilation &C, const JobAction &JA,
54+
const InputInfo &Output, const InputInfoList &Inputs,
55+
const llvm::opt::ArgList &TCArgs,
56+
const char *LinkingOutput) const override;
57+
};
4558
} // namespace hlsl
4659
} // namespace tools
4760

@@ -65,6 +78,7 @@ class LLVM_LIBRARY_VISIBILITY HLSLToolChain : public ToolChain {
6578
static std::optional<std::string> parseTargetProfile(StringRef TargetProfile);
6679
bool requiresValidation(llvm::opt::DerivedArgList &Args) const;
6780
bool requiresBinaryTranslation(llvm::opt::DerivedArgList &Args) const;
81+
bool requiresObjcopy(llvm::opt::DerivedArgList &Args) const;
6882
bool isLastJob(llvm::opt::DerivedArgList &Args, Action::ActionClass AC) const;
6983

7084
// Set default DWARF version to 4 for DXIL uses version 4.
@@ -73,6 +87,7 @@ class LLVM_LIBRARY_VISIBILITY HLSLToolChain : public ToolChain {
7387
private:
7488
mutable std::unique_ptr<tools::hlsl::Validator> Validator;
7589
mutable std::unique_ptr<tools::hlsl::MetalConverter> MetalConverter;
90+
mutable std::unique_ptr<tools::hlsl::LLVMObjcopy> LLVMObjcopy;
7691
};
7792

7893
} // end namespace toolchains
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// RUN: %clang_dxc -Qstrip-rootsignature -T cs_6_0 /Fo %t -### %s 2>&1 | FileCheck %s
2+
3+
// Test to demonstrate that we specify to the root signature with the
4+
// -Qstrip-rootsignature option
5+
6+
// CHECK: "{{.*}}llvm-objcopy{{.*}}" "{{.*}}" "{{.*}}" "--remove-section=RTS0"
7+
8+
[shader("compute"), RootSignature("")]
9+
[numthreads(1,1,1)]
10+
void EmptyEntry() {}

0 commit comments

Comments
 (0)