[clang] [Clang] Enable -fextend-lifetimes at -Og (PR #118026)

2025-05-10 Thread Fangrui Song via cfe-commits

MaskRay wrote:

Since the option has been renamed to `-fextend-variable-liveness`, I think you 
want to edit this title and the description to reflect the naming change as 
well. I wanted to check the status the effort and I tried `git log --grep 
fextend-lifetimes` but found nothing, then I realized that the option has been 
renamed :)

https://github.com/llvm/llvm-project/pull/118026
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Enable -fextend-lifetimes at -Og (PR #118026)

2025-04-25 Thread Mikael Holmén via cfe-commits

mikaelholmen wrote:

> > I did some downstream testing with "-Og" since this patch and noticed that 
> > e.g.
> 
> Thanks for catching these - in terms of legalizer support it looks like 
> everything relevant is implemented except for `SoftenFloatOperand`, so that 
> should be a straightforward fix. The target-specific error is also trivial 
> (`FAKE_USE` has a size of 0), but it would be preferable if every target 
> didn't need to account for it - previously there was a discussion of whether 
> `FAKE_USE` should have `isPosition`, and this probably settles that since it 
> would allow them to be treated correctly by-default.
> 
> I'll revert this for now (after verifying), since it may take a little time 
> to confirm that changing `isPosition()` doesn't introduce any new bugs; I'll 
> try to expand the fake use tests to cover more targets at the same time.

Thanks!

https://github.com/llvm/llvm-project/pull/118026
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Enable -fextend-lifetimes at -Og (PR #118026)

2025-04-25 Thread Stephen Tozer via cfe-commits

SLTozer wrote:

> I did some downstream testing with "-Og" since this patch and noticed that 
> e.g.

Thanks for catching these - in terms of legalizer support it looks like 
everything relevant is implemented except for `SoftenFloatOperand`, so that 
should be a straightforward fix. The target-specific error is also trivial 
(`FAKE_USE` has a size of 0), but it would be preferable if every target didn't 
need to account for it - previously there was a discussion of whether 
`FAKE_USE` should have `isPosition`, and this probably settles that since it 
would allow them to be treated correctly by-default.

I'll revert this for now (after verifying), since it may take a little time to 
confirm that changing `isPosition()` doesn't introduce any new bugs; I'll try 
to expand the fake use tests to cover more targets at the same time.

https://github.com/llvm/llvm-project/pull/118026
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Enable -fextend-lifetimes at -Og (PR #118026)

2025-04-25 Thread Mikael Holmén via cfe-commits

mikaelholmen wrote:

I did some downstream testing with "-Og" since this patch and noticed that e.g.
```
llc -mtriple=avr bbi-106478.ll
```
crashes with
```
SoftenFloatOperand Op #1: t2: ch = fake_use t0, ConstantFP:f64<0.00e+00>

LLVM ERROR: Do not know how to soften this operator's operand!
```
and
```
llc -mtriple=systemz bbi-106478.ll
```
fails with
```
llc: ../lib/Target/SystemZ/SystemZLongBranch.cpp:226: unsigned int 
getInstSizeInBytes(const MachineInstr &, const SystemZInstrInfo *): Assertion 
`(Size || MI.isDebugOrPseudoInstr() || MI.isPosition() || MI.isKill() || 
MI.isImplicitDef() || MI.getOpcode() == TargetOpcode::MEMBARRIER || 
MI.getOpcode() == TargetOpcode::INIT_UNDEF || MI.isInlineAsm() || 
MI.getOpcode() == SystemZ::STACKMAP || MI.getOpcode() == SystemZ::PATCHPOINT || 
MI.getOpcode() == SystemZ::EH_SjLj_Setup) && "Missing size value for 
instruction."' failed.
```
for bbi-106478.ll just being
```
define double @idd() {
entry:
  notail call void (...) @llvm.fake.use(double 0.00e+00)
  ret double 0.00e+00
}
```
(and if removing the llvm.fake.use call they don't crash)
so I guess there is some testing and fixes around llvm.fake.use left to do.


https://github.com/llvm/llvm-project/pull/118026
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Enable -fextend-lifetimes at -Og (PR #118026)

2025-04-23 Thread Stephen Tozer via cfe-commits

SLTozer wrote:

> Any ideas why we might be hitting this issue? This happens during a built 
> with `-Og`, and issue does not remove if I remove `-Og` flag.

At a glance that looks like it could be related, we generate (no-op) cleanup as 
part of -fextend-variable-liveness. I'll look into this, but could you clarify 
what you mean - did you mean that the error occurs with `-Og` and does _not_ 
occur when you remove `-Og`?

https://github.com/llvm/llvm-project/pull/118026
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Enable -fextend-lifetimes at -Og (PR #118026)

2025-04-22 Thread via cfe-commits

gulfemsavrun wrote:

We started seeing the following failure, and I bisected it to this commit.
```
[1343/43547](319) CXX 
host_x64/obj/third_party/protobuf/src/google/protobuf/libprotobuf_lite.generated_message_tctable_lite.cc.o
FAILED: [code=1] 
host_x64/obj/third_party/protobuf/src/google/protobuf/libprotobuf_lite.generated_message_tctable_lite.cc.o
 
../../build/rbe/reclient_cxx.sh --working-subdir=out/not-default 
--exec_strategy=remote_local_fallback --preserve_unchanged_output_mtime --  
../../prebuilt/third_party/clang/linux-x64/bin/clang++ -MD -MF 
host_x64/obj/third_party/protobuf/src/google/protobuf/libprotobuf_lite.generated_message_tctable_lite.cc.o.d
 -DTOOLCHAIN_VERSION=xBAWf3R2sN1ViZaJjFVJTCaaAZEuAtpSDTaGs7iC35IC 
-D_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS -D_LIBCPP_REMOVE_TRANSITIVE_INCLUDES 
-DGOOGLE_PROTOBUF_NO_RTTI -DHAVE_PTHREAD -I../.. -Ihost_x64/gen 
-I../../third_party/protobuf/src 
-I../../third_party/protobuf/third_party/utf8_range 
-I../../third_party/abseil-cpp -fcolor-diagnostics 
-fcrash-diagnostics-dir=clang-crashreports -fcrash-diagnostics=all 
-ffp-contract=off --sysroot=../../prebuilt/third_party/sysroot/linux 
--target=x86_64-unknown-linux-gnu -ffile-compilation-dir=. 
-no-canonical-prefixes -fomit-frame-pointer -fdata-sections -ffunction-sections 
-Og -Xclang -debug-info-kind=constructor -g3 -grecord-gcc-switches -gdwarf-5 
-gz=zstd -Wall -Wextra -Wconversion -Wextra-semi -Wimplicit-fallthrough 
-Wnewline-eof -Wstrict-prototypes -Wwrite-strings -Wno-sign-conversion 
-Wno-unused-parameter -Wnonportable-system-include-path 
-Wno-missing-field-initializers -Wno-extra-qualification 
-Wno-cast-function-type-strict -Wno-cast-function-type-mismatch 
-Wno-unknown-warning-option -Wno-missing-template-arg-list-after-template-kw 
-Wno-deprecated-pragma -Wno-nontrivial-memaccess -fvisibility=hidden -Werror 
-Wa,--fatal-warnings --sysroot=../../prebuilt/third_party/sysroot/linux 
--target=x86_64-unknown-linux-gnu -Wno-deprecated-pragma 
-Wno-enum-enum-conversion -Wno-extra-semi -Wno-float-conversion 
-Wno-implicit-float-conversion -Wno-implicit-int-conversion 
-Wno-implicit-int-float-conversion -Wno-invalid-noreturn 
-Wno-missing-field-initializers -Wno-sign-compare -Wno-unused-function 
-Wno-unused-private-field -Wno-deprecated-declarations 
-Wno-deprecated-this-capture -Wno-unnecessary-virtual-specifier 
-Wno-c++98-compat-extra-semi -Wno-shorten-64-to-32 -Wno-sign-compare 
-Wno-missing-field-initializers -Wno-conversion -Wno-unknown-warning-option 
-Wno-implicit-int-float-conversion -Wno-array-parameter 
-Wno-deprecated-builtins -fvisibility-inlines-hidden -stdlib=libc++ 
-stdlib=libc++ -std=c++20 -Wno-deprecated-this-capture -fno-exceptions 
-fno-rtti -ftemplate-backtrace-limit=0 -stdlib=libc++ -c 
../../third_party/protobuf/src/google/protobuf/generated_message_tctable_lite.cc
 -o 
host_x64/obj/third_party/protobuf/src/google/protobuf/libprotobuf_lite.generated_message_tctable_lite.cc.o
../../third_party/protobuf/src/google/protobuf/generated_message_tctable_lite.cc:68:28:
 error: cannot compile this tail call skipping over cleanups yet
   68 |   PROTOBUF_MUSTTAIL return GenericFallbackImpl(
  |
^~
   69 |   PROTOBUF_TC_PARAM_PASS);
  |   ~~~
```

I'm uploading a reproducer.
[clang-crashreports.zip](https://github.com/user-attachments/files/19859700/clang-crashreports.zip)

Any ideas why we might be hitting this issue? This happens during a built with 
`-Og`, and issue does not remove if I remove `-Og` flag. 


https://github.com/llvm/llvm-project/pull/118026
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Enable -fextend-lifetimes at -Og (PR #118026)

2025-04-17 Thread Stephen Tozer via cfe-commits

https://github.com/SLTozer closed 
https://github.com/llvm/llvm-project/pull/118026
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Enable -fextend-lifetimes at -Og (PR #118026)

2025-04-17 Thread Stephen Tozer via cfe-commits

https://github.com/SLTozer updated 
https://github.com/llvm/llvm-project/pull/118026

>From c0369e1b36594d5d6f6c29d7ebb7319d79edf55b Mon Sep 17 00:00:00 2001
From: Stephen Tozer 
Date: Thu, 28 Nov 2024 13:53:20 +
Subject: [PATCH 1/2] Enable -fextend-lifetimes at -Og

---
 clang/docs/CommandGuide/clang.rst| 7 +--
 clang/docs/ReleaseNotes.rst  | 4 
 clang/lib/Driver/ToolChains/Clang.cpp| 8 +++-
 clang/test/Driver/extend-variable-liveness.c | 3 ++-
 4 files changed, 18 insertions(+), 4 deletions(-)

diff --git a/clang/docs/CommandGuide/clang.rst 
b/clang/docs/CommandGuide/clang.rst
index 42aac7b25d93c..68e9e07ed0005 100644
--- a/clang/docs/CommandGuide/clang.rst
+++ b/clang/docs/CommandGuide/clang.rst
@@ -443,8 +443,11 @@ Code Generation Options
 :option:`-Oz` Like :option:`-Os` (and thus :option:`-O2`), but reduces code
 size further.
 
-:option:`-Og` Like :option:`-O1`. In future versions, this option might
-disable different optimizations in order to improve debuggability.
+:option:`-Og` Similar to :option:`-O1`, but with slightly reduced
+optimization and better variable visibility. The same optimizations are run
+as at :option:`-O1`, but the :option:`-fextend-variable-liveness` flag is
+also set, which tries to prevent optimizations from reducing the liveness 
of
+user variables, improving their availability when debugging.
 
 :option:`-O` Equivalent to :option:`-O1`.
 
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 0a46bbea96f6c..4b7ae1984f85c 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -221,6 +221,10 @@ Modified Compiler Flags
 
 - The ``-mexecute-only`` and ``-mpure-code`` flags are now accepted for 
AArch64 targets. (#GH125688)
 
+- The ``-Og`` optimization flag now sets ``-fextend-variable-liveness``, a new
+  compiler flag which trades a small amount of optimization in exchange for
+  improved variable visibility.
+
 Removed Compiler Flags
 -
 
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp 
b/clang/lib/Driver/ToolChains/Clang.cpp
index 8506a5c00e7bc..b2dd4b3b54869 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -7681,7 +7681,13 @@ void Clang::ConstructJob(Compilation &C, const JobAction 
&JA,
   if (Args.hasArg(options::OPT_fretain_comments_from_system_headers))
 CmdArgs.push_back("-fretain-comments-from-system-headers");
 
-  Args.AddLastArg(CmdArgs, options::OPT_fextend_variable_liveness_EQ);
+  if (Arg *A = Args.getLastArg(options::OPT_fextend_variable_liveness_EQ)) {
+A->render(Args, CmdArgs);
+  } else if (Arg *A = Args.getLastArg(options::OPT_O_Group);
+ A && A->containsValue("g")) {
+// Set -fextend-variable-liveness=all by default at -Og.
+CmdArgs.push_back("-fextend-variable-liveness=all");
+  }
 
   // Forward -fcomment-block-commands to -cc1.
   Args.AddAllArgs(CmdArgs, options::OPT_fcomment_block_commands);
diff --git a/clang/test/Driver/extend-variable-liveness.c 
b/clang/test/Driver/extend-variable-liveness.c
index bbfb2ece6f297..99a5409ceccea 100644
--- a/clang/test/Driver/extend-variable-liveness.c
+++ b/clang/test/Driver/extend-variable-liveness.c
@@ -1,7 +1,8 @@
 // Tests that -fextend-variable-liveness and its aliases are correctly passed
-// by the driver.
+// by the driver, and are set by default at -Og.
 
 // RUN: %clang -### -c %s 2>&1 | FileCheck %s --check-prefixes=CHECK,DEFAULT
+// RUN: %clang -### -Og -c %s 2>&1 | FileCheck %s --check-prefixes=CHECK,ALL
 // RUN: %clang -fextend-variable-liveness=none -### -c %s 2>&1 | FileCheck %s 
--check-prefixes=CHECK,NONE
 // RUN: %clang -fextend-variable-liveness=this -### -c %s 2>&1 | FileCheck %s 
--check-prefixes=CHECK,THIS
 // RUN: %clang -fextend-variable-liveness=all -### -c %s 2>&1 | FileCheck %s 
--check-prefixes=CHECK,ALL

>From 7126cb526d88ae4c53b8ac9d638fa74e2f7defa9 Mon Sep 17 00:00:00 2001
From: Stephen Tozer 
Date: Thu, 17 Apr 2025 15:14:55 +0100
Subject: [PATCH 2/2] Update docs to fit current compiler/doc state

---
 clang/docs/CommandGuide/clang.rst | 2 +-
 clang/docs/ReleaseNotes.rst   | 6 +++---
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/clang/docs/CommandGuide/clang.rst 
b/clang/docs/CommandGuide/clang.rst
index 68e9e07ed0005..a1e738f1afec8 100644
--- a/clang/docs/CommandGuide/clang.rst
+++ b/clang/docs/CommandGuide/clang.rst
@@ -445,7 +445,7 @@ Code Generation Options
 
 :option:`-Og` Similar to :option:`-O1`, but with slightly reduced
 optimization and better variable visibility. The same optimizations are run
-as at :option:`-O1`, but the :option:`-fextend-variable-liveness` flag is
+as at :option:`-O1`, but the ``-fextend-variable-liveness`` flag is
 also set, which tries to prevent optimizations from reducing the liveness 
of
 user variables, improving their availability when debugging.
 
diff --git a/c

[clang] [Clang] Enable -fextend-lifetimes at -Og (PR #118026)

2025-04-17 Thread Stephen Tozer via cfe-commits

https://github.com/SLTozer updated 
https://github.com/llvm/llvm-project/pull/118026

>From 351971bbff4e77b0f36cd92cd1a881584d17a9e7 Mon Sep 17 00:00:00 2001
From: Stephen Tozer 
Date: Thu, 28 Nov 2024 13:53:20 +
Subject: [PATCH 1/2] Enable -fextend-lifetimes at -Og

---
 clang/docs/CommandGuide/clang.rst| 7 +--
 clang/docs/ReleaseNotes.rst  | 4 
 clang/lib/Driver/ToolChains/Clang.cpp| 8 +++-
 clang/test/Driver/extend-variable-liveness.c | 3 ++-
 4 files changed, 18 insertions(+), 4 deletions(-)

diff --git a/clang/docs/CommandGuide/clang.rst 
b/clang/docs/CommandGuide/clang.rst
index 42aac7b25d93c..68e9e07ed0005 100644
--- a/clang/docs/CommandGuide/clang.rst
+++ b/clang/docs/CommandGuide/clang.rst
@@ -443,8 +443,11 @@ Code Generation Options
 :option:`-Oz` Like :option:`-Os` (and thus :option:`-O2`), but reduces code
 size further.
 
-:option:`-Og` Like :option:`-O1`. In future versions, this option might
-disable different optimizations in order to improve debuggability.
+:option:`-Og` Similar to :option:`-O1`, but with slightly reduced
+optimization and better variable visibility. The same optimizations are run
+as at :option:`-O1`, but the :option:`-fextend-variable-liveness` flag is
+also set, which tries to prevent optimizations from reducing the liveness 
of
+user variables, improving their availability when debugging.
 
 :option:`-O` Equivalent to :option:`-O1`.
 
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 4f640697e1817..23a3d481e66c2 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -219,6 +219,10 @@ Modified Compiler Flags
 
 - `-Wpadded` option implemented for the `x86_64-windows-msvc` target. Fixes 
#61702
 
+- The ``-Og`` optimization flag now sets ``-fextend-variable-liveness``, a new
+  compiler flag which trades a small amount of optimization in exchange for
+  improved variable visibility.
+
 Removed Compiler Flags
 -
 
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp 
b/clang/lib/Driver/ToolChains/Clang.cpp
index 8506a5c00e7bc..b2dd4b3b54869 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -7681,7 +7681,13 @@ void Clang::ConstructJob(Compilation &C, const JobAction 
&JA,
   if (Args.hasArg(options::OPT_fretain_comments_from_system_headers))
 CmdArgs.push_back("-fretain-comments-from-system-headers");
 
-  Args.AddLastArg(CmdArgs, options::OPT_fextend_variable_liveness_EQ);
+  if (Arg *A = Args.getLastArg(options::OPT_fextend_variable_liveness_EQ)) {
+A->render(Args, CmdArgs);
+  } else if (Arg *A = Args.getLastArg(options::OPT_O_Group);
+ A && A->containsValue("g")) {
+// Set -fextend-variable-liveness=all by default at -Og.
+CmdArgs.push_back("-fextend-variable-liveness=all");
+  }
 
   // Forward -fcomment-block-commands to -cc1.
   Args.AddAllArgs(CmdArgs, options::OPT_fcomment_block_commands);
diff --git a/clang/test/Driver/extend-variable-liveness.c 
b/clang/test/Driver/extend-variable-liveness.c
index bbfb2ece6f297..99a5409ceccea 100644
--- a/clang/test/Driver/extend-variable-liveness.c
+++ b/clang/test/Driver/extend-variable-liveness.c
@@ -1,7 +1,8 @@
 // Tests that -fextend-variable-liveness and its aliases are correctly passed
-// by the driver.
+// by the driver, and are set by default at -Og.
 
 // RUN: %clang -### -c %s 2>&1 | FileCheck %s --check-prefixes=CHECK,DEFAULT
+// RUN: %clang -### -Og -c %s 2>&1 | FileCheck %s --check-prefixes=CHECK,ALL
 // RUN: %clang -fextend-variable-liveness=none -### -c %s 2>&1 | FileCheck %s 
--check-prefixes=CHECK,NONE
 // RUN: %clang -fextend-variable-liveness=this -### -c %s 2>&1 | FileCheck %s 
--check-prefixes=CHECK,THIS
 // RUN: %clang -fextend-variable-liveness=all -### -c %s 2>&1 | FileCheck %s 
--check-prefixes=CHECK,ALL

>From e5037472fd98a7f4a2d9f8298799f46a8b647f87 Mon Sep 17 00:00:00 2001
From: Stephen Tozer 
Date: Thu, 17 Apr 2025 15:14:55 +0100
Subject: [PATCH 2/2] Update docs to fit current compiler/doc state

---
 clang/docs/CommandGuide/clang.rst | 2 +-
 clang/docs/ReleaseNotes.rst   | 6 +++---
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/clang/docs/CommandGuide/clang.rst 
b/clang/docs/CommandGuide/clang.rst
index 68e9e07ed0005..a1e738f1afec8 100644
--- a/clang/docs/CommandGuide/clang.rst
+++ b/clang/docs/CommandGuide/clang.rst
@@ -445,7 +445,7 @@ Code Generation Options
 
 :option:`-Og` Similar to :option:`-O1`, but with slightly reduced
 optimization and better variable visibility. The same optimizations are run
-as at :option:`-O1`, but the :option:`-fextend-variable-liveness` flag is
+as at :option:`-O1`, but the ``-fextend-variable-liveness`` flag is
 also set, which tries to prevent optimizations from reducing the liveness 
of
 user variables, improving their availability when debugging.
 
diff --git a/clang/docs/ReleaseN

[clang] [Clang] Enable -fextend-lifetimes at -Og (PR #118026)

2025-04-17 Thread Stephen Tozer via cfe-commits

SLTozer wrote:

> Is this ready to go? I can't recall if we ultimately accepted the RFC.

My interpretation of the RFC is that there was a general acceptance of `-Og = 
-O1 + -fextend-variable-liveness`, so with the acceptance here I believe this 
is ready!

https://github.com/llvm/llvm-project/pull/118026
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Enable -fextend-lifetimes at -Og (PR #118026)

2025-04-16 Thread Fangrui Song via cfe-commits

https://github.com/MaskRay approved this pull request.


https://github.com/llvm/llvm-project/pull/118026
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Enable -fextend-lifetimes at -Og (PR #118026)

2025-04-16 Thread Reid Kleckner via cfe-commits

https://github.com/rnk approved this pull request.

Is this ready to go? I can't recall if we ultimately accepted the RFC.

https://github.com/llvm/llvm-project/pull/118026
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Enable -fextend-lifetimes at -Og (PR #118026)

2025-01-28 Thread Stephen Tozer via cfe-commits

SLTozer wrote:

With the prior patches having landed, this is now in a state where it could 
land; I've rebased it onto main, meaning all of the code diff in this PR is now 
relevant.

https://github.com/llvm/llvm-project/pull/118026
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Enable -fextend-lifetimes at -Og (PR #118026)

2025-01-28 Thread Stephen Tozer via cfe-commits

https://github.com/SLTozer updated 
https://github.com/llvm/llvm-project/pull/118026

>From beb5e1955f9b0aa106512bb10d4543232e4d4cfa Mon Sep 17 00:00:00 2001
From: Stephen Tozer 
Date: Thu, 28 Nov 2024 13:53:20 +
Subject: [PATCH] Enable -fextend-lifetimes at -Og

---
 clang/docs/CommandGuide/clang.rst| 7 +--
 clang/docs/ReleaseNotes.rst  | 4 
 clang/lib/Driver/ToolChains/Clang.cpp| 8 +++-
 clang/test/Driver/extend-variable-liveness.c | 3 ++-
 4 files changed, 18 insertions(+), 4 deletions(-)

diff --git a/clang/docs/CommandGuide/clang.rst 
b/clang/docs/CommandGuide/clang.rst
index ca8176f854729b..f8294a01048fec 100644
--- a/clang/docs/CommandGuide/clang.rst
+++ b/clang/docs/CommandGuide/clang.rst
@@ -442,8 +442,11 @@ Code Generation Options
 :option:`-Oz` Like :option:`-Os` (and thus :option:`-O2`), but reduces code
 size further.
 
-:option:`-Og` Like :option:`-O1`. In future versions, this option might
-disable different optimizations in order to improve debuggability.
+:option:`-Og` Similar to :option:`-O1`, but with slightly reduced
+optimization and better variable visibility. The same optimizations are run
+as at :option:`-O1`, but the :option:`-fextend-variable-liveness` flag is
+also set, which tries to prevent optimizations from reducing the liveness 
of
+user variables, improving their availability when debugging.
 
 :option:`-O` Equivalent to :option:`-O1`.
 
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index c5a5da5daf8f5e..7b8670c9f48140 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -580,6 +580,10 @@ Modified Compiler Flags
   ``-fexperimental-modules-reduced-bmi`` flag. The ``-fmodules-reduced-bmi`` 
flag
   is intended to be enabled by default in the future.
 
+- The ``-Og`` optimization flag now sets ``-fextend-variable-liveness``, a new
+  compiler flag which trades a small amount of optimization in exchange for
+  improved variable visibility.
+
 Removed Compiler Flags
 -
 
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp 
b/clang/lib/Driver/ToolChains/Clang.cpp
index 589de953be5be1..0ce32d7d0f1b89 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -7706,7 +7706,13 @@ void Clang::ConstructJob(Compilation &C, const JobAction 
&JA,
   if (Args.hasArg(options::OPT_fretain_comments_from_system_headers))
 CmdArgs.push_back("-fretain-comments-from-system-headers");
 
-  Args.AddLastArg(CmdArgs, options::OPT_fextend_variable_liveness_EQ);
+  if (Arg *A = Args.getLastArg(options::OPT_fextend_variable_liveness_EQ)) {
+A->render(Args, CmdArgs);
+  } else if (Arg *A = Args.getLastArg(options::OPT_O_Group);
+ A && A->containsValue("g")) {
+// Set -fextend-variable-liveness=all by default at -Og.
+CmdArgs.push_back("-fextend-variable-liveness=all");
+  }
 
   // Forward -fcomment-block-commands to -cc1.
   Args.AddAllArgs(CmdArgs, options::OPT_fcomment_block_commands);
diff --git a/clang/test/Driver/extend-variable-liveness.c 
b/clang/test/Driver/extend-variable-liveness.c
index bdd89d6f7721ce..95c9f6bc6904c1 100644
--- a/clang/test/Driver/extend-variable-liveness.c
+++ b/clang/test/Driver/extend-variable-liveness.c
@@ -1,7 +1,8 @@
 // Tests that -fextend-variable-liveness and its aliases are correctly passed
-// by the driver.
+// by the driver, and are set by default at -Og.
 
 // RUN: %clang -### -c %s 2>&1 | FileCheck %s --check-prefixes=CHECK,DEFAULT
+// RUN: %clang -### -Og -c %s 2>&1 | FileCheck %s --check-prefixes=CHECK,ALL
 // RUN: %clang -fextend-variable-liveness=none -### -c %s 2>&1 | FileCheck %s 
--check-prefixes=CHECK,NONE
 // RUN: %clang -fextend-variable-liveness=this -### -c %s 2>&1 | FileCheck %s 
--check-prefixes=CHECK,THIS
 // RUN: %clang -fextend-this-ptr-liveness -### -c %s 2>&1 | FileCheck %s 
--check-prefixes=CHECK,THIS

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Enable -fextend-lifetimes at -Og (PR #118026)

2025-01-24 Thread Stephen Tozer via cfe-commits


@@ -412,6 +412,20 @@ New Compiler Flags
   only for thread-local variables, and none (which corresponds to the
   existing ``-fno-c++-static-destructors`` flag) skips all static
   destructors registration.
+- The ``-fextend-variable-liveness`` flag has been added to allow for improved
+  debugging of optimized code. Using ``-fextend-variable-liveness`` will cause
+  Clang to generate code that tries to preserve the liveness of source 
variables
+  through optimizations, meaning that variables will typically be visible in a
+  debugger more often. The flag has two levels: ``-fextend-variable-liveness``,
+  or ``-fextend-variable-liveness=all``, extendes the liveness of all user
+  variables and the ``this`` pointer. Alternatively ``-fextend-this-ptr``, or
+  ``-fextend-variable-liveness=this``, has the same behaviour but applies only
+  to the ``this`` variable in C++ class member functions, meaning its effect is
+  a strict subset of ``-fextend-variable-liveness``. Note that this flag
+  modifies the results of optimizations that Clang performs, which will result
+  in reduced performance in generated code; however, this feature will not
+  extend the liveness of some variables in cases where doing so would likely
+  have a severe impact on generated code performance.

SLTozer wrote:

The intention here is just to specify to the user that some variables won't 
have their liveness extended, so that they're not caught off-guard if they 
notice some variables getting excluded.

https://github.com/llvm/llvm-project/pull/118026
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Enable -fextend-lifetimes at -Og (PR #118026)

2025-01-24 Thread Jeremy Morse via cfe-commits

https://github.com/jmorse commented:

This looks good, but I'd like to reword some of it

https://github.com/llvm/llvm-project/pull/118026
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Enable -fextend-lifetimes at -Og (PR #118026)

2025-01-24 Thread Jeremy Morse via cfe-commits


@@ -412,6 +412,20 @@ New Compiler Flags
   only for thread-local variables, and none (which corresponds to the
   existing ``-fno-c++-static-destructors`` flag) skips all static
   destructors registration.
+- The ``-fextend-variable-liveness`` flag has been added to allow for improved
+  debugging of optimized code. Using ``-fextend-variable-liveness`` will cause
+  Clang to generate code that tries to preserve the liveness of source 
variables
+  through optimizations, meaning that variables will typically be visible in a
+  debugger more often. The flag has two levels: ``-fextend-variable-liveness``,
+  or ``-fextend-variable-liveness=all``, extendes the liveness of all user
+  variables and the ``this`` pointer. Alternatively ``-fextend-this-ptr``, or
+  ``-fextend-variable-liveness=this``, has the same behaviour but applies only
+  to the ``this`` variable in C++ class member functions, meaning its effect is
+  a strict subset of ``-fextend-variable-liveness``. Note that this flag
+  modifies the results of optimizations that Clang performs, which will result
+  in reduced performance in generated code; however, this feature will not
+  extend the liveness of some variables in cases where doing so would likely
+  have a severe impact on generated code performance.

jmorse wrote:

IMO, just drop the late sentence for brevity -- it's up to the user to work out 
whether the trade-off is worth it.

https://github.com/llvm/llvm-project/pull/118026
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Enable -fextend-lifetimes at -Og (PR #118026)

2025-01-24 Thread Jeremy Morse via cfe-commits

https://github.com/jmorse edited 
https://github.com/llvm/llvm-project/pull/118026
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Enable -fextend-lifetimes at -Og (PR #118026)

2025-01-24 Thread Jeremy Morse via cfe-commits


@@ -4298,6 +4298,26 @@ def stack_usage_file : Separate<["-"], 
"stack-usage-file">,
   Visibility<[CC1Option]>,
   HelpText<"Filename (or -) to write stack usage output to">,
   MarshallingInfoString>;
+def fextend_variable_liveness_EQ : Joined<["-"], "fextend-variable-liveness=">,
+  Group, Visibility<[ClangOption, CC1Option]>,
+  HelpText<"Extend the liveness of user variables through optimizations to "
+   "prevent stale or optimized-out variable values when debugging. Can 
"
+   "be applied to all user variables, or just to the C++ 'this' ptr. "
+   "May choose not to extend the liveness of some variables, such as "
+   "non-scalars larger than 4 unsigned ints, or variables in any "
+   "inlined functions.">,

jmorse wrote:

Are there other compiler flags which have such long help-text? IIRC this is a 
super-short summary that appears on the command line with --help, and thus 
conciseness is key. Developers looking for more information will go to the full 
docs.

https://github.com/llvm/llvm-project/pull/118026
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Enable -fextend-lifetimes at -Og (PR #118026)

2025-01-24 Thread Jeremy Morse via cfe-commits


@@ -412,6 +412,20 @@ New Compiler Flags
   only for thread-local variables, and none (which corresponds to the
   existing ``-fno-c++-static-destructors`` flag) skips all static
   destructors registration.
+- The ``-fextend-variable-liveness`` flag has been added to allow for improved
+  debugging of optimized code. Using ``-fextend-variable-liveness`` will cause
+  Clang to generate code that tries to preserve the liveness of source 
variables
+  through optimizations, meaning that variables will typically be visible in a
+  debugger more often. The flag has two levels: ``-fextend-variable-liveness``,
+  or ``-fextend-variable-liveness=all``, extendes the liveness of all user

jmorse wrote:

```suggestion
  or ``-fextend-variable-liveness=all``, extends the liveness of all user
```

https://github.com/llvm/llvm-project/pull/118026
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Enable -fextend-lifetimes at -Og (PR #118026)

2024-12-12 Thread Stephen Tozer via cfe-commits

SLTozer wrote:

I've updated this patch, and made it dependent on a [previous 
PR](https://github.com/llvm/llvm-project/pull/11) - the logic for enabling 
the flag is coupled with code from the earlier PR, and also we now slightly 
extend the test added in that PR instead of creating a new one.

https://github.com/llvm/llvm-project/pull/118026
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Enable -fextend-lifetimes at -Og (PR #118026)

2024-12-12 Thread Stephen Tozer via cfe-commits


@@ -1834,6 +1834,14 @@ bool CompilerInvocation::ParseCodeGenArgs(CodeGenOptions 
&Opts, ArgList &Args,
 Opts.setInlining(CodeGenOptions::NormalInlining);
   }
 
+  // If we have specified -Og and have not explicitly set 
-fno-extend-lifetimes,
+  // then default to -fextend-lifetimes.
+  if (Arg *A = Args.getLastArg(options::OPT_O_Group);
+  A && A->containsValue("g")) {

SLTozer wrote:

I've moved this behaviour to the driver now. Also regarding the first request 
to use `A->getValue() == "g"` - if just `-O` is passed then `A->getValue()` 
will crash, as there are no values. An alternative would be to change the check 
to `A->getNumValues() && A->getValue() == "g"`, but I think `containsValue` is 
more concise.

https://github.com/llvm/llvm-project/pull/118026
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Enable -fextend-lifetimes at -Og (PR #118026)

2024-12-12 Thread Stephen Tozer via cfe-commits

https://github.com/SLTozer updated 
https://github.com/llvm/llvm-project/pull/118026

>From efd7f58e421b0afdc886688128f72170e0c4a970 Mon Sep 17 00:00:00 2001
From: Stephen Tozer 
Date: Wed, 25 Sep 2024 15:08:39 +0100
Subject: [PATCH 1/2] [Clang] Add "extend lifetime" flags and release note

Following the commit that added the fake use intrinsic to LLVM, this patch
adds a pair of flags for the clang frontend that emit fake use intrinsics,
for the purpose of extending the lifetime of variables (either all source
variables, or just the `this` pointer). This patch does not implement the
fake use intrinsic emission of the flags themselves, it simply adds the flags,
the corresponding release note, and the attachment of the `has_fake_uses`
attribute to affected functions; the remaining functionality appears in the
next patch.
---
 clang/docs/ReleaseNotes.rst  | 14 ++
 clang/include/clang/Basic/CodeGenOptions.def |  3 +++
 clang/include/clang/Basic/CodeGenOptions.h   |  6 ++
 clang/include/clang/Driver/Options.td| 20 
 clang/lib/Driver/ToolChains/Clang.cpp|  2 ++
 clang/test/Driver/extend-variable-liveness.c | 15 +++
 6 files changed, 60 insertions(+)
 create mode 100644 clang/test/Driver/extend-variable-liveness.c

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 601a233b81904f..19a40d4666c727 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -412,6 +412,20 @@ New Compiler Flags
   only for thread-local variables, and none (which corresponds to the
   existing ``-fno-c++-static-destructors`` flag) skips all static
   destructors registration.
+- The ``-fextend-variable-liveness`` flag has been added to allow for improved
+  debugging of optimized code. Using ``-fextend-variable-liveness`` will cause
+  Clang to generate code that tries to preserve the liveness of source 
variables
+  through optimizations, meaning that variables will typically be visible in a
+  debugger more often. The flag has two levels: ``-fextend-variable-liveness``,
+  or ``-fextend-variable-liveness=all``, extendes the liveness of all user
+  variables and the ``this`` pointer. Alternatively ``-fextend-this-ptr``, or
+  ``-fextend-variable-liveness=this``, has the same behaviour but applies only
+  to the ``this`` variable in C++ class member functions, meaning its effect is
+  a strict subset of ``-fextend-variable-liveness``. Note that this flag
+  modifies the results of optimizations that Clang performs, which will result
+  in reduced performance in generated code; however, this feature will not
+  extend the liveness of some variables in cases where doing so would likely
+  have a severe impact on generated code performance.
 
 Deprecated Compiler Flags
 -
diff --git a/clang/include/clang/Basic/CodeGenOptions.def 
b/clang/include/clang/Basic/CodeGenOptions.def
index 4cf22c4ee08ce0..8d181c96bc9641 100644
--- a/clang/include/clang/Basic/CodeGenOptions.def
+++ b/clang/include/clang/Basic/CodeGenOptions.def
@@ -393,6 +393,9 @@ CODEGENOPT(EnableTLSDESC, 1, 0)
 /// Bit size of immediate TLS offsets (0 == use the default).
 VALUE_CODEGENOPT(TLSSize, 8, 0)
 
+/// The types of variables that we will extend the live ranges of.
+ENUM_CODEGENOPT(ExtendVariableLiveness, ExtendVariableLivenessKind, 2, 
ExtendVariableLivenessKind::None)
+
 /// The default stack protector guard offset to use.
 VALUE_CODEGENOPT(StackProtectorGuardOffset, 32, INT_MAX)
 
diff --git a/clang/include/clang/Basic/CodeGenOptions.h 
b/clang/include/clang/Basic/CodeGenOptions.h
index 2dcf98b465661e..f55d9513443e8c 100644
--- a/clang/include/clang/Basic/CodeGenOptions.h
+++ b/clang/include/clang/Basic/CodeGenOptions.h
@@ -95,6 +95,12 @@ class CodeGenOptions : public CodeGenOptionsBase {
 Embed_Marker// Embed a marker as a placeholder for bitcode.
   };
 
+  enum class ExtendVariableLivenessKind {
+None,
+This,
+All,
+  };
+
   enum InlineAsmDialectKind {
 IAD_ATT,
 IAD_Intel,
diff --git a/clang/include/clang/Driver/Options.td 
b/clang/include/clang/Driver/Options.td
index 9c356c9d2ea4ef..bd9e5407d3843b 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -4298,6 +4298,26 @@ def stack_usage_file : Separate<["-"], 
"stack-usage-file">,
   Visibility<[CC1Option]>,
   HelpText<"Filename (or -) to write stack usage output to">,
   MarshallingInfoString>;
+def fextend_variable_liveness_EQ : Joined<["-"], "fextend-variable-liveness=">,
+  Group, Visibility<[ClangOption, CC1Option]>,
+  HelpText<"Extend the liveness of user variables through optimizations to "
+   "prevent stale or optimized-out variable values when debugging. Can 
"
+   "be applied to all user variables, or just to the C++ 'this' ptr. "
+   "May choose not to extend the liveness of some variables, such as "
+   "non-scalars larger than 4 unsigned ints, or 

[clang] [Clang] Enable -fextend-lifetimes at -Og (PR #118026)

2024-12-04 Thread Reid Kleckner via cfe-commits


@@ -1834,6 +1834,14 @@ bool CompilerInvocation::ParseCodeGenArgs(CodeGenOptions 
&Opts, ArgList &Args,
 Opts.setInlining(CodeGenOptions::NormalInlining);
   }
 
+  // If we have specified -Og and have not explicitly set 
-fno-extend-lifetimes,
+  // then default to -fextend-lifetimes.
+  if (Arg *A = Args.getLastArg(options::OPT_O_Group);
+  A && A->containsValue("g")) {

rnk wrote:

I agree, the driver's job is usually to unpack high level flags into 
semi-orthogonal cc1 flags. So, for example, in clang-cl, /O1 becomes a 
collection of things like `-Os -ffunction-sections -fdata-sections` and maybe 
something else I've forgotten. `clang++ -fexceptions` becomes `-fexceptions 
-fcxx-exceptions` in most contexts, separating destructor cleanups from 
try/throw language support.

cc1 flags kind of represent features that are useful to toggle on and off for 
testing and test case reduction, since they're one of the things we reduce in 
the [clang crash reducer 
script](https://github.com/llvm/llvm-project/blob/main/clang/utils/creduce-clang-crash.py).

https://github.com/llvm/llvm-project/pull/118026
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Enable -fextend-lifetimes at -Og (PR #118026)

2024-12-04 Thread David Blaikie via cfe-commits


@@ -1834,6 +1834,14 @@ bool CompilerInvocation::ParseCodeGenArgs(CodeGenOptions 
&Opts, ArgList &Args,
 Opts.setInlining(CodeGenOptions::NormalInlining);
   }
 
+  // If we have specified -Og and have not explicitly set 
-fno-extend-lifetimes,
+  // then default to -fextend-lifetimes.
+  if (Arg *A = Args.getLastArg(options::OPT_O_Group);
+  A && A->containsValue("g")) {

dwblaikie wrote:

I'm not sure if we have a fundamental principle about how flags should work in 
the driver V the frontend, but yeah - in some ways the driver acts as a 
lowering/canonicalizer - representing flag defaults, etc. So I think the 
expectation that `clang -cc1 -Og` does the same thing as `clang -Og` isn't 
valid/not something we're striving for (& in some sense counter to what we are 
striving for: that all the defaults/grouping is handled by the driver, and the 
frontend can just think about whether a flag is enabled or disabled)

https://github.com/llvm/llvm-project/pull/118026
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Enable -fextend-lifetimes at -Og (PR #118026)

2024-12-04 Thread Stephen Tozer via cfe-commits


@@ -1834,6 +1834,14 @@ bool CompilerInvocation::ParseCodeGenArgs(CodeGenOptions 
&Opts, ArgList &Args,
 Opts.setInlining(CodeGenOptions::NormalInlining);
   }
 
+  // If we have specified -Og and have not explicitly set 
-fno-extend-lifetimes,
+  // then default to -fextend-lifetimes.
+  if (Arg *A = Args.getLastArg(options::OPT_O_Group);
+  A && A->containsValue("g")) {

SLTozer wrote:

Is there an advantage to having it passed through the driver? I ask mainly 
because I see `-fextend-lifetimes` as being a feature of `-Og` optimization, 
and if you invoke `clang -cc1 -Og` directly then you'd expect/hope that the 
optimization behaviour would be the same as `clang -Og` - I'm not too familiar 
with expected Clang behaviour though, so if this is quite normal for 
driver/frontend behaviour then I'm fine to switch it.

https://github.com/llvm/llvm-project/pull/118026
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Enable -fextend-lifetimes at -Og (PR #118026)

2024-11-29 Thread Fangrui Song via cfe-commits


@@ -0,0 +1,30 @@
+/// Check that we generate fake uses only when -fextend-lifetimes is set and we
+/// are not setting optnone, or when we have optimizations set to -Og and we 
have
+/// not passed -fno-extend-lifetimes.
+// RUN: %clang_cc1 -emit-llvm -disable-llvm-passes -O0 -fextend-lifetimes %s 
-o - | FileCheck %s

MaskRay wrote:

We just need two RUN lines, one with -fextended-lifetimes and one without. -O 
group is completely orthogonal and does not need to be tested

https://github.com/llvm/llvm-project/pull/118026
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Enable -fextend-lifetimes at -Og (PR #118026)

2024-11-29 Thread Fangrui Song via cfe-commits


@@ -1834,6 +1834,14 @@ bool CompilerInvocation::ParseCodeGenArgs(CodeGenOptions 
&Opts, ArgList &Args,
 Opts.setInlining(CodeGenOptions::NormalInlining);
   }
 
+  // If we have specified -Og and have not explicitly set 
-fno-extend-lifetimes,
+  // then default to -fextend-lifetimes.
+  if (Arg *A = Args.getLastArg(options::OPT_O_Group);
+  A && A->containsValue("g")) {

MaskRay wrote:

`A->getValue() == "g"`

Perhaps move the code around ToolChains/Clang.cpp:9198 and let the driver pass 
-fextend-lifetimes to frontend.

https://github.com/llvm/llvm-project/pull/118026
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Enable -fextend-lifetimes at -Og (PR #118026)

2024-11-29 Thread Stephen Tozer via cfe-commits

https://github.com/SLTozer updated 
https://github.com/llvm/llvm-project/pull/118026

>From 7e42a82d73e3be98aab8e523b16a2e43d31b0552 Mon Sep 17 00:00:00 2001
From: Stephen Tozer 
Date: Thu, 28 Nov 2024 13:53:20 +
Subject: [PATCH 1/2] Enable -fextend-lifetimes at -Og

---
 clang/docs/CommandGuide/clang.rst |  7 --
 clang/docs/ReleaseNotes.rst   |  4 +++
 clang/lib/Frontend/CompilerInvocation.cpp |  8 ++
 clang/test/CodeGen/fake-use-opt-flags.cpp | 30 +++
 4 files changed, 47 insertions(+), 2 deletions(-)
 create mode 100644 clang/test/CodeGen/fake-use-opt-flags.cpp

diff --git a/clang/docs/CommandGuide/clang.rst 
b/clang/docs/CommandGuide/clang.rst
index ca8176f854729b..8f3f0de9446b41 100644
--- a/clang/docs/CommandGuide/clang.rst
+++ b/clang/docs/CommandGuide/clang.rst
@@ -442,8 +442,11 @@ Code Generation Options
 :option:`-Oz` Like :option:`-Os` (and thus :option:`-O2`), but reduces code
 size further.
 
-:option:`-Og` Like :option:`-O1`. In future versions, this option might
-disable different optimizations in order to improve debuggability.
+:option:`-Og` Similar to :option:`-O1`, but with slightly reduced
+optimization and better variable visibility. The same optimizations are run
+as at :option:`-O1`, but the :option:`-fextend-lifetimes` flag is also
+set, which tries to prevent optimizations from reducing the lifetime of
+user variables.
 
 :option:`-O` Equivalent to :option:`-O1`.
 
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 601a233b81904f..14fec70dfb85f7 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -441,6 +441,10 @@ Modified Compiler Flags
   ``memset`` and similar functions for which it is a documented undefined
   behavior. It is implied by ``-Wnontrivial-memaccess``
 
+- The ``-Og`` optimization flag now sets ``-fextend-lifetimes``, a new compiler
+  flag, resulting in slightly reduced optimization compared to ``-O1`` in
+  exchange for improved variable visibility.
+
 Removed Compiler Flags
 -
 
diff --git a/clang/lib/Frontend/CompilerInvocation.cpp 
b/clang/lib/Frontend/CompilerInvocation.cpp
index 3dd94c31b2bc7a..09322d0617aaad 100644
--- a/clang/lib/Frontend/CompilerInvocation.cpp
+++ b/clang/lib/Frontend/CompilerInvocation.cpp
@@ -1834,6 +1834,14 @@ bool CompilerInvocation::ParseCodeGenArgs(CodeGenOptions 
&Opts, ArgList &Args,
 Opts.setInlining(CodeGenOptions::NormalInlining);
   }
 
+  // If we have specified -Og and have not set any explicit -fextend-lifetimes
+  // value, then default to -fextend-lifetimes=all.
+  if (Arg *A = Args.getLastArg(options::OPT_O_Group);
+  A && A->containsValue("g")) {
+if (!Args.hasArg(options::OPT_fextend_lifetimes))
+  Opts.ExtendLifetimes = true;
+  }
+
   // PIC defaults to -fno-direct-access-external-data while non-PIC defaults to
   // -fdirect-access-external-data.
   Opts.DirectAccessExternalData =
diff --git a/clang/test/CodeGen/fake-use-opt-flags.cpp 
b/clang/test/CodeGen/fake-use-opt-flags.cpp
new file mode 100644
index 00..a99ed7a573ab22
--- /dev/null
+++ b/clang/test/CodeGen/fake-use-opt-flags.cpp
@@ -0,0 +1,30 @@
+/// Check that we generate fake uses only when -fextend-lifetimes is set and we
+/// are not setting optnone, or when we have optimizations set to -Og and we 
have
+/// not passed -fno-extend-lifetimes.
+// RUN: %clang_cc1 -emit-llvm -disable-llvm-passes -O0 -fextend-lifetimes %s 
-o - | FileCheck %s
+// RUN: %clang_cc1 -emit-llvm -disable-llvm-passes -O0 -disable-O0-optnone 
-fextend-lifetimes %s -o - | FileCheck %s --check-prefixes=CHECK,EXTEND
+// RUN: %clang_cc1 -emit-llvm -disable-llvm-passes -Og %s -o - | FileCheck %s 
--check-prefixes=CHECK,EXTEND
+// RUN: %clang_cc1 -emit-llvm -disable-llvm-passes -Og -fno-extend-lifetimes 
%s -o - | FileCheck %s
+
+/// Test various optimization flags with -fextend-lifetimes...
+// RUN: %clang_cc1 -emit-llvm -disable-llvm-passes -O1 %s -o - | FileCheck %s
+// RUN: %clang_cc1 -emit-llvm -disable-llvm-passes -O1 -fextend-lifetimes %s 
-o - | FileCheck %s --check-prefixes=CHECK,EXTEND
+// RUN: %clang_cc1 -emit-llvm -disable-llvm-passes -O2 %s -o - | FileCheck %s
+// RUN: %clang_cc1 -emit-llvm -disable-llvm-passes -O2 -fextend-lifetimes %s 
-o - | FileCheck %s --check-prefixes=CHECK,EXTEND
+// RUN: %clang_cc1 -emit-llvm -disable-llvm-passes -O3 %s -o - | FileCheck %s
+// RUN: %clang_cc1 -emit-llvm -disable-llvm-passes -O3 -fextend-lifetimes %s 
-o - | FileCheck %s --check-prefixes=CHECK,EXTEND
+// RUN: %clang_cc1 -emit-llvm -disable-llvm-passes -Os %s -o - | FileCheck %s
+// RUN: %clang_cc1 -emit-llvm -disable-llvm-passes -Os -fextend-lifetimes %s 
-o - | FileCheck %s --check-prefixes=CHECK,EXTEND
+// RUN: %clang_cc1 -emit-llvm -disable-llvm-passes -Oz %s -o - | FileCheck %s
+// RUN: %clang_cc1 -emit-llvm -disable-llvm-passes -Oz -fextend-lifetimes %s 
-o - | FileCheck %s --check-prefixes

[clang] [Clang] Enable -fextend-lifetimes at -Og (PR #118026)

2024-11-29 Thread Stephen Tozer via cfe-commits

https://github.com/SLTozer edited 
https://github.com/llvm/llvm-project/pull/118026
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Enable -fextend-lifetimes at -Og (PR #118026)

2024-11-29 Thread Stephen Tozer via cfe-commits


@@ -0,0 +1,30 @@
+/// Check that we generate fake uses only when -fextend-lifetimes is set and we
+/// are not setting optnone, or when we have optimizations set to -Og and we 
have
+/// not passed -fno-extend-lifetimes.
+// RUN: %clang_cc1 -emit-llvm -disable-llvm-passes -O0 -fextend-lifetimes %s 
-o - | FileCheck %s

SLTozer wrote:

Normally I would use implicit-check-not, but in this case we're `CHECK-NEXT`ing 
from the start of the function to the very end, which equally guarantees that 
no fake uses have been generated.

https://github.com/llvm/llvm-project/pull/118026
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Enable -fextend-lifetimes at -Og (PR #118026)

2024-11-29 Thread Stephen Tozer via cfe-commits


@@ -1834,6 +1834,14 @@ bool CompilerInvocation::ParseCodeGenArgs(CodeGenOptions 
&Opts, ArgList &Args,
 Opts.setInlining(CodeGenOptions::NormalInlining);
   }
 
+  // If we have specified -Og and have not set any explicit -fextend-lifetimes
+  // value, then default to -fextend-lifetimes=all.

SLTozer wrote:

Ah, I implemented the `=` option on a local-only branch, and accidentally left 
this comment in. My mistake, though I may yet decide to merge the feature into 
the earlier PRs.

https://github.com/llvm/llvm-project/pull/118026
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Enable -fextend-lifetimes at -Og (PR #118026)

2024-11-29 Thread Jeremy Morse via cfe-commits


@@ -1834,6 +1834,14 @@ bool CompilerInvocation::ParseCodeGenArgs(CodeGenOptions 
&Opts, ArgList &Args,
 Opts.setInlining(CodeGenOptions::NormalInlining);
   }
 
+  // If we have specified -Og and have not set any explicit -fextend-lifetimes
+  // value, then default to -fextend-lifetimes=all.

jmorse wrote:

This is going to create confusion by speculating the existence of an 
`={all,params,this}`  fextend-lifetimes when it doesn't exist yet, and might 
not.

https://github.com/llvm/llvm-project/pull/118026
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Enable -fextend-lifetimes at -Og (PR #118026)

2024-11-29 Thread Jeremy Morse via cfe-commits

https://github.com/jmorse edited 
https://github.com/llvm/llvm-project/pull/118026
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Enable -fextend-lifetimes at -Og (PR #118026)

2024-11-29 Thread Jeremy Morse via cfe-commits


@@ -441,6 +441,10 @@ Modified Compiler Flags
   ``memset`` and similar functions for which it is a documented undefined
   behavior. It is implied by ``-Wnontrivial-memaccess``
 
+- The ``-Og`` optimization flag now sets ``-fextend-lifetimes``, a new compiler
+  flag, resulting in slightly reduced optimization compared to ``-O1`` in

jmorse wrote:

Can we just say "trading optimization for improved..." to indicate that this is 
a conscious decision the developer might make?

https://github.com/llvm/llvm-project/pull/118026
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Enable -fextend-lifetimes at -Og (PR #118026)

2024-11-29 Thread Jeremy Morse via cfe-commits


@@ -0,0 +1,30 @@
+/// Check that we generate fake uses only when -fextend-lifetimes is set and we
+/// are not setting optnone, or when we have optimizations set to -Og and we 
have
+/// not passed -fno-extend-lifetimes.
+// RUN: %clang_cc1 -emit-llvm -disable-llvm-passes -O0 -fextend-lifetimes %s 
-o - | FileCheck %s

jmorse wrote:

Intention of this RUN line is that no fake uses are generated, but there are no 
test-lines for ensuring that -> add an implicit-fake-not? And the other 
check-only FileCheck commands.

https://github.com/llvm/llvm-project/pull/118026
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Enable -fextend-lifetimes at -Og (PR #118026)

2024-11-29 Thread Jeremy Morse via cfe-commits

https://github.com/jmorse commented:

Some minor test comments, otherwise looking good,

https://github.com/llvm/llvm-project/pull/118026
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Enable -fextend-lifetimes at -Og (PR #118026)

2024-11-28 Thread via cfe-commits

llvmbot wrote:




@llvm/pr-subscribers-clang-codegen

Author: Stephen Tozer (SLTozer)


Changes

This patch follows two other ongoing reviews, 
https://github.com/llvm/llvm-project/pull/11 and 
https://github.com/llvm/llvm-project/pull/110102, which add a new feature to 
Clang - `-fextend-lifetimes`, which generates fake uses for user variables (and 
the `this` pointer) to prevent them from being optimized out, providing a 
better debug experience at the cost of less efficient generated code. This is 
useful for debugging code without the need to disable optimizations outright, 
which is particularly important when debugging applications where some level of 
optimization is necessary, e.g. game code.

Following the inclusion of the flag, this patch enables it by default when 
`-Og` is set. Currently, `-Og` is equivalent to `-O1` - it is effectively just 
an alias. By enabling `-fextend-lifetimes`, this patch changes the code 
generated by Clang with `-Og` to have reduced optimization and greater 
debuggability than `-O1`, differentiating the two according to their respective 
purposes. This idea was discussed previously on 
[Discourse](https://discourse.llvm.org/t/rfc-redefine-og-o1-and-add-a-new-level-of-og/72850),
 where there was general agreement with the principle of this change.

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


4 Files Affected:

- (modified) clang/docs/CommandGuide/clang.rst (+5-2) 
- (modified) clang/docs/ReleaseNotes.rst (+4) 
- (modified) clang/lib/Frontend/CompilerInvocation.cpp (+8) 
- (added) clang/test/CodeGen/fake-use-opt-flags.cpp (+30) 


``diff
diff --git a/clang/docs/CommandGuide/clang.rst 
b/clang/docs/CommandGuide/clang.rst
index ca8176f854729b..8f3f0de9446b41 100644
--- a/clang/docs/CommandGuide/clang.rst
+++ b/clang/docs/CommandGuide/clang.rst
@@ -442,8 +442,11 @@ Code Generation Options
 :option:`-Oz` Like :option:`-Os` (and thus :option:`-O2`), but reduces code
 size further.
 
-:option:`-Og` Like :option:`-O1`. In future versions, this option might
-disable different optimizations in order to improve debuggability.
+:option:`-Og` Similar to :option:`-O1`, but with slightly reduced
+optimization and better variable visibility. The same optimizations are run
+as at :option:`-O1`, but the :option:`-fextend-lifetimes` flag is also
+set, which tries to prevent optimizations from reducing the lifetime of
+user variables.
 
 :option:`-O` Equivalent to :option:`-O1`.
 
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 601a233b81904f..14fec70dfb85f7 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -441,6 +441,10 @@ Modified Compiler Flags
   ``memset`` and similar functions for which it is a documented undefined
   behavior. It is implied by ``-Wnontrivial-memaccess``
 
+- The ``-Og`` optimization flag now sets ``-fextend-lifetimes``, a new compiler
+  flag, resulting in slightly reduced optimization compared to ``-O1`` in
+  exchange for improved variable visibility.
+
 Removed Compiler Flags
 -
 
diff --git a/clang/lib/Frontend/CompilerInvocation.cpp 
b/clang/lib/Frontend/CompilerInvocation.cpp
index 3dd94c31b2bc7a..09322d0617aaad 100644
--- a/clang/lib/Frontend/CompilerInvocation.cpp
+++ b/clang/lib/Frontend/CompilerInvocation.cpp
@@ -1834,6 +1834,14 @@ bool CompilerInvocation::ParseCodeGenArgs(CodeGenOptions 
&Opts, ArgList &Args,
 Opts.setInlining(CodeGenOptions::NormalInlining);
   }
 
+  // If we have specified -Og and have not set any explicit -fextend-lifetimes
+  // value, then default to -fextend-lifetimes=all.
+  if (Arg *A = Args.getLastArg(options::OPT_O_Group);
+  A && A->containsValue("g")) {
+if (!Args.hasArg(options::OPT_fextend_lifetimes))
+  Opts.ExtendLifetimes = true;
+  }
+
   // PIC defaults to -fno-direct-access-external-data while non-PIC defaults to
   // -fdirect-access-external-data.
   Opts.DirectAccessExternalData =
diff --git a/clang/test/CodeGen/fake-use-opt-flags.cpp 
b/clang/test/CodeGen/fake-use-opt-flags.cpp
new file mode 100644
index 00..a99ed7a573ab22
--- /dev/null
+++ b/clang/test/CodeGen/fake-use-opt-flags.cpp
@@ -0,0 +1,30 @@
+/// Check that we generate fake uses only when -fextend-lifetimes is set and we
+/// are not setting optnone, or when we have optimizations set to -Og and we 
have
+/// not passed -fno-extend-lifetimes.
+// RUN: %clang_cc1 -emit-llvm -disable-llvm-passes -O0 -fextend-lifetimes %s 
-o - | FileCheck %s
+// RUN: %clang_cc1 -emit-llvm -disable-llvm-passes -O0 -disable-O0-optnone 
-fextend-lifetimes %s -o - | FileCheck %s --check-prefixes=CHECK,EXTEND
+// RUN: %clang_cc1 -emit-llvm -disable-llvm-passes -Og %s -o - | FileCheck %s 
--check-prefixes=CHECK,EXTEND
+// RUN: %clang_cc1 -emit-llvm -disable-llvm-passes -Og -fno-extend-lifetimes 
%s -o - | FileCheck %s
+
+/// Test various optimization flags with -fextend-lifetimes...
+// RUN: 

[clang] [Clang] Enable -fextend-lifetimes at -Og (PR #118026)

2024-11-28 Thread via cfe-commits

llvmbot wrote:




@llvm/pr-subscribers-clang

Author: Stephen Tozer (SLTozer)


Changes

This patch follows two other ongoing reviews, 
https://github.com/llvm/llvm-project/pull/11 and 
https://github.com/llvm/llvm-project/pull/110102, which add a new feature to 
Clang - `-fextend-lifetimes`, which generates fake uses for user variables (and 
the `this` pointer) to prevent them from being optimized out, providing a 
better debug experience at the cost of less efficient generated code. This is 
useful for debugging code without the need to disable optimizations outright, 
which is particularly important when debugging applications where some level of 
optimization is necessary, e.g. game code.

Following the inclusion of the flag, this patch enables it by default when 
`-Og` is set. Currently, `-Og` is equivalent to `-O1` - it is effectively just 
an alias. By enabling `-fextend-lifetimes`, this patch changes the code 
generated by Clang with `-Og` to have reduced optimization and greater 
debuggability than `-O1`, differentiating the two according to their respective 
purposes. This idea was discussed previously on 
[Discourse](https://discourse.llvm.org/t/rfc-redefine-og-o1-and-add-a-new-level-of-og/72850),
 where there was general agreement with the principle of this change.

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


4 Files Affected:

- (modified) clang/docs/CommandGuide/clang.rst (+5-2) 
- (modified) clang/docs/ReleaseNotes.rst (+4) 
- (modified) clang/lib/Frontend/CompilerInvocation.cpp (+8) 
- (added) clang/test/CodeGen/fake-use-opt-flags.cpp (+30) 


``diff
diff --git a/clang/docs/CommandGuide/clang.rst 
b/clang/docs/CommandGuide/clang.rst
index ca8176f854729b..8f3f0de9446b41 100644
--- a/clang/docs/CommandGuide/clang.rst
+++ b/clang/docs/CommandGuide/clang.rst
@@ -442,8 +442,11 @@ Code Generation Options
 :option:`-Oz` Like :option:`-Os` (and thus :option:`-O2`), but reduces code
 size further.
 
-:option:`-Og` Like :option:`-O1`. In future versions, this option might
-disable different optimizations in order to improve debuggability.
+:option:`-Og` Similar to :option:`-O1`, but with slightly reduced
+optimization and better variable visibility. The same optimizations are run
+as at :option:`-O1`, but the :option:`-fextend-lifetimes` flag is also
+set, which tries to prevent optimizations from reducing the lifetime of
+user variables.
 
 :option:`-O` Equivalent to :option:`-O1`.
 
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 601a233b81904f..14fec70dfb85f7 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -441,6 +441,10 @@ Modified Compiler Flags
   ``memset`` and similar functions for which it is a documented undefined
   behavior. It is implied by ``-Wnontrivial-memaccess``
 
+- The ``-Og`` optimization flag now sets ``-fextend-lifetimes``, a new compiler
+  flag, resulting in slightly reduced optimization compared to ``-O1`` in
+  exchange for improved variable visibility.
+
 Removed Compiler Flags
 -
 
diff --git a/clang/lib/Frontend/CompilerInvocation.cpp 
b/clang/lib/Frontend/CompilerInvocation.cpp
index 3dd94c31b2bc7a..09322d0617aaad 100644
--- a/clang/lib/Frontend/CompilerInvocation.cpp
+++ b/clang/lib/Frontend/CompilerInvocation.cpp
@@ -1834,6 +1834,14 @@ bool CompilerInvocation::ParseCodeGenArgs(CodeGenOptions 
&Opts, ArgList &Args,
 Opts.setInlining(CodeGenOptions::NormalInlining);
   }
 
+  // If we have specified -Og and have not set any explicit -fextend-lifetimes
+  // value, then default to -fextend-lifetimes=all.
+  if (Arg *A = Args.getLastArg(options::OPT_O_Group);
+  A && A->containsValue("g")) {
+if (!Args.hasArg(options::OPT_fextend_lifetimes))
+  Opts.ExtendLifetimes = true;
+  }
+
   // PIC defaults to -fno-direct-access-external-data while non-PIC defaults to
   // -fdirect-access-external-data.
   Opts.DirectAccessExternalData =
diff --git a/clang/test/CodeGen/fake-use-opt-flags.cpp 
b/clang/test/CodeGen/fake-use-opt-flags.cpp
new file mode 100644
index 00..a99ed7a573ab22
--- /dev/null
+++ b/clang/test/CodeGen/fake-use-opt-flags.cpp
@@ -0,0 +1,30 @@
+/// Check that we generate fake uses only when -fextend-lifetimes is set and we
+/// are not setting optnone, or when we have optimizations set to -Og and we 
have
+/// not passed -fno-extend-lifetimes.
+// RUN: %clang_cc1 -emit-llvm -disable-llvm-passes -O0 -fextend-lifetimes %s 
-o - | FileCheck %s
+// RUN: %clang_cc1 -emit-llvm -disable-llvm-passes -O0 -disable-O0-optnone 
-fextend-lifetimes %s -o - | FileCheck %s --check-prefixes=CHECK,EXTEND
+// RUN: %clang_cc1 -emit-llvm -disable-llvm-passes -Og %s -o - | FileCheck %s 
--check-prefixes=CHECK,EXTEND
+// RUN: %clang_cc1 -emit-llvm -disable-llvm-passes -Og -fno-extend-lifetimes 
%s -o - | FileCheck %s
+
+/// Test various optimization flags with -fextend-lifetimes...
+// RUN: %clang_c

[clang] [Clang] Enable -fextend-lifetimes at -Og (PR #118026)

2024-11-28 Thread Stephen Tozer via cfe-commits

https://github.com/SLTozer created 
https://github.com/llvm/llvm-project/pull/118026

This patch follows two other ongoing reviews, 
https://github.com/llvm/llvm-project/pull/11 and 
https://github.com/llvm/llvm-project/pull/110102, which add a new feature to 
Clang - `-fextend-lifetimes`, which generates fake uses for user variables (and 
the `this` pointer) to prevent them from being optimized out, providing a 
better debug experience at the cost of less efficient generated code. This is 
useful for debugging code without the need to disable optimizations outright, 
which is particularly important when debugging applications where some level of 
optimization is necessary, e.g. game code.

Following the inclusion of the flag, this patch enables it by default when 
`-Og` is set. Currently, `-Og` is equivalent to `-O1` - it is effectively just 
an alias. By enabling `-fextend-lifetimes`, this patch changes the code 
generated by Clang with `-Og` to have reduced optimization and greater 
debuggability than `-O1`, differentiating the two according to their respective 
purposes. This idea was discussed previously on 
[Discourse](https://discourse.llvm.org/t/rfc-redefine-og-o1-and-add-a-new-level-of-og/72850),
 where there was general agreement with the principle of this change.

>From 7e42a82d73e3be98aab8e523b16a2e43d31b0552 Mon Sep 17 00:00:00 2001
From: Stephen Tozer 
Date: Thu, 28 Nov 2024 13:53:20 +
Subject: [PATCH] Enable -fextend-lifetimes at -Og

---
 clang/docs/CommandGuide/clang.rst |  7 --
 clang/docs/ReleaseNotes.rst   |  4 +++
 clang/lib/Frontend/CompilerInvocation.cpp |  8 ++
 clang/test/CodeGen/fake-use-opt-flags.cpp | 30 +++
 4 files changed, 47 insertions(+), 2 deletions(-)
 create mode 100644 clang/test/CodeGen/fake-use-opt-flags.cpp

diff --git a/clang/docs/CommandGuide/clang.rst 
b/clang/docs/CommandGuide/clang.rst
index ca8176f854729b..8f3f0de9446b41 100644
--- a/clang/docs/CommandGuide/clang.rst
+++ b/clang/docs/CommandGuide/clang.rst
@@ -442,8 +442,11 @@ Code Generation Options
 :option:`-Oz` Like :option:`-Os` (and thus :option:`-O2`), but reduces code
 size further.
 
-:option:`-Og` Like :option:`-O1`. In future versions, this option might
-disable different optimizations in order to improve debuggability.
+:option:`-Og` Similar to :option:`-O1`, but with slightly reduced
+optimization and better variable visibility. The same optimizations are run
+as at :option:`-O1`, but the :option:`-fextend-lifetimes` flag is also
+set, which tries to prevent optimizations from reducing the lifetime of
+user variables.
 
 :option:`-O` Equivalent to :option:`-O1`.
 
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 601a233b81904f..14fec70dfb85f7 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -441,6 +441,10 @@ Modified Compiler Flags
   ``memset`` and similar functions for which it is a documented undefined
   behavior. It is implied by ``-Wnontrivial-memaccess``
 
+- The ``-Og`` optimization flag now sets ``-fextend-lifetimes``, a new compiler
+  flag, resulting in slightly reduced optimization compared to ``-O1`` in
+  exchange for improved variable visibility.
+
 Removed Compiler Flags
 -
 
diff --git a/clang/lib/Frontend/CompilerInvocation.cpp 
b/clang/lib/Frontend/CompilerInvocation.cpp
index 3dd94c31b2bc7a..09322d0617aaad 100644
--- a/clang/lib/Frontend/CompilerInvocation.cpp
+++ b/clang/lib/Frontend/CompilerInvocation.cpp
@@ -1834,6 +1834,14 @@ bool CompilerInvocation::ParseCodeGenArgs(CodeGenOptions 
&Opts, ArgList &Args,
 Opts.setInlining(CodeGenOptions::NormalInlining);
   }
 
+  // If we have specified -Og and have not set any explicit -fextend-lifetimes
+  // value, then default to -fextend-lifetimes=all.
+  if (Arg *A = Args.getLastArg(options::OPT_O_Group);
+  A && A->containsValue("g")) {
+if (!Args.hasArg(options::OPT_fextend_lifetimes))
+  Opts.ExtendLifetimes = true;
+  }
+
   // PIC defaults to -fno-direct-access-external-data while non-PIC defaults to
   // -fdirect-access-external-data.
   Opts.DirectAccessExternalData =
diff --git a/clang/test/CodeGen/fake-use-opt-flags.cpp 
b/clang/test/CodeGen/fake-use-opt-flags.cpp
new file mode 100644
index 00..a99ed7a573ab22
--- /dev/null
+++ b/clang/test/CodeGen/fake-use-opt-flags.cpp
@@ -0,0 +1,30 @@
+/// Check that we generate fake uses only when -fextend-lifetimes is set and we
+/// are not setting optnone, or when we have optimizations set to -Og and we 
have
+/// not passed -fno-extend-lifetimes.
+// RUN: %clang_cc1 -emit-llvm -disable-llvm-passes -O0 -fextend-lifetimes %s 
-o - | FileCheck %s
+// RUN: %clang_cc1 -emit-llvm -disable-llvm-passes -O0 -disable-O0-optnone 
-fextend-lifetimes %s -o - | FileCheck %s --check-prefixes=CHECK,EXTEND
+// RUN: %clang_cc1 -emit-llvm -disable-llvm-passes -Og %s -o - | FileCheck %s 
--check-prefix