Skip to content

Commit

Permalink
[CIR][NFC] Move LoweringPrepare into CIRGen (#1092)
Browse files Browse the repository at this point in the history
Move LP into CIRGen and give it a handle on the CIRGenModule. A lot of
code has been duplicated from CIRGen into cir/Dialect/Transforms in
order to let LP live there, but with more necessary CIRGen features
(e.g. EH scope and cleanups) going to be used in LP it doesn't make
sense to keep it separate. Add this patch that just refactors
LoweringPrepare into the CIRGen directory and give it a handle on the
CGM.
  • Loading branch information
lanza authored Nov 9, 2024
1 parent 5e91e4f commit 4463352
Show file tree
Hide file tree
Showing 26 changed files with 69 additions and 48 deletions.
1 change: 1 addition & 0 deletions clang/include/clang/CIR/CIRGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ class CIRGenerator : public clang::ASTConsumer {
std::unique_ptr<mlir::MLIRContext> takeContext() {
return std::move(mlirCtx);
};
clang::CIRGen::CIRGenModule &getCGM() { return *CGM; }

bool verifyModule();

Expand Down
17 changes: 10 additions & 7 deletions clang/include/clang/CIR/CIRToCIRPasses.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@

namespace clang {
class ASTContext;
}
namespace CIRGen {
class CIRGenModule;
} // namespace CIRGen
} // namespace clang

namespace mlir {
class MLIRContext;
Expand All @@ -30,12 +33,12 @@ namespace cir {
// Run set of cleanup/prepare/etc passes CIR <-> CIR.
mlir::LogicalResult runCIRToCIRPasses(
mlir::ModuleOp theModule, mlir::MLIRContext *mlirCtx,
clang::ASTContext &astCtx, bool enableVerifier, bool enableLifetime,
llvm::StringRef lifetimeOpts, bool enableIdiomRecognizer,
llvm::StringRef idiomRecognizerOpts, bool enableLibOpt,
llvm::StringRef libOptOpts, std::string &passOptParsingFailure,
bool enableCIRSimplify, bool flattenCIR, bool emitMLIR,
bool enableCallConvLowering, bool enableMem2reg);
clang::CIRGen::CIRGenModule &cgm, clang::ASTContext &astCtx,
bool enableVerifier, bool enableLifetime, llvm::StringRef lifetimeOpts,
bool enableIdiomRecognizer, llvm::StringRef idiomRecognizerOpts,
bool enableLibOpt, llvm::StringRef libOptOpts,
std::string &passOptParsingFailure, bool enableCIRSimplify, bool flattenCIR,
bool emitMLIR, bool enableCallConvLowering, bool enableMem2reg);

} // namespace cir

Expand Down
10 changes: 8 additions & 2 deletions clang/include/clang/CIR/Dialect/Passes.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@

namespace clang {
class ASTContext;
}
namespace CIRGen {
class CIRGenModule;
} // namespace CIRGen
} // namespace clang

