Skip to content

Commit 80ad328

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 2b2beb6 commit 80ad328

File tree

4 files changed

+66
-1
lines changed

4 files changed

+66
-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

@@ -150,7 +151,7 @@ static clang::CodeGenerator *createClangCodeGenerator(ASTContext &Context,
150151
auto *ClangCodeGen = clang::CreateLLVMCodeGen(ClangContext.getDiagnostics(),
151152
ModuleName, &VFS, HSI, PPO, CGO,
152153
LLVMContext);
153-
ClangCodeGen->Initialize(ClangContext);
154+
ClangCodeGen->Initialize(ClangContext, CGTI);
154155
return ClangCodeGen;
155156
}
156157

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: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// REQUIRES: OS=macosx
2+
3+
// RUN: %empty-directory(%t)
4+
// RUN: mkdir -p %t/clang-module-cache
5+
// RUN: mkdir -p %t/inputs
6+
7+
// Pre-build common mandatory deps
8+
// 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
9+
// RUN: %target-swift-emit-pcm -target %target-cpu-apple-macosx12.0 -module-name _SwiftConcurrencyShims %swift-lib-dir/swift/shims/module.modulemap -o %t/inputs/_SwiftConcurrencyShims.pcm
10+
11+
// Pre-build the clang deps with versioned code
12+
// 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
13+
14+
// -disable-implicit-concurrency-module-import
15+
16+
// RUN: echo "[{" > %/t/inputs/map.json
17+
// RUN: echo "\"moduleName\": \"Bar\"," >> %/t/inputs/map.json
18+
// RUN: echo "\"isFramework\": false," >> %/t/inputs/map.json
19+
// RUN: echo "\"clangModuleMapPath\": \"%S/Inputs/CHeaders/ExtraCModules/module.modulemap\"," >> %/t/inputs/map.json
20+
// RUN: echo "\"clangModulePath\": \"%t/inputs/Bar.pcm\"" >> %/t/inputs/map.json
21+
// RUN: echo "}," >> %/t/inputs/map.json
22+
// RUN: echo "{" >> %/t/inputs/map.json
23+
// RUN: echo "\"moduleName\": \"Swift\"," >> %/t/inputs/map.json
24+
// RUN: echo "\"modulePath\": \"%/stdlib_module\"," >> %/t/inputs/map.json
25+
// RUN: echo "\"isFramework\": false" >> %/t/inputs/map.json
26+
// RUN: echo "}," >> %/t/inputs/map.json
27+
// RUN: echo "{" >> %/t/inputs/map.json
28+
// RUN: echo "\"moduleName\": \"SwiftOnoneSupport\"," >> %/t/inputs/map.json
29+
// RUN: echo "\"modulePath\": \"%/ononesupport_module\"," >> %/t/inputs/map.json
30+
// RUN: echo "\"isFramework\": false" >> %/t/inputs/map.json
31+
// RUN: echo "}," >> %/t/inputs/map.json
32+
// RUN: echo "{" >> %/t/inputs/map.json
33+
// RUN: echo "\"moduleName\": \"_StringProcessing\"," >> %/t/inputs/map.json
34+
// RUN: echo "\"modulePath\": \"%/string_processing_module\"," >> %/t/inputs/map.json
35+
// RUN: echo "\"isFramework\": false" >> %/t/inputs/map.json
36+
// RUN: echo "}," >> %/t/inputs/map.json
37+
// RUN: echo "{" >> %/t/inputs/map.json
38+
// RUN: echo "\"moduleName\": \"SwiftShims\"," >> %/t/inputs/map.json
39+
// RUN: echo "\"isFramework\": false," >> %/t/inputs/map.json
40+
// RUN: echo "\"clangModuleMapPath\": \"%swift-lib-dir/swift/shims/module.modulemap\"," >> %/t/inputs/map.json
41+
// RUN: echo "\"clangModulePath\": \"%t/inputs/SwiftShims.pcm\"" >> %/t/inputs/map.json
42+
// RUN: echo "}]" >> %/t/inputs/map.json
43+
44+
// RUN: %swift-frontend -target %target-cpu-apple-macosx10.14 -O -emit-ir -disable-implicit-concurrency-module-import -disable-implicit-swift-modules -explicit-swift-module-map-file %t/inputs/map.json -o %t/explicit-clang-target-irgen.ll %s -clang-target %target-cpu-apple-macosx12.0
45+
// RUN: %FileCheck %s < %t/explicit-clang-target-irgen.ll
46+
47+
import Bar
48+
print(Bar.funcBar())
49+
50+
// Ensure that the platform version check is not optimized away, which it would, if we code-generate for '-clang-target' of macosx12.0
51+
// CHECK: {{%[0-9]+}} = tail call i32 @__isPlatformVersionAtLeast(i32 1, i32 11, i32 0, i32 0)

0 commit comments

Comments
 (0)