From 56109f5f411523967ffef5a32a507d6472eebdaf Mon Sep 17 00:00:00 2001 From: Stephen Tozer Date: Mon, 10 Jun 2024 11:35:02 +0100 Subject: [PATCH 1/2] [Clang] Extend EmitPseudoVariable to support debug records CGDebugInfo::EmitPseudoVariable currently uses detailed logic to exactly collect llvm.dbg.declare users of an alloca. This patch replaces this with an LLVM function for finding debug declares intrinsics and also adds the same for debug records, simplifying the code and adding record support. --- clang/lib/CodeGen/CGDebugInfo.cpp | 32 ++++++++++--------------------- 1 file changed, 10 insertions(+), 22 deletions(-) diff --git a/clang/lib/CodeGen/CGDebugInfo.cpp b/clang/lib/CodeGen/CGDebugInfo.cpp index 681a475f9e4be..d5676be0e8742 100644 --- a/clang/lib/CodeGen/CGDebugInfo.cpp +++ b/clang/lib/CodeGen/CGDebugInfo.cpp @@ -5766,28 +5766,16 @@ void CGDebugInfo::EmitPseudoVariable(CGBuilderTy &Builder, // it is loaded upon use, so we identify such pattern here. if (llvm::LoadInst *Load = dyn_cast(Value)) { llvm::Value *Var = Load->getPointerOperand(); - if (llvm::Metadata *MDValue = llvm::ValueAsMetadata::getIfExists(Var)) { - if (llvm::Value *DbgValue = llvm::MetadataAsValue::getIfExists( - CGM.getLLVMContext(), MDValue)) { - for (llvm::User *U : DbgValue->users()) { - if (llvm::CallInst *DbgDeclare = dyn_cast(U)) { - if (DbgDeclare->getCalledFunction()->getIntrinsicID() == - llvm::Intrinsic::dbg_declare && - DbgDeclare->getArgOperand(0) == DbgValue) { - // There can be implicit type cast applied on a variable if it is - // an opaque ptr, in this case its debug info may not match the - // actual type of object being used as in the next instruction, so - // we will need to emit a pseudo variable for type-casted value. - llvm::DILocalVariable *MDNode = cast( - cast(DbgDeclare->getOperand(1)) - ->getMetadata()); - if (MDNode->getType() == Type) - return; - } - } - } - } - } + // There can be implicit type cast applied on a variable if it is an opaque + // ptr, in this case its debug info may not match the actual type of object + // being used as in the next instruction, so we will need to emit a pseudo + // variable for type-casted value. + auto DeclareTypeMatches = [&](auto *DbgDeclare) { + return DbgDeclare->getVariable()->getType() == Type; + }; + if (any_of(llvm::findDbgDeclares(Var), DeclareMatches) || + any_of(llvm::findDVRDeclares(Var), DeclareMatches)) + return; } // Find the correct location to insert a sequence of instructions to From d1fe1a5aa3d1b5d6eb6097ae8313d8620126c5a5 Mon Sep 17 00:00:00 2001 From: Stephen Tozer Date: Mon, 10 Jun 2024 11:55:36 +0100 Subject: [PATCH 2/2] Update lambda name --- clang/lib/CodeGen/CGDebugInfo.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/clang/lib/CodeGen/CGDebugInfo.cpp b/clang/lib/CodeGen/CGDebugInfo.cpp index d5676be0e8742..11e2d549d8a45 100644 --- a/clang/lib/CodeGen/CGDebugInfo.cpp +++ b/clang/lib/CodeGen/CGDebugInfo.cpp @@ -5773,8 +5773,8 @@ void CGDebugInfo::EmitPseudoVariable(CGBuilderTy &Builder, auto DeclareTypeMatches = [&](auto *DbgDeclare) { return DbgDeclare->getVariable()->getType() == Type; }; - if (any_of(llvm::findDbgDeclares(Var), DeclareMatches) || - any_of(llvm::findDVRDeclares(Var), DeclareMatches)) + if (any_of(llvm::findDbgDeclares(Var), DeclareTypeMatches) || + any_of(llvm::findDVRDeclares(Var), DeclareTypeMatches)) return; }