Skip to content

IRGen: Fix out-of-order task_dealloc with parameter pack metadata #81516

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 3 commits into from
May 15, 2025
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
6 changes: 6 additions & 0 deletions include/swift/AST/TypeTransform.h
Original file line number Diff line number Diff line change
Expand Up @@ -1176,6 +1176,12 @@ case TypeKind::Id:
if (transformedPack.getPointer() == element->getPackType().getPointer())
return element;

if (!transformedPack->isParameterPack() &&
!transformedPack->is<PackArchetypeType>() &&
!transformedPack->isTypeVariableOrMember()) {
return transformedPack;
}

return PackElementType::get(transformedPack, element->getLevel());
}

Expand Down
2 changes: 1 addition & 1 deletion lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3879,7 +3879,7 @@ PackElementType::PackElementType(Type packType, unsigned level,
packType(packType), level(level) {
assert(packType->isParameterPack() ||
packType->is<PackArchetypeType>() ||
packType->is<TypeVariableType>());
packType->isTypeVariableOrMember());
assert(level > 0);
}

Expand Down
2 changes: 0 additions & 2 deletions lib/AST/ParameterPack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,6 @@ static CanPackType getReducedShapeOfPack(const ASTContext &ctx,
}

// Use () as a placeholder for scalar shape.
assert(!elt->template is<PackArchetypeType>() &&
"Pack archetype outside of a pack expansion");
elts.push_back(ctx.TheEmptyTupleType);
}

Expand Down
99 changes: 63 additions & 36 deletions lib/IRGen/IRGenSIL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1158,12 +1158,10 @@ class IRGenSILFunction :
}

emitTypeMetadataRef(archetype);
} else if (auto packArchetype = dyn_cast<PackArchetypeType>(t)) {
emitTypeMetadataRef(packArchetype);
} else if (auto packtype = dyn_cast<SILPackType>(t)) {
} else if (auto packType = dyn_cast<SILPackType>(t)) {
llvm::Value *Shape = emitPackShapeExpression(t);
emitPackCountDebugVariable(Shape);
} else if (auto packtype = dyn_cast<PackType>(t)) {
} else if (auto packType = dyn_cast<PackType>(t)) {
llvm::Value *Shape = emitPackShapeExpression(t);
emitPackCountDebugVariable(Shape);
}
Expand Down Expand Up @@ -1212,8 +1210,13 @@ class IRGenSILFunction :
SILResultInfo ErrorInfo,
DebugValueInst *DbgValue);
void emitPoisonDebugValueInst(DebugValueInst *i);
void emitDebugInfoForAllocStack(AllocStackInst *i, const TypeInfo &type,
llvm::Value *addr);
void emitDebugInfoBeforeAllocStack(AllocStackInst *i,
const TypeInfo &type,
DebugTypeInfo &DbgTy);
void emitDebugInfoAfterAllocStack(AllocStackInst *i,
const TypeInfo &type,
const DebugTypeInfo &DbgTy,
llvm::Value *addr);
void visitAllocStackInst(AllocStackInst *i);
void visitAllocPackInst(AllocPackInst *i);
void visitAllocPackMetadataInst(AllocPackMetadataInst *i);
Expand Down Expand Up @@ -6410,14 +6413,52 @@ void IRGenSILFunction::visitDestroyNotEscapedClosureInst(
setLoweredExplosion(i, out);
}

