[clang] [clang][Driver] Fix triple config loading for clang-cl (PR #111397)

via cfe-commits cfe-commits at lists.llvm.org
Mon Oct 7 09:08:51 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-driver

Author: Bo Anderson (Bo98)

<details>
<summary>Changes</summary>

`clang-cl` sets the default triple to `<arch>-pc-windows-msvc`. The host triple was however being used instead when loading default configs.

Move the default triple calculation earlier to handle this.

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


4 Files Affected:

- (modified) clang/lib/Driver/Driver.cpp (+24-11) 
- (added) clang/test/Driver/Inputs/config-cl/arm64ec-pc-windows-msvc.cfg () 
- (added) clang/test/Driver/Inputs/config-cl/config-arm64ec-arg.cfg (+1) 
- (modified) clang/test/Driver/config-file.c (+10) 


``````````diff
diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index a5d43bdac23735..541e79ec97056b 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -1247,6 +1247,19 @@ Compilation *Driver::BuildCompilation(ArrayRef<const char *> ArgList) {
   CLOptions = std::make_unique<InputArgList>(
       ParseArgStrings(ArgList.slice(1), /*UseDriverMode=*/true, ContainsError));
 
+  // We want to determine the triple early so that we load the correct config.
+  if (IsCLMode()) {
+    // clang-cl targets MSVC-style Win32.
+    llvm::Triple T(TargetTriple);
+    T.setOS(llvm::Triple::Win32);
+    T.setVendor(llvm::Triple::PC);
+    T.setEnvironment(llvm::Triple::MSVC);
+    T.setObjectFormat(llvm::Triple::COFF);
+    if (CLOptions->hasArg(options::OPT__SLASH_arm64EC))
+      T.setArch(llvm::Triple::aarch64, llvm::Triple::AArch64SubArch_arm64ec);
+    TargetTriple = T.str();
+  }
+
   // Try parsing configuration file.
   if (!ContainsError)
     ContainsError = loadConfigFiles();
@@ -1286,6 +1299,16 @@ Compilation *Driver::BuildCompilation(ArrayRef<const char *> ArgList) {
           appendOneArg(Args, Opt, nullptr);
         }
     }
+
+    // The config file may have changed the architecture so apply it.
+    if (HasConfigFile && Args.hasArg(options::OPT__SLASH_arm64EC)) {
+      llvm::Triple T(TargetTriple);
+      if (T.getArch() != llvm::Triple::aarch64 ||
+          T.getSubArch() != llvm::Triple::AArch64SubArch_arm64ec) {
+        T.setArch(llvm::Triple::aarch64, llvm::Triple::AArch64SubArch_arm64ec);
+        TargetTriple = T.str();
+      }
+    }
   }
 
   // Check for working directory option before accessing any files
@@ -1336,17 +1359,7 @@ Compilation *Driver::BuildCompilation(ArrayRef<const char *> ArgList) {
 
   // FIXME: TargetTriple is used by the target-prefixed calls to as/ld
   // and getToolChain is const.
-  if (IsCLMode()) {
-    // clang-cl targets MSVC-style Win32.
-    llvm::Triple T(TargetTriple);
-    T.setOS(llvm::Triple::Win32);
-    T.setVendor(llvm::Triple::PC);
-    T.setEnvironment(llvm::Triple::MSVC);
-    T.setObjectFormat(llvm::Triple::COFF);
-    if (Args.hasArg(options::OPT__SLASH_arm64EC))
-      T.setArch(llvm::Triple::aarch64, llvm::Triple::AArch64SubArch_arm64ec);
-    TargetTriple = T.str();
-  } else if (IsDXCMode()) {
+  if (IsDXCMode()) {
     // Build TargetTriple from target_profile option for clang-dxc.
     if (const Arg *A = Args.getLastArg(options::OPT_target_profile)) {
       StringRef TargetProfile = A->getValue();
diff --git a/clang/test/Driver/Inputs/config-cl/arm64ec-pc-windows-msvc.cfg b/clang/test/Driver/Inputs/config-cl/arm64ec-pc-windows-msvc.cfg
new file mode 100644
index 00000000000000..e69de29bb2d1d6
diff --git a/clang/test/Driver/Inputs/config-cl/config-arm64ec-arg.cfg b/clang/test/Driver/Inputs/config-cl/config-arm64ec-arg.cfg
new file mode 100644
index 00000000000000..2eb4402ddf6cd7
--- /dev/null
+++ b/clang/test/Driver/Inputs/config-cl/config-arm64ec-arg.cfg
@@ -0,0 +1 @@
+/arm64EC
diff --git a/clang/test/Driver/config-file.c b/clang/test/Driver/config-file.c
index 9df830ca4c7538..cbad0e408b9ab4 100644
--- a/clang/test/Driver/config-file.c
+++ b/clang/test/Driver/config-file.c
@@ -82,3 +82,13 @@
 // CHECK-TWO-CONFIGS: -isysroot
 // CHECK-TWO-CONFIGS-SAME: /opt/data
 // CHECK-TWO-CONFIGS-SAME: -Wall
+
+
+//--- clang-cl loads the correct triple
+// RUN: env -u CLANG_NO_DEFAULT_CONFIG %clang_cl /arm64EC --config-system-dir=%S/Inputs/config-cl --config-user-dir= -v 2>&1 | FileCheck %s -check-prefix CHECK-CL --implicit-check-not 'Configuration file:'
+// CHECK-CL: Configuration file: {{.*}}Inputs{{.}}config-cl{{.}}arm64ec-pc-windows-msvc.cfg
+
+//--- clang-cl configs support setting /arm64EC
+// RUN: %clang_cl --config-system-dir=%S/Inputs/config-cl --config-user-dir= --config=config-arm64ec-arg.cfg -v 2>&1 | FileCheck %s -check-prefix CHECK-CL-FLAG --implicit-check-not 'Configuration file:'
+// CHECK-CL-FLAG: Target: arm64ec-pc-windows-msvc
+// CHECK-CL-FLAG: Configuration file: {{.*}}Inputs{{.}}config-cl{{.}}config-arm64ec-arg.cfg

``````````

</details>


https://github.com/llvm/llvm-project/pull/111397


More information about the cfe-commits mailing list