llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[Flang] Compilation of a large array of a derived type with an initialized member needs a lot of memory

Open yus3710-fj opened this issue 2 years ago • 8 comments

This is an issue from Fujitsu testsuite.

Flang-new terminates abnormally when compiling the following program. When I checked with the top command, %MEM gradually increased to 100%. It seems that a lot of memory was used.

There is no problem with gfortran compiling on the same machine.

The following are the test program, Flang-new and gfortran compilation result, and the stack trace of Flang-new.

! test.f90
  type t1
    complex :: c = (0.0, 0.0)
  end type t1
  type(t1) val(1000000)

  print *, val(500000)
end
$ flang-new -v test.f90 
flang-new version 18.0.0git (https://github.com/llvm/llvm-project.git 2af0695b875911144ed1f7b4e5679be500b86a77)
Target: aarch64-unknown-linux-gnu
Thread model: posix
InstalledDir: /path/to/install/bin
Found candidate GCC installation: /opt/gcc/11.2.0/lib/gcc/aarch64-unknown-linux-gnu/11.2.0
Selected GCC installation: /opt/gcc/11.2.0/lib/gcc/aarch64-unknown-linux-gnu/11.2.0
Candidate multilib: .;@m64
Selected multilib: .;@m64
 "/path/to/install/bin/flang-new" -fc1 -triple aarch64-unknown-linux-gnu -emit-obj -fcolor-diagnostics -mrelocation-model pic -pic-level 2 -pic-is-pie -target-cpu generic -target-feature +neon -target-feature +v8a -mframe-pointer=non-leaf -o /tmp/test-f371a2.o -x f95-cpp-input test.f90
flang-new: error: unable to execute command: Killed
flang-new: error: flang frontend command failed due to signal (use -v to see invocation)
flang-new version 18.0.0git (https://github.com/llvm/llvm-project.git 2af0695b875911144ed1f7b4e5679be500b86a77)
Target: aarch64-unknown-linux-gnu
Thread model: posix
InstalledDir: /path/to/install/bin
flang-new: note: diagnostic msg: 
********************

PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT:
Preprocessed source(s) and associated run script(s) are located at:
flang-new: note: diagnostic msg: /tmp/test-e5be39
flang-new: note: diagnostic msg: /tmp/test-e5be39.sh
flang-new: note: diagnostic msg: 

********************
$ gfortran -v test.f90 
Driving: gfortran -v test.f90 -l gfortran -l m -shared-libgcc
Using built-in specs.
COLLECT_GCC=gfortran
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/aarch64-redhat-linux/8/lto-wrapper
Target: aarch64-redhat-linux
Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,fortran,lto --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --with-linker-hash-style=gnu --enable-plugin --enable-initfini-array --with-isl --disable-libmpx --enable-gnu-indirect-function --build=aarch64-redhat-linux
Thread model: posix
gcc version 8.3.1 20190507 (Red Hat 8.3.1-4) (GCC) 
 :
$
#0  0x00007ffff090ff7b in llvm::Constant::getAggregateElement (this=0x7ffc5236d810, Elt=117955)
    at /path/to/llvm-project/llvm/lib/IR/Constants.cpp:423
#1  0x00007ffff08f021c in llvm::ConstantFoldInsertValueInstruction (Agg=0x7ffc5236d810, Val=0x555555967600, Idxs=...)
    at /path/to/llvm-project/llvm/lib/IR/ConstantFold.cpp:605
#2  0x00007ffff0ad9596 in llvm::ConstantFolder::FoldInsertValue (this=0x7fffffffb9b8, Agg=0x7ffc5236d810, Val=0x555555967600, IdxList=...)
    at /path/to/llvm-project/llvm/include/llvm/IR/ConstantFolder.h:145
#3  0x00007fffdfa3d743 in llvm::IRBuilderBase::CreateInsertValue (this=0x7fffffffb940, Agg=0x7ffc5236d810, Val=0x555555967600, Idxs=..., Name=...)
    at /path/to/llvm-project/llvm/include/llvm/IR/IRBuilder.h:2498
#4  0x00007fffdfa177e4 in convertOperationImpl (opInst=..., builder=..., moduleTranslation=...)
    at /path/to/build/tools/mlir/include/mlir/Dialect/LLVMIR/LLVMConversions.inc:211
#5  0x00007fffdfa2bccb in (anonymous namespace)::LLVMDialectLLVMIRTranslationInterface::convertOperation (this=0x5555555ed730, op=0x5555558fbb90,
    builder=..., moduleTranslation=...) at /path/to/llvm-project/mlir/lib/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.cpp:415
#6  0x00007fffe487ab27 in mlir::LLVM::ModuleTranslation::convertOperation (this=0x7fffffffbc00, op=..., builder=..., recordInsertions=false)
    at /path/to/llvm-project/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp:753
#7  0x00007fffe487ba28 in mlir::LLVM::ModuleTranslation::convertGlobals (this=0x7fffffffbc00)
    at /path/to/llvm-project/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp:925
#8  0x00007fffe4880201 in mlir::translateModuleToLLVMIR (module=0x5555556752c0, llvmContext=..., name=...)
    at /path/to/llvm-project/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp:1566
#9  0x00007ffff6c17c69 in Fortran::frontend::CodeGenAction::generateLLVMIR (this=0x5555555998b0)
    at /path/to/llvm-project/flang/lib/Frontend/FrontendActions.cpp:777
#10 0x00007ffff6c192f7 in Fortran::frontend::CodeGenAction::executeAction (this=0x5555555998b0)
    at /path/to/llvm-project/flang/lib/Frontend/FrontendActions.cpp:1148
#11 0x00007ffff6c10650 in Fortran::frontend::FrontendAction::execute (this=0x5555555998b0)
    at /path/to/llvm-project/flang/lib/Frontend/FrontendAction.cpp:112
#12 0x00007ffff6be0c11 in Fortran::frontend::CompilerInstance::executeAction (this=0x555555594970, act=...)
    at /path/to/llvm-project/flang/lib/Frontend/CompilerInstance.cpp:173
#13 0x00007ffff71952b7 in Fortran::frontend::executeCompilerInvocation (flang=0x555555594970)
    at /path/to/llvm-project/flang/lib/FrontendTool/ExecuteCompilerInvocation.cpp:221
#14 0x000055555555e2c5 in fc1_main (argv=..., argv0=0x7fffffffe4ca "/path/to/build/bin/flang-new")
    at /path/to/llvm-project/flang/tools/flang-driver/fc1_main.cpp:67
#15 0x0000555555557509 in executeFC1Tool (argV=...) at /path/to/llvm-project/flang/tools/flang-driver/driver.cpp:66
#16 0x00005555555579de in main (argc=19, argv=0x7fffffffe1d8) at /path/to/llvm-project/flang/tools/flang-driver/driver.cpp:109

The following is a similar program but an array size is smaller.

! small_test.f90
  type t1
    complex :: c = (0.0, 0.0)
  end type t1
  type(t1) val(100)

  print *, val(50)
end

That can be compiled normally, but Flang generates large MLIR with LLVM dialect. I suspect this is the cause of the abnormal termination.

$ flang-new -Xflang -save-temps small_test.f90
$ cat small_test-llvmir.mlir
module attributes {dlti.dl_spec = #dlti.dl_spec<#dlti.dl_entry<i1, dense<8> : vector<2xi64>>, #dlti.dl_entry<!llvm.ptr, dense<64> : vector<4xi64>>, #dlti.dl_entry<f16, dense<16> : vector<2xi64>>, #dlti.dl_entry<i32, dense<32> : vector<2xi64>>, #dlti.dl_entry<i16, dense<[16, 32]> : vector<2xi64>>, #dlti.dl_entry<i8, dense<[8, 32]> : vector<2xi64>>, #dlti.dl_entry<f64, dense<64> : vector<2xi64>>, #dlti.dl_entry<i128, dense<128> : vector<2xi64>>, #dlti.dl_entry<f128, dense<128> : vector<2xi64>>, #dlti.dl_entry<i64, dense<64> : vector<2xi64>>, #dlti.dl_entry<"dlti.stack_alignment", 128 : i64>, #dlti.dl_entry<"dlti.endianness", "little">>, fir.defaultkind = "a1c4d8i4l4r4", fir.kindmap = "", llvm.data_layout = "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128", llvm.target_triple = "aarch64-unknown-linux-gnu"} {
  llvm.func @_QQmain() attributes {frame_pointer = #llvm.framePointerKind<"non-leaf">} {
    %0 = llvm.mlir.constant(1 : i32) : i32
     :
    llvm.return
  }
   :
  llvm.mlir.global internal @_QFEval() {addr_space = 0 : i32} : !llvm.array<100 x struct<"_QFTt1", (struct<(f32, f32)>)>> {
    %0 = llvm.mlir.constant(0.000000e+00 : f32) : f32
    %1 = llvm.mlir.undef : !llvm.struct<"_QFTt1", (struct<(f32, f32)>)>
    %2 = llvm.mlir.undef : !llvm.struct<(f32, f32)>
    %3 = llvm.insertvalue %0, %2[0] : !llvm.struct<(f32, f32)> 
    %4 = llvm.insertvalue %0, %3[1] : !llvm.struct<(f32, f32)> 
    %5 = llvm.insertvalue %4, %1[0] : !llvm.struct<"_QFTt1", (struct<(f32, f32)>)> 
    %6 = llvm.mlir.undef : !llvm.array<100 x struct<"_QFTt1", (struct<(f32, f32)>)>>
    %7 = llvm.insertvalue %5, %6[0] : !llvm.array<100 x struct<"_QFTt1", (struct<(f32, f32)>)>> 
    %8 = llvm.insertvalue %5, %7[1] : !llvm.array<100 x struct<"_QFTt1", (struct<(f32, f32)>)>> 
    %9 = llvm.insertvalue %5, %8[2] : !llvm.array<100 x struct<"_QFTt1", (struct<(f32, f32)>)>> 
    %10 = llvm.insertvalue %5, %9[3] : !llvm.array<100 x struct<"_QFTt1", (struct<(f32, f32)>)>> 
     :
    %106 = llvm.insertvalue %5, %105[99] : !llvm.array<100 x struct<"_QFTt1", (struct<(f32, f32)>)>> 
    llvm.return %106 : !llvm.array<100 x struct<"_QFTt1", (struct<(f32, f32)>)>>
  }
  llvm.mlir.global linkonce_odr constant @_QFE.c.t1() comdat(@__llvm_comdat::@_QFE.c.t1) {addr_space = 0 : i32} : !llvm.array<1 x struct<"_QM__fortran_type_infoTcomponent", (struct<(ptr, i64, i32, i8, i8, i8, i8)>, i8, i8, i8, i8, array<4 x i8>, i64, struct<"_QM__fortran_type_infoTvalue", (i8, array<7 x i8>, i64)>, struct<(ptr, i64, i32, i8, i8, i8, i8, ptr, array<1 x i64>)>, struct<(ptr, i64, i32, i8, i8, i8, i8, array<1 x array<3 x i64>>, ptr, array<1 x i64>)>, struct<(ptr, i64, i32, i8, i8, i8, i8, array<2 x array<3 x i64>>, ptr, array<1 x i64>)>, struct<"_QM__fortran_builtinsT__builtin_c_ptr", (i64)>)>> {
    %0 = llvm.mlir.constant(0 : index) : i64
    %1 = llvm.mlir.constant(0 : i64) : i64
    %2 = llvm.mlir.constant(0 : i8) : i8
    %3 = llvm.mlir.constant(4 : i8) : i8
    %4 = llvm.mlir.constant(2 : i8) : i8
    %5 = llvm.mlir.constant(1 : i8) : i8
    %6 = llvm.mlir.undef : !llvm.array<1 x struct<"_QM__fortran_type_infoTcomponent", (struct<(ptr, i64, i32, i8, i8, i8, i8)>, i8, i8, i8, i8, array<4 x i8>, i64, struct<"_QM__fortran_type_infoTvalue", (i8, array<7 x i8>, i64)>, struct<(ptr, i64, i32, i8, i8, i8, i8, ptr, array<1 x i64>)>, struct<(ptr, i64, i32, i8, i8, i8, i8, array<1 x array<3 x i64>>, ptr, array<1 x i64>)>, struct<(ptr, i64, i32, i8, i8, i8, i8, array<2 x array<3 x i64>>, ptr, array<1 x i64>)>, struct<"_QM__fortran_builtinsT__builtin_c_ptr", (i64)>)>>
    %7 = llvm.mlir.undef : !llvm.struct<"_QM__fortran_type_infoTcomponent", (struct<(ptr, i64, i32, i8, i8, i8, i8)>, i8, i8, i8, i8, array<4 x i8>, i64, struct<"_QM__fortran_type_infoTvalue", (i8, array<7 x i8>, i64)>, struct<(ptr, i64, i32, i8, i8, i8, i8, ptr, array<1 x i64>)>, struct<(ptr, i64, i32, i8, i8, i8, i8, array<1 x array<3 x i64>>, ptr, array<1 x i64>)>, struct<(ptr, i64, i32, i8, i8, i8, i8, array<2 x array<3 x i64>>, ptr, array<1 x i64>)>, struct<"_QM__fortran_builtinsT__builtin_c_ptr", (i64)>)>
    %8 = llvm.mlir.addressof @_QFE.n.c : !llvm.ptr
    %9 = llvm.mlir.constant(40 : i32) : i32
    %10 = llvm.mlir.zero : !llvm.ptr
    %11 = llvm.getelementptr %10[1] : (!llvm.ptr) -> !llvm.ptr, !llvm.array<1 x i8>
    %12 = llvm.ptrtoint %11 : !llvm.ptr to i64
    %13 = llvm.mlir.undef : !llvm.struct<(ptr, i64, i32, i8, i8, i8, i8)>
    %14 = llvm.insertvalue %12, %13[1] : !llvm.struct<(ptr, i64, i32, i8, i8, i8, i8)> 
    %15 = llvm.mlir.constant(20180515 : i32) : i32
    %16 = llvm.insertvalue %15, %14[2] : !llvm.struct<(ptr, i64, i32, i8, i8, i8, i8)> 
    %17 = llvm.mlir.constant(0 : i32) : i32
    %18 = llvm.trunc %17 : i32 to i8
    %19 = llvm.insertvalue %18, %16[3] : !llvm.struct<(ptr, i64, i32, i8, i8, i8, i8)> 
    %20 = llvm.trunc %9 : i32 to i8
    %21 = llvm.insertvalue %20, %19[4] : !llvm.struct<(ptr, i64, i32, i8, i8, i8, i8)> 
    %22 = llvm.mlir.constant(0 : i32) : i32
    %23 = llvm.trunc %22 : i32 to i8
    %24 = llvm.insertvalue %23, %21[5] : !llvm.struct<(ptr, i64, i32, i8, i8, i8, i8)> 
    %25 = llvm.mlir.constant(0 : i32) : i32
    %26 = llvm.trunc %25 : i32 to i8
    %27 = llvm.insertvalue %26, %24[6] : !llvm.struct<(ptr, i64, i32, i8, i8, i8, i8)> 
    %28 = llvm.insertvalue %8, %27[0] : !llvm.struct<(ptr, i64, i32, i8, i8, i8, i8)> 
    %29 = llvm.mlir.constant(1 : i64) : i64
    %30 = llvm.mlir.constant(40 : i32) : i32
    %31 = llvm.mlir.zero : !llvm.ptr
    %32 = llvm.getelementptr %31[1] : (!llvm.ptr) -> !llvm.ptr, i8
    %33 = llvm.ptrtoint %32 : !llvm.ptr to i64
    %34 = llvm.mul %33, %29  : i64
    %35 = llvm.mlir.undef : !llvm.struct<(ptr, i64, i32, i8, i8, i8, i8)>
    %36 = llvm.insertvalue %34, %35[1] : !llvm.struct<(ptr, i64, i32, i8, i8, i8, i8)> 
    %37 = llvm.mlir.constant(20180515 : i32) : i32
    %38 = llvm.insertvalue %37, %36[2] : !llvm.struct<(ptr, i64, i32, i8, i8, i8, i8)> 
    %39 = llvm.mlir.constant(0 : i32) : i32
    %40 = llvm.trunc %39 : i32 to i8
    %41 = llvm.insertvalue %40, %38[3] : !llvm.struct<(ptr, i64, i32, i8, i8, i8, i8)> 
    %42 = llvm.trunc %30 : i32 to i8
    %43 = llvm.insertvalue %42, %41[4] : !llvm.struct<(ptr, i64, i32, i8, i8, i8, i8)> 
    %44 = llvm.mlir.constant(1 : i32) : i32
    %45 = llvm.trunc %44 : i32 to i8
    %46 = llvm.insertvalue %45, %43[5] : !llvm.struct<(ptr, i64, i32, i8, i8, i8, i8)> 
    %47 = llvm.mlir.constant(0 : i32) : i32
    %48 = llvm.trunc %47 : i32 to i8
    %49 = llvm.insertvalue %48, %46[6] : !llvm.struct<(ptr, i64, i32, i8, i8, i8, i8)> 
    %50 = llvm.extractvalue %28[0] : !llvm.struct<(ptr, i64, i32, i8, i8, i8, i8)> 
    %51 = llvm.mlir.constant(0 : i64) : i64
    %52 = llvm.mlir.constant(1 : i64) : i64
    %53 = llvm.insertvalue %50, %49[0] : !llvm.struct<(ptr, i64, i32, i8, i8, i8, i8)> 
    %54 = llvm.insertvalue %53, %7[0] : !llvm.struct<"_QM__fortran_type_infoTcomponent", (struct<(ptr, i64, i32, i8, i8, i8, i8)>, i8, i8, i8, i8, array<4 x i8>, i64, struct<"_QM__fortran_type_infoTvalue", (i8, array<7 x i8>, i64)>, struct<(ptr, i64, i32, i8, i8, i8, i8, ptr, array<1 x i64>)>, struct<(ptr, i64, i32, i8, i8, i8, i8, array<1 x array<3 x i64>>, ptr, array<1 x i64>)>, struct<(ptr, i64, i32, i8, i8, i8, i8, array<2 x array<3 x i64>>, ptr, array<1 x i64>)>, struct<"_QM__fortran_builtinsT__builtin_c_ptr", (i64)>)> 
    %55 = llvm.insertvalue %5, %54[1] : !llvm.struct<"_QM__fortran_type_infoTcomponent", (struct<(ptr, i64, i32, i8, i8, i8, i8)>, i8, i8, i8, i8, array<4 x i8>, i64, struct<"_QM__fortran_type_infoTvalue", (i8, array<7 x i8>, i64)>, struct<(ptr, i64, i32, i8, i8, i8, i8, ptr, array<1 x i64>)>, struct<(ptr, i64, i32, i8, i8, i8, i8, array<1 x array<3 x i64>>, ptr, array<1 x i64>)>, struct<(ptr, i64, i32, i8, i8, i8, i8, array<2 x array<3 x i64>>, ptr, array<1 x i64>)>, struct<"_QM__fortran_builtinsT__builtin_c_ptr", (i64)>)> 
     :

yus3710-fj avatar Jan 12 '24 06:01 yus3710-fj

@llvm/issue-subscribers-flang-ir

Author: Yusuke MINATO (yus3710-fj)

This is an issue from Fujitsu testsuite.

Flang-new terminates abnormally when compiling the following program. When I checked with the top command, %MEM gradually increased to 100%. It seems that a lot of memory was used.

There is no problem with gfortran compiling on the same machine.

The following are the test program, Flang-new and gfortran compilation result, and the stack trace of Flang-new.

! test.f90
  type t1
    complex :: c = (0.0, 0.0)
  end type t1
  type(t1) val(1000000)

  print *, val(500000)
end
$ flang-new -v test.f90 
flang-new version 18.0.0git (https://github.com/llvm/llvm-project.git 2af0695b875911144ed1f7b4e5679be500b86a77)
Target: aarch64-unknown-linux-gnu
Thread model: posix
InstalledDir: /path/to/install/bin
Found candidate GCC installation: /opt/gcc/11.2.0/lib/gcc/aarch64-unknown-linux-gnu/11.2.0
Selected GCC installation: /opt/gcc/11.2.0/lib/gcc/aarch64-unknown-linux-gnu/11.2.0
Candidate multilib: .;@<!-- -->m64
Selected multilib: .;@<!-- -->m64
 "/path/to/install/bin/flang-new" -fc1 -triple aarch64-unknown-linux-gnu -emit-obj -fcolor-diagnostics -mrelocation-model pic -pic-level 2 -pic-is-pie -target-cpu generic -target-feature +neon -target-feature +v8a -mframe-pointer=non-leaf -o /tmp/test-f371a2.o -x f95-cpp-input test.f90
flang-new: error: unable to execute command: Killed
flang-new: error: flang frontend command failed due to signal (use -v to see invocation)
flang-new version 18.0.0git (https://github.com/llvm/llvm-project.git 2af0695b875911144ed1f7b4e5679be500b86a77)
Target: aarch64-unknown-linux-gnu
Thread model: posix
InstalledDir: /path/to/install/bin
flang-new: note: diagnostic msg: 
********************

PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT:
Preprocessed source(s) and associated run script(s) are located at:
flang-new: note: diagnostic msg: /tmp/test-e5be39
flang-new: note: diagnostic msg: /tmp/test-e5be39.sh
flang-new: note: diagnostic msg: 

********************
$ gfortran -v test.f90 
Driving: gfortran -v test.f90 -l gfortran -l m -shared-libgcc
Using built-in specs.
COLLECT_GCC=gfortran
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/aarch64-redhat-linux/8/lto-wrapper
Target: aarch64-redhat-linux
Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,fortran,lto --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --with-linker-hash-style=gnu --enable-plugin --enable-initfini-array --with-isl --disable-libmpx --enable-gnu-indirect-function --build=aarch64-redhat-linux
Thread model: posix
gcc version 8.3.1 20190507 (Red Hat 8.3.1-4) (GCC) 
 :
$
#<!-- -->0  0x00007ffff090ff7b in llvm::Constant::getAggregateElement (this=0x7ffc5236d810, Elt=117955)
    at /path/to/llvm-project/llvm/lib/IR/Constants.cpp:423
#<!-- -->1  0x00007ffff08f021c in llvm::ConstantFoldInsertValueInstruction (Agg=0x7ffc5236d810, Val=0x555555967600, Idxs=...)
    at /path/to/llvm-project/llvm/lib/IR/ConstantFold.cpp:605
#<!-- -->2  0x00007ffff0ad9596 in llvm::ConstantFolder::FoldInsertValue (this=0x7fffffffb9b8, Agg=0x7ffc5236d810, Val=0x555555967600, IdxList=...)
    at /path/to/llvm-project/llvm/include/llvm/IR/ConstantFolder.h:145
#<!-- -->3  0x00007fffdfa3d743 in llvm::IRBuilderBase::CreateInsertValue (this=0x7fffffffb940, Agg=0x7ffc5236d810, Val=0x555555967600, Idxs=..., Name=...)
    at /path/to/llvm-project/llvm/include/llvm/IR/IRBuilder.h:2498
#<!-- -->4  0x00007fffdfa177e4 in convertOperationImpl (opInst=..., builder=..., moduleTranslation=...)
    at /path/to/build/tools/mlir/include/mlir/Dialect/LLVMIR/LLVMConversions.inc:211
#<!-- -->5  0x00007fffdfa2bccb in (anonymous namespace)::LLVMDialectLLVMIRTranslationInterface::convertOperation (this=0x5555555ed730, op=0x5555558fbb90,
    builder=..., moduleTranslation=...) at /path/to/llvm-project/mlir/lib/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.cpp:415
#<!-- -->6  0x00007fffe487ab27 in mlir::LLVM::ModuleTranslation::convertOperation (this=0x7fffffffbc00, op=..., builder=..., recordInsertions=false)
    at /path/to/llvm-project/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp:753
#<!-- -->7  0x00007fffe487ba28 in mlir::LLVM::ModuleTranslation::convertGlobals (this=0x7fffffffbc00)
    at /path/to/llvm-project/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp:925
#<!-- -->8  0x00007fffe4880201 in mlir::translateModuleToLLVMIR (module=0x5555556752c0, llvmContext=..., name=...)
    at /path/to/llvm-project/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp:1566
#<!-- -->9  0x00007ffff6c17c69 in Fortran::frontend::CodeGenAction::generateLLVMIR (this=0x5555555998b0)
    at /path/to/llvm-project/flang/lib/Frontend/FrontendActions.cpp:777
#<!-- -->10 0x00007ffff6c192f7 in Fortran::frontend::CodeGenAction::executeAction (this=0x5555555998b0)
    at /path/to/llvm-project/flang/lib/Frontend/FrontendActions.cpp:1148
#<!-- -->11 0x00007ffff6c10650 in Fortran::frontend::FrontendAction::execute (this=0x5555555998b0)
    at /path/to/llvm-project/flang/lib/Frontend/FrontendAction.cpp:112
#<!-- -->12 0x00007ffff6be0c11 in Fortran::frontend::CompilerInstance::executeAction (this=0x555555594970, act=...)
    at /path/to/llvm-project/flang/lib/Frontend/CompilerInstance.cpp:173
#<!-- -->13 0x00007ffff71952b7 in Fortran::frontend::executeCompilerInvocation (flang=0x555555594970)
    at /path/to/llvm-project/flang/lib/FrontendTool/ExecuteCompilerInvocation.cpp:221
#<!-- -->14 0x000055555555e2c5 in fc1_main (argv=..., argv0=0x7fffffffe4ca "/path/to/build/bin/flang-new")
    at /path/to/llvm-project/flang/tools/flang-driver/fc1_main.cpp:67
#<!-- -->15 0x0000555555557509 in executeFC1Tool (argV=...) at /path/to/llvm-project/flang/tools/flang-driver/driver.cpp:66
#<!-- -->16 0x00005555555579de in main (argc=19, argv=0x7fffffffe1d8) at /path/to/llvm-project/flang/tools/flang-driver/driver.cpp:109

The following is a similar program but an array size is smaller.

! small_test.f90
  type t1
    complex :: c = (0.0, 0.0)
  end type t1
  type(t1) val(100)

  print *, val(50)
end

That can be compiled normally, but Flang generates large MLIR with LLVM dialect. I suspect this is the cause of the abnormal termination.

$ flang-new -Xflang -save-temps small_test.f90
$ cat small_test-llvmir.mlir
module attributes {dlti.dl_spec = #dlti.dl_spec&lt;#dlti.dl_entry&lt;i1, dense&lt;8&gt; : vector&lt;2xi64&gt;&gt;, #dlti.dl_entry&lt;!llvm.ptr, dense&lt;64&gt; : vector&lt;4xi64&gt;&gt;, #dlti.dl_entry&lt;f16, dense&lt;16&gt; : vector&lt;2xi64&gt;&gt;, #dlti.dl_entry&lt;i32, dense&lt;32&gt; : vector&lt;2xi64&gt;&gt;, #dlti.dl_entry&lt;i16, dense&lt;[16, 32]&gt; : vector&lt;2xi64&gt;&gt;, #dlti.dl_entry&lt;i8, dense&lt;[8, 32]&gt; : vector&lt;2xi64&gt;&gt;, #dlti.dl_entry&lt;f64, dense&lt;64&gt; : vector&lt;2xi64&gt;&gt;, #dlti.dl_entry&lt;i128, dense&lt;128&gt; : vector&lt;2xi64&gt;&gt;, #dlti.dl_entry&lt;f128, dense&lt;128&gt; : vector&lt;2xi64&gt;&gt;, #dlti.dl_entry&lt;i64, dense&lt;64&gt; : vector&lt;2xi64&gt;&gt;, #dlti.dl_entry&lt;"dlti.stack_alignment", 128 : i64&gt;, #dlti.dl_entry&lt;"dlti.endianness", "little"&gt;&gt;, fir.defaultkind = "a1c4d8i4l4r4", fir.kindmap = "", llvm.data_layout = "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128", llvm.target_triple = "aarch64-unknown-linux-gnu"} {
  llvm.func @<!-- -->_QQmain() attributes {frame_pointer = #llvm.framePointerKind&lt;"non-leaf"&gt;} {
    %0 = llvm.mlir.constant(1 : i32) : i32
     :
    llvm.return
  }
   :
  llvm.mlir.global internal @<!-- -->_QFEval() {addr_space = 0 : i32} : !llvm.array&lt;100 x struct&lt;"_QFTt1", (struct&lt;(f32, f32)&gt;)&gt;&gt; {
    %0 = llvm.mlir.constant(0.000000e+00 : f32) : f32
    %1 = llvm.mlir.undef : !llvm.struct&lt;"_QFTt1", (struct&lt;(f32, f32)&gt;)&gt;
    %2 = llvm.mlir.undef : !llvm.struct&lt;(f32, f32)&gt;
    %3 = llvm.insertvalue %0, %2[0] : !llvm.struct&lt;(f32, f32)&gt; 
    %4 = llvm.insertvalue %0, %3[1] : !llvm.struct&lt;(f32, f32)&gt; 
    %5 = llvm.insertvalue %4, %1[0] : !llvm.struct&lt;"_QFTt1", (struct&lt;(f32, f32)&gt;)&gt; 
    %6 = llvm.mlir.undef : !llvm.array&lt;100 x struct&lt;"_QFTt1", (struct&lt;(f32, f32)&gt;)&gt;&gt;
    %7 = llvm.insertvalue %5, %6[0] : !llvm.array&lt;100 x struct&lt;"_QFTt1", (struct&lt;(f32, f32)&gt;)&gt;&gt; 
    %8 = llvm.insertvalue %5, %7[1] : !llvm.array&lt;100 x struct&lt;"_QFTt1", (struct&lt;(f32, f32)&gt;)&gt;&gt; 
    %9 = llvm.insertvalue %5, %8[2] : !llvm.array&lt;100 x struct&lt;"_QFTt1", (struct&lt;(f32, f32)&gt;)&gt;&gt; 
    %10 = llvm.insertvalue %5, %9[3] : !llvm.array&lt;100 x struct&lt;"_QFTt1", (struct&lt;(f32, f32)&gt;)&gt;&gt; 
     :
    %106 = llvm.insertvalue %5, %105[99] : !llvm.array&lt;100 x struct&lt;"_QFTt1", (struct&lt;(f32, f32)&gt;)&gt;&gt; 
    llvm.return %106 : !llvm.array&lt;100 x struct&lt;"_QFTt1", (struct&lt;(f32, f32)&gt;)&gt;&gt;
  }
  llvm.mlir.global linkonce_odr constant @<!-- -->_QFE.c.t1() comdat(@<!-- -->__llvm_comdat::@<!-- -->_QFE.c.t1) {addr_space = 0 : i32} : !llvm.array&lt;1 x struct&lt;"_QM__fortran_type_infoTcomponent", (struct&lt;(ptr, i64, i32, i8, i8, i8, i8)&gt;, i8, i8, i8, i8, array&lt;4 x i8&gt;, i64, struct&lt;"_QM__fortran_type_infoTvalue", (i8, array&lt;7 x i8&gt;, i64)&gt;, struct&lt;(ptr, i64, i32, i8, i8, i8, i8, ptr, array&lt;1 x i64&gt;)&gt;, struct&lt;(ptr, i64, i32, i8, i8, i8, i8, array&lt;1 x array&lt;3 x i64&gt;&gt;, ptr, array&lt;1 x i64&gt;)&gt;, struct&lt;(ptr, i64, i32, i8, i8, i8, i8, array&lt;2 x array&lt;3 x i64&gt;&gt;, ptr, array&lt;1 x i64&gt;)&gt;, struct&lt;"_QM__fortran_builtinsT__builtin_c_ptr", (i64)&gt;)&gt;&gt; {
    %0 = llvm.mlir.constant(0 : index) : i64
    %1 = llvm.mlir.constant(0 : i64) : i64
    %2 = llvm.mlir.constant(0 : i8) : i8
    %3 = llvm.mlir.constant(4 : i8) : i8
    %4 = llvm.mlir.constant(2 : i8) : i8
    %5 = llvm.mlir.constant(1 : i8) : i8
    %6 = llvm.mlir.undef : !llvm.array&lt;1 x struct&lt;"_QM__fortran_type_infoTcomponent", (struct&lt;(ptr, i64, i32, i8, i8, i8, i8)&gt;, i8, i8, i8, i8, array&lt;4 x i8&gt;, i64, struct&lt;"_QM__fortran_type_infoTvalue", (i8, array&lt;7 x i8&gt;, i64)&gt;, struct&lt;(ptr, i64, i32, i8, i8, i8, i8, ptr, array&lt;1 x i64&gt;)&gt;, struct&lt;(ptr, i64, i32, i8, i8, i8, i8, array&lt;1 x array&lt;3 x i64&gt;&gt;, ptr, array&lt;1 x i64&gt;)&gt;, struct&lt;(ptr, i64, i32, i8, i8, i8, i8, array&lt;2 x array&lt;3 x i64&gt;&gt;, ptr, array&lt;1 x i64&gt;)&gt;, struct&lt;"_QM__fortran_builtinsT__builtin_c_ptr", (i64)&gt;)&gt;&gt;
    %7 = llvm.mlir.undef : !llvm.struct&lt;"_QM__fortran_type_infoTcomponent", (struct&lt;(ptr, i64, i32, i8, i8, i8, i8)&gt;, i8, i8, i8, i8, array&lt;4 x i8&gt;, i64, struct&lt;"_QM__fortran_type_infoTvalue", (i8, array&lt;7 x i8&gt;, i64)&gt;, struct&lt;(ptr, i64, i32, i8, i8, i8, i8, ptr, array&lt;1 x i64&gt;)&gt;, struct&lt;(ptr, i64, i32, i8, i8, i8, i8, array&lt;1 x array&lt;3 x i64&gt;&gt;, ptr, array&lt;1 x i64&gt;)&gt;, struct&lt;(ptr, i64, i32, i8, i8, i8, i8, array&lt;2 x array&lt;3 x i64&gt;&gt;, ptr, array&lt;1 x i64&gt;)&gt;, struct&lt;"_QM__fortran_builtinsT__builtin_c_ptr", (i64)&gt;)&gt;
    %8 = llvm.mlir.addressof @<!-- -->_QFE.n.c : !llvm.ptr
    %9 = llvm.mlir.constant(40 : i32) : i32
    %10 = llvm.mlir.zero : !llvm.ptr
    %11 = llvm.getelementptr %10[1] : (!llvm.ptr) -&gt; !llvm.ptr, !llvm.array&lt;1 x i8&gt;
    %12 = llvm.ptrtoint %11 : !llvm.ptr to i64
    %13 = llvm.mlir.undef : !llvm.struct&lt;(ptr, i64, i32, i8, i8, i8, i8)&gt;
    %14 = llvm.insertvalue %12, %13[1] : !llvm.struct&lt;(ptr, i64, i32, i8, i8, i8, i8)&gt; 
    %15 = llvm.mlir.constant(20180515 : i32) : i32
    %16 = llvm.insertvalue %15, %14[2] : !llvm.struct&lt;(ptr, i64, i32, i8, i8, i8, i8)&gt; 
    %17 = llvm.mlir.constant(0 : i32) : i32
    %18 = llvm.trunc %17 : i32 to i8
    %19 = llvm.insertvalue %18, %16[3] : !llvm.struct&lt;(ptr, i64, i32, i8, i8, i8, i8)&gt; 
    %20 = llvm.trunc %9 : i32 to i8
    %21 = llvm.insertvalue %20, %19[4] : !llvm.struct&lt;(ptr, i64, i32, i8, i8, i8, i8)&gt; 
    %22 = llvm.mlir.constant(0 : i32) : i32
    %23 = llvm.trunc %22 : i32 to i8
    %24 = llvm.insertvalue %23, %21[5] : !llvm.struct&lt;(ptr, i64, i32, i8, i8, i8, i8)&gt; 
    %25 = llvm.mlir.constant(0 : i32) : i32
    %26 = llvm.trunc %25 : i32 to i8
    %27 = llvm.insertvalue %26, %24[6] : !llvm.struct&lt;(ptr, i64, i32, i8, i8, i8, i8)&gt; 
    %28 = llvm.insertvalue %8, %27[0] : !llvm.struct&lt;(ptr, i64, i32, i8, i8, i8, i8)&gt; 
    %29 = llvm.mlir.constant(1 : i64) : i64
    %30 = llvm.mlir.constant(40 : i32) : i32
    %31 = llvm.mlir.zero : !llvm.ptr
    %32 = llvm.getelementptr %31[1] : (!llvm.ptr) -&gt; !llvm.ptr, i8
    %33 = llvm.ptrtoint %32 : !llvm.ptr to i64
    %34 = llvm.mul %33, %29  : i64
    %35 = llvm.mlir.undef : !llvm.struct&lt;(ptr, i64, i32, i8, i8, i8, i8)&gt;
    %36 = llvm.insertvalue %34, %35[1] : !llvm.struct&lt;(ptr, i64, i32, i8, i8, i8, i8)&gt; 
    %37 = llvm.mlir.constant(20180515 : i32) : i32
    %38 = llvm.insertvalue %37, %36[2] : !llvm.struct&lt;(ptr, i64, i32, i8, i8, i8, i8)&gt; 
    %39 = llvm.mlir.constant(0 : i32) : i32
    %40 = llvm.trunc %39 : i32 to i8
    %41 = llvm.insertvalue %40, %38[3] : !llvm.struct&lt;(ptr, i64, i32, i8, i8, i8, i8)&gt; 
    %42 = llvm.trunc %30 : i32 to i8
    %43 = llvm.insertvalue %42, %41[4] : !llvm.struct&lt;(ptr, i64, i32, i8, i8, i8, i8)&gt; 
    %44 = llvm.mlir.constant(1 : i32) : i32
    %45 = llvm.trunc %44 : i32 to i8
    %46 = llvm.insertvalue %45, %43[5] : !llvm.struct&lt;(ptr, i64, i32, i8, i8, i8, i8)&gt; 
    %47 = llvm.mlir.constant(0 : i32) : i32
    %48 = llvm.trunc %47 : i32 to i8
    %49 = llvm.insertvalue %48, %46[6] : !llvm.struct&lt;(ptr, i64, i32, i8, i8, i8, i8)&gt; 
    %50 = llvm.extractvalue %28[0] : !llvm.struct&lt;(ptr, i64, i32, i8, i8, i8, i8)&gt; 
    %51 = llvm.mlir.constant(0 : i64) : i64
    %52 = llvm.mlir.constant(1 : i64) : i64
    %53 = llvm.insertvalue %50, %49[0] : !llvm.struct&lt;(ptr, i64, i32, i8, i8, i8, i8)&gt; 
    %54 = llvm.insertvalue %53, %7[0] : !llvm.struct&lt;"_QM__fortran_type_infoTcomponent", (struct&lt;(ptr, i64, i32, i8, i8, i8, i8)&gt;, i8, i8, i8, i8, array&lt;4 x i8&gt;, i64, struct&lt;"_QM__fortran_type_infoTvalue", (i8, array&lt;7 x i8&gt;, i64)&gt;, struct&lt;(ptr, i64, i32, i8, i8, i8, i8, ptr, array&lt;1 x i64&gt;)&gt;, struct&lt;(ptr, i64, i32, i8, i8, i8, i8, array&lt;1 x array&lt;3 x i64&gt;&gt;, ptr, array&lt;1 x i64&gt;)&gt;, struct&lt;(ptr, i64, i32, i8, i8, i8, i8, array&lt;2 x array&lt;3 x i64&gt;&gt;, ptr, array&lt;1 x i64&gt;)&gt;, struct&lt;"_QM__fortran_builtinsT__builtin_c_ptr", (i64)&gt;)&gt; 
    %55 = llvm.insertvalue %5, %54[1] : !llvm.struct&lt;"_QM__fortran_type_infoTcomponent", (struct&lt;(ptr, i64, i32, i8, i8, i8, i8)&gt;, i8, i8, i8, i8, array&lt;4 x i8&gt;, i64, struct&lt;"_QM__fortran_type_infoTvalue", (i8, array&lt;7 x i8&gt;, i64)&gt;, struct&lt;(ptr, i64, i32, i8, i8, i8, i8, ptr, array&lt;1 x i64&gt;)&gt;, struct&lt;(ptr, i64, i32, i8, i8, i8, i8, array&lt;1 x array&lt;3 x i64&gt;&gt;, ptr, array&lt;1 x i64&gt;)&gt;, struct&lt;(ptr, i64, i32, i8, i8, i8, i8, array&lt;2 x array&lt;3 x i64&gt;&gt;, ptr, array&lt;1 x i64&gt;)&gt;, struct&lt;"_QM__fortran_builtinsT__builtin_c_ptr", (i64)&gt;)&gt; 
     :

llvmbot avatar Jan 12 '24 06:01 llvmbot

Confirmed.

ceseo avatar Feb 15 '24 21:02 ceseo

This looks like a stack starvation caused by the recursive calls in ConstantFoldInsertValueInstruction. Working on a fix.

ceseo avatar Apr 04 '24 19:04 ceseo

@yus3710-fj could you please test this patch and see if it solves the problem for you?

It takes hours to compile your first example, but memory usage is constant.

ceseo avatar Apr 10 '24 21:04 ceseo

@ceseo, thank you for the patch. The exhaustion of the memory seems to be fixed and the compilation terminates normally.

As you said, the compilation time is quite long. I'll create another issue about this later.

$ time flang-new test_org.f90

real    969m18.953s
user    967m33.702s
sys     1m40.523s

yus3710-fj avatar Apr 12 '24 02:04 yus3710-fj

@yus3710-fj thanks! I'll submit the PR.

ceseo avatar Apr 12 '24 15:04 ceseo

@yus3710-fj this seems to be fixed. Could you please confirm and close?

$ cat test.f90
! test.f90
  type t1
    complex :: c = (0.0, 0.0)
  end type t1
  type(t1) val(1000000)

  print *, val(500000)
end
$ time ~/.local/llvm-head/bin/flang test.f90 -o test

real    0m0.573s
user    0m0.466s
sys     0m0.134s

ceseo avatar Aug 28 '25 18:08 ceseo

Thank you for the notice. I've confirmed that this is resolved.

yus3710-fj avatar Aug 29 '25 01:08 yus3710-fj