-
Notifications
You must be signed in to change notification settings - Fork 157
[CIR][ThroughMLIR] Lower simple SwitchOp #1742
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
Open
terapines-osc-cir
wants to merge
1
commit into
llvm:main
Choose a base branch
from
Terapines:cir-switch
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
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
120 changes: 120 additions & 0 deletions
120
clang/lib/CIR/Lowering/ThroughMLIR/MLIRCoreDialectsLoweringPrepare.cpp
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,120 @@ | ||
//===- MLIRCoreDialectsLoweringPrepare.cpp - CIR lowering preparation -----===// | ||
// | ||
// 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 "mlir/IR/BuiltinOps.h" | ||
#include "mlir/IR/IRMapping.h" | ||
#include "mlir/Pass/Pass.h" | ||
#include "mlir/Transforms/DialectConversion.h" | ||
#include "clang/CIR/Dialect/Builder/CIRBaseBuilder.h" | ||
#include "clang/CIR/Dialect/IR/CIRDialect.h" | ||
|
||
using namespace llvm; | ||
using namespace cir; | ||
|
||
namespace cir { | ||
|
||
struct MLIRLoweringPrepare | ||
: public mlir::PassWrapper<MLIRLoweringPrepare, | ||
mlir::OperationPass<mlir::ModuleOp>> { | ||
// `scf.index_switch` requires that switch branches do not fall through. | ||
// We need to copy the next branch's body when the current `cir.case` does | ||
// not terminate with a break. | ||
void removeFallthrough(llvm::SmallVector<CaseOp> &cases); | ||
|
||
void runOnOp(mlir::Operation *op); | ||
void runOnOperation() final; | ||
|
||
StringRef getDescription() const override { | ||
return "Rewrite CIR module to be more 'scf' dialect-friendly"; | ||
} | ||
|
||
StringRef getArgument() const override { return "mlir-lowering-prepare"; } | ||
}; | ||
|
||
// `scf.index_switch` requires that switch branches do not fall through. | ||
// We need to copy the next branch's body when the current `cir.case` does not | ||
// terminate with a break. | ||
void MLIRLoweringPrepare::removeFallthrough(llvm::SmallVector<CaseOp> &cases) { | ||
CIRBaseBuilderTy builder(getContext()); | ||
// Note we enumerate in the reverse order, to facilitate the cloning. | ||
for (auto it = cases.rbegin(); it != cases.rend(); it++) { | ||
auto caseOp = *it; | ||
auto ®ion = caseOp.getRegion(); | ||
auto &lastBlock = region.back(); | ||
mlir::Operation &last = lastBlock.back(); | ||
if (isa<BreakOp>(last)) | ||
continue; | ||
|
||
// The last op must be a `cir.yield`. As it falls through, we copy the | ||
// previous case's body to this one. | ||
if (!isa<YieldOp>(last)) { | ||
caseOp->dump(); | ||
continue; | ||
} | ||
assert(isa<YieldOp>(last)); | ||
|
||
// If there's no previous case, we can simply change the yield into a break. | ||
if (it == cases.rbegin()) { | ||
builder.setInsertionPointAfter(&last); | ||
builder.create<BreakOp>(last.getLoc()); | ||
last.erase(); | ||
continue; | ||
} | ||
|
||
auto prevIt = it; | ||
--prevIt; | ||
CaseOp &prev = *prevIt; | ||
auto &prevRegion = prev.getRegion(); | ||
mlir::IRMapping mapping; | ||
builder.cloneRegionBefore(prevRegion, region, region.end()); | ||
|
||
// We inline the block to the end. | ||
// This is required because `scf.index_switch` expects that each of its | ||
// region contains a single block. | ||
mlir::Block *cloned = lastBlock.getNextNode(); | ||
for (auto it = cloned->begin(); it != cloned->end();) { | ||
auto next = it; | ||
next++; | ||
it->moveBefore(&last); | ||
it = next; | ||
} | ||
cloned->erase(); | ||
last.erase(); | ||
} | ||
} | ||
|
||
void MLIRLoweringPrepare::runOnOp(mlir::Operation *op) { | ||
if (auto switchOp = dyn_cast<SwitchOp>(op)) { | ||
llvm::SmallVector<CaseOp> cases; | ||
if (!switchOp.isSimpleForm(cases)) | ||
op->emitError("NYI"); | ||
|
||
removeFallthrough(cases); | ||
return; | ||
} | ||
op->emitError("unexpected op type"); | ||
} | ||
|
||
void MLIRLoweringPrepare::runOnOperation() { | ||
auto module = getOperation(); | ||
|
||
llvm::SmallVector<mlir::Operation *> opsToTransform; | ||
module->walk([&](mlir::Operation *op) { | ||
if (isa<SwitchOp>(op)) | ||
opsToTransform.push_back(op); | ||
}); | ||
|
||
for (auto *op : opsToTransform) | ||
runOnOp(op); | ||
} | ||
|
||
std::unique_ptr<mlir::Pass> createMLIRCoreDialectsLoweringPreparePass() { | ||
return std::make_unique<MLIRLoweringPrepare>(); | ||
} | ||
|
||
} // namespace cir |
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,50 @@ | ||
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -fno-clangir-direct-lowering -emit-mlir=core %s -o %t.mlir | ||
// RUN: FileCheck --input-file=%t.mlir %s | ||
|
||
void fallthrough() { | ||
int i = 0; | ||
switch (i) { | ||
case 2: | ||
i++; | ||
case 3: | ||
i++; | ||
break; | ||
case 8: | ||
i++; | ||
} | ||
|
||
// This should copy the `i++; break` in case 3 to case 2. | ||
|
||
// CHECK: memref.alloca_scope { | ||
// CHECK: %[[I:.+]] = memref.load %alloca[] | ||
// CHECK: %[[CASTED:.+]] = arith.index_cast %[[I]] | ||
// CHECK: scf.index_switch %[[CASTED]] | ||
// CHECK: case 2 { | ||
// CHECK: %[[I:.+]] = memref.load %alloca[] | ||
// CHECK: %[[ONE:.+]] = arith.constant 1 | ||
// CHECK: %[[ADD:.+]] = arith.addi %[[I]], %[[ONE]] | ||
// CHECK: memref.store %[[ADD]], %alloca[] | ||
// CHECK: %[[I:.+]] = memref.load %alloca[] | ||
// CHECK: %[[ONE:.+]] = arith.constant 1 | ||
// CHECK: %[[ADD:.+]] = arith.addi %[[I]], %[[ONE]] | ||
// CHECK: memref.store %[[ADD]], %alloca[] | ||
// CHECK: scf.yield | ||
// CHECK: } | ||
// CHECK: case 3 { | ||
// CHECK: %[[I:.+]] = memref.load %alloca[] | ||
// CHECK: %[[ONE:.+]] = arith.constant 1 | ||
// CHECK: %[[ADD:.+]] = arith.addi %[[I]], %[[ONE]] | ||
// CHECK: memref.store %[[ADD]], %alloca[] | ||
// CHECK: scf.yield | ||
// CHECK: } | ||
// CHECK: case 8 { | ||
// CHECK: %[[I:.+]] = memref.load %alloca[] | ||
// CHECK: %[[ONE:.+]] = arith.constant 1 | ||
// CHECK: %[[ADD:.+]] = arith.addi %[[I]], %[[ONE]] | ||
// CHECK: memref.store %[[ADD]], %alloca[] | ||
// CHECK: scf.yield | ||
// CHECK: } | ||
// CHECK: default { | ||
// CHECK: } | ||
// CHECK: } | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of
llvm_unreachable
you can use emitError here and everywhere else