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

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

Merged
merged 2 commits into from Dec 4, 2023

Conversation

DavidTruby
Copy link
Member

@DavidTruby DavidTruby commented Nov 23, 2023

Fixes #67675

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' flang:driver flang Flang issues not falling into any other category labels Nov 23, 2023
@llvmbot
Copy link
Collaborator

llvmbot commented Nov 23, 2023

@llvm/pr-subscribers-clang-driver

@llvm/pr-subscribers-clang

Author: David Truby (DavidTruby)

Changes

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

2 Files Affected:

  • (modified) clang/lib/Driver/ToolChains/Flang.cpp (+24)
  • (added) flang/test/Driver/msvc-defines.f90 (+10)
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

@llvmbot
Copy link
Collaborator

llvmbot commented Nov 23, 2023

@llvm/pr-subscribers-flang-driver

Author: David Truby (DavidTruby)

Changes

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

2 Files Affected:

  • (modified) clang/lib/Driver/ToolChains/Flang.cpp (+24)
  • (added) flang/test/Driver/msvc-defines.f90 (+10)
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

@DavidTruby
Copy link
Member Author

@pbo-linaro @bradking I think this fixes the linked issue, could someone confirm that for me? Thanks!

@pbo-linaro
Copy link
Contributor

I'll let @bradking check this, as he implemented this.

Comment on lines +220 to +227
} 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");
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Flang doesn't support 32bit

message(FATAL_ERROR "flang isn't supported on 32 bit CPUs")

Copy link
Member Author

Choose a reason for hiding this comment

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

We don't support 32 bit hosts but I think we do support 32 bit targets

Copy link
Contributor

Choose a reason for hiding this comment

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

This file has CRLF newlines. Is that expected?

Copy link
Member Author

Choose a reason for hiding this comment

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

It wasn't, I think I have replaced this

@@ -204,6 +204,29 @@ void Flang::AddAArch64TargetArgs(const ArgList &Args,
}
}

static void addVSDefines(const ToolChain &TC, const ArgList &Args,
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
static void addVSDefines(const ToolChain &TC, const ArgList &Args,
static void addMSVCDefines(const ToolChain &TC, const ArgList &Args,

?

Copy link
Member Author

Choose a reason for hiding this comment

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

I named it this way to match processVSRuntimeLibs which I stole from clang :)


llvm::Triple triple = TC.getTriple();
if (triple.isAArch64()) {
CmdArgs.push_back("-D_M_ARM64=1");
Copy link
Contributor

Choose a reason for hiding this comment

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

Does LLVM/Flang support the _M_ARM64EC ABI?

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't believe so. It's not tested at all if it does as far as I can tell!

@@ -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.
Copy link
Contributor

Choose a reason for hiding this comment

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

Please also add -D_WIN32 to indicate that this is targeting a Windows platform.

Copy link
Contributor

Choose a reason for hiding this comment

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

OTOH this could be considered out of scope for this PR.

@bradking
Copy link
Contributor

@DavidTruby thanks. This LGTM now. I've locally updated CMake to use the preprocessor definitions in place of the workaround we had before, and it seems to work with this PR.

@DavidTruby DavidTruby merged commit dff5bb9 into llvm:main Dec 4, 2023
3 checks passed
@DavidTruby DavidTruby deleted the msvc-defines branch December 4, 2023 02:20
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 flang:driver flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[flang] Implement MSVC predefined macros for Windows
6 participants