Also respect -mergeable-traps when merging cond_fails in SILOptimizer

This commit is contained in:
Kuba Mracek
2025-03-24 09:04:44 -07:00
parent a1cbb85b56
commit ed5e89a501
6 changed files with 19 additions and 4 deletions

View File

@@ -39,10 +39,12 @@ private func runMergeCondFails(function: Function, context: FunctionPassContext)
for inst in block.instructions {
if let cfi = inst as? CondFailInst {
let messageIsSame = condFailToMerge.isEmpty || cfi.message == condFailToMerge.first!.message
let forceAllowMerge = context.options.enableMergeableTraps
// Do not process arithmetic overflow checks. We typically generate more
// efficient code with separate jump-on-overflow.
if !hasOverflowConditionOperand(cfi) &&
(condFailToMerge.isEmpty || cfi.message == condFailToMerge.first!.message) {
if !hasOverflowConditionOperand(cfi) && (messageIsSame || forceAllowMerge) {
condFailToMerge.push(cfi)
}
} else if inst.mayHaveSideEffects || inst.mayReadFromMemory {

View File

@@ -40,6 +40,10 @@ struct Options {
_bridged.hasFeature(.Embedded)
}
var enableMergeableTraps: Bool {
_bridged.enableMergeableTraps()
}
func hasFeature(_ feature: BridgedFeature) -> Bool {
_bridged.hasFeature(feature)
}

View File

@@ -335,6 +335,9 @@ public:
/// Temporarily used to bootstrap the AddressableParameters feature.
bool EnableAddressDependencies = true;
// Whether to allow merging traps and cond_fails.
bool MergeableTraps = false;
SILOptions() {}
/// Return a hash code of any components from these options that should

View File

@@ -376,6 +376,7 @@ struct BridgedPassContext {
BRIDGED_INLINE bool useAggressiveReg2MemForCodeSize() const;
BRIDGED_INLINE bool enableStackProtection() const;
BRIDGED_INLINE bool enableMergeableTraps() const;
BRIDGED_INLINE bool hasFeature(BridgedFeature feature) const;
BRIDGED_INLINE bool enableMoveInoutStackProtection() const;
BRIDGED_INLINE AssertConfiguration getAssertConfiguration() const;

View File

@@ -543,6 +543,11 @@ bool BridgedPassContext::enableStackProtection() const {
return mod->getOptions().EnableStackProtection;
}
bool BridgedPassContext::enableMergeableTraps() const {
swift::SILModule *mod = invocation->getPassManager()->getModule();
return mod->getOptions().MergeableTraps;
}
bool BridgedPassContext::hasFeature(BridgedFeature feature) const {
swift::SILModule *mod = invocation->getPassManager()->getModule();
return mod->getASTContext().LangOpts.hasFeature((swift::Feature)feature);

View File

@@ -3109,11 +3109,11 @@ static bool ParseSILArgs(SILOptions &Opts, ArgList &Args,
Opts.ShouldFunctionsBePreservedToDebugger &=
LTOKind.value() == IRGenLLVMLTOKind::None;
Opts.EnableAddressDependencies =
Opts.EnableAddressDependencies =
Args.hasFlag(OPT_enable_address_dependencies,
OPT_disable_address_dependencies,
Opts.EnableAddressDependencies);
Opts.MergeableTraps = Args.hasArg(OPT_mergeable_traps);
return false;
}