1//===----------------------------------------------------------------------===//
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// Similar to MLIR/LLVM's "opt" tools but also deals with analysis and custom
10// arguments. TODO: this is basically a copy from MlirOptMain.cpp, but capable
11// of module emission as specified by the user.
12//
13//===----------------------------------------------------------------------===//
14
15#include "mlir/Conversion/ReconcileUnrealizedCasts/ReconcileUnrealizedCasts.h"
16#include "mlir/Dialect/DLTI/DLTI.h"
17#include "mlir/Dialect/Func/IR/FuncOps.h"
18#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
19#include "mlir/Dialect/MemRef/IR/MemRef.h"
20#include "mlir/InitAllPasses.h"
21#include "mlir/Pass/PassManager.h"
22#include "mlir/Pass/PassOptions.h"
23#include "mlir/Pass/PassRegistry.h"
24#include "mlir/Tools/mlir-opt/MlirOptMain.h"
25#include "clang/CIR/Dialect/IR/CIRDialect.h"
26#include "clang/CIR/Dialect/Passes.h"
27#include "clang/CIR/Passes.h"
28
29struct CIRToLLVMPipelineOptions
30 : public mlir::PassPipelineOptions<CIRToLLVMPipelineOptions> {};
31
32int main(int argc, char **argv) {
33 // TODO: register needed MLIR passes for CIR?
34 mlir::DialectRegistry registry;
35 registry.insert<mlir::BuiltinDialect, cir::CIRDialect,
36 mlir::memref::MemRefDialect, mlir::LLVM::LLVMDialect,
37 mlir::DLTIDialect>();
38
39 ::mlir::registerPass([]() -> std::unique_ptr<::mlir::Pass> {
40 return mlir::createCIRCanonicalizePass();
41 });
42 ::mlir::registerPass([]() -> std::unique_ptr<::mlir::Pass> {
43 return mlir::createCIRSimplifyPass();
44 });
45
46 mlir::PassPipelineRegistration<CIRToLLVMPipelineOptions> pipeline(
47 "cir-to-llvm", "",
48 [](mlir::OpPassManager &pm, const CIRToLLVMPipelineOptions &options) {
49 cir::direct::populateCIRToLLVMPasses(pm);
50 });
51
52 ::mlir::registerPass([]() -> std::unique_ptr<::mlir::Pass> {
53 return mlir::createCIRFlattenCFGPass();
54 });
55
56 ::mlir::registerPass([]() -> std::unique_ptr<::mlir::Pass> {
57 return mlir::createHoistAllocasPass();
58 });
59
60 mlir::registerTransformsPasses();
61
62 return mlir::asMainReturnCode(MlirOptMain(
63 argc, argv, "Clang IR analysis and optimization tool\n", registry));
64}
65

source code of clang/tools/cir-opt/cir-opt.cpp