Skip to content

Commit 3dbeeba

Browse files
committed
[SIL] Add var_decl flag to alloc_stack.
1 parent 37d8984 commit 3dbeeba

File tree

16 files changed

+100
-35
lines changed

16 files changed

+100
-35
lines changed

docs/SIL.rst

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3786,6 +3786,7 @@ alloc_stack
37863786
sil-instruction ::= 'alloc_stack' alloc-stack-option* sil-type (',' debug-var-attr)*
37873787
alloc-stack-option ::= '[dynamic_lifetime]'
37883788
alloc-stack-option ::= '[lexical]'
3789+
alloc-stack-option ::= '[var_decl]'
37893790
alloc-stack-option ::= '[moveable_value_debuginfo]'
37903791

37913792
%1 = alloc_stack $T
@@ -3805,7 +3806,12 @@ The ``dynamic_lifetime`` attribute specifies that the initialization and
38053806
destruction of the stored value cannot be verified at compile time.
38063807
This is the case, e.g. for conditionally initialized objects.
38073808

3808-
The optional ``lexical`` attribute specifies that the storage corresponds to a
3809+
The optional ``lexical`` attribute specifies that the operand corresponds to a
3810+
local variable with a lexical lifetime in the Swift source, so special care
3811+
must be taken when hoisting ``destroy_addr``s. Compare to the ``var_decl``
3812+
attribute.
3813+
3814+
The optional ``var_decl`` attribute specifies that the storage corresponds to a
38093815
local variable in the Swift source.
38103816

38113817
The optional ``moveable_value_debuginfo`` attribute specifies that when emitting

include/swift/SIL/SILBridgingImpl.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1465,7 +1465,8 @@ BridgedInstruction BridgedBuilder::createAllocStack(BridgedType type,
14651465
regularLoc(), type.unbridged(), std::nullopt,
14661466
swift::HasDynamicLifetime_t(hasDynamicLifetime),
14671467
swift::IsLexical_t(isLexical),
1468-
swift::UsesMoveableValueDebugInfo_t(wasMoved))};
1468+
// TODO: Add this as an argument.
1469+
swift::IsNotFromVarDecl, swift::UsesMoveableValueDebugInfo_t(wasMoved))};
14691470
}
14701471

14711472
BridgedInstruction BridgedBuilder::createAllocVector(BridgedValue capacity, BridgedType type) const {

include/swift/SIL/SILBuilder.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,7 @@ class SILBuilder {
407407
std::optional<SILDebugVariable> Var = std::nullopt,
408408
HasDynamicLifetime_t dynamic = DoesNotHaveDynamicLifetime,
409409
IsLexical_t isLexical = IsNotLexical,
410+
IsFromVarDecl_t isFromVarDecl = IsNotFromVarDecl,
410411
UsesMoveableValueDebugInfo_t wasMoved = DoesNotUseMoveableValueDebugInfo,
411412
bool skipVarDeclAssert = false) {
412413
llvm::SmallString<4> Name;
@@ -420,7 +421,8 @@ class SILBuilder {
420421
#endif
421422
return insert(AllocStackInst::create(
422423
getSILDebugLocation(Loc, true), elementType, getFunction(),
423-
substituteAnonymousArgs(Name, Var, Loc), dynamic, isLexical, wasMoved));
424+
substituteAnonymousArgs(Name, Var, Loc), dynamic, isLexical,
425+
isFromVarDecl, wasMoved));
424426
}
425427

426428
AllocVectorInst *

include/swift/SIL/SILCloner.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -877,7 +877,7 @@ SILCloner<ImplClass>::visitAllocStackInst(AllocStackInst *Inst) {
877877
}
878878
auto *NewInst = getBuilder().createAllocStack(
879879
Loc, getOpType(Inst->getElementType()), VarInfo,
880-
Inst->hasDynamicLifetime(), Inst->isLexical(),
880+
Inst->hasDynamicLifetime(), Inst->isLexical(), Inst->isFromVarDecl(),
881881
Inst->usesMoveableValueDebugInfo()
882882
#ifndef NDEBUG
883883
,

include/swift/SIL/SILInstruction.h

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2008,14 +2008,14 @@ class AllocStackInst final
20082008
ArrayRef<SILValue> TypeDependentOperands, SILFunction &F,
20092009
std::optional<SILDebugVariable> Var,
20102010
HasDynamicLifetime_t hasDynamicLifetime, IsLexical_t isLexical,
2011+
IsFromVarDecl_t isFromVarDecl,
20112012
UsesMoveableValueDebugInfo_t usesMoveableValueDebugInfo);
20122013

2013-
static AllocStackInst *create(SILDebugLocation Loc, SILType elementType,
2014-
SILFunction &F,
2015-
std::optional<SILDebugVariable> Var,
2016-
HasDynamicLifetime_t hasDynamicLifetime,
2017-
IsLexical_t isLexical,
2018-
UsesMoveableValueDebugInfo_t wasMoved);
2014+
static AllocStackInst *
2015+
create(SILDebugLocation Loc, SILType elementType, SILFunction &F,
2016+
std::optional<SILDebugVariable> Var,
2017+
HasDynamicLifetime_t hasDynamicLifetime, IsLexical_t isLexical,
2018+
IsFromVarDecl_t isFromVarDecl, UsesMoveableValueDebugInfo_t wasMoved);
20192019

20202020
SIL_DEBUG_VAR_SUPPLEMENT_TRAILING_OBJS_IMPL()
20212021

@@ -2062,7 +2062,7 @@ class AllocStackInst final
20622062
return HasDynamicLifetime_t(sharedUInt8().AllocStackInst.dynamicLifetime);
20632063
}
20642064

2065-
/// Whether the alloc_stack instruction corresponds to a source-level VarDecl.
2065+
/// Whether the alloc_stack instruction has a lexical lifetime.
20662066
IsLexical_t isLexical() const {
20672067
return IsLexical_t(sharedUInt8().AllocStackInst.lexical);
20682068
}
@@ -2079,6 +2079,15 @@ class AllocStackInst final
20792079
sharedUInt8().AllocStackInst.lexical = (bool)IsLexical;
20802080
}
20812081

