Skip to content

[6.0] IRGen: don't stack promote classes for which the layout has not a fixed size #74982

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
merged 1 commit into from
Jul 8, 2024
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
2 changes: 2 additions & 0 deletions lib/IRGen/GenClass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,8 @@ static llvm::Value *stackPromote(IRGenFunction &IGF,
return nullptr;
if (!FieldLayout.isFixedLayout())
return nullptr;
if (!FieldLayout.isFixedSize())
return nullptr;

// Calculate the total size needed.
// The first part is the size of the class itself.
Expand Down
53 changes: 53 additions & 0 deletions test/SILOptimizer/stack-promotion-crash.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// RUN: %empty-directory(%t)
// RUN: split-file %s %t
// RUN: %target-build-swift -O -wmo -parse-as-library -emit-module -emit-module-path=%t/XMod.swiftmodule -module-name=XMod %t/xmod.swift -I%t -c -o %t/xmod.o
// RUN: %target-build-swift -O -wmo -module-name=Main -I%t %t/main.swift -c -o %t/main.o
// RUN: %target-swiftc_driver %t/main.o %t/xmod.o -o %t/a.out
// RUN: %target-codesign %t/a.out
// RUN: %target-run %t/a.out | %FileCheck %s

//--- module.modulemap

module CModule {
header "c-header.h"
export *
}


//--- c-header.h

struct CS {
int x;
};

//--- xmod.swift

@_implementationOnly import CModule

// The layout of this class does not include the C-imported CS field.
// Therefore it must not be stack-promoted in `testit`. Otherwise the reserved stack space
// would be wrong and the executable would crash.
final public class X {
public var i: Int
var cs: CS? = nil

public init(_ i: Int) { self.i = i }
}

//--- main.swift

import XMod

@inline(never)
func getit(_ x: X, _ y: X) -> Int {
return x.i + y.i
}

@inline(never)
public func testit() -> Int {
return getit(X(27), X(11))
}

// CHECK: 38
print(testit())