flang test questions

All,

I’m getting started with writing tests for flang and have a few questions. First, would you recommend contributing tests to the llvm-test-suite repository at https://github.com/llvm/llvm-test-suite?

Second, I was informed in April that the f18-llvm-project fork’s fir-dev branch is able to produce executable files from Fortran 77 source. Could someone explain how to use this capability and whether this capability has been merged upstream to the llvm-project repository? I can see from an error message that my locally installed flang is invoking gfortran to produce executable files. Do I need to pass a flag to tell flang to produce an executable program without invoking gfortran?

Damian

Yes, I think the llvm-test-suite should feature more Fortran tests.
See the documentation at [1] and [2] as an example for how tests are
added. For instance, NASA Advanced Supercomputing Parallel Benchmarks
[3] could be added.

[1] Fortran Tests in the LLVM Test Suite — The Flang Compiler
[2] ⚙ D104150 [test-suite] NIST Fortran Compiler Validation Suite.
[3] NAS Parallel Benchmarks

Michael

Hi Damian,

Second, I was informed in April that the f18-llvm-project fork's fir-dev branch is able to produce executable files from Fortran 77 source.
Could someone explain how to use this capability and whether this capability has been merged upstream to the llvm-project repository?

Merging this upstream is an ongoing process. I am not active in this area so can't comment on timescales, but it is one of our main challenges right now.

As for compiling with the fir-dev branch on f18-llvm-project, we have been recently discussing this on flang-compiler.slack.com. Are you active there? I can send you an invite.

Do I need to pass a flag to tell flang to produce an executable program without invoking gfortran?

`flang` that you are referring to is just a bash wrapper script [1] that was added for experimenting. It will only use LLVM Flang to parse-unparse the input source, and then will call e.g. `gfortran` for code-generation. You can replace `gfortran` with some other Fortran compiler. The name is rather confusing. We should rename it as `flang-to-gnu` or something similar.

Welcome aboard! :slight_smile:

-Andrzej

[1] https://github.com/llvm/llvm-project/blob/main/flang/tools/f18/flang.in

Hello Damian,

If you intend to use the fir-dev branch. You may find the following instructions helpful.

  1. You can build Flang on the fir-dev branch by enabling the flang project along with clang, mlir and openmp.
    Note: Please change TARGETS_TO_BUILD to your platform (X86, PowerPC).

git clone https://github.com/flang-compiler/f18-llvm-project.git
cd f18-llvm-project

git checkout fir-dev

mkdir build

cd build

cmake -G Ninja …/llvm -DLLVM_ENABLE_PROJECTS=“flang;clang;mlir;openmp” -DLLVM_TARGETS_TO_BUILD=AArch64 -DCMAKE_BUILD_TYPE=Release