namespace mlir {

std::unique_ptr<Pass> createLifetimeCheckPass();
Expand All @@ -31,7 +35,9 @@ std::unique_ptr<Pass> createCIRSimplifyPass();
std::unique_ptr<Pass> createDropASTPass();
std::unique_ptr<Pass> createSCFPreparePass();
std::unique_ptr<Pass> createLoweringPreparePass();
std::unique_ptr<Pass> createLoweringPreparePass(clang::ASTContext *astCtx);
std::unique_ptr<Pass>
createLoweringPreparePass(clang::ASTContext *astCtx,
clang::CIRGen::CIRGenModule &cgm);
std::unique_ptr<Pass> createIdiomRecognizerPass();
std::unique_ptr<Pass> createIdiomRecognizerPass(clang::ASTContext *astCtx);
std::unique_ptr<Pass> createLibOptPass();
Expand Down
4 changes: 2 additions & 2 deletions clang/include/clang/CIR/Dialect/Passes.td
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def SCFPrepare : Pass<"cir-mlir-scf-prepare"> {

def HoistAllocas : Pass<"cir-hoist-allocas"> {
let summary = "Hoist allocas to the entry of the function";
let description = [{
let description = [{
This pass hoist all non-dynamic allocas to the entry of the function.
This is helpful for later code generation.
}];
Expand All @@ -119,7 +119,7 @@ def HoistAllocas : Pass<"cir-hoist-allocas"> {

def FlattenCFG : Pass<"cir-flatten-cfg"> {
let summary = "Produces flatten cfg";
let description = [{
let description = [{
This pass transforms CIR and inline all the nested regions. Thus,
the next post condtions are met after the pass applied:
- there is not any nested region in a function body
Expand Down
15 changes: 8 additions & 7 deletions clang/lib/CIR/CodeGen/CIRPasses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "clang/AST/ASTContext.h"
#include "clang/CIR/Dialect/Passes.h"

#include "CIRGenModule.h"
#include "mlir/IR/BuiltinOps.h"
#include "mlir/Pass/Pass.h"
#include "mlir/Pass/PassManager.h"
Expand All @@ -24,12 +25,12 @@
namespace cir {
mlir::LogicalResult runCIRToCIRPasses(
mlir::ModuleOp theModule, mlir::MLIRContext *mlirCtx,
clang::ASTContext &astCtx, bool enableVerifier, bool enableLifetime,
llvm::StringRef lifetimeOpts, bool enableIdiomRecognizer,
llvm::StringRef idiomRecognizerOpts, bool enableLibOpt,
llvm::StringRef libOptOpts, std::string &passOptParsingFailure,
bool enableCIRSimplify, bool flattenCIR, bool emitMLIR,
bool enableCallConvLowering, bool enableMem2Reg) {
clang::CIRGen::CIRGenModule &cgm, clang::ASTContext &astCtx,
bool enableVerifier, bool enableLifetime, llvm::StringRef lifetimeOpts,
bool enableIdiomRecognizer, llvm::StringRef idiomRecognizerOpts,
bool enableLibOpt, llvm::StringRef libOptOpts,
std::string &passOptParsingFailure, bool enableCIRSimplify, bool flattenCIR,
bool emitMLIR, bool enableCallConvLowering, bool enableMem2Reg) {

llvm::TimeTraceScope scope("CIR To CIR Passes");

Expand Down Expand Up @@ -73,7 +74,7 @@ mlir::LogicalResult runCIRToCIRPasses(
if (enableCIRSimplify)
pm.addPass(mlir::createCIRSimplifyPass());

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

if (flattenCIR || enableMem2Reg)
mlir::populateCIRPreLoweringPasses(pm, enableCallConvLowering);
Expand Down
1 change: 1 addition & 0 deletions clang/lib/CIR/CodeGen/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ add_clang_library(clangCIR
CIRPasses.cpp
CIRRecordLayoutBuilder.cpp
ConstantInitBuilder.cpp
LoweringPrepare.cpp
TargetInfo.cpp

DEPENDS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
//
//===----------------------------------------------------------------------===//

#include "LoweringPrepareCXXABI.h"
#include "PassDetail.h"
#include "CIRGenModule.h"

#include "mlir/IR/BuiltinAttributes.h"
#include "mlir/IR/Region.h"
#include "clang/AST/ASTContext.h"
Expand All @@ -19,6 +19,8 @@
#include "clang/CIR/Dialect/IR/CIRDataLayout.h"
#include "clang/CIR/Dialect/IR/CIRDialect.h"
#include "clang/CIR/Dialect/Passes.h"
#include "clang/CIR/Dialect/Transforms/LoweringPrepareCXXABI.h"
#include "clang/CIR/Dialect/Transforms/PassDetail.h"
#include "clang/CIR/Interfaces/ASTAttrInterfaces.h"
#include "llvm/ADT/APFloat.h"
#include "llvm/ADT/SmallVector.h"
Expand Down Expand Up @@ -121,6 +123,7 @@ struct LoweringPreparePass : public LoweringPrepareBase<LoweringPreparePass> {

clang::ASTContext *astCtx;
std::shared_ptr<cir::LoweringPrepareCXXABI> cxxABI;
clang::CIRGen::CIRGenModule *cgm;

void setASTContext(clang::ASTContext *c) {
astCtx = c;
Expand All @@ -147,6 +150,8 @@ struct LoweringPreparePass : public LoweringPrepareBase<LoweringPreparePass> {
}
}

void setCGM(clang::CIRGen::CIRGenModule &cgm) { this->cgm = &cgm; }

/// Tracks current module.
ModuleOp theModule;

Expand Down Expand Up @@ -1205,8 +1210,10 @@ std::unique_ptr<Pass> mlir::createLoweringPreparePass() {
}

std::unique_ptr<Pass>
mlir::createLoweringPreparePass(clang::ASTContext *astCtx) {
mlir::createLoweringPreparePass(clang::ASTContext *astCtx,
clang::CIRGen::CIRGenModule &cgm) {
auto pass = std::make_unique<LoweringPreparePass>();
pass->setASTContext(astCtx);
pass->setCGM(cgm);
return std::move(pass);
}
2 changes: 1 addition & 1 deletion clang/lib/CIR/Dialect/Transforms/CIRCanonicalize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
//
//===----------------------------------------------------------------------===//

#include "PassDetail.h"
#include "mlir/Dialect/Func/IR/FuncOps.h"
#include "mlir/IR/Block.h"
#include "mlir/IR/Operation.h"
Expand All @@ -16,6 +15,7 @@
#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
#include "clang/CIR/Dialect/IR/CIRDialect.h"
#include "clang/CIR/Dialect/Passes.h"
#include "clang/CIR/Dialect/Transforms/PassDetail.h"

using namespace mlir;
using namespace cir;
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/CIR/Dialect/Transforms/CIRSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
//
//===----------------------------------------------------------------------===//

#include "PassDetail.h"
#include "mlir/Dialect/Func/IR/FuncOps.h"
#include "mlir/IR/Block.h"
#include "mlir/IR/Operation.h"
Expand All @@ -16,6 +15,7 @@
#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
#include "clang/CIR/Dialect/IR/CIRDialect.h"
#include "clang/CIR/Dialect/Passes.h"
#include "clang/CIR/Dialect/Transforms/PassDetail.h"
#include "llvm/ADT/SmallVector.h"

using namespace mlir;
Expand Down
1 change: 0 additions & 1 deletion clang/lib/CIR/Dialect/Transforms/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ add_subdirectory(TargetLowering)

add_clang_library(MLIRCIRTransforms
LifetimeCheck.cpp
LoweringPrepare.cpp
CIRCanonicalize.cpp
CIRSimplify.cpp
DropAST.cpp
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/CIR/Dialect/Transforms/DropAST.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@

#include "clang/CIR/Dialect/Passes.h"

#include "PassDetail.h"
#include "mlir/Dialect/Func/IR/FuncOps.h"
#include "clang/AST/ASTContext.h"
#include "clang/CIR/Dialect/IR/CIRDialect.h"
#include "clang/CIR/Dialect/Transforms/PassDetail.h"

#include "llvm/ADT/SetOperations.h"
#include "llvm/ADT/SmallSet.h"
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/CIR/Dialect/Transforms/FlattenCFG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
// function region.
//
//===----------------------------------------------------------------------===//
#include "PassDetail.h"
#include "mlir/Dialect/Func/IR/FuncOps.h"
#include "mlir/IR/PatternMatch.h"
#include "mlir/Support/LogicalResult.h"
#include "mlir/Transforms/DialectConversion.h"
#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
#include "clang/CIR/Dialect/IR/CIRDialect.h"
#include "clang/CIR/Dialect/Passes.h"
#include "clang/CIR/Dialect/Transforms/PassDetail.h"

using namespace mlir;
using namespace cir;
Expand Down
4 changes: 2 additions & 2 deletions clang/lib/CIR/Dialect/Transforms/GotoSolver.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#include "PassDetail.h"
#include "mlir/Dialect/Func/IR/FuncOps.h"
#include "mlir/IR/PatternMatch.h"
#include "mlir/Support/LogicalResult.h"
#include "mlir/Transforms/DialectConversion.h"
#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
#include "clang/CIR/Dialect/IR/CIRDialect.h"
#include "clang/CIR/Dialect/Passes.h"
#include "clang/CIR/Dialect/Transforms/PassDetail.h"

#include "llvm/Support/TimeProfiler.h"

Expand Down Expand Up @@ -54,4 +54,4 @@ void GotoSolverPass::runOnOperation() {

std::unique_ptr<Pass> mlir::createGotoSolverPass() {
return std::make_unique<GotoSolverPass>();
}
}
4 changes: 2 additions & 2 deletions clang/lib/CIR/Dialect/Transforms/HoistAllocas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
//
//===----------------------------------------------------------------------===//

#include "PassDetail.h"
#include "mlir/Dialect/Func/IR/FuncOps.h"
#include "mlir/IR/PatternMatch.h"
#include "mlir/Support/LogicalResult.h"
#include "mlir/Transforms/DialectConversion.h"
#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
#include "clang/CIR/Dialect/IR/CIRDialect.h"
#include "clang/CIR/Dialect/Passes.h"
#include "clang/CIR/Dialect/Transforms/PassDetail.h"

#include "llvm/Support/TimeProfiler.h"

Expand Down Expand Up @@ -62,4 +62,4 @@ void HoistAllocasPass::runOnOperation() {

std::unique_ptr<Pass> mlir::createHoistAllocasPass() {
return std::make_unique<HoistAllocasPass>();
}
}
2 changes: 1 addition & 1 deletion clang/lib/CIR/Dialect/Transforms/IdiomRecognizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
//
//===----------------------------------------------------------------------===//

#include "PassDetail.h"
#include "mlir/Dialect/Func/IR/FuncOps.h"
#include "mlir/IR/BuiltinAttributes.h"
#include "mlir/IR/Region.h"
Expand All @@ -16,6 +15,7 @@
#include "clang/CIR/Dialect/Builder/CIRBaseBuilder.h"
#include "clang/CIR/Dialect/IR/CIRDialect.h"
#include "clang/CIR/Dialect/Passes.h"
#include "clang/CIR/Dialect/Transforms/PassDetail.h"
#include "clang/CIR/Interfaces/ASTAttrInterfaces.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringMap.h"
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/CIR/Dialect/Transforms/LibOpt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
//
//===----------------------------------------------------------------------===//

#include "PassDetail.h"
#include "mlir/Dialect/Func/IR/FuncOps.h"
#include "mlir/IR/BuiltinAttributes.h"
#include "mlir/IR/Region.h"
Expand All @@ -16,6 +15,7 @@
#include "clang/CIR/Dialect/Builder/CIRBaseBuilder.h"
#include "clang/CIR/Dialect/IR/CIRDialect.h"
#include "clang/CIR/Dialect/Passes.h"
#include "clang/CIR/Dialect/Transforms/PassDetail.h"
#include "clang/CIR/Interfaces/ASTAttrInterfaces.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringMap.h"
Expand Down
3 changes: 1 addition & 2 deletions clang/lib/CIR/Dialect/Transforms/LifetimeCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@
//
//===----------------------------------------------------------------------===//

#include "PassDetail.h"

#include "clang/AST/ASTContext.h"
#include "clang/AST/Attr.h"
#include "clang/AST/DeclCXX.h"
#include "clang/AST/DeclTemplate.h"
#include "clang/CIR/Dialect/IR/CIRAttrs.h"
#include "clang/CIR/Dialect/IR/CIRDialect.h"
#include "clang/CIR/Dialect/Passes.h"
#include "clang/CIR/Dialect/Transforms/PassDetail.h"

#include "clang/CIR/Interfaces/CIRLoopOpInterface.h"
#include "llvm/ADT/SetOperations.h"
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/CIR/Dialect/Transforms/SCFPrepare.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
//
//===----------------------------------------------------------------------===//

#include "PassDetail.h"
#include "mlir/IR/PatternMatch.h"
#include "mlir/Support/LogicalResult.h"
#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
#include "clang/CIR/Dialect/IR/CIRDialect.h"
#include "clang/CIR/Dialect/Passes.h"
#include "clang/CIR/Dialect/Transforms/PassDetail.h"

using namespace mlir;
using namespace cir;
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/CIR/Dialect/Transforms/StdHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
//
//===----------------------------------------------------------------------===//

#include "PassDetail.h"
#include "mlir/IR/BuiltinAttributes.h"
#include "mlir/IR/Region.h"
#include "clang/AST/ASTContext.h"
#include "clang/Basic/Module.h"
#include "clang/CIR/Dialect/Builder/CIRBaseBuilder.h"
#include "clang/CIR/Dialect/IR/CIRDialect.h"
#include "clang/CIR/Dialect/Passes.h"
#include "clang/CIR/Dialect/Transforms/PassDetail.h"
#include "clang/CIR/Interfaces/ASTAttrInterfaces.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringMap.h"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@

// TODO(cir): Refactor this to follow some level of codegen parity.

#include "../LoweringPrepareItaniumCXXABI.h"
#include "clang/AST/CharUnits.h"
#include "clang/CIR/Dialect/IR/CIRDataLayout.h"
#include "clang/CIR/Dialect/IR/CIRTypes.h"
#include "clang/CIR/Dialect/Transforms/LoweringPrepareItaniumCXXABI.h"
#include "clang/CIR/MissingFeatures.h"

#include <assert.h>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

// TODO(cir): Refactor this to follow some level of codegen parity.

#include "../LoweringPrepareItaniumCXXABI.h"
#include "clang/CIR/Dialect/Transforms/LoweringPrepareItaniumCXXABI.h"
#include "mlir/IR/BuiltinAttributes.h"
#include "mlir/IR/Value.h"
#include "mlir/IR/ValueRange.h"
Expand Down
Loading

0 comments on commit 4463352

Please sign in to comment.