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 <stephen.to...@sony.com> Date: Thu, 28 Nov 2024 13:53:20 +0000 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 00000000000000..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=CHECK,EXTEND + +// CHECK-LABEL: define{{.*}} void @_Z3fooi(i32{{.*}} %a) +// CHECK-NEXT: entry: +// CHECK-NEXT: %a.addr = alloca i32 +// CHECK-NEXT: store i32 %a, ptr %a.addr +// EXTEND-NEXT: %fake.use = load i32, ptr %a.addr +// EXTEND-NEXT: call void (...) @llvm.fake.use(i32 %fake.use) +// CHECK-NEXT: ret void +// CHECK-NEXT: } + +void foo(int a) {} >From ec32824c6677486f4cc54c6827abf9e9ce709ad7 Mon Sep 17 00:00:00 2001 From: Stephen Tozer <stephen.to...@sony.com> Date: Fri, 29 Nov 2024 17:15:38 +0000 Subject: [PATCH 2/2] Reword release note, fix comment --- clang/docs/ReleaseNotes.rst | 4 ++-- clang/lib/Frontend/CompilerInvocation.cpp | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 14fec70dfb85f7..0b5c2e3c5f35bb 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -442,8 +442,8 @@ Modified Compiler Flags 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. + flag which trades a small amount of optimization in exchange for improved + variable visibility. Removed Compiler Flags ------------------------- diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp index 09322d0617aaad..e2e433319b7e58 100644 --- a/clang/lib/Frontend/CompilerInvocation.cpp +++ b/clang/lib/Frontend/CompilerInvocation.cpp @@ -1834,8 +1834,8 @@ 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 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")) { if (!Args.hasArg(options::OPT_fextend_lifetimes)) _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits