Skip to content

Commit 583a252

Browse files
committed
Address review feedback
1 parent f75c9fd commit 583a252

File tree

4 files changed

+24
-23
lines changed

4 files changed

+24
-23
lines changed

clang/include/clang/CIR/Dialect/Passes.td

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ include "mlir/Pass/PassBase.td"
1414
def CIRFlattenCFG : Pass<"cir-flatten-cfg"> {
1515
let summary = "Produces flatten CFG";
1616
let description = [{
17-
This pass transforms CIR and inline all the nested regions. Thus,
18-
the next post condtions are met after the pass applied:
19-
- there is not any nested region in a function body
17+
This pass transforms CIR by inlining all the nested regions. Thus,
18+
the following condtions are true after the pass applied:
19+
- there are no nested regions in any function body
2020
- all the blocks in a function belong to the parent region
2121
In other words, this pass removes such CIR operations like IfOp, LoopOp,
2222
ScopeOp and etc. and produces a flat CIR.

clang/lib/CIR/Dialect/Transforms/FlattenCFG.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@ class CIRScopeOpFlattening : public mlir::OpRewritePattern<cir::ScopeOp> {
4444

4545
// Empty scope: just remove it.
4646
// TODO: Remove this logic once CIR uses MLIR infrastructure to remove
47-
// trivially dead operations
47+
// trivially dead operations. MLIR canonicalizer is too aggressive and we
48+
// need to either (a) make sure all our ops model all side-effects and/or
49+
// (b) have more options in the canonicalizer in MLIR to temper
50+
// aggressiveness level.
4851
if (scopeOp.isEmpty()) {
4952
rewriter.eraseOp(scopeOp);
5053
return mlir::success();

clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "llvm/ADT/TypeSwitch.h"
3434
#include "llvm/IR/Module.h"
3535
#include "llvm/Support/Error.h"
36+
#include "llvm/Support/ErrorHandling.h"
3637
#include "llvm/Support/TimeProfiler.h"
3738

3839
using namespace cir;
@@ -138,6 +139,7 @@ mlir::LLVM::Linkage convertLinkage(cir::GlobalLinkageKind linkage) {
138139
case CIR::WeakODRLinkage:
139140
return LLVM::WeakODR;
140141
};
142+
llvm_unreachable("Unknown CIR linkage type");
141143
}
142144

143145
class CIRAttrToValue {
@@ -606,10 +608,15 @@ static void prepareTypeConverter(mlir::LLVMTypeConverter &converter,
606608
});
607609
}
608610

609-
// The unreachable code is not lowered by applyPartialConversion function
610-
// since it traverses blocks in the dominance order. At the same time we
611-
// do need to lower such code - otherwise verification errors occur.
612-
// For instance, the next CIR code:
611+
// The applyPartialConversion function traverses blocks in the dominance order,
612+
// so it does not lower and operations that are not reachachable from the
613+
// operations passed in as arguments. Since we do need to lower such code in
614+
// order to avoid verification errors occur, we cannot just pass the module op
615+
// to applyPartialConversion. We must build a set of unreachable ops and
616+
// explicitly add them, along with the module, to the vector we pass to
617+
// applyPartialConversion.
618+
//
619+
// For instance, this CIR code:
613620
//
614621
// cir.func @foo(%arg0: !s32i) -> !s32i {
615622
// %4 = cir.cast(int_to_bool, %arg0 : !s32i), !cir.bool
@@ -620,18 +627,18 @@ static void prepareTypeConverter(mlir::LLVMTypeConverter &converter,
620627
// %5 = cir.const #cir.int<0> : !s32i
621628
// cir.return %5 : !s32i
622629
// }
623-
// cir.return %arg0 : !s32i
630+
// cir.return %arg0 : !s32i
624631
// }
625632
//
626633
// contains an unreachable return operation (the last one). After the flattening
627-
// pass it will be placed into the unreachable block. And the possible error
634+
// pass it will be placed into the unreachable block. The possible error
628635
// after the lowering pass is: error: 'cir.return' op expects parent op to be
629636
// one of 'cir.func, cir.scope, cir.if ... The reason that this operation was
630637
// not lowered and the new parent is llvm.func.
631638
//
632-
// In the future we may want to get rid of this function and use DCE pass or
633-
// something similar. But now we need to guarantee the absence of the dialect
634-
// verification errors.
639+
// In the future we may want to get rid of this function and use a DCE pass or
640+
// something similar. But for now we need to guarantee the absence of the
641+
// dialect verification errors.
635642
static void collectUnreachable(mlir::Operation *parent,
636643
llvm::SmallVector<mlir::Operation *> &ops) {
637644

clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVMIR.cpp

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,7 @@ class CIRDialectLLVMIRTranslationInterface
3333
: public mlir::LLVMTranslationDialectInterface {
3434
public:
3535
using LLVMTranslationDialectInterface::LLVMTranslationDialectInterface;
36-
#if 0
37-
/// Any named attribute in the CIR dialect, i.e, with name started with
38-
/// "cir.", will be handled here.
39-
mlir::LogicalResult amendOperation(
40-
mlir::Operation *op, llvm::ArrayRef<llvm::Instruction *> instructions,
41-
mlir::NamedAttribute attribute,
42-
mlir::LLVM::ModuleTranslation &moduleTranslation) const override {
43-
return mlir::success();
44-
}
45-
#endif
36+
4637
/// Translates the given operation to LLVM IR using the provided IR builder
4738
/// and saving the state in `moduleTranslation`.
4839
mlir::LogicalResult convertOperation(

0 commit comments

Comments
 (0)