From 2a030a3aec48ed18846518aab60e5e5e72f381cc Mon Sep 17 00:00:00 2001 From: Alp Toker Date: Thu, 2 Jan 2014 15:08:04 +0000 Subject: [PATCH] Verify that clang TargetInfo descriptions match DataLayout strings from LLVM The backend string is only verified when available as it's possible to run clang IRGen for targets that haven't been built or don't exist in LLVM. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@198309 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/CodeGen/BackendUtil.h | 4 ++-- lib/CodeGen/BackendUtil.cpp | 19 ++++++++++++++++--- lib/CodeGen/CodeGenAction.cpp | 8 ++++---- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/include/clang/CodeGen/BackendUtil.h b/include/clang/CodeGen/BackendUtil.h index b9f352cc6d..f8b90b34ee 100644 --- a/include/clang/CodeGen/BackendUtil.h +++ b/include/clang/CodeGen/BackendUtil.h @@ -33,8 +33,8 @@ namespace clang { void EmitBackendOutput(DiagnosticsEngine &Diags, const CodeGenOptions &CGOpts, const TargetOptions &TOpts, const LangOptions &LOpts, - llvm::Module *M, - BackendAction Action, raw_ostream *OS); + StringRef TDesc, llvm::Module *M, BackendAction Action, + raw_ostream *OS); } #endif diff --git a/lib/CodeGen/BackendUtil.cpp b/lib/CodeGen/BackendUtil.cpp index 55970cdf84..db979b3524 100644 --- a/lib/CodeGen/BackendUtil.cpp +++ b/lib/CodeGen/BackendUtil.cpp @@ -610,10 +610,23 @@ void EmitAssemblyHelper::EmitAssembly(BackendAction Action, raw_ostream *OS) { void clang::EmitBackendOutput(DiagnosticsEngine &Diags, const CodeGenOptions &CGOpts, const clang::TargetOptions &TOpts, - const LangOptions &LOpts, - Module *M, - BackendAction Action, raw_ostream *OS) { + const LangOptions &LOpts, StringRef TDesc, + Module *M, BackendAction Action, + raw_ostream *OS) { EmitAssemblyHelper AsmHelper(Diags, CGOpts, TOpts, LOpts, M); AsmHelper.EmitAssembly(Action, OS); + + // If an optional clang TargetInfo description string was passed in, use it to + // verify the LLVM TargetMachine's DataLayout. + if (AsmHelper.TM && !TDesc.empty()) { + std::string DLDesc = + AsmHelper.TM->getDataLayout()->getStringRepresentation(); + if (DLDesc != TDesc) { + unsigned DiagID = Diags.getCustomDiagID( + DiagnosticsEngine::Error, "backend data layout '%0' does not match " + "expected target description '%1'"); + Diags.Report(DiagID) << DLDesc << TDesc; + } + } } diff --git a/lib/CodeGen/CodeGenAction.cpp b/lib/CodeGen/CodeGenAction.cpp index 8efeba68e5..d4f3b8db95 100644 --- a/lib/CodeGen/CodeGenAction.cpp +++ b/lib/CodeGen/CodeGenAction.cpp @@ -150,6 +150,7 @@ namespace clang { Ctx.setInlineAsmDiagnosticHandler(InlineAsmDiagHandler, this); EmitBackendOutput(Diags, CodeGenOpts, TargetOpts, LangOpts, + C.getTargetInfo().getTargetDescription(), TheModule.get(), Action, AsmOutStream); Ctx.setInlineAsmDiagnosticHandler(OldHandler, OldContext); @@ -420,10 +421,9 @@ void CodeGenAction::ExecuteAction() { TheModule->setTargetTriple(TargetOpts.Triple); } - EmitBackendOutput(CI.getDiagnostics(), CI.getCodeGenOpts(), - TargetOpts, CI.getLangOpts(), - TheModule.get(), - BA, OS); + EmitBackendOutput(CI.getDiagnostics(), CI.getCodeGenOpts(), TargetOpts, + CI.getLangOpts(), CI.getTarget().getTargetDescription(), + TheModule.get(), BA, OS); return; } -- 2.40.0