@@ -719,6 +719,32 @@ class IRGenSILFunction :
719
719
}
720
720
}
721
721
722
+ // / To make it unambiguous whether a `var` binding has been initialized,
723
+ // / zero-initialize the first pointer-sized field. LLDB uses this to
724
+ // / recognize to detect uninitizialized variables. This can be removed once
725
+ // / swiftc switches to @llvm.dbg.addr() intrinsics.
726
+ void zeroInit (llvm::AllocaInst *AI) {
727
+ if (!AI)
728
+ return ;
729
+
730
+ // Only do this at -Onone.
731
+ if (IGM.IRGen .Opts .shouldOptimize ())
732
+ return ;
733
+
734
+ auto &DL = IGM.DataLayout ;
735
+ if (DL.getTypeSizeInBits (AI->getAllocatedType ()) <
736
+ DL.getPointerSizeInBits ())
737
+ return ;
738
+
739
+ llvm::IRBuilder<> ZeroInitBuilder (AI->getNextNode ());
740
+ ZeroInitBuilder.SetCurrentDebugLocation (nullptr );
741
+ auto *BC =
742
+ ZeroInitBuilder.CreateBitCast (AI, IGM.OpaquePtrTy ->getPointerTo ());
743
+ ZeroInitBuilder.CreateAlignedStore (
744
+ llvm::ConstantPointerNull::get (IGM.OpaquePtrTy ), BC,
745
+ IGM.getPointerAlignment ().getValue ());
746
+ }
747
+
722
748
// / Account for bugs in LLVM.
723
749
// /
724
750
// / - The LLVM type legalizer currently doesn't update debug
@@ -742,6 +768,7 @@ class IRGenSILFunction :
742
768
auto &Alloca = ShadowStackSlots[{ArgNo, {Scope, Name}}];
743
769
if (!Alloca.isValid ())
744
770
Alloca = createAlloca (Storage->getType (), Align, Name+" .addr" );
771
+ zeroInit (dyn_cast<llvm::AllocaInst>(Alloca.getAddress ()));
745
772
746
773
ArtificialLocation AutoRestore (Scope, IGM.DebugInfo , Builder);
747
774
Builder.CreateStore (Storage, Alloca.getAddress (), Align);
@@ -4000,31 +4027,10 @@ void IRGenSILFunction::visitAllocStackInst(swift::AllocStackInst *i) {
4000
4027
if (!Decl)
4001
4028
return ;
4002
4029
4003
- // To make it unambiguous whether a `var` binding has been initialized,
4004
- // zero-initialize the first pointer-sized field. LLDB uses this to
4005
- // recognize to detect uninitizialized variables. This can be removed once
4006
- // swiftc switches to @llvm.dbg.addr() intrinsics.
4007
- auto zeroInit = [&]() {
4008
- if (IGM.IRGen .Opts .shouldOptimize ())
4009
- return ;
4010
- Type Desugared = Decl->getType ()->getDesugaredType ();
4011
- if (!Desugared->getClassOrBoundGenericClass () &&
4012
- !Desugared->getStructOrBoundGenericStruct ())
4013
- return ;
4014
-
4015
- auto *AI = dyn_cast<llvm::AllocaInst>(addr.getAddress ().getAddress ());
4016
- if (!AI)
4017
- return ;
4018
-
4019
- auto &DL = IGM.DataLayout ;
4020
- if (DL.getTypeSizeInBits (AI->getAllocatedType ()) < DL.getPointerSizeInBits ())
4021
- return ;
4022
-
4023
- auto *BC = Builder.CreateBitCast (AI, IGM.OpaquePtrTy ->getPointerTo ());
4024
- Builder.CreateStore (llvm::ConstantPointerNull::get (IGM.OpaquePtrTy ), BC,
4025
- IGM.getPointerAlignment ());
4026
- };
4027
- zeroInit ();
4030
+ Type Desugared = Decl->getType ()->getDesugaredType ();
4031
+ if (Desugared->getClassOrBoundGenericClass () ||
4032
+ Desugared->getStructOrBoundGenericStruct ())
4033
+ zeroInit (dyn_cast<llvm::AllocaInst>(addr.getAddress ().getAddress ()));
4028
4034
emitDebugInfoForAllocStack (i, type, addr.getAddress ().getAddress ());
4029
4035
}
4030
4036
0 commit comments