Skip to content
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

[mlir][arith] -arith-emulate-wide-int crashes in EmulateWideInt.cpp:160: Value constructResultVector(ConversionPatternRewriter &, Location, VectorType, ValueRange): Assertion `resultShape.back() == static_cast<int64_t>(resultComponents.size()) && "Wrong number of result components"' failed. #108163

Closed
axeabc opened this issue Sep 11, 2024 · 5 comments · Fixed by #112104
Assignees
Labels
crash Prefer [crash-on-valid] or [crash-on-invalid] mlir:arith

Comments

@axeabc
Copy link

axeabc commented Sep 11, 2024

git version: 761bf33

system: Ubuntu 18.04.6 LTS

reproduce with: mlir-opt -arith-emulate-wide-int a.mlir

This case also crashes in -memref-emulate-wide-int
a.mlir:

module {
   func.func @test_muli_vector(%arg0: vector<8xi64>, %arg1: vector<8xi64>) -> vector<8xi64> {
    %0 = arith.extsi %arg1: vector<8xi64> to vector<8xi128>
    %1 = arith.extsi %arg0: vector<8xi64> to vector<8xi128>
    %2 = arith.muli %0, %1 : vector<8xi128>
    %3 = arith.trunci %2 : vector<8xi128> to vector<8xi64>
    return %3 : vector<8xi64>
  }
}

stack trace:

mlir-opt: /data/szy/MLIR/llvm-release/llvm-project/mlir/lib/Dialect/Arith/Transforms/EmulateWideInt.cpp:160: Value constructResultVector(ConversionPatternRewriter &, Location, VectorType, ValueRange): Assertion `resultShape.back() == static_cast<int64_t>(resultComponents.size()) && "Wrong number of result components"' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.	Program arguments: /data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt -arith-emulate-wide-int a.mlir
 #0 0x00005575a3b54128 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt+0x10d7128)
 #1 0x00005575a3b51c3e llvm::sys::RunSignalHandlers() (/data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt+0x10d4c3e)
 #2 0x00005575a3b54abd SignalHandler(int) Signals.cpp:0:0
 #3 0x00007f938c563420 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x14420)
 #4 0x00007f938bba000b raise /build/glibc-LcI20x/glibc-2.31/signal/../sysdeps/unix/sysv/linux/raise.c:51:1
 #5 0x00007f938bb7f859 abort /build/glibc-LcI20x/glibc-2.31/stdlib/abort.c:81:7
 #6 0x00007f938bb7f729 get_sysdep_segment_value /build/glibc-LcI20x/glibc-2.31/intl/loadmsgcat.c:509:8
 #7 0x00007f938bb7f729 _nl_load_domain /build/glibc-LcI20x/glibc-2.31/intl/loadmsgcat.c:970:34
 #8 0x00007f938bb90fd6 (/lib/x86_64-linux-gnu/libc.so.6+0x33fd6)
 #9 0x00005575a3e31afe (/data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt+0x13b4afe)
#10 0x00005575a3e3a636 (anonymous namespace)::ConvertExtSI::matchAndRewrite(mlir::arith::ExtSIOp, mlir::arith::ExtSIOpAdaptor, mlir::ConversionPatternRewriter&) const EmulateWideInt.cpp:0:0
#11 0x00005575a3e3a2a0 mlir::OpConversionPattern<mlir::arith::ExtSIOp>::matchAndRewrite(mlir::Operation*, llvm::ArrayRef<mlir::Value>, mlir::ConversionPatternRewriter&) const (/data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt+0x13bd2a0)
#12 0x00005575a6ca4b11 mlir::ConversionPattern::matchAndRewrite(mlir::Operation*, mlir::PatternRewriter&) const (/data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt+0x4227b11)
#13 0x00005575a962fc91 void llvm::function_ref<void ()>::callback_fn<mlir::PatternApplicator::matchAndRewrite(mlir::Operation*, mlir::PatternRewriter&, llvm::function_ref<bool (mlir::Pattern const&)>, llvm::function_ref<void (mlir::Pattern const&)>, llvm::function_ref<llvm::LogicalResult (mlir::Pattern const&)>)::$_0>(long) PatternApplicator.cpp:0:0
#14 0x00005575a962c94b mlir::PatternApplicator::matchAndRewrite(mlir::Operation*, mlir::PatternRewriter&, llvm::function_ref<bool (mlir::Pattern const&)>, llvm::function_ref<void (mlir::Pattern const&)>, llvm::function_ref<llvm::LogicalResult (mlir::Pattern const&)>) (/data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt+0x6baf94b)
#15 0x00005575a6ca5b53 (anonymous namespace)::OperationLegalizer::legalize(mlir::Operation*, mlir::ConversionPatternRewriter&) DialectConversion.cpp:0:0
#16 0x00005575a6ca4bb7 mlir::OperationConverter::convert(mlir::ConversionPatternRewriter&, mlir::Operation*) (/data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt+0x4227bb7)
#17 0x00005575a6ca5d7f mlir::OperationConverter::convertOperations(llvm::ArrayRef<mlir::Operation*>) (/data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt+0x4228d7f)
#18 0x00005575a6cad96b mlir::applyPartialConversion(mlir::Operation*, mlir::ConversionTarget const&, mlir::FrozenRewritePatternSet const&, mlir::ConversionConfig) (/data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt+0x423096b)
#19 0x00005575a3e2de26 (anonymous namespace)::EmulateWideIntPass::runOnOperation() EmulateWideInt.cpp:0:0
#20 0x00005575a6c462d6 mlir::detail::OpToOpPassAdaptor::run(mlir::Pass*, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int) (/data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt+0x41c92d6)
#21 0x00005575a6c46c40 mlir::detail::OpToOpPassAdaptor::runPipeline(mlir::OpPassManager&, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int, mlir::PassInstrumentor*, mlir::PassInstrumentation::PipelineParentInfo const*) (/data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt+0x41c9c40)
#22 0x00005575a6c49282 mlir::PassManager::run(mlir::Operation*) (/data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt+0x41cc282)
#23 0x00005575a6c41ab1 performActions(llvm::raw_ostream&, std::shared_ptr<llvm::SourceMgr> const&, mlir::MLIRContext*, mlir::MlirOptMainConfig const&) MlirOptMain.cpp:0:0
#24 0x00005575a6c4170b llvm::LogicalResult llvm::function_ref<llvm::LogicalResult (std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, llvm::raw_ostream&)>::callback_fn<mlir::MlirOptMain(llvm::raw_ostream&, std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, mlir::DialectRegistry&, mlir::MlirOptMainConfig const&)::$_0>(long, std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, llvm::raw_ostream&) MlirOptMain.cpp:0:0
#25 0x00005575a6cee3a5 mlir::splitAndProcessBuffer(std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, llvm::function_ref<llvm::LogicalResult (std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, llvm::raw_ostream&)>, llvm::raw_ostream&, llvm::StringRef, llvm::StringRef) (/data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt+0x42713a5)
#26 0x00005575a6c3cb35 mlir::MlirOptMain(llvm::raw_ostream&, std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, mlir::DialectRegistry&, mlir::MlirOptMainConfig const&) (/data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt+0x41bfb35)
#27 0x00005575a6c3cddf mlir::MlirOptMain(int, char**, llvm::StringRef, llvm::StringRef, mlir::DialectRegistry&) (/data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt+0x41bfddf)
#28 0x00005575a6c3d10e mlir::MlirOptMain(int, char**, llvm::StringRef, mlir::DialectRegistry&) (/data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt+0x41c010e)
#29 0x00005575a3b34d67 main (/data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt+0x10b7d67)
#30 0x00007f938bb81083 __libc_start_main /build/glibc-LcI20x/glibc-2.31/csu/../csu/libc-start.c:342:3
#31 0x00005575a3b348ee _start (/data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt+0x10b78ee)
@github-actions github-actions bot added the mlir label Sep 11, 2024
@EugeneZelenko EugeneZelenko added crash Prefer [crash-on-valid] or [crash-on-invalid] mlir:arith and removed mlir labels Sep 11, 2024
@CoTinker CoTinker self-assigned this Sep 21, 2024
@CoTinker
Copy link
Contributor

