Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port Classic Flang to LLVM 19.1.0 #193

Merged
merged 56 commits into from
Oct 14, 2024

Conversation

bryanpkc
Copy link
Collaborator

@bryanpkc bryanpkc commented Sep 19, 2024

This pull request cherry-picks the Classic Flang patches from the release_18x branch, resolving a number of minor conflicts:

  1. Upstream changes to clang/include/clang/Driver/Options.td that had been backported to release_18x were reverted, or reconciled with the new base branch.
  2. Upstream Clang had dropped the option -fno-fortran-main in llvm/llvm-project@8d53866. To resolve a merge conflict and to keep the code building without error, the option had been restored (but now guarded with the ENABLE_CLASSIC_FLANG macro).
  3. Uses of StringRef::equals have been replaced with StringRef::operator==, StringRef::endswith with StringRef::ends_with, and StringRef::startswith with StringRef::starts_with. See llvm/llvm-project@3fa409f and llvm/llvm-project@4ec9a66.
  4. Uses of Driver::InstalledDir had been replaced with Driver::Dir. See llvm/llvm-project@2b5cd8b.
  5. The #undef directives in llvm/include/llvm/Analysis/VecFuncs.def had been moved into llvm/lib/Analysis/TargetLibraryInfo.cpp in llvm/llvm-project@430729d.
  6. A commit had been added after the port to fix up the FileCheck patterns in two test cases.

Workflow-related commits have already been pushed to the release_19x branch.

@bryanpkc bryanpkc force-pushed the release_19x branch 2 times, most recently from 615c1fb to 4d1a0c9 Compare September 25, 2024 22:44
schweitzpgi and others added 28 commits September 26, 2024 10:38
This commit cherry-picks 427c0e8 from the
release_14x branch, which is a combination of the following legacy patches:

Commit 82b38d2:

    [ClassicFlang] Port release_90 changes from flang-compiler/llvm

    Cherry-picked commit 2085211cfcca70411dc63f0d08763facc8a02090 by Eric Schweitz,
    resolved merge conflicts, fixed build failures (e.g. adapted CGDebugInfo.cpp to
    the new API), and fixed the DIGlobalVariable unit tests, which have been broken
    since commit edfad65eebdf045b050f37380b6b61d673513982.

Commit 885dd87e5fdc:

    [DebugInfo] Removal of DIFortranArrayType and DIFortranSubrange

    These extensions are no more required after merge of below PR.

Commit 5c9b2e0867d5:

    Modification to incorporate comments from @bryanpkc.

Commit 822de2c:

    [AsmPrinter] Fix redundant names and types

    A bug was introduced in 82b38d2 while
    cherry-picking a DIGlobalVariable-related patch.

Commit 45a70a8:

    Port driver changes from release/11x

    Cherry-picked c51f89679135f84675f492d560ec5535c2000cfe by Varun Jayathirtha
    from release_90, and resolved merge conflicts.

    To avoid conflicts with the new Flang, lib/Driver/ToolChains/Flang.{cpp,h}
    have been renamed to ClassicFlang.{cpp,h}, and the ENABLE_CLASSIC_FLANG macro
    is introduced to select which incarnation of Flang to build. The macro is set
    by running CMake with -DLLVM_ENABLE_CLASSIC_FLANG.

    After merge with LLVM 11: Move flang options to the end of the definitions list.

Commit a9a8036:

    Port Classic Flang to LLVM 13

    File changes to TargetLibraryInfo and DebugLocEntry to adapt to the code from
    release/13.x and make it work. Comment out the changes due a segmentation
    fault, code need to be reviewed properly once all commits are in

Commit fe989b7:

    Fix -fveclib=PGMATH

    The use of #ifdef in include/clang/Driver/Options.td was incorrect and
    unsupported. As a result -fveclib=PGMATH was silently ignored, and
    in LLVM 12, it causes the invocation to fail. This patch unguards the
    option so that it is parsed correctly, but lets the FLANG_LLVM_EXTENSIONS
    macro continue to toggle the feature.

Commit 7c224ae:

    Fix use of classic Flang as preprocessor

Commit 8403c83:

    Merge FLANG_LLVM_EXTENSIONS macro into ENABLE_CLASSIC_FLANG

