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

[Flang] Missing complex exponentiation function

Open kiranchandramohan opened this issue 4 years ago • 14 comments

For a complex exponentiation function, Flang currently generates a call to fc_exp_1 in the pgmath library. This function does not seem to have an implementation in the pgmath runtime. We have to either add an implementation for complex exponentiation using functions from https://en.cppreference.com/w/c/numeric/c or use the exponentiation that works on floats (mth_i_cexp) like in classic flang. Location of pgmath: https://github.com/flang-compiler/flang/tree/master/runtime/libpgmath

An alternative way would be to do custom handling for complex exponentiation inside the file which handles lowering of intrinsic functions (flang/lib/Lower/IntrinsicCall.cpp). Custom handling can be generating an MLIR dialect complex operation, and adding calls to the conversion pass that lowers to libm calls. Handling could also be to extract the parts (complex, imaginary) and then call the non-complex exponentiation function in pgmath.

Maybe an RFC to discuss the issue first would be a good idea.

Simple reproducer

  complex :: i
  i = (0.0, 1.0)
  print *, exp(i)
end

Runtime Error

Error during math dispatch processing...                                         
  2 __pgmath_abort:Math dispatch table is either misconfigured or corrupted.exit 1

This issue was found while investigating 549.fotonik3d_r.

kiranchandramohan avatar Jun 02 '22 14:06 kiranchandramohan

@llvm/issue-subscribers-flang-runtime

llvmbot avatar Jun 02 '22 14:06 llvmbot

@llvm/issue-subscribers-flang-ir

llvmbot avatar Jun 02 '22 14:06 llvmbot

$ flang test.f90 && ./a.out
 (0.5403023,0.8414710)

@kiranchandramohan I checked my built classic flang and it works for me. Can you check if there is something wrong with the build?

PeixinQiao avatar Jul 01 '22 01:07 PeixinQiao

Did you try on an AArch64/X86 machine? If you tried on X86, could you try on AArch64, and if it only happens there I can change the summary to restrict this to AArch64.

kiranchandramohan avatar Jul 01 '22 01:07 kiranchandramohan

I tried on aarch64. Haven't try it on X86.

PeixinQiao avatar Jul 01 '22 01:07 PeixinQiao

Is this with a pgmath version from upstream classic-flang?

kiranchandramohan avatar Jul 01 '22 01:07 kiranchandramohan

Yes. I tried my baseline version, which is the upstream version.

classic-flang$ git show HEAD
commit 4bb8c37601972aa2ca0e5b74a842b1affb0dfdee (HEAD -> master, tag: flang_20210324, origin/1714-test-baseline)
Author: Bryan Chan <[email protected]>
Date:   Sun Mar 14 09:21:26 2021 -0400

    [libpgmath] Use Python 3 to run tests
classic-flang-llvm-project]$ git show HEAD
commit 5b601378fcb0f78e880068b31c3e84f8756d0cba (HEAD -> release_12x, origin/release_12x)
Author: michalpasztamobica <[email protected]>
Date:   Fri Mar 19 10:41:22 2021 +0100

    Remove unnecessary caret from path in classic-flang test
buildtype=Debug #Release
installdir=$PWD/install
builddir=$PWD/build

mkdir -p $builddir/llvm
pushd $builddir/llvm
cmake -G Ninja \
  -DCMAKE_BUILD_TYPE=$buildtype \
  -DCMAKE_INSTALL_PREFIX=$installdir \
  -DCMAKE_C_COMPILER=gcc \
  -DCMAKE_CXX_COMPILER=g++ \
  -DCLANG_OPENMP_NVPTX_DEFAULT_ARCH=sm_70 \
  -DLLVM_ENABLE_PROJECTS="clang" \
  -DLLVM_ENABLE_CLASSIC_FLANG=on \
  -DLLVM_ENABLE_ASSERTIONS=on \
  -DLLVM_OPTIMIZED_TABLEGEN=on \
  -DLLVM_TARGETS_TO_BUILD="AArch64;NVPTX;X86" \
  ../../classic-flang-llvm-project/llvm || exit 1
