Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CIR][Lowering] Add an empty LoweringPrepare pass #236

Merged
merged 1 commit into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions clang/include/clang/CIR/Dialect/Passes.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ std::unique_ptr<Pass> createLifetimeCheckPass(ArrayRef<StringRef> remark,
clang::ASTContext *astCtx);
std::unique_ptr<Pass> createMergeCleanupsPass();
std::unique_ptr<Pass> createDropASTPass();
std::unique_ptr<Pass> createLoweringPreparePass();
std::unique_ptr<Pass> createLoweringPreparePass(clang::ASTContext *astCtx);

//===----------------------------------------------------------------------===//
// Registration
Expand Down
10 changes: 10 additions & 0 deletions clang/include/clang/CIR/Dialect/Passes.td
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,14 @@ def DropAST : Pass<"cir-drop-ast"> {
let dependentDialects = ["cir::CIRDialect"];
}

def LoweringPrepare : Pass<"cir-lowering-prepare"> {
let summary = "Preparation work before lowering to LLVM dialect";
let description = [{
This pass does preparation work for LLVM lowering. For example, it may
expand the global variable initialziation in a more ABI-friendly form.
}];
let constructor = "mlir::createLoweringPreparePass()";
let dependentDialects = ["cir::CIRDialect"];
}

#endif // MLIR_DIALECT_CIR_PASSES
2 changes: 2 additions & 0 deletions clang/lib/CIR/CodeGen/CIRPasses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ mlir::LogicalResult runCIRToCIRPasses(mlir::ModuleOp theModule,
pm.addPass(std::move(lifetimePass));
}

pm.addPass(mlir::createLoweringPreparePass(&astCtx));

// FIXME: once CIRCodenAction fixes emission other than CIR we
// need to run this right before dialect emission.
pm.addPass(mlir::createDropASTPass());
Expand Down
1 change: 1 addition & 0 deletions clang/lib/CIR/Dialect/Transforms/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
add_clang_library(MLIRCIRTransforms
LifetimeCheck.cpp
LoweringPrepare.cpp
MergeCleanups.cpp
DropAST.cpp

Expand Down
51 changes: 51 additions & 0 deletions clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//===- LoweringPrepare.cpp - pareparation work for LLVM lowering ----------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "PassDetail.h"
#include "clang/AST/ASTContext.h"
#include "clang/CIR/Dialect/IR/CIRDialect.h"
#include "clang/CIR/Dialect/Passes.h"

using namespace mlir;
using namespace cir;

namespace {
struct LoweringPreparePass : public LoweringPrepareBase<LoweringPreparePass> {
LoweringPreparePass() = default;
void runOnOperation() override;

///
/// AST related
/// -----------

clang::ASTContext *astCtx;
void setASTContext(clang::ASTContext *c) { astCtx = c; }

/// Tracks current module.
ModuleOp theModule;
};
} // namespace


void LoweringPreparePass::runOnOperation() {
assert(astCtx && "Missing ASTContext, please construct with the right ctor");
auto* op = getOperation();
if (isa<::mlir::ModuleOp>(op)) {
theModule = cast<::mlir::ModuleOp>(op);
}
}

std::unique_ptr<Pass> mlir::createLoweringPreparePass() {
return std::make_unique<LoweringPreparePass>();
}

std::unique_ptr<Pass> mlir::createLoweringPreparePass(clang::ASTContext *astCtx) {
auto pass = std::make_unique<LoweringPreparePass>();
pass->setASTContext(astCtx);
return std::move(pass);
}
2 changes: 2 additions & 0 deletions clang/test/CIR/mlirprint.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ int foo(void) {

// CIR: IR Dump After MergeCleanups (cir-merge-cleanups)
// CIR: cir.func @foo() -> !s32i
// CIR: IR Dump After LoweringPrepare (cir-lowering-prepare)
// CIR: cir.func @foo() -> !s32i
// CIR: IR Dump After DropAST (cir-drop-ast)
// CIR: cir.func @foo() -> !s32i
// LLVM: IR Dump After cir::direct::ConvertCIRToLLVMPass (cir-to-llvm)
Expand Down