Commit 1ddda541 authored by Diego Caballero's avatar Diego Caballero Committed by Scott Cyphers

[MLIR] Pass optimization level to MLIR ExecutionEngine (#3703)

An optional optimization level was added to ExecutionEngine, initialized
to None by default. This caused fast-isel to be used in LLVM CG,
overriding all the flags related to not using fast-isel.

In this PR we pass the right optimization level to ExecutionEngine.
parent 014e4fbd
...@@ -137,7 +137,7 @@ static llvm::cl::opt<std::string> ...@@ -137,7 +137,7 @@ static llvm::cl::opt<std::string>
createOp<op_name>(MLIRCompiler & compiler, const ngraph::Node* ngNode) createOp<op_name>(MLIRCompiler & compiler, const ngraph::Node* ngNode)
// Default optimization level. // Default optimization level.
unsigned MLIRCompiler::mlirOptLevel = 2; llvm::CodeGenOpt::Level MLIRCompiler::mlirOptLevel = llvm::CodeGenOpt::Level::Aggressive;
// Target machine will be properly initialized by `init_mlir`. // Target machine will be properly initialized by `init_mlir`.
std::unique_ptr<llvm::TargetMachine> MLIRCompiler::targetMachine; std::unique_ptr<llvm::TargetMachine> MLIRCompiler::targetMachine;
...@@ -184,8 +184,9 @@ void MLIRCompiler::init_mlir() ...@@ -184,8 +184,9 @@ void MLIRCompiler::init_mlir()
// Override default optimization level with macro value. // Override default optimization level with macro value.
if (char* optLevelStr = std::getenv("NGRAPH_MLIR_OPT_LEVEL")) if (char* optLevelStr = std::getenv("NGRAPH_MLIR_OPT_LEVEL"))
{ {
mlirOptLevel = std::stoi(optLevelStr); unsigned clOptLevel = std::stoi(optLevelStr);
NGRAPH_CHECK(mlirOptLevel >= 0 && mlirOptLevel <= 3, "Invalid optimization level"); NGRAPH_CHECK(clOptLevel >= 0 && clOptLevel <= 3, "Invalid optimization level");
mlirOptLevel = (llvm::CodeGenOpt::Level)clOptLevel;
} }
// Initialize LLVM targets and target machine for current host. // Initialize LLVM targets and target machine for current host.
...@@ -391,7 +392,7 @@ void MLIRCompiler::lowerNgDialect() ...@@ -391,7 +392,7 @@ void MLIRCompiler::lowerNgDialect()
// the default or user-provided optimization level. // the default or user-provided optimization level.
auto llvmTransformer = auto llvmTransformer =
mlir::makeOptimizingTransformer(mlirOptLevel, /*sizeLevel=*/0, targetMachine.get()); mlir::makeOptimizingTransformer(mlirOptLevel, /*sizeLevel=*/0, targetMachine.get());
auto maybeEngine = mlir::ExecutionEngine::create(m_module.get(), llvmTransformer); auto maybeEngine = mlir::ExecutionEngine::create(m_module.get(), llvmTransformer, mlirOptLevel);
NGRAPH_CHECK(maybeEngine, "failed to construct an execution engine"); NGRAPH_CHECK(maybeEngine, "failed to construct an execution engine");
m_engine = std::move(maybeEngine.get()); m_engine = std::move(maybeEngine.get());
} }
......
...@@ -167,8 +167,15 @@ namespace ngraph ...@@ -167,8 +167,15 @@ namespace ngraph
TensorToInfoMap m_tensorToValueMap; TensorToInfoMap m_tensorToValueMap;
static const MLIRCompOpMap opDispatcher; static const MLIRCompOpMap opDispatcher;
// Optimization level used by MLIR and LLVM compilers. // Optimization level used by MLIR and LLVM compilers. It's based on LLVM CG
static unsigned mlirOptLevel; // optimization levels:
// enum Level {
// None, // -O0
// Less, // -O1
// Default, // -O2, -Os
// Aggressive // -O3
// };
static llvm::CodeGenOpt::Level mlirOptLevel;
// LLVM target machine to be used by this MLIR compiler instance to retrieve // LLVM target machine to be used by this MLIR compiler instance to retrieve
// information about target features. // information about target features.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment