Skip to content

Commit 0894069

Browse files
ChuanqiXu9lanza
authored andcommitted
[CIR] [Lowering] care trailing zero for lowering constant array (llvm#976)
Close llvm#975 See the attached test case for example
1 parent a40580c commit 0894069

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2281,8 +2281,12 @@ class CIRGlobalOpLowering
22812281
// Initializer is a constant array: convert it to a compatible llvm init.
22822282
if (auto constArr = mlir::dyn_cast<mlir::cir::ConstArrayAttr>(init.value())) {
22832283
if (auto attr = mlir::dyn_cast<mlir::StringAttr>(constArr.getElts())) {
2284-
init = rewriter.getStringAttr(attr.getValue());
2285-
} else if (auto attr = mlir::dyn_cast<mlir::ArrayAttr>(constArr.getElts())) {
2284+
llvm::SmallString<256> literal(attr.getValue());
2285+
if (constArr.getTrailingZerosNum())
2286+
literal.append(constArr.getTrailingZerosNum(), '\0');
2287+
init = rewriter.getStringAttr(literal);
2288+
} else if (auto attr =
2289+
mlir::dyn_cast<mlir::ArrayAttr>(constArr.getElts())) {
22862290
// Failed to use a compact attribute as an initializer:
22872291
// initialize elements individually.
22882292
if (!(init = lowerConstArrayAttr(constArr, getTypeConverter()))) {

clang/test/CIR/Lowering/str.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o %t.ll
2+
// RUN: FileCheck --input-file=%t.ll %s -check-prefix=LLVM
3+
4+
void f(char *fmt, ...);
5+
void test() {
6+
f("test\0");
7+
}
8+
9+
// LLVM: @.str = {{.*}}[6 x i8] c"test\00\00"

0 commit comments

Comments
 (0)