Skip to content

Commit ceaf226

Browse files
author
Bharadwaj Yadavalli
committed
Refine normalization of DXIL triple string.
Add unit tests for DXIL triple normalization
1 parent 3b74e41 commit ceaf226

File tree

2 files changed

+47
-12
lines changed

2 files changed

+47
-12
lines changed

llvm/lib/TargetParser/Triple.cpp

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,6 +1038,8 @@ Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
10381038
ObjectFormat = getDefaultFormat(*this);
10391039
}
10401040

1041+
static VersionTuple parseVersionFromName(StringRef Name);
1042+
10411043
std::string Triple::normalize(StringRef Str) {
10421044
bool IsMinGW32 = false;
10431045
bool IsCygwin = false;
@@ -1236,21 +1238,39 @@ std::string Triple::normalize(StringRef Str) {
12361238
// version numbering from that of Shader Model DXIL version 1.Y corresponds to
12371239
// SM 6.Y. E.g., dxilv1.Y-unknown-shadermodelX.Y-hull
12381240
if (Components[0] == "dxil") {
1239-
std::string DXILVerStr{"dxilv1."};
1240-
if (Components.size() > 2) {
1241-
// OS component specified
1242-
if (Components[2].starts_with("shadermodel6.")) {
1243-
Components[0] = DXILVerStr.append(
1244-
Components[2].drop_front(strlen("shadermodel6.")));
1245-
} else if (Components[2].starts_with("shadermodel")) {
1246-
// If shader model specified is other than 6.x, set DXIL Version to 1.0
1247-
Components[0] = DXILVerStr.append("0");
1241+
if (Components.size() > 4) {
1242+
Components.resize(4);
1243+
}
1244+
// Add DXIL version only if shadermodel is specified in the triple
1245+
if (OS == Triple::ShaderModel) {
1246+
VersionTuple Ver = parseVersionFromName(Components[2].drop_front(strlen("shadermodel")));
1247+
// Default DXIL minor version when Shader Model version is anything other than
1248+
// 6.[0...8] or 6.x (which translates to latest current SM version)
1249+
// DXIL version corresponding to Shader Model version other than 6.x is 1.0
1250+
unsigned DXILMinor = 0;
1251+
const unsigned SMMajor = 6;
1252+
const unsigned LatestCurrentDXILMinor = 8;
1253+
if (!Ver.empty()) {
1254+
if (Ver.getMajor() == SMMajor) {
1255+
if (std::optional<unsigned> SMMinor = Ver.getMinor()) {
1256+
DXILMinor = *SMMinor;
1257+
// Ensure specified minor version is supported
1258+
if (DXILMinor > LatestCurrentDXILMinor) {
1259+
report_fatal_error("Unsupported Shader Model version", false);
1260+
}
1261+
}
1262+
}
1263+
} else {
1264+
// Special case: DXIL minor version is set to LatestCurrentDXILMinor for
1265+
// shadermodel6.x is
1266+
if (Components[2] == "shadermodel6.x") {
1267+
DXILMinor = LatestCurrentDXILMinor;
1268+
}
12481269
}
1270+
Components[0] =
1271+
Components[0].str().append("v1.").append(std::to_string(DXILMinor));
12491272
}
1250-
// DXIL version is not set for a non-specified OS string or one that does
1251-
// not starts with shadermodel.
12521273
}
1253-
12541274
// Stick the corrected components back together to form the normalized string.
12551275
return join(Components, "-");
12561276
}

llvm/unittests/TargetParser/TripleTest.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2454,4 +2454,19 @@ 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", Triple::normalize("dxil-shadermodel5.0-pc-compute"));
2471+
}
24572472
} // end anonymous namespace

0 commit comments

Comments
 (0)