Skip to content

Commit 162180d

Browse files
bob80905llvm-beanz
andauthored
[HLSL] Add -HV option translation to clang-dxc.exe (#83938)
Previously, clang-dxc.exe would not recognize -HV as a valid argument to DXC, and would be unable to translate the argument to a legal clang argument. This PR implements a translation of the HV option and its value to the appropriate clang flag and the appropriate value. It adds a test by using the -### option to spit out the translated options, and checks to see that the correct option was generated. Fixes #83479 --------- Co-authored-by: Chris B <[email protected]>
1 parent 274db64 commit 162180d

File tree

5 files changed

+51
-0
lines changed

5 files changed

+51
-0
lines changed

clang/include/clang/Basic/LangStandard.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ struct LangStandard {
139139
bool isOpenCL() const { return Flags & OpenCL; }
140140

141141
static Kind getLangKind(StringRef Name);
142+
static Kind getHLSLLangKind(StringRef Name);
142143
static const LangStandard &getLangStandardForKind(Kind K);
143144
static const LangStandard *getLangStandardForName(StringRef Name);
144145
};

clang/include/clang/Driver/Options.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8578,6 +8578,11 @@ def dxc_entrypoint : Option<["--", "/", "-"], "E", KIND_JOINED_OR_SEPARATE>,
85788578
Group<dxc_Group>,
85798579
Visibility<[DXCOption]>,
85808580
HelpText<"Entry point name">;
8581+
def dxc_hlsl_version : Option<["/", "-"], "HV", KIND_JOINED_OR_SEPARATE>,
8582+
Group<dxc_Group>,
8583+
Visibility<[DXCOption]>,
8584+
HelpText<"HLSL Version">,
8585+
Values<"2016, 2017, 2018, 2021, 202x">;
85818586
def dxc_validator_path_EQ : Joined<["--"], "dxv-path=">, Group<dxc_Group>,
85828587
HelpText<"DXIL validator installation path">;
85838588
def dxc_disable_validation : DXCFlag<"Vd">,

clang/lib/Basic/LangStandards.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,16 @@ LangStandard::Kind LangStandard::getLangKind(StringRef Name) {
6969
.Default(lang_unspecified);
7070
}
7171

72+
LangStandard::Kind LangStandard::getHLSLLangKind(StringRef Name) {
73+
return llvm::StringSwitch<LangStandard::Kind>(Name)
74+
.Case("2016", LangStandard::lang_hlsl2016)
75+
.Case("2017", LangStandard::lang_hlsl2017)
76+
.Case("2018", LangStandard::lang_hlsl2018)
77+
.Case("2021", LangStandard::lang_hlsl2021)
78+
.Case("202x", LangStandard::lang_hlsl202x)
79+
.Default(LangStandard::lang_unspecified);
80+
}
81+
7282
const LangStandard *LangStandard::getLangStandardForName(StringRef Name) {
7383
Kind K = getLangKind(Name);
7484
if (K == lang_unspecified)

clang/lib/Driver/ToolChains/HLSL.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,21 @@ HLSLToolChain::TranslateArgs(const DerivedArgList &Args, StringRef BoundArch,
226226
A->claim();
227227
continue;
228228
}
229+
if (A->getOption().getID() == options::OPT_dxc_hlsl_version) {
230+
// Translate -HV into -std for llvm
231+
// depending on the value given
232+
LangStandard::Kind LangStd = LangStandard::getHLSLLangKind(A->getValue());
233+
if (LangStd != LangStandard::lang_unspecified) {
234+
LangStandard l = LangStandard::getLangStandardForKind(LangStd);
235+
DAL->AddSeparateArg(nullptr, Opts.getOption(options::OPT_std_EQ),
236+
l.getName());
237+
} else {
238+
getDriver().Diag(diag::err_drv_invalid_value) << "HV" << A->getValue();
239+
}
240+
241+
A->claim();
242+
continue;
243+
}
229244
DAL->append(A);
230245
}
231246

clang/test/Options/HV.hlsl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// RUN: %clang_dxc -T lib_6_4 -HV 2016 %s 2>&1 -### | FileCheck -check-prefix=2016 %s
2+
// RUN: %clang_dxc -T lib_6_4 -HV 2017 %s 2>&1 -### | FileCheck -check-prefix=2017 %s
3+
// RUN: %clang_dxc -T lib_6_4 /HV 2018 %s 2>&1 -### | FileCheck -check-prefix=2018 %s
4+
// RUN: %clang_dxc -T lib_6_4 /HV 2021 %s 2>&1 -### | FileCheck -check-prefix=2021 %s
5+
// RUN: %clang_dxc -T lib_6_4 /HV 202x %s 2>&1 -### | FileCheck -check-prefix=202x %s
6+
// RUN: %clang_dxc -T lib_6_4 %s 2>&1 -### | FileCheck -check-prefix=NO_HV %s
7+
// RUN: not %clang_dxc -T lib_6_4 /HV gibberish -### %s 2>&1 | FileCheck -check-prefix=CHECK-ERR %s
8+
9+
// 2016: "-std=hlsl2016"
10+
// 2017: "-std=hlsl2017"
11+
// 2018: "-std=hlsl2018"
12+
// 2021: "-std=hlsl2021"
13+
// 202x: "-std=hlsl202x"
14+
// NO_HV-NOT: "-std="
15+
// CHECK-ERR: error: invalid value 'gibberish' in 'HV'
16+
float4 main(float4 a : A) : SV_TARGET
17+
{
18+
return -a.yxxx;
19+
}
20+

0 commit comments

Comments
 (0)