[clang] [clang][driver] Support `--precompile` and `-fmodule-*` options in Clang-CL (PR #98761)
Sharadh Rajaraman via cfe-commits
cfe-commits at lists.llvm.org
Wed Jul 17 02:58:20 PDT 2024
https://github.com/sharadhr updated https://github.com/llvm/llvm-project/pull/98761
>From 1fed92a00f0d732a2575861c2bf6a6d053407255 Mon Sep 17 00:00:00 2001
From: Sharadh Rajaraman <r.sharadh at outlook.sg>
Date: Sat, 13 Jul 2024 19:25:47 +0100
Subject: [PATCH 1/7] Allow `--precompile` and `-fprebuilt-module-path` to be
passed directly into CL driver without `/clang:` prefix
---
clang/include/clang/Driver/Options.td | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
index 4ab8638175dd3..ca7cfef8453a0 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -3085,7 +3085,7 @@ def fmodules_user_build_path : Separate<["-"], "fmodules-user-build-path">, Grou
HelpText<"Specify the module user build path">,
MarshallingInfoString<HeaderSearchOpts<"ModuleUserBuildPath">>;
def fprebuilt_module_path : Joined<["-"], "fprebuilt-module-path=">, Group<i_Group>,
- Flags<[]>, Visibility<[ClangOption, CC1Option]>,
+ Flags<[]>, Visibility<[ClangOption, CLOption, CC1Option]>,
MetaVarName<"<directory>">,
HelpText<"Specify the prebuilt module path">;
defm prebuilt_implicit_modules : BoolFOption<"prebuilt-implicit-modules",
@@ -5874,6 +5874,7 @@ def _output : Separate<["--"], "output">, Alias<o>;
def _param : Separate<["--"], "param">, Group<CompileOnly_Group>;
def _param_EQ : Joined<["--"], "param=">, Alias<_param>;
def _precompile : Flag<["--"], "precompile">, Flags<[NoXarchOption]>,
+ Visibility<[ClangOption, CLOption]>,
Group<Action_Group>, HelpText<"Only precompile the input">;
def _prefix_EQ : Joined<["--"], "prefix=">, Alias<B>;
def _prefix : Separate<["--"], "prefix">, Alias<B>;
>From 182cff89e777b6f22d34fb074c79814e521a7a88 Mon Sep 17 00:00:00 2001
From: Sharadh Rajaraman <r.sharadh at outlook.sg>
Date: Sun, 14 Jul 2024 11:05:57 +0100
Subject: [PATCH 2/7] `TY_ModuleFile` should be a 'CXX' file type
---
clang/lib/Driver/Types.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/lib/Driver/Types.cpp b/clang/lib/Driver/Types.cpp
index a7b6b9000e1d2..c6a03f4491dd7 100644
--- a/clang/lib/Driver/Types.cpp
+++ b/clang/lib/Driver/Types.cpp
@@ -242,7 +242,7 @@ bool types::isCXX(ID Id) {
case TY_CXXHUHeader:
case TY_PP_CXXHeaderUnit:
case TY_ObjCXXHeader: case TY_PP_ObjCXXHeader:
- case TY_CXXModule: case TY_PP_CXXModule:
+ case TY_CXXModule: case TY_PP_CXXModule: case TY_ModuleFile:
case TY_PP_CLCXX:
case TY_CUDA: case TY_PP_CUDA: case TY_CUDA_DEVICE:
case TY_HIP:
>From 8daa27a9d45b85a55f180e5f84ae9b7fa8c11572 Mon Sep 17 00:00:00 2001
From: Sharadh Rajaraman <r.sharadh at outlook.sg>
Date: Sun, 14 Jul 2024 16:28:46 +0100
Subject: [PATCH 3/7] Support more `fmodule-*` options from CL driver
---
clang/include/clang/Driver/Options.td | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
index ca7cfef8453a0..bfe369a6284fe 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -3094,11 +3094,11 @@ defm prebuilt_implicit_modules : BoolFOption<"prebuilt-implicit-modules",
NegFlag<SetFalse>, BothFlags<[], [ClangOption, CC1Option]>>;
def fmodule_output_EQ : Joined<["-"], "fmodule-output=">,
- Flags<[NoXarchOption]>, Visibility<[ClangOption, CC1Option]>,
+ Flags<[NoXarchOption]>, Visibility<[ClangOption, CLOption, CC1Option]>,
MarshallingInfoString<FrontendOpts<"ModuleOutputPath">>,
HelpText<"Save intermediate module file results when compiling a standard C++ module unit.">;
def fmodule_output : Flag<["-"], "fmodule-output">, Flags<[NoXarchOption]>,
- Visibility<[ClangOption, CC1Option]>,
+ Visibility<[ClangOption, CLOption, CC1Option]>,
HelpText<"Save intermediate module file results when compiling a standard C++ module unit.">;
defm skip_odr_check_in_gmf : BoolOption<"f", "skip-odr-check-in-gmf",
@@ -3278,8 +3278,10 @@ def fretain_comments_from_system_headers : Flag<["-"], "fretain-comments-from-sy
Visibility<[ClangOption, CC1Option]>,
MarshallingInfoFlag<LangOpts<"RetainCommentsFromSystemHeaders">>;
def fmodule_header : Flag <["-"], "fmodule-header">, Group<f_Group>,
+ Visibility<[ClangOption, CLOption]>,
HelpText<"Build a C++20 Header Unit from a header">;
def fmodule_header_EQ : Joined<["-"], "fmodule-header=">, Group<f_Group>,
+ Visibility<[ClangOption, CLOption]>,
MetaVarName<"<kind>">,
HelpText<"Build a C++20 Header Unit from a header that should be found in the user (fmodule-header=user) or system (fmodule-header=system) search path.">;
>From 865a8e57af0ede6f6d6ec60ebd837f6827019aec Mon Sep 17 00:00:00 2001
From: Sharadh Rajaraman <3754080+sharadhr at users.noreply.github.com>
Date: Tue, 16 Jul 2024 20:10:12 +0100
Subject: [PATCH 4/7] Resolve `clang-format` issues
---
clang/lib/Driver/Types.cpp | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/clang/lib/Driver/Types.cpp b/clang/lib/Driver/Types.cpp
index c6a03f4491dd7..842eb59b7382d 100644
--- a/clang/lib/Driver/Types.cpp
+++ b/clang/lib/Driver/Types.cpp
@@ -242,7 +242,9 @@ bool types::isCXX(ID Id) {
case TY_CXXHUHeader:
case TY_PP_CXXHeaderUnit:
case TY_ObjCXXHeader: case TY_PP_ObjCXXHeader:
- case TY_CXXModule: case TY_PP_CXXModule: case TY_ModuleFile:
+ case TY_CXXModule:
+ case TY_PP_CXXModule:
+ case TY_ModuleFile:
case TY_PP_CLCXX:
case TY_CUDA: case TY_PP_CUDA: case TY_CUDA_DEVICE:
case TY_HIP:
>From 8f9c90b9dac6cd5ba96ab2a8c32635c70b32309b Mon Sep 17 00:00:00 2001
From: Sharadh Rajaraman <r.sharadh at petagene.com>
Date: Tue, 16 Jul 2024 23:49:08 +0100
Subject: [PATCH 5/7] `clang-cl` C++20 modules compile test
---
clang/test/Driver/cl-cxx20-modules.cppm | 65 +++++++++++++++++++++++++
1 file changed, 65 insertions(+)
create mode 100644 clang/test/Driver/cl-cxx20-modules.cppm
diff --git a/clang/test/Driver/cl-cxx20-modules.cppm b/clang/test/Driver/cl-cxx20-modules.cppm
new file mode 100644
index 0000000000000..84807324b4cbc
--- /dev/null
+++ b/clang/test/Driver/cl-cxx20-modules.cppm
@@ -0,0 +1,65 @@
+// REQUIRES: system-windows
+
+// RUN: rm -rf %t
+// RUN: mkdir -p %t
+// RUN: split-file %s %t
+
+// RUN: %clang_cl /std:c++20 --precompile "%/t/Hello.cppm" "/Fo%/t/Hello.pcm"
+// RUN: %clang_cl /std:c++20 %/t/use.cpp -fmodule-file=Hello=%/t/Hello.pcm %/t/Hello.pcm /Fo%/t/SimpleHelloWorld.exe 2>&1
+
+// RUN: %/t/SimpleHelloWorld.exe
+// CHECK: Simple Hello World!
+
+//--- Hello.cppm
+module;
+#include <iostream>
+export module Hello;
+export void hello() {
+ std::cout << "Simple Hello World!\n";
+}
+
+//--- use.cpp
+import Hello;
+int main() {
+ hello();
+ return 0;
+}
+
+//--- M.cppm
+export module M;
+export import :interface_part;
+import :impl_part;
+export void Hello();
+
+//--- interface_part.cpp
+export module M:interface_part;
+export void World();
+
+//--- impl_part.cppm
+module;
+#include <iostream>
+#include <string>
+module M:impl_part;
+import :interface_part;
+
+std::string W = "World!";
+void World() {
+ std::cout << W << std::endl;
+}
+
+//--- Impl.cpp
+module;
+#include <iostream>
+module M;
+void Hello() {
+ std::cout << "Complex Hello ";
+}
+
+//--- User.cpp
+import M;
+int main() {
+ Hello();
+ World();
+ return 0;
+}
+
>From 71b8d723ee69bc6ec9bfdaea81657e0a46ab00d5 Mon Sep 17 00:00:00 2001
From: Sharadh Rajaraman <r.sharadh at petagene.com>
Date: Wed, 17 Jul 2024 00:08:29 +0100
Subject: [PATCH 6/7] Document the new options
---
clang/docs/StandardCPlusPlusModules.rst | 17 ++++++++++++-----
clang/docs/UsersManual.rst | 6 ++++++
2 files changed, 18 insertions(+), 5 deletions(-)
diff --git a/clang/docs/StandardCPlusPlusModules.rst b/clang/docs/StandardCPlusPlusModules.rst
index 1c3c4d319c0e1..cfa8a027c9d26 100644
--- a/clang/docs/StandardCPlusPlusModules.rst
+++ b/clang/docs/StandardCPlusPlusModules.rst
@@ -398,6 +398,13 @@ BMIs cannot be shipped in an archive to create a module library. Instead, the
BMIs(``*.pcm``) are compiled into object files(``*.o``) and those object files
are added to the archive instead.
+clang-cl
+~~~~~~~~
+
+``clang-cl`` supports the same options as ``clang++`` for modules as detailed above;
+there is no need to prefix these options with ``/clang:``. Note that ``cl.exe``
+options to emit `IFC files <https://devblogs.microsoft.com/cppblog/using-cpp-modules-in-msvc-from-the-command-line-part-1/>` are *not* supported. The precompiled modules are also not compatible for use with ``cl.exe``.
+
Consistency Requirements
~~~~~~~~~~~~~~~~~~~~~~~~
@@ -1176,12 +1183,12 @@ have ``.cppm`` (or ``.ccm``, ``.cxxm``, ``.c++m``) as the file extension.
However, the behavior is inconsistent with other compilers. This is tracked by
`#57416 <https://github.com/llvm/llvm-project/issues/57416>`_.
-clang-cl is not compatible with standard C++ modules
-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+.. clang-cl is not compatible with standard C++ modules
+.. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-``/clang:-fmodule-file`` and ``/clang:-fprebuilt-module-path`` cannot be used
-to specify the BMI with ``clang-cl.exe``. This is tracked by
-`#64118 <https://github.com/llvm/llvm-project/issues/64118>`_.
+.. ``/clang:-fmodule-file`` and ``/clang:-fprebuilt-module-path`` cannot be used
+.. to specify the BMI with ``clang-cl.exe``. This is tracked by
+.. `#64118 <https://github.com/llvm/llvm-project/issues/64118>`_.
Incorrect ODR violation diagnostics
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
diff --git a/clang/docs/UsersManual.rst b/clang/docs/UsersManual.rst
index 8e01ea15064ba..51bc871ebbe13 100644
--- a/clang/docs/UsersManual.rst
+++ b/clang/docs/UsersManual.rst
@@ -4732,6 +4732,12 @@ Execute ``clang-cl /?`` to see a list of supported options:
-flto=<value> Set LTO mode to either 'full' or 'thin'
-flto Enable LTO in 'full' mode
-fmerge-all-constants Allow merging of constants
+ -fmodule-file=<module_name>=<module-file>
+ Use the specified module file that provides the module <module_name>
+ -fmodule-header=<header>
+ Build <header> as a C++20 header unit
+ -fmodule-output=<path>
+ Save intermediate module file results when compiling a standard C++ module unit.
-fms-compatibility-version=<value>
Dot-separated value representing the Microsoft compiler version
number to report in _MSC_VER (0 = don't define it; default is same value as installed cl.exe, or 1933)
>From 2d4067f1ac9e268b9d63f25e25a1a16c7d4a3234 Mon Sep 17 00:00:00 2001
From: Sharadh Rajaraman <r.sharadh at outlook.sg>
Date: Wed, 17 Jul 2024 10:58:02 +0100
Subject: [PATCH 7/7] Remove commented-out docs - Also add a note for build
system authors to use `clang++` options
---
clang/docs/StandardCPlusPlusModules.rst | 12 ++++--------
1 file changed, 4 insertions(+), 8 deletions(-)
diff --git a/clang/docs/StandardCPlusPlusModules.rst b/clang/docs/StandardCPlusPlusModules.rst
index cfa8a027c9d26..2e5af67e53cb7 100644
--- a/clang/docs/StandardCPlusPlusModules.rst
+++ b/clang/docs/StandardCPlusPlusModules.rst
@@ -403,7 +403,10 @@ clang-cl
``clang-cl`` supports the same options as ``clang++`` for modules as detailed above;
there is no need to prefix these options with ``/clang:``. Note that ``cl.exe``
-options to emit `IFC files <https://devblogs.microsoft.com/cppblog/using-cpp-modules-in-msvc-from-the-command-line-part-1/>` are *not* supported. The precompiled modules are also not compatible for use with ``cl.exe``.
+`options to emit/consume IFC files <https://devblogs.microsoft.com/cppblog/using-cpp-modules-in-msvc-from-the-command-line-part-1/>` are *not* supported.
+The resultant precompiled modules are also not compatible for use with ``cl.exe``.
+
+We recommend that build system authors use the above-mentioned ``clang++`` options with ``clang-cl`` to build modules.
Consistency Requirements
~~~~~~~~~~~~~~~~~~~~~~~~
@@ -1183,13 +1186,6 @@ have ``.cppm`` (or ``.ccm``, ``.cxxm``, ``.c++m``) as the file extension.
However, the behavior is inconsistent with other compilers. This is tracked by
`#57416 <https://github.com/llvm/llvm-project/issues/57416>`_.
-.. clang-cl is not compatible with standard C++ modules
-.. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-
-.. ``/clang:-fmodule-file`` and ``/clang:-fprebuilt-module-path`` cannot be used
-.. to specify the BMI with ``clang-cl.exe``. This is tracked by
-.. `#64118 <https://github.com/llvm/llvm-project/issues/64118>`_.
-
Incorrect ODR violation diagnostics
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
More information about the cfe-commits
mailing list