Skip to content

Commit afac821

Browse files
committed
Also respect -mergeable-traps when merging cond_fails in SILOptimizer
1 parent 40fb23a commit afac821

File tree

6 files changed

+18
-2
lines changed

6 files changed

+18
-2
lines changed

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/MergeCondFails.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,12 @@ private func runMergeCondFails(function: Function, context: FunctionPassContext)
3939

4040
for inst in block.instructions {
4141
if let cfi = inst as? CondFailInst {
42+
let messageIsSame = condFailToMerge.isEmpty || cfi.message == condFailToMerge.first!.message
43+
let forceAllowMerge = context.options.enableMergeableTraps
44+
4245
// Do not process arithmetic overflow checks. We typically generate more
4346
// efficient code with separate jump-on-overflow.
44-
if !hasOverflowConditionOperand(cfi) &&
45-
(condFailToMerge.isEmpty || cfi.message == condFailToMerge.first!.message) {
47+
if !hasOverflowConditionOperand(cfi) && (messageIsSame || forceAllowMerge) {
4648
condFailToMerge.push(cfi)
4749
}
4850
} else if inst.mayHaveSideEffects || inst.mayReadFromMemory {

SwiftCompilerSources/Sources/Optimizer/PassManager/Options.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ struct Options {
4040
_bridged.hasFeature(.Embedded)
4141
}
4242

43+
var enableMergeableTraps: Bool {
44+
_bridged.enableMergeableTraps()
45+
}
46+
4347
func hasFeature(_ feature: BridgedFeature) -> Bool {
4448
_bridged.hasFeature(feature)
4549
}

include/swift/AST/SILOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,9 @@ class SILOptions {
335335
/// Temporarily used to bootstrap the AddressableParameters feature.
336336
bool EnableAddressDependencies = false;
337337

338+
// Whether to allow merging traps and cond_fails.
339+
bool MergeableTraps = false;
340+
338341
SILOptions() {}
339342

340343
/// Return a hash code of any components from these options that should

include/swift/SILOptimizer/OptimizerBridging.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,7 @@ struct BridgedPassContext {
376376

377377
BRIDGED_INLINE bool useAggressiveReg2MemForCodeSize() const;
378378
BRIDGED_INLINE bool enableStackProtection() const;
379+
BRIDGED_INLINE bool enableMergeableTraps() const;
379380
BRIDGED_INLINE bool hasFeature(BridgedFeature feature) const;
380381
BRIDGED_INLINE bool enableMoveInoutStackProtection() const;
381382
BRIDGED_INLINE AssertConfiguration getAssertConfiguration() const;

include/swift/SILOptimizer/OptimizerBridgingImpl.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,11 @@ bool BridgedPassContext::enableStackProtection() const {
543543
return mod->getOptions().EnableStackProtection;
544544
}
545545

546+
bool BridgedPassContext::enableMergeableTraps() const {
547+
swift::SILModule *mod = invocation->getPassManager()->getModule();
548+
return mod->getOptions().MergeableTraps;
549+
}
550+
546551
bool BridgedPassContext::hasFeature(BridgedFeature feature) const {
547552
swift::SILModule *mod = invocation->getPassManager()->getModule();
548553
return mod->getASTContext().LangOpts.hasFeature((swift::Feature)feature);

lib/Frontend/CompilerInvocation.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3108,6 +3108,7 @@ static bool ParseSILArgs(SILOptions &Opts, ArgList &Args,
31083108

31093109

31103110
Opts.EnableAddressDependencies = Args.hasArg(OPT_enable_address_dependencies);
3111+
Opts.MergeableTraps = Args.hasArg(OPT_mergeable_traps);
31113112

31123113
return false;
31133114
}

0 commit comments

Comments
 (0)