diff --git a/llvm/lib/IR/Verifier.cpp b/llvm/lib/IR/Verifier.cpp --- a/llvm/lib/IR/Verifier.cpp +++ b/llvm/lib/IR/Verifier.cpp @@ -263,6 +263,13 @@ /// instruction has an operand that is an instruction in the same block. SmallPtrSet InstsInThisBlock; + /// When verifying a basic block, keep track of the mapping between + /// !DILocalVariable fragments and addresses (typically stack slots). This + /// can help identify variables that are accidentally described as being in + /// two different stack slots. + DenseMap, DbgDeclareInst *> + LocalFragment2Address; + /// Keep track of the metadata nodes that have been checked already. SmallPtrSet MDNodes; @@ -2524,6 +2531,7 @@ // void Verifier::visitBasicBlock(BasicBlock &BB) { InstsInThisBlock.clear(); + LocalFragment2Address.clear(); // Ensure that basic blocks have terminators! Assert(BB.getTerminator(), "Basic Block does not have terminator!", &BB); @@ -5322,6 +5330,22 @@ AssertDI(isType(Var->getRawType()), "invalid type ref", Var, Var->getRawType()); verifyFnArgs(DII); + + // A block should not have two dbg.declares which map the same variable + // to two different addresses. However, if a function is inlined more than + // once, it's not unexpected for there to be multiple dbg.declares for an + // inlined entity. + bool InlinedDef = Loc->getInlinedAt(); + if (!InlinedDef && isa(&DII)) { + DbgDeclareInst *&Address = + LocalFragment2Address[{Var, DII.getExpression()}]; + if (!Address) + Address = cast(&DII); + else + AssertDI(Address->getVariableLocation() == DII.getVariableLocation(), + "dbg.declares specify different addresses for the same fragment", + Address, &DII, BB, F, Var); + } } void Verifier::visitDbgLabelIntrinsic(StringRef Kind, DbgLabelInst &DLI) {