[clang] [flang] [flang] Add MSC_VER and target arch defines when targeting the MSVC ABI (PR #73250)

David Truby via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 30 08:25:29 PST 2023


https://github.com/DavidTruby updated https://github.com/llvm/llvm-project/pull/73250

>From 656463c6298b981cbd43d1d5042438eab700143b Mon Sep 17 00:00:00 2001
From: David Truby <david at truby.dev>
Date: Thu, 23 Nov 2023 15:32:57 +0000
Subject: [PATCH 1/2] [flang] Add MSC_VER and target arch defines when
 targeting the MSVC ABI

---
 clang/lib/Driver/ToolChains/Flang.cpp | 24 ++++++++++++++++++++++++
 flang/test/Driver/msvc-defines.f90    | 10 ++++++++++
 2 files changed, 34 insertions(+)
 create mode 100644 flang/test/Driver/msvc-defines.f90

diff --git a/clang/lib/Driver/ToolChains/Flang.cpp b/clang/lib/Driver/ToolChains/Flang.cpp
index 01f07b9228256cf..87aa546e5549423 100644
--- a/clang/lib/Driver/ToolChains/Flang.cpp
+++ b/clang/lib/Driver/ToolChains/Flang.cpp
@@ -204,6 +204,29 @@ void Flang::AddAArch64TargetArgs(const ArgList &Args,
   }
 }
 
