[Flang] Missing complex exponentiation function
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.
@llvm/issue-subscribers-flang-runtime
@llvm/issue-subscribers-flang-ir
$ 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?
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.
I tried on aarch64. Haven't try it on X86.
Is this with a pgmath version from upstream classic-flang?
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
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>
$ 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
$ 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.
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.
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.
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!
Thank, David. I confirmed that your changes work on PowerPC.
$ ./a.out
(1.4142135,0.)
$