|
| 1 | +//===- LoweringPrepare.cpp - pareparation work for LLVM lowering ----------===// |
| 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 | +#include "PassDetail.h" |
| 10 | +#include "clang/AST/ASTContext.h" |
| 11 | +#include "clang/CIR/Dialect/IR/CIRDialect.h" |
| 12 | +#include "clang/CIR/Dialect/Passes.h" |
| 13 | + |
| 14 | +using namespace mlir; |
| 15 | +using namespace cir; |
| 16 | + |
| 17 | +namespace { |
| 18 | +struct LoweringPreparePass : public LoweringPrepareBase<LoweringPreparePass> { |
| 19 | + LoweringPreparePass() = default; |
| 20 | + void runOnOperation() override; |
| 21 | + |
| 22 | + /// |
| 23 | + /// AST related |
| 24 | + /// ----------- |
| 25 | + |
| 26 | + clang::ASTContext *astCtx; |
| 27 | + void setASTContext(clang::ASTContext *c) { astCtx = c; } |
| 28 | + |
| 29 | + /// Tracks current module. |
| 30 | + ModuleOp theModule; |
| 31 | +}; |
| 32 | +} // namespace |
| 33 | + |
| 34 | + |
| 35 | +void LoweringPreparePass::runOnOperation() { |
| 36 | + assert(astCtx && "Missing ASTContext, please construct with the right ctor"); |
| 37 | + auto* op = getOperation(); |
| 38 | + if (isa<::mlir::ModuleOp>(op)) { |
| 39 | + theModule = cast<::mlir::ModuleOp>(op); |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +std::unique_ptr<Pass> mlir::createLoweringPreparePass() { |
| 44 | + return std::make_unique<LoweringPreparePass>(); |
| 45 | +} |
| 46 | + |
| 47 | +std::unique_ptr<Pass> mlir::createLoweringPreparePass(clang::ASTContext *astCtx) { |
| 48 | + auto pass = std::make_unique<LoweringPreparePass>(); |
| 49 | + pass->setASTContext(astCtx); |
| 50 | + return std::move(pass); |
| 51 | +} |
0 commit comments