Skip to content

Commit

Permalink
[Driver] Add support for -finline-functions and /Ob2 flags
Browse files Browse the repository at this point in the history
-finline-functions and /Ob2 are currently ignored by Clang. The only way to
enable inlining is to use the global O flags, which also enable other options,
or to emit LLVM bitcode using Clang, then running opt by hand with the inline
pass.

This patch allows to simply use the -finline-functions flag (same as GCC) or
/Ob2 in clang-cl mode to enable inlining without other optimizations.

This is the first patch of a serie to improve support for the /Ob flags.

Patch by Rudy Pons <rudy.pons@ilod.org>!

Differential Revision: http://reviews.llvm.org/D20576

llvm-svn: 270609
  • Loading branch information
zmodem committed May 24, 2016
1 parent 0429751 commit 7a00888
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 9 deletions.
3 changes: 2 additions & 1 deletion clang/include/clang/Driver/CLCompatOptions.td
Expand Up @@ -102,6 +102,8 @@ def _SLASH_O0 : CLFlag<"O0">, Alias<O0>;
def _SLASH_O : CLJoined<"O">, HelpText<"Optimization level">;
def _SLASH_Ob0 : CLFlag<"Ob0">, HelpText<"Disable inlining">,
Alias<fno_inline>;
def _SLASH_Ob2 : CLFlag<"Ob2">, HelpText<"Enable inlining">,
Alias<finline_functions>;
def _SLASH_Od : CLFlag<"Od">, HelpText<"Disable optimization">, Alias<O0>;
def _SLASH_Oi : CLFlag<"Oi">, HelpText<"Enable use of builtin functions">,
Alias<fbuiltin>;
Expand Down Expand Up @@ -293,7 +295,6 @@ def _SLASH_GS_ : CLIgnoredFlag<"GS-">;
def _SLASH_kernel_ : CLIgnoredFlag<"kernel-">;
def _SLASH_nologo : CLIgnoredFlag<"nologo">;
def _SLASH_Ob1 : CLIgnoredFlag<"Ob1">;
def _SLASH_Ob2 : CLIgnoredFlag<"Ob2">;
def _SLASH_Og : CLIgnoredFlag<"Og">;
def _SLASH_openmp_ : CLIgnoredFlag<"openmp-">;
def _SLASH_RTC : CLIgnoredJoined<"RTC">;
Expand Down
2 changes: 1 addition & 1 deletion clang/include/clang/Driver/Options.td
Expand Up @@ -741,7 +741,7 @@ def fgnu_runtime : Flag<["-"], "fgnu-runtime">, Group<f_Group>,
def fheinous_gnu_extensions : Flag<["-"], "fheinous-gnu-extensions">, Flags<[CC1Option]>;
def filelist : Separate<["-"], "filelist">, Flags<[LinkerInput]>;
def : Flag<["-"], "findirect-virtual-calls">, Alias<fapple_kext>;
def finline_functions : Flag<["-"], "finline-functions">, Group<clang_ignored_gcc_optimization_f_Group>;
def finline_functions : Flag<["-"], "finline-functions">, Group<f_clang_Group>, Flags<[CC1Option]>;
def finline : Flag<["-"], "finline">, Group<clang_ignored_f_Group>;
def finput_charset_EQ : Joined<["-"], "finput-charset=">, Group<f_Group>;
def fexec_charset_EQ : Joined<["-"], "fexec-charset=">, Group<f_Group>;
Expand Down
5 changes: 3 additions & 2 deletions clang/lib/Driver/Tools.cpp
Expand Up @@ -5332,8 +5332,9 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
if (Args.hasArg(options::OPT_fno_inline))
CmdArgs.push_back("-fno-inline");

if (Args.hasArg(options::OPT_fno_inline_functions))
CmdArgs.push_back("-fno-inline-functions");
if (Arg* InlineArg = Args.getLastArg(options::OPT_finline_functions,
options::OPT_fno_inline_functions))
InlineArg->render(Args, CmdArgs);

ObjCRuntime objcRuntime = AddObjCRuntimeArgs(Args, CmdArgs, rewriteKind);

