[clang] [clang][Driver] Support simplified triple versions for config files (PR #111387)

2024-12-12 Thread LLVM Continuous Integration via cfe-commits

llvm-ci wrote:

LLVM Buildbot has detected a new failure on builder 
`openmp-offload-libc-amdgpu-runtime` running on `omp-vega20-1` while building 
`clang` at step 7 "Add check check-offload".

Full details are available at: 
https://lab.llvm.org/buildbot/#/builders/73/builds/10156


Here is the relevant piece of the build log for the reference

```
Step 7 (Add check check-offload) failure: test (failure)
 TEST 'libomptarget :: amdgcn-amd-amdhsa :: 
sanitizer/kernel_crash_async.c' FAILED 
Exit Code: 1

Command Output (stdout):
--
# RUN: at line 2
/home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.build/./bin/clang 
-fopenmp-I 
/home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.src/offload/test 
-I 
/home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.build/runtimes/runtimes-bins/openmp/runtime/src
 -L 
/home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.build/runtimes/runtimes-bins/offload
 -L /home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.build/./lib -L 
/home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.build/runtimes/runtimes-bins/openmp/runtime/src
  -nogpulib 
-Wl,-rpath,/home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.build/runtimes/runtimes-bins/offload
 
-Wl,-rpath,/home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.build/runtimes/runtimes-bins/openmp/runtime/src
 
-Wl,-rpath,/home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.build/./lib
  -fopenmp-targets=amdgcn-amd-amdhsa -O3 
/home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.src/offload/test/sanitizer/kernel_crash_async.c
 -o 
/home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.build/runtimes/runtimes-bins/offload/test/amdgcn-amd-amdhsa/sanitizer/Output/kernel_crash_async.c.tmp
 -Xoffload-linker -lc -Xoffload-linker -lm 
/home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.build/./lib/libomptarget.devicertl.a
# executed command: 
/home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.build/./bin/clang 
-fopenmp -I 
/home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.src/offload/test 
-I 
/home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.build/runtimes/runtimes-bins/openmp/runtime/src
 -L 
/home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.build/runtimes/runtimes-bins/offload
 -L /home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.build/./lib -L 
/home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.build/runtimes/runtimes-bins/openmp/runtime/src
 -nogpulib 
-Wl,-rpath,/home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.build/runtimes/runtimes-bins/offload
 
-Wl,-rpath,/home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.build/runtimes/runtimes-bins/openmp/runtime/src
 
-Wl,-rpath,/home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.build/./lib
 -fopenmp-targets=amdgcn-amd-amdhsa -O3 
/home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.src/offload/test/sanitizer/kernel_crash_async.c
 -o 
/home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.build/runtimes/runtimes-bins/offload/test/amdgcn-amd-amdhsa/sanitizer/Output/kernel_crash_async.c.tmp
 -Xoffload-linker -lc -Xoffload-linker -lm 
/home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.build/./lib/libomptarget.devicertl.a
# RUN: at line 3
/home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.build/./bin/not 
--crash env -u LLVM_DISABLE_SYMBOLIZATION 
OFFLOAD_TRACK_NUM_KERNEL_LAUNCH_TRACES=1 
/home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.build/runtimes/runtimes-bins/offload/test/amdgcn-amd-amdhsa/sanitizer/Output/kernel_crash_async.c.tmp
 2>&1 | 
/home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.build/./bin/FileCheck
 
/home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.src/offload/test/sanitizer/kernel_crash_async.c
 --check-prefixes=TRACE
# executed command: 
/home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.build/./bin/not 
--crash env -u LLVM_DISABLE_SYMBOLIZATION 
OFFLOAD_TRACK_NUM_KERNEL_LAUNCH_TRACES=1 
/home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.build/runtimes/runtimes-bins/offload/test/amdgcn-amd-amdhsa/sanitizer/Output/kernel_crash_async.c.tmp
# executed command: 
/home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.build/./bin/FileCheck
 
/home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.src/offload/test/sanitizer/kernel_crash_async.c
 --check-prefixes=TRACE
# RUN: at line 4
/home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.build/./bin/not 
--crash 
/home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.build/runtimes/runtimes-bins/offload/test/amdgcn-amd-amdhsa/sanitizer/Output/kernel_crash_async.c.tmp
 2>&1 | 
/home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.build/./bin/FileCheck
 
/home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.src/offload/test/sanitizer/kernel_crash_async.c
 --check-prefixes=C

[clang] [clang][Driver] Support simplified triple versions for config files (PR #111387)

2024-12-12 Thread via cfe-commits

github-actions[bot] wrote:



@Bo98 Congratulations on having your first Pull Request (PR) merged into the 
LLVM Project!

Your changes will be combined with recent changes from other authors, then 
tested by our [build bots](https://lab.llvm.org/buildbot/). If there is a 
problem with a build, you may receive a report in an email or a comment on this 
PR.

Please check whether problems have been caused by your change specifically, as 
the builds can include changes from many authors. It is not uncommon for your 
change to be included in a build that fails due to someone else's changes, or 
infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail 
[here](https://llvm.org/docs/MyFirstTypoFix.html#myfirsttypofix-issues-after-landing-your-pr).

If your change does cause a problem, it may be reverted, or you can revert it 
yourself. This is a normal part of [LLVM 
development](https://llvm.org/docs/DeveloperPolicy.html#patch-reversion-policy).
 You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are 
working as expected, well done!


https://github.com/llvm/llvm-project/pull/111387
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][Driver] Support simplified triple versions for config files (PR #111387)

2024-12-12 Thread Carlo Cabrera via cfe-commits

https://github.com/carlocab closed 
https://github.com/llvm/llvm-project/pull/111387
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][Driver] Support simplified triple versions for config files (PR #111387)

2024-12-12 Thread Carlo Cabrera via cfe-commits

carlocab wrote:

Merging this to land the improvement. Happy to work on suggested improvements 
for the tests if any are still outstanding from the discussion in 
https://github.com/llvm/llvm-project/pull/111387#discussion_r1873784293 (since 
it's not clear to me which comments still apply). CC @MaskRay 

https://github.com/llvm/llvm-project/pull/111387
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][Driver] Support simplified triple versions for config files (PR #111387)

2024-12-09 Thread Steven Wu via cfe-commits


@@ -226,3 +226,26 @@
 //
 // RUN: HOME=%S/Inputs/config %clang -### --config-user-dir=~ -v 2>&1 | 
FileCheck %s --check-prefix=CHECK-TILDE
 // CHECK-TILDE: User configuration file directory: {{.*}}/Inputs/config
+
+//--- Fallback to stripping OS versions
+//
+// RUN: touch %t/testdmode/x86_64-apple-darwin23.6.0-clang.cfg
+// RUN: touch %t/testdmode/x86_64-apple-darwin23-clang.cfg
+// RUN: touch %t/testdmode/x86_64-apple-darwin-clang.cfg
+// RUN: %clang -target x86_64-apple-darwin23.6.0 
--config-system-dir=%t/testdmode --config-user-dir= -no-canonical-prefixes 
--version 2>&1 | FileCheck %s -check-prefix DARWIN --implicit-check-not 
'Configuration file:'

cachemeifyoucan wrote:

It actually spells `-target` for Darwin. 

https://github.com/llvm/llvm-project/pull/111387
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][Driver] Support simplified triple versions for config files (PR #111387)

2024-12-06 Thread Fangrui Song via cfe-commits

https://github.com/MaskRay approved this pull request.


https://github.com/llvm/llvm-project/pull/111387
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][Driver] Support simplified triple versions for config files (PR #111387)

2024-12-06 Thread Fangrui Song via cfe-commits


@@ -226,3 +226,26 @@
 //
 // RUN: HOME=%S/Inputs/config %clang -### --config-user-dir=~ -v 2>&1 | 
FileCheck %s --check-prefix=CHECK-TILDE
 // CHECK-TILDE: User configuration file directory: {{.*}}/Inputs/config
+
+//--- Fallback to stripping OS versions
+//
+// RUN: touch %t/testdmode/x86_64-apple-darwin23.6.0-clang.cfg
+// RUN: touch %t/testdmode/x86_64-apple-darwin23-clang.cfg
+// RUN: touch %t/testdmode/x86_64-apple-darwin-clang.cfg
+// RUN: %clang -target x86_64-apple-darwin23.6.0 
--config-system-dir=%t/testdmode --config-user-dir= -no-canonical-prefixes 
--version 2>&1 | FileCheck %s -check-prefix DARWIN --implicit-check-not 
'Configuration file:'

MaskRay wrote:

Prefer `--target=`. `-target ` has been deprecated for a long time.

No need to have `^//$` lines.

https://github.com/llvm/llvm-project/pull/111387
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][Driver] Support simplified triple versions for config files (PR #111387)

2024-12-06 Thread Michał Górny via cfe-commits

https://github.com/mgorny approved this pull request.

LGTM

https://github.com/llvm/llvm-project/pull/111387
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][Driver] Support simplified triple versions for config files (PR #111387)

2024-12-06 Thread Steven Wu via cfe-commits

https://github.com/cachemeifyoucan approved this pull request.

LGTM

https://github.com/llvm/llvm-project/pull/111387
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][Driver] Support simplified triple versions for config files (PR #111387)

2024-10-22 Thread Bo Anderson via cfe-commits

Bo98 wrote:

Ping?

https://github.com/llvm/llvm-project/pull/111387
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][Driver] Support simplified triple versions for config files (PR #111387)

2024-10-07 Thread Bo Anderson via cfe-commits

https://github.com/Bo98 edited https://github.com/llvm/llvm-project/pull/111387
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][Driver] Support simplified triple versions for config files (PR #111387)

2024-10-07 Thread via cfe-commits

llvmbot wrote:




@llvm/pr-subscribers-clang-driver

Author: Bo Anderson (Bo98)


Changes

Currently, the config file system loads the full target triple, e.g. 
`arm64-apple-darwin23.6.0.cfg`.

This is however not very useful as this is a moving target. In the case of 
macOS, that target moves every 2 months.

We can improve this by adding fallbacks that simplify the version component of 
the triple. This pull request adds support for loading 
`arm64-apple-darwin23.cfg` and `arm64-apple-darwin.cfg`. See the included test 
for a demonstration on how it works.

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


2 Files Affected:

- (modified) clang/lib/Driver/Driver.cpp (+39-14) 
- (modified) clang/test/Driver/config-file3.c (+23) 


``diff
diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index a5d43bdac23735..26aa7f67842ce9 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -1147,6 +1147,34 @@ bool Driver::loadConfigFiles() {
   return false;
 }
 
+static bool findTripleConfigFile(llvm::cl::ExpansionContext &ExpCtx,
+ SmallString<128> &ConfigFilePath,
+ llvm::Triple Triple, std::string Suffix) {
+  // First, try the full unmodified triple.
+  if (ExpCtx.findConfigFile(Triple.str() + Suffix, ConfigFilePath))
+return true;
+
+  // Don't continue if we didn't find a parsable version in the triple.
+  VersionTuple OSVersion = Triple.getOSVersion();
+  if (!OSVersion.getMinor().has_value())
+return false;
+
+  std::string BaseOSName = Triple.getOSTypeName(Triple.getOS()).str();
+
+  // Next try strip the version to only include the major component.
+  // e.g. arm64-apple-darwin23.6.0 -> arm64-apple-darwin23
+  if (OSVersion.getMajor() != 0) {
+Triple.setOSName(BaseOSName + llvm::utostr(OSVersion.getMajor()));
+if (ExpCtx.findConfigFile(Triple.str() + Suffix, ConfigFilePath))
+  return true;
+  }
+
+  // Finally, try without any version suffix at all.
+  // e.g. arm64-apple-darwin23.6.0 -> arm64-apple-darwin
+  Triple.setOSName(BaseOSName);
+  return ExpCtx.findConfigFile(Triple.str() + Suffix, ConfigFilePath);
+}
+
 bool Driver::loadDefaultConfigFiles(llvm::cl::ExpansionContext &ExpCtx) {
   // Disable default config if CLANG_NO_DEFAULT_CONFIG is set to a non-empty
   // value.
@@ -1158,7 +1186,7 @@ bool 
Driver::loadDefaultConfigFiles(llvm::cl::ExpansionContext &ExpCtx) {
 return false;
 
   std::string RealMode = getExecutableForDriverMode(Mode);
-  std::string Triple;
+  llvm::Triple Triple;
 
   // If name prefix is present, no --target= override was passed via CLOptions
   // and the name prefix is not a valid triple, force it for backwards
@@ -1169,15 +1197,13 @@ bool 
Driver::loadDefaultConfigFiles(llvm::cl::ExpansionContext &ExpCtx) {
 llvm::Triple PrefixTriple{ClangNameParts.TargetPrefix};
 if (PrefixTriple.getArch() == llvm::Triple::UnknownArch ||
 PrefixTriple.isOSUnknown())
-  Triple = PrefixTriple.str();
+  Triple = PrefixTriple;
   }
 
   // Otherwise, use the real triple as used by the driver.
-  if (Triple.empty()) {
-llvm::Triple RealTriple =
-computeTargetTriple(*this, TargetTriple, *CLOptions);
-Triple = RealTriple.str();
-assert(!Triple.empty());
+  if (Triple.str().empty()) {
+Triple = computeTargetTriple(*this, TargetTriple, *CLOptions);
+assert(!Triple.str().empty());
   }
 
   // Search for config files in the following order:
@@ -1192,21 +1218,21 @@ bool 
Driver::loadDefaultConfigFiles(llvm::cl::ExpansionContext &ExpCtx) {
 
   // Try loading -.cfg, and return if we find a match.
   SmallString<128> CfgFilePath;
-  std::string CfgFileName = Triple + '-' + RealMode + ".cfg";
-  if (ExpCtx.findConfigFile(CfgFileName, CfgFilePath))
+  if (findTripleConfigFile(ExpCtx, CfgFilePath, Triple,
+   "-" + RealMode + ".cfg"))
 return readConfigFile(CfgFilePath, ExpCtx);
 
   bool TryModeSuffix = !ClangNameParts.ModeSuffix.empty() &&
ClangNameParts.ModeSuffix != RealMode;
   if (TryModeSuffix) {
-CfgFileName = Triple + '-' + ClangNameParts.ModeSuffix + ".cfg";
-if (ExpCtx.findConfigFile(CfgFileName, CfgFilePath))
+if (findTripleConfigFile(ExpCtx, CfgFilePath, Triple,
+ "-" + ClangNameParts.ModeSuffix + ".cfg"))
   return readConfigFile(CfgFilePath, ExpCtx);
   }
 
   // Try loading .cfg, and return if loading failed.  If a matching file
   // was not found, still proceed on to try .cfg.
-  CfgFileName = RealMode + ".cfg";
+  std::string CfgFileName = RealMode + ".cfg";
   if (ExpCtx.findConfigFile(CfgFileName, CfgFilePath)) {
 if (readConfigFile(CfgFilePath, ExpCtx))
   return true;
@@ -1218,8 +1244,7 @@ bool 
Driver::loadDefaultConfigFiles(llvm::cl::ExpansionContext &ExpCtx) {
   }
 
   // Try loading .cfg and return if we find a match.
-  CfgFileName = Triple + ".cfg";
-  if (ExpCt

[clang] [clang][Driver] Support simplified triple versions for config files (PR #111387)

2024-10-07 Thread via cfe-commits

llvmbot wrote:




@llvm/pr-subscribers-clang

Author: Bo Anderson (Bo98)


Changes

Currently, the config file system loads the full target triple, e.g. 
`arm64-apple-darwin23.6.0.cfg`.

This is however not very useful as this is a moving target. In the case of 
macOS, that target moves every 2 months.

We can improve this by adding fallbacks that simplify the version component of 
the triple. This pull request adds support for loading 
`arm64-apple-darwin23.cfg` and `arm64-apple-darwin.cfg`. See the included test 
for a demonstration on how it works.

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


2 Files Affected:

- (modified) clang/lib/Driver/Driver.cpp (+39-14) 
- (modified) clang/test/Driver/config-file3.c (+23) 


``diff
diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index a5d43bdac23735..26aa7f67842ce9 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -1147,6 +1147,34 @@ bool Driver::loadConfigFiles() {
   return false;
 }
 
+static bool findTripleConfigFile(llvm::cl::ExpansionContext &ExpCtx,
+ SmallString<128> &ConfigFilePath,
+ llvm::Triple Triple, std::string Suffix) {
+  // First, try the full unmodified triple.
+  if (ExpCtx.findConfigFile(Triple.str() + Suffix, ConfigFilePath))
+return true;
+
+  // Don't continue if we didn't find a parsable version in the triple.
+  VersionTuple OSVersion = Triple.getOSVersion();
+  if (!OSVersion.getMinor().has_value())
+return false;
+
+  std::string BaseOSName = Triple.getOSTypeName(Triple.getOS()).str();
+
+  // Next try strip the version to only include the major component.
+  // e.g. arm64-apple-darwin23.6.0 -> arm64-apple-darwin23
+  if (OSVersion.getMajor() != 0) {
+Triple.setOSName(BaseOSName + llvm::utostr(OSVersion.getMajor()));
+if (ExpCtx.findConfigFile(Triple.str() + Suffix, ConfigFilePath))
+  return true;
+  }
+
+  // Finally, try without any version suffix at all.
+  // e.g. arm64-apple-darwin23.6.0 -> arm64-apple-darwin
+  Triple.setOSName(BaseOSName);
+  return ExpCtx.findConfigFile(Triple.str() + Suffix, ConfigFilePath);
+}
+
 bool Driver::loadDefaultConfigFiles(llvm::cl::ExpansionContext &ExpCtx) {
   // Disable default config if CLANG_NO_DEFAULT_CONFIG is set to a non-empty
   // value.
@@ -1158,7 +1186,7 @@ bool 
Driver::loadDefaultConfigFiles(llvm::cl::ExpansionContext &ExpCtx) {
 return false;
 
   std::string RealMode = getExecutableForDriverMode(Mode);
-  std::string Triple;
+  llvm::Triple Triple;
 
   // If name prefix is present, no --target= override was passed via CLOptions
   // and the name prefix is not a valid triple, force it for backwards
@@ -1169,15 +1197,13 @@ bool 
Driver::loadDefaultConfigFiles(llvm::cl::ExpansionContext &ExpCtx) {
 llvm::Triple PrefixTriple{ClangNameParts.TargetPrefix};
 if (PrefixTriple.getArch() == llvm::Triple::UnknownArch ||
 PrefixTriple.isOSUnknown())
-  Triple = PrefixTriple.str();
+  Triple = PrefixTriple;
   }
 
   // Otherwise, use the real triple as used by the driver.
-  if (Triple.empty()) {
-llvm::Triple RealTriple =
-computeTargetTriple(*this, TargetTriple, *CLOptions);
-Triple = RealTriple.str();
-assert(!Triple.empty());
+  if (Triple.str().empty()) {
+Triple = computeTargetTriple(*this, TargetTriple, *CLOptions);
+assert(!Triple.str().empty());
   }
 
   // Search for config files in the following order:
@@ -1192,21 +1218,21 @@ bool 
Driver::loadDefaultConfigFiles(llvm::cl::ExpansionContext &ExpCtx) {
 
   // Try loading -.cfg, and return if we find a match.
   SmallString<128> CfgFilePath;
-  std::string CfgFileName = Triple + '-' + RealMode + ".cfg";
-  if (ExpCtx.findConfigFile(CfgFileName, CfgFilePath))
+  if (findTripleConfigFile(ExpCtx, CfgFilePath, Triple,
+   "-" + RealMode + ".cfg"))
 return readConfigFile(CfgFilePath, ExpCtx);
 
   bool TryModeSuffix = !ClangNameParts.ModeSuffix.empty() &&
ClangNameParts.ModeSuffix != RealMode;
   if (TryModeSuffix) {
-CfgFileName = Triple + '-' + ClangNameParts.ModeSuffix + ".cfg";
-if (ExpCtx.findConfigFile(CfgFileName, CfgFilePath))
+if (findTripleConfigFile(ExpCtx, CfgFilePath, Triple,
+ "-" + ClangNameParts.ModeSuffix + ".cfg"))
   return readConfigFile(CfgFilePath, ExpCtx);
   }
 
   // Try loading .cfg, and return if loading failed.  If a matching file
   // was not found, still proceed on to try .cfg.
-  CfgFileName = RealMode + ".cfg";
+  std::string CfgFileName = RealMode + ".cfg";
   if (ExpCtx.findConfigFile(CfgFileName, CfgFilePath)) {
 if (readConfigFile(CfgFilePath, ExpCtx))
   return true;
@@ -1218,8 +1244,7 @@ bool 
Driver::loadDefaultConfigFiles(llvm::cl::ExpansionContext &ExpCtx) {
   }
 
   // Try loading .cfg and return if we find a match.
-  CfgFileName = Triple + ".cfg";
-  if (ExpCtx.findC

[clang] [clang][Driver] Support simplified triple versions for config files (PR #111387)

2024-10-07 Thread via cfe-commits

github-actions[bot] wrote:



Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this 
page.

If this is not working for you, it is probably because you do not have write 
permissions for the repository. In which case you can instead tag reviewers by 
name in a comment by using `@` followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a 
review by "ping"ing the PR by adding a comment “Ping”. The common courtesy 
"ping" rate is once a week. Please remember that you are asking for valuable 
time from other developers.

If you have further questions, they may be answered by the [LLVM GitHub User 
Guide](https://llvm.org/docs/GitHub.html).

You can also ask questions in a comment on this PR, on the [LLVM 
Discord](https://discord.com/invite/xS7Z362) or on the 
[forums](https://discourse.llvm.org/).

https://github.com/llvm/llvm-project/pull/111387
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][Driver] Support simplified triple versions for config files (PR #111387)

2024-10-07 Thread Bo Anderson via cfe-commits

https://github.com/Bo98 created https://github.com/llvm/llvm-project/pull/111387

Currently, the config file system loads the full target triple, e.g. 
`arm64-apple-darwin23.6.0.cfg`.

This is however not very useful as this is a moving target. In the case of 
macOS, that target moves every 2 months.

We can improve this by adding fallbacks that simplify the version component of 
the triple. This pull request adds support for loading 
`arm64-apple-darwin23.cfg` and `arm64-apple-darwin.cfg`. See the included test 
for a demonstration on how it works.

>From 88dd0d33147a7f46a3c9df4aed28ad4e47ef597c Mon Sep 17 00:00:00 2001
From: Bo Anderson 
Date: Mon, 7 Oct 2024 15:44:23 +0100
Subject: [PATCH] [clang][Driver] Support simplified triple versions for config
 files

---
 clang/lib/Driver/Driver.cpp  | 53 +++-
 clang/test/Driver/config-file3.c | 23 ++
 2 files changed, 62 insertions(+), 14 deletions(-)

diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index a5d43bdac23735..26aa7f67842ce9 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -1147,6 +1147,34 @@ bool Driver::loadConfigFiles() {
   return false;
 }
 
+static bool findTripleConfigFile(llvm::cl::ExpansionContext &ExpCtx,
+ SmallString<128> &ConfigFilePath,
+ llvm::Triple Triple, std::string Suffix) {
+  // First, try the full unmodified triple.
+  if (ExpCtx.findConfigFile(Triple.str() + Suffix, ConfigFilePath))
+return true;
+
+  // Don't continue if we didn't find a parsable version in the triple.
+  VersionTuple OSVersion = Triple.getOSVersion();
+  if (!OSVersion.getMinor().has_value())
+return false;
+
+  std::string BaseOSName = Triple.getOSTypeName(Triple.getOS()).str();
+
+  // Next try strip the version to only include the major component.
+  // e.g. arm64-apple-darwin23.6.0 -> arm64-apple-darwin23
+  if (OSVersion.getMajor() != 0) {
+Triple.setOSName(BaseOSName + llvm::utostr(OSVersion.getMajor()));
+if (ExpCtx.findConfigFile(Triple.str() + Suffix, ConfigFilePath))
+  return true;
+  }
+
+  // Finally, try without any version suffix at all.
+  // e.g. arm64-apple-darwin23.6.0 -> arm64-apple-darwin
+  Triple.setOSName(BaseOSName);
+  return ExpCtx.findConfigFile(Triple.str() + Suffix, ConfigFilePath);
+}
+
 bool Driver::loadDefaultConfigFiles(llvm::cl::ExpansionContext &ExpCtx) {
   // Disable default config if CLANG_NO_DEFAULT_CONFIG is set to a non-empty
   // value.
@@ -1158,7 +1186,7 @@ bool 
Driver::loadDefaultConfigFiles(llvm::cl::ExpansionContext &ExpCtx) {
 return false;
 
   std::string RealMode = getExecutableForDriverMode(Mode);
-  std::string Triple;
+  llvm::Triple Triple;
 
   // If name prefix is present, no --target= override was passed via CLOptions
   // and the name prefix is not a valid triple, force it for backwards
@@ -1169,15 +1197,13 @@ bool 
Driver::loadDefaultConfigFiles(llvm::cl::ExpansionContext &ExpCtx) {
 llvm::Triple PrefixTriple{ClangNameParts.TargetPrefix};
 if (PrefixTriple.getArch() == llvm::Triple::UnknownArch ||
 PrefixTriple.isOSUnknown())
-  Triple = PrefixTriple.str();
+  Triple = PrefixTriple;
   }
 
   // Otherwise, use the real triple as used by the driver.
-  if (Triple.empty()) {
-llvm::Triple RealTriple =
-computeTargetTriple(*this, TargetTriple, *CLOptions);
-Triple = RealTriple.str();
-assert(!Triple.empty());
+  if (Triple.str().empty()) {
+Triple = computeTargetTriple(*this, TargetTriple, *CLOptions);
+assert(!Triple.str().empty());
   }
 
   // Search for config files in the following order:
@@ -1192,21 +1218,21 @@ bool 
Driver::loadDefaultConfigFiles(llvm::cl::ExpansionContext &ExpCtx) {
 
   // Try loading -.cfg, and return if we find a match.
   SmallString<128> CfgFilePath;
-  std::string CfgFileName = Triple + '-' + RealMode + ".cfg";
-  if (ExpCtx.findConfigFile(CfgFileName, CfgFilePath))
+  if (findTripleConfigFile(ExpCtx, CfgFilePath, Triple,
+   "-" + RealMode + ".cfg"))
 return readConfigFile(CfgFilePath, ExpCtx);
 
   bool TryModeSuffix = !ClangNameParts.ModeSuffix.empty() &&
ClangNameParts.ModeSuffix != RealMode;
   if (TryModeSuffix) {
-CfgFileName = Triple + '-' + ClangNameParts.ModeSuffix + ".cfg";
-if (ExpCtx.findConfigFile(CfgFileName, CfgFilePath))
+if (findTripleConfigFile(ExpCtx, CfgFilePath, Triple,
+ "-" + ClangNameParts.ModeSuffix + ".cfg"))
   return readConfigFile(CfgFilePath, ExpCtx);
   }
 
   // Try loading .cfg, and return if loading failed.  If a matching file
   // was not found, still proceed on to try .cfg.
-  CfgFileName = RealMode + ".cfg";
+  std::string CfgFileName = RealMode + ".cfg";
   if (ExpCtx.findConfigFile(CfgFileName, CfgFilePath)) {
 if (readConfigFile(CfgFilePath, ExpCtx))
   return true;
@@ -1218,8 +1244,7 @@ bool