+static void addVSDefines(const ToolChain &TC, const ArgList &Args,
+                         ArgStringList &CmdArgs) {
+
+  unsigned ver = 0;
+  const VersionTuple vt = TC.computeMSVCVersion(nullptr, Args);
+  ver = vt.getMajor() * 10000000 + vt.getMinor().value_or(0) * 100000 +
+        vt.getSubminor().value_or(0);
+  CmdArgs.push_back(Args.MakeArgString("-D_MSC_VER=" + Twine(ver / 100000)));
+  CmdArgs.push_back(Args.MakeArgString("-D_MSC_FULL_VER=" + Twine(ver)));
+
+  llvm::Triple triple = TC.getTriple();
+  if (triple.isAArch64()) {
+    CmdArgs.push_back("-D_M_ARM64=1");
+  } else if (triple.isX86() && triple.isArch32Bit()) {
+    CmdArgs.push_back("-D_M_IX86=600");
+  } else if (triple.isX86() && triple.isArch64Bit()) {
+    CmdArgs.push_back("-D_M_X64=100");
+  } else {
+    llvm_unreachable(
+        "Flang on Windows only supports X86_32, X86_64 and AArch64");
+  }
+}
+
 static void processVSRuntimeLibrary(const ToolChain &TC, const ArgList &Args,
                                     ArgStringList &CmdArgs) {
   assert(TC.getTriple().isKnownWindowsMSVCEnvironment() &&
@@ -322,6 +345,7 @@ void Flang::addTargetOptions(const ArgList &Args,
 
   if (Triple.isKnownWindowsMSVCEnvironment()) {
     processVSRuntimeLibrary(TC, Args, CmdArgs);
+    addVSDefines(TC, Args, CmdArgs);
   }
 
   // TODO: Add target specific flags, ABI, mtune option etc.
diff --git a/flang/test/Driver/msvc-defines.f90 b/flang/test/Driver/msvc-defines.f90
new file mode 100644
index 000000000000000..97a6ce888d7f579
--- /dev/null
+++ b/flang/test/Driver/msvc-defines.f90
@@ -0,0 +1,10 @@
+! RUN: %flang -### --target=aarch64-windows-msvc %S/Inputs/hello.f90 -v 2>&1 | FileCheck %s --check-prefixes=MSVC,MSVC-AARCH64
+! RUN: %flang -### --target=i386-windows-msvc %S/Inputs/hello.f90 -v 2>&1 | FileCheck %s --check-prefixes=MSVC,MSVC-X86_32
+! RUN: %flang -### --target=x86_64-windows-msvc %S/Inputs/hello.f90 -v 2>&1 | FileCheck %s --check-prefixes=MSVC,MSVC-X86_64
+
+! MSVC: -fc1
+! MSVC-SAME: -D_MSC_VER={{[0-9]*}}
+! MSVC-SAME: -D_MSC_FULL_VER={{[0-9]*}}
+! MSVC-AARCH64-SAME: -D_M_ARM64=1
+! MSVC-X86_32-SAME: -D_M_IX86=600
+! MSVC-X86_64-SAME: -D_M_X64=100

>From 50833e29971704db234fc8a88ba4032517b4cfdb Mon Sep 17 00:00:00 2001
From: David Truby <david at truby.dev>
Date: Thu, 30 Nov 2023 16:24:58 +0000
Subject: [PATCH 2/2] Changes for review

---
 clang/lib/Driver/ToolChains/Flang.cpp |  1 +
 flang/test/Driver/msvc-defines.f90    | 20 ++++++++++----------
 2 files changed, 11 insertions(+), 10 deletions(-)

diff --git a/clang/lib/Driver/ToolChains/Flang.cpp b/clang/lib/Driver/ToolChains/Flang.cpp
index 87aa546e5549423..1b199704a372083 100644
--- a/clang/lib/Driver/ToolChains/Flang.cpp
+++ b/clang/lib/Driver/ToolChains/Flang.cpp
@@ -213,6 +213,7 @@ static void addVSDefines(const ToolChain &TC, const ArgList &Args,
         vt.getSubminor().value_or(0);
   CmdArgs.push_back(Args.MakeArgString("-D_MSC_VER=" + Twine(ver / 100000)));
   CmdArgs.push_back(Args.MakeArgString("-D_MSC_FULL_VER=" + Twine(ver)));
+  CmdArgs.push_back(Args.MakeArgString("-D_WIN32"));
 
   llvm::Triple triple = TC.getTriple();
   if (triple.isAArch64()) {
diff --git a/flang/test/Driver/msvc-defines.f90 b/flang/test/Driver/msvc-defines.f90
index 97a6ce888d7f579..a05df666ac09a6a 100644
--- a/flang/test/Driver/msvc-defines.f90
+++ b/flang/test/Driver/msvc-defines.f90
@@ -1,10 +1,10 @@
-! RUN: %flang -### --target=aarch64-windows-msvc %S/Inputs/hello.f90 -v 2>&1 | FileCheck %s --check-prefixes=MSVC,MSVC-AARCH64
-! RUN: %flang -### --target=i386-windows-msvc %S/Inputs/hello.f90 -v 2>&1 | FileCheck %s --check-prefixes=MSVC,MSVC-X86_32
-! RUN: %flang -### --target=x86_64-windows-msvc %S/Inputs/hello.f90 -v 2>&1 | FileCheck %s --check-prefixes=MSVC,MSVC-X86_64
-
-! MSVC: -fc1
-! MSVC-SAME: -D_MSC_VER={{[0-9]*}}
-! MSVC-SAME: -D_MSC_FULL_VER={{[0-9]*}}
-! MSVC-AARCH64-SAME: -D_M_ARM64=1
-! MSVC-X86_32-SAME: -D_M_IX86=600
-! MSVC-X86_64-SAME: -D_M_X64=100
+! RUN: %flang -### --target=aarch64-windows-msvc %S/Inputs/hello.f90 -v 2>&1 | FileCheck %s --check-prefixes=MSVC,MSVC-AARCH64
+! RUN: %flang -### --target=i386-windows-msvc %S/Inputs/hello.f90 -v 2>&1 | FileCheck %s --check-prefixes=MSVC,MSVC-X86_32
+! RUN: %flang -### --target=x86_64-windows-msvc %S/Inputs/hello.f90 -v 2>&1 | FileCheck %s --check-prefixes=MSVC,MSVC-X86_64
+
+! MSVC: -fc1
+! MSVC-SAME: -D_MSC_VER={{[0-9]*}}
+! MSVC-SAME: -D_MSC_FULL_VER={{[0-9]*}}
+! MSVC-AARCH64-SAME: -D_M_ARM64=1
+! MSVC-X86_32-SAME: -D_M_IX86=600
+! MSVC-X86_64-SAME: -D_M_X64=100



More information about the cfe-commits mailing list