[CodeView] Fortran debug info emission in Code View.

Differential Revision: https://reviews.llvm.org/D112826
This commit is contained in:
Chih-Ping Chen 2021-10-29 11:46:53 -04:00
parent 5c8d3053fa
commit 2ed29d87ef
3 changed files with 263 additions and 53 deletions

View File

@ -561,6 +561,44 @@ void CodeViewDebug::emitCodeViewMagicVersion() {
OS.emitInt32(COFF::DEBUG_SECTION_MAGIC);
}
static SourceLanguage MapDWLangToCVLang(unsigned DWLang) {
switch (DWLang) {
case dwarf::DW_LANG_C:
case dwarf::DW_LANG_C89:
case dwarf::DW_LANG_C99:
case dwarf::DW_LANG_C11:
case dwarf::DW_LANG_ObjC:
return SourceLanguage::C;
case dwarf::DW_LANG_C_plus_plus:
case dwarf::DW_LANG_C_plus_plus_03:
case dwarf::DW_LANG_C_plus_plus_11:
case dwarf::DW_LANG_C_plus_plus_14:
return SourceLanguage::Cpp;
case dwarf::DW_LANG_Fortran77:
case dwarf::DW_LANG_Fortran90:
case dwarf::DW_LANG_Fortran95:
case dwarf::DW_LANG_Fortran03:
case dwarf::DW_LANG_Fortran08:
return SourceLanguage::Fortran;
case dwarf::DW_LANG_Pascal83:
return SourceLanguage::Pascal;
case dwarf::DW_LANG_Cobol74:
case dwarf::DW_LANG_Cobol85:
return SourceLanguage::Cobol;
case dwarf::DW_LANG_Java:
return SourceLanguage::Java;
case dwarf::DW_LANG_D:
return SourceLanguage::D;
case dwarf::DW_LANG_Swift:
return SourceLanguage::Swift;
default:
// There's no CodeView representation for this language, and CV doesn't
// have an "unknown" option for the language field, so we'll use MASM,
// as it's very low level.
return SourceLanguage::Masm;
}
}
void CodeViewDebug::beginModule(Module *M) {
// If module doesn't have named metadata anchors or COFF debug section
// is not available, skip any debug info related stuff.
@ -574,6 +612,13 @@ void CodeViewDebug::beginModule(Module *M) {
TheCPU = mapArchToCVCPUType(Triple(M->getTargetTriple()).getArch());
// Get the current source language.
NamedMDNode *CUs = MMI->getModule()->getNamedMetadata("llvm.dbg.cu");
const MDNode *Node = *CUs->operands().begin();
const auto *CU = cast<DICompileUnit>(Node);
CurrentSourceLanguage = MapDWLangToCVLang(CU->getSourceLanguage());
collectGlobalVariableInfo();
// Check if we should emit type record hashes.
@ -731,44 +776,6 @@ void CodeViewDebug::emitTypeGlobalHashes() {
}
}
static SourceLanguage MapDWLangToCVLang(unsigned DWLang) {
switch (DWLang) {
case dwarf::DW_LANG_C:
case dwarf::DW_LANG_C89:
case dwarf::DW_LANG_C99:
case dwarf::DW_LANG_C11:
case dwarf::DW_LANG_ObjC:
return SourceLanguage::C;
case dwarf::DW_LANG_C_plus_plus:
case dwarf::DW_LANG_C_plus_plus_03:
case dwarf::DW_LANG_C_plus_plus_11:
case dwarf::DW_LANG_C_plus_plus_14:
return SourceLanguage::Cpp;
case dwarf::DW_LANG_Fortran77:
case dwarf::DW_LANG_Fortran90:
case dwarf::DW_LANG_Fortran95:
case dwarf::DW_LANG_Fortran03:
case dwarf::DW_LANG_Fortran08:
return SourceLanguage::Fortran;
case dwarf::DW_LANG_Pascal83:
return SourceLanguage::Pascal;
case dwarf::DW_LANG_Cobol74:
case dwarf::DW_LANG_Cobol85:
return SourceLanguage::Cobol;
case dwarf::DW_LANG_Java:
return SourceLanguage::Java;
case dwarf::DW_LANG_D:
return SourceLanguage::D;
case dwarf::DW_LANG_Swift:
return SourceLanguage::Swift;
default:
// There's no CodeView representation for this language, and CV doesn't
// have an "unknown" option for the language field, so we'll use MASM,
// as it's very low level.
return SourceLanguage::Masm;
}
}
namespace {
struct Version {
int Part[4];
@ -798,12 +805,8 @@ void CodeViewDebug::emitCompilerInformation() {
MCSymbol *CompilerEnd = beginSymbolRecord(SymbolKind::S_COMPILE3);
uint32_t Flags = 0;
NamedMDNode *CUs = MMI->getModule()->getNamedMetadata("llvm.dbg.cu");
const MDNode *Node = *CUs->operands().begin();
const auto *CU = cast<DICompileUnit>(Node);
// The low byte of the flags indicates the source language.
Flags = MapDWLangToCVLang(CU->getSourceLanguage());
Flags = CurrentSourceLanguage;
// TODO: Figure out which other flags need to be set.
if (MMI->getModule()->getProfileSummary(/*IsCS*/ false) != nullptr) {
Flags |= static_cast<uint32_t>(CompileSym3Flags::PGO);
@ -815,6 +818,10 @@ void CodeViewDebug::emitCompilerInformation() {
OS.AddComment("CPUType");
OS.emitInt16(static_cast<uint64_t>(TheCPU));
NamedMDNode *CUs = MMI->getModule()->getNamedMetadata("llvm.dbg.cu");
const MDNode *Node = *CUs->operands().begin();
const auto *CU = cast<DICompileUnit>(Node);
StringRef CompilerVersion = CU->getProducer();
Version FrontVer = parseVersion(CompilerVersion);
OS.AddComment("Frontend version");
@ -1574,6 +1581,8 @@ TypeIndex CodeViewDebug::lowerType(const DIType *Ty, const DIType *ClassTy) {
return lowerTypeClass(cast<DICompositeType>(Ty));
case dwarf::DW_TAG_union_type:
return lowerTypeUnion(cast<DICompositeType>(Ty));
case dwarf::DW_TAG_string_type:
return lowerTypeString(cast<DIStringType>(Ty));
case dwarf::DW_TAG_unspecified_type:
if (Ty->getName() == "decltype(nullptr)")
return TypeIndex::NullptrT();
@ -1618,14 +1627,19 @@ TypeIndex CodeViewDebug::lowerTypeArray(const DICompositeType *Ty) {
const DISubrange *Subrange = cast<DISubrange>(Element);
int64_t Count = -1;
// Calculate the count if either LowerBound is absent or is zero and
// either of Count or UpperBound are constant.
auto *LI = Subrange->getLowerBound().dyn_cast<ConstantInt *>();
if (!Subrange->getRawLowerBound() || (LI && (LI->getSExtValue() == 0))) {
if (auto *CI = Subrange->getCount().dyn_cast<ConstantInt*>())
Count = CI->getSExtValue();
else if (auto *UI = Subrange->getUpperBound().dyn_cast<ConstantInt*>())
Count = UI->getSExtValue() + 1; // LowerBound is zero
// If Subrange has a Count field, use it.
// Otherwise, if it has an upperboud, use (upperbound - lowerbound + 1),
// where lowerbound is from the LowerBound field of the Subrange,
// or the language default lowerbound if that field is unspecified.
if (auto *CI = Subrange->getCount().dyn_cast<ConstantInt *>())
Count = CI->getSExtValue();
else if (auto *UI = Subrange->getUpperBound().dyn_cast<ConstantInt *>()) {
// Fortran uses 1 as the default lowerbound; other languages use 0.
int64_t Lowerbound = (moduleIsInFortran()) ? 1 : 0;
auto *LI = Subrange->getLowerBound().dyn_cast<ConstantInt *>();
Lowerbound = (LI) ? LI->getSExtValue() : Lowerbound;
Count = UI->getSExtValue() - Lowerbound + 1;
}
// Forward declarations of arrays without a size and VLAs use a count of -1.
@ -1651,6 +1665,26 @@ TypeIndex CodeViewDebug::lowerTypeArray(const DICompositeType *Ty) {
return ElementTypeIndex;
}
// This function lowers a Fortran character type (DIStringType).
// Note that it handles only the character*n variant (using SizeInBits
// field in DIString to describe the type size) at the moment.
// Other variants (leveraging the StringLength and StringLengthExp
// fields in DIStringType) remain TBD.
TypeIndex CodeViewDebug::lowerTypeString(const DIStringType *Ty) {
TypeIndex CharType = TypeIndex(SimpleTypeKind::NarrowCharacter);
uint64_t ArraySize = Ty->getSizeInBits() >> 3;
StringRef Name = Ty->getName();
// IndexType is size_t, which depends on the bitness of the target.
TypeIndex IndexType = getPointerSizeInBytes() == 8
? TypeIndex(SimpleTypeKind::UInt64Quad)
: TypeIndex(SimpleTypeKind::UInt32Long);
// Create a type of character array of ArraySize.
ArrayRecord AR(CharType, IndexType, ArraySize, Name);
return TypeTable.writeLeafType(AR);
}
TypeIndex CodeViewDebug::lowerTypeBasic(const DIBasicType *Ty) {
TypeIndex Index;
dwarf::TypeKind Kind;
@ -2183,6 +2217,7 @@ void CodeViewDebug::clear() {
TypeIndices.clear();
CompleteTypeIndices.clear();
ScopeGlobals.clear();
CVGlobalVariableOffsets.clear();
}
void CodeViewDebug::collectMemberInfo(ClassInfo &Info,
@ -3068,6 +3103,15 @@ void CodeViewDebug::collectGlobalVariableInfo() {
const DIGlobalVariable *DIGV = GVE->getVariable();
const DIExpression *DIE = GVE->getExpression();
if ((DIE->getNumElements() == 2) &&
(DIE->getElement(0) == dwarf::DW_OP_plus_uconst))
// Record the constant offset for the variable.
//
// A Fortran common block uses this idiom to encode the offset
// of a variable from the common block's starting address.
CVGlobalVariableOffsets.insert(
std::make_pair(DIGV, DIE->getElement(1)));
// Emit constant global variables in a global symbol section.
if (GlobalMap.count(GVE) == 0 && DIE->isConstant()) {
CVGlobalVariable CVGV = {DIGV, DIE};
@ -3232,7 +3276,11 @@ void CodeViewDebug::emitDebugInfoForGlobal(const CVGlobalVariable &CVGV) {
if (const auto *MemberDecl = dyn_cast_or_null<DIDerivedType>(
DIGV->getRawStaticDataMemberDeclaration()))
Scope = MemberDecl->getScope();
std::string QualifiedName = getFullyQualifiedName(Scope, DIGV->getName());
// For Fortran, the scoping portion is elided in its name so that we can
// reference the variable in the command line of the VS debugger.
std::string QualifiedName =
(moduleIsInFortran()) ? std::string(DIGV->getName())
: getFullyQualifiedName(Scope, DIGV->getName());
if (const GlobalVariable *GV =
CVGV.GVInfo.dyn_cast<const GlobalVariable *>()) {
@ -3248,7 +3296,13 @@ void CodeViewDebug::emitDebugInfoForGlobal(const CVGlobalVariable &CVGV) {
OS.AddComment("Type");
OS.emitInt32(getCompleteTypeIndex(DIGV->getType()).getIndex());
OS.AddComment("DataOffset");
OS.EmitCOFFSecRel32(GVSym, /*Offset=*/0);
uint64_t Offset = 0;
if (CVGlobalVariableOffsets.find(DIGV) != CVGlobalVariableOffsets.end())
// Use the offset seen while collecting info on globals.
Offset = CVGlobalVariableOffsets[DIGV];
OS.EmitCOFFSecRel32(GVSym, Offset);
OS.AddComment("Segment");
OS.EmitCOFFSectionIndex(GVSym);
OS.AddComment("Name");

View File

@ -186,6 +186,13 @@ class LLVM_LIBRARY_VISIBILITY CodeViewDebug : public DebugHandlerBase {
};
FunctionInfo *CurFn = nullptr;
codeview::SourceLanguage CurrentSourceLanguage =
codeview::SourceLanguage::Masm;
// This map records the constant offset in DIExpression of the
// DIGlobalVariableExpression referencing the DIGlobalVariable.
DenseMap<const DIGlobalVariable *, uint64_t> CVGlobalVariableOffsets;
// Map used to seperate variables according to the lexical scope they belong
// in. This is populated by recordLocalVariable() before
// collectLexicalBlocks() separates the variables between the FunctionInfo
@ -400,6 +407,7 @@ class LLVM_LIBRARY_VISIBILITY CodeViewDebug : public DebugHandlerBase {
codeview::TypeIndex lowerType(const DIType *Ty, const DIType *ClassTy);
codeview::TypeIndex lowerTypeAlias(const DIDerivedType *Ty);
codeview::TypeIndex lowerTypeArray(const DICompositeType *Ty);
codeview::TypeIndex lowerTypeString(const DIStringType *Ty);
codeview::TypeIndex lowerTypeBasic(const DIBasicType *Ty);
codeview::TypeIndex lowerTypePointer(
const DIDerivedType *Ty,
@ -464,6 +472,11 @@ protected:
/// Gather post-function debug information.
void endFunctionImpl(const MachineFunction *) override;
/// Check if the current module is in Fortran.
bool moduleIsInFortran() {
return CurrentSourceLanguage == codeview::SourceLanguage::Fortran;
}
public:
CodeViewDebug(AsmPrinter *AP);

View File

@ -0,0 +1,143 @@
; RUN: llc < %s -filetype=obj | llvm-readobj - --codeview | FileCheck %s
;
; The IR in this test derives from the following Fortran program:
; program array
; integer array1, array2
; dimension array1(10)
; dimension array2(3:10)
; double precision d
; logical l
; character*6 c
;
; common /com/ d, l, c
;
; array1(1) = 1
; array2(3) = 2
; d = 8.0
; l = .TRUE.
; c = 'oooooo'
; end
;
; CHECK: Array ([[array2_t:.*]]) {
; CHECK-NEXT: TypeLeafKind: LF_ARRAY
; CHECK-NEXT: ElementType: int
; CHECK-NEXT: IndexType: unsigned __int64
; CHECK-NEXT: SizeOf: 32
;
; CHECK: Array ([[array1_t:.*]]) {
; CHECK-NEXT: TypeLeafKind: LF_ARRAY
; CHECK-NEXT: ElementType: int
; CHECK-NEXT: IndexType: unsigned __int64
; CHECK-NEXT: SizeOf: 40
;
; CHECK: Array ([[char_6_t:.*]]) {
; CHECK-NEXT: TypeLeafKind: LF_ARRAY
; CHECK-NEXT: ElementType: char
; CHECK-NEXT: IndexType: unsigned __int64
; CHECK-NEXT: SizeOf: 6
; CHECK-NEXT: CHARACTER_0
;
; CHECK: DataOffset: ARRAY$ARRAY2+0x0
; CHECK-NEXT: Type: [[array2_t]]
; CHECK-NEXT: DisplayName: ARRAY2
; CHECK-NEXT: LinkageName: ARRAY$ARRAY2
;
; CHECK: DataOffset: ARRAY$ARRAY1+0x0
; CHECK-NEXT: Type: [[array1_t]]
; CHECK-NEXT: DisplayName: ARRAY1
; CHECK-NEXT: LinkageName: ARRAY$ARRAY1
;
; CHECK: DataOffset: COM+0x0
; CHECK-NEXT: Type: double
; CHECK-NEXT: DisplayName: D
; CHECK-NEXT: LinkageName: COM
;
; CHECK: DataOffset: COM+0x8
; CHECK-NEXT: Type: __bool32
; CHECK-NEXT: DisplayName: L
; CHECK-NEXT: LinkageName: COM
;
; CHECK: DataOffset: COM+0xC
; CHECK-NEXT: Type: CHARACTER_0 ([[char_6_t]])
; CHECK-NEXT: DisplayName: C
; CHECK-NEXT: LinkageName: COM
; ModuleID = 'fortran-basic.f'
source_filename = "fortran-basic.f"
target datalayout = "e-m:w-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-pc-windows-msvc"
@strlit = internal unnamed_addr constant [6 x i8] c"oooooo"
@COM = common unnamed_addr global [18 x i8] zeroinitializer, align 32, !dbg !0, !dbg !9, !dbg !12
@"ARRAY$ARRAY2" = internal global [8 x i32] zeroinitializer, align 16, !dbg !15
@"ARRAY$ARRAY1" = internal global [10 x i32] zeroinitializer, align 16, !dbg !21
@0 = internal unnamed_addr constant i32 2
; Function Attrs: noinline nounwind optnone uwtable
define void @MAIN__() #0 !dbg !3 {
alloca_0:
%"$io_ctx" = alloca [6 x i64], align 8
%strlit_fetch.1 = load [6 x i8], [6 x i8]* @strlit, align 1, !dbg !39
%func_result = call i32 @for_set_reentrancy(i32* @0), !dbg !39
store i32 1, i32* getelementptr inbounds ([10 x i32], [10 x i32]* @"ARRAY$ARRAY1", i32 0, i32 0), align 1, !dbg !40
store i32 2, i32* getelementptr inbounds ([8 x i32], [8 x i32]* @"ARRAY$ARRAY2", i32 0, i32 0), align 1, !dbg !41
store double 8.000000e+00, double* bitcast ([18 x i8]* @COM to double*), align 1, !dbg !42
store i32 -1, i32* bitcast (i8* getelementptr inbounds ([18 x i8], [18 x i8]* @COM, i32 0, i64 8) to i32*), align 1, !dbg !43
call void @llvm.for.cpystr.i64.i64.i64(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @COM, i32 0, i64 12), i64 6, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @strlit, i32 0, i32 0), i64 3, i64 0, i1 false), !dbg !44
ret void, !dbg !45
}
declare i32 @for_set_reentrancy(i32* nocapture readonly)
; Function Attrs: nounwind readnone speculatable
declare i32* @llvm.intel.subscript.p0i32.i64.i64.p0i32.i64(i8, i64, i64, i32*, i64) #1
; Function Attrs: argmemonly nofree nosync nounwind willreturn
declare void @llvm.for.cpystr.i64.i64.i64(i8* noalias nocapture writeonly, i64, i8* noalias nocapture readonly, i64, i64, i1 immarg) #2
attributes #0 = { noinline nounwind optnone uwtable "frame-pointer"="none" "intel-lang"="fortran" "min-legal-vector-width"="0" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" }
attributes #1 = { nounwind readnone speculatable }
attributes #2 = { argmemonly nofree nosync nounwind willreturn }
!llvm.module.flags = !{!28, !29, !30}
!llvm.dbg.cu = !{!7}
!omp_offload.info = !{}
!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression())
!1 = distinct !DIGlobalVariable(name: "D", linkageName: "COM", scope: !2, file: !4, line: 5, type: !27, isLocal: false, isDefinition: true)
!2 = !DICommonBlock(scope: !3, declaration: null, name: "COM", file: !4, line: 8)
!3 = distinct !DISubprogram(name: "ARRAY", linkageName: "MAIN__", scope: !4, file: !4, line: 1, type: !5, scopeLine: 1, spFlags: DISPFlagDefinition | DISPFlagMainSubprogram, unit: !7, retainedNodes: !26)
!4 = !DIFile(filename: "fortran-basic.f", directory: "d:\\iusers\\cchen15\\examples\\tests\\vsdF-nightly\\vsdF\\opt_none_debug")
!5 = !DISubroutineType(types: !6)
!6 = !{null}
!7 = distinct !DICompileUnit(language: DW_LANG_Fortran95, file: !4, producer: "Intel(R) Fortran 22.0-1034", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, globals: !8, splitDebugInlining: false, nameTableKind: None)
!8 = !{!0, !9, !12, !15, !21}
!9 = !DIGlobalVariableExpression(var: !10, expr: !DIExpression(DW_OP_plus_uconst, 8))
!10 = distinct !DIGlobalVariable(name: "L", linkageName: "COM", scope: !2, file: !4, line: 6, type: !11, isLocal: false, isDefinition: true)
!11 = !DIBasicType(name: "LOGICAL*4", size: 32, encoding: DW_ATE_boolean)
!12 = !DIGlobalVariableExpression(var: !13, expr: !DIExpression(DW_OP_plus_uconst, 12))
!13 = distinct !DIGlobalVariable(name: "C", linkageName: "COM", scope: !2, file: !4, line: 7, type: !14, isLocal: false, isDefinition: true)
!14 = !DIStringType(name: "CHARACTER_0", size: 48)
!15 = !DIGlobalVariableExpression(var: !16, expr: !DIExpression())
!16 = distinct !DIGlobalVariable(name: "ARRAY2", linkageName: "ARRAY$ARRAY2", scope: !3, file: !4, line: 2, type: !17, isLocal: true, isDefinition: true)
!17 = !DICompositeType(tag: DW_TAG_array_type, baseType: !18, elements: !19)
!18 = !DIBasicType(name: "INTEGER*4", size: 32, encoding: DW_ATE_signed)
!19 = !{!20}
!20 = !DISubrange(lowerBound: 3, upperBound: 10)
!21 = !DIGlobalVariableExpression(var: !22, expr: !DIExpression())
!22 = distinct !DIGlobalVariable(name: "ARRAY1", linkageName: "ARRAY$ARRAY1", scope: !3, file: !4, line: 2, type: !23, isLocal: true, isDefinition: true)
!23 = !DICompositeType(tag: DW_TAG_array_type, baseType: !18, elements: !24)
!24 = !{!25}
!25 = !DISubrange(count: 10, lowerBound: 1)
!26 = !{}
!27 = !DIBasicType(name: "REAL*8", size: 64, encoding: DW_ATE_float)
!28 = !{i32 7, !"PIC Level", i32 2}
!29 = !{i32 2, !"Debug Info Version", i32 3}
!30 = !{i32 2, !"CodeView", i32 1}
!39 = !DILocation(line: 1, column: 10, scope: !3)
!40 = !DILocation(line: 9, column: 9, scope: !3)
!41 = !DILocation(line: 10, column: 9, scope: !3)
!42 = !DILocation(line: 11, column: 9, scope: !3)
!43 = !DILocation(line: 12, column: 9, scope: !3)
!44 = !DILocation(line: 13, column: 9, scope: !3)
!45 = !DILocation(line: 14, column: 2, scope: !3)