Skip to content

[flang][lowering] Fix clash between string literals of different kinds #67576

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 28, 2023

Conversation

jeanPerier
Copy link
Contributor

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

At least fir.global for empty string literals of different kinds.
Add the kind to the prefix if it is not 1.
@llvmbot llvmbot added flang Flang issues not falling into any other category flang:fir-hlfir labels Sep 27, 2023
@llvmbot
Copy link
Member

llvmbot commented Sep 27, 2023

@llvm/pr-subscribers-flang-fir-hlfir

Changes

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


Full diff: https://github.com/llvm/llvm-project/pull/67576.diff

2 Files Affected:

  • (modified) flang/lib/Lower/ConvertConstant.cpp (+2-1)
  • (added) flang/test/Lower/HLFIR/constant-character.f90 (+17)
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>

@jeanPerier jeanPerier merged commit 7ad3825 into llvm:main Sep 28, 2023
@jeanPerier jeanPerier deleted the jpr-empty-space branch September 28, 2023 08:47
legrosbuffle pushed a commit to legrosbuffle/llvm-project that referenced this pull request Sep 29, 2023
llvm#67576)

At least fir.global for empty string literals of different kinds. Add
the kind to the prefix if it is not 1.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:fir-hlfir flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants