From b26404a7505dab7ec3a16f6e3b85f95cfd59ba93 Mon Sep 17 00:00:00 2001 From: Chandler Carruth Date: Thu, 8 Aug 2013 08:34:35 +0000 Subject: [PATCH] The only useful loop unrolling flag to give realistically is '-fno-unroll-loops'. The option to the backend is even called 'DisableUnrollLoops'. This is precisely the form that Clang *didn't* support. We didn't recognize the flag, we didn't pass it to the CC1 layer, and even if we did we wouldn't use it. Clang only inspected the positive form of the flag, and only did so to enable loop unrolling when the optimization level wasn't high enough. This only occurs for an optimization level that even has a chance of running the loop unroller when optimizing for size. This commit wires up the 'no' variant, and switches the code to actually follow the standard flag pattern of using the last flag and allowing a flag in either direction to override the default. I think this is still wrong. I don't know why we disable the loop unroller entirely *from Clang* when optimizing for size, as the loop unrolling pass *already has special logic* for the case where the function is attributed as optimized for size! We should really be trusting that. Maybe in a follow-up patch, I don't really want to change behavior here. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@187969 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Driver/Options.td | 2 ++ lib/Driver/Tools.cpp | 3 ++- lib/Frontend/CompilerInvocation.cpp | 5 +++-- test/Driver/clang_f_opts.c | 7 +++++++ 4 files changed, 14 insertions(+), 3 deletions(-) diff --git a/include/clang/Driver/Options.td b/include/clang/Driver/Options.td index 21f0417b35..4598265aeb 100644 --- a/include/clang/Driver/Options.td +++ b/include/clang/Driver/Options.td @@ -791,6 +791,8 @@ def ftrap_function_EQ : Joined<["-"], "ftrap-function=">, Group, Flags< def funit_at_a_time : Flag<["-"], "funit-at-a-time">, Group; def funroll_loops : Flag<["-"], "funroll-loops">, Group, HelpText<"Turn on loop unroller">, Flags<[CC1Option]>; +def fno_unroll_loops : Flag<["-"], "fno-unroll-loops">, Group, + HelpText<"Turn off loop unroller">, Flags<[CC1Option]>; def funsigned_bitfields : Flag<["-"], "funsigned-bitfields">, Group; def funsigned_char : Flag<["-"], "funsigned-char">, Group; def funwind_tables : Flag<["-"], "funwind-tables">, Group; diff --git a/lib/Driver/Tools.cpp b/lib/Driver/Tools.cpp index e4acadbb51..ebfc3175f0 100644 --- a/lib/Driver/Tools.cpp +++ b/lib/Driver/Tools.cpp @@ -2977,7 +2977,8 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, CmdArgs.push_back("-fwrapv"); } Args.AddLastArg(CmdArgs, options::OPT_fwritable_strings); - Args.AddLastArg(CmdArgs, options::OPT_funroll_loops); + Args.AddLastArg(CmdArgs, options::OPT_funroll_loops, + options::OPT_fno_unroll_loops); Args.AddLastArg(CmdArgs, options::OPT_pthread); diff --git a/lib/Frontend/CompilerInvocation.cpp b/lib/Frontend/CompilerInvocation.cpp index a4c93fa5b5..f88c124f08 100644 --- a/lib/Frontend/CompilerInvocation.cpp +++ b/lib/Frontend/CompilerInvocation.cpp @@ -351,8 +351,9 @@ static bool ParseCodeGenArgs(CodeGenOptions &Opts, ArgList &Args, InputKind IK, Opts.OptimizeSize = getOptimizationLevelSize(Args); Opts.SimplifyLibCalls = !(Args.hasArg(OPT_fno_builtin) || Args.hasArg(OPT_ffreestanding)); - Opts.UnrollLoops = Args.hasArg(OPT_funroll_loops) || - (Opts.OptimizationLevel > 1 && !Opts.OptimizeSize); + Opts.UnrollLoops = + Args.hasFlag(OPT_funroll_loops, OPT_fno_unroll_loops, + (Opts.OptimizationLevel > 1 && !Opts.OptimizeSize)); Opts.Autolink = !Args.hasArg(OPT_fno_autolink); Opts.AsmVerbose = Args.hasArg(OPT_masm_verbose); diff --git a/test/Driver/clang_f_opts.c b/test/Driver/clang_f_opts.c index 41d0b5e63d..87843d3b22 100644 --- a/test/Driver/clang_f_opts.c +++ b/test/Driver/clang_f_opts.c @@ -37,6 +37,13 @@ // FP-CONTRACT-FAST-CHECK: -ffp-contract=fast // FP-CONTRACT-OFF-CHECK: -ffp-contract=off +// RUN: %clang -### -S -funroll-loops %s 2>&1 | FileCheck -check-prefix=CHECK-UNROLL-LOOPS %s +// RUN: %clang -### -S -fno-unroll-loops %s 2>&1 | FileCheck -check-prefix=CHECK-NO-UNROLL-LOOPS %s +// RUN: %clang -### -S -fno-unroll-loops -funroll-loops %s 2>&1 | FileCheck -check-prefix=CHECK-UNROLL-LOOPS %s +// RUN: %clang -### -S -funroll-loops -fno-unroll-loops %s 2>&1 | FileCheck -check-prefix=CHECK-NO-UNROLL-LOOPS %s +// CHECK-UNROLL-LOOPS: "-funroll-loops" +// CHECK-NO-UNROLL-LOOPS: "-fno-unroll-loops" + // RUN: %clang -### -S -fvectorize %s 2>&1 | FileCheck -check-prefix=CHECK-VECTORIZE %s // RUN: %clang -### -S -fno-vectorize -fvectorize %s 2>&1 | FileCheck -check-prefix=CHECK-VECTORIZE %s // RUN: %clang -### -S -fno-vectorize %s 2>&1 | FileCheck -check-prefix=CHECK-NO-VECTORIZE %s -- 2.40.0