Skip to content

Commit

Permalink
[flang] Fixed slice offset computation in XEmbox codegen.
Browse files Browse the repository at this point in the history
For character type with unknown length we end up generating
a GEP with the base type `llvm.ptr<i[width]>`. The GEP produces
the address of the first element of the slice, and it should be
using the offset computed in the number of characters, while we were
providing the offset in bytes.

Simple reproducer fails with and w/o HLFIR:
```
program test
  integer,parameter :: ck = 4
  character(:,ck),allocatable :: res(:,:)
  allocate(character(3,ck) :: res(2,2))
  res(1,1) = ck_'111'
  res(1,2) = ck_'222'
  res(2,1) = ck_'333'
  res(2,2) = ck_'444'
  call check(res)
contains
  subroutine check(res)
    character(:,ck),allocatable :: res(:,:)
    print *, res(2,:)
  end subroutine check
end program test
```

Reviewed By: clementval

Differential Revision: https://reviews.llvm.org/D156849
  • Loading branch information
vzakhari committed Aug 2, 2023
1 parent 449823e commit 315939f
Show file tree
Hide file tree
Showing 2 changed files with 213 additions and 3 deletions.
10 changes: 7 additions & 3 deletions flang/lib/Optimizer/CodeGen/CodeGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1754,8 +1754,11 @@ struct XEmboxOpConversion : public EmboxCommonConversion<fir::cg::XEmboxOp> {
// Adjust the element scaling factor if the element is a dependent type.
if (fir::hasDynamicSize(seqEleTy)) {
if (auto charTy = seqEleTy.dyn_cast<fir::CharacterType>()) {
prevPtrOff =
getCharacterByteSize(loc, rewriter, charTy, adaptor.getLenParams());
// The GEP pointer type decays to llvm.ptr<i[width]>.
// The scaling factor is the runtime value of the length.
assert(!adaptor.getLenParams().empty());
prevPtrOff = FIROpConversion::integerCast(
loc, rewriter, i64Ty, adaptor.getLenParams().back());
} else if (seqEleTy.isa<fir::RecordType>()) {
// prevPtrOff = ;
TODO(loc, "generate call to calculate size of PDT");
Expand Down Expand Up @@ -1783,7 +1786,8 @@ struct XEmboxOpConversion : public EmboxCommonConversion<fir::cg::XEmboxOp> {
// per CHARACTER element.
auto charTy = seqEleTy.cast<fir::CharacterType>();
if (fir::hasDynamicSize(charTy)) {
prevDimByteStride = prevPtrOff;
prevDimByteStride =
getCharacterByteSize(loc, rewriter, charTy, adaptor.getLenParams());
} else {
prevDimByteStride = genConstantIndex(
loc, i64Ty, rewriter,
Expand Down

0 comments on commit 315939f

Please sign in to comment.