llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.6k stars 11.82k forks source link

[MLIR][OpenMP] Memref params in `omp.target` raise error when lowering #76579

Open EllisLambda opened 9 months ago

EllisLambda commented 9 months ago

The MLIR and LLVM toolchain was built with https://github.com/llvm/llvm-project/commit/8c6172b0ac2b254dec7d57326abfd666a7954a03. Use mlir-opt -convert-vector-to-llvm -finalize-memref-to-llvm -convert-arith-to-llvm -convert-openmp-to-llvm

module {
  func.func @omp_target() {
    %alloca = memref.alloca() : memref<64x64xf64>
    %alloca_0 = memref.alloca() : memref<64x64xf64>
    %0 = omp.map_info var_ptr(%alloca : memref<64x64xf64>, tensor<?xi32>) map_clauses(to) capture(ByRef) -> memref<64x64xf64>
    %1 = omp.map_info var_ptr(%alloca_0 : memref<64x64xf64>, tensor<?xi32>) map_clauses(from) capture(ByRef) -> memref<64x64xf64>
    omp.target map_entries(%0 -> %arg0, %1 -> %arg1 : memref<64x64xf64>, memref<64x64xf64>) {
    ^bb0(%arg0: memref<64x64xf64>, %arg1: memref<64x64xf64>):
      omp.teams {
        omp.parallel {
          %c0 = arith.constant 0 : index
          %c1 = arith.constant 1 : index
          %c8192 = arith.constant 8192 : index
          omp.wsloop for  (%arg2, %arg3, %arg4, %arg5) : index = (%c0, %c0, %c0, %c0) to (%c8192, %c8192, %c8192, %c8192) step (%c1, %c1, %c1, %c1) {
            %2 = vector.load %arg0[%arg2, %arg3] : memref<64x64xf64>, vector<16xf64>
            %3 = vector.load %arg1[%arg4, %arg5] : memref<64x64xf64>, vector<16xf64>
            %4 = vector.load %arg1[%arg3, %arg2] : memref<64x64xf64>, vector<16xf64>
            %5 = arith.mulf %2, %3 : vector<16xf64>
            %6 = arith.divf %3, %4 : vector<16xf64>
            %7 = arith.addf %5, %6 : vector<16xf64>
            vector.store %7, %arg1[%arg2, %arg3] : memref<64x64xf64>, vector<16xf64>
            omp.terminator
          }
          omp.terminator
        }
        omp.terminator
      }
      omp.terminator
    }
    omp.barrier
    return
  }
}

raise error

 #0 0x0000000004005f7b llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (mlir-opt+0x4005f7b)
 #1 0x000000000400312b llvm::sys::RunSignalHandlers() (mlir-opt+0x400312b)
 #2 0x0000000004003255 SignalHandler(int) Signals.cpp:0:0
 #3 0x00007f97b603f190 __restore_rt (/lib64/libc.so.6+0x3f190)
 #4 0x0000000006c6b1d3 mlir::Type::getContext() const (mlir-opt+0x6c6b1d3)
 #5 0x0000000006b98e71 mlir::TypeAttr::get(mlir::Type) (mlir-opt+0x6b98e71)
 #6 0x0000000006299107 (anonymous namespace)::MapInfoOpConversion::matchAndRewrite(mlir::omp::MapInfoOp, mlir::omp::MapInfoOpAdaptor, mlir::ConversionPatternRewriter&) const OpenMPToLLVM.cpp:0:0
 #7 0x00000000062933b4 mlir::ConvertOpToLLVMPattern<mlir::omp::MapInfoOp>::matchAndRewrite(mlir::Operation*, llvm::ArrayRef<mlir::Value>, mlir::ConversionPatternRewriter&) const OpenMPToLLVM.cpp:0:0
 #8 0x0000000006aca734 mlir::ConversionPattern::matchAndRewrite(mlir::Operation*, mlir::PatternRewriter&) const (mlir-opt+0x6aca734)
 #9 0x00000000095aec3a 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&)>) (mlir-opt+0x95aec3a)
