Skip to content

Commit f3d4a64

Browse files
committed
[Explicit Module Builds] Initialize ClangImporter's 'CodeGenerator' using Swift compilation Target Triple.
As per #65930, the Clang importer's Clang instance may be configured with a different (higher) OS version than the compilation target itself in order to be able to load pre-compiled Clang modules that are aligned with the broader SDK, and match the SDK deployment target against which Swift modules are also built. Code-generation, however, must use the actual compilation target triple. This matches how Swift itself loads Swift module dependencies as well: dependency '.swiftinterface' files are type-checked against the availability epoch and code-generated against the actual compilation triple. Resolves rdar://113712186
1 parent 3aea3a0 commit f3d4a64

File tree

4 files changed

+67
-1
lines changed

4 files changed

+67
-1
lines changed

lib/IRGen/IRGenModule.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ static clang::CodeGenerator *createClangCodeGenerator(ASTContext &Context,
9898
assert(Importer && "No clang module loader!");
9999
auto &ClangContext = Importer->getClangASTContext();
100100

101+
auto &CGTI = Importer->getTargetInfo();
101102
auto &CGO = Importer->getCodeGenOpts();
102103
CGO.OptimizationLevel = Opts.shouldOptimize() ? 3 : 0;
103104

@@ -152,7 +153,7 @@ static clang::CodeGenerator *createClangCodeGenerator(ASTContext &Context,
152153
auto *ClangCodeGen = clang::CreateLLVMCodeGen(ClangContext.getDiagnostics(),
153154
ModuleName, &VFS, HSI, PPO, CGO,
154155
LLVMContext);
155-
ClangCodeGen->Initialize(ClangContext);
156+
ClangCodeGen->Initialize(ClangContext, CGTI);
156157
return ClangCodeGen;
157158
}
158159

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
static inline int funcBar(void) {
2+
int result = 0;
3+
if (__builtin_available(macos 11.0, *)) {
4+
result += 1;
5+
} else {
6+
result -= 1;
7+
}
8+
return result;
9+
}

test/ScanDependencies/Inputs/CHeaders/ExtraCModules/module.modulemap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,8 @@ module SubE {
55
module CrossImportTestModule {
66
header "CrossImportTestModule.h"
77
export *
8+
}
9+
module Bar {
10+
header "Bar.h"
11+
export *
812
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// REQUIRES: objc_interop
2+
// RUN: %empty-directory(%t)
3+
// RUN: %empty-directory(%t/clang-module-cache)
4+
// RUN: %empty-directory(%t/inputs)
5+
6+
// RUN: split-file %s %t
7+
// RUN: sed -e "s|INPUTSDIR|%/t/inputs|g" %t/map.json.template > %t/map.json.template1
8+
// RUN: sed -e "s|STDLIBMOD|%/stdlib_module|g" %t/map.json.template1 > %t/map.json.template2
9+
// RUN: sed -e "s|ONONEMOD|%/ononesupport_module|g" %t/map.json.template2 > %t/map.json.template3
10+
// RUN: sed -e "s|CHEADERSDIR|%/S/Inputs/CHeaders|g" %t/map.json.template3 > %t/map.json.template4
11+
// RUN: sed -e "s|SWIFTLIBDIR|%swift-lib-dir|g" %t/map.json.template4 > %t/inputs/map.json
12+
13+
// Pre-build common mandatory deps
14+
// RUN: %target-swift-emit-pcm -target %target-cpu-apple-macosx12.0 -module-name SwiftShims %swift-lib-dir/swift/shims/module.modulemap -o %t/inputs/SwiftShims.pcm
15+
// Pre-build the clang deps with versioned code
16+
// RUN: %target-swift-emit-pcm -target %target-cpu-apple-macosx12.0 -module-name Bar %S/Inputs/CHeaders/ExtraCModules/module.modulemap -o %t/inputs/Bar.pcm
17+
18+
// RUN: %swift-frontend -target %target-cpu-apple-macosx10.14 -O -emit-ir -disable-implicit-concurrency-module-import -disable-implicit-string-processing-module-import -disable-implicit-swift-modules -explicit-swift-module-map-file %t/inputs/map.json -o %t/explicit-clang-target-irgen.ll %t/test.swift -clang-target %target-cpu-apple-macosx12.0
19+
// RUN: %FileCheck %s < %t/explicit-clang-target-irgen.ll
20+
21+
// Ensure that the platform version check is not optimized away, which it would, if we code-generate for '-clang-target' of macosx12.0
22+
// CHECK: {{%[0-9]+}} = tail call i32 @__isPlatformVersionAtLeast(i32 1, i32 11, i32 0, i32 0)
23+
24+
//--- map.json.template
25+
[
26+
{
27+
"moduleName": "Bar",
28+
"clangModulePath": "INPUTSDIR/Bar.pcm",
29+
"clangModuleMapPath": "CHEADERSDIR/ExtraCModules/module.modulemap",
30+
"isFramework": false
31+
},
32+
{
33+
"moduleName": "Swift",
34+
"modulePath": "STDLIBMOD",
35+
"isFramework": false
36+
},
37+
{
38+
"moduleName": "SwiftOnoneSupport",
39+
"modulePath": "ONONEMOD",
40+
"isFramework": false
41+
},
42+
{
43+
"moduleName": "SwiftShims",
44+
"isFramework": false,
45+
"clangModuleMapPath": "SWIFTLIBDIR/swift/shims/module.modulemap",
46+
"clangModulePath": "INPUTSDIR/SwiftShims.pcm"
47+
}]
48+
49+
50+
//--- test.swift
51+
import Bar
52+
print(Bar.funcBar())

0 commit comments

Comments
 (0)