Skip to content

Fix LowerAggregateInstrs to avoid lowering move-only types #66142

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

Closed
wants to merge 1 commit into from
Closed
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
9 changes: 9 additions & 0 deletions lib/SILOptimizer/Transforms/SILLowerAggregateInstrs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,21 @@ static llvm::cl::opt<bool> EnableExpandAll("sil-lower-agg-instrs-expand-all",
// Utility
//===----------------------------------------------------------------------===//

/// Expansion of struct-with-deinit is illegal because it drops the deinit. For
/// now, simply avoid all move-only AST types.
static bool canExpandType(SILType type) {
return !type.isPureMoveOnly();
}

/// We only expand if we are not in ownership and shouldExpand is true. The
/// reason why is that this was originally done to help the low level ARC
/// optimizer. To the high level ARC optimizer, this is just noise and
/// unnecessary IR. At the same time for testing purposes, we want to provide a
/// way even with ownership enabled to expand so we can check correctness.
static bool shouldExpandShim(SILFunction *fn, SILType type) {
if (!canExpandType(type))
return false;

return EnableExpandAll ||
(!fn->hasOwnership() && shouldExpand(fn->getModule(), type));
}
Expand Down
32 changes: 32 additions & 0 deletions test/SILOptimizer/loweraggregateinstrs_moveonly.sil
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// RUN: %target-sil-opt -enable-sil-verify-all -lower-aggregate-instrs %s | %FileCheck %s

sil_stage canonical

import Builtin

struct S : ~Copyable {
deinit {}
}
struct S2 : ~Copyable {
var s1 = S()
var s2 = S()
}

// Test that a struct-with-deinit is not expanded. Doing so would
// forget the deinit.
//
// public func testDeinitTwo() {
// var s = S2()
// }
//
// CHECK-LABEL: sil @testDeinitTwo : $@convention(thin) () -> () {
// CHECK: release_value %{{.*}} : $S2
// CHECK-LABEL: } // end sil function 'testDeinitTwo'
sil @testDeinitTwo : $@convention(thin) () -> () {
bb0:
%0 = struct $S ()
%1 = struct $S2 (%0 : $S, %0 : $S)
release_value %1 : $S2
%5 = tuple ()
return %5 : $()
}