Skip to content

Commit 5b3bf8b

Browse files
committed
[DebugInfo] Expose Fortran array debug info attributes through DIBuilder.
The support of a few debug info attributes specifically for Fortran arrays have been added to LLVM recently, but there's no way to take advantage of them through DIBuilder. This patch extends DIBuilder::createArrayType to enable the settings of those attributes. Patch by Chih-Ping Chen! Differential Revision: https://reviews.llvm.org/D89817
1 parent 78f37b7 commit 5b3bf8b

File tree

3 files changed

+77
-8
lines changed

3 files changed

+77
-8
lines changed

llvm/include/llvm/IR/DIBuilder.h

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -494,8 +494,24 @@ namespace llvm {
494494
/// \param AlignInBits Alignment.
495495
/// \param Ty Element type.
496496
/// \param Subscripts Subscripts.
497-
DICompositeType *createArrayType(uint64_t Size, uint32_t AlignInBits,
498-
DIType *Ty, DINodeArray Subscripts);
497+
/// \param DataLocation The location of the raw data of a descriptor-based
498+
/// Fortran array, either a DIExpression* or
499+
/// a DIVariable*.
500+
/// \param Associated The associated attribute of a descriptor-based
501+
/// Fortran array, either a DIExpression* or
502+
/// a DIVariable*.
503+
/// \param Allocated The allocated attribute of a descriptor-based
504+
/// Fortran array, either a DIExpression* or
505+
/// a DIVariable*.
506+
/// \param Rank The rank attribute of a descriptor-based
507+
/// Fortran array, either a DIExpression* or
508+
/// a DIVariable*.
509+
DICompositeType *createArrayType(
510+
uint64_t Size, uint32_t AlignInBits, DIType *Ty, DINodeArray Subscripts,
511+
PointerUnion<DIExpression *, DIVariable *> DataLocation = nullptr,
512+
PointerUnion<DIExpression *, DIVariable *> Associated = nullptr,
513+
PointerUnion<DIExpression *, DIVariable *> Allocated = nullptr,
514+
PointerUnion<DIExpression *, DIVariable *> Rank = nullptr);
499515

500516
/// Create debugging information entry for a vector type.
501517
/// \param Size Array size.

llvm/lib/IR/DIBuilder.cpp

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -525,12 +525,24 @@ DICompositeType *DIBuilder::createEnumerationType(
525525
return CTy;
526526
}
527527

528-
DICompositeType *DIBuilder::createArrayType(uint64_t Size,
529-
uint32_t AlignInBits, DIType *Ty,
530-
DINodeArray Subscripts) {
531-
auto *R = DICompositeType::get(VMContext, dwarf::DW_TAG_array_type, "",
532-
nullptr, 0, nullptr, Ty, Size, AlignInBits, 0,
533-
DINode::FlagZero, Subscripts, 0, nullptr);
528+
DICompositeType *DIBuilder::createArrayType(
529+
uint64_t Size, uint32_t AlignInBits, DIType *Ty, DINodeArray Subscripts,
530+
PointerUnion<DIExpression *, DIVariable *> DL,
531+
PointerUnion<DIExpression *, DIVariable *> AS,
532+
PointerUnion<DIExpression *, DIVariable *> AL,
533+
PointerUnion<DIExpression *, DIVariable *> RK) {
534+
auto *R = DICompositeType::get(
535+
VMContext, dwarf::DW_TAG_array_type, "", nullptr, 0,
536+
nullptr, Ty, Size, AlignInBits, 0, DINode::FlagZero,
537+
Subscripts, 0, nullptr, nullptr, "", nullptr,
538+
DL.is<DIExpression *>() ? (Metadata *)DL.get<DIExpression *>()
539+
: (Metadata *)DL.get<DIVariable *>(),
540+
AS.is<DIExpression *>() ? (Metadata *)AS.get<DIExpression *>()
541+
: (Metadata *)AS.get<DIVariable *>(),
542+
AL.is<DIExpression *>() ? (Metadata *)AL.get<DIExpression *>()
543+
: (Metadata *)AL.get<DIVariable *>(),
544+
RK.is<DIExpression *>() ? (Metadata *)RK.get<DIExpression *>()
545+
: (Metadata *)RK.get<DIVariable *>());
534546
trackIfUnresolved(R);
535547
return R;
536548
}

llvm/unittests/IR/DebugInfoTest.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "llvm/IR/DebugInfo.h"
10+
#include "llvm/IR/DIBuilder.h"
1011
#include "llvm/AsmParser/Parser.h"
1112
#include "llvm/IR/DebugInfoMetadata.h"
1213
#include "llvm/IR/IntrinsicInst.h"
@@ -185,4 +186,44 @@ TEST(MetadataTest, DeleteInstUsedByDbgValue) {
185186
EXPECT_TRUE(isa<UndefValue>(DVIs[0]->getValue()));
186187
}
187188

189+
TEST(DIBuilder, CreateFortranArrayTypeWithAttributes) {
190+
LLVMContext Ctx;
191+
std::unique_ptr<Module> M(new Module("MyModule", Ctx));
192+
DIBuilder DIB(*M);
193+
194+
DISubrange *Subrange = DIB.getOrCreateSubrange(1,1);
195+
SmallVector<Metadata*, 4> Subranges;
196+
Subranges.push_back(Subrange);
197+
DINodeArray Subscripts = DIB.getOrCreateArray(Subranges);
198+
199+
auto getDIExpression = [&DIB](int offset) {
200+
SmallVector<uint64_t, 4> ops;
201+
ops.push_back(llvm::dwarf::DW_OP_push_object_address);
202+
DIExpression::appendOffset(ops, offset);
203+
ops.push_back(llvm::dwarf::DW_OP_deref);
204+
205+
return DIB.createExpression(ops);
206+
};
207+
208+
DIFile *F = DIB.createFile("main.c", "/");
209+
DICompileUnit *CU = DIB.createCompileUnit(
210+
dwarf::DW_LANG_C, DIB.createFile("main.c", "/"), "llvm-c", true, "", 0);
211+
212+
DIVariable *DataLocation =
213+
DIB.createTempGlobalVariableFwdDecl(CU, "dl", "_dl", F, 1, nullptr, true);
214+
DIExpression *Associated = getDIExpression(1);
215+
DIExpression *Allocated = getDIExpression(2);
216+
DIExpression *Rank = DIB.createConstantValueExpression(3);
217+
218+
DICompositeType *ArrayType = DIB.createArrayType(0, 0, nullptr, Subscripts,
219+
DataLocation, Associated,
220+
Allocated, Rank);
221+
222+
EXPECT_TRUE(isa_and_nonnull<DICompositeType>(ArrayType));
223+
EXPECT_EQ(ArrayType->getRawDataLocation(), DataLocation);
224+
EXPECT_EQ(ArrayType->getRawAssociated(), Associated);
225+
EXPECT_EQ(ArrayType->getRawAllocated(), Allocated);
226+
EXPECT_EQ(ArrayType->getRawRank(), Rank);
227+
}
228+
188229
} // end namespace

0 commit comments

Comments
 (0)