Skip to content

[sil-parser] Fix harmless bug when parsing ossa. #26851

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion include/swift/AST/DiagnosticsParse.def
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

//===--- DiagnosticsParse.def - Diagnostics Text ----------------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
Expand Down Expand Up @@ -485,6 +484,10 @@ ERROR(referenced_value_no_accessor,none,
"referenced declaration has no %select{getter|setter}0", (unsigned))
ERROR(expected_sil_value_ownership_kind,none,
"expected value ownership kind in SIL code", ())
ERROR(silfunc_and_silarg_have_incompatible_sil_value_ownership,none,
"SILFunction and SILArgument have mismatching ValueOwnershipKinds. "
"Function type specifies: '@%0'. SIL argument specifies: '@%1'.",
(StringRef, StringRef))
ERROR(expected_sil_colon,none,
"expected ':' before %0", (StringRef))
ERROR(expected_sil_tuple_index,none,
Expand Down
2 changes: 2 additions & 0 deletions include/swift/SIL/SILValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@ struct ValueOwnershipKind {
return acc.getValue().merge(x);
});
}

StringRef asString() const;
};

llvm::raw_ostream &operator<<(llvm::raw_ostream &os, ValueOwnershipKind Kind);
Expand Down
15 changes: 14 additions & 1 deletion lib/ParseSIL/ParseSIL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,8 @@ namespace {
bool parseSILOwnership(ValueOwnershipKind &OwnershipKind) {
// We parse here @ <identifier>.
if (!P.consumeIf(tok::at_sign)) {
// If we fail, we must have @any ownership.
// If we fail, we must have @any ownership. We check elsewhere in the
// parser that this matches what the function signature wants.
OwnershipKind = ValueOwnershipKind::Any;
return false;
}
Expand Down Expand Up @@ -5339,6 +5340,18 @@ bool SILParser::parseSILBasicBlock(SILBuilder &B) {
SILArgument *Arg;
if (IsEntry) {
Arg = BB->createFunctionArgument(Ty);
// Today, we construct the ownership kind straight from the function
// type. Make sure they are in sync, otherwise bail. We want this to
// be an exact check rather than a compatibility check since we do not
// want incompatibilities in between @any and other types of ownership
// to be ignored.
if (F->hasOwnership() && Arg->getOwnershipKind() != OwnershipKind) {
auto diagID =
diag::silfunc_and_silarg_have_incompatible_sil_value_ownership;
P.diagnose(NameLoc, diagID, Arg->getOwnershipKind().asString(),
OwnershipKind.asString());
return true;
}
} else {
Arg = BB->createPhiArgument(Ty, OwnershipKind);
}
Expand Down
19 changes: 11 additions & 8 deletions lib/SIL/SILValue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,22 +188,25 @@ ValueOwnershipKind::ValueOwnershipKind(const SILFunction &F, SILType Type,
}
}

llvm::raw_ostream &swift::operator<<(llvm::raw_ostream &os,
ValueOwnershipKind Kind) {
switch (Kind) {
StringRef ValueOwnershipKind::asString() const {
switch (Value) {
case ValueOwnershipKind::Unowned:
return os << "unowned";
return "unowned";
case ValueOwnershipKind::Owned:
return os << "owned";
return "owned";
case ValueOwnershipKind::Guaranteed:
return os << "guaranteed";
return "guaranteed";
case ValueOwnershipKind::Any:
return os << "any";
return "any";
}

llvm_unreachable("Unhandled ValueOwnershipKind in switch.");
}

llvm::raw_ostream &swift::operator<<(llvm::raw_ostream &os,
ValueOwnershipKind kind) {
return os << kind.asString();
}

Optional<ValueOwnershipKind>
ValueOwnershipKind::merge(ValueOwnershipKind RHS) const {
auto LHSVal = Value;
Expand Down
2 changes: 1 addition & 1 deletion test/SIL/Parser/apply_with_conformance.sil
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ sil @_TFV4test1S3foofS0_US_1P__FQ_T_ : $@convention(method) <T where T : P> (@in
// CHECK: call
// test.bar (test.S, test.X) -> ()
sil [ossa] @_TF4test3barFTVS_1SVS_1X_T_ : $@convention(thin) (S, X) -> () {
bb0(%0 : @unowned $S, %1 : @unowned $X):
bb0(%0 : $S, %1 : $X):
debug_value %0 : $S // let s // id: %2
debug_value %1 : $X // let x // id: %3
// function_ref test.S.foo (test.S)<A : test.P>(A) -> ()
Expand Down
19 changes: 19 additions & 0 deletions test/SIL/Parser/ossa_needs_ownership_on_args.sil
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// RUN: not %sil-opt -verify %s

// Make sure that we error if ossa functions do not parse unless the argument
// has the proper ownership specifier on it.

import Builtin

sil [ossa] @test1 : $@convention(thin) (@owned Builtin.NativeObject) -> () {
bb0(%0 : $Builtin.NativeObject): // {{expected-error: SILFunction and SILArgument have mismatching ValueOwnershipKinds. Function type specifies: '@owned'. SIL argument specifies: '@any'.}}
destroy_value %0 : $Builtin.NativeObject
%9999 = tuple()
return %9999 : $()
}

sil [ossa] @test2 : $@convention(thin) (Builtin.Int32) -> () {
bb0(%0 : @owned $Builtin.Int32): // {{expected-error: SILFunction and SILArgument have mismatching ValueOwnershipKinds. Function type specifies: '@any'. SIL argument specifies: '@owned'.}}
%9999 = tuple()
return %9999 : $()
}
2 changes: 1 addition & 1 deletion test/SIL/Parser/overloaded_member.sil
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class B {
}

sil [ossa] @test_overloaded_subscript : $@convention(thin) (@guaranteed B, B.Index) -> () {
bb0(%0 : $B, %1 : $B.Index):
bb0(%0 : @guaranteed $B, %1 : $B.Index):
%reader = class_method %0 : $B, #B.subscript!read.1 : (B) -> (B.Index) -> (), $@convention(method) @yield_once (B.Index, @guaranteed B) -> @yields @guaranteed B.Element
(%element, %token) = begin_apply %reader(%1, %0) : $@convention(method) @yield_once (B.Index, @guaranteed B) -> @yields @guaranteed B.Element
end_apply %token
Expand Down
2 changes: 1 addition & 1 deletion test/SIL/Parser/unmanaged.sil
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ bb0(%0 : $*Optional<U>):
}

sil [ossa] @retain_release : $@convention(thin) (@sil_unmanaged Optional<C>) -> () {
bb0(%0 : @unowned $@sil_unmanaged Optional<C>):
bb0(%0 : $@sil_unmanaged Optional<C>):
%1 = unmanaged_to_ref %0 : $@sil_unmanaged Optional<C> to $Optional<C>
unmanaged_retain_value %1 : $Optional<C>
unmanaged_autorelease_value %1 : $Optional<C>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@

struct X {}
sil_stage raw

import Builtin

struct X {
let x: Builtin.NativeObject
}

// Make sure that we can deserialize an apply with various parameter calling
// conventions.
sil [serialized] [ossa] @foo : $@convention(thin) (@in X, @inout X, @in_guaranteed X, @owned X, X, @guaranteed X) -> @out X {
bb0(%0 : $*X, %1 : $*X, %2 : $*X, %3 : $*X, %4 : @owned $X, %5 : @unowned $X, %6 : @guaranteed $X):
copy_addr [take] %1 to [initialization] %0 : $*X
destroy_value %4 : $X
%9999 = tuple()
return %9999 : $()
}
2 changes: 1 addition & 1 deletion test/SIL/Serialization/Recovery/function.sil
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import Types
// CHECK-LABEL: sil @missingParam : $@convention(thin) (SoonToBeMissing) -> () {
// CHECK-RECOVERY-NEGATIVE-NOT: sil [ossa] @missingParam
sil [ossa] @missingParam : $@convention(thin) (SoonToBeMissing) -> () {
entry(%arg : @unowned $SoonToBeMissing):
entry(%arg : $SoonToBeMissing):
%9999 = tuple()
return %9999 : $()
}
Expand Down
2 changes: 1 addition & 1 deletion test/SIL/Serialization/unmanaged.sil
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class C {}
// CHECK: unmanaged_autorelease_value [[REF]]
// CHECK: unmanaged_release_value [[REF]]
sil [serialized] [ossa] @retain_release : $@convention(thin) (@sil_unmanaged Optional<C>) -> () {
bb0(%0 : @unowned $@sil_unmanaged Optional<C>):
bb0(%0 : $@sil_unmanaged Optional<C>):
%1 = unmanaged_to_ref %0 : $@sil_unmanaged Optional<C> to $Optional<C>
unmanaged_retain_value %1 : $Optional<C>
unmanaged_autorelease_value %1 : $Optional<C>
Expand Down
4 changes: 2 additions & 2 deletions test/SIL/memory_lifetime.sil
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ bb6:
}

sil [ossa] @test_struct2 : $@convention(thin) (@in Outer, @owned T) -> () {
bb0(%0 : $*Outer, %1 : $T):
bb0(%0 : $*Outer, %1 : @owned $T):
%2 = struct_element_addr %0 : $*Outer, #Outer.x
store %1 to [assign] %2 : $*T
destroy_addr %0 : $*Outer
Expand Down Expand Up @@ -122,7 +122,7 @@ bb3:
}

sil [ossa] @test_unreachable_block : $@convention(thin) (@owned T) -> () {
bb0(%0 : $T):
bb0(%0 : @owned $T):
%2 = alloc_stack $T
store %0 to [init] %2 : $*T
br bb2
Expand Down
2 changes: 1 addition & 1 deletion test/SIL/memory_lifetime_failures.sil
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ bb2(%4 : @owned $Error):

// CHECK: SIL memory lifetime failure in @test_single_block: memory is initialized, but shouldn't
sil [ossa] @test_single_block : $@convention(thin) (@owned T) -> () {
bb0(%0 : $T):
bb0(%0 : @owned $T):
%2 = alloc_stack $T
store %0 to [init] %2 : $*T
dealloc_stack %2 : $*T
Expand Down
4 changes: 2 additions & 2 deletions test/SILOptimizer/allocbox_to_stack_ownership.sil
Original file line number Diff line number Diff line change
Expand Up @@ -1037,7 +1037,7 @@ bb3:
// CHECK: return %{{.*}} : $()
// CHECK-LABEL: } // end sil function 'deinit_access'
sil [ossa] @deinit_access : $@convention(thin) <T> (@in T) -> (@out T, @out T) {
bb0(%0 : @owned $*T, %1 : @owned $*T, %2 : @owned $*T):
bb0(%0 : $*T, %1 : $*T, %2 : $*T):
%4 = alloc_box $<τ_0_0> { var τ_0_0 } <T>, var, name "x"
%5 = project_box %4 : $<τ_0_0> { var τ_0_0 } <T>, 0
copy_addr %2 to [initialization] %5 : $*T
Expand Down Expand Up @@ -1076,7 +1076,7 @@ bb0(%0 : @owned $*T, %1 : @owned $*T, %2 : @owned $*T):
// CHECK: return %{{.*}} : $()
// CHECK-LABEL: } // end sil function 'nodeinit_access'
sil [ossa] @nodeinit_access : $@convention(thin) <T> (@in T) -> (@out T, @out T) {
bb0(%0 : @owned $*T, %1 : @owned $*T, %2 : @owned $*T):
bb0(%0 : $*T, %1 : $*T, %2 : $*T):
%4 = alloc_box $<τ_0_0> { var τ_0_0 } <T>, var, name "x"
%5 = project_box %4 : $<τ_0_0> { var τ_0_0 } <T>, 0
copy_addr %2 to [initialization] %5 : $*T
Expand Down
2 changes: 1 addition & 1 deletion test/SILOptimizer/devirtualize_ownership.sil
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ bb0(%0 : @guaranteed $Node):

// CHECK-LABEL: sil private [transparent] [noinline] [ossa] @transparent_target
sil private [transparent] [noinline] [ossa] @transparent_target : $@convention(method) (@guaranteed Node) -> Int {
bb0(%0 : @owned $Node):
bb0(%0 : @guaranteed $Node):
%2 = ref_element_addr %0 : $Node, #Node.index
%3 = load [trivial] %2 : $*Int
// CHECK: return
Expand Down
2 changes: 1 addition & 1 deletion test/SILOptimizer/mandatory_inlining_ownership2.sil
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ bb0(%0 : @owned $Builtin.NativeObject):
// CHECK-NEXT: return
// CHECK: } // end sil function 'test_recursive_nativeobject_baz'
sil [transparent] [ossa] @test_recursive_nativeobject_baz : $@convention(thin) (@owned Builtin.NativeObject) -> @owned Builtin.NativeObject {
bb0(%0 : $Builtin.NativeObject):
bb0(%0 : @owned $Builtin.NativeObject):
return %0 : $Builtin.NativeObject
}

Expand Down
4 changes: 2 additions & 2 deletions test/SILOptimizer/opaque_values_mandatory.sil
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ typealias Int = Builtin.Int64
// CHECK: return %0 : $T
// CHECK-LABEL: } // end sil function 'f010_diagnose_identity'
sil hidden [ossa] @f010_diagnose_identity : $@convention(thin) <T> (@in T) -> @out T {
bb0(%0 : $T):
bb0(%0 : @owned $T):
%c = copy_value %0 : $T
destroy_value %0 : $T
return %c : $T
Expand All @@ -31,7 +31,7 @@ bb0(%0 : $T):
// CHECK: return %{{.*}} : $()
// CHECK-LABEL: } // end sil function 'f020_assign_inout'
sil hidden [ossa] @f020_assign_inout : $@convention(thin) <T> (@inout T, @in T) -> () {
bb0(%0 : $*T, %1 : $T):
bb0(%0 : $*T, %1 : @owned $T):
%2 = copy_value %1 : $T
assign %2 to %0 : $*T
destroy_value %1 : $T
Expand Down
2 changes: 1 addition & 1 deletion test/SILOptimizer/silgen_cleanup.sil
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ struct X3 {
// CHECK: end_access [[ACCESS]] : $*X3
// CHECK-LABEL: } // end sil function 'testStoreNontrivial'
sil private [ossa] @testStoreNontrivial : $@convention(thin) (@inout X3, @guaranteed AnyObject) -> () {
bb0(%0 : $*X3, %1 : $AnyObject):
bb0(%0 : $*X3, %1 : @guaranteed $AnyObject):
%4 = copy_value %1 : $AnyObject
%5 = begin_access [modify] [unknown] %0 : $*X3
%6 = struct_element_addr %5 : $*X3, #X3.x2
Expand Down