[llvm] [LLVM][Triple] Add an argument to specify canonical form to `Triple::normalize` (PR #122935)
Shilei Tian via llvm-commits
llvm-commits at lists.llvm.org
Tue Jan 14 08:55:34 PST 2025
https://github.com/shiltian created https://github.com/llvm/llvm-project/pull/122935
Currently, the output of `Triple::normalize` can vary depending on how the
`Triple` object is constructed, producing a 3-field, 4-field, or even 5-field
string. However, there is no way to control the format of the output, as all
forms are considered canonical according to the LangRef.
This lack of control can be inconvenient when a specific format is required. To
address this, this PR introduces an argument to specify the desired format (3,
4, or 5 identifiers), with the default set to none to maintain the current
behavior. If the requested format requires more components than are available in
the actual `Data`, `"unknown"` is appended as needed.
>From c7c5b740c813afed4ab2c29ac4d4951d62a04bfc Mon Sep 17 00:00:00 2001
From: Shilei Tian <i at tianshilei.me>
Date: Tue, 14 Jan 2025 11:55:10 -0500
Subject: [PATCH] [LLVM][Triple] Add an argument to specify canonical form to
`Triple::normalize`
Currently, the output of `Triple::normalize` can vary depending on how the
`Triple` object is constructed, producing a 3-field, 4-field, or even 5-field
string. However, there is no way to control the format of the output, as all
forms are considered canonical according to the LangRef.
This lack of control can be inconvenient when a specific format is required. To
address this, this PR introduces an argument to specify the desired format (3,
4, or 5 identifiers), with the default set to none to maintain the current
behavior. If the requested format requires more components than are available in
the actual `Data`, `"unknown"` is appended as needed.
---
llvm/include/llvm/TargetParser/Triple.h | 18 ++++++++++++---
llvm/lib/TargetParser/Triple.cpp | 17 +++++++++++++-
llvm/unittests/TargetParser/TripleTest.cpp | 27 ++++++++++++++++++++++
3 files changed, 58 insertions(+), 4 deletions(-)
diff --git a/llvm/include/llvm/TargetParser/Triple.h b/llvm/include/llvm/TargetParser/Triple.h
index 76914ab34c1f67..6f6300a90dde5b 100644
--- a/llvm/include/llvm/TargetParser/Triple.h
+++ b/llvm/include/llvm/TargetParser/Triple.h
@@ -366,14 +366,26 @@ class Triple {
/// @name Normalization
/// @{
+ /// Canonical form
+ enum class CanonicalForm {
+ NONE = 0,
+ THREE_IDENT = 3, // ARCHITECTURE-VENDOR-OPERATING_SYSTEM
+ FOUR_IDENT = 4, // ARCHITECTURE-VENDOR-OPERATING_SYSTEM-ENVIRONMENT
+ FIVE_IDENT = 5, // ARCHITECTURE-VENDOR-OPERATING_SYSTEM-ENVIRONMENT-FORMAT
+ };
+
/// Turn an arbitrary machine specification into the canonical triple form (or
/// something sensible that the Triple class understands if nothing better can
/// reasonably be done). In particular, it handles the common case in which
- /// otherwise valid components are in the wrong order.
- static std::string normalize(StringRef Str);
+ /// otherwise valid components are in the wrong order. \p Form is used to
+ /// specify the output canonical form.
+ static std::string normalize(StringRef Str,
+ CanonicalForm Form = CanonicalForm::NONE);
/// Return the normalized form of this triple's string.
- std::string normalize() const { return normalize(Data); }
+ std::string normalize(CanonicalForm Form = CanonicalForm::NONE) const {
+ return normalize(Data, Form);
+ }
/// @}
/// @name Typed Component Access
diff --git a/llvm/lib/TargetParser/Triple.cpp b/llvm/lib/TargetParser/Triple.cpp
index 4c1de09e91f21c..acacfa0bf1cd43 100644
--- a/llvm/lib/TargetParser/Triple.cpp
+++ b/llvm/lib/TargetParser/Triple.cpp
@@ -1128,7 +1128,7 @@ static StringRef getDXILArchNameFromShaderModel(StringRef ShaderModelStr) {
return Triple::getArchName(Triple::dxil, Triple::DXILSubArch_v1_0);
}
-std::string Triple::normalize(StringRef Str) {
+std::string Triple::normalize(StringRef Str, CanonicalForm Form) {
bool IsMinGW32 = false;
bool IsCygwin = false;
@@ -1334,6 +1334,21 @@ std::string Triple::normalize(StringRef Str) {
Components[0] = getDXILArchNameFromShaderModel(Components[2]);
}
}
+
+ // Canonicalize the components if necessary.
+ switch (Form) {
+ case CanonicalForm::NONE:
+ break;
+ case CanonicalForm::THREE_IDENT:
+ case CanonicalForm::FOUR_IDENT:
+ case CanonicalForm::FIVE_IDENT: {
+ unsigned NumIdents = static_cast<unsigned>(Form);
+ if (Components.size() < NumIdents)
+ Components.resize(NumIdents, "unknown");
+ break;
+ }
+ }
+
// Stick the corrected components back together to form the normalized string.
return join(Components, "-");
}
diff --git a/llvm/unittests/TargetParser/TripleTest.cpp b/llvm/unittests/TargetParser/TripleTest.cpp
index 7fb7625f8c2d10..dba524adb327cd 100644
--- a/llvm/unittests/TargetParser/TripleTest.cpp
+++ b/llvm/unittests/TargetParser/TripleTest.cpp
@@ -1416,6 +1416,33 @@ TEST(TripleTest, Normalization) {
EXPECT_EQ("x86_64-unknown-linux-gnu", Triple::normalize("x86_64-gnu-linux"));
+ EXPECT_EQ("a-unknown-unknown",
+ Triple::normalize("a", Triple::CanonicalForm::THREE_IDENT));
+ EXPECT_EQ("a-b-unknown",
+ Triple::normalize("a-b", Triple::CanonicalForm::THREE_IDENT));
+ EXPECT_EQ("a-b-c",
+ Triple::normalize("a-b-c", Triple::CanonicalForm::THREE_IDENT));
+ EXPECT_EQ("a-b-c-d",
+ Triple::normalize("a-b-c-d", Triple::CanonicalForm::THREE_IDENT));
+
+ EXPECT_EQ("a-unknown-unknown-unknown",
+ Triple::normalize("a", Triple::CanonicalForm::FOUR_IDENT));
+ EXPECT_EQ("a-b-unknown-unknown",
+ Triple::normalize("a-b", Triple::CanonicalForm::FOUR_IDENT));
+ EXPECT_EQ("a-b-c-unknown",
+ Triple::normalize("a-b-c", Triple::CanonicalForm::FOUR_IDENT));
+ EXPECT_EQ("a-b-c-d",
+ Triple::normalize("a-b-c-d", Triple::CanonicalForm::FOUR_IDENT));
+
+ EXPECT_EQ("a-unknown-unknown-unknown-unknown",
+ Triple::normalize("a", Triple::CanonicalForm::FIVE_IDENT));
+ EXPECT_EQ("a-b-unknown-unknown-unknown",
+ Triple::normalize("a-b", Triple::CanonicalForm::FIVE_IDENT));
+ EXPECT_EQ("a-b-c-unknown-unknown",
+ Triple::normalize("a-b-c", Triple::CanonicalForm::FIVE_IDENT));
+ EXPECT_EQ("a-b-c-d-unknown",
+ Triple::normalize("a-b-c-d", Triple::CanonicalForm::FIVE_IDENT));
+
// Check that normalizing a permutated set of valid components returns a
// triple with the unpermuted components.
//
More information about the llvm-commits
mailing list