Skip to content

Commit 6f10dcc

Browse files
bob80905damyanpllvm-beanz
authored
[HLSL] Add validation for the -enable-16bit-types option (#85340)
Previously, the clang compiler with the dxc driver would accept the -enable-16bit-types flag without checking to see if the required conditions are met for proper processing of the flag. Specifically, -enable-16bit-types requires a shader model of at least 6.2 and an HLSL version of at least 2021. This PR adds a validation check for these other options having the required values, and emits an error if these constraints are not met. Fixes #57876 --------- Co-authored-by: Damyan Pepper <[email protected]> Co-authored-by: Chris B <[email protected]>
1 parent 6e58efa commit 6f10dcc

File tree

6 files changed

+62
-5
lines changed

6 files changed

+62
-5
lines changed

clang/include/clang/Basic/DiagnosticDriverKinds.td

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -753,7 +753,8 @@ def err_drv_hlsl_unsupported_target : Error<
753753
"HLSL code generation is unsupported for target '%0'">;
754754
def err_drv_hlsl_bad_shader_required_in_target : Error<
755755
"%select{shader model|Vulkan environment|shader stage}0 is required as %select{OS|environment}1 in target '%2' for HLSL code generation">;
756-
756+
def err_drv_hlsl_16bit_types_unsupported: Error<
757+
"'%0' option requires target HLSL Version >= 2018%select{| and shader model >= 6.2}1, but HLSL Version is '%2'%select{| and shader model is '%3'}1">;
757758
def err_drv_hlsl_bad_shader_unsupported : Error<
758759
"%select{shader model|Vulkan environment|shader stage}0 '%1' in target '%2' is invalid for HLSL code generation">;
759760
def warn_drv_dxc_missing_dxv : Warning<"dxv not found. "

clang/lib/Driver/ToolChains/HLSL.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,9 +255,7 @@ HLSLToolChain::TranslateArgs(const DerivedArgList &Args, StringRef BoundArch,
255255
if (!DAL->hasArg(options::OPT_O_Group)) {
256256
DAL->AddJoinedArg(nullptr, Opts.getOption(options::OPT_O), "3");
257257
}
258-
// FIXME: add validation for enable_16bit_types should be after HLSL 2018 and
259-
// shader model 6.2.
260-
// See: https://github.com/llvm/llvm-project/issues/57876
258+
261259
return DAL;
262260
}
263261

clang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4284,11 +4284,30 @@ bool CompilerInvocation::ParseLangArgs(LangOptions &Opts, ArgList &Args,
42844284
Diags.Report(diag::err_drv_hlsl_bad_shader_unsupported)
42854285
<< ShaderModel << T.getOSName() << T.str();
42864286
}
4287+
// Validate that if fnative-half-type is given, that
4288+
// the language standard is at least hlsl2018, and that
4289+
// the target shader model is at least 6.2.
4290+
if (Args.getLastArg(OPT_fnative_half_type)) {
4291+
const LangStandard &Std =
4292+
LangStandard::getLangStandardForKind(Opts.LangStd);
4293+
if (!(Opts.LangStd >= LangStandard::lang_hlsl2018 &&
4294+
T.getOSVersion() >= VersionTuple(6, 2)))
4295+
Diags.Report(diag::err_drv_hlsl_16bit_types_unsupported)
4296+
<< "-enable-16bit-types" << true << Std.getName()
4297+
<< T.getOSVersion().getAsString();
4298+
}
42874299
} else if (T.isSPIRVLogical()) {
42884300
if (!T.isVulkanOS() || T.getVulkanVersion() == VersionTuple(0)) {
42894301
Diags.Report(diag::err_drv_hlsl_bad_shader_unsupported)
42904302
<< VulkanEnv << T.getOSName() << T.str();
42914303
}
4304+
if (Args.getLastArg(OPT_fnative_half_type)) {
4305+
const LangStandard &Std =
4306+
LangStandard::getLangStandardForKind(Opts.LangStd);
4307+
if (!(Opts.LangStd >= LangStandard::lang_hlsl2018))
4308+
Diags.Report(diag::err_drv_hlsl_16bit_types_unsupported)
4309+
<< "-fnative-half-type" << false << Std.getName();
4310+
}
42924311
} else {
42934312
llvm_unreachable("expected DXIL or SPIR-V target");
42944313
}

