Index: test/DebugInfo/debugify-bogus-dbg-value.ll =================================================================== --- /dev/null +++ test/DebugInfo/debugify-bogus-dbg-value.ll @@ -0,0 +1,51 @@ +; RUN: opt -check-debugify < %s 2>&1 | FileCheck %s + +define <2 x i64> @test-fun(<2 x i64> %A) !dbg !6 { + %and = and <2 x i64> %A, , !dbg !14 + +; CHECK: ERROR: dbg.value operand has size 128, but its variable has size 256 + call void @llvm.dbg.value(metadata <2 x i64> %and, metadata !12, metadata !DIExpression()), !dbg !15 + +; CHECK: ERROR: dbg.value operand has size 512, but its variable has size 256 + call void @llvm.dbg.value(metadata <2 x i256> zeroinitializer, metadata !12, metadata !DIExpression()), !dbg !15 + +; CHECK: ERROR: dbg.value operand has size 8, but its variable has size 256 + call void @llvm.dbg.value(metadata i8 0, metadata !12, metadata !DIExpression()), !dbg !15 + +; CHECK-NOT: ERROR: dbg.value operand has size 512, but its variable has size 256 + call void @llvm.dbg.value(metadata i512 0, metadata !12, metadata !DIExpression()), !dbg !15 + + ret <2 x i64> %and, !dbg !16 +} +; CHECK: CheckModuleDebugify: FAIL + +; Function Attrs: nounwind readnone speculatable +declare void @llvm.dbg.value(metadata, metadata, metadata) #0 + +attributes #0 = { nounwind readnone speculatable } + +!llvm.dbg.cu = !{!0} +!llvm.debugify = !{!3, !4} +!llvm.module.flags = !{!5} + +!0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2) +!1 = !DIFile(filename: "/Users/vsk/src/llvm.org-master/llvm/test/DebugInfo/debugify-bogus-dbg-value.ll", directory: "/") +!2 = !{} +!3 = !{i32 4} +!4 = !{i32 3} +!5 = !{i32 2, !"Debug Info Version", i32 3} +!6 = distinct !DISubprogram(name: "test-fun", linkageName: "test-fun", scope: null, file: !1, line: 1, type: !7, isLocal: false, isDefinition: true, scopeLine: 1, isOptimized: true, unit: !0, retainedNodes: !8) +!7 = !DISubroutineType(types: !2) +!8 = !{!9, !11, !12} +!9 = !DILocalVariable(name: "1", scope: !6, file: !1, line: 1, type: !10) +!10 = !DIBasicType(name: "ty64", size: 64, encoding: DW_ATE_unsigned) +!11 = !DILocalVariable(name: "2", scope: !6, file: !1, line: 2, type: !10) +!12 = !DILocalVariable(name: "3", scope: !6, file: !1, line: 3, type: !13) + +; Set the size here to an incorrect value to check that the size diagnostic +; triggers. +!13 = !DIBasicType(name: "ty128", size: 256, encoding: DW_ATE_unsigned) + +!14 = !DILocation(line: 2, column: 1, scope: !6) +!15 = !DILocation(line: 3, column: 1, scope: !6) +!16 = !DILocation(line: 4, column: 1, scope: !6) Index: tools/opt/Debugify.cpp =================================================================== --- tools/opt/Debugify.cpp +++ tools/opt/Debugify.cpp @@ -40,6 +40,10 @@ raw_ostream &dbg() { return Quiet ? nulls() : errs(); } +uint64_t getSizeInBits(Module &M, Type *Ty) { + return Ty->isSized() ? M.getDataLayout().getTypeAllocSizeInBits(Ty) : 0; +} + bool isFunctionSkipped(Function &F) { return F.isDeclaration() || !F.hasExactDefinition(); } @@ -71,8 +75,7 @@ // Get a DIType which corresponds to Ty. DenseMap TypeCache; auto getCachedDIType = [&](Type *Ty) -> DIType * { - uint64_t Size = - Ty->isSized() ? M.getDataLayout().getTypeAllocSizeInBits(Ty) : 0; + uint64_t Size = getSizeInBits(M, Ty); DIType *&DTy = TypeCache[Size]; if (!DTy) { std::string Name = "ty" + utostr(Size); @@ -208,7 +211,7 @@ HasErrors = true; } - // Find missing variables. + // Find missing variables and mis-sized debug values. for (Instruction &I : instructions(F)) { auto *DVI = dyn_cast(&I); if (!DVI) @@ -218,6 +221,41 @@ (void)to_integer(DVI->getVariable()->getName(), Var, 10); assert(Var <= OriginalNumVars && "Unexpected name for DILocalVariable"); MissingVars.reset(Var - 1); + + // The size of a dbg.value's value operand should match the size of the + // variable it corresponds to. + // + // TODO: This, along with a check for non-null value operands, should be + // promoted to verifier failures. + if (Value *V = DVI->getValue()) { + // For now, don't try to interpret DW_OP_deref, or anything more + // complicated than an empty DIExpression. + if (!DVI->getExpression()->getNumElements()) { + Type *Ty = V->getType(); + uint64_t ActualSizeInBits = getSizeInBits(M, Ty); + uint64_t ExpectedSizeInBits = *DVI->getFragmentSizeInBits(); + + // If the described integer is smaller than the actual value, the + // debugger can display the right value using truncation. However, if + // the described integer is larger than the actual value, it cannot + // display the proper value without guessing (resorting to sign/zero + // extension). + // + // For all other types (vectors, floats, etc.): it's a bug if the + // described value doesn't have the same size as the actual value. + bool HasBadSize = Ty->isIntegerTy() + ? (ActualSizeInBits < ExpectedSizeInBits) + : (ActualSizeInBits != ExpectedSizeInBits); + if (HasBadSize) { + dbg() << "ERROR: dbg.value operand has size " << ActualSizeInBits + << ", but its variable has size " << ExpectedSizeInBits + << ": "; + DVI->print(dbg()); + dbg() << "\n"; + HasErrors = true; + } + } + } } }