Skip to content

Commit 6fd5ff5

Browse files
authored
Merge pull request swiftlang#20545 from gottesmm/pr-189d72ec762043fa9c3df485d13393c182342406
[semantic-sil] Enable the mandatory sil ownership optimization on all…
2 parents 6c68ac2 + e2d7f77 commit 6fd5ff5

File tree

6 files changed

+42
-9
lines changed

6 files changed

+42
-9
lines changed

cmake/modules/SwiftSource.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ function(_compile_swift_files
243243

244244
if(SWIFTFILE_IS_STDLIB)
245245
list(APPEND swift_flags "-Xfrontend" "-enable-sil-ownership")
246+
list(APPEND swift_flags "-Xfrontend" "-enable-mandatory-semantic-arc-opts")
246247
endif()
247248

248249
if(NOT SWIFT_ENABLE_STDLIBCORE_EXCLUSIVITY_CHECKING AND SWIFTFILE_IS_STDLIB)

include/swift/Option/FrontendOptions.td

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,8 +304,8 @@ def enable_sil_ownership : Flag<["-"], "enable-sil-ownership">,
304304
def disable_guaranteed_normal_arguments : Flag<["-"], "disable-guaranteed-normal-arguments">,
305305
HelpText<"If set to true, all normal parameters (except to inits/setters) will be passed at +1">;
306306

307-
def disable_mandatory_semantic_arc_opts : Flag<["-"], "disable-mandatory-semantic-arc-opts">,
308-
HelpText<"Disable the mandatory semantic arc optimizer">;
307+
def enable_mandatory_semantic_arc_opts : Flag<["-"], "enable-mandatory-semantic-arc-opts">,
308+
HelpText<"Enable the mandatory semantic arc optimizer">;
309309

310310
def assume_parsing_unqualified_ownership_sil : Flag<["-"], "assume-parsing-unqualified-ownership-sil">,
311311
HelpText<"Assume unqualified SIL ownership when parsing SIL">;

lib/Frontend/CompilerInvocation.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,7 @@ static bool ParseSILArgs(SILOptions &Opts, ArgList &Args,
743743
Opts.AssumeUnqualifiedOwnershipWhenParsing
744744
|= Args.hasArg(OPT_assume_parsing_unqualified_ownership_sil);
745745
Opts.EnableMandatorySemanticARCOpts |=
746-
!Args.hasArg(OPT_disable_mandatory_semantic_arc_opts);
746+
Args.hasArg(OPT_enable_mandatory_semantic_arc_opts);
747747
Opts.EnableLargeLoadableTypes |= Args.hasArg(OPT_enable_large_loadable_types);
748748

749749
if (const Arg *A = Args.getLastArg(OPT_save_optimization_record_path))

lib/SIL/OwnershipUtils.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,19 +79,35 @@ bool swift::isOwnershipForwardingInst(SILInstruction *i) {
7979

8080
bool swift::getUnderlyingBorrowIntroducers(SILValue inputValue,
8181
SmallVectorImpl<SILValue> &out) {
82+
if (inputValue.getOwnershipKind() != ValueOwnershipKind::Guaranteed)
83+
return false;
84+
8285
SmallVector<SILValue, 32> worklist;
8386
worklist.emplace_back(inputValue);
8487

8588
while (!worklist.empty()) {
8689
SILValue v = worklist.pop_back_val();
8790

8891
// First check if v is an introducer. If so, stash it and continue.
89-
if (isa<SILFunctionArgument>(v) || isa<LoadBorrowInst>(v) ||
92+
if (isa<LoadBorrowInst>(v) ||
9093
isa<BeginBorrowInst>(v)) {
9194
out.push_back(v);
9295
continue;
9396
}
9497

98+
// If we have a function argument with guaranteed convention, it is also an
99+
// introducer.
100+
if (auto *arg = dyn_cast<SILFunctionArgument>(v)) {
101+
if (arg->getOwnershipKind() == ValueOwnershipKind::Guaranteed) {
102+
out.push_back(v);
103+
continue;
104+
}
105+
106+
// Otherwise, we do not know how to handle this function argument, so
107+
// bail.
108+
return false;
109+
}
110+
95111
// Otherwise if v is an ownership forwarding value, add its defining
96112
// instruction
97113
if (isGuaranteedForwardingValue(v)) {

lib/SILOptimizer/Mandatory/SemanticARCOpts.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -180,11 +180,11 @@ namespace {
180180
struct SemanticARCOpts : SILFunctionTransform {
181181
void run() override {
182182
SILFunction &f = *getFunction();
183-
// Do not run the semantic arc opts unless we are running on the
184-
// standard library. This is because this is the only module that
185-
// passes the ownership verifier.
186-
if (!f.getModule().isStdlibModule())
187-
return;
183+
184+
// Make sure we are running with ownership verification enabled.
185+
assert(f.getModule().getOptions().EnableSILOwnership &&
186+
"Can not perform semantic arc optimization unless ownership "
187+
"verification is enabled");
188188

189189
// Iterate over all of the arguments, performing small peephole
190190
// ARC optimizations.

test/SILOptimizer/semantic-arc-opts.sil

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,3 +172,19 @@ bb0(%0 : @guaranteed $Builtin.NativeObject):
172172
%9999 = tuple()
173173
return %9999 : $()
174174
}
175+
176+
// Do not eliminate a copy from an unowned value. This will cause us to pass the
177+
// unowned value as guaranteed... =><=.
178+
//
179+
// CHECK-LABEL: sil @unowned_arg_copy : $@convention(thin) (Builtin.NativeObject) -> () {
180+
// CHECK: copy_value
181+
// CHECK: } // end sil function 'unowned_arg_copy'
182+
sil @unowned_arg_copy : $@convention(thin) (Builtin.NativeObject) -> () {
183+
bb0(%0 : @unowned $Builtin.NativeObject):
184+
%1 = copy_value %0 : $Builtin.NativeObject
185+
%2 = function_ref @guaranteed_user : $@convention(thin) (@guaranteed Builtin.NativeObject) -> ()
186+
apply %2(%1) : $@convention(thin) (@guaranteed Builtin.NativeObject) -> ()
187+
destroy_value %1 : $Builtin.NativeObject
188+
%9999 = tuple()
189+
return %9999 : $()
190+
}

0 commit comments

Comments
 (0)