[flang-commits] [flang] [clang-tools-extra] [libcxx] [libc] [llvm] [lld] [lldb] [clang] [compiler-rt] Make clang report invalid target versions for all environment types. (PR #78655)

via flang-commits flang-commits at lists.llvm.org
Thu Jan 25 18:07:12 PST 2024


https://github.com/ZijunZhaoCCK updated https://github.com/llvm/llvm-project/pull/78655

>From f440f44e7e270d4636ad39f4e4223c904e496d3a Mon Sep 17 00:00:00 2001
From: zijunzhao <zijunzhao at google.com>
Date: Fri, 19 Jan 2024 00:47:05 +0000
Subject: [PATCH 1/4] Make clang report invalid target versions for all
 environment.

Followup for https://github.com/llvm/llvm-project/pull/75373

1. Make this feature not just available for android, but everyone.
2. Correct some target triples/
3. Add opencl to the environment type list.
---
 clang/lib/Driver/Driver.cpp                  | 19 ++++++++++---------
 clang/test/CodeGen/fp128_complex.c           |  2 +-
 clang/test/Driver/mips-features.c            |  4 ++--
 clang/test/Frontend/fixed_point_bit_widths.c |  4 ++--
 llvm/include/llvm/TargetParser/Triple.h      |  2 +-
 llvm/lib/TargetParser/Triple.cpp             | 14 ++++++++++++++
 6 files changed, 30 insertions(+), 15 deletions(-)

diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index 1889ea28079df10..2d6986d145483b8 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -1430,15 +1430,16 @@ Compilation *Driver::BuildCompilation(ArrayRef<const char *> ArgList) {
   const ToolChain &TC = getToolChain(
       *UArgs, computeTargetTriple(*this, TargetTriple, *UArgs));
 
-  if (TC.getTriple().isAndroid()) {
-    llvm::Triple Triple = TC.getTriple();
-    StringRef TripleVersionName = Triple.getEnvironmentVersionString();
-
-    if (Triple.getEnvironmentVersion().empty() && TripleVersionName != "") {
-      Diags.Report(diag::err_drv_triple_version_invalid)
-          << TripleVersionName << TC.getTripleString();
-      ContainsError = true;
-    }
+  // Check if the environment version is valid.
+  llvm::Triple Triple = TC.getTriple();
+  StringRef TripleVersionName = Triple.getEnvironmentVersionString();
+  StringRef TripleObjectFormat = Triple.getObjectFormatTypeName(Triple.getObjectFormat());
+
+  if (Triple.getEnvironmentVersion().empty() && TripleVersionName != ""
+    && TripleVersionName != TripleObjectFormat) {
+    Diags.Report(diag::err_drv_triple_version_invalid)
+        << TripleVersionName << TC.getTripleString();
+    ContainsError = true;
   }
 
   // Report warning when arm64EC option is overridden by specified target
diff --git a/clang/test/CodeGen/fp128_complex.c b/clang/test/CodeGen/fp128_complex.c
index 48659d224168416..0e87cbe8ce81219 100644
--- a/clang/test/CodeGen/fp128_complex.c
+++ b/clang/test/CodeGen/fp128_complex.c
@@ -1,4 +1,4 @@
-// RUN: %clang -target aarch64-linux-gnuabi %s -S -emit-llvm -o - | FileCheck %s
+// RUN: %clang -target aarch64-linux-gnueabi %s -S -emit-llvm -o - | FileCheck %s
 
 _Complex long double a, b, c, d;
 void test_fp128_compound_assign(void) {
diff --git a/clang/test/Driver/mips-features.c b/clang/test/Driver/mips-features.c
index fad6009ffb89bab..18edcd05ea85c9f 100644
--- a/clang/test/Driver/mips-features.c
+++ b/clang/test/Driver/mips-features.c
@@ -400,12 +400,12 @@
 // LONG-CALLS-DEF-NOT: "long-calls"
 //
 // -mbranch-likely
-// RUN: %clang -target -mips-mti-linux-gnu -### -c %s -mbranch-likely 2>&1 \
+// RUN: %clang -target mips-mti-linux-gnu -### -c %s -mbranch-likely 2>&1 \
 // RUN:   | FileCheck --check-prefix=BRANCH-LIKELY %s
 // BRANCH-LIKELY: argument unused during compilation: '-mbranch-likely'
 //
 // -mno-branch-likely
-// RUN: %clang -target -mips-mti-linux-gnu -### -c %s -mno-branch-likely 2>&1 \
+// RUN: %clang -target mips-mti-linux-gnu -### -c %s -mno-branch-likely 2>&1 \
 // RUN:   | FileCheck --check-prefix=NO-BRANCH-LIKELY %s
 // NO-BRANCH-LIKELY: argument unused during compilation: '-mno-branch-likely'
 
diff --git a/clang/test/Frontend/fixed_point_bit_widths.c b/clang/test/Frontend/fixed_point_bit_widths.c
index ac8db49ed516aef..e56f787e824f24a 100644
--- a/clang/test/Frontend/fixed_point_bit_widths.c
+++ b/clang/test/Frontend/fixed_point_bit_widths.c
@@ -1,7 +1,7 @@
 // RUN: %clang -x c -ffixed-point -S -emit-llvm -o - %s | FileCheck %s
-// RUN: %clang -x c -ffixed-point -S -emit-llvm -o - --target=x86_64-scei-ps4-ubuntu-fast %s | FileCheck %s
+// RUN: %clang -x c -ffixed-point -S -emit-llvm -o - --target=x86_64-scei-ps4 %s | FileCheck %s
 // RUN: %clang -x c -ffixed-point -S -emit-llvm -o - --target=ppc64 %s | FileCheck %s
-// RUN: %clang -x c -ffixed-point -S -emit-llvm -o - --target=x86_64-scei-ps4-windows10pro-fast %s | FileCheck %s
+// RUN: %clang -x c -ffixed-point -S -emit-llvm -o - --target=x86_64-scei-ps4 %s | FileCheck %s
 
 /* Primary signed _Accum */
 
diff --git a/llvm/include/llvm/TargetParser/Triple.h b/llvm/include/llvm/TargetParser/Triple.h
index 95014a546f72453..525ea6df3643ca6 100644
--- a/llvm/include/llvm/TargetParser/Triple.h
+++ b/llvm/include/llvm/TargetParser/Triple.h
@@ -273,7 +273,7 @@ class Triple {
     Callable,
     Mesh,
     Amplification,
-
+    OpenCL,
     OpenHOS,
 
     LastEnvironmentType = OpenHOS
diff --git a/llvm/lib/TargetParser/Triple.cpp b/llvm/lib/TargetParser/Triple.cpp
index b9971c25af71f39..7bb2fb9d1365e2f 100644
--- a/llvm/lib/TargetParser/Triple.cpp
+++ b/llvm/lib/TargetParser/Triple.cpp
@@ -320,6 +320,7 @@ StringRef Triple::getEnvironmentTypeName(EnvironmentType Kind) {
   case Callable: return "callable";
   case Mesh: return "mesh";
   case Amplification: return "amplification";
+  case OpenCL: return "opencl";
   case OpenHOS: return "ohos";
   }
 
@@ -687,6 +688,7 @@ static Triple::EnvironmentType parseEnvironment(StringRef EnvironmentName) {
       .StartsWith("callable", Triple::Callable)
       .StartsWith("mesh", Triple::Mesh)
       .StartsWith("amplification", Triple::Amplification)
+      .StartsWith("opencl", Triple::OpenCL)
       .StartsWith("ohos", Triple::OpenHOS)
       .Default(Triple::UnknownEnvironment);
 }
@@ -1211,8 +1213,20 @@ VersionTuple Triple::getEnvironmentVersion() const {
 
 StringRef Triple::getEnvironmentVersionString() const {
   StringRef EnvironmentName = getEnvironmentName();
+
+  // none is a valid environment type - it basically amounts to a freestanding environment.
+  if (EnvironmentName == "none")
+    return "";
+
   StringRef EnvironmentTypeName = getEnvironmentTypeName(getEnvironment());
   EnvironmentName.consume_front(EnvironmentTypeName);
+
+  if (EnvironmentName.starts_with("-")) {
+    // arch-vendor-os-env-obj is correct
+    StringRef ObjectFormatType = getObjectFormatTypeName(getObjectFormat());
+    if (ObjectFormatType == StringRef(EnvironmentName).split('-').second)
+      return "";
+  }
   return EnvironmentName;
 }
 

>From a2e1335bbc39c8e84b1c1528be9a928588fbbc11 Mon Sep 17 00:00:00 2001
From: zijunzhao <zijunzhao at google.com>
Date: Fri, 19 Jan 2024 00:47:05 +0000
Subject: [PATCH 2/4] Make clang report invalid target versions for all
 environment.

Followup for https://github.com/llvm/llvm-project/pull/75373

1. Make this feature not just available for android, but everyone.
2. Correct some target triples.
3. Add opencl to the environment type list.
---
 clang/lib/Driver/Driver.cpp             | 7 ++++---
 llvm/include/llvm/TargetParser/Triple.h | 2 +-
 llvm/lib/TargetParser/Triple.cpp        | 6 ++++--
 3 files changed, 9 insertions(+), 6 deletions(-)

diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index 55df204d98a44d7..04e2f5248fce658 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -1433,10 +1433,11 @@ Compilation *Driver::BuildCompilation(ArrayRef<const char *> ArgList) {
   // Check if the environment version is valid.
   llvm::Triple Triple = TC.getTriple();
   StringRef TripleVersionName = Triple.getEnvironmentVersionString();
-  StringRef TripleObjectFormat = Triple.getObjectFormatTypeName(Triple.getObjectFormat());
+  StringRef TripleObjectFormat =
+      Triple.getObjectFormatTypeName(Triple.getObjectFormat());
 
-  if (Triple.getEnvironmentVersion().empty() && TripleVersionName != ""
-    && TripleVersionName != TripleObjectFormat) {
+  if (Triple.getEnvironmentVersion().empty() && TripleVersionName != "" &&
+      TripleVersionName != TripleObjectFormat) {
     Diags.Report(diag::err_drv_triple_version_invalid)
         << TripleVersionName << TC.getTripleString();
     ContainsError = true;
diff --git a/llvm/include/llvm/TargetParser/Triple.h b/llvm/include/llvm/TargetParser/Triple.h
index ac8931871eef063..1031e4d091bf2fe 100644
--- a/llvm/include/llvm/TargetParser/Triple.h
+++ b/llvm/include/llvm/TargetParser/Triple.h
@@ -255,7 +255,7 @@ class Triple {
     Cygnus,
     CoreCLR,
     Simulator, // Simulator variants of other systems, e.g., Apple's iOS
-    MacABI, // Mac Catalyst variant of Apple's iOS deployment target.
+    MacABI,    // Mac Catalyst variant of Apple's iOS deployment target.
 
     // Shader Stages
     // The order of these values matters, and must be kept in sync with the
diff --git a/llvm/lib/TargetParser/Triple.cpp b/llvm/lib/TargetParser/Triple.cpp
index 5e8ca8f224ff5e9..3cf6a774f3678fb 100644
--- a/llvm/lib/TargetParser/Triple.cpp
+++ b/llvm/lib/TargetParser/Triple.cpp
@@ -323,7 +323,8 @@ StringRef Triple::getEnvironmentTypeName(EnvironmentType Kind) {
   case Callable: return "callable";
   case Mesh: return "mesh";
   case Amplification: return "amplification";
-  case OpenCL: return "opencl";
+  case OpenCL:
+    return "opencl";
   case OpenHOS: return "ohos";
   }
 
@@ -1220,7 +1221,8 @@ VersionTuple Triple::getEnvironmentVersion() const {
 StringRef Triple::getEnvironmentVersionString() const {
   StringRef EnvironmentName = getEnvironmentName();
 
-  // none is a valid environment type - it basically amounts to a freestanding environment.
+  // none is a valid environment type - it basically amounts to a freestanding
+  // environment.
   if (EnvironmentName == "none")
     return "";
 

>From 036ea3456719c080b84840d7937fa8e59091c288 Mon Sep 17 00:00:00 2001
From: zijunzhao <zijunzhao at google.com>
Date: Fri, 19 Jan 2024 00:47:05 +0000
Subject: [PATCH 3/4] Make clang report invalid target versions for all
 environment.

Followup for https://github.com/llvm/llvm-project/pull/75373

1. Make this feature not just available for android, but everyone.
2. Correct some target triples.
3. Add opencl to the environment type list.
---
 llvm/lib/TargetParser/Triple.cpp | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

diff --git a/llvm/lib/TargetParser/Triple.cpp b/llvm/lib/TargetParser/Triple.cpp
index 8ecd189120e488a..8076872f310293d 100644
--- a/llvm/lib/TargetParser/Triple.cpp
+++ b/llvm/lib/TargetParser/Triple.cpp
@@ -1231,11 +1231,13 @@ StringRef Triple::getEnvironmentVersionString() const {
   StringRef EnvironmentTypeName = getEnvironmentTypeName(getEnvironment());
   EnvironmentName.consume_front(EnvironmentTypeName);
 
-  if (EnvironmentName.starts_with("-")) {
-    // arch-vendor-os-env-obj is correct
-    StringRef ObjectFormatType = getObjectFormatTypeName(getObjectFormat());
-    if (ObjectFormatType == StringRef(EnvironmentName).split('-').second)
-      return "";
+  if (EnvironmentName.contains("-")) {
+    // -obj is the suffix
+    if (getObjectFormat() != Triple::UnknownObjectFormat) {
+      StringRef ObjectFormatTypeName = getObjectFormatTypeName(getObjectFormat());
+      StringRef Suffix = (Twine("-") + ObjectFormatTypeName).str();
+      EnvironmentName.consume_back(Suffix);
+    }
   }
   return EnvironmentName;
 }

>From 2ab3f7be920e35d840dd978ad8027fd9b5f5d7d2 Mon Sep 17 00:00:00 2001
From: zijunzhao <zijunzhao at google.com>
Date: Fri, 19 Jan 2024 00:47:05 +0000
Subject: [PATCH 4/4] Make clang report invalid target versions for all
 environment.

Followup for https://github.com/llvm/llvm-project/pull/75373

1. Make this feature not just available for android, but everyone.
2. Correct some target triples.
3. Add opencl to the environment type list.
---
 llvm/lib/TargetParser/Triple.cpp | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/llvm/lib/TargetParser/Triple.cpp b/llvm/lib/TargetParser/Triple.cpp
index 8076872f310293d..64ae9f7e7fd29f8 100644
--- a/llvm/lib/TargetParser/Triple.cpp
+++ b/llvm/lib/TargetParser/Triple.cpp
@@ -1234,7 +1234,8 @@ StringRef Triple::getEnvironmentVersionString() const {
   if (EnvironmentName.contains("-")) {
     // -obj is the suffix
     if (getObjectFormat() != Triple::UnknownObjectFormat) {
-      StringRef ObjectFormatTypeName = getObjectFormatTypeName(getObjectFormat());
+      StringRef ObjectFormatTypeName =
+          getObjectFormatTypeName(getObjectFormat());
       StringRef Suffix = (Twine("-") + ObjectFormatTypeName).str();
       EnvironmentName.consume_back(Suffix);
     }



More information about the flang-commits mailing list