clang/test/CodeGenHLSL/builtins/RWBuffer-elementtype.hlsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-compute -finclude-default-header -fnative-half-type -emit-llvm -o - %s | FileCheck %s
1+
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.2-compute -finclude-default-header -fnative-half-type -emit-llvm -o - %s | FileCheck %s
22

33
RWBuffer<int16_t> BufI16;
44
RWBuffer<uint16_t> BufU16;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// RUN: not %clang_dxc -enable-16bit-types -T cs_6_0 -HV 2016 %s 2>&1 | FileCheck -check-prefix=both_invalid %s
2+
// RUN: not %clang_dxc -enable-16bit-types -T lib_6_4 -HV 2017 %s 2>&1 | FileCheck -check-prefix=HV_invalid_2017 %s
3+
// RUN: not %clang_dxc -enable-16bit-types -T cs_6_0 /HV 2021 %s 2>&1 | FileCheck -check-prefix=TP_invalid %s
4+
// RUN: %clang_dxc -enable-16bit-types -T lib_6_4 /HV 2018 %s 2>&1 -### | FileCheck -check-prefix=valid_2018 %s
5+
// RUN: %clang_dxc -enable-16bit-types -T lib_6_4 /HV 2021 %s 2>&1 -### | FileCheck -check-prefix=valid_2021 %s
6+
7+
8+
// both_invalid: error: '-enable-16bit-types' option requires target HLSL Version >= 2018 and shader model >= 6.2, but HLSL Version is 'hlsl2016' and shader model is '6.0'
9+
// HV_invalid_2017: error: '-enable-16bit-types' option requires target HLSL Version >= 2018 and shader model >= 6.2, but HLSL Version is 'hlsl2017' and shader model is '6.4'
10+
// TP_invalid: error: '-enable-16bit-types' option requires target HLSL Version >= 2018 and shader model >= 6.2, but HLSL Version is 'hlsl2021' and shader model is '6.0'
11+
12+
// valid_2021: "dxil-unknown-shadermodel6.4-library"
13+
// valid_2021-SAME: "-std=hlsl2021"
14+
// valid_2021-SAME: "-fnative-half-type"
15+
16+
// valid_2018: "dxil-unknown-shadermodel6.4-library"
17+
// valid_2018-SAME: "-std=hlsl2018"
18+
// valid_2018-SAME: "-fnative-half-type"
19+
20+
[numthreads(1,1,1)]
21+
void main()
22+
{
23+
return;
24+
}
25+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// RUN: not %clang_cc1 -internal-isystem D:\llvm-project\build\x64-Release\lib\clang\19\include -nostdsysteminc -triple spirv-vulkan-library -x hlsl -std=hlsl2016 -fnative-half-type -emit-llvm -disable-llvm-passes -o - %s 2>&1 | FileCheck %s --check-prefix=SPIRV
2+
// RUN: %clang_cc1 -internal-isystem D:\llvm-project\build\x64-Release\lib\clang\19\include -nostdsysteminc -triple spirv-vulkan-library -x hlsl -std=hlsl2021 -fnative-half-type -emit-llvm -disable-llvm-passes -o - %s 2>&1 | FileCheck %s --check-prefix=valid
3+
4+
// SPIRV: error: '-fnative-half-type' option requires target HLSL Version >= 2018, but HLSL Version is 'hlsl2016'
5+
6+
// valid: "spirv-unknown-vulkan-library"
7+
// valid: define spir_func void @main() #0 {
8+
9+
[numthreads(1,1,1)]
10+
void main()
11+
{
12+
return;
13+
}
14+

0 commit comments

Comments
 (0)