Skip to content

Commit

Permalink
[HLSL][SPIR-V] Add support -fspv-target-env opt (#78611)
Browse files Browse the repository at this point in the history
Add the -fspv-target-env option to the clang-dxc compatibility driver to
specify the SPIR-V target environment, which is propagated to the target
Triple.
  • Loading branch information
sudonatalie committed Jan 22, 2024
1 parent 55cb52b commit ce519b5
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
3 changes: 3 additions & 0 deletions clang/include/clang/Driver/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -8479,3 +8479,6 @@ def : Option<["/", "-"], "Qembed_debug", KIND_FLAG>, Group<dxc_Group>,
HelpText<"Embed PDB in shader container (ignored)">;
def spirv : DXCFlag<"spirv">,
HelpText<"Generate SPIR-V code">;
def fspv_target_env_EQ : Joined<["-"], "fspv-target-env=">, Group<dxc_Group>,
HelpText<"Specify the target environment">,
Values<"vulkan1.2, vulkan1.3">;
15 changes: 14 additions & 1 deletion clang/lib/Driver/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1310,10 +1310,23 @@ Compilation *Driver::BuildCompilation(ArrayRef<const char *> ArgList) {

A->claim();

// TODO: Specify Vulkan target environment somewhere in the triple.
if (Args.hasArg(options::OPT_spirv)) {
llvm::Triple T(TargetTriple);
T.setArch(llvm::Triple::spirv);
T.setOS(llvm::Triple::Vulkan);

// Set specific Vulkan version if applicable.
if (const Arg *A = Args.getLastArg(options::OPT_fspv_target_env_EQ)) {
const llvm::StringSet<> ValidValues = {"vulkan1.2", "vulkan1.3"};
if (ValidValues.contains(A->getValue())) {
T.setOSName(A->getValue());
} else {
Diag(diag::err_drv_invalid_value)
<< A->getAsString(Args) << A->getValue();
}
A->claim();
}

TargetTriple = T.str();
}
} else {
Expand Down
11 changes: 10 additions & 1 deletion clang/test/Driver/dxc_spirv.hlsl
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
// RUN: %clang_dxc -T cs_6_0 -spirv -### %s 2>&1 | FileCheck %s
// RUN: %clang_dxc -T cs_6_0 -spirv -fspv-target-env=vulkan1.2 -### %s 2>&1 | FileCheck %s --check-prefix=CHECK-VULKAN12
// RUN: %clang_dxc -T cs_6_0 -spirv -fspv-target-env=vulkan1.3 -### %s 2>&1 | FileCheck %s --check-prefix=CHECK-VULKAN13
// RUN: not %clang_dxc -T cs_6_0 -spirv -fspv-target-env=vulkan1.0 -### %s 2>&1 | FileCheck %s --check-prefix=CHECK-ERROR

// CHECK: "-triple" "spirv-unknown-shadermodel6.0-compute"
// CHECK: "-triple" "spirv-unknown-vulkan-compute"
// CHECK-SAME: "-x" "hlsl"

// CHECK-VULKAN12: "-triple" "spirv-unknown-vulkan1.2-compute"

// CHECK-VULKAN13: "-triple" "spirv-unknown-vulkan1.3-compute"

// CHECK-ERROR: error: invalid value 'vulkan1.0' in '-fspv-target-env=vulkan1.0'

0 comments on commit ce519b5

Please sign in to comment.