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
3839using 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
143145class 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.
635642static void collectUnreachable (mlir::Operation *parent,
636643 llvm::SmallVector<mlir::Operation *> &ops) {
637644
0 commit comments