Skip to content

Commit 6c53c95

Browse files
author
Xiang Li
committed
Force OS to shader model for dxc mode.
1 parent c2fccc5 commit 6c53c95

File tree

7 files changed

+136
-60
lines changed

7 files changed

+136
-60
lines changed

clang/include/clang/Driver/Options.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ enum ClangFlags {
3535
FlangOption = (1 << 14),
3636
FC1Option = (1 << 15),
3737
FlangOnlyOption = (1 << 16),
38-
Ignored = (1 << 17),
39-
DXCOption = (1 << 18),
38+
DXCOption = (1 << 17),
39+
Ignored = (1 << 18),
4040
};
4141

4242
enum ID {

clang/include/clang/Driver/Options.td

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6651,7 +6651,7 @@ def _SLASH_ZW : CLJoined<"ZW">;
66516651
//===----------------------------------------------------------------------===//
66526652

66536653
def dxc_Group : OptionGroup<"<clang-dxc options>">, Flags<[DXCOption]>,
6654-
HelpText<"DXC.EXE COMPATIBILITY OPTIONS">;
6654+
HelpText<"dxc compatibility options">;
66556655

66566656
class DXCJoinedOrSeparate<string name> : Option<["/", "-"], name,
66576657
KIND_JOINED_OR_SEPARATE>, Group<dxc_Group>, Flags<[DXCOption, NoXarchOption]>;
@@ -6666,4 +6666,12 @@ def Fo : DXCJoinedOrSeparate<"Fo">, Alias<o>,
66666666

66676667
def target_profile : DXCJoinedOrSeparate<"T">, MetaVarName<"<profile>">,
66686668
HelpText<"Set target profile.">,
6669-
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, vs_6_0, vs_6_1, vs_6_2, vs_6_3, vs_6_4, vs_6_5, vs_6_6, vs_6_7, gs_6_0, gs_6_1, gs_6_2, gs_6_3, gs_6_4, gs_6_5, gs_6_6, gs_6_7, hs_6_0, hs_6_1, hs_6_2, hs_6_3, hs_6_4, hs_6_5, hs_6_6, hs_6_7, ds_6_0, ds_6_1, ds_6_2, ds_6_3, ds_6_4, ds_6_5, ds_6_6, ds_6_7, cs_6_0, cs_6_1, cs_6_2, cs_6_3, cs_6_4, cs_6_5, cs_6_6, cs_6_7, lib_6_3, lib_6_4, lib_6_5, lib_6_6, lib_6_7, lib_6_x, ms_6_5, ms_6_6, ms_6_7, as_6_5, as_6_6, as_6_7">;
6669+
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,"
6670+
"vs_6_0, vs_6_1, vs_6_2, vs_6_3, vs_6_4, vs_6_5, vs_6_6, vs_6_7,"
6671+
"gs_6_0, gs_6_1, gs_6_2, gs_6_3, gs_6_4, gs_6_5, gs_6_6, gs_6_7,"
6672+
"hs_6_0, hs_6_1, hs_6_2, hs_6_3, hs_6_4, hs_6_5, hs_6_6, hs_6_7,"
6673+
"ds_6_0, ds_6_1, ds_6_2, ds_6_3, ds_6_4, ds_6_5, ds_6_6, ds_6_7,"
6674+
"cs_6_0, cs_6_1, cs_6_2, cs_6_3, cs_6_4, cs_6_5, cs_6_6, cs_6_7,"
6675+
"lib_6_3, lib_6_4, lib_6_5, lib_6_6, lib_6_7, lib_6_x,"
6676+
"ms_6_5, ms_6_6, ms_6_7,"
6677+
"as_6_5, as_6_6, as_6_7">;

clang/lib/Driver/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ add_clang_library(clangDriver
4848
ToolChains/CrossWindows.cpp
4949
ToolChains/Cuda.cpp
5050
ToolChains/Darwin.cpp
51-
ToolChains/DirectX.cpp
5251
ToolChains/DragonFly.cpp
5352
ToolChains/Flang.cpp
5453
ToolChains/FreeBSD.cpp
@@ -59,6 +58,7 @@ add_clang_library(clangDriver
5958
ToolChains/HIPAMD.cpp
6059
ToolChains/HIPSPV.cpp
6160
ToolChains/Hexagon.cpp
61+
ToolChains/HLSL.cpp
6262
ToolChains/Hurd.cpp
6363
ToolChains/Linux.cpp
6464
ToolChains/MipsLinux.cpp

clang/lib/Driver/Driver.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
#include "ToolChains/CrossWindows.h"
2020
#include "ToolChains/Cuda.h"
2121
#include "ToolChains/Darwin.h"
22-
#include "ToolChains/DirectX.h"
2322
#include "ToolChains/DragonFly.h"
2423
#include "ToolChains/FreeBSD.h"
2524
#include "ToolChains/Fuchsia.h"
@@ -28,6 +27,7 @@
2827
#include "ToolChains/HIPSPV.h"
2928
#include "ToolChains/Haiku.h"
3029
#include "ToolChains/Hexagon.h"
30+
#include "ToolChains/HLSL.h"
3131
#include "ToolChains/Hurd.h"
3232
#include "ToolChains/Lanai.h"
3333
#include "ToolChains/Linux.h"
@@ -1192,6 +1192,15 @@ Compilation *Driver::BuildCompilation(ArrayRef<const char *> ArgList) {
11921192
T.setObjectFormat(llvm::Triple::COFF);
11931193
TargetTriple = T.str();
11941194
}
1195+
1196+
if (IsDXCMode()) {
1197+
// clang-dxc target is build from target_profile option.
1198+
// Just set OS to shader model to select HLSLToolChain.
1199+
llvm::Triple T(TargetTriple);
1200+
T.setOS(llvm::Triple::ShaderModel);
1201+
TargetTriple = T.str();
1202+
}
1203+
11951204
if (const Arg *A = Args.getLastArg(options::OPT_target))
11961205
TargetTriple = A->getValue();
11971206
if (const Arg *A = Args.getLastArg(options::OPT_ccc_install_dir))
@@ -5682,6 +5691,9 @@ const ToolChain &Driver::getToolChain(const ArgList &Args,
56825691
case llvm::Triple::ZOS:
56835692
TC = std::make_unique<toolchains::ZOS>(*this, Target, Args);
56845693
break;
5694+
case llvm::Triple::ShaderModel:
5695+
TC = std::make_unique<toolchains::HLSLToolChain>(*this, Target, Args);
5696+
break;
56855697
default:
56865698
// Of these targets, Hexagon is the only one that might have
56875699
// an OS of Linux, in which case it got handled above already.
@@ -5728,9 +5740,6 @@ const ToolChain &Driver::getToolChain(const ArgList &Args,
57285740
case llvm::Triple::spirv64:
57295741
TC = std::make_unique<toolchains::SPIRVToolChain>(*this, Target, Args);
57305742
break;
5731-
case llvm::Triple::dxil:
5732-
TC = std::make_unique<toolchains::DirectXToolChain>(*this, Target, Args);
5733-
break;
57345743
default:
57355744
if (Target.getVendor() == llvm::Triple::Myriad)
57365745
TC = std::make_unique<toolchains::MyriadToolChain>(*this, Target,

clang/lib/Driver/ToolChains/DirectX.cpp renamed to clang/lib/Driver/ToolChains/HLSL.cpp

Lines changed: 94 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
//===--- DirectX.cpp - DirectX ToolChain Implementations ----------*- C++ -*-===//
1+
//===--- HLSL.cpp - HLSL ToolChain Implementations --------------*- C++ -*-===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include "DirectX.h"
10-
#include "llvm/ADT/Triple.h"
9+
#include "HLSL.h"
1110
#include "CommonArgs.h"
1211
#include "clang/Driver/DriverDiagnostic.h"
1312
#include "llvm/ADT/StringSwitch.h"
13+
#include "llvm/ADT/Triple.h"
1414

1515
using namespace clang::driver;
1616
using namespace clang::driver::tools;
@@ -20,6 +20,88 @@ using namespace llvm::opt;
2020
using namespace llvm;
2121

2222
namespace {
23+
24+
const unsigned OfflineLibMinor = 0xF;
25+
const unsigned MaxShaderModel6Minor = 7;
26+
bool isLegalShaderModel(Triple &T) {
27+
if (T.getOS() != Triple::OSType::ShaderModel)
28+
return false;
29+
30+
auto Kind = T.getEnvironment();
31+
32+
switch (Kind) {
33+
default:
34+
return false;
35+
case Triple::EnvironmentType::Vertex:
36+
case Triple::EnvironmentType::Hull:
37+
case Triple::EnvironmentType::Domain:
38+
case Triple::EnvironmentType::Geometry:
39+
case Triple::EnvironmentType::Pixel:
40+
case Triple::EnvironmentType::Compute:
41+
case Triple::EnvironmentType::Library:
42+
case Triple::EnvironmentType::Amplification:
43+
case Triple::EnvironmentType::Mesh:
44+
break;
45+
}
46+
47+
auto Version = T.getOSVersion();
48+
if (Version.getBuild())
49+
return false;
50+
if (Version.getSubminor())
51+
return false;
52+
53+
auto OMinor = Version.getMinor();
54+
if (!OMinor.hasValue())
55+
return false;
56+
57+
unsigned Minor = OMinor.getValue();
58+
unsigned Major = Version.getMajor();
59+
60+
switch (Major) {
61+
case 4:
62+
case 5: {
63+
switch (Minor) {
64+
case 0:
65+
case 1:
66+
switch (Kind) {
67+
case Triple::EnvironmentType::Vertex:
68+
break;
69+
default:
70+
return false;
71+
}
72+
break;
73+
default:
74+
return false;
75+
}
76+
} break;
77+
case 6: {
78+
switch (Kind) {
79+
default:
80+
break;
81+
case Triple::EnvironmentType::Library: {
82+
if (Minor < 3)
83+
return false;
84+
} break;
85+
case Triple::EnvironmentType::Amplification:
86+
case Triple::EnvironmentType::Mesh: {
87+
if (Minor < 5)
88+
return false;
89+
} break;
90+
}
91+
if (Minor == OfflineLibMinor) {
92+
if (Kind != Triple::EnvironmentType::Library)
93+
return false;
94+
} else if (Minor > MaxShaderModel6Minor) {
95+
return false;
96+
}
97+
} break;
98+
default:
99+
return false;
100+
}
101+
102+
return true;
103+
}
104+
23105
std::string tryParseProfile(StringRef Profile) {
24106
// [ps|vs|gs|hs|ds|cs|ms|as]_[major]_[minor]
25107
SmallVector<StringRef, 3> Parts;
@@ -50,7 +132,6 @@ std::string tryParseProfile(StringRef Profile) {
50132
if (Major == 0)
51133
return "";
52134

53-
const unsigned OfflineMinor = 0xF;
54135
const unsigned InvalidMinor = -1;
55136
unsigned Minor = StringSwitch<unsigned>(Parts[2])
56137
.Case("0", 0)
@@ -61,55 +142,33 @@ std::string tryParseProfile(StringRef Profile) {
61142
.Case("5", 5)
62143
.Case("6", 6)
63144
.Case("7", 7)
64-
.Case("x", OfflineMinor)
145+
.Case("x", OfflineLibMinor)
65146
.Default(InvalidMinor);
66147
if (Minor == InvalidMinor)
67148
return "";
68149

69-
if (Major != 6 && Minor > 1)
70-
return "";
71-
72-
if (Minor == OfflineMinor && Kind != Triple::EnvironmentType::Library)
73-
return "";
74-
75-
switch (Kind) {
76-
default:
77-
break;
78-
case Triple::EnvironmentType::Library: {
79-
if (Major < 6)
80-
return "";
81-
if (Major == 6 && Minor < 3)
82-
return "";
83-
} break;
84-
case Triple::EnvironmentType::Amplification:
85-
case Triple::EnvironmentType::Mesh: {
86-
if (Major < 6)
87-
return "";
88-
if (Major == 6 && Minor < 5)
89-
return "";
90-
} break;
91-
}
92-
93150
// dxil-unknown-shadermodel-hull
94151
llvm::Triple T;
95152
T.setArch(Triple::ArchType::dxil);
96153
T.setOSName(Triple::getOSTypeName(Triple::OSType::ShaderModel).str() +
97154
VersionTuple(Major, Minor).getAsString());
98155
T.setEnvironment(Kind);
99-
;
100-
return T.getTriple();
156+
if (isLegalShaderModel(T))
157+
return T.getTriple();
158+
else
159+
return "";
101160
}
102161

103162
} // namespace
104163

105164
/// DirectX Toolchain
106-
DirectXToolChain::DirectXToolChain(const Driver &D, const llvm::Triple &Triple,
107-
const ArgList &Args)
165+
HLSLToolChain::HLSLToolChain(const Driver &D, const llvm::Triple &Triple,
166+
const ArgList &Args)
108167
: ToolChain(D, Triple, Args) {}
109168

110169
std::string
111-
DirectXToolChain::ComputeEffectiveClangTriple(const ArgList &Args,
112-
types::ID InputType) const {
170+
HLSLToolChain::ComputeEffectiveClangTriple(const ArgList &Args,
171+
types::ID InputType) const {
113172
if (Arg *A = Args.getLastArg(options::OPT_target_profile)) {
114173
StringRef Profile = A->getValue();
115174
std::string Triple = tryParseProfile(Profile);

clang/lib/Driver/ToolChains/DirectX.h renamed to clang/lib/Driver/ToolChains/HLSL.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//===--- DirectX.h - DirectX ToolChain Implementations ----------*- C++ -*-===//
1+
//===--- HLSL.h - HLSL ToolChain Implementations ----------------*- C++ -*-===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
@@ -15,10 +15,10 @@ namespace driver {
1515

1616
namespace toolchains {
1717

18-
class LLVM_LIBRARY_VISIBILITY DirectXToolChain : public ToolChain {
18+
class LLVM_LIBRARY_VISIBILITY HLSLToolChain : public ToolChain {
1919
public:
20-
DirectXToolChain(const Driver &D, const llvm::Triple &Triple,
21-
const llvm::opt::ArgList &Args);
20+
HLSLToolChain(const Driver &D, const llvm::Triple &Triple,
21+
const llvm::opt::ArgList &Args);
2222
bool isPICDefault() const override { return false; }
2323
bool isPIEDefault(const llvm::opt::ArgList &Args) const override {
2424
return false;
Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
1-
// RUN: %clang_dxc -Tvs_6_0 --target=dxil -Fo - %s 2>&1 | FileCheck %s --check-prefix=VS60
1+
// RUN: %clang_dxc -Tvs_6_0 -Fo - %s 2>&1 | FileCheck %s --check-prefix=VS60
22
// VS60:target triple = "dxil-unknown-shadermodel6.0-vertex"
33

4-
// RUN: %clang_dxc -Ths_6_1 --target=dxil -Fo - %s 2>&1 | FileCheck %s --check-prefix=HS61
4+
// RUN: %clang_dxc -Ths_6_1 -Fo - %s 2>&1 | FileCheck %s --check-prefix=HS61
55
// HS61:target triple = "dxil-unknown-shadermodel6.1-hull"
66

7-
// RUN: %clang_dxc -Tds_6_2 --target=dxil -Fo - %s 2>&1 | FileCheck %s --check-prefix=DS62
7+
// RUN: %clang_dxc -Tds_6_2 -Fo - %s 2>&1 | FileCheck %s --check-prefix=DS62
88
// DS62:target triple = "dxil-unknown-shadermodel6.2-domain"
99

10-
// RUN: %clang_dxc -Tgs_6_3 --target=dxil -Fo - %s 2>&1 | FileCheck %s --check-prefix=GS63
10+
// RUN: %clang_dxc -Tgs_6_3 -Fo - %s 2>&1 | FileCheck %s --check-prefix=GS63
1111
// GS63:target triple = "dxil-unknown-shadermodel6.3-geometry"
1212

13-
// RUN: %clang_dxc -Tps_6_4 --target=dxil -Fo - %s 2>&1 | FileCheck %s --check-prefix=PS64
13+
// RUN: %clang_dxc -Tps_6_4 -Fo - %s 2>&1 | FileCheck %s --check-prefix=PS64
1414
// PS64:target triple = "dxil-unknown-shadermodel6.4-pixel"
1515

16-
// RUN: %clang_dxc -Tms_6_5 --target=dxil -Fo - %s 2>&1 | FileCheck %s --check-prefix=MS65
16+
// RUN: %clang_dxc -Tms_6_5 -Fo - %s 2>&1 | FileCheck %s --check-prefix=MS65
1717
// MS65:target triple = "dxil-unknown-shadermodel6.5-mesh"
1818

19-
// RUN: %clang_dxc -Tas_6_6 --target=dxil -Fo - %s 2>&1 | FileCheck %s --check-prefix=AS66
19+
// RUN: %clang_dxc -Tas_6_6 -Fo - %s 2>&1 | FileCheck %s --check-prefix=AS66
2020
// AS66:target triple = "dxil-unknown-shadermodel6.6-amplification"
2121

22-
// RUN: %clang_dxc -Tlib_6_x --target=dxil -Fo - %s 2>&1 | FileCheck %s --check-prefix=LIB6x
22+
// RUN: %clang_dxc -Tlib_6_x -Fo - %s 2>&1 | FileCheck %s --check-prefix=LIB6x
2323
// LIB6x:target triple = "dxil-unknown-shadermodel6.15-library"
2424

25-
// RUN: %clang_dxc -### -Tps_3_1 --target=dxil -Fo - %s 2>&1 | FileCheck %s --check-prefix=INVALID
25+
// RUN: %clang_dxc -### -Tps_3_1 -Fo - %s 2>&1 | FileCheck %s --check-prefix=INVALID
2626
// INVALID:invalid profile : ps_3_1
2727

28-
// RUN: %clang_dxc -### -Tlib_6_1 --target=dxil -Fo - %s 2>&1 | FileCheck %s --check-prefix=INVALID2
28+
// RUN: %clang_dxc -### -Tlib_6_1 -Fo - %s 2>&1 | FileCheck %s --check-prefix=INVALID2
2929
// INVALID2:invalid profile : lib_6_1
3030

31-
// RUN: %clang_dxc -### -Tms_6_1 --target=dxil -Fo - %s 2>&1 | FileCheck %s --check-prefix=INVALID3
31+
// RUN: %clang_dxc -### -Tms_6_1 -Fo - %s 2>&1 | FileCheck %s --check-prefix=INVALID3
3232
// INVALID3:invalid profile : ms_6_1
3333

34-
// RUN: %clang_dxc -### -Tas_6_4 --target=dxil -Fo - %s 2>&1 | FileCheck %s --check-prefix=INVALID4
34+
// RUN: %clang_dxc -### -Tas_6_4 -Fo - %s 2>&1 | FileCheck %s --check-prefix=INVALID4
3535
// INVALID4:invalid profile : as_6_4

0 commit comments

Comments
 (0)