[flang][driver] Use `-O{0|1|2|3}` to define LLVM backend pass pipeline

Support for optimisation flags in LLVM Flang was originally added in
https://reviews.llvm.org/D128043. That patch focused on LLVM
middle-end/optimisation pipelines. With this patch, Flang will
additionally configure LLVM backend pass pipelines accordingly. This
behavior is consistent with Clang.

New hook is added to translate compiler optimisation flags (e.g. `-O3`)
into backend optimisation level: `getCGOptLevel`. Identical hooks are
available in Clang and LLVM. In other words, the meaning of these
optimisation flags remains consistent with other sub-projects that use
LLVM backends.

Differential Revision: https://reviews.llvm.org/D128050
This commit is contained in:
Andrzej Warzynski 2022-06-17 13:08:42 +00:00
parent f164814f2f
commit 48eb2bc608
2 changed files with 47 additions and 3 deletions

View File

@ -517,6 +517,22 @@ void CodeGenAction::generateLLVMIR() {
}
}
static llvm::CodeGenOpt::Level
getCGOptLevel(const Fortran::frontend::CodeGenOptions &opts) {
switch (opts.OptimizationLevel) {
default:
llvm_unreachable("Invalid optimization level!");
case 0:
return llvm::CodeGenOpt::None;
case 1:
return llvm::CodeGenOpt::Less;
case 2:
return llvm::CodeGenOpt::Default;
case 3:
return llvm::CodeGenOpt::Aggressive;
}
}
void CodeGenAction::setUpTargetMachine() {
CompilerInstance &ci = this->getInstance();
@ -535,9 +551,12 @@ void CodeGenAction::setUpTargetMachine() {
assert(theTarget && "Failed to create Target");
// Create `TargetMachine`
tm.reset(theTarget->createTargetMachine(theTriple, /*CPU=*/"",
/*Features=*/"",
llvm::TargetOptions(), llvm::None));
llvm::CodeGenOpt::Level OptLevel =
getCGOptLevel(ci.getInvocation().getCodeGenOpts());
tm.reset(theTarget->createTargetMachine(
theTriple, /*CPU=*/"",
/*Features=*/"", llvm::TargetOptions(), /*Reloc::Model=*/llvm::None,
/*CodeModel::Model=*/llvm::None, OptLevel));
assert(tm && "Failed to create TargetMachine");
}

View File

@ -0,0 +1,25 @@
! Verify that`-O{n}` is indeed taken into account when definining the LLVM backend pass pipeline.
! REQUIRES: aarch64-registered-target
!-----------
! RUN LINES
!-----------
! RUN: %flang_fc1 -S -O2 %s -triple aarch64-unknown-linux-gnu -mllvm -debug-pass=Structure -o /dev/null 2>&1 | FileCheck %s --check-prefix=CHECK-O2
! RUN: %flang_fc1 -S -O3 %s -triple aarch64-unknown-linux-gnu -mllvm -debug-pass=Structure -o /dev/null 2>&1 | FileCheck %s --check-prefix=CHECK-O3
!-----------------------
! EXPECTED OUTPUT
!-----------------------
! CHECK-O2-NOT: SVE intrinsics optimizations
! CHECK-O3: SVE intrinsics optimizations
!-------
! INPUT
!-------
subroutine simple_loop
integer :: i
do i=1,5
end do
end subroutine