2082+
/// Whether the alloc_stack instruction corresponds to a source-level VarDecl.
2083+
IsFromVarDecl_t isFromVarDecl() const {
2084+
return IsFromVarDecl_t(sharedUInt8().AllocStackInst.fromVarDecl);
2085+
}
2086+
2087+
/// Set that the alloc_stack instruction corresponds to a source-level
2088+
/// VarDecl.
2089+
void setIsFromVarDecl() { sharedUInt8().AllocStackInst.fromVarDecl = true; }
2090+
20822091
/// Return the debug variable information attached to this instruction.
20832092
std::optional<SILDebugVariable> getVarInfo() const {
20842093
// If we used to have debug info attached but our debug info is now

include/swift/SIL/SILNode.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ class alignas(8) SILNode :
225225
SHARED_FIELD(AllocStackInst, uint8_t
226226
dynamicLifetime : 1,
227227
lexical : 1,
228+
fromVarDecl : 1,
228229
usesMoveableValueDebugInfo : 1,
229230
hasInvalidatedVarInfo : 1);
230231

lib/SIL/IR/SILInstructions.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ AllocStackInst::AllocStackInst(
227227
ArrayRef<SILValue> TypeDependentOperands, SILFunction &F,
228228
std::optional<SILDebugVariable> Var,
229229
HasDynamicLifetime_t hasDynamicLifetime, IsLexical_t isLexical,
230+
IsFromVarDecl_t isFromVarDecl,
230231
UsesMoveableValueDebugInfo_t usesMoveableValueDebugInfo)
231232
: InstructionBase(Loc, elementType.getAddressType()),
232233
SILDebugVariableSupplement(Var ? Var->DIExpr.getNumElements() : 0,
@@ -238,6 +239,7 @@ AllocStackInst::AllocStackInst(
238239
VarInfo(0) {
239240
sharedUInt8().AllocStackInst.dynamicLifetime = (bool)hasDynamicLifetime;
240241
sharedUInt8().AllocStackInst.lexical = (bool)isLexical;
242+
sharedUInt8().AllocStackInst.fromVarDecl = (bool)isFromVarDecl;
241243
sharedUInt8().AllocStackInst.usesMoveableValueDebugInfo =
242244
(bool)usesMoveableValueDebugInfo || elementType.isMoveOnly();
243245
sharedUInt32().AllocStackInst.numOperands = TypeDependentOperands.size();
@@ -267,6 +269,7 @@ AllocStackInst *AllocStackInst::create(SILDebugLocation Loc,
267269
std::optional<SILDebugVariable> Var,
268270
HasDynamicLifetime_t hasDynamicLifetime,
269271
IsLexical_t isLexical,
272+
IsFromVarDecl_t isFromVarDecl,
270273
UsesMoveableValueDebugInfo_t wasMoved) {
271274
SmallVector<SILValue, 8> TypeDependentOperands;
272275
collectTypeDependentOperands(TypeDependentOperands, F,
@@ -275,7 +278,7 @@ AllocStackInst *AllocStackInst::create(SILDebugLocation Loc,
275278
F.getModule(), Var, TypeDependentOperands);
276279
return ::new (Buffer)
277280
AllocStackInst(Loc, elementType, TypeDependentOperands, F, Var,
278-
hasDynamicLifetime, isLexical, wasMoved);
281+
hasDynamicLifetime, isLexical, isFromVarDecl, wasMoved);
279282
}
280283

281284
VarDecl *AllocationInst::getDecl() const {

lib/SIL/IR/SILPrinter.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1440,6 +1440,8 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
14401440
*this << "[dynamic_lifetime] ";
14411441
if (AVI->isLexical())
14421442
*this << "[lexical] ";
1443+
if (AVI->isFromVarDecl())
1444+
*this << "[var_decl] ";
14431445
if (AVI->usesMoveableValueDebugInfo() && !AVI->getType().isMoveOnly())
14441446
*this << "[moveable_value_debuginfo] ";
14451447
*this << AVI->getElementType();

lib/SIL/Parser/ParseSIL.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4742,6 +4742,7 @@ bool SILParser::parseSpecificSILInstruction(SILBuilder &B,
47424742
case SILInstructionKind::AllocStackInst: {
47434743
auto hasDynamicLifetime = DoesNotHaveDynamicLifetime;
47444744
auto isLexical = IsNotLexical;
4745+
auto isFromVarDecl = IsNotFromVarDecl;
47454746
UsesMoveableValueDebugInfo_t usesMoveableValueDebugInfo =
47464747
DoesNotUseMoveableValueDebugInfo;
47474748

@@ -4752,6 +4753,8 @@ bool SILParser::parseSpecificSILInstruction(SILBuilder &B,
47524753
hasDynamicLifetime = HasDynamicLifetime;
47534754
else if (attributeName == "lexical")
47544755
isLexical = IsLexical;
4756+
else if (attributeName == "var_decl")
4757+
isFromVarDecl = IsFromVarDecl;
47554758
else if (attributeName == "moveable_value_debuginfo")
47564759
usesMoveableValueDebugInfo = UsesMoveableValueDebugInfo;
47574760
else {
@@ -4775,10 +4778,12 @@ bool SILParser::parseSpecificSILInstruction(SILBuilder &B,
47754778
// It doesn't make sense to attach a debug var info if the name is empty
47764779
if (VarInfo.Name.size())
47774780
ResultVal = B.createAllocStack(InstLoc, Ty, VarInfo, hasDynamicLifetime,
4778-
isLexical, usesMoveableValueDebugInfo);
4781+
isLexical, isFromVarDecl,
4782+
usesMoveableValueDebugInfo);
47794783
else
4780-
ResultVal = B.createAllocStack(InstLoc, Ty, {}, hasDynamicLifetime,
4781-
isLexical, usesMoveableValueDebugInfo);
4784+
ResultVal =
4785+
B.createAllocStack(InstLoc, Ty, {}, hasDynamicLifetime, isLexical,
4786+
isFromVarDecl, usesMoveableValueDebugInfo);
47824787
break;
47834788
}
47844789
case SILInstructionKind::AllocVectorInst: {

lib/SILOptimizer/Transforms/AllocBoxToStack.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -649,12 +649,12 @@ static bool rewriteAllocBoxAsAllocStack(AllocBoxInst *ABI) {
649649
}
650650
return IsNotLexical;
651651
};
652-
auto *ASI = Builder.createAllocStack(ABI->getLoc(), ty, ABI->getVarInfo(),
653-
ABI->hasDynamicLifetime(), isLexical(),
654-
DoesNotUseMoveableValueDebugInfo
652+
auto *ASI = Builder.createAllocStack(
653+
ABI->getLoc(), ty, ABI->getVarInfo(), ABI->hasDynamicLifetime(),
654+
isLexical(), IsNotFromVarDecl, DoesNotUseMoveableValueDebugInfo
655655
#ifndef NDEBUG
656-
,
657-
true
656+
,
657+
true
658658
#endif
659659
);
660660

0 commit comments

Comments
 (0)