#10 0x0000000006ad74a5 (anonymous namespace)::OperationLegalizer::legalize(mlir::Operation*, mlir::ConversionPatternRewriter&) DialectConversion.cpp:0:0
#11 0x0000000006ad7b5d (anonymous namespace)::OperationConverter::convertOperations(llvm::ArrayRef<mlir::Operation*>, llvm::function_ref<void (mlir::Diagnostic&)>) DialectConversion.cpp:0:0
#12 0x0000000006ada0cd mlir::applyPartialConversion(mlir::Operation*, mlir::ConversionTarget const&, mlir::FrozenRewritePatternSet const&, llvm::DenseSet<mlir::Operation*, llvm::DenseMapInfo<mlir::Operation*, void>>*) (mlir-opt+0x6ada0cd)
#13 0x000000000629fbf7 (anonymous namespace)::ConvertOpenMPToLLVMPass::runOnOperation() OpenMPToLLVM.cpp:0:0
#14 0x0000000006a2bb8e mlir::detail::OpToOpPassAdaptor::run(mlir::Pass*, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int) (mlir-opt+0x6a2bb8e)
#15 0x0000000006a2c1a8 mlir::detail::OpToOpPassAdaptor::runPipeline(mlir::OpPassManager&, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int, mlir::PassInstrumentor*, mlir::PassInstrumentation::PipelineParentInfo const*) (mlir-opt+0x6a2c1a8)
#16 0x0000000006a2d0bd mlir::PassManager::run(mlir::Operation*) (mlir-opt+0x6a2d0bd)
#17 0x0000000006a11459 performActions(llvm::raw_ostream&, std::shared_ptr<llvm::SourceMgr> const&, mlir::MLIRContext*, mlir::MlirOptMainConfig const&) MlirOptMain.cpp:0:0
#18 0x0000000006a1247b processBuffer(llvm::raw_ostream&, std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, mlir::MlirOptMainConfig const&, mlir::DialectRegistry&, llvm::ThreadPool*) MlirOptMain.cpp:0:0
#19 0x0000000006a125a9 mlir::LogicalResult llvm::function_ref<mlir::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&)::'lambda'(std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, llvm::raw_ostream&)>(long, std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, llvm::raw_ostream&) MlirOptMain.cpp:0:0
#20 0x0000000006b2f613 mlir::splitAndProcessBuffer(std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, llvm::function_ref<mlir::LogicalResult (std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, llvm::raw_ostream&)>, llvm::raw_ostream&, bool, bool) (mlir-opt+0x6b2f613)
#21 0x0000000006a0bf3e mlir::MlirOptMain(llvm::raw_ostream&, std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, mlir::DialectRegistry&, mlir::MlirOptMainConfig const&) (mlir-opt+0x6a0bf3e)
#22 0x0000000006a126be mlir::MlirOptMain(int, char**, llvm::StringRef, llvm::StringRef, mlir::DialectRegistry&) (mlir-opt+0x6a126be)
#23 0x0000000006a12b15 mlir::MlirOptMain(int, char**, llvm::StringRef, mlir::DialectRegistry&) (mlir-opt+0x6a12b15)
#24 0x0000000003f49d7b main (mlir-opt+0x3f49d7b)
#25 0x00007f97b60281b0 __libc_start_call_main (/lib64/libc.so.6+0x281b0)
#26 0x00007f97b6028279 __libc_start_main@GLIBC_2.2.5 (/lib64/libc.so.6+0x28279)
#27 0x0000000003f3ecb5 _start /home/abuild/rpmbuild/BUILD/glibc-2.38/csu/../sysdeps/x86_64/start.S:117:0
wjy99-c commented 8 months ago

Hi Ellis. With my delta-debugging tool, I find that the error might not be due to the omp.target. Instead, it might be due to the -convert-openmp-to-llvm pass. I reproduced the crash with the following code:

module {
  func.func @omp_target() {
    %alloca = memref.alloca() : memref<64x64xf64>
    %0 = omp.map_info var_ptr(%alloca : memref<64x64xf64>, tensor<?xi32>) map_clauses(to) capture(ByRef) -> memref<64x64xf64>
    return
  }
}