Skip to content

Commit

Permalink
[flang][driver] Use -O{0|1|2|3} to define LLVM backend pass pipeline
Browse files Browse the repository at this point in the history
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
  • Loading branch information
banach-space committed Jun 27, 2022
1 parent f164814 commit 48eb2bc
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
25 changes: 22 additions & 3 deletions flang/lib/Frontend/FrontendActions.cpp
Expand Up @@ -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();

Expand All @@ -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");
}

Expand Down
25 changes: 25 additions & 0 deletions flang/test/Driver/default-backend-pipelines.f90
@@ -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

0 comments on commit 48eb2bc

Please sign in to comment.