Skip to content

Conversation

@dpaoliello
Copy link
Contributor

Fixes #146802

#146582 started using the Reserved Frame Pointer kind for Arm64 Windows, but this revealed a bug in Flang where it copied the -mframe-pointer=reserved flag from Clang, but didn't correctly handle it in its own command line parser and subsequent compilation pipeline.

This change adds support for -mframe-pointer=reserved and adds a test to make sure that functions are correctly marked when the flag is set.

@llvmbot llvmbot added flang:driver flang Flang issues not falling into any other category flang:fir-hlfir labels Jul 3, 2025
@llvmbot
Copy link
Member

llvmbot commented Jul 3, 2025

@llvm/pr-subscribers-flang-driver

@llvm/pr-subscribers-flang-fir-hlfir

Author: Daniel Paoliello (dpaoliello)

Changes

Fixes #<!-- -->146802

#146582 started using the Reserved Frame Pointer kind for Arm64 Windows, but this revealed a bug in Flang where it copied the -mframe-pointer=reserved flag from Clang, but didn't correctly handle it in its own command line parser and subsequent compilation pipeline.

This change adds support for -mframe-pointer=reserved and adds a test to make sure that functions are correctly marked when the flag is set.


Full diff: https://github.com/llvm/llvm-project/pull/146937.diff

3 Files Affected:

  • (modified) flang/lib/Frontend/CompilerInvocation.cpp (+2-1)
  • (modified) flang/lib/Optimizer/Passes/Pipelines.cpp (+2)
  • (modified) flang/test/Driver/func-attr.f90 (+2)
diff --git a/flang/lib/Frontend/CompilerInvocation.cpp b/flang/lib/Frontend/CompilerInvocation.cpp
index 30d81f3daa969..c4d334c4b6de9 100644
--- a/flang/lib/Frontend/CompilerInvocation.cpp
+++ b/flang/lib/Frontend/CompilerInvocation.cpp
@@ -291,13 +291,14 @@ static void parseCodeGenArgs(Fortran::frontend::CodeGenOptions &opts,
 
   opts.AliasAnalysis = opts.OptimizationLevel > 0;
 
-  // -mframe-pointer=none/non-leaf/all option.
+  // -mframe-pointer=none/non-leaf/reserved/all option.
   if (const llvm::opt::Arg *a =
           args.getLastArg(clang::driver::options::OPT_mframe_pointer_EQ)) {
     std::optional<llvm::FramePointerKind> val =
         llvm::StringSwitch<std::optional<llvm::FramePointerKind>>(a->getValue())
             .Case("none", llvm::FramePointerKind::None)
             .Case("non-leaf", llvm::FramePointerKind::NonLeaf)
+            .Case("reserved", llvm::FramePointerKind::Reserved)
             .Case("all", llvm::FramePointerKind::All)
             .Default(std::nullopt);
 
diff --git a/flang/lib/Optimizer/Passes/Pipelines.cpp b/flang/lib/Optimizer/Passes/Pipelines.cpp
index 42d9e7ba2418f..dca176abe5ec5 100644
--- a/flang/lib/Optimizer/Passes/Pipelines.cpp
+++ b/flang/lib/Optimizer/Passes/Pipelines.cpp
@@ -358,6 +358,8 @@ void createDefaultFIRCodeGenPassPipeline(mlir::PassManager &pm,
     framePointerKind = mlir::LLVM::framePointerKind::FramePointerKind::NonLeaf;
   else if (config.FramePointerKind == llvm::FramePointerKind::All)
     framePointerKind = mlir::LLVM::framePointerKind::FramePointerKind::All;
+  else if (config.FramePointerKind == llvm::FramePointerKind::Reserved)
+    framePointerKind = mlir::LLVM::framePointerKind::FramePointerKind::Reserved;
   else
     framePointerKind = mlir::LLVM::framePointerKind::FramePointerKind::None;
 
diff --git a/flang/test/Driver/func-attr.f90 b/flang/test/Driver/func-attr.f90
index 7442ac54dcaa5..e8cab4fca2226 100644
--- a/flang/test/Driver/func-attr.f90
+++ b/flang/test/Driver/func-attr.f90
@@ -4,6 +4,7 @@
 
 ! RUN: %flang_fc1 -triple aarch64-none-none -mframe-pointer=none -emit-llvm -o - %s 2>&1| FileCheck %s --check-prefix=CHECK-NONEFP
 ! RUN: %flang_fc1 -triple aarch64-none-none -mframe-pointer=non-leaf -emit-llvm -o - %s 2>&1| FileCheck %s --check-prefix=CHECK-NONLEAFFP
+! RUN: %flang_fc1 -triple aarch64-none-none -mframe-pointer=reserved -emit-llvm -o - %s 2>&1| FileCheck %s --check-prefix=CHECK-RESERVEDFP
 ! RUN: %flang_fc1 -triple aarch64-none-none -mframe-pointer=all -emit-llvm -o - %s 2>&1| FileCheck %s --check-prefix=CHECK-ALLFP
 ! RUN: not %flang_fc1 -triple aarch64-none-none -mframe-pointer=wrongval -emit-llvm -o - %s 2>&1| FileCheck %s --check-prefix=CHECK-WRONGVALUEFP
 
@@ -20,6 +21,7 @@ end subroutine func
 
 ! CHECK-NONEFP-NOT: attributes #0 = { "frame-pointer"="{{.*}}" }
 ! CHECK-NONLEAFFP: attributes #0 = { "frame-pointer"="non-leaf" }
+! CHECK-RESERVEDFP: attributes #0 = { "frame-pointer"="reserved" }
 ! CHECK-ALLFP: attributes #0 = { "frame-pointer"="all" }
 
 ! CHECK-WRONGVALUEFP:error: invalid value 'wrongval' in '-mframe-pointer=wrongval'

@dpaoliello dpaoliello requested a review from DavidTruby July 3, 2025 18:12
Copy link
Contributor

@tarunprabhu tarunprabhu left a comment

Choose a reason for hiding this comment

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

Thanks for this. This LGTM, but please wait for @DavidTruby

Copy link
Member

@DavidTruby DavidTruby left a comment

Choose a reason for hiding this comment

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

LGTM too!

@dpaoliello dpaoliello merged commit 71ffa2a into llvm:main Jul 7, 2025
13 checks passed
@dpaoliello dpaoliello deleted the flangfpreserved branch July 7, 2025 16:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

flang:driver flang:fir-hlfir flang Flang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants