[flang-commits] [flang] [flang][debug] Support assumed shape arrays. (PR #94644)
Abid Qadeer via flang-commits
flang-commits at lists.llvm.org
Mon Jun 10 04:16:59 PDT 2024
https://github.com/abidh updated https://github.com/llvm/llvm-project/pull/94644
>From 421180eb4a98a010d23cbe344f82274c01769b39 Mon Sep 17 00:00:00 2001
From: Abid Qadeer <haqadeer at amd.com>
Date: Wed, 5 Jun 2024 17:38:44 +0100
Subject: [PATCH 1/4] [flang][debug] Support assume shape arrays.
This PR generates dwarf to extract the information about the arrays
from descriptor. To calculate the offset of the fields in the
descriptor, we are adding up the sizes of the fields above. It seems to
work ok for 64-bit target. Will have to see if some changes are
required for 32-bits targets.
As we use data layout now, some tests needed to be adjusted to have
a dummy data layout to avoid failure.
With this change in place, GDB is able show the assumed shape arrays
correctly.
subroutine ff(n, m, arr)
integer n, m
integer :: arr(:, :)
print *, arr
do i = 1, n
do j = 1, m
arr(j, i) = (i * 5) + j + 10
end do
end do
print *, arr
end subroutine ff
Breakpoint 1, ff (n=4, m=3, arr=...) at test1.f90:13
13 print *, arr
(gdb) p arr
$1 = ((6, 7, 8, 9) (11, 12, 13, 14) (16, 17, 18, 19))
(gdb) ptype arr
type = integer (4,3)
(gdb) c
Continuing.
6 7 8 9 11 12 13 14 16 17 18 19
---
.../Optimizer/CodeGen/DescriptorModel.h | 0
flang/lib/Optimizer/CodeGen/TypeConverter.cpp | 2 +-
.../Transforms/DebugTypeGenerator.cpp | 138 +++++++++++++++++-
.../Optimizer/Transforms/DebugTypeGenerator.h | 12 ++
.../Integration/debug-assumed-shape-array.f90 | 13 ++
flang/test/Transforms/debug-90683.fir | 2 +-
.../Transforms/debug-assumed-shape-array.fir | 16 ++
flang/test/Transforms/debug-complex-1.fir | 2 +-
.../Transforms/debug-fixed-array-type.fir | 2 +-
.../Transforms/debug-line-table-existing.fir | 2 +-
.../Transforms/debug-line-table-inc-file.fir | 4 +-
.../debug-line-table-inc-same-file.fir | 2 +-
flang/test/Transforms/debug-line-table.fir | 2 +-
flang/test/Transforms/debug-module-1.fir | 2 +-
14 files changed, 188 insertions(+), 11 deletions(-)
rename flang/{lib => include/flang}/Optimizer/CodeGen/DescriptorModel.h (100%)
create mode 100644 flang/test/Integration/debug-assumed-shape-array.f90
create mode 100644 flang/test/Transforms/debug-assumed-shape-array.fir
diff --git a/flang/lib/Optimizer/CodeGen/DescriptorModel.h b/flang/include/flang/Optimizer/CodeGen/DescriptorModel.h
similarity index 100%
rename from flang/lib/Optimizer/CodeGen/DescriptorModel.h
rename to flang/include/flang/Optimizer/CodeGen/DescriptorModel.h
diff --git a/flang/lib/Optimizer/CodeGen/TypeConverter.cpp b/flang/lib/Optimizer/CodeGen/TypeConverter.cpp
index 07d3bd713ce45..501a36f5b68ba 100644
--- a/flang/lib/Optimizer/CodeGen/TypeConverter.cpp
+++ b/flang/lib/Optimizer/CodeGen/TypeConverter.cpp
@@ -13,9 +13,9 @@
#define DEBUG_TYPE "flang-type-conversion"
#include "flang/Optimizer/CodeGen/TypeConverter.h"
-#include "DescriptorModel.h"
#include "flang/Common/Fortran.h"
#include "flang/Optimizer/Builder/Todo.h" // remove when TODO's are done
+#include "flang/Optimizer/CodeGen/DescriptorModel.h"
#include "flang/Optimizer/CodeGen/TBAABuilder.h"
#include "flang/Optimizer/CodeGen/Target.h"
#include "flang/Optimizer/Dialect/FIRType.h"
diff --git a/flang/lib/Optimizer/Transforms/DebugTypeGenerator.cpp b/flang/lib/Optimizer/Transforms/DebugTypeGenerator.cpp
index a174f2c2bc4bf..858f58b8e2703 100644
--- a/flang/lib/Optimizer/Transforms/DebugTypeGenerator.cpp
+++ b/flang/lib/Optimizer/Transforms/DebugTypeGenerator.cpp
@@ -13,6 +13,10 @@
#define DEBUG_TYPE "flang-debug-type-generator"
#include "DebugTypeGenerator.h"
+#include "flang/Optimizer/CodeGen/DescriptorModel.h"
+#include "flang/Optimizer/CodeGen/TypeConverter.h"
+#include "flang/Optimizer/Support/DataLayout.h"
+#include "mlir/Pass/Pass.h"
#include "llvm/ADT/ScopeExit.h"
#include "llvm/BinaryFormat/Dwarf.h"
#include "llvm/Support/Debug.h"
@@ -22,6 +26,60 @@ namespace fir {
DebugTypeGenerator::DebugTypeGenerator(mlir::ModuleOp m)
: module(m), kindMapping(getKindMapping(m)) {
LLVM_DEBUG(llvm::dbgs() << "DITypeAttr generator\n");
+
+ std::optional<mlir::DataLayout> dl =
+ fir::support::getOrSetDataLayout(module, /*allowDefaultLayout=*/true);
+ if (!dl)
+ mlir::emitError(module.getLoc(), "Missing data layout attribute in module");
+
+ mlir::MLIRContext *context = module.getContext();
+
+ // The debug information requires the offset of certain fields in the
+ // descriptors like lower_bound and extent for each dimension. The code
+ // below uses getDescFieldTypeModel to get the type representing each field
+ // and then use data layout to get its size. It adds the size to get the
+ // offset.
+ // As has been mentioned in DescriptorModel.h that code may be confusing
+ // host for the target in calculating the type of the descriptor fields. But
+ // debug info is using similar logic to what codegen is doing so it will
+ // atleast be representing the generated code correctly.
+ // My testing for a 32-bit shows that base_addr* is correctly given as a
+ // 32-bit entity. The index types are 64-bit so I am a bit uncertain how
+ // the alignment will effect the calculation of offsets in that case.
+
+ // base_addr*
+ dimsOffset =
+ dl->getTypeSizeInBits(getDescFieldTypeModel<kAddrPosInBox>()(context));
+
+ // elem_len
+ dimsOffset +=
+ dl->getTypeSizeInBits(getDescFieldTypeModel<kElemLenPosInBox>()(context));
+
+ // version
+ dimsOffset +=
+ dl->getTypeSizeInBits(getDescFieldTypeModel<kVersionPosInBox>()(context));
+
+ // rank
+ dimsOffset +=
+ dl->getTypeSizeInBits(getDescFieldTypeModel<kRankPosInBox>()(context));
+
+ // type
+ dimsOffset +=
+ dl->getTypeSizeInBits(getDescFieldTypeModel<kTypePosInBox>()(context));
+
+ // attribute
+ dimsOffset += dl->getTypeSizeInBits(
+ getDescFieldTypeModel<kAttributePosInBox>()(context));
+
+ // f18Addendum
+ dimsOffset += dl->getTypeSizeInBits(
+ getDescFieldTypeModel<kF18AddendumPosInBox>()(context));
+
+ // dims
+ dimsSize =
+ dl->getTypeSizeInBits(getDescFieldTypeModel<kDimsPosInBox>()(context));
+ dimsOffset /= 8;
+ dimsSize /= 8;
}
static mlir::LLVM::DITypeAttr genBasicType(mlir::MLIRContext *context,
@@ -37,10 +95,82 @@ static mlir::LLVM::DITypeAttr genPlaceholderType(mlir::MLIRContext *context) {
llvm::dwarf::DW_ATE_signed);
}
+mlir::LLVM::DITypeAttr DebugTypeGenerator::convertBoxedSequenceType(
+ fir::SequenceType seqTy, mlir::LLVM::DIFileAttr fileAttr,
+ mlir::LLVM::DIScopeAttr scope, mlir::Location loc, bool genAllocated,
+ bool genAssociated) {
+
+ mlir::MLIRContext *context = module.getContext();
+ // FIXME: Assumed rank arrays not supported yet
+ if (seqTy.hasUnknownShape())
+ return genPlaceholderType(context);
+
+ llvm::SmallVector<mlir::LLVM::DIExpressionElemAttr> ops;
+ auto addOp = [&](unsigned opc, llvm::ArrayRef<uint64_t> vals) {
+ ops.push_back(mlir::LLVM::DIExpressionElemAttr::get(context, opc, vals));
+ };
+
+ addOp(llvm::dwarf::DW_OP_push_object_address, {});
+ addOp(llvm::dwarf::DW_OP_deref, {});
+
+ // dataLocation = *base_addr
+ mlir::LLVM::DIExpressionAttr dataLocation =
+ mlir::LLVM::DIExpressionAttr::get(context, ops);
+ addOp(llvm::dwarf::DW_OP_lit0, {});
+ addOp(llvm::dwarf::DW_OP_ne, {});
+
+ // allocated = associated = (*base_addr != 0)
+ mlir::LLVM::DIExpressionAttr valid =
+ mlir::LLVM::DIExpressionAttr::get(context, ops);
+ mlir::LLVM::DIExpressionAttr associated = genAllocated ? valid : nullptr;
+ mlir::LLVM::DIExpressionAttr allocated = genAssociated ? valid : nullptr;
+ ops.clear();
+
+ llvm::SmallVector<mlir::LLVM::DINodeAttr> elements;
+ mlir::LLVM::DITypeAttr elemTy =
+ convertType(seqTy.getEleTy(), fileAttr, scope, loc);
+ unsigned offset = dimsOffset;
+ const unsigned indexSize = dimsSize / 3;
+ for ([[maybe_unused]] auto _ : seqTy.getShape()) {
+ // For each dimension, find the offset of count and lower bound in the
+ // descriptor and generate the dwarf expression to extract it.
+ // FIXME: If `indexSize` happens to be bigger than address size on the
+ // system then we may have to change 'DW_OP_deref' here.
+ addOp(llvm::dwarf::DW_OP_push_object_address, {});
+ addOp(llvm::dwarf::DW_OP_plus_uconst,
+ {offset + (indexSize * kDimExtentPos)});
+ addOp(llvm::dwarf::DW_OP_deref, {});
+ // count[i] = *(base_addr + offset + (indexSize * kDimExtentPos))
+ // where 'offset' is dimsOffset + (i * dimsSize)
+ mlir::LLVM::DIExpressionAttr countAttr =
+ mlir::LLVM::DIExpressionAttr::get(context, ops);
+ ops.clear();
+
+ addOp(llvm::dwarf::DW_OP_push_object_address, {});
+ addOp(llvm::dwarf::DW_OP_plus_uconst,
+ {offset + (indexSize * kDimLowerBoundPos)});
+ addOp(llvm::dwarf::DW_OP_deref, {});
+ // lower_bound[i] = *(base_addr + offset + (indexSize * kDimLowerBoundPos))
+ mlir::LLVM::DIExpressionAttr lowerAttr =
+ mlir::LLVM::DIExpressionAttr::get(context, ops);
+ ops.clear();
+
+ offset += dimsSize;
+ mlir::LLVM::DISubrangeAttr subrangeTy = mlir::LLVM::DISubrangeAttr::get(
+ context, nullptr, lowerAttr, countAttr, nullptr);
+ elements.push_back(subrangeTy);
+ }
+ return mlir::LLVM::DICompositeTypeAttr::get(
+ context, llvm::dwarf::DW_TAG_array_type, /*recursive id*/ {},
+ /* name */ nullptr, /* file */ nullptr, /* line */ 0,
+ /* scope */ nullptr, elemTy, mlir::LLVM::DIFlags::Zero,
+ /* sizeInBits */ 0, /*alignInBits*/ 0, elements, dataLocation,
+ /* rank */ nullptr, allocated, associated);
+}
+
mlir::LLVM::DITypeAttr DebugTypeGenerator::convertSequenceType(
fir::SequenceType seqTy, mlir::LLVM::DIFileAttr fileAttr,
mlir::LLVM::DIScopeAttr scope, mlir::Location loc) {
-
mlir::MLIRContext *context = module.getContext();
// FIXME: Only fixed sizes arrays handled at the moment.
if (seqTy.hasDynamicExtents())
@@ -112,6 +242,12 @@ DebugTypeGenerator::convertType(mlir::Type Ty, mlir::LLVM::DIFileAttr fileAttr,
bitWidth * 2, llvm::dwarf::DW_ATE_complex_float);
} else if (auto seqTy = mlir::dyn_cast_or_null<fir::SequenceType>(Ty)) {
return convertSequenceType(seqTy, fileAttr, scope, loc);
+ } else if (auto boxTy = mlir::dyn_cast_or_null<fir::BoxType>(Ty)) {
+ auto elTy = boxTy.getElementType();
+ if (auto seqTy = mlir::dyn_cast_or_null<fir::SequenceType>(elTy))
+ return convertBoxedSequenceType(seqTy, fileAttr, scope, loc, false,
+ false);
+ return genPlaceholderType(context);
} else {
// FIXME: These types are currently unhandled. We are generating a
// placeholder type to allow us to test supported bits.
diff --git a/flang/lib/Optimizer/Transforms/DebugTypeGenerator.h b/flang/lib/Optimizer/Transforms/DebugTypeGenerator.h
index 963c919d66825..45e2371af3432 100644
--- a/flang/lib/Optimizer/Transforms/DebugTypeGenerator.h
+++ b/flang/lib/Optimizer/Transforms/DebugTypeGenerator.h
@@ -35,8 +35,20 @@ class DebugTypeGenerator {
mlir::LLVM::DIFileAttr fileAttr,
mlir::LLVM::DIScopeAttr scope,
mlir::Location loc);
+
+ /// The 'genAllocated' is true when we want to generate 'allocated' field
+ /// in the DICompositeType. It is needed for the allocatable arrays.
+ /// Similarly, 'genAssociated' is used with 'pointer' type to generate
+ /// 'associated' field.
+ mlir::LLVM::DITypeAttr
+ convertBoxedSequenceType(fir::SequenceType seqTy,
+ mlir::LLVM::DIFileAttr fileAttr,
+ mlir::LLVM::DIScopeAttr scope, mlir::Location loc,
+ bool genAllocated, bool genAssociated);
mlir::ModuleOp module;
KindMapping kindMapping;
+ size_t dimsSize;
+ size_t dimsOffset;
};
} // namespace fir
diff --git a/flang/test/Integration/debug-assumed-shape-array.f90 b/flang/test/Integration/debug-assumed-shape-array.f90
new file mode 100644
index 0000000000000..7b0801c12dba1
--- /dev/null
+++ b/flang/test/Integration/debug-assumed-shape-array.f90
@@ -0,0 +1,13 @@
+! RUN: %flang_fc1 -emit-llvm -debug-info-kind=standalone %s -o - | FileCheck %s
+
+subroutine ff(arr)
+ implicit none
+ integer :: arr(:, :)
+ return arr(1,1)
+end subroutine ff
+
+! CHECK-DAG: !DICompositeType(tag: DW_TAG_array_type{{.*}}elements: ![[ELEMS:[0-9]+]], dataLocation: !DIExpression(DW_OP_push_object_address, DW_OP_deref))
+! CHECK-DAG: ![[ELEMS]] = !{![[ELEM1:[0-9]+]], ![[ELEM2:[0-9]+]]}
+! CHECK-DAG: ![[ELEM1]] = !DISubrange(lowerBound: !DIExpression(DW_OP_push_object_address, DW_OP_plus_uconst, 24, DW_OP_deref), upperBound: !DIExpression(DW_OP_push_object_address, DW_OP_plus_uconst, 32, DW_OP_deref))
+! CHECK-DAG: ![[ELEM2]] = !DISubrange(lowerBound: !DIExpression(DW_OP_push_object_address, DW_OP_plus_uconst, 48, DW_OP_deref), upperBound: !DIExpression(DW_OP_push_object_address, DW_OP_plus_uconst, 56, DW_OP_deref))
+
diff --git a/flang/test/Transforms/debug-90683.fir b/flang/test/Transforms/debug-90683.fir
index 9da0e5347d3f8..cc6929c10411f 100644
--- a/flang/test/Transforms/debug-90683.fir
+++ b/flang/test/Transforms/debug-90683.fir
@@ -2,7 +2,7 @@
// This test checks that debug information for fir.real type works ok.
-module attributes {} {
+module attributes {dlti.dl_spec = #dlti.dl_spec<>} {
func.func @_QPfn1(%arg0: !fir.ref<!fir.complex<8>> {fir.bindc_name = "a"} ) {
%0 = fir.declare %arg0 {uniq_name = "_QFfn1Ea"} : (!fir.ref<!fir.complex<8>>) -> !fir.ref<!fir.complex<8>>
%1 = fir.alloca f32 {bindc_name = "abserror", uniq_name = "_QFfn1Eabserror"}
diff --git a/flang/test/Transforms/debug-assumed-shape-array.fir b/flang/test/Transforms/debug-assumed-shape-array.fir
new file mode 100644
index 0000000000000..00dec9b318c81
--- /dev/null
+++ b/flang/test/Transforms/debug-assumed-shape-array.fir
@@ -0,0 +1,16 @@
+// RUN: fir-opt --add-debug-info --mlir-print-debuginfo %s | FileCheck %s
+
+module attributes {dlti.dl_spec = #dlti.dl_spec<#dlti.dl_entry<i64, dense<64> : vector<2xi64>>, #dlti.dl_entry<!llvm.ptr<272>, dense<64> : vector<4xi64>>, #dlti.dl_entry<!llvm.ptr<271>, dense<32> : vector<4xi64>>, #dlti.dl_entry<!llvm.ptr<270>, dense<32> : vector<4xi64>>, #dlti.dl_entry<f128, dense<128> : vector<2xi64>>, #dlti.dl_entry<f80, dense<128> : vector<2xi64>>, #dlti.dl_entry<i128, dense<128> : vector<2xi64>>, #dlti.dl_entry<i8, dense<8> : vector<2xi64>>, #dlti.dl_entry<!llvm.ptr, dense<64> : vector<4xi64>>, #dlti.dl_entry<i1, dense<8> : vector<2xi64>>, #dlti.dl_entry<f16, dense<16> : vector<2xi64>>, #dlti.dl_entry<f64, dense<64> : vector<2xi64>>, #dlti.dl_entry<i32, dense<32> : vector<2xi64>>, #dlti.dl_entry<i16, dense<16> : vector<2xi64>>, #dlti.dl_entry<"dlti.stack_alignment", 128 : i64>, #dlti.dl_entry<"dlti.endianness", "little">>, fir.defaultkind = "a1c4d8i4l4r4", fir.kindmap = "", llvm.data_layout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"} {
+ func.func @ff_(%arg0: !fir.box<!fir.array<?x?xi32>> {fir.bindc_name = "arr"} ) {
+ %0 = fir.undefined !fir.dscope
+ %1 = fircg.ext_declare %arg0 dummy_scope %0 {uniq_name = "_QFffEarr"} : (!fir.box<!fir.array<?x?xi32>>, !fir.dscope) -> !fir.box<!fir.array<?x?xi32>> loc(#loc1)
+ return
+ } loc(#loc2)
+}
+#loc1 = loc("test1.f90":1:1)
+#loc2 = loc("test1.f90":3:16)
+
+// CHECK: #llvm.di_composite_type<tag = DW_TAG_array_type
+// CHECK-SAME: elements = #llvm.di_subrange<lowerBound = #llvm.di_expression<[DW_OP_push_object_address, DW_OP_plus_uconst(24), DW_OP_deref]>, upperBound = #llvm.di_expression<[DW_OP_push_object_address, DW_OP_plus_uconst(32), DW_OP_deref]>>
+// CHECK-SAME: #llvm.di_subrange<lowerBound = #llvm.di_expression<[DW_OP_push_object_address, DW_OP_plus_uconst(48), DW_OP_deref]>, upperBound = #llvm.di_expression<[DW_OP_push_object_address, DW_OP_plus_uconst(56), DW_OP_deref]>>
+// CHECK-SAME: dataLocation = <[DW_OP_push_object_address, DW_OP_deref]>>
diff --git a/flang/test/Transforms/debug-complex-1.fir b/flang/test/Transforms/debug-complex-1.fir
index a3cbd767d8a58..cc742d3b183bb 100644
--- a/flang/test/Transforms/debug-complex-1.fir
+++ b/flang/test/Transforms/debug-complex-1.fir
@@ -3,7 +3,7 @@
// check conversion of complex type of different size. Both fir and mlir
// variants are checked.
-module attributes {fir.defaultkind = "a1c4d8i4l4r4", fir.kindmap = "", llvm.target_triple = "native"} {
+module attributes {dlti.dl_spec = #dlti.dl_spec<>} {
func.func @test1(%x : !fir.complex<4>) -> !fir.complex<8> {
%1 = fir.convert %x : (!fir.complex<4>) -> !fir.complex<8>
return %1 : !fir.complex<8>
diff --git a/flang/test/Transforms/debug-fixed-array-type.fir b/flang/test/Transforms/debug-fixed-array-type.fir
index 401c725411831..d4ed0b9702089 100644
--- a/flang/test/Transforms/debug-fixed-array-type.fir
+++ b/flang/test/Transforms/debug-fixed-array-type.fir
@@ -1,6 +1,6 @@
// RUN: fir-opt --add-debug-info --mlir-print-debuginfo %s | FileCheck %s
-module attributes {} {
+module attributes {dlti.dl_spec = #dlti.dl_spec<>} {
func.func @_QQmain() attributes {fir.bindc_name = "mn"} {
%c7 = arith.constant 7 : index
%c8 = arith.constant 8 : index
diff --git a/flang/test/Transforms/debug-line-table-existing.fir b/flang/test/Transforms/debug-line-table-existing.fir
index 534278ebc972d..0e006303c8a81 100644
--- a/flang/test/Transforms/debug-line-table-existing.fir
+++ b/flang/test/Transforms/debug-line-table-existing.fir
@@ -3,7 +3,7 @@
// REQUIRES: system-linux
// Test that there are no changes to a function with existed fused loc debug
-module attributes {} {
+module attributes {dlti.dl_spec = #dlti.dl_spec<>} {
func.func @_QPs1() {
return loc(#loc1)
} loc(#loc2)
diff --git a/flang/test/Transforms/debug-line-table-inc-file.fir b/flang/test/Transforms/debug-line-table-inc-file.fir
index 9370c138fd42f..065039b59c5ae 100644
--- a/flang/test/Transforms/debug-line-table-inc-file.fir
+++ b/flang/test/Transforms/debug-line-table-inc-file.fir
@@ -3,7 +3,7 @@
// REQUIRES: system-linux
// Test for included functions that have a different debug location than the current file
-module attributes {} {
+module attributes {dlti.dl_spec = #dlti.dl_spec<>} {
func.func @_QPsinc() {
return loc(#loc2)
} loc(#loc1)
@@ -19,7 +19,7 @@ module attributes {} {
#loc4 = loc("/home/user01/llvm-project/build_release/simple.f90":4:3)
#loc5 = loc("/home/user01/llvm-project/build_release/simple.f90":5:1)
-// CHECK: module {
+// CHECK: module
// CHECK: func.func @_QPsinc() {
// CHECK: } loc(#[[FUSED_LOC_INC_FILE:.*]])
// CHECK: func.func @_QQmain() {
diff --git a/flang/test/Transforms/debug-line-table-inc-same-file.fir b/flang/test/Transforms/debug-line-table-inc-same-file.fir
index 4836f2e21dd9d..bcaf449798231 100644
--- a/flang/test/Transforms/debug-line-table-inc-same-file.fir
+++ b/flang/test/Transforms/debug-line-table-inc-same-file.fir
@@ -4,7 +4,7 @@
// Test that there is only one FileAttribute generated for multiple functions
// in the same file.
-module attributes {} {
+module attributes {dlti.dl_spec = #dlti.dl_spec<>} {
func.func @_QPs1() {
return loc(#loc2)
} loc(#loc1)
diff --git a/flang/test/Transforms/debug-line-table.fir b/flang/test/Transforms/debug-line-table.fir
index 8a72ca2a856a7..d6e54fd1ac467 100644
--- a/flang/test/Transforms/debug-line-table.fir
+++ b/flang/test/Transforms/debug-line-table.fir
@@ -3,7 +3,7 @@
// RUN: fir-opt --add-debug-info="debug-level=LineTablesOnly" --mlir-print-debuginfo %s | FileCheck %s --check-prefix=LINETABLE
// RUN: fir-opt --add-debug-info="is-optimized=true" --mlir-print-debuginfo %s | FileCheck %s --check-prefix=OPT
-module attributes { fir.defaultkind = "a1c4d8i4l4r4", fir.kindmap = "", llvm.data_layout = "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128", llvm.target_triple = "aarch64-unknown-linux-gnu"} {
+module attributes {dlti.dl_spec = #dlti.dl_spec<>} {
func.func @_QPsb() {
return loc(#loc_sb)
} loc(#loc_sb)
diff --git a/flang/test/Transforms/debug-module-1.fir b/flang/test/Transforms/debug-module-1.fir
index 822ae01b99aa7..71457d32b1596 100644
--- a/flang/test/Transforms/debug-module-1.fir
+++ b/flang/test/Transforms/debug-module-1.fir
@@ -1,7 +1,7 @@
// RUN: fir-opt --add-debug-info --mlir-print-debuginfo %s | FileCheck %s
-module attributes {} {
+module attributes {dlti.dl_spec = #dlti.dl_spec<>} {
fir.global @_QMhelperEgli : i32 {
%0 = fir.zero_bits i32
fir.has_value %0 : i32
>From e866ca1b7ae0b0352a648c06c222db176384ab70 Mon Sep 17 00:00:00 2001
From: Abid Qadeer <haqadeer at amd.com>
Date: Fri, 7 Jun 2024 11:32:17 +0100
Subject: [PATCH 2/4] Fix build error.
Add static to getModel() and constexpr to template specializations.
---
.../flang/Optimizer/CodeGen/DescriptorModel.h | 24 +++++++++----------
1 file changed, 12 insertions(+), 12 deletions(-)
diff --git a/flang/include/flang/Optimizer/CodeGen/DescriptorModel.h b/flang/include/flang/Optimizer/CodeGen/DescriptorModel.h
index ed35caef93014..ff0cf29e8073e 100644
--- a/flang/include/flang/Optimizer/CodeGen/DescriptorModel.h
+++ b/flang/include/flang/Optimizer/CodeGen/DescriptorModel.h
@@ -35,73 +35,73 @@ using TypeBuilderFunc = mlir::Type (*)(mlir::MLIRContext *);
/// Get the LLVM IR dialect model for building a particular C++ type, `T`.
template <typename T>
-TypeBuilderFunc getModel();
+static TypeBuilderFunc getModel();
template <>
-TypeBuilderFunc getModel<void *>() {
+constexpr TypeBuilderFunc getModel<void *>() {
return [](mlir::MLIRContext *context) -> mlir::Type {
return mlir::LLVM::LLVMPointerType::get(context);
};
}
template <>
-TypeBuilderFunc getModel<unsigned>() {
+constexpr TypeBuilderFunc getModel<unsigned>() {
return [](mlir::MLIRContext *context) -> mlir::Type {
return mlir::IntegerType::get(context, sizeof(unsigned) * 8);
};
}
template <>
-TypeBuilderFunc getModel<int>() {
+constexpr TypeBuilderFunc getModel<int>() {
return [](mlir::MLIRContext *context) -> mlir::Type {
return mlir::IntegerType::get(context, sizeof(int) * 8);
};
}
template <>
-TypeBuilderFunc getModel<unsigned long>() {
+constexpr TypeBuilderFunc getModel<unsigned long>() {
return [](mlir::MLIRContext *context) -> mlir::Type {
return mlir::IntegerType::get(context, sizeof(unsigned long) * 8);
};
}
template <>
-TypeBuilderFunc getModel<unsigned long long>() {
+constexpr TypeBuilderFunc getModel<unsigned long long>() {
return [](mlir::MLIRContext *context) -> mlir::Type {
return mlir::IntegerType::get(context, sizeof(unsigned long long) * 8);
};
}
template <>
-TypeBuilderFunc getModel<long long>() {
+constexpr TypeBuilderFunc getModel<long long>() {
return [](mlir::MLIRContext *context) -> mlir::Type {
return mlir::IntegerType::get(context, sizeof(long long) * 8);
};
}
template <>
-TypeBuilderFunc getModel<Fortran::ISO::CFI_rank_t>() {
+constexpr TypeBuilderFunc getModel<Fortran::ISO::CFI_rank_t>() {
return [](mlir::MLIRContext *context) -> mlir::Type {
return mlir::IntegerType::get(context,
sizeof(Fortran::ISO::CFI_rank_t) * 8);
};
}
template <>
-TypeBuilderFunc getModel<Fortran::ISO::CFI_type_t>() {
+constexpr TypeBuilderFunc getModel<Fortran::ISO::CFI_type_t>() {
return [](mlir::MLIRContext *context) -> mlir::Type {
return mlir::IntegerType::get(context,
sizeof(Fortran::ISO::CFI_type_t) * 8);
};
}
template <>
-TypeBuilderFunc getModel<long>() {
+constexpr TypeBuilderFunc getModel<long>() {
return [](mlir::MLIRContext *context) -> mlir::Type {
return mlir::IntegerType::get(context, sizeof(long) * 8);
};
}
template <>
-TypeBuilderFunc getModel<Fortran::ISO::CFI_dim_t>() {
+constexpr TypeBuilderFunc getModel<Fortran::ISO::CFI_dim_t>() {
return [](mlir::MLIRContext *context) -> mlir::Type {
auto indexTy = getModel<Fortran::ISO::CFI_index_t>()(context);
return mlir::LLVM::LLVMArrayType::get(indexTy, 3);
};
}
template <>
-TypeBuilderFunc
+constexpr TypeBuilderFunc
getModel<Fortran::ISO::cfi_internal::FlexibleArray<Fortran::ISO::CFI_dim_t>>() {
return getModel<Fortran::ISO::CFI_dim_t>();
}
>From 1d502d00b4545299a8ba32a155721b6fb39fe23a Mon Sep 17 00:00:00 2001
From: Abid Qadeer <haqadeer at amd.com>
Date: Fri, 7 Jun 2024 17:52:12 +0100
Subject: [PATCH 3/4] Handle review comments.
1. Return if data layout is null.
2. Use getComponentOffset to calculate the offset of any field in the descriptor.
This was suggested by @jeanPerier.
---
.../Transforms/DebugTypeGenerator.cpp | 74 +++++++------------
.../Optimizer/Transforms/DebugTypeGenerator.h | 4 +-
2 files changed, 30 insertions(+), 48 deletions(-)
diff --git a/flang/lib/Optimizer/Transforms/DebugTypeGenerator.cpp b/flang/lib/Optimizer/Transforms/DebugTypeGenerator.cpp
index 858f58b8e2703..8eb6e568c1152 100644
--- a/flang/lib/Optimizer/Transforms/DebugTypeGenerator.cpp
+++ b/flang/lib/Optimizer/Transforms/DebugTypeGenerator.cpp
@@ -23,63 +23,45 @@
namespace fir {
+/// Calculate offset of any field in the descriptor.
+template <int DescriptorField>
+std::uint64_t getComponentOffset(const mlir::DataLayout &dl,
+ mlir::MLIRContext *context,
+ mlir::Type llvmFieldType) {
+ assert(DescriptorField > 0 && DescriptorField < 10);
+ mlir::Type previousFieldType =
+ getDescFieldTypeModel<DescriptorField - 1>()(context);
+ std::uint64_t previousOffset =
+ getComponentOffset<DescriptorField - 1>(dl, context, previousFieldType);
+ std::uint64_t offset = previousOffset + dl.getTypeSize(previousFieldType);
+ std::uint64_t fieldAlignment = dl.getTypeABIAlignment(llvmFieldType);
+ return llvm::alignTo(offset, fieldAlignment);
+}
+template <>
+std::uint64_t getComponentOffset<0>(const mlir::DataLayout &dl,
+ mlir::MLIRContext *context,
+ mlir::Type llvmFieldType) {
+ return 0;
+}
+
DebugTypeGenerator::DebugTypeGenerator(mlir::ModuleOp m)
: module(m), kindMapping(getKindMapping(m)) {
LLVM_DEBUG(llvm::dbgs() << "DITypeAttr generator\n");
std::optional<mlir::DataLayout> dl =
fir::support::getOrSetDataLayout(module, /*allowDefaultLayout=*/true);
- if (!dl)
+ if (!dl) {
mlir::emitError(module.getLoc(), "Missing data layout attribute in module");
+ return;
+ }
mlir::MLIRContext *context = module.getContext();
// The debug information requires the offset of certain fields in the
- // descriptors like lower_bound and extent for each dimension. The code
- // below uses getDescFieldTypeModel to get the type representing each field
- // and then use data layout to get its size. It adds the size to get the
- // offset.
- // As has been mentioned in DescriptorModel.h that code may be confusing
- // host for the target in calculating the type of the descriptor fields. But
- // debug info is using similar logic to what codegen is doing so it will
- // atleast be representing the generated code correctly.
- // My testing for a 32-bit shows that base_addr* is correctly given as a
- // 32-bit entity. The index types are 64-bit so I am a bit uncertain how
- // the alignment will effect the calculation of offsets in that case.
-
- // base_addr*
- dimsOffset =
- dl->getTypeSizeInBits(getDescFieldTypeModel<kAddrPosInBox>()(context));
-
- // elem_len
- dimsOffset +=
- dl->getTypeSizeInBits(getDescFieldTypeModel<kElemLenPosInBox>()(context));
-
- // version
- dimsOffset +=
- dl->getTypeSizeInBits(getDescFieldTypeModel<kVersionPosInBox>()(context));
-
- // rank
- dimsOffset +=
- dl->getTypeSizeInBits(getDescFieldTypeModel<kRankPosInBox>()(context));
-
- // type
- dimsOffset +=
- dl->getTypeSizeInBits(getDescFieldTypeModel<kTypePosInBox>()(context));
-
- // attribute
- dimsOffset += dl->getTypeSizeInBits(
- getDescFieldTypeModel<kAttributePosInBox>()(context));
-
- // f18Addendum
- dimsOffset += dl->getTypeSizeInBits(
- getDescFieldTypeModel<kF18AddendumPosInBox>()(context));
-
- // dims
- dimsSize =
- dl->getTypeSizeInBits(getDescFieldTypeModel<kDimsPosInBox>()(context));
- dimsOffset /= 8;
- dimsSize /= 8;
+ // descriptors like lower_bound and extent for each dimension.
+ mlir::Type llvmDimsType = getDescFieldTypeModel<kDimsPosInBox>()(context);
+ dimsOffset = getComponentOffset<kDimsPosInBox>(*dl, context, llvmDimsType);
+ dimsSize = dl->getTypeSize(getDescFieldTypeModel<kDimsPosInBox>()(context));
}
static mlir::LLVM::DITypeAttr genBasicType(mlir::MLIRContext *context,
diff --git a/flang/lib/Optimizer/Transforms/DebugTypeGenerator.h b/flang/lib/Optimizer/Transforms/DebugTypeGenerator.h
index 45e2371af3432..11515d11dfed6 100644
--- a/flang/lib/Optimizer/Transforms/DebugTypeGenerator.h
+++ b/flang/lib/Optimizer/Transforms/DebugTypeGenerator.h
@@ -47,8 +47,8 @@ class DebugTypeGenerator {
bool genAllocated, bool genAssociated);
mlir::ModuleOp module;
KindMapping kindMapping;
- size_t dimsSize;
- size_t dimsOffset;
+ std::uint64_t dimsSize;
+ std::uint64_t dimsOffset;
};
} // namespace fir
>From acde7d78a4ddf5b2a99595803bf22d3b6c56db04 Mon Sep 17 00:00:00 2001
From: Abid Qadeer <haqadeer at amd.com>
Date: Mon, 10 Jun 2024 12:16:05 +0100
Subject: [PATCH 4/4] Handle review comments.
---
flang/lib/Optimizer/Transforms/DebugTypeGenerator.cpp | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/flang/lib/Optimizer/Transforms/DebugTypeGenerator.cpp b/flang/lib/Optimizer/Transforms/DebugTypeGenerator.cpp
index 8eb6e568c1152..53745d10fe9e4 100644
--- a/flang/lib/Optimizer/Transforms/DebugTypeGenerator.cpp
+++ b/flang/lib/Optimizer/Transforms/DebugTypeGenerator.cpp
@@ -28,7 +28,7 @@ template <int DescriptorField>
std::uint64_t getComponentOffset(const mlir::DataLayout &dl,
mlir::MLIRContext *context,
mlir::Type llvmFieldType) {
- assert(DescriptorField > 0 && DescriptorField < 10);
+ static_assert(DescriptorField > 0 && DescriptorField < 10);
mlir::Type previousFieldType =
getDescFieldTypeModel<DescriptorField - 1>()(context);
std::uint64_t previousOffset =
@@ -61,7 +61,7 @@ DebugTypeGenerator::DebugTypeGenerator(mlir::ModuleOp m)
// descriptors like lower_bound and extent for each dimension.
mlir::Type llvmDimsType = getDescFieldTypeModel<kDimsPosInBox>()(context);
dimsOffset = getComponentOffset<kDimsPosInBox>(*dl, context, llvmDimsType);
- dimsSize = dl->getTypeSize(getDescFieldTypeModel<kDimsPosInBox>()(context));
+ dimsSize = dl->getTypeSize(llvmDimsType);
}
static mlir::LLVM::DITypeAttr genBasicType(mlir::MLIRContext *context,
More information about the flang-commits
mailing list