From 18ca6038ae8d216a9ad9a44e3442778096052aa2 Mon Sep 17 00:00:00 2001 From: Andrew Luo Date: Thu, 7 Oct 2021 15:06:37 -0700 Subject: [PATCH 01/16] flags to turn off and on --- src/target/llvm/codegen_llvm.cc | 6 ++++++ src/target/llvm/codegen_llvm.h | 7 +++++++ src/target/llvm/llvm_module.cc | 9 +++++++++ 3 files changed, 22 insertions(+) diff --git a/src/target/llvm/codegen_llvm.cc b/src/target/llvm/codegen_llvm.cc index 12fbf2c3e42c..21e37c81d894 100644 --- a/src/target/llvm/codegen_llvm.cc +++ b/src/target/llvm/codegen_llvm.cc @@ -77,6 +77,12 @@ void CodeGenLLVM::Init(const std::string& module_name, llvm::TargetMachine* tm, this->InitTarget(tm); } +void CodeGenLLVM::SetFastMathFlag(bool flag) { + llvm::FastMathFlags fmf; + fmf.setFast(flag); + builder_->setFastMathFlags(fmf); +} + void CodeGenLLVM::InitTarget(llvm::TargetMachine* tm) { module_->setTargetTriple(tm->getTargetTriple().str()); module_->setDataLayout(tm->createDataLayout()); diff --git a/src/target/llvm/codegen_llvm.h b/src/target/llvm/codegen_llvm.h index 177b53056354..4867843ed2eb 100644 --- a/src/target/llvm/codegen_llvm.h +++ b/src/target/llvm/codegen_llvm.h @@ -78,6 +78,13 @@ class CodeGenLLVM : public ExprFunctor, */ virtual void Init(const std::string& module_name, llvm::TargetMachine* tm, llvm::LLVMContext* ctx, bool system_lib, bool dynamic_lookup, bool target_c_runtime); + + /*! + * \brief Turn on fast math flags for floating point operations. + * \param flag True to set FastMathMode for llvm, False to turn off FastMathMode for llvm. + */ + void SetFastMathFlag(bool flag); + /*! * \brief Compile and add function f to the current module. * \param f The function to be added. diff --git a/src/target/llvm/llvm_module.cc b/src/target/llvm/llvm_module.cc index 0e4bca4396f5..53ec694eb332 100644 --- a/src/target/llvm/llvm_module.cc +++ b/src/target/llvm/llvm_module.cc @@ -258,6 +258,15 @@ class LLVMModuleNode final : public runtime::ModuleNode { // makes sense when we start to use multiple modules. cg->Init("TVMMod", tm_.get(), ctx_.get(), system_lib, system_lib, target_c_runtime); + char* fast_math_flag_setting = getenv("TVM_FAST_MATH_FLAG"); + bool fast_math_flag; + if (fast_math_flag_setting != nullptr) { + fast_math_flag = atoi(fast_math_flag_setting); + } else { + fast_math_flag = false; + } + cg->SetFastMathFlag(fast_math_flag); + cg->AddFunctionsOrdered(funcs.begin(), funcs.end()); if (entry_func.length() != 0) { From 1c8da0ff21cdcffb65634f414c14204c3b2f4128 Mon Sep 17 00:00:00 2001 From: Andrew Luo Date: Thu, 7 Oct 2021 15:26:15 -0700 Subject: [PATCH 02/16] turn fast math on always --- src/target/llvm/llvm_module.cc | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/target/llvm/llvm_module.cc b/src/target/llvm/llvm_module.cc index 53ec694eb332..6487798c6329 100644 --- a/src/target/llvm/llvm_module.cc +++ b/src/target/llvm/llvm_module.cc @@ -265,7 +265,9 @@ class LLVMModuleNode final : public runtime::ModuleNode { } else { fast_math_flag = false; } - cg->SetFastMathFlag(fast_math_flag); + // For testing purposes only + // cg->SetFastMathFlag(fast_math_flag); + cg->SetFastMathFlag(true); cg->AddFunctionsOrdered(funcs.begin(), funcs.end()); From 444e51a38baf19724c92128b1054a099edb150b1 Mon Sep 17 00:00:00 2001 From: Andrew Luo Date: Fri, 8 Oct 2021 17:06:29 -0700 Subject: [PATCH 03/16] llvm more opts --- src/target/llvm/llvm_common.cc | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/target/llvm/llvm_common.cc b/src/target/llvm/llvm_common.cc index be80a8bc767e..f67b2a0c56e3 100644 --- a/src/target/llvm/llvm_common.cc +++ b/src/target/llvm/llvm_common.cc @@ -106,10 +106,13 @@ void ParseLLVMTargetOptions(const Target& target, std::string* triple, std::stri #if TVM_LLVM_VERSION < 50 opt.LessPreciseFPMADOption = true; #endif + // Turn on all flags which trade FP precision for speed opt.AllowFPOpFusion = llvm::FPOpFusion::Fast; - opt.UnsafeFPMath = false; - opt.NoInfsFPMath = false; + opt.UnsafeFPMath = true; + opt.NoInfsFPMath = true; opt.NoNaNsFPMath = true; + opt.NoTrappingFPMath = true; + opt.NoSignedZerosFPMath = true; if (soft_float_abi) { opt.FloatABIType = llvm::FloatABI::Soft; } else { @@ -139,8 +142,8 @@ std::unique_ptr GetLLVMTargetMachine(const Target& target, ICHECK(allow_null) << err << " target_triple=" << target_triple; return nullptr; } - llvm::TargetMachine* tm = - llvm_target->createTargetMachine(target_triple, mcpu, mattr, opt, llvm::Reloc::PIC_); + llvm::TargetMachine* tm = llvm_target->createTargetMachine( + target_triple, mcpu, mattr, opt, llvm::Reloc::PIC_, llvm::None, llvm::CodeGenOpt::Aggressive); return std::unique_ptr(tm); } From 6938d70136909779aa1fe098a1628d07245734ca Mon Sep 17 00:00:00 2001 From: Andrew Luo Date: Sun, 10 Oct 2021 21:48:56 -0700 Subject: [PATCH 04/16] move to default codegen opt --- src/target/llvm/llvm_common.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/target/llvm/llvm_common.cc b/src/target/llvm/llvm_common.cc index f67b2a0c56e3..ea3c0948bcb9 100644 --- a/src/target/llvm/llvm_common.cc +++ b/src/target/llvm/llvm_common.cc @@ -143,7 +143,7 @@ std::unique_ptr GetLLVMTargetMachine(const Target& target, return nullptr; } llvm::TargetMachine* tm = llvm_target->createTargetMachine( - target_triple, mcpu, mattr, opt, llvm::Reloc::PIC_, llvm::None, llvm::CodeGenOpt::Aggressive); + target_triple, mcpu, mattr, opt, llvm::Reloc::PIC_, llvm::None, llvm::CodeGenOpt::Default); return std::unique_ptr(tm); } From 17fa49c8e4fe6e6a32dfb8f42abdb24161d3a48b Mon Sep 17 00:00:00 2001 From: Andrew Luo Date: Mon, 11 Oct 2021 11:41:46 -0700 Subject: [PATCH 05/16] TODO --- src/target/llvm/codegen_llvm.cc | 1 + 1 file changed, 1 insertion(+) diff --git a/src/target/llvm/codegen_llvm.cc b/src/target/llvm/codegen_llvm.cc index 21e37c81d894..e6e3318e01f9 100644 --- a/src/target/llvm/codegen_llvm.cc +++ b/src/target/llvm/codegen_llvm.cc @@ -78,6 +78,7 @@ void CodeGenLLVM::Init(const std::string& module_name, llvm::TargetMachine* tm, } void CodeGenLLVM::SetFastMathFlag(bool flag) { + // TODO: set target options via triple https://llvm.org/doxygen/CommandFlags_8cpp_source.html#l00496 llvm::FastMathFlags fmf; fmf.setFast(flag); builder_->setFastMathFlags(fmf); From 2795510d324939d66eada3f0bde38fe2467f7bb5 Mon Sep 17 00:00:00 2001 From: Andrew Zhao Luo Date: Tue, 12 Oct 2021 11:45:21 -0700 Subject: [PATCH 06/16] add fast math options to llvm target --- src/target/target_kind.cc | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/target/target_kind.cc b/src/target/target_kind.cc index d719386d204b..29403f5d15d7 100644 --- a/src/target/target_kind.cc +++ b/src/target/target_kind.cc @@ -222,6 +222,14 @@ TVM_REGISTER_TARGET_KIND("llvm", kDLCPU) .add_attr_option("link-params", Bool(false)) .add_attr_option("unpacked-api") .add_attr_option("interface-api") + // Fast math flags, see https://llvm.org/docs/LangRef.html#fast-math-flags + .add_attr_option("fast-math") // implies all the below + .add_attr_option("fast-math-nnan") + .add_attr_option("fast-math-ninf") + .add_attr_option("fast-math-nsz") + .add_attr_option("fast-math-arcp") + .add_attr_option("fast-math-contract") + .add_attr_option("fast-math-reassoc") .set_default_keys({"cpu"}); TVM_REGISTER_TARGET_KIND("c", kDLCPU) From 02cd251dbaef4201978fc88daf8684626a70c1ce Mon Sep 17 00:00:00 2001 From: Andrew Zhao Luo Date: Tue, 12 Oct 2021 15:03:13 -0700 Subject: [PATCH 07/16] move to using new target attributes --- src/target/llvm/codegen_llvm.cc | 7 +------ src/target/llvm/codegen_llvm.h | 4 ++-- src/target/llvm/llvm_module.cc | 30 +++++++++++++++++++++--------- 3 files changed, 24 insertions(+), 17 deletions(-) diff --git a/src/target/llvm/codegen_llvm.cc b/src/target/llvm/codegen_llvm.cc index e6e3318e01f9..88ad2e5b51cf 100644 --- a/src/target/llvm/codegen_llvm.cc +++ b/src/target/llvm/codegen_llvm.cc @@ -77,12 +77,7 @@ void CodeGenLLVM::Init(const std::string& module_name, llvm::TargetMachine* tm, this->InitTarget(tm); } -void CodeGenLLVM::SetFastMathFlag(bool flag) { - // TODO: set target options via triple https://llvm.org/doxygen/CommandFlags_8cpp_source.html#l00496 - llvm::FastMathFlags fmf; - fmf.setFast(flag); - builder_->setFastMathFlags(fmf); -} +void CodeGenLLVM::SetFastMathFlag(llvm::FastMathFlags fmf) { builder_->setFastMathFlags(fmf); } void CodeGenLLVM::InitTarget(llvm::TargetMachine* tm) { module_->setTargetTriple(tm->getTargetTriple().str()); diff --git a/src/target/llvm/codegen_llvm.h b/src/target/llvm/codegen_llvm.h index 4867843ed2eb..4a9df65951c0 100644 --- a/src/target/llvm/codegen_llvm.h +++ b/src/target/llvm/codegen_llvm.h @@ -81,9 +81,9 @@ class CodeGenLLVM : public ExprFunctor, /*! * \brief Turn on fast math flags for floating point operations. - * \param flag True to set FastMathMode for llvm, False to turn off FastMathMode for llvm. + * \param fmf FastMathFlags to use for code generation. */ - void SetFastMathFlag(bool flag); + void SetFastMathFlag(llvm::FastMathFlags fmf); /*! * \brief Compile and add function f to the current module. diff --git a/src/target/llvm/llvm_module.cc b/src/target/llvm/llvm_module.cc index 6487798c6329..72d385f79916 100644 --- a/src/target/llvm/llvm_module.cc +++ b/src/target/llvm/llvm_module.cc @@ -258,19 +258,31 @@ class LLVMModuleNode final : public runtime::ModuleNode { // makes sense when we start to use multiple modules. cg->Init("TVMMod", tm_.get(), ctx_.get(), system_lib, system_lib, target_c_runtime); - char* fast_math_flag_setting = getenv("TVM_FAST_MATH_FLAG"); - bool fast_math_flag; - if (fast_math_flag_setting != nullptr) { - fast_math_flag = atoi(fast_math_flag_setting); + // See https://llvm.org/docs/LangRef.html#fast-math-flags for details + Bool fast_math_all = target->GetAttr("fast-math").value_or(Bool(false)); + llvm::FastMathFlags fmf; + if (fast_math_all) { + fmf.setFast(fast_math_all); } else { - fast_math_flag = false; + Bool fast_math_nnan = target->GetAttr("fast-math-nnan").value_or(Bool(false)); + Bool fast_math_ninf = target->GetAttr("fast-math-ninf").value_or(Bool(false)); + Bool fast_math_nsz = target->GetAttr("fast-math-nsz").value_or(Bool(false)); + Bool fast_math_arcp = target->GetAttr("fast-math-arcp").value_or(Bool(false)); + Bool fast_math_contract = target->GetAttr("fast-math-contract").value_or(Bool(false)); + Bool fast_math_afn = target->GetAttr("fast-math-afn").value_or(Bool(false)); + Bool fast_math_reassoc = target->GetAttr("fast-math-reassoc").value_or(Bool(false)); + + fmf.setNoNaNs(fast_math_nnan); + fmf.setNoInfs(fast_math_ninf); + fmf.setNoSignedZeros(fast_math_nsz); + fmf.setAllowReciprocal(fast_math_arcp); + fmf.setAllowContract(fast_math_contract); + fmf.setApproxFunc(fast_math_afn); + fmf.setAllowReassoc(fast_math_reassoc); } - // For testing purposes only - // cg->SetFastMathFlag(fast_math_flag); - cg->SetFastMathFlag(true); + cg->SetFastMathFlag(fmf); cg->AddFunctionsOrdered(funcs.begin(), funcs.end()); - if (entry_func.length() != 0) { cg->AddMainFunction(entry_func); } From 3d6c2c3c19113d69f9c63690b8f4b197bfba9d45 Mon Sep 17 00:00:00 2001 From: Andrew Zhao Luo Date: Tue, 12 Oct 2021 15:21:40 -0700 Subject: [PATCH 08/16] llvm fast math target opt code --- src/target/llvm/llvm_common.cc | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/target/llvm/llvm_common.cc b/src/target/llvm/llvm_common.cc index ea3c0948bcb9..fd4336636af3 100644 --- a/src/target/llvm/llvm_common.cc +++ b/src/target/llvm/llvm_common.cc @@ -106,13 +106,21 @@ void ParseLLVMTargetOptions(const Target& target, std::string* triple, std::stri #if TVM_LLVM_VERSION < 50 opt.LessPreciseFPMADOption = true; #endif - // Turn on all flags which trade FP precision for speed - opt.AllowFPOpFusion = llvm::FPOpFusion::Fast; + // We depend on generating IR with proper fast math flags to control fast math + // semantics. These just enable these optimizations if the proper IR flags + // are set. opt.UnsafeFPMath = true; opt.NoInfsFPMath = true; opt.NoNaNsFPMath = true; - opt.NoTrappingFPMath = true; opt.NoSignedZerosFPMath = true; + opt.ApproxFuncFPMath = true; + + // Assume no generated code ever needs to handle floating point exceptions. + opt.NoTrappingFPMath = true; + + // TODO (AndrewZhaoLuo): Look into control of setting this flag. + opt.AllowFPOpFusion = llvm::FPOpFusion::Fast; + if (soft_float_abi) { opt.FloatABIType = llvm::FloatABI::Soft; } else { From b244dec084d7339ce0a535df9219f2761901471f Mon Sep 17 00:00:00 2001 From: Andrew Zhao Luo Date: Tue, 12 Oct 2021 15:29:24 -0700 Subject: [PATCH 09/16] add -O flags --- src/target/llvm/llvm_common.cc | 17 +++++++++++++++-- src/target/target_kind.cc | 1 + 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/target/llvm/llvm_common.cc b/src/target/llvm/llvm_common.cc index fd4336636af3..f9c35ff59023 100644 --- a/src/target/llvm/llvm_common.cc +++ b/src/target/llvm/llvm_common.cc @@ -113,7 +113,6 @@ void ParseLLVMTargetOptions(const Target& target, std::string* triple, std::stri opt.NoInfsFPMath = true; opt.NoNaNsFPMath = true; opt.NoSignedZerosFPMath = true; - opt.ApproxFuncFPMath = true; // Assume no generated code ever needs to handle floating point exceptions. opt.NoTrappingFPMath = true; @@ -150,8 +149,22 @@ std::unique_ptr GetLLVMTargetMachine(const Target& target, ICHECK(allow_null) << err << " target_triple=" << target_triple; return nullptr; } + + Integer llvm_opt_level = target->GetAttr("O").value_or(Integer(2)); + llvm::CodeGenOpt::Level llvm_opt; + if (llvm_opt_level <= 0) { + llvm_opt = llvm::CodeGenOpt::None; + } else if (llvm_opt_level == 1) { + llvm_opt = llvm::CodeGenOpt::Less; + } else if (llvm_opt_level == 2) { + llvm_opt = llvm::CodeGenOpt::Default; + } else { + // llvm_opt_level >= 3 + llvm_opt = llvm::CodeGenOpt::Aggressive; + } + llvm::TargetMachine* tm = llvm_target->createTargetMachine( - target_triple, mcpu, mattr, opt, llvm::Reloc::PIC_, llvm::None, llvm::CodeGenOpt::Default); + target_triple, mcpu, mattr, opt, llvm::Reloc::PIC_, llvm::None, llvm_opt); return std::unique_ptr(tm); } diff --git a/src/target/target_kind.cc b/src/target/target_kind.cc index 29403f5d15d7..bc05d96212f4 100644 --- a/src/target/target_kind.cc +++ b/src/target/target_kind.cc @@ -230,6 +230,7 @@ TVM_REGISTER_TARGET_KIND("llvm", kDLCPU) .add_attr_option("fast-math-arcp") .add_attr_option("fast-math-contract") .add_attr_option("fast-math-reassoc") + .add_attr_option("O") .set_default_keys({"cpu"}); TVM_REGISTER_TARGET_KIND("c", kDLCPU) From 0c5d38b3424e81b82ef792e4db3b4c877eea565f Mon Sep 17 00:00:00 2001 From: Andrew Zhao Luo Date: Tue, 12 Oct 2021 15:36:42 -0700 Subject: [PATCH 10/16] fix todo lint --- src/target/llvm/llvm_common.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/target/llvm/llvm_common.cc b/src/target/llvm/llvm_common.cc index f9c35ff59023..0339a43cf37b 100644 --- a/src/target/llvm/llvm_common.cc +++ b/src/target/llvm/llvm_common.cc @@ -117,7 +117,7 @@ void ParseLLVMTargetOptions(const Target& target, std::string* triple, std::stri // Assume no generated code ever needs to handle floating point exceptions. opt.NoTrappingFPMath = true; - // TODO (AndrewZhaoLuo): Look into control of setting this flag. + // TODO(AndrewZhaoLuo): Look into control of setting this flag. opt.AllowFPOpFusion = llvm::FPOpFusion::Fast; if (soft_float_abi) { From c9ac146feb24c3cc573b422a2f3fc024df4f778e Mon Sep 17 00:00:00 2001 From: Andrew Luo Date: Wed, 13 Oct 2021 12:57:30 -0700 Subject: [PATCH 11/16] support llvm 4.0, 5.0 --- src/target/llvm/llvm_common.cc | 5 ++- src/target/llvm/llvm_module.cc | 56 +++++++++++++++++++++++----------- 2 files changed, 43 insertions(+), 18 deletions(-) diff --git a/src/target/llvm/llvm_common.cc b/src/target/llvm/llvm_common.cc index 0339a43cf37b..17a77878020c 100644 --- a/src/target/llvm/llvm_common.cc +++ b/src/target/llvm/llvm_common.cc @@ -112,7 +112,10 @@ void ParseLLVMTargetOptions(const Target& target, std::string* triple, std::stri opt.UnsafeFPMath = true; opt.NoInfsFPMath = true; opt.NoNaNsFPMath = true; + +#if TVM_LLVM_VERSION >= 50 opt.NoSignedZerosFPMath = true; +#endif // Assume no generated code ever needs to handle floating point exceptions. opt.NoTrappingFPMath = true; @@ -164,7 +167,7 @@ std::unique_ptr GetLLVMTargetMachine(const Target& target, } llvm::TargetMachine* tm = llvm_target->createTargetMachine( - target_triple, mcpu, mattr, opt, llvm::Reloc::PIC_, llvm::None, llvm_opt); + target_triple, mcpu, mattr, opt, llvm::Reloc::PIC_, llvm::CodeModel::Small, llvm_opt); return std::unique_ptr(tm); } diff --git a/src/target/llvm/llvm_module.cc b/src/target/llvm/llvm_module.cc index 72d385f79916..657778df0e93 100644 --- a/src/target/llvm/llvm_module.cc +++ b/src/target/llvm/llvm_module.cc @@ -260,26 +260,48 @@ class LLVMModuleNode final : public runtime::ModuleNode { // See https://llvm.org/docs/LangRef.html#fast-math-flags for details Bool fast_math_all = target->GetAttr("fast-math").value_or(Bool(false)); + Bool fast_math_nnan = target->GetAttr("fast-math-nnan").value_or(Bool(false)); + Bool fast_math_ninf = target->GetAttr("fast-math-ninf").value_or(Bool(false)); + Bool fast_math_nsz = target->GetAttr("fast-math-nsz").value_or(Bool(false)); + Bool fast_math_arcp = target->GetAttr("fast-math-arcp").value_or(Bool(false)); + llvm::FastMathFlags fmf; if (fast_math_all) { - fmf.setFast(fast_math_all); - } else { - Bool fast_math_nnan = target->GetAttr("fast-math-nnan").value_or(Bool(false)); - Bool fast_math_ninf = target->GetAttr("fast-math-ninf").value_or(Bool(false)); - Bool fast_math_nsz = target->GetAttr("fast-math-nsz").value_or(Bool(false)); - Bool fast_math_arcp = target->GetAttr("fast-math-arcp").value_or(Bool(false)); - Bool fast_math_contract = target->GetAttr("fast-math-contract").value_or(Bool(false)); - Bool fast_math_afn = target->GetAttr("fast-math-afn").value_or(Bool(false)); - Bool fast_math_reassoc = target->GetAttr("fast-math-reassoc").value_or(Bool(false)); - - fmf.setNoNaNs(fast_math_nnan); - fmf.setNoInfs(fast_math_ninf); - fmf.setNoSignedZeros(fast_math_nsz); - fmf.setAllowReciprocal(fast_math_arcp); - fmf.setAllowContract(fast_math_contract); - fmf.setApproxFunc(fast_math_afn); - fmf.setAllowReassoc(fast_math_reassoc); +#if TVM_LLVM_VERSION >= 60 + fmf.setFast(); +#else + fmf.setUnsafeAlgebra(); +#endif + } + + if (fast_math_nnan) { + fmf.setNoNaNs(); + } + if (fast_math_ninf) { + fmf.setNoInfs(); + } + if (fast_math_nsz) { + fmf.setNoSignedZeros(); } + if (fast_math_arcp) { + fmf.setAllowReciprocal(); + } + +#if TVM_LLVM_VERSION >= 60 + Bool fast_math_contract = target->GetAttr("fast-math-contract").value_or(Bool(false)); + Bool fast_math_afn = target->GetAttr("fast-math-afn").value_or(Bool(false)); + Bool fast_math_reassoc = target->GetAttr("fast-math-reassoc").value_or(Bool(false)); + if (fast_math_contract) { + fmf.setAllowContract(); + } + if (fast_math_afn) { + fmf.setApproxFunc(); + } + if (fast_math_reassoc) { + fmf.setAllowReassoc(); + } +#endif + cg->SetFastMathFlag(fmf); cg->AddFunctionsOrdered(funcs.begin(), funcs.end()); From 99ae59f437d889973cd572e45ebdf8998189458c Mon Sep 17 00:00:00 2001 From: Andrew Zhao Luo Date: Mon, 18 Oct 2021 14:26:18 -0700 Subject: [PATCH 12/16] use same opt level as target machine --- src/target/llvm/codegen_llvm.cc | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/target/llvm/codegen_llvm.cc b/src/target/llvm/codegen_llvm.cc index 88ad2e5b51cf..e776861c5bbd 100644 --- a/src/target/llvm/codegen_llvm.cc +++ b/src/target/llvm/codegen_llvm.cc @@ -345,7 +345,26 @@ void CodeGenLLVM::Optimize() { // place optimization pass llvm::PassManagerBuilder builder; - builder.OptLevel = 3; + + // Use the same opt-level as specified in TargetMachine for running passes + llvm::CodeGenOpt::Level opt_level = target_machine_->getOptLevel(); + + switch (opt_level) { + case llvm::CodeGenOpt::Level::None: + builder.OptLevel = 0; + break; + case llvm::CodeGenOpt::Level::Less: + builder.OptLevel = 1; + break; + + case llvm::CodeGenOpt::Level::Default: + builder.OptLevel = 2; + break; + + default: + // CodeGenOpt::Level::Aggressive + builder.OptLevel = 3; + } #if TVM_LLVM_VERSION >= 50 builder.Inliner = llvm::createFunctionInliningPass(builder.OptLevel, 0, false); @@ -412,7 +431,7 @@ llvm::Type* CodeGenLLVM::DTypeToLLVMType(const DataType& dtype) const { } else { return etype; } -} +} // namespace codegen llvm::Type* CodeGenLLVM::GetLLVMType(const Type& type) const { if (auto* ptr = type.as()) { From d9e352454a3136f07e79e8b418c51ee7d808e990 Mon Sep 17 00:00:00 2001 From: Andrew Zhao Luo Date: Mon, 18 Oct 2021 14:37:57 -0700 Subject: [PATCH 13/16] revert TargetOptions --- src/target/llvm/llvm_common.cc | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/src/target/llvm/llvm_common.cc b/src/target/llvm/llvm_common.cc index 17a77878020c..2866d6f36ff7 100644 --- a/src/target/llvm/llvm_common.cc +++ b/src/target/llvm/llvm_common.cc @@ -106,23 +106,11 @@ void ParseLLVMTargetOptions(const Target& target, std::string* triple, std::stri #if TVM_LLVM_VERSION < 50 opt.LessPreciseFPMADOption = true; #endif - // We depend on generating IR with proper fast math flags to control fast math - // semantics. These just enable these optimizations if the proper IR flags - // are set. - opt.UnsafeFPMath = true; - opt.NoInfsFPMath = true; - opt.NoNaNsFPMath = true; - -#if TVM_LLVM_VERSION >= 50 - opt.NoSignedZerosFPMath = true; -#endif - - // Assume no generated code ever needs to handle floating point exceptions. - opt.NoTrappingFPMath = true; - - // TODO(AndrewZhaoLuo): Look into control of setting this flag. + // In clang, these are fed from LangOpts which describe language specific features + // TODO(AndrewZhaoLuo): figure out how these relate to fast math flags opt.AllowFPOpFusion = llvm::FPOpFusion::Fast; - + opt.UnsafeFPMath = false; + opt.NoInfsFPMath = false; if (soft_float_abi) { opt.FloatABIType = llvm::FloatABI::Soft; } else { From 5466663a156580922cfb84f24d5a0b5b5461ef02 Mon Sep 17 00:00:00 2001 From: Andrew Zhao Luo Date: Mon, 18 Oct 2021 14:40:10 -0700 Subject: [PATCH 14/16] fix thing --- src/target/llvm/llvm_common.cc | 1 + 1 file changed, 1 insertion(+) diff --git a/src/target/llvm/llvm_common.cc b/src/target/llvm/llvm_common.cc index 2866d6f36ff7..79a1dc615cd4 100644 --- a/src/target/llvm/llvm_common.cc +++ b/src/target/llvm/llvm_common.cc @@ -111,6 +111,7 @@ void ParseLLVMTargetOptions(const Target& target, std::string* triple, std::stri opt.AllowFPOpFusion = llvm::FPOpFusion::Fast; opt.UnsafeFPMath = false; opt.NoInfsFPMath = false; + opt.NoNaNsFPMath = true; if (soft_float_abi) { opt.FloatABIType = llvm::FloatABI::Soft; } else { From cfeb699b09da372f3f9807a1b24c5e291b82c9d0 Mon Sep 17 00:00:00 2001 From: Andrew Zhao Luo Date: Mon, 18 Oct 2021 14:55:12 -0700 Subject: [PATCH 15/16] prevent regression in llvm --- src/target/llvm/llvm_common.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/target/llvm/llvm_common.cc b/src/target/llvm/llvm_common.cc index 79a1dc615cd4..f410cc093e42 100644 --- a/src/target/llvm/llvm_common.cc +++ b/src/target/llvm/llvm_common.cc @@ -142,7 +142,7 @@ std::unique_ptr GetLLVMTargetMachine(const Target& target, return nullptr; } - Integer llvm_opt_level = target->GetAttr("O").value_or(Integer(2)); + Integer llvm_opt_level = target->GetAttr("O").value_or(Integer(3)); llvm::CodeGenOpt::Level llvm_opt; if (llvm_opt_level <= 0) { llvm_opt = llvm::CodeGenOpt::None; From 2abbed5ddbb021782c6d2e5d9455be69a42594bd Mon Sep 17 00:00:00 2001 From: Andrew Zhao Luo Date: Mon, 18 Oct 2021 14:56:59 -0700 Subject: [PATCH 16/16] togglable opt-levels --- src/target/llvm/llvm_common.cc | 2 +- src/target/target_kind.cc | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/target/llvm/llvm_common.cc b/src/target/llvm/llvm_common.cc index f410cc093e42..06b2be2d9fb6 100644 --- a/src/target/llvm/llvm_common.cc +++ b/src/target/llvm/llvm_common.cc @@ -142,7 +142,7 @@ std::unique_ptr GetLLVMTargetMachine(const Target& target, return nullptr; } - Integer llvm_opt_level = target->GetAttr("O").value_or(Integer(3)); + Integer llvm_opt_level = target->GetAttr("opt-level").value_or(Integer(3)); llvm::CodeGenOpt::Level llvm_opt; if (llvm_opt_level <= 0) { llvm_opt = llvm::CodeGenOpt::None; diff --git a/src/target/target_kind.cc b/src/target/target_kind.cc index bc05d96212f4..cd83d9b65a2c 100644 --- a/src/target/target_kind.cc +++ b/src/target/target_kind.cc @@ -230,7 +230,7 @@ TVM_REGISTER_TARGET_KIND("llvm", kDLCPU) .add_attr_option("fast-math-arcp") .add_attr_option("fast-math-contract") .add_attr_option("fast-math-reassoc") - .add_attr_option("O") + .add_attr_option("opt-level") .set_default_keys({"cpu"}); TVM_REGISTER_TARGET_KIND("c", kDLCPU)