From 0b307f4c68b87eb18e09cd6e4b75dd6bee937b0c Mon Sep 17 00:00:00 2001 From: Yaxun Liu Date: Tue, 12 Apr 2016 20:22:32 +0000 Subject: [PATCH] Pass -backend-option to LLVM when there is no target machine. Clang should pass -backend-option to LLVM even though there is no target machine, since LLVM passes are used when emitting LLVM IR. Differential Revision: http://reviews.llvm.org/D17552 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@266117 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/BackendUtil.cpp | 39 ++++++++++++++++++++-------------- test/Frontend/backend-option.c | 4 ++++ 2 files changed, 27 insertions(+), 16 deletions(-) create mode 100644 test/Frontend/backend-option.c diff --git a/lib/CodeGen/BackendUtil.cpp b/lib/CodeGen/BackendUtil.cpp index 8984a07256..a463b7de0e 100644 --- a/lib/CodeGen/BackendUtil.cpp +++ b/lib/CodeGen/BackendUtil.cpp @@ -98,6 +98,9 @@ private: return PerFunctionPasses; } + /// Set LLVM command line options passed through -backend-option. + void setCommandLineOpts(); + void CreatePasses(ModuleSummaryIndex *ModuleSummary); /// Generates the TargetMachine. @@ -453,6 +456,24 @@ void EmitAssemblyHelper::CreatePasses(ModuleSummaryIndex *ModuleSummary) { PMBuilder.populateModulePassManager(*MPM); } +void EmitAssemblyHelper::setCommandLineOpts() { + SmallVector BackendArgs; + BackendArgs.push_back("clang"); // Fake program name. + if (!CodeGenOpts.DebugPass.empty()) { + BackendArgs.push_back("-debug-pass"); + BackendArgs.push_back(CodeGenOpts.DebugPass.c_str()); + } + if (!CodeGenOpts.LimitFloatPrecision.empty()) { + BackendArgs.push_back("-limit-float-precision"); + BackendArgs.push_back(CodeGenOpts.LimitFloatPrecision.c_str()); + } + for (const std::string &BackendOption : CodeGenOpts.BackendOptions) + BackendArgs.push_back(BackendOption.c_str()); + BackendArgs.push_back(nullptr); + llvm::cl::ParseCommandLineOptions(BackendArgs.size() - 1, + BackendArgs.data()); +} + TargetMachine *EmitAssemblyHelper::CreateTargetMachine(bool MustCreateTM) { // Create the TargetMachine for generating code. std::string Error; @@ -475,22 +496,6 @@ TargetMachine *EmitAssemblyHelper::CreateTargetMachine(bool MustCreateTM) { assert(CodeModel != ~0u && "invalid code model!"); llvm::CodeModel::Model CM = static_cast(CodeModel); - SmallVector BackendArgs; - BackendArgs.push_back("clang"); // Fake program name. - if (!CodeGenOpts.DebugPass.empty()) { - BackendArgs.push_back("-debug-pass"); - BackendArgs.push_back(CodeGenOpts.DebugPass.c_str()); - } - if (!CodeGenOpts.LimitFloatPrecision.empty()) { - BackendArgs.push_back("-limit-float-precision"); - BackendArgs.push_back(CodeGenOpts.LimitFloatPrecision.c_str()); - } - for (const std::string &BackendOption : CodeGenOpts.BackendOptions) - BackendArgs.push_back(BackendOption.c_str()); - BackendArgs.push_back(nullptr); - llvm::cl::ParseCommandLineOptions(BackendArgs.size() - 1, - BackendArgs.data()); - std::string FeaturesStr = llvm::join(TargetOpts.Features.begin(), TargetOpts.Features.end(), ","); @@ -629,6 +634,8 @@ void EmitAssemblyHelper::EmitAssembly(BackendAction Action, raw_pwrite_stream *OS) { TimeRegion Region(llvm::TimePassesIsEnabled ? &CodeGenerationTime : nullptr); + setCommandLineOpts(); + bool UsesCodeGen = (Action != Backend_EmitNothing && Action != Backend_EmitBC && Action != Backend_EmitLL); diff --git a/test/Frontend/backend-option.c b/test/Frontend/backend-option.c new file mode 100644 index 0000000000..e17757422e --- /dev/null +++ b/test/Frontend/backend-option.c @@ -0,0 +1,4 @@ +// RUN: %clang_cc1 %s -emit-llvm -backend-option -time-passes -o - 2>&1 | FileCheck %s +// RUN: %clang_cc1 %s -emit-llvm -backend-option -time-passes -o - -triple spir-unknown-unknown 2>&1 | FileCheck %s +// CHECK: Pass execution timing report + -- 2.40.0