nice -n 10 ninja

  1. Currently there is no driver support for end-to-end codegen. You must use two developer tools (bbc and tco) to generate llvm IR and then use clang or gcc to link and generate the executable. Two sample example invocations are given below. A few tests are also end-to-end and you may also use those as a reference. (https://github.com/flang-compiler/f18-llvm-project/blob/fir-dev/flang/test/Lower/end-to-end-always-exec-loopbody.f90).
    Note: Ryuta Suzuki created a script that might help here. (https://flang-compiler.slack.com/archives/C5C58TT32/p1625041964086400?thread_ts=1624955427.084800&cid=C5C58TT32)

input=“fraction.f90”

bbc $input -o - | tco | llc --relocation-model=pic --filetype=obj -o $input.o

gcc $input.o -L/…/f18-llvm-project/install/lib -lFortran_main -lFortranRuntime -lFortranDecimal -lm -o $input.out

./bin/bbc -emit-fir array.f90 -o - | ./bin/tco &> array.ll
./bin/clang++ array.ll -lFortran_main -lFortranRuntime -lFortranDecimal

LD_LIBRARY_PATH=$HOME/f18-llvm-project/build/lib ./a.out

  1. It is possible that the linking process might complain about some math symbols. If so, you must download classic flang and place the library libpgmath.so somewhere and point to it in the link step.

git clone https://github.com/flang-compiler/flang.git

cd flang/runtime/libpgmath

mkdir build

cd build

cmake …/

make

And then you can find lipgmath.* in the lib directory in build.

(There might be an option to build pgmath automatically if you set an environment variable. There might also be an option to use mlir/libm math functions instead of pgmath. But I have not explored these options.)

Thanks,
Kiran

If these are tests for Fortran intrinsics then wouldn’t the better place be somewhere in the flang directory? Like in unit-tests or something?

–Kiran

After some discussion offline, I decided to put the tests in the LLVM repository – presumably the flang directory – instead of in llvm-test-suite.

Damian

Hi Damian,

For a bit broader visibility…

We recently added a (temporary?) directory for intrinsic tests under “flang/test/Intrinsics” but we still need to decide how to best integrate it into the testing infrastructure. For now, my vote would be that it is OK to land your tests there and we can then decide where these tests should live in the long run. Personally, I think the intrinsic specific details (both lowering and “correctness”) make a bit more sense as part flang’s testing vs. rolling them all the way up to the llvm level. But that’s just my take — others should chime in if there is a better path…

—Pat

I agree with Pat. For me, anything that tests one of Flang's internal representations (parse-tree, FIR, LLVM IR) or the generated diagnostics, belongs somewhere within flang/test (i.e. with other regression tests). I always run all of them (ninja check-flang) when developing a patch. I rarely use the test-suite. Not sure about the others. Btw, LLVM's test-suite requires CMake support, which we don't have. But perhaps I'm missing something here!

As for the actual location for new tests, I've just submitted a patch to move all of regression tests for lowering the intrinsics into f18-llvm-project/flang/test/Lower/intrinsic-procedures [1]. These tests only look at FIR. We may want a different directory for LLVM IR. It would be very nice to keep all tests for intrinsics in sync :slight_smile:

Thanks for working on this!

-Andrzej

[1] https://github.com/flang-compiler/f18-llvm-project/pull/927/

I believe, Pat and Damian are talking about execution tests for Fortran intrinsics. While these are not testing the intermediate representations, these are unit-tests that only apply to Flang. And when we make changes to the compiler, it will be good to test (with check-flang) that they pass. I don’t know what is the framework that is being chosen for running these tests, but for the trivial cases, we do not need CMake. There are already end-to-end tests running in the fir-dev branch. https://github.com/flang-compiler/f18-llvm-project/blob/fir-dev/flang/test/Lower/end-to-end-character-assignment.f90

–Kiran

I believe, Pat and Damian are talking about execution tests for Fortran intrinsics.

Correct. As someone who thinks more from the perspective of an application developer, I’d like to write execution tests, which is also what led me to the fir-dev branch as I don’t think the ability to produce executable files has been accepted upstream yet.

While these are not testing the intermediate representations, these are unit-tests that only apply to Flang. And when we make changes to the compiler, it will be good to test (with check-flang) that they pass. I don’t know what is the framework that is being chosen for running these tests, but for the trivial cases, we do not need CMake.

For testing, I was originally planning to use the Vegetables unit testing framework, which I like because it supports writing tests in the form of a specification. I would also like to add a capability for building the tests using the Fortran Package Manager (fpm). Doing so is trivial – much simpler than modifying the CMake and with the benefit that it’s trivial to set things up so that fpm automatically downloads any test dependencies such as Vegetables and we don’t have to touch the CMake scripts. Alternatively, if this proves problematic in any way, I’ll commit a simple assertion utility borrowed from the Sourcery library.

Damian

Hi Damian,

Just a quick note to give you a feel for the testing infrastructure here — it is much broader than what it might appear at first glance as you have to look beyond Flang.

The testing mechanisms need to be usable/stable across the community as testing and releases are done LLVM-wide (across all projects: Clang, MLIR, Flang, LLVM, etc.). As a result, any new external package dependencies could have an impact across established mechanisms across all projects (e.g., see https://lab.llvm.org/buildbot for a glance at coverage, platforms, etc.).

For this reason, I would suggest adopting existing capabilities from the current mechanisms used across the LLVM infrastructure as your starting point. A key step will be to make sure any approach taken can be run without additional requirements on the broader community — or we will have to open a discussion with the broader community about expanding the supported infrastructure. Like everything, there are always pros and cons, but keeping Flang’s approach usable and sound as a component of the larger code base is paramount to maintaining LLVM’s established testing and release process.

Thanks,

—Pat

I know that we've already discussed this, but I've just realised that we might have missed one appealing alternative. How about LLVM's cross-project-tests as the landing place for your new tests: https://github.com/llvm/llvm-project/tree/main/cross-project-tests?

-Andrzej