Commit 486741e:

    Fix test failures when in classic Flang mode

    Add a new lit feature tag "classic_flang" to select which tests can or cannot be
    run when the driver is built for classic Flang. Handle LLVM_ENABLE_CLASSIC_FLANG
    in llvm/cmake/modules/HandleLLVMOptions.cmake instead of clang/CMakeLists.txt so
    that macro works in both clang and llvm.

Commit a10f592:

    Port Classic Flang to LLVM 13

    LLVM port from release_12x to release_13x, changes done in order to
    make project able to be build.

Commit d385321 (partial):

    Change to Options.td in order to add the correct invocation for
    ffixed_line_length_VALUE.

This commit also includes the following legacy patch:

Commit 5da2c11:

    Add DebugInfo tests

Commit c379c8d:

    [ClassicFlang][Driver] Correct the LLVM version passed by the Driver
Make TableGen respect the ENABLE_CLASSIC_FLANG macro and better separate the
driver options that are mutually exclusive. When the macro is defined, do not
build LLVM Flang at all.
…lang-compiler#92)

* Support for -gdwarf-5 option in Flang driver.

Summary:
  FLANG driver doesnt pass -gdwarf-4/5 to flang1 in form of xbits,
while it passes for -gdwarf-2/3
   -gdwarf-2 => -x 120 0x200
   -gdwarf-3 => -x 120 0x4000

Due to this -gdwarf-5 is never honored and default option -gdwarf-4
is taken.
    # flang -gdwarf-5 test.f90
    # llvm-dwarfdump a.out | grep version
    0x00000000: Compile Unit: length = 0x0000008e version = 0x0004

  Now 0x1000000/0x2000000 will be passed for -gdwarf-4/5
   -gdwarf-4 => -x 120 0x1000000
   -gdwarf-5 => -x 120 0x2000000

Testing:
  - GNU gdb fortran testsuite
  - check-llvm
  - check-debuginfo

* Flang doenst choose correct dwarf version when multiple -g/-gdwarfN mentioned

 Summary:
When multiple -g/-gdwarfN options are passed together at compile time,
flang chooses the least one. Clang/gfortran etc choose the last one.

-gdwarf-5 -gdwarf-3 => flang chooses 5 while clang/gfortran choose 3
-gdwarf-5 -g => flang choses the default while clang/gfortran choose 5

  Testing:
- check-llvm
- check-debuginfo

* Default dwarf version should be 4 for -g with flang

Currently flang dumps dwarf version 2 for -g and 4 for absence of -g

-------------------------
$ flang my.f90
$ llvm-dwarfdump a.out | grep version
0x00000000: Compile Unit: length = 0x0000003d version = 0x0004 abbr_offset = 0x0000 addr_size = 0x08 (next unit at 0x00000041)

$ flang -g my.f90
$ llvm-dwarfdump a.out | grep version
0x00000000: Compile Unit: length = 0x00000047 version = 0x0002 abbr_offset = 0x0000 addr_size = 0x08 (next unit at 0x0000004b)
-------------------------

It should be 4 for -g as it is the case with clang.
This commit is motivated by reducing the merge burden by shrinking the
diff between llvm upstream and classic-flang-llvm-project.

Outside of Flang, Fortran code is fed through the Compile phase, and
the appropriate tooling is picked up through ToolChain::SelectTool.

Classic Flang introduced a FortranFrontend, but these days this seems
unnecessary. Fortran can go through the same machinery as everything else.

* Use the Preprocess phase to preprocess Fortran code. This phase is
  always combined with the Compile phase.

* Use the Compile phase to lower Fortran code to LLVM IR, and use the
  Backend phase to compile and optimize the IR. These phases are never
  combined.

* Remove FortranFrontendJobClass.

* Remove FortranFrontend tool (instead it's just the Flang tool, which
  in Classic Flang mode is Classic Flang).

* Update tests which inspect the output of the Classic Flang tooling,
  and ensures that the driver does the right thing for various types of
  inputs.

Based on a patch from Peter Waller <peter.waller@arm.com>.
Signed-off-by: Itay Bookstein <itay.bookstein@nextsilicon.com>
* -Wsuggest-override for ClassicFlangMacroBuilder::defineMacro

