<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/95246>95246</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [mlir] Crash when using --convert-scf-to-cf
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            mlir
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          wangyongj1a
      </td>
    </tr>
</table>

<pre>
    I have the following MLIR program:
test.mlir:
```
func.func nested @func1() {
  %c0 = arith.constant 0 : index
  %43 = scf.execute_region -> i32 {
    scf.index_switch %c0
    default {
    }
    %164 = arith.constant 0 : i32
    scf.yield %164 : i32
  }
 return
}
```
The above MLIR program will cause a crash when using the following command:
```
mlir-opt --convert-scf-to-cf test.mlir
```
And the crash backtrace is:
```
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.      Program arguments: /data/tmp/v0612/llvm-project/build/bin/mlir-opt --convert-scf-to-cf test.mlir
  #0 0x000055d9cba0d42f llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/data/tmp/v0612/llvm-project/build/bin/mlir-opt+0x14bf42f)
  #1 0x000055d9cba0a484 SignalHandler(int) Signals.cpp:0:0
  #2 0x00007ff8f635e420 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x14420)
  #3 0x000055d9cba47ced llvm::StringRef::find(llvm::StringRef, unsigned long) const (/data/tmp/v0612/llvm-project/build/bin/mlir-opt+0x14f9ced)
  #4 0x000055d9cbb573b2 llvm::StringRef llvm::getTypeName<mlir::TypedAttr>() (/data/tmp/v0612/llvm-project/build/bin/mlir-opt+0x16093b2)
  #5 0x000055d9cbc6f9f4 mlir::AttributeInterface<mlir::TypedAttr, mlir::detail::TypedAttrInterfaceTraits>::getInterfaceFor(mlir::Attribute) (/data/tmp/v0612/llvm-project/build/bin/mlir-opt+0x17219f4)
  #6 0x000055d9cbc72218 mlir::arith::ConstantOp::fold(mlir::arith::ConstantOpGenericAdaptor<llvm::ArrayRef<mlir::Attribute>>) (/data/tmp/v0612/llvm-project/build/bin/mlir-opt+0x1724218)
  #7 0x000055d9cbce8904 mlir::LogicalResult llvm::detail::UniqueFunctionBase<mlir::LogicalResult, mlir::Operation*, llvm::ArrayRef<mlir::Attribute>, llvm::SmallVectorImpl<mlir::OpFoldResult>&>::CallImpl<mlir::Op<mlir::arith::ConstantOp, mlir::OpTrait::ZeroRegions, mlir::OpTrait::OneResult, mlir::OpTrait::OneTypedResult<mlir::Type>::Impl, mlir::OpTrait::ZeroSuccessors, mlir::OpTrait::ZeroOperands, mlir::OpTrait::OpInvariants, mlir::BytecodeOpInterface::Trait, mlir::OpTrait::ConstantLike, mlir::ConditionallySpeculatable::Trait, mlir::OpTrait::AlwaysSpeculatableImplTrait, mlir::MemoryEffectOpInterface::Trait, mlir::OpAsmOpInterface::Trait, mlir::InferIntRangeInterface::Trait, mlir::InferTypeOpInterface::Trait>::getFoldHookFn()::'lambda'(mlir::Operation*, llvm::ArrayRef<mlir::Attribute>, llvm::SmallVectorImpl<mlir::OpFoldResult>&) const>(void*, mlir::Operation*, llvm::ArrayRef<mlir::Attribute>, llvm::SmallVectorImpl<mlir::OpFoldResult>&) (/data/tmp/v0612/llvm-project/build/bin/mlir-opt+0x179a904)
  #8 0x000055d9cbcde996 mlir::RegisteredOperationName::Model<mlir::arith::ConstantOp>::foldHook(mlir::Operation*, llvm::ArrayRef<mlir::Attribute>, llvm::SmallVectorImpl<mlir::OpFoldResult>&) (/data/tmp/v0612/llvm-project/build/bin/mlir-opt+0x1790996)
  #9 0x000055d9cebe7d04 mlir::Operation::fold(llvm::ArrayRef<mlir::Attribute>, llvm::SmallVectorImpl<mlir::OpFoldResult>&) (/data/tmp/v0612/llvm-project/build/bin/mlir-opt+0x4699d04)
 #10 0x000055d9d19febaa mlir::getConstantIntValue(mlir::OpFoldResult) (/data/tmp/v0612/llvm-project/build/bin/mlir-opt+0x74b0baa)
 #11 0x000055d9cd2ee95e mlir::scf::IndexSwitchOp::fold(mlir::scf::IndexSwitchOpGenericAdaptor<llvm::ArrayRef<mlir::Attribute>>, llvm::SmallVectorImpl<mlir::OpFoldResult>&) (/data/tmp/v0612/llvm-project/build/bin/mlir-opt+0x2da095e)
 #12 0x000055d9cd2eec81 mlir::LogicalResult llvm::detail::UniqueFunctionBase<mlir::LogicalResult, mlir::Operation*, llvm::ArrayRef<mlir::Attribute>, llvm::SmallVectorImpl<mlir::OpFoldResult>&>::CallImpl<mlir::Op<mlir::scf::IndexSwitchOp, mlir::OpTrait::AtLeastNRegions<1u>::Impl, mlir::OpTrait::VariadicResults, mlir::OpTrait::ZeroSuccessors, mlir::OpTrait::OneOperand, mlir::OpTrait::SingleBlock, mlir::OpTrait::SingleBlockImplicitTerminator<mlir::scf::YieldOp>::Impl, mlir::OpTrait::OpInvariants, mlir::BytecodeOpInterface::Trait, mlir::OpTrait::HasRecursiveMemoryEffects, mlir::RegionBranchOpInterface::Trait>::getFoldHookFn()::'lambda'(mlir::Operation*, llvm::ArrayRef<mlir::Attribute>, llvm::SmallVectorImpl<mlir::OpFoldResult>&) const>(void*, mlir::Operation*, llvm::ArrayRef<mlir::Attribute>, llvm::SmallVectorImpl<mlir::OpFoldResult>&) (/data/tmp/v0612/llvm-project/build/bin/mlir-opt+0x2da0c81)
 #13 0x000055d9cd2fe266 mlir::RegisteredOperationName::Model<mlir::scf::IndexSwitchOp>::foldHook(mlir::Operation*, llvm::ArrayRef<mlir::Attribute>, llvm::SmallVectorImpl<mlir::OpFoldResult>&) (/data/tmp/v0612/llvm-project/build/bin/mlir-opt+0x2db0266)
 #14 0x000055d9cebe7d04 mlir::Operation::fold(llvm::ArrayRef<mlir::Attribute>, llvm::SmallVectorImpl<mlir::OpFoldResult>&) (/data/tmp/v0612/llvm-project/build/bin/mlir-opt+0x4699d04)
 #15 0x000055d9cebe8098 mlir::Operation::fold(llvm::SmallVectorImpl<mlir::OpFoldResult>&) (/data/tmp/v0612/llvm-project/build/bin/mlir-opt+0x469a098)
 #16 0x000055d9ceb14de7 mlir::OpBuilder::tryFold(mlir::Operation*, llvm::SmallVectorImpl<mlir::Value>&) (/data/tmp/v0612/llvm-project/build/bin/mlir-opt+0x45c6de7)
 #17 0x000055d9cea738ee (anonymous namespace)::OperationLegalizer::legalize(mlir::Operation*, mlir::ConversionPatternRewriter&) DialectConversion.cpp:0:0
 #18 0x000055d9cea73bde (anonymous namespace)::OperationLegalizer::legalize(mlir::Operation*, mlir::ConversionPatternRewriter&) DialectConversion.cpp:0:0
 #19 0x000055d9cea73bde (anonymous namespace)::OperationLegalizer::legalize(mlir::Operation*, mlir::ConversionPatternRewriter&) DialectConversion.cpp:0:0
 #20 0x000055d9cea73bde (anonymous namespace)::OperationLegalizer::legalize(mlir::Operation*, mlir::ConversionPatternRewriter&) DialectConversion.cpp:0:0
 #21 0x000055d9cea73bde (anonymous namespace)::OperationLegalizer::legalize(mlir::Operation*, mlir::ConversionPatternRewriter&) DialectConversion.cpp:0:0
 #22 0x000055d9cea73bde (anonymous namespace)::OperationLegalizer::legalize(mlir::Operation*, mlir::ConversionPatternRewriter&) DialectConversion.cpp:0:0
 #23 0x000055d9cea73bde (anonymous namespace)::OperationLegalizer::legalize(mlir::Operation*, mlir::ConversionPatternRewriter&) DialectConversion.cpp:0:0
 #24 0x000055d9cea73bde (anonymous namespace)::OperationLegalizer::legalize(mlir::Operation*, mlir::ConversionPatternRewriter&) DialectConversion.cpp:0:0
 #25 0x000055d9cea73bde (anonymous namespace)::OperationLegalizer::legalize(mlir::Operation*, mlir::ConversionPatternRewriter&) DialectConversion.cpp:0:0
 #26 0x000055d9cea73bde (anonymous namespace)::OperationLegalizer::legalize(mlir::Operation*, mlir::ConversionPatternRewriter&) DialectConversion.cpp:0:0
 #27 0x000055d9cea73bde (anonymous namespace)::OperationLegalizer::legalize(mlir::Operation*, mlir::ConversionPatternRewriter&) DialectConversion.cpp:0:0
 #28 0x000055d9cea73bde (anonymous namespace)::OperationLegalizer::legalize(mlir::Operation*, mlir::ConversionPatternRewriter&) DialectConversion.cpp:0:0
 #29 0x000055d9cea73bde (anonymous namespace)::OperationLegalizer::legalize(mlir::Operation*, mlir::ConversionPatternRewriter&) DialectConversion.cpp:0:0
 #30 0x000055d9cea73bde (anonymous namespace)::OperationLegalizer::legalize(mlir::Operation*, mlir::ConversionPatternRewriter&) DialectConversion.cpp:0:0
 #31 0x000055d9cea73bde (anonymous namespace)::OperationLegalizer::legalize(mlir::Operation*, mlir::ConversionPatternRewriter&) DialectConversion.cpp:0:0
 #32 0x000055d9cea73bde (anonymous namespace)::OperationLegalizer::legalize(mlir::Operation*, mlir::ConversionPatternRewriter&) DialectConversion.cpp:0:0
 #33 0x000055d9cea73bde (anonymous namespace)::OperationLegalizer::legalize(mlir::Operation*, mlir::ConversionPatternRewriter&) DialectConversion.cpp:0:0
 
... ...

#236 0x000055d9cea73bde (anonymous namespace)::OperationLegalizer::legalize(mlir::Operation*, mlir::ConversionPatternRewriter&) DialectConversion.cpp:0:0
#237 0x000055d9cea73bde (anonymous namespace)::OperationLegalizer::legalize(mlir::Operation*, mlir::ConversionPatternRewriter&) DialectConversion.cpp:0:0
#238 0x000055d9cea73bde (anonymous namespace)::OperationLegalizer::legalize(mlir::Operation*, mlir::ConversionPatternRewriter&) DialectConversion.cpp:0:0
#239 0x000055d9cea73bde (anonymous namespace)::OperationLegalizer::legalize(mlir::Operation*, mlir::ConversionPatternRewriter&) DialectConversion.cpp:0:0
#240 0x000055d9cea73bde (anonymous namespace)::OperationLegalizer::legalize(mlir::Operation*, mlir::ConversionPatternRewriter&) DialectConversion.cpp:0:0
#241 0x000055d9cea73bde (anonymous namespace)::OperationLegalizer::legalize(mlir::Operation*, mlir::ConversionPatternRewriter&) DialectConversion.cpp:0:0
#242 0x000055d9cea73bde (anonymous namespace)::OperationLegalizer::legalize(mlir::Operation*, mlir::ConversionPatternRewriter&) DialectConversion.cpp:0:0
#243 0x000055d9cea73bde (anonymous namespace)::OperationLegalizer::legalize(mlir::Operation*, mlir::ConversionPatternRewriter&) DialectConversion.cpp:0:0
#244 0x000055d9cea73bde (anonymous namespace)::OperationLegalizer::legalize(mlir::Operation*, mlir::ConversionPatternRewriter&) DialectConversion.cpp:0:0
#245 0x000055d9cea73bde (anonymous namespace)::OperationLegalizer::legalize(mlir::Operation*, mlir::ConversionPatternRewriter&) DialectConversion.cpp:0:0
#246 0x000055d9cea73bde (anonymous namespace)::OperationLegalizer::legalize(mlir::Operation*, mlir::ConversionPatternRewriter&) DialectConversion.cpp:0:0
#247 0x000055d9cea73bde (anonymous namespace)::OperationLegalizer::legalize(mlir::Operation*, mlir::ConversionPatternRewriter&) DialectConversion.cpp:0:0
#248 0x000055d9cea73bde (anonymous namespace)::OperationLegalizer::legalize(mlir::Operation*, mlir::ConversionPatternRewriter&) DialectConversion.cpp:0:0
#249 0x000055d9cea73bde (anonymous namespace)::OperationLegalizer::legalize(mlir::Operation*, mlir::ConversionPatternRewriter&) DialectConversion.cpp:0:0
#250 0x000055d9cea73bde (anonymous namespace)::OperationLegalizer::legalize(mlir::Operation*, mlir::ConversionPatternRewriter&) DialectConversion.cpp:0:0
#251 0x000055d9cea73bde (anonymous namespace)::OperationLegalizer::legalize(mlir::Operation*, mlir::ConversionPatternRewriter&) DialectConversion.cpp:0:0
#252 0x000055d9cea73bde (anonymous namespace)::OperationLegalizer::legalize(mlir::Operation*, mlir::ConversionPatternRewriter&) DialectConversion.cpp:0:0
#253 0x000055d9cea73bde (anonymous namespace)::OperationLegalizer::legalize(mlir::Operation*, mlir::ConversionPatternRewriter&) DialectConversion.cpp:0:0
#254 0x000055d9cea73bde (anonymous namespace)::OperationLegalizer::legalize(mlir::Operation*, mlir::ConversionPatternRewriter&) DialectConversion.cpp:0:0
#255 0x000055d9cea73bde (anonymous namespace)::OperationLegalizer::legalize(mlir::Operation*, mlir::ConversionPatternRewriter&) DialectConversion.cpp:0:0
Segmentation fault (core dumped)
```
My git version is fc1c34bbcb7811ebdbb623c5a4473c4f186c434d.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzsW19v2zry_TTKCxFDov4_-MFx4l8DpLdF3F-B3ZeAIkcybynSS1JOvJ9-IdqORV8nTbdZ3Gr3Am0cSsPhOcPRnJFbEmN4IwGmQXoVpNcXpLMrpaePRDZbJZvfI3JRKbad3qIV2QCyK0C1EkI9ctmgj3e392itVaNJG8SzILwOwpkFYyet4Pr5SpCF-z9uWHeSTvofSIKxwFCQhP0wCnAR4BIF-dXOEKEApzREQXyNiOZ2NaFKGkukRf3FGeKSwdPANomdraH1BJ6AdhYeNDRcSXQZxDeIx3joHDlD5-PBPHJLV7v1jvcZ1KQT1p8U5NeDAU6jLHkFYYz95bYcBDtO8yyOnjXYTst99A5XT8L4ZQWIVGoD3jagRy4EoqQzgAiimpgVelyBRJ3pd8zfP6ralkj20kb1m3ip1hZdXlIlN6DtpaH1pVWXtEbHbT43dSaZW2sHoCL0m9WEAuLmpcU-393MljfIdFXLLSKo6hqkYa20RVahlbVrNxUvArxouF111YSqNsALITaHj8u1Vr8DtQFecGM6MAFeICIZ4pKKjsE5SJPd8ktL6DfEunb9DDCcBGH5eR9WopuuBWl7ECjAC0YsCfDCtusALzZhFuE_Yqg6Llj_yWWAFz8SzT6x4hCFT2EYhmnKSlqRkCW4Ro5tPAvimdma3S-fNZfW4f_SEwpwcTTS5PFBGauBtAHOAjxHXFr3kPXP2k-wCPBV-BQlVZ3gOsDlAHZ0ApskRYKWvJFEfCCSCdABLvYodpfNhK77sIfu79ET3nvK67qosziFBIfo4UGDsUrDg7Z7FoJXAV48FdlDllwKLruny0Z2uxtru9JA2MSoSbjHnODQRxz7iJOcAhsEemk1l8091LthzSXzYny8j-eok66eMiSUbHqKriC8V7jrkgLzwSce-CrN4wqfAz-41oD9sl3Db6SFIJ4fSnU86y-ymbU6iG8OtfhdcGdhGVfYx516uGlWl3WCjlh6GLzqLNx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