[flang-commits] [flang] [flang][debug] Support assumed shape arrays. (PR #94644)
via flang-commits
flang-commits at lists.llvm.org
Fri Jun 7 08:02:55 PDT 2024
================
@@ -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.
+
----------------
jeanPerier wrote:
You can query component alignments to the data layout. I think you could have something a bit more generic like (untested):
```
template<int DescriptorField> std::uint64_t getComponentOffset(const mlir::DataLayout& dl, mlir::Context& context, mlit::Type llvmFieldType) {
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::unit64_t getComponentOffset<0>(const mlir::DataLayout& dl, mlir::Context& context) {return 0;}
```
Then you safely (*) get the info with:
```
mlir::Type llvmDimsType = getDescFieldTypeModel<kDimsPosInBox>()(context);
std::unit64_t dimOffset = getComponentOffset<kDimsPosInBox>(dl, context, llvmDimsType)
```
(*) the target/host issue being pushed into the getDescFieldTypeModel<> helper, but not into the code you are adding.
https://github.com/llvm/llvm-project/pull/94644
More information about the flang-commits
mailing list