Signed-off-by: Itay Bookstein <itay.bookstein@nextsilicon.com>
-emit-flang-llvm instructs flang to stop after flang2 and dump the LLVM IR.

Can be useful for debugging and also would be a useful option for testing flang
output more accurately.

Signed-off-by: Richard Barton <richard.barton@arm.com>
Add %flang as a tool substitution available in lit tests. This apes the way
%clang is defined and adds a $FLANG override in a similar vein.

To avoid this being dead code, add a single test to check the flang driver is
reporting the correct phases when running under various phase control options.

Signed-off-by: Richard Barton <richard.barton@arm.com>
This patch partially reverts the following commit from an early version of
Classic Flang LLVM:

flang-compiler/llvm@6866909

When the LoopVectorize.cpp change in that commit is ported to LLVM 14, it causes
a failure in the following test:

llvm/test/Transforms/LoopVectorize/AArch64/smallest-and-widest-types.ll

Reverting the change allows all tests to run cleanly.
Port commit 64bf2a6 to LLVM 16 and resolve merge conflicts. The original
commit message follows:

Pass LLVM target features (-mattr strings) to flang to embed in generated
.ll files. For normal compilation this won't make much difference as the
attributes are passed to clang after flang2 and can be applied there but this
is crucial to enable LTO with flang as clang will not apply the attributes
when building the flang2 output. libLTO will need to read these out of the
object files to apply them.

Signed-off-by: Richard Barton <richard.barton@arm.com>
Use existing clang support for uniquifying target feature lists to pass
a unique list through to flang.
Signed-off-by: Paul Osmialowski <pawel.osmialowski@arm.com>
Version screen has been erroneously reporting as flang-new since LLVM 12
These CMake variables are used to set python variables in the lit
config. As such, they need to bet set with a valid CMake boolean value
that is also a valid python boolean value (1, True, etc.) and not one
that is not (ON, NO, etc.) to work. This is fragile.

Call the LLVM cmake helper function on these two downstream CMake
Signed-off-by: Richard Barton <richard.barton@arm.com>
Fix a bug where flang -E -save-temps input.F90 would preprocess
the input file twice. Add a new test for preprocessor behaviour that
would expose the bug and updated fortran-phases test for new behaviour.

Also added -emit-flang-llvm to that test for completeness.

Signed-off-by: Richard Barton <richard.barton@arm.com>
…tency out)

Signed-off-by: Paul Osmialowski <pawel.osmialowski@arm.com>
1) All fma and contraction behvaviour follows from the -ffp-contract= flag
setting.
2) At optimization level 0, -ffp-contract=fast flag will not be honoured.
3) At all other levels, -ffp-contract=fast will be the default behaviour and
the flag is honoured.
In some workloads, users get "-lompstub" by running "flang -v" and link
it explicitly, which may cause failure when compiling programs with
OpenMP (flang -fopenmp test.f90 -lompstub).

There is no runtime call in ompstub.c generated when "-fopenmp" is not
added to compile the program. When "-fopenmp" is added to compile the
program, the runtime in llvm/openmp(libomp.so) will be used. If users
use some runtime library routines such as "omp_get_thread_num" and
compiles it without the option "-fopenmp", both gfortran and ifort
report errors of "undefined reference to ...". After remove "-lompstub"
in the driver, classic-flang reports the error, too. So, it's safe to
remove the "-lompstub" in the driver.
In LLVM 15, the Fortran and OpenMP runtime libraries are added to the linker
command line using common methods (addFortranRuntime* and addOpenMPRuntime*).
This commit adds Classic Flang awareness to addFortranRuntime*, so that
Classic Flang doesn't attempt to link with LLVM Flang libraries. Re-using
the same methods as Clang and LLVM Flang also helps reduce downstream delta.
A Classic Flang test is added to ensure that the linker command is constructed
correctly.

