Skip to content

Commit 8d12f89

Browse files
committed
[sil] Add a moveable_value_debuginfo field to AllocBoxInst.
Just getting parsing/serialization to work. I haven't wired it up to anything.
1 parent 1a76f8f commit 8d12f89

File tree

14 files changed

+100
-33
lines changed

14 files changed

+100
-33
lines changed

docs/SIL.rst

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3867,7 +3867,8 @@ alloc_box
38673867
`````````
38683868
::
38693869

3870-
sil-instruction ::= 'alloc_box' sil-type (',' debug-var-attr)*
3870+
sil-instruction ::= 'alloc_box' alloc-box-option* sil-type (',' debug-var-attr)*
3871+
alloc-box-option ::= moveable_value_debuginfo
38713872

38723873
%1 = alloc_box $T
38733874
// %1 has type $@box T
@@ -3885,6 +3886,11 @@ Releasing a box is undefined behavior if the box's value is uninitialized.
38853886
To deallocate a box whose value has not been initialized, ``dealloc_box``
38863887
should be used.
38873888

3889+
The optional ``moveable_value_debuginfo`` attribute specifies that when
3890+
emitting debug info, the code generator can not assume that the value in the
3891+
alloc_stack can be semantically valid over the entire function frame when
3892+
emitting debug info.
3893+
38883894
alloc_global
38893895
````````````
38903896

include/swift/SIL/DebugUtils.h

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#define SWIFT_SIL_DEBUGUTILS_H
3838

3939
#include "swift/SIL/SILBasicBlock.h"
40+
#include "swift/SIL/SILBuilder.h"
4041
#include "swift/SIL/SILGlobalVariable.h"
4142
#include "swift/SIL/SILInstruction.h"
4243

@@ -466,7 +467,8 @@ struct DebugVarCarryingInst : VarDeclCarryingInst {
466467
cast<AllocStackInst>(**this)->markUsesMoveableValueDebugInfo();
467468
break;
468469
case Kind::AllocBox:
469-
llvm_unreachable("Not implemented");
470+
cast<AllocBoxInst>(**this)->setUsesMoveableValueDebugInfo();
471+
break;
470472
}
471473
}
472474

@@ -480,8 +482,7 @@ struct DebugVarCarryingInst : VarDeclCarryingInst {
480482
case Kind::AllocStack:
481483
return cast<AllocStackInst>(**this)->getUsesMoveableValueDebugInfo();
482484
case Kind::AllocBox:
483-
// We do not support moving alloc box today, so we always return false.
484-
return false;
485+
return cast<AllocBoxInst>(**this)->getUsesMoveableValueDebugInfo();
485486
}
486487
}
487488

@@ -491,7 +492,10 @@ struct DebugVarCarryingInst : VarDeclCarryingInst {
491492
///
492493
/// For a debug_value, we just return the actual operand, otherwise we return
493494
/// the pointer address.
494-
SILValue getOperandForDebugValueClone() const {
495+
///
496+
/// If we have an alloc_box, we return a new project_box. This is the only
497+
/// case where a SILBuilder is required.
498+
SILValue getOperandForDebugValueClone(SILBuilder *builder = nullptr) const {
495499
switch (getKind()) {
496500
case Kind::Invalid:
497501
llvm_unreachable("Invalid?!");
@@ -500,7 +504,10 @@ struct DebugVarCarryingInst : VarDeclCarryingInst {
500504
case Kind::AllocStack:
501505
return cast<AllocStackInst>(**this);
502506
case Kind::AllocBox:
503-
llvm_unreachable("Not implemented");
507+
assert(builder);
508+
return builder->createProjectBox(
509+
RegularLocation::getAutoGeneratedLocation(),
510+
cast<AllocBoxInst>(**this), 0);
504511
}
505512
}
506513

include/swift/SIL/SILBuilder.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -444,22 +444,26 @@ class SILBuilder {
444444
AllocBoxInst *createAllocBox(SILLocation loc, SILType fieldType,
445445
Optional<SILDebugVariable> Var = None,
446446
bool hasDynamicLifetime = false,
447-
bool reflection = false) {
447+
bool reflection = false,
448+
bool usesMoveableValueDebugInfo = false) {
448449
return createAllocBox(loc, SILBoxType::get(fieldType.getASTType()), Var,
449-
hasDynamicLifetime, reflection);
450+
hasDynamicLifetime, reflection,
451+
usesMoveableValueDebugInfo);
450452
}
451453

452454
AllocBoxInst *createAllocBox(SILLocation Loc, CanSILBoxType BoxType,
453455
Optional<SILDebugVariable> Var = None,
454456
bool hasDynamicLifetime = false,
455-
bool reflection = false) {
457+
bool reflection = false,
458+
bool usesMoveableValueDebugInfo = false) {
456459
llvm::SmallString<4> Name;
457460
Loc.markAsPrologue();
458461
assert((!dyn_cast_or_null<VarDecl>(Loc.getAsASTNode<Decl>()) || Var) &&
459462
"location is a VarDecl, but SILDebugVariable is empty");
460463
return insert(AllocBoxInst::create(getSILDebugLocation(Loc), BoxType, *F,
461464
substituteAnonymousArgs(Name, Var, Loc),
462-
hasDynamicLifetime, reflection));
465+
hasDynamicLifetime, reflection,
466+
usesMoveableValueDebugInfo));
463467
}
464468

465469
AllocExistentialBoxInst *

include/swift/SIL/SILInstruction.h

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2461,13 +2461,13 @@ class AllocBoxInst final
24612461
AllocBoxInst(SILDebugLocation DebugLoc, CanSILBoxType BoxType,
24622462
ArrayRef<SILValue> TypeDependentOperands, SILFunction &F,
24632463
Optional<SILDebugVariable> Var, bool hasDynamicLifetime,
2464-
bool reflection = false);
2464+
bool reflection = false,
2465+
bool usesMoveableValueDebugInfo = false);
24652466

24662467
static AllocBoxInst *create(SILDebugLocation Loc, CanSILBoxType boxType,
2467-
SILFunction &F,
2468-
Optional<SILDebugVariable> Var,
2469-
bool hasDynamicLifetime,
2470-
bool reflection = false);
2468+
SILFunction &F, Optional<SILDebugVariable> Var,
2469+
bool hasDynamicLifetime, bool reflection = false,
2470+
bool usesMoveableValueDebugInfo = false);
24712471

24722472
public:
24732473
CanSILBoxType getBoxType() const {
@@ -2495,6 +2495,14 @@ class AllocBoxInst final
24952495
Optional<SILDebugVariable> getVarInfo() const {
24962496
return VarInfo.get(getDecl(), getTrailingObjects<char>());
24972497
};
2498+
2499+
void setUsesMoveableValueDebugInfo() {
2500+
sharedUInt8().AllocBoxInst.usesMoveableValueDebugInfo = true;
2501+
}
2502+
2503+
bool getUsesMoveableValueDebugInfo() const {
2504+
return sharedUInt8().AllocBoxInst.usesMoveableValueDebugInfo;
2505+
}
24982506
};
24992507

25002508
/// This represents the allocation of a heap box for an existential container.

include/swift/SIL/SILNode.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,8 @@ class alignas(8) SILNode :
216216

217217
SHARED_FIELD(AllocBoxInst, uint8_t
218218
dynamicLifetime : 1,
219-
reflection : 1);
219+
reflection : 1,
220+
usesMoveableValueDebugInfo : 1);
220221

221222
SHARED_FIELD(AllocRefInstBase, uint8_t
222223
objC : 1,

lib/SIL/IR/SILBuilder.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,7 @@ DebugValueInst *SILBuilder::createDebugValue(SILLocation Loc, SILValue src,
612612
bool operandWasMoved,
613613
bool trace) {
614614
llvm::SmallString<4> Name;
615+
615616
// Debug location overrides cannot apply to debug value instructions.
616617
DebugLocOverrideRAII LocOverride{*this, None};
617618
return insert(DebugValueInst::create(
@@ -624,6 +625,7 @@ DebugValueInst *SILBuilder::createDebugValueAddr(SILLocation Loc, SILValue src,
624625
SILDebugVariable Var,
625626
bool wasMoved, bool trace) {
626627
llvm::SmallString<4> Name;
628+
627629
// Debug location overrides cannot apply to debug addr instructions.
628630
DebugLocOverrideRAII LocOverride{*this, None};
629631
return insert(DebugValueInst::createAddr(

lib/SIL/IR/SILInstructions.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -375,27 +375,30 @@ bool AllocRefDynamicInst::isDynamicTypeDeinitAndSizeKnownEquivalentToBaseType()
375375
AllocBoxInst::AllocBoxInst(SILDebugLocation Loc, CanSILBoxType BoxType,
376376
ArrayRef<SILValue> TypeDependentOperands,
377377
SILFunction &F, Optional<SILDebugVariable> Var,
378-
bool hasDynamicLifetime, bool reflection)
378+
bool hasDynamicLifetime, bool reflection,
379+
bool usesMoveableValueDebugInfo)
379380
: NullaryInstructionWithTypeDependentOperandsBase(
380381
Loc, TypeDependentOperands, SILType::getPrimitiveObjectType(BoxType)),
381382
VarInfo(Var, getTrailingObjects<char>()) {
382383
sharedUInt8().AllocBoxInst.dynamicLifetime = hasDynamicLifetime;
383384
sharedUInt8().AllocBoxInst.reflection = reflection;
385+
sharedUInt8().AllocBoxInst.usesMoveableValueDebugInfo =
386+
usesMoveableValueDebugInfo;
384387
}
385388

386-
AllocBoxInst *AllocBoxInst::create(SILDebugLocation Loc,
387-
CanSILBoxType BoxType,
389+
AllocBoxInst *AllocBoxInst::create(SILDebugLocation Loc, CanSILBoxType BoxType,
388390
SILFunction &F,
389391
Optional<SILDebugVariable> Var,
390-
bool hasDynamicLifetime,
391-
bool reflection) {
392+
bool hasDynamicLifetime, bool reflection,
393+
bool usesMoveableValueDebugInfo) {
392394
SmallVector<SILValue, 8> TypeDependentOperands;
393395
collectTypeDependentOperands(TypeDependentOperands, F, BoxType);
394396
auto Sz = totalSizeToAlloc<swift::Operand, char>(TypeDependentOperands.size(),
395397
Var ? Var->Name.size() : 0);
396398
auto Buf = F.getModule().allocateInst(Sz, alignof(AllocBoxInst));
397-
return ::new (Buf) AllocBoxInst(Loc, BoxType, TypeDependentOperands, F, Var,
398-
hasDynamicLifetime, reflection);
399+
return ::new (Buf)
400+
AllocBoxInst(Loc, BoxType, TypeDependentOperands, F, Var,
401+
hasDynamicLifetime, reflection, usesMoveableValueDebugInfo);
399402
}
400403

401404
SILType AllocBoxInst::getAddressType() const {

lib/SIL/IR/SILPrinter.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1434,6 +1434,11 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
14341434
if (ABI->emitReflectionMetadata()) {
14351435
*this << "[reflection] ";
14361436
}
1437+
1438+
if (ABI->getUsesMoveableValueDebugInfo()) {
1439+
*this << "[moveable_value_debuginfo] ";
1440+
}
1441+
14371442
*this << ABI->getType();
14381443
printDebugVar(ABI->getVarInfo(),
14391444
&ABI->getModule().getASTContext().SourceMgr);

lib/SIL/Parser/ParseSIL.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2833,16 +2833,20 @@ bool SILParser::parseSpecificSILInstruction(SILBuilder &B,
28332833
case SILInstructionKind::AllocBoxInst: {
28342834
bool hasDynamicLifetime = false;
28352835
bool hasReflection = false;
2836+
bool usesMoveableValueDebugInfo = false;
28362837
StringRef attrName;
28372838
SourceLoc attrLoc;
28382839
while (parseSILOptional(attrName, attrLoc, *this)) {
28392840
if (attrName.equals("dynamic_lifetime")) {
28402841
hasDynamicLifetime = true;
28412842
} else if (attrName.equals("reflection")) {
28422843
hasReflection = true;
2844+
} else if (attrName.equals("moveable_value_debuginfo")) {
2845+
usesMoveableValueDebugInfo = true;
28432846
} else {
2844-
P.diagnose(attrLoc, diag::sil_invalid_attribute_for_expected, attrName,
2845-
"dynamic_lifetime or reflection");
2847+
P.diagnose(
2848+
attrLoc, diag::sil_invalid_attribute_for_expected, attrName,
2849+
"dynamic_lifetime, reflection, or usesMoveableValueDebugInfo");
28462850
}
28472851
}
28482852

@@ -2855,7 +2859,8 @@ bool SILParser::parseSpecificSILInstruction(SILBuilder &B,
28552859
if (parseSILDebugLocation(InstLoc, B))
28562860
return true;
28572861
ResultVal = B.createAllocBox(InstLoc, Ty.castTo<SILBoxType>(), VarInfo,
2858-
hasDynamicLifetime, hasReflection);
2862+
hasDynamicLifetime, hasReflection,
2863+
usesMoveableValueDebugInfo);
28592864
break;
28602865
}
28612866
case SILInstructionKind::ApplyInst:

lib/Serialization/DeserializeSIL.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1308,13 +1308,16 @@ bool SILDeserializer::readSILInstruction(SILFunction *Fn,
13081308
case SILInstructionKind::TestSpecificationInst:
13091309
llvm_unreachable("not supported");
13101310

1311-
case SILInstructionKind::AllocBoxInst:
1311+
case SILInstructionKind::AllocBoxInst: {
13121312
assert(RecordKind == SIL_ONE_TYPE && "Layout should be OneType.");
1313+
bool hasDynamicLifetime = Attr & 0x1;
1314+
bool reflection = (Attr >> 1) & 0x1;
1315+
bool usesMoveableValueDebugInfo = (Attr >> 2) & 0x1;
13131316
ResultInst = Builder.createAllocBox(
13141317
Loc, cast<SILBoxType>(MF->getType(TyID)->getCanonicalType()), None,
1315-
/*bool hasDynamicLifetime*/ Attr & 1,
1316-
/*bool reflection*/ Attr & 2);
1318+
hasDynamicLifetime, reflection, usesMoveableValueDebugInfo);
13171319
break;
1320+
}
13181321
case SILInstructionKind::AllocStackInst: {
13191322
assert(RecordKind == SIL_ONE_TYPE && "Layout should be OneType.");
13201323
bool hasDynamicLifetime = Attr & 0x1;

0 commit comments

Comments
 (0)