[flang-commits] [flang] [flang][lowering] Fix clash between string literals of different kinds (PR #67576)

via flang-commits flang-commits at lists.llvm.org
Wed Sep 27 10:14:43 PDT 2023


https://github.com/jeanPerier created https://github.com/llvm/llvm-project/pull/67576

At least fir.global for empty string literals of different kinds. Add the kind to the prefix if it is not 1.

>From 83c67c3ebf9cb407738bd662da40dcf6d60e0706 Mon Sep 17 00:00:00 2001
From: Jean Perier <jperier at nvidia.com>
Date: Wed, 27 Sep 2023 09:51:05 -0700
Subject: [PATCH] [flang][lowering] Fix clash between string literals of
 different kinds

At least fir.global for empty string literals of different kinds.
Add the kind to the prefix if it is not 1.
---
 flang/lib/Lower/ConvertConstant.cpp           |  3 ++-
 flang/test/Lower/HLFIR/constant-character.f90 | 17 +++++++++++++++++
 2 files changed, 19 insertions(+), 1 deletion(-)
 create mode 100644 flang/test/Lower/HLFIR/constant-character.f90

diff --git a/flang/lib/Lower/ConvertConstant.cpp b/flang/lib/Lower/ConvertConstant.cpp
index ded0a1959a6c1eb..940e70da511c22e 100644
--- a/flang/lib/Lower/ConvertConstant.cpp
+++ b/flang/lib/Lower/ConvertConstant.cpp
@@ -323,7 +323,8 @@ genScalarLit(fir::FirOpBuilder &builder, mlir::Location loc,
 
   auto size = builder.getKindMap().getCharacterBitsize(KIND) / 8 * value.size();
   llvm::StringRef strVal(reinterpret_cast<const char *>(value.c_str()), size);
-  std::string globalName = fir::factory::uniqueCGIdent("cl", strVal);
+  std::string globalName = fir::factory::uniqueCGIdent(
+      KIND == 1 ? "cl"s : "cl"s + std::to_string(KIND), strVal);
   fir::GlobalOp global = builder.getNamedGlobal(globalName);
   fir::CharacterType type =
       fir::CharacterType::get(builder.getContext(), KIND, len);
diff --git a/flang/test/Lower/HLFIR/constant-character.f90 b/flang/test/Lower/HLFIR/constant-character.f90
new file mode 100644
index 000000000000000..0c788488930bd92
--- /dev/null
+++ b/flang/test/Lower/HLFIR/constant-character.f90
@@ -0,0 +1,17 @@
+! Test that character literals of different types do not clash.
+! RUN: bbc -emit-hlfir %s -o - | FileCheck %s
+
+subroutine repro(c1, c4)
+  character(kind=1,len=*) :: c1
+  character(kind=4,len=*) :: c4
+  print *, ""
+  print *, 4_""
+end subroutine
+!CHECK-LABEL: func.func @_QPrepro
+!CHECK:    fir.address_of(@_QQcl.) : !fir.ref<!fir.char<1,0>>
+!CHECK:    fir.call @_FortranAioOutputAscii
+!CHECK:    fir.address_of(@_QQcl4.) : !fir.ref<!fir.char<4,0>>
+!CHECK:    fir.call @_FortranAioOutputDescriptor(
+
+!CHECK-DAG:  fir.global linkonce @_QQcl. constant : !fir.char<1,0>
+!CHECK-DAG:  fir.global linkonce @_QQcl4. constant : !fir.char<4,0>



More information about the flang-commits mailing list