Skip to content

Commit c2fccc5

Browse files
author
Xiang Li
committed
Cleanup target profile parsing.
1 parent 581cb15 commit c2fccc5

File tree

3 files changed

+100
-124
lines changed

3 files changed

+100
-124
lines changed

clang/include/clang/Driver/Options.td

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6665,4 +6665,5 @@ def Fo : DXCJoinedOrSeparate<"Fo">, Alias<o>,
66656665
HelpText<"Output object file.">;
66666666

66676667
def target_profile : DXCJoinedOrSeparate<"T">, MetaVarName<"<profile>">,
6668-
HelpText<"Set target profile. \n\t<profile>: ps_6_0, ps_6_1, ps_6_2, ps_6_3, ps_6_4, ps_6_5, ps_6_6, ps_6_7, \n\t\t vs_6_0, vs_6_1, vs_6_2, vs_6_3, vs_6_4, vs_6_5, vs_6_6, vs_6_7, \n\t\t gs_6_0, gs_6_1, gs_6_2, gs_6_3, gs_6_4, gs_6_5, gs_6_6, gs_6_7, \n\t\t hs_6_0, hs_6_1, hs_6_2, hs_6_3, hs_6_4, hs_6_5, hs_6_6, hs_6_7, \n\t\t ds_6_0, ds_6_1, ds_6_2, ds_6_3, ds_6_4, ds_6_5, ds_6_6, ds_6_7, \n\t\t cs_6_0, cs_6_1, cs_6_2, cs_6_3, cs_6_4, cs_6_5, cs_6_6, cs_6_7, \n\t\t lib_6_1, lib_6_2, lib_6_3, lib_6_4, lib_6_5, lib_6_6, lib_6_7, \n\t\t ms_6_5, ms_6_6, ms_6_7, \n\t\t as_6_5, as_6_6, as_6_7, \n\t\t ">;
6668+
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">;

clang/lib/Driver/ToolChains/DirectX.cpp

Lines changed: 72 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "llvm/ADT/Triple.h"
1111
#include "CommonArgs.h"
1212
#include "clang/Driver/DriverDiagnostic.h"
13+
#include "llvm/ADT/StringSwitch.h"
1314

1415
using namespace clang::driver;
1516
using namespace clang::driver::tools;
@@ -19,130 +20,83 @@ using namespace llvm::opt;
1920
using namespace llvm;
2021

