@@ -893,19 +893,44 @@ class IRGenSILFunction :
893
893
// / Unconditionally emit a stack shadow copy of an \c llvm::Value.
894
894
llvm::Value *emitShadowCopy (llvm::Value *Storage, const SILDebugScope *Scope,
895
895
SILDebugVariable VarInfo,
896
- llvm::Optional<Alignment> _Align, bool Init) {
896
+ llvm::Optional<Alignment> _Align, bool Init,
897
+ bool WasMoved) {
897
898
auto Align = _Align.getValueOr (IGM.getPointerAlignment ());
898
899
unsigned ArgNo = VarInfo.ArgNo ;
899
900
auto &Alloca = ShadowStackSlots[{ArgNo, {Scope, VarInfo.Name }}];
901
+
900
902
if (!Alloca.isValid ())
901
903
Alloca = createAlloca (Storage->getType (), Align, VarInfo.Name + " .debug" );
902
- assert (canAllocaStoreValue (Alloca.getAddress (), Storage, VarInfo, Scope) &&
904
+
905
+ // If our value was ever moved, we may be reinitializing the shadow
906
+ // copy. Insert the bit cast so that the types line up and we do not get the
907
+ // duplicate shadow copy error (which triggers based off of type
908
+ // differences).
909
+ auto *Address = Alloca.getAddress ();
910
+ if (WasMoved) {
911
+ auto nonPtrAllocaType =
912
+ cast<llvm::PointerType>(Address->getType ())->getElementType ();
913
+ if (nonPtrAllocaType != Storage->getType ())
914
+ Address = Builder.CreateBitOrPointerCast (
915
+ Address, llvm::PointerType::get (Storage->getType (), 0 ));
916
+ }
917
+
918
+ assert (canAllocaStoreValue (Address, Storage, VarInfo, Scope) &&
903
919
" bad scope?" );
920
+
904
921
if (Init) {
922
+ // Zero init our bare allocation.
905
923
zeroInit (cast<llvm::AllocaInst>(Alloca.getAddress ()));
906
924
ArtificialLocation AutoRestore (Scope, IGM.DebugInfo .get (), Builder);
907
- Builder.CreateStore (Storage, Alloca.getAddress (), Align);
925
+ // But store into the address with maybe bitcast.
926
+ Builder.CreateStore (Storage, Address, Align);
908
927
}
928
+
929
+ // If this allocation was moved at some point, we might be reinitializing a
930
+ // shadow copy. In such a case, lets insert an identity bit cast so that our
931
+ // callers will use this address with respect to the place where we
932
+ // reinit. Otherwise, callers may use the alloca's insert point. The
933
+ // optimizer will eliminate these later without issue.
909
934
return Alloca.getAddress ();
910
935
}
911
936
@@ -928,7 +953,7 @@ class IRGenSILFunction :
928
953
llvm::Value *emitShadowCopyIfNeeded (llvm::Value *Storage,
929
954
const SILDebugScope *Scope,
930
955
SILDebugVariable VarInfo,
931
- bool IsAnonymous,
956
+ bool IsAnonymous, bool WasMoved,
932
957
llvm::Optional<Alignment> Align = None) {
933
958
// Never emit shadow copies when optimizing, or if already on the stack. No
934
959
// debug info is emitted for refcounts either
@@ -950,7 +975,8 @@ class IRGenSILFunction :
950
975
}
951
976
952
977
// Emit a shadow copy.
953
- auto shadow = emitShadowCopy (Storage, Scope, VarInfo, Align, true );
978
+ auto shadow =
979
+ emitShadowCopy (Storage, Scope, VarInfo, Align, true , WasMoved);
954
980
955
981
// Mark variables in async functions for lifetime extension, so they get
956
982
// spilled into the async context.
@@ -969,14 +995,16 @@ class IRGenSILFunction :
969
995
llvm::Value *emitShadowCopyIfNeeded (Address Storage,
970
996
const SILDebugScope *Scope,
971
997
SILDebugVariable VarInfo,
972
- bool IsAnonymous) {
998
+ bool IsAnonymous, bool WasMoved ) {
973
999
return emitShadowCopyIfNeeded (Storage.getAddress (), Scope, VarInfo,
974
- IsAnonymous, Storage.getAlignment ());
1000
+ IsAnonymous, WasMoved,
1001
+ Storage.getAlignment ());
975
1002
}
976
1003
977
1004
// / Like \c emitShadowCopyIfNeeded() but takes an exploded value.
978
1005
void emitShadowCopyIfNeeded (SILValue &SILVal, const SILDebugScope *Scope,
979
1006
SILDebugVariable VarInfo, bool IsAnonymous,
1007
+ bool WasMoved,
980
1008
llvm::SmallVectorImpl<llvm::Value *> ©) {
981
1009
Explosion e = getLoweredExplosion (SILVal);
982
1010
@@ -1003,8 +1031,8 @@ class IRGenSILFunction :
1003
1031
return ;
1004
1032
1005
1033
if (e.size () == 1 ) {
1006
- copy.push_back (
1007
- emitShadowCopyIfNeeded (e. claimNext (), Scope, VarInfo, IsAnonymous));
1034
+ copy.push_back (emitShadowCopyIfNeeded (e. claimNext (), Scope, VarInfo,
1035
+ IsAnonymous, WasMoved ));
1008
1036
return ;
1009
1037
}
1010
1038
@@ -1053,12 +1081,11 @@ class IRGenSILFunction :
1053
1081
1054
1082
// / Emit debug info for a function argument or a local variable.
1055
1083
template <typename StorageType>
1056
- void emitDebugVariableDeclaration (StorageType Storage, DebugTypeInfo Ty,
1057
- SILType SILTy, const SILDebugScope *DS,
1058
- SILLocation VarLoc,
1059
- SILDebugVariable VarInfo,
1060
- IndirectionKind Indirection,
1061
- AddrDbgInstrKind DbgInstrKind = AddrDbgInstrKind::DbgDeclare) {
1084
+ void emitDebugVariableDeclaration (
1085
+ StorageType Storage, DebugTypeInfo Ty, SILType SILTy,
1086
+ const SILDebugScope *DS, SILLocation VarLoc, SILDebugVariable VarInfo,
1087
+ IndirectionKind Indirection,
1088
+ AddrDbgInstrKind DbgInstrKind = AddrDbgInstrKind::DbgDeclare) {
1062
1089
// TODO: fix demangling for C++ types (SR-13223).
1063
1090
if (swift::TypeBase *ty = SILTy.getASTType ().getPointer ()) {
1064
1091
if (MetatypeType *metaTy = dyn_cast<MetatypeType>(ty))
@@ -1070,15 +1097,18 @@ class IRGenSILFunction :
1070
1097
}
1071
1098
1072
1099
assert (IGM.DebugInfo && " debug info not enabled" );
1100
+
1073
1101
if (VarInfo.ArgNo ) {
1074
1102
PrologueLocation AutoRestore (IGM.DebugInfo .get (), Builder);
1075
1103
IGM.DebugInfo ->emitVariableDeclaration (Builder, Storage, Ty, DS, VarLoc,
1076
1104
VarInfo, Indirection, ArtificialKind::RealValue,
1077
1105
DbgInstrKind);
1078
- } else
1079
- IGM.DebugInfo ->emitVariableDeclaration (Builder, Storage, Ty, DS, VarLoc,
1080
- VarInfo, Indirection, ArtificialKind::RealValue,
1081
- DbgInstrKind);
1106
+ return ;
1107
+ }
1108
+
1109
+ IGM.DebugInfo ->emitVariableDeclaration (
1110
+ Builder, Storage, Ty, DS, VarLoc, VarInfo, Indirection,
1111
+ ArtificialKind::RealValue, DbgInstrKind);
1082
1112
}
1083
1113
1084
1114
void emitFailBB () {
@@ -4787,8 +4817,9 @@ void IRGenSILFunction::emitErrorResultVar(CanSILFunctionType FnTy,
4787
4817
ErrorInfo, FnTy, IGM.getMaximalTypeExpansionContext ()));
4788
4818
auto Var = DbgValue->getVarInfo ();
4789
4819
assert (Var && " error result without debug info" );
4790
- auto Storage = emitShadowCopyIfNeeded (ErrorResultSlot.getAddress (),
4791
- getDebugScope (), *Var, false );
4820
+ auto Storage =
4821
+ emitShadowCopyIfNeeded (ErrorResultSlot.getAddress (), getDebugScope (),
4822
+ *Var, false , false /* was move*/ );
4792
4823
if (!IGM.DebugInfo )
4793
4824
return ;
4794
4825
auto DbgTy = DebugTypeInfo::getErrorResult (
@@ -4853,7 +4884,8 @@ void IRGenSILFunction::emitPoisonDebugValueInst(DebugValueInst *i) {
4853
4884
// vs. multi-value explosions.
4854
4885
llvm::Value *shadowAddress;
4855
4886
if (singleValueExplosion) {
4856
- shadowAddress = emitShadowCopy (storage, scope, *varInfo, ptrAlign, false );
4887
+ shadowAddress = emitShadowCopy (storage, scope, *varInfo, ptrAlign, false ,
4888
+ false /* was moved*/ );
4857
4889
} else {
4858
4890
assert (refTy->isClassExistentialType () && " unknown multi-value explosion" );
4859
4891
// FIXME: Handling Optional existentials requires TypeInfo
@@ -4984,12 +5016,12 @@ void IRGenSILFunction::visitDebugValueInst(DebugValueInst *i) {
4984
5016
// Put the value into a shadow-copy stack slot at -Onone.
4985
5017
llvm::SmallVector<llvm::Value *, 8 > Copy;
4986
5018
if (IsAddrVal)
4987
- Copy.emplace_back (
4988
- emitShadowCopyIfNeeded ( getLoweredAddress (SILVal).getAddress (),
4989
- i->getDebugScope (), *VarInfo, IsAnonymous ));
5019
+ Copy.emplace_back (emitShadowCopyIfNeeded (
5020
+ getLoweredAddress (SILVal).getAddress (), i-> getDebugScope (), *VarInfo ,
5021
+ IsAnonymous, i->getWasMoved () ));
4990
5022
else
4991
5023
emitShadowCopyIfNeeded (SILVal, i->getDebugScope (), *VarInfo, IsAnonymous,
4992
- Copy);
5024
+ i-> getWasMoved (), Copy);
4993
5025
4994
5026
bindArchetypes (DbgTy.getType ());
4995
5027
if (!IGM.DebugInfo )
@@ -5314,7 +5346,7 @@ void IRGenSILFunction::emitDebugInfoForAllocStack(AllocStackInst *i,
5314
5346
if (!Alloca->isStaticAlloca ()) {
5315
5347
// Store the address of the dynamic alloca on the stack.
5316
5348
addr = emitShadowCopy (addr, DS, *VarInfo, IGM.getPointerAlignment (),
5317
- /* init*/ true );
5349
+ /* init*/ true , i-> getWasMoved () );
5318
5350
Indirection =
5319
5351
InCoroContext (*CurSILFn, *i) ? CoroIndirectValue : IndirectValue;
5320
5352
}
@@ -5356,17 +5388,18 @@ void IRGenSILFunction::emitDebugInfoForAllocStack(AllocStackInst *i,
5356
5388
!isa<llvm::UndefValue>(shadow)) {
5357
5389
if (ValueVariables.insert (shadow).second )
5358
5390
ValueDomPoints.push_back ({shadow, getActiveDominancePoint ()});
5359
- auto inst = cast<llvm::Instruction>(shadow);
5360
- llvm::IRBuilder<> builder (inst ->getNextNode ());
5391
+ auto shadowInst = cast<llvm::Instruction>(shadow);
5392
+ llvm::IRBuilder<> builder (shadowInst ->getNextNode ());
5361
5393
addr =
5362
5394
builder.CreateLoad (shadow->getType ()->getPointerElementType (), shadow);
5363
5395
}
5364
5396
5365
5397
bindArchetypes (DbgTy.getType ());
5366
- if (IGM.DebugInfo )
5367
- emitDebugVariableDeclaration (addr, DbgTy, SILTy, DS,
5368
- i->getLoc (), *VarInfo,
5369
- Indirection);
5398
+ if (IGM.DebugInfo ) {
5399
+ emitDebugVariableDeclaration (addr, DbgTy, SILTy, DS, i->getLoc (), *VarInfo,
5400
+ Indirection,
5401
+ AddrDbgInstrKind (i->getWasMoved ()));
5402
+ }
5370
5403
}
5371
5404
5372
5405
void IRGenSILFunction::visitAllocStackInst (swift::AllocStackInst *i) {
@@ -5585,8 +5618,9 @@ void IRGenSILFunction::visitAllocBoxInst(swift::AllocBoxInst *i) {
5585
5618
auto VarInfo = i->getVarInfo ();
5586
5619
assert (VarInfo && " debug_value without debug info" );
5587
5620
5588
- auto Storage = emitShadowCopyIfNeeded (
5589
- boxWithAddr.getAddress (), i->getDebugScope (), *VarInfo, IsAnonymous);
5621
+ auto Storage =
5622
+ emitShadowCopyIfNeeded (boxWithAddr.getAddress (), i->getDebugScope (),
5623
+ *VarInfo, IsAnonymous, false /* was moved*/ );
5590
5624
5591
5625
if (!IGM.DebugInfo )
5592
5626
return ;
0 commit comments