void IRGenSILFunction::emitDebugInfoForAllocStack(AllocStackInst *i,
const TypeInfo &type,
llvm::Value *addr) {
void IRGenSILFunction::emitDebugInfoBeforeAllocStack(AllocStackInst *i,
const TypeInfo &type,
DebugTypeInfo &DbgTy) {
auto VarInfo = i->getVarInfo();
if (!VarInfo)
if (!VarInfo ||
!i->getDebugScope() ||
i->getDebugScope()->getInlinedFunction()->isTransparent())
return;

VarDecl *Decl = i->getDecl();

SILType SILTy;
if (auto MaybeSILTy = VarInfo->Type) {
// If there is auxiliary type info, use it
SILTy = *MaybeSILTy;
} else {
SILTy = i->getType();
}
auto RealType = SILTy.getASTType();
if (Decl) {
DbgTy = DebugTypeInfo::getLocalVariable(Decl, RealType, type, IGM);
} else if (i->getFunction()->isBare() && !SILTy.hasArchetype() &&
!VarInfo->Name.empty()) {
DbgTy = DebugTypeInfo::getFromTypeInfo(RealType, getTypeInfo(SILTy), IGM);
} else
return;

bindArchetypes(DbgTy.getType());
}

/// Do not instantiate type metadata in here, since this may allocate on-stack
/// packs which will then be cleaned up in the wrong order with respect to the
/// value stack allocation.
void IRGenSILFunction::emitDebugInfoAfterAllocStack(AllocStackInst *i,
const TypeInfo &type,
const DebugTypeInfo &DbgTy,
llvm::Value *addr) {
auto VarInfo = i->getVarInfo();
if (!VarInfo ||
!i->getDebugScope() ||
i->getDebugScope()->getInlinedFunction()->isTransparent())
return;

VarDecl *Decl = i->getDecl();
auto *DS = i->getDebugScope();

// Describe the underlying alloca. This way an llvm.dbg.declare intrinsic
// is used, which is valid for the entire lifetime of the alloca.
if (auto *BitCast = dyn_cast<llvm::BitCastInst>(addr)) {
Expand All @@ -6434,13 +6475,6 @@ void IRGenSILFunction::emitDebugInfoForAllocStack(AllocStackInst *i,
(void)isTaskAlloc;
}
}

auto DS = i->getDebugScope();
if (!DS)
return;

if (i->getDebugScope()->getInlinedFunction()->isTransparent())
return;

bool IsAnonymous = false;
VarInfo->Name = getVarName(i, IsAnonymous);
Expand Down Expand Up @@ -6475,25 +6509,15 @@ void IRGenSILFunction::emitDebugInfoForAllocStack(AllocStackInst *i,
}
}

SILType SILTy;
if (auto MaybeSILTy = VarInfo->Type) {
// If there is auxiliary type info, use it
SILTy = *MaybeSILTy;
} else {
SILTy = i->getType();
}
auto RealType = SILTy.getASTType();
DebugTypeInfo DbgTy;
if (Decl) {
DbgTy = DebugTypeInfo::getLocalVariable(Decl, RealType, type, IGM);
} else if (i->getFunction()->isBare() && !SILTy.hasArchetype() &&
!VarInfo->Name.empty()) {
DbgTy = DebugTypeInfo::getFromTypeInfo(RealType, getTypeInfo(SILTy), IGM);
} else
return;
if (DbgTy.getType() && IGM.DebugInfo) {
SILType SILTy;
if (auto MaybeSILTy = VarInfo->Type) {
// If there is auxiliary type info, use it
SILTy = *MaybeSILTy;
} else {
SILTy = i->getType();
}

bindArchetypes(DbgTy.getType());
if (IGM.DebugInfo) {
emitDebugVariableDeclaration(
addr, DbgTy, SILTy, DS, i->getLoc(), *VarInfo, Indirection,
AddrDbgInstrKind(i->usesMoveableValueDebugInfo()));
Expand All @@ -6511,6 +6535,9 @@ void IRGenSILFunction::visitAllocStackInst(swift::AllocStackInst *i) {
dbgname = getVarName(i, IsAnonymous);
# endif

DebugTypeInfo DbgTy;
emitDebugInfoBeforeAllocStack(i, type, DbgTy);

auto stackAddr = type.allocateStack(*this, i->getElementType(), dbgname);
setLoweredStackAddress(i, stackAddr);
Address addr = stackAddr.getAddress();
Expand All @@ -6525,7 +6552,7 @@ void IRGenSILFunction::visitAllocStackInst(swift::AllocStackInst *i) {
Ty->getStructOrBoundGenericStruct())
zeroInit(dyn_cast<llvm::AllocaInst>(addr.getAddress()));
}
emitDebugInfoForAllocStack(i, type, addr.getAddress());
emitDebugInfoAfterAllocStack(i, type, DbgTy, addr.getAddress());
}

void IRGenSILFunction::visitAllocPackInst(swift::AllocPackInst *i) {
Expand Down
21 changes: 21 additions & 0 deletions test/Concurrency/Runtime/async_parameter_pack.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// RUN: %target-run-simple-swift( -target %target-swift-5.9-abi-triple)
// REQUIRES: executable_test
// REQUIRES: concurrency

protocol P {
associatedtype A
var a: A { get }
}

func f<each T: P>(_ t: repeat each T) async -> (repeat (each T).A) {
let x = (repeat (each t).a)
return x
}

struct S: P {
var a: String { "" }
}

_ = await f()
_ = await f(S())
_ = await f(S(), S())
30 changes: 30 additions & 0 deletions test/IRGen/pack_metadata_dealloc.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// RUN: %target-swift-frontend -emit-ir %s -target %target-swift-5.9-abi-triple | %FileCheck %s

protocol P {
associatedtype A
var a: A { get }
}

func f<each T: P>(_ t: repeat each T) -> (repeat (each T).A) {
let data = (repeat (each t).a)
return data
}

// CHECK-LABEL: define {{.*}} void @"$s21pack_metadata_dealloc1fy1AQzxQp_txxQpRvzAA1PRzlF"
// CHECK: [[SPSAVE:%.*]] = call ptr @llvm.stacksave.p0()
// CHECK: call void @llvm.stackrestore.p0(ptr [[SPSAVE]])
// CHECK: [[SPSAVE1:%.*]] = call ptr @llvm.stacksave.p0()
// CHECK: [[SPSAVE2:%.*]] = call ptr @llvm.stacksave.p0()
// CHECK-NOT: call ptr llvm.stacksave.p0()
// CHECK: call void @llvm.stackrestore.p0(ptr [[SPSAVE2]])
// CHECK: call void @llvm.stackrestore.p0(ptr [[SPSAVE1]])
// CHECK: ret void

struct G<each T> {
init(_: repeat each T) {}
}

func f2<each T: P, each U>(t: repeat each T, u: repeat each U) async -> (repeat G<(each T).A, repeat each U>) {
let x = (repeat G((each t).a, repeat each u))
return x
}