Skip to content

Commit 1411c56

Browse files
authored
Fix a character constant mangling bug (#1010)
The hash portion of a character constant name is currently derived by hashing over a multiple of the actual character constant length, rather than the actual length. Incorporating arbitrary "garbage" characters in the hash value generation will not usually be a correctness issue because the resulting "invalid" hash name will be used for both the value definition and any references to it. However, beyond wasting compile time, it can create duplicate constants rather than reusing a single constant.
1 parent ba3942f commit 1411c56

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

flang/include/flang/Lower/Mangler.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ mangleArrayLiteral(const Fortran::evaluate::Constant<Fortran::evaluate::Type<
7979
Fortran::common::TypeCategory::Character, KIND>> &x) {
8080
return mangleArrayLiteral(
8181
reinterpret_cast<const uint8_t *>(x.values().data()),
82-
x.values().size() * sizeof(x.values()[0]) * x.LEN(), x.shape(),
82+
x.values().size() * sizeof(x.values()[0]), x.shape(),
8383
Fortran::common::TypeCategory::Character, KIND, x.LEN());
8484
}
8585

flang/test/Lower/array-character.f90

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,29 @@ subroutine issue(c1, c2)
3030
c1 = c2
3131
end subroutine
3232

33+
! CHECK-LABEL: func @_QQmain
3334
program p
3435
character(4) :: c1(3)
3536
character(4) :: c2(3) = ["abcd", " ", " "]
3637
print *, c2
3738
call issue(c1, c2)
3839
print *, c1
40+
call charlit
3941
end program p
4042

43+
! CHECK-LABEL: func @_QPcharlit
44+
subroutine charlit
45+
! CHECK: fir.address_of(@_QQro.4x3xc1.1636b396a657de68ffb870a885ac44b4) : !fir.ref<!fir.array<4x!fir.char<1,3>>>
46+
print*, ['AA ', 'MM ', 'MM ', 'ZZ ']
47+
print*, ['AA ', 'MM ', 'MM ', 'ZZ ']
48+
print*, ['AA ', 'MM ', 'MM ', 'ZZ ']
49+
end
50+
51+
! CHECK: fir.global internal @_QQro.4x3xc1.1636b396a657de68ffb870a885ac44b4 constant : !fir.array<4x!fir.char<1,3>>
52+
! CHECK: AA
53+
! CHECK: MM
54+
! CHECK: ZZ
55+
! CHECK-NOT: fir.global internal @_QQro.4x3xc1
56+
! CHECK-NOT: AA
57+
! CHECK-NOT: MM
58+
! CHECK-NOT: ZZ

0 commit comments

Comments
 (0)