Skip to content

Commit 080978d

Browse files
authored
[DirectX][DXIL] Set DXIL Version in DXIL target triple based on shader model version (#90809)
An earlier commit provided a way to decouple DXIL version from Shader Model version by representing the DXIL version as `SubArch` in the DXIL Target Triple and adding corresponding valid DXIL Arch types. This change constructs DXIL target triple with DXIL version that is deduced from Shader Model version specified in the following scenarios: 1. When compilation target profile is specified: For e.g., DXIL target triple `dxilv1.8-unknown-shader6.8-library` is constructed when `-T lib_6_8` is specified. 2. When DXIL target triple without DXIL version is specified: For e.g., DXIL target triple `dxilv1.8-pc-shadermodel6.8-library` is constructed when `-mtriple=dxil-pc-shadermodel6.8-library` is specified. Updated relevant HLSL tests that check for target triple. Validated that Clang (`check-clang`) and LLVM (`check-llvm`) regression tests pass.
1 parent 89e0557 commit 080978d

File tree

10 files changed

+148
-21
lines changed

10 files changed

+148
-21
lines changed

clang/lib/Basic/Targets.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -760,7 +760,7 @@ using namespace clang::targets;
760760
TargetInfo *
761761
TargetInfo::CreateTargetInfo(DiagnosticsEngine &Diags,
762762
const std::shared_ptr<TargetOptions> &Opts) {
763-
llvm::Triple Triple(Opts->Triple);
763+
llvm::Triple Triple(llvm::Triple::normalize(Opts->Triple));
764764

765765
// Construct the target
766766
std::unique_ptr<TargetInfo> Target = AllocateTarget(Triple, *Opts);

clang/lib/Driver/ToolChains/HLSL.cpp

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,49 @@ std::optional<std::string> tryParseProfile(StringRef Profile) {
9898
else if (llvm::getAsUnsignedInteger(Parts[2], 0, Minor))
9999
return std::nullopt;
100100

101-
// dxil-unknown-shadermodel-hull
101+
// Determine DXIL version using the minor version number of Shader
102+
// Model version specified in target profile. Prior to decoupling DXIL version
103+
// numbering from that of Shader Model DXIL version 1.Y corresponds to SM 6.Y.
104+
// E.g., dxilv1.Y-unknown-shadermodelX.Y-hull
102105
llvm::Triple T;
103-
T.setArch(Triple::ArchType::dxil);
106+
Triple::SubArchType SubArch = llvm::Triple::NoSubArch;
107+
switch (Minor) {
108+
case 0:
109+
SubArch = llvm::Triple::DXILSubArch_v1_0;
110+
break;
111+
case 1:
112+
SubArch = llvm::Triple::DXILSubArch_v1_1;
113+
break;
114+
case 2:
115+
SubArch = llvm::Triple::DXILSubArch_v1_2;
116+
break;
117+
case 3:
118+
SubArch = llvm::Triple::DXILSubArch_v1_3;
119+
break;
120+
case 4:
121+
SubArch = llvm::Triple::DXILSubArch_v1_4;
122+
break;
123+
case 5:
124+
SubArch = llvm::Triple::DXILSubArch_v1_5;
125+
break;
126+
case 6:
127+
SubArch = llvm::Triple::DXILSubArch_v1_6;
128+
break;
129+
case 7:
130+
SubArch = llvm::Triple::DXILSubArch_v1_7;
131+
break;
132+
case 8:
133+
SubArch = llvm::Triple::DXILSubArch_v1_8;
134+
break;
135+
case OfflineLibMinor:
136+
// Always consider minor version x as the latest supported DXIL version
137+
SubArch = llvm::Triple::LatestDXILSubArch;
138+
break;
139+
default:
140+
// No DXIL Version corresponding to specified Shader Model version found
141+
return std::nullopt;
142+
}
143+
T.setArch(Triple::ArchType::dxil, SubArch);
104144
T.setOSName(Triple::getOSTypeName(Triple::OSType::ShaderModel).str() +
105145
VersionTuple(Major, Minor).getAsString());
106146
T.setEnvironment(Kind);

clang/test/CodeGenHLSL/basic-target.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77
// RUN: %clang -target dxil-pc-shadermodel6.0-geometry -S -emit-llvm -o - %s | FileCheck %s
88

99
// CHECK: target datalayout = "e-m:e-p:32:32-i1:32-i8:8-i16:16-i32:32-i64:64-f16:16-f32:32-f64:64-n8:16:32:64"
10-
// CHECK: target triple = "dxil-pc-shadermodel6.0-{{[a-z]+}}"
10+
// CHECK: target triple = "dxilv1.0-pc-shadermodel6.0-{{[a-z]+}}"

clang/test/Driver/dxc_dxv_path.hlsl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
// DXV_PATH:dxv{{(.exe)?}}" "-" "-o" "-"
88

99
// RUN: %clang_dxc -I test -Vd -Tlib_6_3 -### %s 2>&1 | FileCheck %s --check-prefix=VD
10-
// VD:"-cc1"{{.*}}"-triple" "dxil-unknown-shadermodel6.3-library"
10+
// VD:"-cc1"{{.*}}"-triple" "dxilv1.3-unknown-shadermodel6.3-library"
1111
// VD-NOT:dxv not found
1212

1313
// RUN: %clang_dxc -Tlib_6_3 -ccc-print-bindings --dxv-path=%T -Fo %t.dxo %s 2>&1 | FileCheck %s --check-prefix=BINDINGS
14-
// BINDINGS: "dxil-unknown-shadermodel6.3-library" - "clang", inputs: ["[[INPUT:.+]]"], output: "[[DXC:.+]].dxo"
15-
// BINDINGS-NEXT: "dxil-unknown-shadermodel6.3-library" - "hlsl::Validator", inputs: ["[[DXC]].dxo"]
14+
// BINDINGS: "dxilv1.3-unknown-shadermodel6.3-library" - "clang", inputs: ["[[INPUT:.+]]"], output: "[[DXC:.+]].dxo"
15+
// BINDINGS-NEXT: "dxilv1.3-unknown-shadermodel6.3-library" - "hlsl::Validator", inputs: ["[[DXC]].dxo"]
1616

1717
// RUN: %clang_dxc -Tlib_6_3 -ccc-print-phases --dxv-path=%T -Fo %t.dxc %s 2>&1 | FileCheck %s --check-prefix=PHASES
1818

clang/test/Options/enable_16bit_types_validation.hlsl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
// 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'
1010
// 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'
1111

12-
// valid_2021: "dxil-unknown-shadermodel6.4-library"
12+
// valid_2021: "dxilv1.4-unknown-shadermodel6.4-library"
1313
// valid_2021-SAME: "-std=hlsl2021"
1414
// valid_2021-SAME: "-fnative-half-type"
1515

16-
// valid_2018: "dxil-unknown-shadermodel6.4-library"
16+
// valid_2018: "dxilv1.4-unknown-shadermodel6.4-library"
1717
// valid_2018-SAME: "-std=hlsl2018"
1818
// valid_2018-SAME: "-fnative-half-type"
1919

clang/unittests/Driver/DXCModeTest.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,25 +68,27 @@ TEST(DxcModeTest, TargetProfileValidation) {
6868
IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
6969
DiagnosticsEngine Diags(DiagID, &*DiagOpts, DiagConsumer);
7070

71-
validateTargetProfile("-Tvs_6_0", "dxil--shadermodel6.0-vertex",
71+
validateTargetProfile("-Tvs_6_0", "dxilv1.0--shadermodel6.0-vertex",
7272
InMemoryFileSystem, Diags);
73-
validateTargetProfile("-Ths_6_1", "dxil--shadermodel6.1-hull",
73+
validateTargetProfile("-Ths_6_1", "dxilv1.1--shadermodel6.1-hull",
7474
InMemoryFileSystem, Diags);
75-
validateTargetProfile("-Tds_6_2", "dxil--shadermodel6.2-domain",
75+
validateTargetProfile("-Tds_6_2", "dxilv1.2--shadermodel6.2-domain",
7676
InMemoryFileSystem, Diags);
77-
validateTargetProfile("-Tds_6_2", "dxil--shadermodel6.2-domain",
77+
validateTargetProfile("-Tds_6_2", "dxilv1.2--shadermodel6.2-domain",
7878
InMemoryFileSystem, Diags);
79-
validateTargetProfile("-Tgs_6_3", "dxil--shadermodel6.3-geometry",
79+
validateTargetProfile("-Tgs_6_3", "dxilv1.3--shadermodel6.3-geometry",
8080
InMemoryFileSystem, Diags);
81-
validateTargetProfile("-Tps_6_4", "dxil--shadermodel6.4-pixel",
81+
validateTargetProfile("-Tps_6_4", "dxilv1.4--shadermodel6.4-pixel",
8282
InMemoryFileSystem, Diags);
83-
validateTargetProfile("-Tcs_6_5", "dxil--shadermodel6.5-compute",
83+
validateTargetProfile("-Tcs_6_5", "dxilv1.5--shadermodel6.5-compute",
8484
InMemoryFileSystem, Diags);
85-
validateTargetProfile("-Tms_6_6", "dxil--shadermodel6.6-mesh",
85+
validateTargetProfile("-Tms_6_6", "dxilv1.6--shadermodel6.6-mesh",
8686
InMemoryFileSystem, Diags);
87-
validateTargetProfile("-Tas_6_7", "dxil--shadermodel6.7-amplification",
87+
validateTargetProfile("-Tas_6_7", "dxilv1.7--shadermodel6.7-amplification",
8888
InMemoryFileSystem, Diags);
89-
validateTargetProfile("-Tlib_6_x", "dxil--shadermodel6.15-library",
89+
validateTargetProfile("-Tcs_6_8", "dxilv1.8--shadermodel6.8-compute",
90+
InMemoryFileSystem, Diags);
91+
validateTargetProfile("-Tlib_6_x", "dxilv1.8--shadermodel6.15-library",
9092
InMemoryFileSystem, Diags);
9193

9294
// Invalid tests.

llvm/include/llvm/TargetParser/Triple.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ class Triple {
176176
DXILSubArch_v1_6,
177177
DXILSubArch_v1_7,
178178
DXILSubArch_v1_8,
179+
LatestDXILSubArch = DXILSubArch_v1_8,
179180
};
180181
enum VendorType {
181182
UnknownVendor,

llvm/lib/IR/Verifier.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,8 @@ struct VerifierSupport {
152152
bool TreatBrokenDebugInfoAsError = true;
153153

154154
explicit VerifierSupport(raw_ostream *OS, const Module &M)
155-
: OS(OS), M(M), MST(&M), TT(M.getTargetTriple()), DL(M.getDataLayout()),
156-
Context(M.getContext()) {}
155+
: OS(OS), M(M), MST(&M), TT(Triple::normalize(M.getTargetTriple())),
156+
DL(M.getDataLayout()), Context(M.getContext()) {}
157157

158158
private:
159159
void Write(const Module *M) {

llvm/lib/TargetParser/Triple.cpp

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,31 @@ StringRef Triple::getArchName(ArchType Kind, SubArchType SubArch) {
115115
if (SubArch == AArch64SubArch_arm64e)
116116
return "arm64e";
117117
break;
118+
case Triple::dxil:
119+
switch (SubArch) {
120+
case Triple::NoSubArch:
121+
case Triple::DXILSubArch_v1_0:
122+
return "dxilv1.0";
123+
case Triple::DXILSubArch_v1_1:
124+
return "dxilv1.1";
125+
case Triple::DXILSubArch_v1_2:
126+
return "dxilv1.2";
127+
case Triple::DXILSubArch_v1_3:
128+
return "dxilv1.3";
129+
case Triple::DXILSubArch_v1_4:
130+
return "dxilv1.4";
131+
case Triple::DXILSubArch_v1_5:
132+
return "dxilv1.5";
133+
case Triple::DXILSubArch_v1_6:
134+
return "dxilv1.6";
135+
case Triple::DXILSubArch_v1_7:
136+
return "dxilv1.7";
137+
case Triple::DXILSubArch_v1_8:
138+
return "dxilv1.8";
139+
default:
140+
break;
141+
}
142+
break;
118143
default:
119144
break;
120145
}
@@ -1014,6 +1039,8 @@ Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
10141039
ObjectFormat = getDefaultFormat(*this);
10151040
}
10161041

1042+
static VersionTuple parseVersionFromName(StringRef Name);
1043+
10171044
std::string Triple::normalize(StringRef Str) {
10181045
bool IsMinGW32 = false;
10191046
bool IsCygwin = false;
@@ -1206,6 +1233,47 @@ std::string Triple::normalize(StringRef Str) {
12061233
}
12071234
}
12081235

1236+
// Normalize DXIL triple if it does not include DXIL version number.
1237+
// Determine DXIL version number using the minor version number of Shader
1238+
// Model version specified in target triple, if any. Prior to decoupling DXIL
1239+
// version numbering from that of Shader Model DXIL version 1.Y corresponds to
1240+
// SM 6.Y. E.g., dxilv1.Y-unknown-shadermodelX.Y-hull
1241+
if (Components[0] == "dxil") {
1242+
if (Components.size() > 4) {
1243+
Components.resize(4);
1244+
}
1245+
// Add DXIL version only if shadermodel is specified in the triple
1246+
if (OS == Triple::ShaderModel) {
1247+
VersionTuple Ver =
1248+
parseVersionFromName(Components[2].drop_front(strlen("shadermodel")));
1249+
// Default DXIL minor version when Shader Model version is anything other
1250+
// than 6.[0...8] or 6.x (which translates to latest current SM version)
1251+
// DXIL version corresponding to Shader Model version other than 6.x
1252+
// is 1.0
1253+
unsigned DXILMinor = 0;
1254+
const unsigned SMMajor = 6;
1255+
const unsigned LatestCurrentDXILMinor = 8;
1256+
if (!Ver.empty()) {
1257+
if (Ver.getMajor() == SMMajor) {
1258+
if (std::optional<unsigned> SMMinor = Ver.getMinor()) {
1259+
DXILMinor = *SMMinor;
1260+
// Ensure specified minor version is supported
1261+
if (DXILMinor > LatestCurrentDXILMinor) {
1262+
report_fatal_error("Unsupported Shader Model version", false);
1263+
}
1264+
}
1265+
}
1266+
} else {
1267+
// Special case: DXIL minor version is set to LatestCurrentDXILMinor for
1268+
// shadermodel6.x is
1269+
if (Components[2] == "shadermodel6.x") {
1270+
DXILMinor = LatestCurrentDXILMinor;
1271+
}
1272+
}
1273+
Components[0] =
1274+
Components[0].str().append("v1.").append(std::to_string(DXILMinor));
1275+
}
1276+
}
12091277
// Stick the corrected components back together to form the normalized string.
12101278
return join(Components, "-");
12111279
}

llvm/unittests/TargetParser/TripleTest.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2454,4 +2454,20 @@ TEST(TripleTest, isArmMClass) {
24542454
EXPECT_TRUE(T.isArmMClass());
24552455
}
24562456
}
2457+
2458+
TEST(TripleTest, DXILNormaizeWithVersion) {
2459+
EXPECT_EQ("dxilv1.0-unknown-shadermodel6.0",
2460+
Triple::normalize("dxilv1.0--shadermodel6.0"));
2461+
EXPECT_EQ("dxilv1.0-unknown-shadermodel6.0",
2462+
Triple::normalize("dxil--shadermodel6.0"));
2463+
EXPECT_EQ("dxilv1.1-unknown-shadermodel6.1-library",
2464+
Triple::normalize("dxil-shadermodel6.1-unknown-library"));
2465+
EXPECT_EQ("dxilv1.8-unknown-shadermodel6.x-unknown",
2466+
Triple::normalize("dxil-unknown-shadermodel6.x-unknown"));
2467+
EXPECT_EQ("dxilv1.8-unknown-shadermodel6.x-unknown",
2468+
Triple::normalize("dxil-unknown-shadermodel6.x-unknown"));
2469+
EXPECT_EQ("dxil-unknown-unknown-unknown", Triple::normalize("dxil---"));
2470+
EXPECT_EQ("dxilv1.0-pc-shadermodel5.0-compute",
2471+
Triple::normalize("dxil-shadermodel5.0-pc-compute"));
2472+
}
24572473
} // end anonymous namespace

0 commit comments

Comments
 (0)