Skip to content

IRGen: Use clangs's LLVM datalayout #16842

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/IRGen/IRGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1138,6 +1138,10 @@ bool swift::performLLVM(IRGenOptions &Opts, ASTContext &Ctx,
if (!TargetMachine)
return true;

auto *Clang = static_cast<ClangImporter *>(Ctx.getClangModuleLoader());
// Use clang's datalayout.
Module->setDataLayout(Clang->getTargetInfo().getDataLayout());

embedBitcode(Module, Opts);
if (::performLLVM(Opts, &Ctx.Diags, nullptr, nullptr, Module,
TargetMachine.get(),
Expand Down
9 changes: 8 additions & 1 deletion lib/IRGen/IRGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ IRGenModule::IRGenModule(IRGenerator &irgen,
ClangCodeGen(createClangCodeGenerator(Context, LLVMContext, irgen.Opts,
ModuleName)),
Module(*ClangCodeGen->GetModule()), LLVMContext(Module.getContext()),
DataLayout(target->createDataLayout()),
DataLayout(irgen.getClangDataLayout()),
Triple(irgen.getEffectiveClangTriple()), TargetMachine(std::move(target)),
silConv(irgen.SIL), OutputFilename(OutputFilename),
MainInputFilenameForDebugInfo(MainInputFilenameForDebugInfo),
Expand Down Expand Up @@ -1154,3 +1154,10 @@ llvm::Triple IRGenerator::getEffectiveClangTriple() {
assert(CI && "no clang module loader");
return llvm::Triple(CI->getTargetInfo().getTargetOpts().Triple);
}

const llvm::DataLayout &IRGenerator::getClangDataLayout() {
return static_cast<ClangImporter *>(
SIL.getASTContext().getClangModuleLoader())
->getTargetInfo()
.getDataLayout();
}
2 changes: 2 additions & 0 deletions lib/IRGen/IRGenModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,8 @@ class IRGenerator {

/// Return the effective triple used by clang.
llvm::Triple getEffectiveClangTriple();

const llvm::DataLayout &getClangDataLayout();
};

class ConstantReference {
Expand Down
47 changes: 47 additions & 0 deletions test/Frontend/test_datalayout.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
; RUN: %swiftc_driver_plain -frontend -target x86_64-apple-darwin10 -emit-object %s
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a specific reason to use %swiftc_driver_plain -frontend instead of %swift?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


; REQUIRES: CPU=x86_64

; Make sure we update the datalayout with the current clang's. clang knows
; better about the ABI intricacies.
; This test case woud crash if we did not do this because of i64:128 in the
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/woud/would/

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

; string below.

target datalayout = "e-m:o-i64:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-apple-macosx10.13.0"

define swiftcc i64 @"$S11TestBitcode3add1x1yS2i_SitF"(i64, i64) #0 {
entry:
%2 = call { i64, i1 } @llvm.sadd.with.overflow.i64(i64 %0, i64 %1)
%3 = extractvalue { i64, i1 } %2, 0
%4 = extractvalue { i64, i1 } %2, 1
br i1 %4, label %6, label %5

ret i64 %3

call void @llvm.trap()
unreachable
}

declare { i64, i1 } @llvm.sadd.with.overflow.i64(i64, i64) #1

declare void @llvm.trap() #2

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" }
attributes #1 = { nounwind readnone speculatable }
attributes #2 = { noreturn nounwind }

!llvm.module.flags = !{!0, !1, !2, !3, !4, !5, !6, !7}
!llvm.linker.options = !{!8, !9, !10}

!0 = !{i32 1, !"Objective-C Version", i32 2}
!1 = !{i32 1, !"Objective-C Image Info Version", i32 0}
!2 = !{i32 1, !"Objective-C Image Info Section", !"__DATA,__objc_imageinfo,regular,no_dead_strip"}
!3 = !{i32 4, !"Objective-C Garbage Collection", i32 1536}
!4 = !{i32 1, !"Objective-C Class Properties", i32 64}
!5 = !{i32 1, !"wchar_size", i32 4}
!6 = !{i32 7, !"PIC Level", i32 2}
!7 = !{i32 1, !"Swift Version", i32 6}
!8 = !{!"-lswiftSwiftOnoneSupport"}
!9 = !{!"-lswiftCore"}
!10 = !{!"-lobjc"}