Skip to content

[embedded] Fix IR verification crash when using arrays of zero-sized structs #69010

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
Oct 9, 2023
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
13 changes: 13 additions & 0 deletions lib/IRGen/GenBuiltin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1115,6 +1115,10 @@ void irgen::emitBuiltinCall(IRGenFunction &IGF, const BuiltinInfo &Builtin,
SILType elemTy = valueTy.first;
const TypeInfo &elemTI = valueTy.second;

if (elemTI.isTriviallyDestroyable(ResilienceExpansion::Maximal) ==
IsTriviallyDestroyable)
return;

llvm::Value *firstElem = IGF.Builder.CreateBitCast(
ptr, elemTI.getStorageType()->getPointerTo());

Expand Down Expand Up @@ -1175,6 +1179,15 @@ void irgen::emitBuiltinCall(IRGenFunction &IGF, const BuiltinInfo &Builtin,
SILType elemTy = tyPair.first;
const TypeInfo &elemTI = tyPair.second;

// Do nothing for zero-sized POD array elements.
if (llvm::Constant *SizeConst = elemTI.getStaticSize(IGF.IGM)) {
auto *SizeInt = cast<llvm::ConstantInt>(SizeConst);
if (SizeInt->getSExtValue() == 0 &&
elemTI.isTriviallyDestroyable(ResilienceExpansion::Maximal) ==
IsTriviallyDestroyable)
return;
}

llvm::Value *firstSrcElem = IGF.Builder.CreateBitCast(
src, elemTI.getStorageType()->getPointerTo());
llvm::Value *firstDestElem = IGF.Builder.CreateBitCast(
Expand Down
21 changes: 21 additions & 0 deletions test/embedded/array-zero-size-struct.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// RUN: %target-swift-frontend -emit-ir %s -enable-experimental-feature Embedded | %FileCheck %s

// REQUIRES: VENDOR=apple
// REQUIRES: OS=macosx

public struct MyStruct {
}

public func main() {
var arr: [MyStruct] = []
var arr2: [MyStruct] = [.init()]
var arr3 = arr2
arr3.append(MyStruct())
}

public func copy(_ a: inout [MyStruct]) {
var a = a
}

// CHECK: define {{.*}}@"$s4mainAAyyF"
// CHECK: define {{.*}}@"$s4main4copyyySayAA8MyStructVGzF"