diff --git a/llvm/include/llvm/IR/DebugInfoMetadata.h b/llvm/include/llvm/IR/DebugInfoMetadata.h --- a/llvm/include/llvm/IR/DebugInfoMetadata.h +++ b/llvm/include/llvm/IR/DebugInfoMetadata.h @@ -335,10 +335,9 @@ Metadata *getRawStride() const { return getOperand(3).get(); } - typedef PointerUnion CountType; typedef PointerUnion BoundType; - CountType getCount() const; + BoundType getCount() const; BoundType getLowerBound() const; diff --git a/llvm/lib/AsmParser/LLParser.cpp b/llvm/lib/AsmParser/LLParser.cpp --- a/llvm/lib/AsmParser/LLParser.cpp +++ b/llvm/lib/AsmParser/LLParser.cpp @@ -4716,11 +4716,6 @@ Metadata *LowerBound = nullptr; Metadata *UpperBound = nullptr; Metadata *Stride = nullptr; - if (count.isMDSignedField()) - Count = ConstantAsMetadata::get(ConstantInt::getSigned( - Type::getInt64Ty(Context), count.getMDSignedValue())); - else if (count.isMDField()) - Count = count.getMDFieldValue(); auto convToMetadata = [&](MDSignedOrMDField Bound) -> Metadata * { if (Bound.isMDSignedField()) @@ -4731,6 +4726,7 @@ return nullptr; }; + Count = convToMetadata(count); LowerBound = convToMetadata(lowerBound); UpperBound = convToMetadata(upperBound); Stride = convToMetadata(stride); diff --git a/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp b/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp --- a/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp +++ b/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp @@ -1313,9 +1313,6 @@ // Count == -1 then the array is unbounded and we do not emit // DW_AT_lower_bound and DW_AT_count attributes. int64_t DefaultLowerBound = getDefaultLowerBound(); - int64_t Count = -1; - if (auto *CI = SR->getCount().dyn_cast()) - Count = CI->getSExtValue(); auto AddBoundTypeEntry = [&](dwarf::Attribute Attr, DISubrange::BoundType Bound) -> void { @@ -1329,19 +1326,18 @@ DwarfExpr.addExpression(BE); addBlock(DW_Subrange, Attr, DwarfExpr.finalize()); } else if (auto *BI = Bound.dyn_cast()) { - if (Attr != dwarf::DW_AT_lower_bound || DefaultLowerBound == -1 || - BI->getSExtValue() != DefaultLowerBound) + if (Attr == dwarf::DW_AT_count) { + if (BI->getSExtValue() != -1) + addUInt(DW_Subrange, Attr, None, BI->getSExtValue()); + } else if (Attr != dwarf::DW_AT_lower_bound || DefaultLowerBound == -1 || + BI->getSExtValue() != DefaultLowerBound) addSInt(DW_Subrange, Attr, dwarf::DW_FORM_sdata, BI->getSExtValue()); } }; AddBoundTypeEntry(dwarf::DW_AT_lower_bound, SR->getLowerBound()); - if (auto *CV = SR->getCount().dyn_cast()) { - if (auto *CountVarDIE = getDIE(CV)) - addDIEEntry(DW_Subrange, dwarf::DW_AT_count, *CountVarDIE); - } else if (Count != -1) - addUInt(DW_Subrange, dwarf::DW_AT_count, None, Count); + AddBoundTypeEntry(dwarf::DW_AT_count, SR->getCount()); AddBoundTypeEntry(dwarf::DW_AT_upper_bound, SR->getUpperBound()); diff --git a/llvm/lib/IR/AsmWriter.cpp b/llvm/lib/IR/AsmWriter.cpp --- a/llvm/lib/IR/AsmWriter.cpp +++ b/llvm/lib/IR/AsmWriter.cpp @@ -1889,11 +1889,14 @@ const Module *Context) { Out << "!DISubrange("; MDFieldPrinter Printer(Out, TypePrinter, Machine, Context); - if (auto *CE = N->getCount().dyn_cast()) - Printer.printInt("count", CE->getSExtValue(), /* ShouldSkipZero */ false); - else - Printer.printMetadata("count", N->getCount().dyn_cast(), - /*ShouldSkipNull */ true); + + auto *Count = N->getRawCountNode(); + if (auto *CE = dyn_cast_or_null(Count)) { + auto *CV = cast(CE->getValue()); + Printer.printInt("count", CV->getSExtValue(), + /* ShouldSkipZero */ false); + } else + Printer.printMetadata("count", Count, /*ShouldSkipNull */ true); // A lowerBound of constant 0 should not be skipped, since it is different // from an unspecified lower bound (= nullptr). diff --git a/llvm/lib/IR/DebugInfoMetadata.cpp b/llvm/lib/IR/DebugInfoMetadata.cpp --- a/llvm/lib/IR/DebugInfoMetadata.cpp +++ b/llvm/lib/IR/DebugInfoMetadata.cpp @@ -359,17 +359,25 @@ DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(DISubrange, Ops); } -DISubrange::CountType DISubrange::getCount() const { - if (!getRawCountNode()) - return CountType(); +DISubrange::BoundType DISubrange::getCount() const { + Metadata *CB = getRawCountNode(); + if (!CB) + return BoundType(); + + assert((isa(CB) || isa(CB) || + isa(CB)) && + "Count must be signed constant or DIVariable or DIExpression"); + + if (auto *MD = dyn_cast(CB)) + return BoundType(cast(MD->getValue())); - if (auto *MD = dyn_cast(getRawCountNode())) - return CountType(cast(MD->getValue())); + if (auto *MD = dyn_cast(CB)) + return BoundType(MD); - if (auto *DV = dyn_cast(getRawCountNode())) - return CountType(DV); + if (auto *MD = dyn_cast(CB)) + return BoundType(MD); - return CountType(); + return BoundType(); } DISubrange::BoundType DISubrange::getLowerBound() const { 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 @@ -922,8 +922,10 @@ "Subrange must contain count or upperBound", &N); AssertDI(!N.getRawCountNode() || !N.getRawUpperBound(), "Subrange can have any one of count or upperBound", &N); - AssertDI(!N.getRawCountNode() || N.getCount(), - "Count must either be a signed constant or a DIVariable", &N); + auto *CBound = N.getRawCountNode(); + AssertDI(!CBound || isa(CBound) || + isa(CBound) || isa(CBound), + "Count must be signed constant or DIVariable or DIExpression", &N); auto Count = N.getCount(); AssertDI(!Count || !Count.is() || Count.get()->getSExtValue() >= -1, diff --git a/llvm/test/DebugInfo/fortranSubrangeCountExpr.ll b/llvm/test/DebugInfo/fortranSubrangeCountExpr.ll new file mode 100644 --- /dev/null +++ b/llvm/test/DebugInfo/fortranSubrangeCountExpr.ll @@ -0,0 +1,42 @@ +;; This test checks DISubrange count bound for DIExpression +; REQUIRES: x86_64-linux + +; RUN: %llc_dwarf %s -filetype=obj -o - | llvm-dwarfdump - | FileCheck %s + +;; Test whether bounds are generated correctly. +; CHECK-LABEL: DW_TAG_array_type +; CHECK: DW_TAG_subrange_type +; CHECK: DW_AT_lower_bound (DW_OP_push_object_address, DW_OP_plus_uconst 0x50, DW_OP_deref) +; CHECK-NEXT: DW_AT_count (DW_OP_push_object_address, DW_OP_plus_uconst 0x58, DW_OP_deref) + +; ModuleID = 'fortsubrange.modified.strategy3check-in.ll' +source_filename = "fortsubrange.ll" + +define void @MAIN_() !dbg !5 { +L.entry: + %"arr$sd1_349" = alloca [16 x i64], align 8 + call void @llvm.dbg.declare(metadata [16 x i64]* %"arr$sd1_349", metadata !8, metadata !DIExpression()), !dbg !13 + ret void, !dbg !14 +} + +; Function Attrs: nounwind readnone speculatable willreturn +declare void @llvm.dbg.declare(metadata, metadata, metadata) + +!llvm.module.flags = !{!0, !1} +!llvm.dbg.cu = !{!2} + +!0 = !{i32 2, !"Dwarf Version", i32 4} +!1 = !{i32 2, !"Debug Info Version", i32 3} +!2 = distinct !DICompileUnit(language: DW_LANG_Fortran90, file: !3, producer: " F90 Flang - 1.5 2017-05-01", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !4, retainedTypes: !4, globals: !4, imports: !4) +!3 = !DIFile(filename: "fortsubrange.f90", directory: "/dir") +!4 = !{} +!5 = distinct !DISubprogram(name: "main", scope: !2, file: !3, line: 1, type: !6, scopeLine: 1, spFlags: DISPFlagDefinition | DISPFlagMainSubprogram, unit: !2) +!6 = !DISubroutineType(cc: DW_CC_program, types: !7) +!7 = !{null} +!8 = !DILocalVariable(name: "arr", scope: !5, file: !3, type: !9) +!9 = !DICompositeType(tag: DW_TAG_array_type, baseType: !10, size: 32, align: 32, elements: !11) +!10 = !DIBasicType(name: "integer", size: 32, align: 32, encoding: DW_ATE_signed) +!11 = !{!12} +!12 = !DISubrange(count: !DIExpression(DW_OP_push_object_address, DW_OP_plus_uconst, 88, DW_OP_deref), lowerBound: !DIExpression(DW_OP_push_object_address, DW_OP_plus_uconst, 80, DW_OP_deref)) +!13 = !DILocation(line: 0, scope: !5) +!14 = !DILocation(line: 6, column: 1, scope: !5) diff --git a/llvm/test/Verifier/invalid-disubrange-count-node.ll b/llvm/test/Verifier/invalid-disubrange-count-node.ll --- a/llvm/test/Verifier/invalid-disubrange-count-node.ll +++ b/llvm/test/Verifier/invalid-disubrange-count-node.ll @@ -33,5 +33,5 @@ !19 = !DILocalVariable(name: "vla", scope: !7, file: !1, line: 21, type: !20) !20 = !DICompositeType(tag: DW_TAG_array_type, baseType: !10, align: 32, elements: !21) !21 = !{!22} -; CHECK: Count must either be a signed constant or a DIVariable +; CHECK: Count must be signed constant or DIVariable or DIExpression !22 = !DISubrange(count: !17)