-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[CIR] Upstream initial support for CIR flattening #130648
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,7 @@ | ||
add_subdirectory(IR) | ||
|
||
set(LLVM_TARGET_DEFINITIONS Passes.td) | ||
mlir_tablegen(Passes.h.inc -gen-pass-decls -name CIR) | ||
mlir_tablegen(Passes.capi.h.inc -gen-pass-capi-header --prefix CIR) | ||
mlir_tablegen(Passes.capi.cpp.inc -gen-pass-capi-impl --prefix CIR) | ||
add_public_tablegen_target(MLIRCIRPassIncGen) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
//===- Passes.h - CIR pass entry points -------------------------*- C++ -*-===// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This header file defines prototypes that expose pass constructors. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef CLANG_CIR_DIALECT_PASSES_H | ||
#define CLANG_CIR_DIALECT_PASSES_H | ||
|
||
#include "mlir/Pass/Pass.h" | ||
|
||
namespace clang { | ||
class ASTContext; | ||
} | ||
namespace mlir { | ||
|
||
std::unique_ptr<Pass> createCIRFlattenCFGPass(); | ||
|
||
void populateCIRPreLoweringPasses(mlir::OpPassManager &pm); | ||
|
||
//===----------------------------------------------------------------------===// | ||
// Registration | ||
//===----------------------------------------------------------------------===// | ||
|
||
void registerCIRDialectTranslation(mlir::MLIRContext &context); | ||
|
||
/// Generate the code for registering passes. | ||
#define GEN_PASS_REGISTRATION | ||
#include "clang/CIR/Dialect/Passes.h.inc" | ||
|
||
} // namespace mlir | ||
|
||
#endif // CLANG_CIR_DIALECT_PASSES_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef CLANG_CIR_DIALECT_PASSES_TD | ||
#define CLANG_CIR_DIALECT_PASSES_TD | ||
|
||
include "mlir/Pass/PassBase.td" | ||
|
||
def CIRFlattenCFG : Pass<"cir-flatten-cfg"> { | ||
let summary = "Produces flatten CFG"; | ||
let description = [{ | ||
This pass transforms CIR by inlining all the nested regions. Thus, | ||
the following conditions are true after the pass applied: | ||
- there are no nested regions in any function body | ||
- all the blocks in a function belong to the parent region | ||
In other words, this pass removes such CIR operations like IfOp, LoopOp, | ||
ScopeOp and etc. and produces a flat CIR. | ||
}]; | ||
let constructor = "mlir::createCIRFlattenCFGPass()"; | ||
let dependentDialects = ["cir::CIRDialect"]; | ||
} | ||
|
||
#endif // CLANG_CIR_DIALECT_PASSES_TD |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
add_subdirectory(IR) | ||
add_subdirectory(Transforms) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
add_clang_library(MLIRCIRTransforms | ||
FlattenCFG.cpp | ||
|
||
DEPENDS | ||
MLIRCIRPassIncGen | ||
|
||
LINK_LIBS PUBLIC | ||
clangAST | ||
clangBasic | ||
|
||
MLIRAnalysis | ||
MLIRIR | ||
MLIRPass | ||
MLIRTransformUtils | ||
|
||
MLIRCIR | ||
MLIRCIRInterfaces | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This file implements pass that inlines CIR operations regions into the parent | ||
// 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/MissingFeatures.h" | ||
|
||
using namespace mlir; | ||
using namespace cir; | ||
|
||
namespace { | ||
|
||
struct CIRFlattenCFGPass : public CIRFlattenCFGBase<CIRFlattenCFGPass> { | ||
|
||
CIRFlattenCFGPass() = default; | ||
void runOnOperation() override; | ||
erichkeane marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}; | ||
|
||
class CIRScopeOpFlattening : public mlir::OpRewritePattern<cir::ScopeOp> { | ||
public: | ||
using OpRewritePattern<cir::ScopeOp>::OpRewritePattern; | ||
|
||
mlir::LogicalResult | ||
matchAndRewrite(cir::ScopeOp scopeOp, | ||
mlir::PatternRewriter &rewriter) const override { | ||
mlir::OpBuilder::InsertionGuard guard(rewriter); | ||
mlir::Location loc = scopeOp.getLoc(); | ||
|
||
// Empty scope: just remove it. | ||
// TODO: Remove this logic once CIR uses MLIR infrastructure to remove | ||
// trivially dead operations. MLIR canonicalizer is too aggressive and we | ||
// need to either (a) make sure all our ops model all side-effects and/or | ||
// (b) have more options in the canonicalizer in MLIR to temper | ||
// aggressiveness level. | ||
if (scopeOp.isEmpty()) { | ||
rewriter.eraseOp(scopeOp); | ||
return mlir::success(); | ||
} | ||
|
||
// Split the current block before the ScopeOp to create the inlining | ||
// point. | ||
mlir::Block *currentBlock = rewriter.getInsertionBlock(); | ||
mlir::Block *continueBlock = | ||
rewriter.splitBlock(currentBlock, rewriter.getInsertionPoint()); | ||
if (scopeOp.getNumResults() > 0) | ||
continueBlock->addArguments(scopeOp.getResultTypes(), loc); | ||
|
||
// Inline body region. | ||
mlir::Block *beforeBody = &scopeOp.getScopeRegion().front(); | ||
mlir::Block *afterBody = &scopeOp.getScopeRegion().back(); | ||
rewriter.inlineRegionBefore(scopeOp.getScopeRegion(), continueBlock); | ||
|
||
// Save stack and then branch into the body of the region. | ||
rewriter.setInsertionPointToEnd(currentBlock); | ||
assert(!cir::MissingFeatures::stackSaveOp()); | ||
rewriter.create<cir::BrOp>(loc, mlir::ValueRange(), beforeBody); | ||
|
||
// Replace the scopeop return with a branch that jumps out of the body. | ||
// Stack restore before leaving the body region. | ||
rewriter.setInsertionPointToEnd(afterBody); | ||
if (auto yieldOp = dyn_cast<cir::YieldOp>(afterBody->getTerminator())) { | ||
rewriter.replaceOpWithNewOp<cir::BrOp>(yieldOp, yieldOp.getArgs(), | ||
continueBlock); | ||
} | ||
|
||
// Replace the op with values return from the body region. | ||
rewriter.replaceOp(scopeOp, continueBlock->getArguments()); | ||
|
||
return mlir::success(); | ||
} | ||
}; | ||
|
||
void populateFlattenCFGPatterns(RewritePatternSet &patterns) { | ||
patterns.add<CIRScopeOpFlattening>(patterns.getContext()); | ||
} | ||
|
||
void CIRFlattenCFGPass::runOnOperation() { | ||
RewritePatternSet patterns(&getContext()); | ||
populateFlattenCFGPatterns(patterns); | ||
|
||
// Collect operations to apply patterns. | ||
llvm::SmallVector<Operation *, 16> ops; | ||
getOperation()->walk<mlir::WalkOrder::PostOrder>([&](Operation *op) { | ||
if (isa<ScopeOp>(op)) | ||
ops.push_back(op); | ||
}); | ||
|
||
// Apply patterns. | ||
if (applyOpPatternsGreedily(ops, std::move(patterns)).failed()) | ||
signalPassFailure(); | ||
} | ||
|
||
} // namespace | ||
|
||
namespace mlir { | ||
|
||
std::unique_ptr<Pass> createCIRFlattenCFGPass() { | ||
return std::make_unique<CIRFlattenCFGPass>(); | ||
} | ||
|
||
} // namespace mlir |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef CIR_DIALECT_TRANSFORMS_PASSDETAIL_H | ||
#define CIR_DIALECT_TRANSFORMS_PASSDETAIL_H | ||
|
||
#include "mlir/IR/Dialect.h" | ||
#include "mlir/Pass/Pass.h" | ||
|
||
namespace cir { | ||
class CIRDialect; | ||
} // namespace cir | ||
|
||
namespace mlir { | ||
// Forward declaration from Dialect.h | ||
template <typename ConcreteDialect> | ||
void registerDialect(DialectRegistry ®istry); | ||
|
||
#define GEN_PASS_CLASSES | ||
#include "clang/CIR/Dialect/Passes.h.inc" | ||
|
||
} // namespace mlir | ||
|
||
#endif // CIR_DIALECT_TRANSFORMS_PASSDETAIL_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This file implements machinery for any CIR <-> CIR passes used by clang. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
// #include "clang/AST/ASTContext.h" | ||
#include "clang/CIR/Dialect/Passes.h" | ||
|
||
#include "mlir/Pass/PassManager.h" | ||
|
||
namespace mlir { | ||
|
||
void populateCIRPreLoweringPasses(OpPassManager &pm) { | ||
pm.addPass(createCIRFlattenCFGPass()); | ||
} | ||
|
||
} // namespace mlir |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,20 @@ | ||
set(LLVM_LINK_COMPONENTS | ||
Core | ||
Support | ||
) | ||
|
||
get_property(dialect_libs GLOBAL PROPERTY MLIR_DIALECT_LIBS) | ||
|
||
add_clang_library(clangCIRLoweringCommon | ||
CIRPasses.cpp | ||
|
||
LINK_LIBS | ||
clangCIR | ||
${dialect_libs} | ||
MLIRCIR | ||
MLIRCIRTransforms | ||
MLIRTransforms | ||
MLIRSupport | ||
) | ||
|
||
add_subdirectory(DirectToLLVM) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.