MLIR 23.0.0git
OptUtils.cpp
Go to the documentation of this file.
1//===- OptUtils.cpp - MLIR Execution Engine optimization pass utilities ---===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the utility functions to trigger LLVM optimizations from
10// MLIR Execution Engine.
11//
12//===----------------------------------------------------------------------===//
13
15
16#include "llvm/Analysis/TargetTransformInfo.h"
17#include "llvm/IR/Module.h"
18#include "llvm/Passes/OptimizationLevel.h"
19#include "llvm/Passes/PassBuilder.h"
20#include "llvm/Support/Error.h"
21#include "llvm/Support/FormatVariadic.h"
22#include "llvm/Target/TargetMachine.h"
23#include <optional>
24
25using namespace llvm;
26
27static std::optional<OptimizationLevel> mapToLevel(unsigned optLevel,
28 unsigned sizeLevel) {
29 switch (optLevel) {
30 case 0:
31 return OptimizationLevel::O0;
32
33 case 1:
34 return OptimizationLevel::O1;
35
36 case 2:
37 return OptimizationLevel::O2;
38
39 case 3:
40 return OptimizationLevel::O3;
41 }
42 return std::nullopt;
43}
44// Create and return a lambda that uses LLVM pass manager builder to set up
45// optimizations based on the given level.
46std::function<Error(Module *)>
47mlir::makeOptimizingTransformer(unsigned optLevel, unsigned sizeLevel,
48 TargetMachine *targetMachine) {
49 return [optLevel, sizeLevel, targetMachine](Module *m) -> Error {
50 std::optional<OptimizationLevel> ol = mapToLevel(optLevel, sizeLevel);
51 if (!ol) {
52 return make_error<StringError>(
53 formatv("invalid optimization/size level {0}/{1}", optLevel,
54 sizeLevel)
55 .str(),
56 inconvertibleErrorCode());
57 }
58 LoopAnalysisManager lam;
59 FunctionAnalysisManager fam;
60 CGSCCAnalysisManager cgam;
61 ModuleAnalysisManager mam;
62
63 PipelineTuningOptions tuningOptions;
64 tuningOptions.LoopUnrolling = true;
65 tuningOptions.LoopInterleaving = true;
66 tuningOptions.LoopVectorization = true;
67 tuningOptions.SLPVectorization = true;
68
69 PassBuilder pb(targetMachine, tuningOptions);
70
71 pb.registerModuleAnalyses(mam);
72 pb.registerCGSCCAnalyses(cgam);
73 pb.registerFunctionAnalyses(fam);
74 pb.registerLoopAnalyses(lam);
75 pb.crossRegisterProxies(lam, fam, cgam, mam);
76
77 ModulePassManager mpm;
78 mpm.addPass(pb.buildPerModuleDefaultPipeline(*ol));
79 mpm.run(*m, mam);
80 return Error::success();
81 };
82}
@ Error
static std::optional< OptimizationLevel > mapToLevel(unsigned optLevel, unsigned sizeLevel)
Definition OptUtils.cpp:27
The OpAsmOpInterface, see OpAsmInterface.td for more details.
Definition CallGraph.h:229
std::function< llvm::Error(llvm::Module *)> makeOptimizingTransformer(unsigned optLevel, unsigned sizeLevel, llvm::TargetMachine *targetMachine)
Create a module transformer function for MLIR ExecutionEngine that runs LLVM IR passes corresponding ...