Create func.CallOp failed

Hi, I’m using mlir to build a dialect, when lowering a operation, I need to call an external c function, I create the call using

class FooOpLowering : public OpRewritePattern<snn::FooOp> {
    // some operations before
    Type IndexTy = rewriter.getIndexType();
    Type unrankIndexTy = UnrankedMemRefType::get(IndexTy, 0);
    TypeRange args_type = TypeRange{IndexTy, unrankIndexTy, unrankIndexTy, unrankIndexTy};
    TypeRange result_type = TypeRange{};
    auto func_type = rewriter.getFunctionType(args_type, result_type);
    auto my_func = rewriter.create<func::FuncOp>(loc, "macaque_weight_get_static", func_type);
    my_func.setPrivate();

    ValueRange get_static_args = ValueRange{num_files, unrank_nnzs, unrank_rows, unrank_cols};
    auto get_static_res = rewriter.create<func::CallOp>(loc, my_func, get_static_args); // cause error
    // some operations after
};

if I don’t create func::CallOp, the Lowering worked well and can mak a symbol “macaque_weight_get_static”, but when I try to add this func::CallOp and lower my application again, there will be a dump backtrace without further explanation.Here is the backtrace:

PLEASE submit a bug report to Issues · llvm/llvm-project · GitHub and include the crash backtrace.
Stack dump:
0. Program arguments: …/…/…/build/bin/buddy-opt macaque_delay.mlir --lower-snn=vl=8 -o ./log.mlir
#0 0x000055774e7aeb4f llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (.localalias) (…/…/…/build/bin/buddy-opt+0x464bb4f)
#1 0x000055774e7ac664 SignalHandler(int) (…/…/…/build/bin/buddy-opt+0x4649664)
#2 0x00007f09c5b93420 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x14420)
#3 0x000055774e6acf12 mlir::detail::OperandStorage::OperandStorage(mlir::Operation*, mlir::OpOperand*, mlir::ValueRange) (…/…/…/build/bin/buddy-opt+0x4549f12)
#4 0x000055774e69f497 mlir::Operation::create(mlir::Location, mlir::OperationName, mlir::TypeRange, mlir::ValueRange, mlir::DictionaryAttr, mlir::OpaqueProperties, mlir::BlockRange, unsigned int) (.localalias) (…/…/…/build/bin/buddy-opt+0x453c497)
#5 0x000055774e69fa67 mlir::Operation::create(mlir::Location, mlir::OperationName, mlir::TypeRange, mlir::ValueRange, mlir::NamedAttrList&&, mlir::OpaqueProperties, mlir::BlockRange, mlir::RegionRange) (.localalias) (…/…/…/build/bin/buddy-opt+0x453ca67)
#6 0x000055774e6a011e mlir::Operation::create(mlir::OperationState const&) (…/…/…/build/bin/buddy-opt+0x453d11e)
#7 0x000055774e6020b3 mlir::OpBuilder::create(mlir::OperationState const&) (…/…/…/build/bin/buddy-opt+0x449f0b3)
#8 0x000055774c0f83a7 (anonymous namespace)::SNNCompMatrixWithDelayOpLowering::matchAndRewrite(buddy::snn::CompMatrixWithDelayOp, mlir::PatternRewriter&) const (…/…/…/build/bin/buddy-opt+0x1f953a7)
#9 0x000055774e4404c8 mlir::PatternApplicator::matchAndRewrite(mlir::Operation*, mlir::PatternRewriter&, llvm::function_ref<bool (mlir::Pattern const&)>, llvm::function_ref<void (mlir::Pattern const&)>, llvm::function_ref<mlir::LogicalResult (mlir::Pattern const&)>) (…/…/…/build/bin/buddy-opt+0x42dd4c8)
#10 0x000055774e40e5b7 (anonymous namespace)::OperationLegalizer::legalize(mlir::Operation*, mlir::ConversionPatternRewriter&) (…/…/…/build/bin/buddy-opt+0x42ab5b7)
#11 0x000055774e40ef57 (anonymous namespace)::OperationConverter::convertOperations(llvm::ArrayRefmlir::Operation*, llvm::function_ref<void (mlir::Diagnostic&)>) (…/…/…/build/bin/buddy-opt+0x42abf57)
#12 0x000055774e4110b4 mlir::applyPartialConversion(mlir::Operation*, mlir::ConversionTarget&, mlir::FrozenRewritePatternSet const&, llvm::DenseSet<mlir::Operation*, llvm::DenseMapInfo<mlir::Operation*, void> >) (…/…/…/build/bin/buddy-opt+0x42ae0b4)
#13 0x000055774c0f56bf (anonymous namespace)::LowerSNNPass::runOnOperation() (…/…/…/build/bin/buddy-opt+0x1f926bf)
#14 0x000055774e5000ce mlir::detail::OpToOpPassAdaptor::run(mlir::Pass
, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int) (.localalias) (…/…/…/build/bin/buddy-opt+0x439d0ce)
#15 0x000055774e5005b2 mlir::detail::OpToOpPassAdaptor::runPipeline(mlir::OpPassManager&, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int, mlir::PassInstrumentor*, mlir::PassInstrumentation::PipelineParentInfo const*) (.localalias) (…/…/…/build/bin/buddy-opt+0x439d5b2)
#16 0x000055774e500e5d mlir::PassManager::run(mlir::Operation*) (…/…/…/build/bin/buddy-opt+0x439de5d)
#17 0x000055774c0ea5ab performActions(llvm::raw_ostream&, std::shared_ptrllvm::SourceMgr const&, mlir::MLIRContext*, mlir::MlirOptMainConfig const&) (…/…/…/build/bin/buddy-opt+0x1f875ab)
#18 0x000055774c0eb924 processBuffer(llvm::raw_ostream&, std::unique_ptr<llvm::MemoryBuffer, std::default_deletellvm::MemoryBuffer >, mlir::MlirOptMainConfig const&, mlir::DialectRegistry&, llvm::ThreadPool*) (…/…/…/build/bin/buddy-opt+0x1f88924)
#19 0x000055774c0ebad4 mlir::LogicalResult llvm::function_ref<mlir::LogicalResult (std::unique_ptr<llvm::MemoryBuffer, std::default_deletellvm::MemoryBuffer >, llvm::raw_ostream&)>::callback_fn<mlir::MlirOptMain(llvm::raw_ostream&, std::unique_ptr<llvm::MemoryBuffer, std::default_deletellvm::MemoryBuffer >, mlir::DialectRegistry&, mlir::MlirOptMainConfig const&)::‘lambda’(std::unique_ptr<llvm::MemoryBuffer, std::default_deletellvm::MemoryBuffer >, llvm::raw_ostream&)>(long, std::unique_ptr<llvm::MemoryBuffer, std::default_deletellvm::MemoryBuffer >, llvm::raw_ostream&) (…/…/…/build/bin/buddy-opt+0x1f88ad4)
#20 0x000055774e6de9a4 mlir::splitAndProcessBuffer(std::unique_ptr<llvm::MemoryBuffer, std::default_deletellvm::MemoryBuffer >, llvm::function_ref<mlir::LogicalResult (std::unique_ptr<llvm::MemoryBuffer, std::default_deletellvm::MemoryBuffer >, llvm::raw_ostream&)>, llvm::raw_ostream&, bool, bool) (…/…/…/build/bin/buddy-opt+0x457b9a4)
#21 0x000055774c0e4a57 mlir::MlirOptMain(llvm::raw_ostream&, std::unique_ptr<llvm::MemoryBuffer, std::default_deletellvm::MemoryBuffer >, mlir::DialectRegistry&, mlir::MlirOptMainConfig const&) (.localalias) (…/…/…/build/bin/buddy-opt+0x1f81a57)
#22 0x000055774c0ebdfb mlir::MlirOptMain(int, char**, llvm::StringRef, mlir::DialectRegistry&) (…/…/…/build/bin/buddy-opt+0x1f88dfb)
#23 0x000055774a4541bc main (…/…/…/build/bin/buddy-opt+0x2f11bc)
#24 0x00007f09c5581083 __libc_start_main /build/glibc-SzIz7B/glibc-2.31/csu/…/csu/libc-start.c:342:3
#25 0x000055774a4e830e _start (…/…/…/build/bin/buddy-opt+0x38530e)
Segmentation fault (core dumped)
make: *** [makefile:111: lower] Error 139

So what cause the error and how can I fix it. Thanks for any opinions

This is creating a reference to a temporary object that don’t outlive this statement, you can’t do that.
This should work:

    auto get_static_res = rewriter.create<func::CallOp>(loc, my_func, ValueRange{num_files, unrank_nnzs, unrank_rows, unrank_cols})

I’d advise to build with the sanitizer enabled when you hit such issue, it should pin point the failure here hopefully.

thanks for your help! I replace the get_static_args and it works :pray: , I will try sanitizer next time