Skip to content

Commit

Permalink
Merge pull request #12658 from rajbarik/master
Browse files Browse the repository at this point in the history
Improve Osize
  • Loading branch information
aschwaighofer committed Oct 30, 2017
2 parents 0355fdd + cdeb346 commit 56a1663
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 4 deletions.
6 changes: 6 additions & 0 deletions include/swift/AST/SILOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ class SILOptions {
/// Controls the aggressiveness of the performance inliner.
int InlineThreshold = -1;

/// Controls the aggressiveness of the performance inliner for Osize.
int CallerBaseBenefitReductionFactor = 2;

/// Controls the aggressiveness of the loop unroller.
int UnrollThreshold = 250;

/// The number of threads for multi-threaded code generation.
int NumThreads = 0;

Expand Down
10 changes: 10 additions & 0 deletions include/swift/Option/FrontendOptions.td
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,16 @@ def sil_inline_threshold : Separate<["-"], "sil-inline-threshold">,
MetaVarName<"<50>">,
HelpText<"Controls the aggressiveness of performance inlining">;

def sil_inline_caller_benefit_reduction_factor : Separate<["-"], "sil-inline-caller-benefit-reduction-factor">,
MetaVarName<"<2>">,
HelpText<"Controls the aggressiveness of performance inlining in -Osize "
"mode by reducing the base benefits of a caller (lower value "
"permits more inlining!)">;

def sil_unroll_threshold : Separate<["-"], "sil-unroll-threshold">,
MetaVarName<"<250>">,
HelpText<"Controls the aggressiveness of loop unrolling">;

def sil_merge_partial_modules : Flag<["-"], "sil-merge-partial-modules">,
HelpText<"Merge SIL from all partial swiftmodules into the final module">;

Expand Down
14 changes: 14 additions & 0 deletions lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1188,6 +1188,20 @@ static bool ParseSILArgs(SILOptions &Opts, ArgList &Args,
return true;
}
}
if (const Arg *A = Args.getLastArg(OPT_sil_inline_caller_benefit_reduction_factor)) {
if (StringRef(A->getValue()).getAsInteger(10, Opts.CallerBaseBenefitReductionFactor)) {
Diags.diagnose(SourceLoc(), diag::error_invalid_arg_value,
A->getAsString(Args), A->getValue());
return true;
}
}
if (const Arg *A = Args.getLastArg(OPT_sil_unroll_threshold)) {
if (StringRef(A->getValue()).getAsInteger(10, Opts.UnrollThreshold)) {
Diags.diagnose(SourceLoc(), diag::error_invalid_arg_value,
A->getAsString(Args), A->getValue());
return true;
}
}
if (const Arg *A = Args.getLastArg(OPT_num_threads)) {
if (StringRef(A->getValue()).getAsInteger(10, Opts.NumThreads)) {
Diags.diagnose(SourceLoc(), diag::error_invalid_arg_value,
Expand Down
2 changes: 1 addition & 1 deletion lib/IRGen/IRGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -845,7 +845,7 @@ void IRGenModule::constructInitialFnAttributes(llvm::AttrBuilder &Attrs) {
Attrs.addAttribute("target-features", allFeatures);
}
if (IRGen.Opts.OptimizeForSize)
Attrs.addAttribute(llvm::Attribute::OptimizeForSize);
Attrs.addAttribute(llvm::Attribute::MinSize);
}

llvm::AttributeList IRGenModule::constructInitialAttributes() {
Expand Down
4 changes: 3 additions & 1 deletion lib/SILOptimizer/LoopTransforms/LoopUnroll.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ using namespace swift::PatternMatch;
using llvm::DenseMap;
using llvm::MapVector;

static const uint64_t SILLoopUnrollThreshold = 250;

namespace {

Expand Down Expand Up @@ -187,6 +186,9 @@ static bool canAndShouldUnrollLoop(SILLoop *Loop, uint64_t TripCount) {
// It is used to estimate the cost of the callee
// inside a loop.
const uint64_t InsnsPerBB = 4;
// Use command-line threshold for unrolling.
const uint64_t SILLoopUnrollThreshold = Loop->getBlocks().empty() ? 0 :
(Loop->getBlocks())[0]->getParent()->getModule().getOptions().UnrollThreshold;
for (auto *BB : Loop->getBlocks()) {
for (auto &Inst : *BB) {
if (!Loop->canDuplicate(&Inst))
Expand Down
4 changes: 3 additions & 1 deletion lib/SILOptimizer/Transforms/PerformanceInliner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,9 @@ bool SILPerformanceInliner::isProfitableToInline(
return false;
}

BaseBenefit = BaseBenefit / 2;
// Use command line option to control inlining in Osize mode.
const uint64_t CallerBaseBenefitReductionFactor = AI.getFunction()->getModule().getOptions().CallerBaseBenefitReductionFactor;
BaseBenefit = BaseBenefit / CallerBaseBenefitReductionFactor;
}

// It is always OK to inline a simple call.
Expand Down
2 changes: 1 addition & 1 deletion test/IRGen/optimize_for_size.sil
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ bb0(%0 : $Builtin.Int32):
}

// O-NOT: attributes #0 = {{{.*}}optsize
// CHECK: attributes #0 = {{{.*}}optsize
// CHECK: attributes #0 = {{{.*}}minsize

0 comments on commit 56a1663

Please sign in to comment.