Expand Down
8 changes: 6 additions & 2 deletions clang/lib/Frontend/CompilerInvocation.cpp
Expand Up @@ -441,8 +441,12 @@ static bool ParseCodeGenArgs(CodeGenOptions &Opts, ArgList &Args, InputKind IK,
: CodeGenOptions::OnlyAlwaysInlining);
// -fno-inline-functions overrides OptimizationLevel > 1.
Opts.NoInline = Args.hasArg(OPT_fno_inline);
Opts.setInlining(Args.hasArg(OPT_fno_inline_functions) ?
CodeGenOptions::OnlyAlwaysInlining : Opts.getInlining());
if (Arg* InlineArg = Args.getLastArg(options::OPT_finline_functions,
options::OPT_fno_inline_functions)) {
Opts.setInlining(
InlineArg->getOption().matches(options::OPT_finline_functions) ?
CodeGenOptions::NormalInlining : CodeGenOptions::OnlyAlwaysInlining);
}

if (Arg *A = Args.getLastArg(OPT_fveclib)) {
StringRef Name = A->getValue();
Expand Down
26 changes: 26 additions & 0 deletions clang/test/CodeGen/inline-optim.cc
@@ -0,0 +1,26 @@
// Make sure -finline-functions family flags are behaving correctly.

// RUN: %clang_cc1 -emit-llvm %s -o - | FileCheck -check-prefix=NOINLINE %s
// RUN: %clang_cc1 -O3 -fno-inline-functions -emit-llvm %s -o - | FileCheck -check-prefix=NOINLINE %s
// RUN: %clang_cc1 -finline-functions -emit-llvm %s -o - | FileCheck -check-prefix=INLINE %s

inline int inline_hint(int a, int b) { return(a+b); }

int inline_no_hint(int a, int b) { return (a/b); }

inline __attribute__ ((__always_inline__)) int inline_always(int a, int b) { return(a*b); }

volatile int *pa = (int*) 0x1000;
void foo() {
// NOINLINE-LABEL: @foo
// INLINE-LABEL: @foo
// NOINLINE: call i32 @inline_hint
// INLINE-NOT: call i32 @inline_hint
pa[0] = inline_hint(pa[1],pa[2]);
// NOINLINE-NOT: call i32 @inline_always
// INLINE-NOT: call i32 @inline_always
pa[3] = inline_always(pa[4],pa[5]);
// NOINLINE: call i32 @inline_no_hint
// INLINE-NOT: call i32 @inline_no_hint
pa[6] = inline_no_hint(pa[7], pa[8]);
}
4 changes: 3 additions & 1 deletion clang/test/Driver/cl-options.c
Expand Up @@ -97,6 +97,9 @@
// RUN: %clang_cl /Ob0 -### -- %s 2>&1 | FileCheck -check-prefix=Ob0 %s
// Ob0: -fno-inline

// RUN: %clang_cl /Ob2 -### -- %s 2>&1 | FileCheck -check-prefix=Ob2 %s
// Ob2: -finline-functions

// RUN: %clang_cl /Od -### -- %s 2>&1 | FileCheck -check-prefix=Od %s
// Od: -O0

Expand Down Expand Up @@ -265,7 +268,6 @@
// RUN: /kernel- \
// RUN: /nologo \
// RUN: /Ob1 \
// RUN: /Ob2 \
// RUN: /openmp- \
// RUN: /RTC1 \
// RUN: /sdl \
Expand Down
2 changes: 0 additions & 2 deletions clang/test/Driver/clang_f_opts.c
Expand Up @@ -285,7 +285,6 @@
// RUN: -fexpensive-optimizations \
// RUN: -fno-expensive-optimizations \
// RUN: -fno-defer-pop \
// RUN: -finline-functions \
// RUN: -fkeep-inline-functions \
// RUN: -fno-keep-inline-functions \
// RUN: -freorder-blocks \
Expand Down Expand Up @@ -353,7 +352,6 @@
// CHECK-WARNING-DAG: optimization flag '-fexpensive-optimizations' is not supported
// CHECK-WARNING-DAG: optimization flag '-fno-expensive-optimizations' is not supported
// CHECK-WARNING-DAG: optimization flag '-fno-defer-pop' is not supported
// CHECK-WARNING-DAG: optimization flag '-finline-functions' is not supported
// CHECK-WARNING-DAG: optimization flag '-fkeep-inline-functions' is not supported
// CHECK-WARNING-DAG: optimization flag '-fno-keep-inline-functions' is not supported
// CHECK-WARNING-DAG: optimization flag '-freorder-blocks' is not supported
Expand Down

0 comments on commit 7a00888

Please sign in to comment.