#ninja check-all install || exit 1
ninja install || exit 1
popd

if ! llvm_lit="$(realpath --canonicalize-existing $builddir/llvm/bin/llvm-lit 2>/dev/null)" || \
    [ ! -x "$llvm_lit" ]; then
  echo "no usable llvm-lit"
  exit 1
fi

mkdir -p $builddir/openmp-shared
pushd $builddir/openmp-shared
cmake -G Ninja \
  -DCMAKE_BUILD_TYPE=$buildtype \
  -DCMAKE_INSTALL_PREFIX=$installdir \
  -DCMAKE_C_COMPILER=$installdir/bin/clang \
  -DCMAKE_CXX_COMPILER=$installdir/bin/clang++ \
  -DOPENMP_LLVM_TOOLS_DIR="$(dirname $llvm_lit)" \
  -DLIBOMPTARGET_NVPTX_COMPUTE_CAPABILITIES=70 \
  ../../classic-flang-llvm-project/openmp || exit 1
ninja install || exit 1
popd

mkdir -p $builddir/libpgmath
pushd $builddir/libpgmath
cmake -G Ninja \
  -DCMAKE_BUILD_TYPE=$buildtype \
  -DCMAKE_INSTALL_PREFIX=$installdir \
  -DCMAKE_C_COMPILER=$installdir/bin/clang \
  -DCMAKE_CXX_COMPILER=$installdir/bin/clang++ \
  -DLLVM_CONFIG=$installdir/bin/llvm-config \
  -DLLVM_TARGETS_TO_BUILD="AArch64;X86" \
  -DLIBPGMATH_LLVM_LIT_EXECUTABLE="$llvm_lit" \
  ../../classic-flang/runtime/libpgmath || exit 1
ninja install || exit 1
#ninja check-libpgmath install || exit 1
popd

mkdir -p $builddir/classic-flang
pushd $builddir/classic-flang
cmake -Wno-dev -G 'Unix Makefiles' \
  -DCMAKE_BUILD_TYPE=$buildtype \
  -DCMAKE_INSTALL_PREFIX=$installdir \
  -DCMAKE_C_COMPILER=$installdir/bin/clang \
  -DCMAKE_CXX_COMPILER=$installdir/bin/clang++ \
  -DCMAKE_Fortran_COMPILER=$installdir/bin/flang \
  -DCMAKE_Fortran_COMPILER_ID=Flang \
  -DFLANG_LLVM_EXTENSIONS=on \
  -DLLVM_INCLUDE_DOCS=on \
  -DLLVM_ENABLE_DOXYGEN=on \
  -DLLVM_CONFIG=$installdir/bin/llvm-config \
  -DLLVM_TARGETS_TO_BUILD="AArch64;X86" \
  -DLLVM_EXTERNAL_LIT="$llvm_lit" \
  -DWITH_WERROR=off \
  ../../classic-flang || exit 1
make -j $1 install || exit 1
#make check-all || exit 1

PeixinQiao avatar Jul 01 '22 01:07 PeixinQiao

I get the runtime error when I try.

BTW, do you see the call to the complex exponentiation function in FIR?

%8 = fir.call @__fc_exp_1(%7) : (!fir.complex<4>) -> !fir.complex<4>

kiranchandramohan avatar Jul 01 '22 01:07 kiranchandramohan

