Skip to content

Commit 35eb5cb

Browse files
Merge pull request #69707 from ian-twilightcoder/builtin-flag
[ClangImporter] Swift needs to pass `-Xclang -fbuiltin-headers-in-system-modules` for its module maps that group cstd headers
2 parents 5dfd1f1 + 94e860e commit 35eb5cb

File tree

13 files changed

+98
-17
lines changed

13 files changed

+98
-17
lines changed

include/swift/ClangImporter/ClangImporter.h

+1
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,7 @@ bool isCxxConstReferenceType(const clang::Type *type);
650650
struct ClangInvocationFileMapping {
651651
SmallVector<std::pair<std::string, std::string>, 2> redirectedFiles;
652652
SmallVector<std::pair<std::string, std::string>, 1> overridenFiles;
653+
bool requiresBuiltinHeadersInSystemModules;
653654
};
654655

655656
/// On Linux, some platform libraries (glibc, libstdc++) are not modularized.

lib/ClangImporter/ClangImporter.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -1205,6 +1205,10 @@ ClangImporter::create(ASTContext &ctx,
12051205
// Create a new Clang compiler invocation.
12061206
{
12071207
importer->Impl.ClangArgs = getClangArguments(ctx);
1208+
if (fileMapping.requiresBuiltinHeadersInSystemModules) {
1209+
importer->Impl.ClangArgs.push_back("-Xclang");
1210+
importer->Impl.ClangArgs.push_back("-fbuiltin-headers-in-system-modules");
1211+
}
12081212
ArrayRef<std::string> invocationArgStrs = importer->Impl.ClangArgs;
12091213
if (importerOpts.DumpClangDiagnostics) {
12101214
llvm::errs() << "'";

lib/ClangImporter/ClangIncludePaths.cpp

+37-7
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,8 @@ GetWindowsAuxiliaryFile(StringRef modulemap, const SearchPathOptions &Options) {
420420

421421
SmallVector<std::pair<std::string, std::string>, 2> GetWindowsFileMappings(
422422
ASTContext &Context,
423-
const llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> &driverVFS) {
423+
const llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> &driverVFS,
424+
bool &requiresBuiltinHeadersInSystemModules) {
424425
const llvm::Triple &Triple = Context.LangOpts.Target;
425426
const SearchPathOptions &SearchPathOpts = Context.SearchPathOpts;
426427
SmallVector<std::pair<std::string, std::string>, 2> Mappings;
@@ -468,8 +469,21 @@ SmallVector<std::pair<std::string, std::string>, 2> GetWindowsFileMappings(
468469
llvm::sys::path::append(UCRTInjection, "module.modulemap");
469470

470471
AuxiliaryFile = GetWindowsAuxiliaryFile("ucrt.modulemap", SearchPathOpts);
471-
if (!AuxiliaryFile.empty())
472+
if (!AuxiliaryFile.empty()) {
473+
// The ucrt module map has the C standard library headers all together.
474+
// That leads to module cycles with the clang _Builtin_ modules. e.g.
475+
// <fenv.h> on ucrt includes <float.h>. The clang builtin <float.h>
476+
// include-nexts <float.h>. When both of those UCRT headers are in the
477+
// ucrt module, there's a module cycle ucrt -> _Builtin_float -> ucrt
478+
// (i.e. fenv.h (ucrt) -> float.h (builtin) -> float.h (ucrt)). Until the
479+
// ucrt module map is updated, the builtin headers need to join the system
480+
// modules. i.e. when the builtin float.h is in the ucrt module too, the
481+
// cycle goes away. Note that -fbuiltin-headers-in-system-modules does
482+
// nothing to fix the same problem with C++ headers, and is generally
483+
// fragile.
472484
Mappings.emplace_back(std::string(UCRTInjection), AuxiliaryFile);
485+
requiresBuiltinHeadersInSystemModules = true;
486+
}
473487
}
474488

475489
struct {
@@ -515,20 +529,36 @@ ClangInvocationFileMapping swift::getClangInvocationFileMapping(
515529

516530
const llvm::Triple &triple = ctx.LangOpts.Target;
517531

532+
SmallVector<std::pair<std::string, std::string>, 2> libcFileMapping;
518533
if (triple.isOSWASI()) {
519534
// WASI Mappings
520-
result.redirectedFiles.append(
521-
getLibcFileMapping(ctx, "wasi-libc.modulemap", std::nullopt, vfs));
535+
libcFileMapping =
536+
getLibcFileMapping(ctx, "wasi-libc.modulemap", std::nullopt, vfs);
522537
} else {
523538
// Android/BSD/Linux Mappings
524-
result.redirectedFiles.append(getLibcFileMapping(
525-
ctx, "glibc.modulemap", StringRef("SwiftGlibc.h"), vfs));
539+
libcFileMapping = getLibcFileMapping(ctx, "glibc.modulemap",
540+
StringRef("SwiftGlibc.h"), vfs);
526541
}
542+
result.redirectedFiles.append(libcFileMapping);
543+
// Both libc module maps have the C standard library headers all together in a
544+
// SwiftLibc module. That leads to module cycles with the clang _Builtin_
545+
// modules. e.g. <inttypes.h> includes <stdint.h> on these platforms. The
546+
// clang builtin <stdint.h> include-nexts <stdint.h>. When both of those
547+
// platform headers are in the SwiftLibc module, there's a module cycle
548+
// SwiftLibc -> _Builtin_stdint -> SwiftLibc (i.e. inttypes.h (platform) ->
549+
// stdint.h (builtin) -> stdint.h (platform)). Until this can be fixed in
550+
// these module maps, the clang builtin headers need to join the "system"
551+
// modules (SwiftLibc). i.e. when the clang builtin stdint.h is in the
552+
// SwiftLibc module too, the cycle goes away. Note that
553+
// -fbuiltin-headers-in-system-modules does nothing to fix the same problem
554+
// with C++ headers, and is generally fragile.
555+
result.requiresBuiltinHeadersInSystemModules = !libcFileMapping.empty();
527556

528557
if (ctx.LangOpts.EnableCXXInterop)
529558
getLibStdCxxFileMapping(result, ctx, vfs);
530559

531-
result.redirectedFiles.append(GetWindowsFileMappings(ctx, vfs));
560+
result.redirectedFiles.append(GetWindowsFileMappings(
561+
ctx, vfs, result.requiresBuiltinHeadersInSystemModules));
532562

533563
return result;
534564
}

lib/DriverTool/sil_opt_main.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,11 @@ struct SILOptOptions {
193193
"interfaces for all public declarations by "
194194
"default"));
195195

196+
llvm::cl::opt<bool>
197+
StrictImplicitModuleContext = llvm::cl::opt<bool>("strict-implicit-module-context",
198+
llvm::cl::desc("Enable the strict forwarding of compilation "
199+
"context to downstream implicit module dependencies"));
200+
196201
llvm::cl::opt<bool>
197202
DisableSILOwnershipVerifier = llvm::cl::opt<bool>(
198203
"disable = llvm::cl::opt<bool> DisableSILOwnershipVerifier(-sil-ownership-verifier",
@@ -518,6 +523,10 @@ struct SILOptOptions {
518523
swift::UnavailableDeclOptimization::Complete, "complete",
519524
"Eliminate unavailable decls from lowered SIL/IR")),
520525
llvm::cl::init(swift::UnavailableDeclOptimization::None));
526+
527+
llvm::cl::list<std::string> ClangXCC = llvm::cl::list<std::string>(
528+
"Xcc",
529+
llvm::cl::desc("option to pass to clang"));
521530
};
522531

523532
/// Regular expression corresponding to the value given in one of the
@@ -608,6 +617,8 @@ int sil_opt_main(ArrayRef<const char *> argv, void *MainAddr) {
608617
Invocation.setRuntimeResourcePath(options.ResourceDir);
609618
Invocation.getFrontendOptions().EnableLibraryEvolution
610619
= options.EnableLibraryEvolution;
620+
Invocation.getFrontendOptions().StrictImplicitModuleContext
621+
= options.StrictImplicitModuleContext;
611622
// Set the module cache path. If not passed in we use the default swift module
612623
// cache.
613624
Invocation.getClangImporterOptions().ModuleCachePath = options.ModuleCachePath;
@@ -671,6 +682,12 @@ int sil_opt_main(ArrayRef<const char *> argv, void *MainAddr) {
671682
Invocation.getDiagnosticOptions().VerifyMode =
672683
options.VerifyMode ? DiagnosticOptions::Verify : DiagnosticOptions::NoVerify;
673684

685+
ClangImporterOptions &clangImporterOptions =
686+
Invocation.getClangImporterOptions();
687+
for (const auto &xcc : options.ClangXCC) {
688+
clangImporterOptions.ExtraArgs.push_back(xcc);
689+
}
690+
674691
// Setup the SIL Options.
675692
SILOptions &SILOpts = Invocation.getSILOptions();
676693
SILOpts.InlineThreshold = options.SILInlineThreshold;

stdlib/cmake/modules/AddSwiftStdlib.cmake

+2-2
Original file line numberDiff line numberDiff line change
@@ -929,7 +929,7 @@ function(add_swift_target_library_single target name)
929929
-Xcc;-Xclang;-Xcc;-ivfsoverlay;-Xcc;-Xclang;-Xcc;${SWIFTLIB_SINGLE_VFS_OVERLAY})
930930
endif()
931931
list(APPEND SWIFTLIB_SINGLE_SWIFT_COMPILE_FLAGS
932-
-vfsoverlay;"${SWIFT_WINDOWS_VFS_OVERLAY}")
932+
-vfsoverlay;"${SWIFT_WINDOWS_VFS_OVERLAY}";-strict-implicit-module-context;-Xcc;-Xclang;-Xcc;-fbuiltin-headers-in-system-modules)
933933
if(NOT CMAKE_HOST_SYSTEM MATCHES Windows)
934934
swift_windows_include_for_arch(${SWIFTLIB_SINGLE_ARCHITECTURE} SWIFTLIB_INCLUDE)
935935
foreach(directory ${SWIFTLIB_INCLUDE})
@@ -2659,7 +2659,7 @@ function(_add_swift_target_executable_single name)
26592659

26602660
if(SWIFTEXE_SINGLE_SDK STREQUAL "WINDOWS")
26612661
list(APPEND SWIFTEXE_SINGLE_COMPILE_FLAGS
2662-
-vfsoverlay;"${SWIFT_WINDOWS_VFS_OVERLAY}")
2662+
-vfsoverlay;"${SWIFT_WINDOWS_VFS_OVERLAY}";-strict-implicit-module-context;-Xcc;-Xclang;-Xcc;-fbuiltin-headers-in-system-modules)
26632663
endif()
26642664

26652665
if ("${SWIFTEXE_SINGLE_SDK}" STREQUAL "LINUX")

stdlib/public/Backtracing/SymbolicatedBacktrace.swift

+5
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ import Swift
2323

2424
@_implementationOnly import OS.Libc
2525
@_implementationOnly import Runtime
26+
// Because we've turned off the OS/SDK modules, and we don't have a module for
27+
// stddef.h, and we sometimes build with -fbuiltin-headers-in-system-modules for
28+
// vfs reasons, stddef.h can be absorbed into a random module. Sometimes it's
29+
// SwiftOverlayShims.
30+
@_implementationOnly import SwiftOverlayShims
2631

2732
/// A symbolicated backtrace
2833
public struct SymbolicatedBacktrace: CustomStringConvertible {

test/ClangImporter/pcm-emit-direct-cc1-mode.swift

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
// RUN: %empty-directory(%t)
33
// RUN: %swift-frontend -emit-pcm -direct-clang-cc1-module-build -only-use-extra-clang-opts -module-name script -o %t/script.pcm %S/Inputs/custom-modules/module.modulemap -Xcc %S/Inputs/custom-modules/module.modulemap -Xcc -o -Xcc %t/script.pcm -Xcc -fmodules -Xcc -triple -Xcc %target-triple -Xcc -x -Xcc objective-c -dump-clang-diagnostics 2> %t.diags.txt
44

5+
// Sometimes returns a 1 exit code with no stdout or stderr or even an indication of which command failed.
6+
// XFAIL: OS=linux-gnu
7+
58
// Verify some of the output of the -dump-pcm flag.
69
// RUN: %swift-dump-pcm %t/script.pcm | %FileCheck %s --check-prefix=CHECK-DUMP
710
// CHECK-DUMP: Information for module file '{{.*}}/script.pcm':

test/DebugInfo/C-typedef-Darwin.swift

+10-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,13 @@ let blah = size_t(1024)
99
use(blah)
1010
// CHECK: !DIDerivedType(tag: DW_TAG_typedef,
1111
// CHECK-SAME: scope: ![[DARWIN_MODULE:[0-9]+]],
12-
// CHECK: ![[DARWIN_MODULE]] = !DIModule({{.*}}, name: "stddef"
12+
13+
// size_t is defined in clang, originally by <stddef.h>, and later split out to
14+
// <__stddef_size_t.h>. Depending on the state of clang's builtin headers and
15+
// modules, size_t can be in either Darwin.C.stddef or _Builtin_stddef.size_t.
16+
// size_t is also defined in macOS by <sys/_types/_size_t.h> which is in the
17+
// Darwin.POSIX._types._size_t module. Ideally Apple will remove the duplicate
18+
// declaration and clang will settle to _Builtin_stddef.size_t, but while it's
19+
// all in flux allow all three of those modules.
20+
// Darwin.C.stddef|_Builtin_stddef.size_t|Darwin.POSIX._types._size_t
21+
// CHECK: ![[DARWIN_MODULE]] = !DIModule({{.*}}, name: "{{stddef|size_t|_size_t}}"

test/Inputs/clang-importer-sdk/usr/include/module.modulemap

+2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ module ctypes {
33
header "ctypes.h"
44
explicit module bits {
55
header "ctypes/bits.h"
6+
export *
67
}
8+
export *
79
}
810
module stdio { header "stdio.h" }
911
module cvars { header "cvars.h" }

test/Interop/lit.local.cfg

+6-3
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,12 @@ if get_target_os() in ['windows-msvc']:
3333
# Clang should build object files with link settings equivalent to -libc MD
3434
# when building for the MSVC target.
3535
clang_opt = clang_compile_opt + '-D_MT -D_DLL -Xclang --dependent-lib=msvcrt -Xclang --dependent-lib=oldnames '
36-
config.substitutions.insert(0, ('%target-swift-flags', '-vfsoverlay {}'.format(os.path.join(config.swift_obj_root,
37-
'stdlib',
38-
'windows-vfs-overlay.yaml'))))
36+
# ucrt.modulemap currently requires -fbuiltin-headers-in-system-modules. -strict-implicit-module-context
37+
# is necessary for -Xcc arguments to be passed through ModuleInterfaceLoader.
38+
config.substitutions.insert(0, ('%target-swift-flags', '-vfsoverlay {} -strict-implicit-module-context -Xcc -Xclang -Xcc -fbuiltin-headers-in-system-modules'.format(
39+
os.path.join(config.swift_obj_root,
40+
'stdlib',
41+
'windows-vfs-overlay.yaml'))))
3942
else:
4043
# FIXME(compnerd) do all the targets we currently support use SysV ABI?
4144
config.substitutions.insert(0, ('%target-abi', 'SYSV'))

test/ModuleInterface/no-implicit-extra-clang-opts.swift

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
// RUN: %empty-directory(%t)
22
// RUN: %empty-directory(%t/ModuleCache)
33

4+
// https://github.com/apple/swift/issues/70330
5+
// We can't yet call member functions correctly on Windows.
6+
// XFAIL: OS=windows-msvc
7+
48
import SIMod
59

610
// Step 0: Copy relevant files into the temp dir which will serve as the search path

test/lit.cfg

+6-3
Original file line numberDiff line numberDiff line change
@@ -375,10 +375,12 @@ else:
375375
config.swift_system_overlay_opt = ""
376376
config.clang_system_overlay_opt = ""
377377
if kIsWindows:
378-
config.swift_system_overlay_opt = "-vfsoverlay {}".format(
378+
# ucrt.modulemap currently requires -fbuiltin-headers-in-system-modules. -strict-implicit-module-context
379+
# is necessary for -Xcc arguments to be passed through ModuleInterfaceLoader.
380+
config.swift_system_overlay_opt = "-vfsoverlay {} -strict-implicit-module-context -Xcc -Xclang -Xcc -fbuiltin-headers-in-system-modules".format(
379381
os.path.join(config.swift_obj_root, "stdlib", "windows-vfs-overlay.yaml")
380382
)
381-
config.clang_system_overlay_opt = "-Xcc -ivfsoverlay -Xcc {}".format(
383+
config.clang_system_overlay_opt = "-Xcc -ivfsoverlay -Xcc {} -Xcc -Xclang -Xcc -fbuiltin-headers-in-system-modules".format(
382384
os.path.join(config.swift_obj_root, "stdlib", "windows-vfs-overlay.yaml")
383385
)
384386
stdlib_resource_dir_opt = config.resource_dir_opt
@@ -1595,7 +1597,8 @@ elif run_os in ['windows-msvc']:
15951597
config.target_swift_modulewrap = \
15961598
('%r -modulewrap -target %s' % (config.swiftc, config.variant_triple))
15971599
config.target_swift_emit_pcm = \
1598-
('%r -emit-pcm -target %s' % (config.swiftc, config.variant_triple))
1600+
('%r -emit-pcm -target %s %s' % (config.swiftc, config.variant_triple, \
1601+
config.swift_system_overlay_opt))
15991602

16001603

16011604
elif (run_os in ['linux-gnu', 'linux-gnueabihf', 'freebsd', 'openbsd', 'windows-cygnus', 'windows-gnu'] or

utils/build.ps1

+1-1
Original file line numberDiff line numberDiff line change
@@ -670,7 +670,7 @@ function Build-CMakeProject {
670670

671671
$SwiftArgs += @("-resource-dir", "$SwiftResourceDir")
672672
$SwiftArgs += @("-L", "$SwiftResourceDir\windows")
673-
$SwiftArgs += @("-vfsoverlay", "$RuntimeBinaryCache\stdlib\windows-vfs-overlay.yaml")
673+
$SwiftArgs += @("-vfsoverlay", "$RuntimeBinaryCache\stdlib\windows-vfs-overlay.yaml", "-strict-implicit-module-context", "-Xcc", "-Xclang", "-Xcc", "-fbuiltin-headers-in-system-modules")
674674
}
675675
} else {
676676
$SwiftArgs += @("-sdk", (Get-PinnedToolchainSDK))

0 commit comments

Comments
 (0)