CoTinker commented Oct 12, 2024

// Vector case.
addConversion([this](VectorType ty) -> std::optional<Type> {
auto intTy = dyn_cast<IntegerType>(ty.getElementType());
if (!intTy)
return ty;
unsigned width = intTy.getWidth();
if (width <= maxIntWidth)
return ty;
// vector<...xi2N> --> vector<...x2xiN>
if (width == 2 * maxIntWidth) {
auto newShape = to_vector(ty.getShape());
newShape.push_back(2);
return VectorType::get(newShape,
IntegerType::get(ty.getContext(), maxIntWidth));
}
return std::nullopt;
});

op.getType() is vector<8xi128> and maxIntWidth is 32, converter expected to return std::nullopt. But newTy actually is vector<8xi128>. I don't know why this is.
auto newTy = getTypeConverter()->convertType<VectorType>(op.getType());

@kuhar

@kuhar kuhar self-assigned this Oct 12, 2024
@kuhar
Copy link
Member

kuhar commented Oct 12, 2024

I can reproduce this. Minimized repro:

func.func @test_muli_vector(%arg0: vector<8xi64>) -> vector<8xi64> {
  %0 = arith.extsi %arg0: vector<8xi64> to vector<8xi128>
  %2 = arith.muli %0, %0 : vector<8xi128>
  %3 = arith.trunci %2 : vector<8xi128> to vector<8xi64>
  return %3 : vector<8xi64>
}

@kuhar
Copy link
Member

kuhar commented Oct 12, 2024

In this case, I believe that we should bail out on i128 since it's not 2x the widest int supported (32).

@kuhar
Copy link
Member

kuhar commented Oct 12, 2024

It's because of a bug in the return values of the type converter. They return nullopt which allows for retries (and going to the 'passthrough' conversion for all the other types) instead of nullptr which indicates immediate conversion failure:

/// * std::optional<LogicalResult>(T, SmallVectorImpl<Type> &)
/// - This form represents a 1-N type conversion. It should return
/// `failure` or `std::nullopt` to signify a failed conversion. If the
/// new set of types is empty, the type is removed and any usages of the
/// existing value are expected to be removed during conversion. If
/// `std::nullopt` is returned, the converter is allowed to try another
/// conversion function to perform the conversion.

kuhar added a commit to kuhar/llvm-project that referenced this issue Oct 12, 2024
Use `nullptr` to indicate that type conversion failed and no fallback
conversion should be attempted.

Fixes: llvm#108163
kuhar added a commit to kuhar/llvm-project that referenced this issue Oct 12, 2024
Use `nullptr` to indicate that type conversion failed and no fallback
conversion should be attempted.

Fixes: llvm#108163
@CoTinker
Copy link
Contributor

Thanks, I understand it.

kuhar added a commit that referenced this issue Oct 12, 2024
Use `nullptr` to indicate that type conversion failed and no fallback
conversion should be attempted.

Fixes: #108163
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
crash Prefer [crash-on-valid] or [crash-on-invalid] mlir:arith
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants