From 7629a9849d9fcdc7806a5ad7434d8cf2c67bbfc0 Mon Sep 17 00:00:00 2001 From: Aaron Siddhartha Mondal Date: Thu, 20 Apr 2023 20:18:05 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=98=8D=20Add=20module=20std?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It's finally here. Dayyyyyuuuummm I'm excited~ --- MODULE.bazel | 5 +- examples/BUILD.bazel | 1 + examples/std_module_example/BUILD.bazel | 8 ++ examples/std_module_example/main.cpp | 6 ++ ll/args.bzl | 7 ++ ll/inputs.bzl | 11 ++- ll/toolchain.bzl | 6 ++ llvm-project-overlay/libcxx/BUILD.bazel | 122 ++++++++++++++++++++++- patches/rules_ll_overlay_patch.diff | 126 +++++++++++++++++++++++- patches/std_modules_tuple_fix.diff | 17 ++++ 10 files changed, 301 insertions(+), 8 deletions(-) create mode 100644 examples/std_module_example/BUILD.bazel create mode 100644 examples/std_module_example/main.cpp create mode 100644 patches/std_modules_tuple_fix.diff diff --git a/MODULE.bazel b/MODULE.bazel index b183fccd..eb089017 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -30,8 +30,8 @@ llvm_project_overlay = use_extension( "llvm_project_overlay", ) llvm_project_overlay.configure( - commit = "de45ab3c92d530c00d9c7ce20a85e01da4d37303", - sha256 = "be51ca84dd5888ab0b160e22e65bc6ee43045f8974df03092a8ea48a9cd7fe6d", + commit = "4ef1393e1b35049f0d35b0b74dbffeaa104288f0", + sha256 = "756af75d3a3b02033e8979d19b9a94c9bda55e3178fa202aea823f2caf6112b0", targets = ["AMDGPU", "NVPTX", "X86", "WebAssembly"], patches = [ "@rules_ll//patches:back_inserter_patch.diff", @@ -39,6 +39,7 @@ llvm_project_overlay.configure( "@rules_ll//patches:mallinfo2_patch.diff", "@rules_ll//patches:rules_ll_overlay_patch.diff", "@rules_ll//patches:clang_new_offload_driver.diff", + "@rules_ll//patches:std_modules_tuple_fix.diff", ], ) diff --git a/examples/BUILD.bazel b/examples/BUILD.bazel index 263f0641..7230147a 100644 --- a/examples/BUILD.bazel +++ b/examples/BUILD.bazel @@ -51,6 +51,7 @@ EXAMPLES = [ # "sanitizers", # "clang_tidy_example", "shared_library_example", + "std_module_example", "modules_draft_example", ] diff --git a/examples/std_module_example/BUILD.bazel b/examples/std_module_example/BUILD.bazel new file mode 100644 index 00000000..c7ec956a --- /dev/null +++ b/examples/std_module_example/BUILD.bazel @@ -0,0 +1,8 @@ +load("@rules_ll//ll:defs.bzl", "ll_binary") + +ll_binary( + name = "std_module_example", + srcs = ["main.cpp"], + compile_flags = ["-std=c++2b"], + visibility = ["@//:__pkg__"], +) diff --git a/examples/std_module_example/main.cpp b/examples/std_module_example/main.cpp new file mode 100644 index 00000000..a192e6ac --- /dev/null +++ b/examples/std_module_example/main.cpp @@ -0,0 +1,6 @@ +import std; + +auto main() -> int { + std::cout << "Hello, World!" << std::endl; + return 0; +} diff --git a/ll/args.bzl b/ll/args.bzl index dc345a91..b8779099 100644 --- a/ll/args.bzl +++ b/ll/args.bzl @@ -365,6 +365,13 @@ def compile_object_args( omit_if_empty = True, ) + if ctx.attr.compilation_mode != "bootstrap": + args.add_all( + toolchain.cpp_stdmodules, + map_each = _create_module_import, + format_each = "-fmodule-file=%s", + ) + # Input file. args.add(in_file) diff --git a/ll/inputs.bzl b/ll/inputs.bzl index bf42de77..95e9e6b1 100644 --- a/ll/inputs.bzl +++ b/ll/inputs.bzl @@ -57,7 +57,10 @@ def compile_object_inputs( toolchain.omp_header + toolchain.rocm_device_libs + toolchain.unwind_library - ), + ) + [ + module.bmi + for module in toolchain.cpp_stdmodules + ], transitive = [ headers, depset([interface.bmi for interface in interfaces.to_list()]), @@ -104,7 +107,11 @@ def link_executable_inputs(ctx, in_files): toolchain.unwind_library + ( toolchain.llvm_project_artifacts if ctx.attr.depends_on_llvm else [] - ), + ) + + [ + module.bmi + for module in toolchain.cpp_stdmodules + ], ) def link_shared_object_inputs(ctx, in_files): diff --git a/ll/toolchain.bzl b/ll/toolchain.bzl index 2bbbc1aa..a789d8dd 100644 --- a/ll/toolchain.bzl +++ b/ll/toolchain.bzl @@ -4,6 +4,7 @@ This file declares the `ll_toolchain` rule. """ load("//ll:attributes.bzl", "LL_TOOLCHAIN_ATTRS") +load("//ll:providers.bzl", "LlInfo") def _ll_toolchain_impl(ctx): # We always need to invoke lld via an ld.lld -> lld symlink. @@ -20,6 +21,10 @@ def _ll_toolchain_impl(ctx): ]) llvm_project_artifacts = ctx.files.llvm_project_deps + std_modules = [] + for target in ctx.attr.cpp_stdlib: + std_modules += target[LlInfo].exposed_bmis.to_list() + return [ platform_common.ToolchainInfo( c_driver = ctx.executable.c_driver, @@ -42,6 +47,7 @@ def _ll_toolchain_impl(ctx): builtin_includes = ctx.files.builtin_includes, cpp_stdlib = ctx.files.cpp_stdlib, cpp_stdhdrs = ctx.files.cpp_stdhdrs, + cpp_stdmodules = std_modules, cpp_abilib = ctx.files.cpp_abilib, cpp_abihdrs = ctx.files.cpp_abihdrs, compiler_runtime = ctx.files.compiler_runtime, diff --git a/llvm-project-overlay/libcxx/BUILD.bazel b/llvm-project-overlay/libcxx/BUILD.bazel index 51566253..9b72daf5 100644 --- a/llvm-project-overlay/libcxx/BUILD.bazel +++ b/llvm-project-overlay/libcxx/BUILD.bazel @@ -1,6 +1,116 @@ load("@rules_ll//ll:defs.bzl", "ll_library") load("@bazel_skylib//rules:expand_template.bzl", "expand_template") +STD_PARTITIONS = [ + "algorithm", + "any", + "array", + "atomic", + "barrier", + "bit", + "bitset", + "cassert", + "cctype", + "cerrno", + "cfenv", + "cfloat", + "charconv", + "chrono", + "cinttypes", + "climits", + "clocale", + "cmath", + "codecvt", + "compare", + "complex", + "concepts", + "condition_variable", + "coroutine", + "csetjmp", + "csignal", + "cstdarg", + "cstddef", + "cstdio", + "cstdlib", + "cstdint", + "cstring", + "ctime", + "cuchar", + "cwchar", + "cwctype", + "deque", + "exception", + "execution", + "expected", + "filesystem", + "flat_map", + "flat_set", + "format", + "forward_list", + "fstream", + "functional", + "future", + "generator", + "initializer_list", + "iomanip", + "ios", + "iosfwd", + "iostream", + "istream", + "iterator", + "latch", + "limits", + "list", + "locale", + "map", + "mdspan", + "memory", + "memory_resource", + "mutex", + # "new", # Needs special treatment. Module is called __new. + "numbers", + "numeric", + "optional", + "ostream", + "print", + "queue", + "random", + "ranges", + "ratio", + "regex", + "scoped_allocator", + "semaphore", + "set", + "shared_mutex", + "source_location", + "span", + "spanstream", + "sstream", + "stack", + "stacktrace", + "stdexcept", + "stdfloat", + "stop_token", + "streambuf", + "string", + "string_view", + "strstream", + "syncstream", + "system_error", + "thread", + "tuple", + "typeindex", + "typeinfo", + "type_traits", # Manually had to include . + "unordered_map", + "unordered_set", + "utility", + "valarray", + "variant", + "vector", + "version", +] + expand_template( name = "module_modulemap_gen", out = "include/module.modulemap", @@ -151,13 +261,14 @@ ll_library( ], compilation_mode = "bootstrap", compile_flags = [ - "-std=c++20", + "-std=c++2b", "-faligned-allocation", "-fno-omit-frame-pointer", "-funwind-tables", "-fstrict-aliasing", "-fvisibility-inlines-hidden", "-Wno-user-defined-literals", + "-Wno-reserved-module-identifier", ], defines = [ "_LIBCPP_BUILDING_LIBRARY", @@ -177,9 +288,18 @@ ll_library( exposed_hdrs = [ "//libcxx:headers", ], + exposed_interfaces = { + "modules/std.cppm": "std", + }, includes = [ "libcxx/src", ], + interfaces = { + "modules/std/{}.cppm".format(name): "std:{}".format(name) + for name in STD_PARTITIONS + } | { + "modules/std/new.cppm": "std:__new", + }, visibility = ["//visibility:public"], deps = ["//libcxxabi"], ) diff --git a/patches/rules_ll_overlay_patch.diff b/patches/rules_ll_overlay_patch.diff index 3c07643e..c820097a 100644 --- a/patches/rules_ll_overlay_patch.diff +++ b/patches/rules_ll_overlay_patch.diff @@ -1272,13 +1272,123 @@ index 000000000000..8ad56ad46df6 +) diff --git a/utils/bazel/llvm-project-overlay/libcxx/BUILD.bazel b/utils/bazel/llvm-project-overlay/libcxx/BUILD.bazel new file mode 100644 -index 000000000000..515662537ebf +index 000000000000..9b72daf50b6e --- /dev/null +++ b/utils/bazel/llvm-project-overlay/libcxx/BUILD.bazel -@@ -0,0 +1,185 @@ +@@ -0,0 +1,305 @@ +load("@rules_ll//ll:defs.bzl", "ll_library") +load("@bazel_skylib//rules:expand_template.bzl", "expand_template") + ++STD_PARTITIONS = [ ++ "algorithm", ++ "any", ++ "array", ++ "atomic", ++ "barrier", ++ "bit", ++ "bitset", ++ "cassert", ++ "cctype", ++ "cerrno", ++ "cfenv", ++ "cfloat", ++ "charconv", ++ "chrono", ++ "cinttypes", ++ "climits", ++ "clocale", ++ "cmath", ++ "codecvt", ++ "compare", ++ "complex", ++ "concepts", ++ "condition_variable", ++ "coroutine", ++ "csetjmp", ++ "csignal", ++ "cstdarg", ++ "cstddef", ++ "cstdio", ++ "cstdlib", ++ "cstdint", ++ "cstring", ++ "ctime", ++ "cuchar", ++ "cwchar", ++ "cwctype", ++ "deque", ++ "exception", ++ "execution", ++ "expected", ++ "filesystem", ++ "flat_map", ++ "flat_set", ++ "format", ++ "forward_list", ++ "fstream", ++ "functional", ++ "future", ++ "generator", ++ "initializer_list", ++ "iomanip", ++ "ios", ++ "iosfwd", ++ "iostream", ++ "istream", ++ "iterator", ++ "latch", ++ "limits", ++ "list", ++ "locale", ++ "map", ++ "mdspan", ++ "memory", ++ "memory_resource", ++ "mutex", ++ # "new", # Needs special treatment. Module is called __new. ++ "numbers", ++ "numeric", ++ "optional", ++ "ostream", ++ "print", ++ "queue", ++ "random", ++ "ranges", ++ "ratio", ++ "regex", ++ "scoped_allocator", ++ "semaphore", ++ "set", ++ "shared_mutex", ++ "source_location", ++ "span", ++ "spanstream", ++ "sstream", ++ "stack", ++ "stacktrace", ++ "stdexcept", ++ "stdfloat", ++ "stop_token", ++ "streambuf", ++ "string", ++ "string_view", ++ "strstream", ++ "syncstream", ++ "system_error", ++ "thread", ++ "tuple", ++ "typeindex", ++ "typeinfo", ++ "type_traits", # Manually had to include . ++ "unordered_map", ++ "unordered_set", ++ "utility", ++ "valarray", ++ "variant", ++ "vector", ++ "version", ++] ++ +expand_template( + name = "module_modulemap_gen", + out = "include/module.modulemap", @@ -1429,13 +1539,14 @@ index 000000000000..515662537ebf + ], + compilation_mode = "bootstrap", + compile_flags = [ -+ "-std=c++20", ++ "-std=c++2b", + "-faligned-allocation", + "-fno-omit-frame-pointer", + "-funwind-tables", + "-fstrict-aliasing", + "-fvisibility-inlines-hidden", + "-Wno-user-defined-literals", ++ "-Wno-reserved-module-identifier", + ], + defines = [ + "_LIBCPP_BUILDING_LIBRARY", @@ -1455,9 +1566,18 @@ index 000000000000..515662537ebf + exposed_hdrs = [ + "//libcxx:headers", + ], ++ exposed_interfaces = { ++ "modules/std.cppm": "std", ++ }, + includes = [ + "libcxx/src", + ], ++ interfaces = { ++ "modules/std/{}.cppm".format(name): "std:{}".format(name) ++ for name in STD_PARTITIONS ++ } | { ++ "modules/std/new.cppm": "std:__new", ++ }, + visibility = ["//visibility:public"], + deps = ["//libcxxabi"], +) diff --git a/patches/std_modules_tuple_fix.diff b/patches/std_modules_tuple_fix.diff new file mode 100644 index 00000000..f8c3b1a4 --- /dev/null +++ b/patches/std_modules_tuple_fix.diff @@ -0,0 +1,17 @@ +diff --git a/libcxx/include/tuple b/libcxx/include/tuple +--- a/libcxx/include/tuple ++++ b/libcxx/include/tuple +@@ -1508,9 +1508,10 @@ + const __ignore_t& operator=(_Tp&&) const {return *this;} + }; + +-namespace { +- constexpr __ignore_t ignore = __ignore_t(); +-} // namespace ++// SEPARATE REVIEW NEEDS TO USE NEW INLINE CONSTEXPR MACRO ++//namespace { ++ inline constexpr __ignore_t ignore = __ignore_t(); ++//} // namespace + + template + inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14