$ bbc -emit-fir test.f90 $ cat test.mlir func.func @_QQmain() { %0 = fir.alloca !fir.complex<4> {bindc_name = "i", uniq_name = "_QFEi"} %cst = arith.constant 0.000000e+00 : f32 %cst_0 = arith.constant 1.000000e+00 : f32 %1 = fir.undefined !fir.complex<4> %2 = fir.insert_value %1, %cst, [0 : index] : (!fir.complex<4>, f32) -> !fir.complex<4> %3 = fir.insert_value %2, %cst_0, [1 : index] : (!fir.complex<4>, f32) -> !fir.complex<4> fir.store %3 to %0 : !fir.ref<!fir.complex<4>> %c-1_i32 = arith.constant -1 : i32 %4 = fir.address_of(@_QQcl.2E2F746573742E66393000) : !fir.ref<!fir.char<1,11>> %5 = fir.convert %4 : (!fir.ref<!fir.char<1,11>>) -> !fir.ref %c3_i32 = arith.constant 3 : i32 %6 = fir.call @_FortranAioBeginExternalListOutput(%c-1_i32, %5, %c3_i32) : (i32, !fir.ref, i32) -> !fir.ref %7 = fir.load %0 : !fir.ref<!fir.complex<4>> %8 = fir.call @__fc_exp_1(%7) : (!fir.complex<4>) -> !fir.complex<4> %9 = fir.extract_value %8, [0 : index] : (!fir.complex<4>) -> f32 %10 = fir.extract_value %8, [1 : index] : (!fir.complex<4>) -> f32 %11 = fir.call @_FortranAioOutputComplex32(%6, %9, %10) : (!fir.ref, f32, f32) -> i1 %12 = fir.call @_FortranAioEndIoStatement(%6) : (!fir.ref) -> i32 return } func.func private @_FortranAioBeginExternalListOutput(i32, !fir.ref, i32) -> !fir.ref attributes {fir.io, fir.runtime} fir.global linkonce @_QQcl.2E2F746573742E66393000 constant : !fir.char<1,11> { %0 = fir.string_lit "./test.f90\00"(11) : !fir.char<1,11> fir.has_value %0 : !fir.char<1,11> } func.func private @_FortranAioOutputComplex32(!fir.ref, f32, f32) -> i1 attributes {fir.io, fir.runtime} func.func private @__fc_exp_1(!fir.complex<4>) -> !fir.complex<4> attributes {fir.runtime} func.func private @_FortranAioEndIoStatement(!fir.ref) -> i32 attributes {fir.io, fir.runtime}

PeixinQiao avatar Jul 01 '22 02:07 PeixinQiao

$ flang-new -flang-experimental-exec test.f90 -L/path-to-classic-flang/baseline-12/install/lib/ -lpgmath && ./a.out Error during math dispatch processing... __pgmath_abort:Math dispatch table is either misconfigured or corrupted.

PeixinQiao avatar Jul 01 '22 02:07 PeixinQiao

Did you try on an AArch64/X86 machine? If you tried on X86, could you try on AArch64, and if it only happens there I can change the summary to restrict this to AArch64

Just in case it is still helpful/I think this came up in the community meeting but just to have it here as well: I think this is an aarch64-specific issue as 549.fotonik3d_r passes for me on x86_64 (test data) but fails on aarch64 as described (and same for the test case provided above).

Also, I think 459.GemsFDTD in SPEC2006 has a similar issue with fz_exp_1 in case anyone else runs into this.

jonathonpenix avatar Sep 01 '22 23:09 jonathonpenix

I'm seeing this error on PowerPC as well. Test case:

program foo
    complex :: c
    c = 2.0
    write(*,*) sqrt(c)
end program

Error when running a.out:

__pgmath_abort:Math dispatch table is either misconfigured or corrupted.

rzurob avatar Sep 26 '22 13:09 rzurob

I've posted a patch that fixes this for me on AArch64 by calling into libm's exp function instead of trying to get one from pgmath: https://reviews.llvm.org/D134655. Can you check if this also fixes PowerPC? Thanks!

DavidTruby avatar Sep 26 '22 17:09 DavidTruby

Thank, David. I confirmed that your changes work on PowerPC.

$ ./a.out
 (1.4142135,0.)
$

rzurob avatar Sep 26 '22 17:09 rzurob