2122
namespace {
22-
std::string tryParseProfile(StringRef profile) {
23+
std::string tryParseProfile(StringRef Profile) {
2324
// [ps|vs|gs|hs|ds|cs|ms|as]_[major]_[minor]
24-
Triple::EnvironmentType kind;
25-
switch (profile[0]) {
26-
case 'p':
27-
kind = Triple::EnvironmentType::Pixel;
28-
break;
29-
case 'v':
30-
kind = Triple::EnvironmentType::Vertex;
31-
break;
32-
case 'g':
33-
kind = Triple::EnvironmentType::Geometry;
34-
break;
35-
case 'h':
36-
kind = Triple::EnvironmentType::Hull;
37-
break;
38-
case 'd':
39-
kind = Triple::EnvironmentType::Domain;
40-
break;
41-
case 'c':
42-
kind = Triple::EnvironmentType::Compute;
43-
break;
44-
case 'l':
45-
kind = Triple::EnvironmentType::Library;
46-
break;
47-
case 'm':
48-
kind = Triple::EnvironmentType::Mesh;
49-
break;
50-
case 'a':
51-
kind = Triple::EnvironmentType::Amplification;
52-
break;
53-
default:
25+
SmallVector<StringRef, 3> Parts;
26+
Profile.split(Parts, "_");
27+
if (Parts.size() != 3)
5428
return "";
55-
}
56-
unsigned Idx = 3;
57-
if (kind != Triple::EnvironmentType::Library) {
58-
if (profile[1] != 's' || profile[2] != '_')
59-
return "";
60-
} else {
61-
if (profile[1] != 'i' || profile[2] != 'b' || profile[3] != '_')
62-
return "";
63-
Idx = 4;
64-
}
65-
Triple::OSType::ShaderModel;
66-
unsigned Major;
67-
switch (profile[Idx++]) {
68-
case '4':
69-
Major = 4;
70-
break;
71-
case '5':
72-
Major = 5;
73-
break;
74-
case '6':
75-
Major = 6;
76-
break;
77-
default:
29+
30+
Triple::EnvironmentType Kind =
31+
StringSwitch<Triple::EnvironmentType>(Parts[0])
32+
.Case("ps", Triple::EnvironmentType::Pixel)
33+
.Case("vs", Triple::EnvironmentType::Vertex)
34+
.Case("gs", Triple::EnvironmentType::Geometry)
35+
.Case("hs", Triple::EnvironmentType::Hull)
36+
.Case("ds", Triple::EnvironmentType::Domain)
37+
.Case("cs", Triple::EnvironmentType::Compute)
38+
.Case("lib", Triple::EnvironmentType::Library)
39+
.Case("ms", Triple::EnvironmentType::Mesh)
40+
.Case("as", Triple::EnvironmentType::Amplification)
41+
.Default(Triple::EnvironmentType::UnknownEnvironment);
42+
if (Kind == Triple::EnvironmentType::UnknownEnvironment)
7843
return "";
79-
}
80-
if (profile[Idx++] != '_')
44+
45+
unsigned Major = StringSwitch<unsigned>(Parts[1])
46+
.Case("4", 4)
47+
.Case("5", 5)
48+
.Case("6", 6)
49+
.Default(0);
50+
if (Major == 0)
8151
return "";
8252

83-
static const unsigned kOfflineMinor = 0xF;
84-
unsigned Minor;
85-
switch (profile[Idx++]) {
86-
case '0':
87-
Minor = 0;
88-
break;
89-
case '1':
90-
Minor = 1;
53+
const unsigned OfflineMinor = 0xF;
54+
const unsigned InvalidMinor = -1;
55+
unsigned Minor = StringSwitch<unsigned>(Parts[2])
56+
.Case("0", 0)
57+
.Case("1", 1)
58+
.Case("2", 2)
59+
.Case("3", 3)
60+
.Case("4", 4)
61+
.Case("5", 5)
62+
.Case("6", 6)
63+
.Case("7", 7)
64+
.Case("x", OfflineMinor)
65+
.Default(InvalidMinor);
66+
if (Minor == InvalidMinor)
67+
return "";
68+
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:
9177
break;
92-
case '2':
93-
if (Major == 6) {
94-
Minor = 2;
95-
break;
96-
} else
97-
return "";
98-
case '3':
99-
if (Major == 6) {
100-
Minor = 3;
101-
break;
102-
} else
103-
return "";
104-
case '4':
105-
if (Major == 6) {
106-
Minor = 4;
107-
break;
108-
} else
78+
case Triple::EnvironmentType::Library: {
79+
if (Major < 6)
10980
return "";
110-
case '5':
111-
if (Major == 6) {
112-
Minor = 5;
113-
break;
114-
} else
115-
"";
116-
case '6':
117-
if (Major == 6) {
118-
Minor = 6;
119-
break;
120-
} else
81+
if (Major == 6 && Minor < 3)
12182
return "";
122-
case '7':
123-
if (Major == 6) {
124-
Minor = 7;
125-
break;
126-
} else
83+
} break;
84+
case Triple::EnvironmentType::Amplification:
85+
case Triple::EnvironmentType::Mesh: {
86+
if (Major < 6)
12787
return "";
128-
case 'x':
129-
if (kind == Triple::EnvironmentType::Library && Major == 6) {
130-
Minor = kOfflineMinor;
131-
break;
132-
} else
88+
if (Major == 6 && Minor < 5)
13389
return "";
134-
default:
135-
return "";
90+
} break;
13691
}
137-
if (profile.size() != Idx && profile[Idx++] != 0)
138-
return "";
92+
13993
// dxil-unknown-shadermodel-hull
140-
llvm::Triple T(Twine("dxil"), Twine("unknown"),
141-
Twine("shadermodel")
142-
.concat(Twine(Major))
143-
.concat(".")
144-
.concat(Twine(Minor)),
145-
Triple::getEnvironmentTypeName(kind));
94+
llvm::Triple T;
95+
T.setArch(Triple::ArchType::dxil);
96+
T.setOSName(Triple::getOSTypeName(Triple::OSType::ShaderModel).str() +
97+
VersionTuple(Major, Minor).getAsString());
98+
T.setEnvironment(Kind);
99+
;
146100
return T.getTriple();
147101
}
148102

@@ -157,14 +111,14 @@ std::string
157111
DirectXToolChain::ComputeEffectiveClangTriple(const ArgList &Args,
158112
types::ID InputType) const {
159113
if (Arg *A = Args.getLastArg(options::OPT_target_profile)) {
160-
StringRef profile = A->getValue();
161-
std::string triple = tryParseProfile(profile);
162-
if (triple == "") {
163-
getDriver().Diag(diag::err_drv_invalid_directx_shader_module) << profile;
164-
triple = ToolChain::ComputeEffectiveClangTriple(Args, InputType);
114+
StringRef Profile = A->getValue();
115+
std::string Triple = tryParseProfile(Profile);
116+
if (Triple == "") {
117+
getDriver().Diag(diag::err_drv_invalid_directx_shader_module) << Profile;
118+
Triple = ToolChain::ComputeEffectiveClangTriple(Args, InputType);
165119
}
166120
A->claim();
167-
return triple;
121+
return Triple;
168122
} else {
169123
return ToolChain::ComputeEffectiveClangTriple(Args, InputType);
170124
}

clang/test/Driver/dxil_target_profile.hlsl

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,32 @@
44
// RUN: %clang_dxc -Ths_6_1 --target=dxil -Fo - %s 2>&1 | FileCheck %s --check-prefix=HS61
55
// HS61:target triple = "dxil-unknown-shadermodel6.1-hull"
66

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

10-
// RUN: %clang_dxc -Tlib_6_x --target=dxil -Fo - %s 2>&1 | FileCheck %s --check-prefix=LIB63
11-
// LIB63:target triple = "dxil-unknown-shadermodel6.15-library"
10+
// RUN: %clang_dxc -Tgs_6_3 --target=dxil -Fo - %s 2>&1 | FileCheck %s --check-prefix=GS63
11+
// GS63:target triple = "dxil-unknown-shadermodel6.3-geometry"
12+
13+
// RUN: %clang_dxc -Tps_6_4 --target=dxil -Fo - %s 2>&1 | FileCheck %s --check-prefix=PS64
14+
// PS64:target triple = "dxil-unknown-shadermodel6.4-pixel"
15+
16+
// RUN: %clang_dxc -Tms_6_5 --target=dxil -Fo - %s 2>&1 | FileCheck %s --check-prefix=MS65
17+
// MS65:target triple = "dxil-unknown-shadermodel6.5-mesh"
18+
19+
// RUN: %clang_dxc -Tas_6_6 --target=dxil -Fo - %s 2>&1 | FileCheck %s --check-prefix=AS66
20+
// AS66:target triple = "dxil-unknown-shadermodel6.6-amplification"
21+
22+
// RUN: %clang_dxc -Tlib_6_x --target=dxil -Fo - %s 2>&1 | FileCheck %s --check-prefix=LIB6x
23+
// LIB6x:target triple = "dxil-unknown-shadermodel6.15-library"
1224

1325
// RUN: %clang_dxc -### -Tps_3_1 --target=dxil -Fo - %s 2>&1 | FileCheck %s --check-prefix=INVALID
14-
// INVALID:invalid profile : ps_3_1
26+
// INVALID:invalid profile : ps_3_1
27+
28+
// RUN: %clang_dxc -### -Tlib_6_1 --target=dxil -Fo - %s 2>&1 | FileCheck %s --check-prefix=INVALID2
29+
// INVALID2:invalid profile : lib_6_1
30+
31+
// RUN: %clang_dxc -### -Tms_6_1 --target=dxil -Fo - %s 2>&1 | FileCheck %s --check-prefix=INVALID3
32+
// INVALID3:invalid profile : ms_6_1
33+
34+
// RUN: %clang_dxc -### -Tas_6_4 --target=dxil -Fo - %s 2>&1 | FileCheck %s --check-prefix=INVALID4
35+
// INVALID4:invalid profile : as_6_4

0 commit comments

Comments
 (0)