Skip to content

Commit 042d837

Browse files
Merge pull request #16842 from aschwaighofer/irgen_use_clangs_datalayout
IRGen: Use clangs's LLVM datalayout
2 parents ea09f2d + 81a15fb commit 042d837

File tree

4 files changed

+61
-1
lines changed

4 files changed

+61
-1
lines changed

lib/IRGen/IRGen.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,6 +1138,10 @@ bool swift::performLLVM(IRGenOptions &Opts, ASTContext &Ctx,
11381138
if (!TargetMachine)
11391139
return true;
11401140

1141+
auto *Clang = static_cast<ClangImporter *>(Ctx.getClangModuleLoader());
1142+
// Use clang's datalayout.
1143+
Module->setDataLayout(Clang->getTargetInfo().getDataLayout());
1144+
11411145
embedBitcode(Module, Opts);
11421146
if (::performLLVM(Opts, &Ctx.Diags, nullptr, nullptr, Module,
11431147
TargetMachine.get(),

lib/IRGen/IRGenModule.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ IRGenModule::IRGenModule(IRGenerator &irgen,
131131
ClangCodeGen(createClangCodeGenerator(Context, LLVMContext, irgen.Opts,
132132
ModuleName)),
133133
Module(*ClangCodeGen->GetModule()), LLVMContext(Module.getContext()),
134-
DataLayout(target->createDataLayout()),
134+
DataLayout(irgen.getClangDataLayout()),
135135
Triple(irgen.getEffectiveClangTriple()), TargetMachine(std::move(target)),
136136
silConv(irgen.SIL), OutputFilename(OutputFilename),
137137
MainInputFilenameForDebugInfo(MainInputFilenameForDebugInfo),
@@ -1154,3 +1154,10 @@ llvm::Triple IRGenerator::getEffectiveClangTriple() {
11541154
assert(CI && "no clang module loader");
11551155
return llvm::Triple(CI->getTargetInfo().getTargetOpts().Triple);
11561156
}
1157+
1158+
const llvm::DataLayout &IRGenerator::getClangDataLayout() {
1159+
return static_cast<ClangImporter *>(
1160+
SIL.getASTContext().getClangModuleLoader())
1161+
->getTargetInfo()
1162+
.getDataLayout();
1163+
}

lib/IRGen/IRGenModule.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,8 @@ class IRGenerator {
395395

396396
/// Return the effective triple used by clang.
397397
llvm::Triple getEffectiveClangTriple();
398+
399+
const llvm::DataLayout &getClangDataLayout();
398400
};
399401

400402
class ConstantReference {

test/Frontend/test_datalayout.ll

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
; RUN: %swiftc_driver_plain -frontend -target x86_64-apple-darwin10 -emit-object %s
2+
3+
; REQUIRES: CPU=x86_64
4+
5+
; Make sure we update the datalayout with the current clang's. clang knows
6+
; better about the ABI intricacies.
7+
; This test case woud crash if we did not do this because of i64:128 in the
8+
; string below.
9+
10+
target datalayout = "e-m:o-i64:128-f80:128-n8:16:32:64-S128"
11+
target triple = "x86_64-apple-macosx10.13.0"
12+
13+
define swiftcc i64 @"$S11TestBitcode3add1x1yS2i_SitF"(i64, i64) #0 {
14+
entry:
15+
%2 = call { i64, i1 } @llvm.sadd.with.overflow.i64(i64 %0, i64 %1)
16+
%3 = extractvalue { i64, i1 } %2, 0
17+
%4 = extractvalue { i64, i1 } %2, 1
18+
br i1 %4, label %6, label %5
19+
20+
ret i64 %3
21+
22+
call void @llvm.trap()
23+
unreachable
24+
}
25+
26+
declare { i64, i1 } @llvm.sadd.with.overflow.i64(i64, i64) #1
27+
28+
declare void @llvm.trap() #2
29+
30+
attributes #0 = { "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "target-cpu"="penryn" "target-features"="+cx16,+fxsr,+mmx,+sahf,+sse,+sse2,+sse3,+sse4.1,+ssse3,+x87" }
31+
attributes #1 = { nounwind readnone speculatable }
32+
attributes #2 = { noreturn nounwind }
33+
34+
!llvm.module.flags = !{!0, !1, !2, !3, !4, !5, !6, !7}
35+
!llvm.linker.options = !{!8, !9, !10}
36+
37+
!0 = !{i32 1, !"Objective-C Version", i32 2}
38+
!1 = !{i32 1, !"Objective-C Image Info Version", i32 0}
39+
!2 = !{i32 1, !"Objective-C Image Info Section", !"__DATA,__objc_imageinfo,regular,no_dead_strip"}
40+
!3 = !{i32 4, !"Objective-C Garbage Collection", i32 1536}
41+
!4 = !{i32 1, !"Objective-C Class Properties", i32 64}
42+
!5 = !{i32 1, !"wchar_size", i32 4}
43+
!6 = !{i32 7, !"PIC Level", i32 2}
44+
!7 = !{i32 1, !"Swift Version", i32 6}
45+
!8 = !{!"-lswiftSwiftOnoneSupport"}
46+
!9 = !{!"-lswiftCore"}
47+
!10 = !{!"-lobjc"}

0 commit comments

Comments
 (0)