Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Driver] Report invalid target triple versions for all environment types. #78655

Merged
merged 20 commits into from
Feb 5, 2024

Conversation

ZijunZhaoCCK
Copy link
Contributor

@ZijunZhaoCCK ZijunZhaoCCK commented Jan 19, 2024

Followup for #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.

Followup for llvm#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.
@ZijunZhaoCCK ZijunZhaoCCK changed the title Make clang report invalid target versions for all environment. Make clang report invalid target versions for all environment types. Jan 19, 2024
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' labels Jan 19, 2024
@llvmbot
Copy link
Collaborator

llvmbot commented Jan 19, 2024

@llvm/pr-subscribers-clang

@llvm/pr-subscribers-clang-driver

Author: None (ZijunZhaoCCK)

Changes

Followup for #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.

Full diff: https://github.com/llvm/llvm-project/pull/78655.diff

6 Files Affected:

  • (modified) clang/lib/Driver/Driver.cpp (+10-9)
  • (modified) clang/test/CodeGen/fp128_complex.c (+1-1)
  • (modified) clang/test/Driver/mips-features.c (+2-2)
  • (modified) clang/test/Frontend/fixed_point_bit_widths.c (+2-2)
  • (modified) llvm/include/llvm/TargetParser/Triple.h (+1-1)
  • (modified) llvm/lib/TargetParser/Triple.cpp (+14)
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;
 }
 

Copy link

github-actions bot commented Jan 19, 2024

⚠️ C/C++ code formatter, clang-format found issues in your code. ⚠️

You can test this locally with the following command:
git-clang-format --diff 7a94acb2da5b20d12f13f3c5f4eb0f3f46e78e73 6f41d09f2265338b6403cbb345b7befdb84fe115 -- clang-tools-extra/test/clang-tidy/checkers/bugprone/implicit-widening-of-multiplication-result-char.cpp clang/lib/Driver/Driver.cpp clang/test/CodeGen/fp128_complex.c clang/test/Driver/mips-features.c clang/test/Frontend/fixed_point_bit_widths.c llvm/include/llvm/TargetParser/Triple.h llvm/lib/TargetParser/Triple.cpp
View the diff from clang-format here.
diff --git a/llvm/include/llvm/TargetParser/Triple.h b/llvm/include/llvm/TargetParser/Triple.h
index 98d8490cc9..5683afd583 100644
--- a/llvm/include/llvm/TargetParser/Triple.h
+++ b/llvm/include/llvm/TargetParser/Triple.h
@@ -255,7 +255,7 @@ public:
     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

ZijunZhaoCCK and others added 7 commits January 19, 2024 13:02
Followup for llvm#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.
Followup for llvm#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.
Followup for llvm#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.
@pirama-arumuga-nainar
Copy link
Collaborator

@llvm/clang-vendors Adding clang vendors. FYI, this change expands error reporting on invalid version numbers to all target triples (This was previously restricted to Android triples). This can have potential downstream impact. Please review/test and let us know if this breaks any valid usages.

ZijunZhaoCCK and others added 4 commits January 25, 2024 18:06
Followup for llvm#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.
ZijunZhaoCCK and others added 2 commits February 1, 2024 15:45
Followup for llvm#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.
Copy link
Member

@MaskRay MaskRay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A better title may be:

[Driver] Report invalid target triple versions for all environment types.

"Driver" is better than "clang" as it is specific about where the error arises.

llvm/lib/TargetParser/Triple.cpp Outdated Show resolved Hide resolved
@ZijunZhaoCCK ZijunZhaoCCK changed the title Make clang report invalid target versions for all environment types. [Driver] Report invalid target triple versions for all environment types. Feb 2, 2024
ZijunZhaoCCK and others added 2 commits February 2, 2024 13:42
Followup for llvm#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.
ZijunZhaoCCK and others added 3 commits February 2, 2024 14:36
Followup for llvm#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.
@ZijunZhaoCCK ZijunZhaoCCK merged commit 34c4a0f into llvm:main Feb 5, 2024
3 of 4 checks passed
@ZijunZhaoCCK ZijunZhaoCCK deleted the check-version branch February 5, 2024 00:42
@glandium
Copy link
Contributor

glandium commented Feb 5, 2024

This broke the wasi-threads target:
clang: error: version 'threads' in target triple 'wasm32-unknown-wasi-threads' is invalid

agozillon pushed a commit to agozillon/llvm-project that referenced this pull request Feb 5, 2024
…pes. (llvm#78655)

Followup for llvm#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.
@ZijunZhaoCCK
Copy link
Contributor Author

This broke the wasi-threads target: clang: error: version 'threads' in target triple 'wasm32-unknown-wasi-threads' is invalid

Because threads is not in EnvironmentType list: https://github.com/llvm/llvm-project/blob/main/llvm/include/llvm/TargetParser/Triple.h#L231 or ObjectType list: https://github.com/llvm/llvm-project/blob/main/llvm/include/llvm/TargetParser/Triple.h#L284 .

The format should be arch-vendor-os-env. If threads is a new environment type, please add it in the list. If wasi-threads is a special case, please let me know!

And one more question, would you mind pointing me out the test case of wasm32-unknown-wasi-threads ? I don't see it. Thank you!

@glandium
Copy link
Contributor

glandium commented Feb 5, 2024

We stumbled upon this downstream because we have jobs building wasi-sdk with clang-trunk, and wasi-sdk builds some things with that target. It apparently comes from WebAssembly/wasi-libc#381

@glandium
Copy link
Contributor

glandium commented Feb 5, 2024

There's apparently also wasm32-wasi-preview2 and wasm32-wasi-pthread, which I suppose are equally broken by this change.

@ZijunZhaoCCK
Copy link
Contributor Author

There's apparently also wasm32-wasi-preview2 and wasm32-wasi-pthread, which I suppose are equally broken by this change.

Yes, I think so. I think adding these environment types in wasi-libc repo could help fix those errors.

@glandium
Copy link
Contributor

glandium commented Feb 5, 2024

Yes, I think so. I think adding these environment types in wasi-libc repo could help fix those errors.

WDYM?

@MaskRay
Copy link
Member

MaskRay commented Feb 5, 2024

There's apparently also wasm32-wasi-preview2 and wasm32-wasi-pthread, which I suppose are equally broken by this change.

Yes, I think so. I think adding these environment types in wasi-libc repo could help fix those errors.

If wasm can arbitrary environment types, it seems that we can opt out the check for isWasm().
We define new environment types when they affect the compiler behavior. If wasm arbitrary sets the environment, we should not define a type for each one.

@ZijunZhaoCCK
Copy link
Contributor Author

There's apparently also wasm32-wasi-preview2 and wasm32-wasi-pthread, which I suppose are equally broken by this change.

Yes, I think so. I think adding these environment types in wasi-libc repo could help fix those errors.

If wasm can arbitrary environment types, it seems that we can opt out the check for isWasm(). We define new environment types when they affect the compiler behavior. If wasm arbitrary sets the environment, we should not define a type for each one.

Okay, I will add this check.

ZijunZhaoCCK added a commit that referenced this pull request Feb 6, 2024
llvm-beanz added a commit to llvm-beanz/llvm-project that referenced this pull request Feb 7, 2024
This updates the HLSL invalid environment tests to adapt to llvm#78655.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants