Skip to content

Commit 10194a5

Browse files
committed
[HLSL] Restrict to supported targets
Someday we would like to support HLSL on a wider range of targets, but today targeting anything other than `dxil` is likly to cause lots of headaches. This adds an error and tests to validate that the expected target is `dxil-?-shadermodel`. We will continue to do a best effort to ensure the code we write makes it easy to support other targets (like SPIR-V), but this error will prevent users from hitting frustrating errors for unsupported cases. Reviewed By: jcranmer-intel, Anastasia Differential Revision: https://reviews.llvm.org/D132056
1 parent 662ee93 commit 10194a5

File tree

5 files changed

+33
-7
lines changed

5 files changed

+33
-7
lines changed

clang/include/clang/Basic/DiagnosticDriverKinds.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,8 @@ def err_drv_invalid_directx_shader_module : Error<
666666
"invalid profile : %0">;
667667
def err_drv_dxc_missing_target_profile : Error<
668668
"target profile option (-T) is missing">;
669+
def err_drv_hlsl_unsupported_target : Error<
670+
"HLSL code generation is unsupported for target '%0'">;
669671

670672
def err_drv_invalid_range_dxil_validator_version : Error<
671673
"invalid validator version : %0\n"

clang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4107,6 +4107,14 @@ bool CompilerInvocation::ParseLangArgs(LangOptions &Opts, ArgList &Args,
41074107
if (const Arg *A = Args.getLastArg(OPT_frandomize_layout_seed_EQ))
41084108
Opts.RandstructSeed = A->getValue(0);
41094109

4110+
// Validate options for HLSL
4111+
if (Opts.HLSL) {
4112+
bool SupportedTarget = T.getArch() == llvm::Triple::dxil &&
4113+
T.getOS() == llvm::Triple::ShaderModel;
4114+
if (!SupportedTarget)
4115+
Diags.Report(diag::err_drv_hlsl_unsupported_target) << T.str();
4116+
}
4117+
41104118
return Diags.getNumErrors() == NumErrorsBefore;
41114119
}
41124120

clang/test/CodeGenHLSL/validator_version.hlsl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// RUN: %clang -cc1 -S -triple dxil-pc-shadermodel6.3-library -S -emit-llvm -xhlsl -validator-version 1.1 -o - %s | FileCheck %s
2-
// RUN: %clang -cc1 -S -triple spirv32 -S -emit-llvm -xhlsl -validator-version 1.1 -o - %s | FileCheck %s --check-prefix=NOT_DXIL
2+
3+
// FIXME:The following line should work once SPIR-V support for HLSL is added.
4+
// DISABLED: %clang -cc1 -S -triple spirv32 -S -emit-llvm -xhlsl -validator-version 1.1 -o - %s | FileCheck %s --check-prefix=NOT_DXIL
35

46
// CHECK:!dx.valver = !{![[valver:[0-9]+]]}
57
// CHECK:![[valver]] = !{i32 1, i32 1}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// RUN: not %clang -target x86_64-unknown-unknown %s 2>&1 | FileCheck %s --check-prefix=X86
2+
// RUN: not %clang -target dxil-unknown-unknown %s 2>&1 | FileCheck %s --check-prefix=DXIL
3+
// RUN: not %clang -target x86_64-unknown-shadermodel %s 2>&1 | FileCheck %s --check-prefix=SM
4+
// RUN: not %clang -target spirv64-unknown-unknown %s 2>&1 | FileCheck %s --check-prefix=SPIRV
5+
6+
7+
// A completely unsupported target...
8+
// X86: error: HLSL code generation is unsupported for target 'x86_64-unknown-unknown'
9+
10+
// Poorly specified targets
11+
// DXIL: error: HLSL code generation is unsupported for target 'dxil-unknown-unknown'
12+
// SM: error: HLSL code generation is unsupported for target 'x86_64-unknown-shadermodel'
13+
14+
// FIXME// SPIRV: error: HLSL code generation is unsupported for target 'spirv64-unknown-unknown'

clang/test/Preprocessor/predefined-macros-hlsl.hlsl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,20 @@
2929
// PIXEL: #define __SHADER_TARGET_STAGE 0
3030
// VERTEX: #define __SHADER_TARGET_STAGE 1
3131

32-
// RUN: %clang_cc1 %s -E -dM -o - -std=hlsl2015 | FileCheck -match-full-lines %s --check-prefixes=STD2015
32+
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-library %s -E -dM -o - -x hlsl -std=hlsl2015 | FileCheck -match-full-lines %s --check-prefixes=STD2015
3333
// STD2015: #define __HLSL_VERSION 2015
3434

35-
// RUN: %clang_cc1 %s -E -dM -o - -std=hlsl2016 | FileCheck -match-full-lines %s --check-prefixes=STD2016
35+
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-library %s -E -dM -o - -x hlsl -std=hlsl2016 | FileCheck -match-full-lines %s --check-prefixes=STD2016
3636
// STD2016: #define __HLSL_VERSION 2016
3737

38-
// RUN: %clang_cc1 %s -E -dM -o - -std=hlsl2017 | FileCheck -match-full-lines %s --check-prefixes=STD2017
38+
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-library %s -E -dM -o - -x hlsl -std=hlsl2017 | FileCheck -match-full-lines %s --check-prefixes=STD2017
3939
// STD2017: #define __HLSL_VERSION 2017
4040

41-
// RUN: %clang_cc1 %s -E -dM -o - -std=hlsl2018 | FileCheck -match-full-lines %s --check-prefixes=STD2018
41+
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-library %s -E -dM -o - -x hlsl -std=hlsl2018 | FileCheck -match-full-lines %s --check-prefixes=STD2018
4242
// STD2018: #define __HLSL_VERSION 2018
4343

44-
// RUN: %clang_cc1 %s -E -dM -o - -std=hlsl2021 | FileCheck -match-full-lines %s --check-prefixes=STD2021
44+
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-library %s -E -dM -o - -x hlsl -std=hlsl2021 | FileCheck -match-full-lines %s --check-prefixes=STD2021
4545
// STD2021: #define __HLSL_VERSION 2021
4646

47-
// RUN: %clang_cc1 %s -E -dM -o - -std=hlsl202x | FileCheck -match-full-lines %s --check-prefixes=STD202x
47+
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-library %s -E -dM -o - -x hlsl -std=hlsl202x | FileCheck -match-full-lines %s --check-prefixes=STD202x
4848
// STD202x: #define __HLSL_VERSION 2029

0 commit comments

Comments
 (0)