LLVM 19 porting note: This commit has been amended to add back the
-fno-fortran-main option that was deleted in upstream commit 8d53866.
Delete some snippets that had been commented out since commit a10f592
in release_13x.
bryanpkc and others added 4 commits September 26, 2024 10:39
Otherwise Classic Flang's CMakeLists.txt will be unable to find llvm-lit and
cannot generate test-related goals, e.g. `check-all`.
The -save-temps option has been causing the driver to run flang1/flang2
on the input file twice, and the fortran-preprocessor.f90 test was unable
to detect the error, because it had used -c instead of -x in a couple of
RUN commands.  This patch fixes the test and the driver to make sure that
the frontend is not invoked multiple times with -save-temps.
@bryanpkc bryanpkc force-pushed the release_19x branch 2 times, most recently from c7e51fe to 31c269c Compare October 2, 2024 04:23
bryanpkc and others added 14 commits October 2, 2024 00:33
Some header files have been moved to new directories in LLVM 17.
StringExtras.h is no longer included transitively since D155178,
and now needs to be included explicitly by clang/Frontend/Utils.h.
Upstream commit 555b572 had reverted D118493, which let Clang add a
toolchain-specific -rpath option to the linker command for the OpenMP
library. This patch fixes up a Classic Flang driver test which is impacted
by the revert.
The IR in clang/test/CodeGen/libpgmath-logfun-aarch64.ll can now be
vectorized by a factor of two. The IR still does not call the unsupported
libpgmath functions, as per the intention of the test.
-Mnomain and -fno-fortran-main were ineffective unless they were both used
at the same time. This patch fixes the bug, formats needFortranMain and
needFortranLibs, and adds some test cases for the logic.
Upstream commit 01797da had added a new VABIPrefix field to the VecDesc
structure; modify the downstream libpgmath mappings accordingly.
...because the runners do not have enough threads.
Classic Flang accepts some Clang options that LLVM Flang (F18) does not.
The Visibility of such options are modified to include "FlangOption".
...after upstream commit 13e977d changed
how target feature dependencies are handled.
This patch cherry-picks a number of Visibility changes from the tip of trunk
upstream, to make selected Clang options visible to Classic Flang. It also
makes target-specific codegen options (-mxxxx) visible to Flang by default.
Note that accepting the option does not imply support; Classic Flang may
simply ignore certain options for backward compatibility.
Through this commit Mbackslash will be the default option.
By default, escape characters are treated as regular characters unless Mnobackslash
is specified.

There is also the issue of treating both Mbackslash and Mnobackslash options independently.
This is addressed in this patch and the testcases in flang repository will test this feature.
…lang-compiler#185)

PGMATH has AVX512 runtime functions and can be executed only when the application is compiled
in avx512 mode. The VecFuncs.def has no information about the TargetOptions and avx512 functions
are selected even in avx2 mode. This issue is fixed by creating separate table for AVX512 functions
and using them only when avx512 mode is specified.
LLVM 19 had changed how target feature flags are generated. In particular,
the prerequisite flags "+sha2" and "+sha3" to "+sve2-sha3" are now explicit.
@bryanpkc
Copy link
Collaborator Author

bryanpkc commented Oct 7, 2024

The AArch64 build is failing due to CMake not finding ZLIB, even though the container already has the zlib1g-dev package installed.

@pawosm-arm
Copy link
Collaborator

The AArch64 build is failing due to CMake not finding ZLIB, even though the container already has the zlib1g-dev package installed.

What is the use of Zlib in LLVM? There's CMake option for not using it (and a separate option for not using ZSTD too)

@bryanpkc
Copy link
Collaborator Author

bryanpkc commented Oct 8, 2024

What is the use of Zlib in LLVM? There's CMake option for not using it (and a separate option for not using ZSTD too)

Yes, I know. The problem here is that it isn't the LLVM build that breaks (because the zlib libraries and headers do exist), but the Flang build. We have never had to specify LLVM_ENABLE_ZLIB for CMake in the Flang build, so I suspect that something has changed with the CMake modules under llvm/cmake/ in LLVM 19. I'm trying to understand why the behaviour has changed.

@bryanpkc
Copy link
Collaborator Author

I have fixed the AArch64 Docker container which was using an old version of CMake, causing the problem. See fa9105e.

@bryanpkc
Copy link
Collaborator Author

@shivaramaarao Could you review this PR?

Copy link
Collaborator

@shivaramaarao shivaramaarao left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. thanks for working on this.

@bryanpkc bryanpkc merged commit a760735 into flang-compiler:release_19x Oct 14, 2024
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.