Skip to content

Commit 667c786

Browse files
authored
[CIR] Handle global string literals as char array initializer (#144384)
This change adds the line of code needed to handle a string literal as an initializer for a character array.
1 parent 57828fe commit 667c786

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

clang/lib/CIR/CodeGen/CIRGenExprConstant.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,8 @@ class ConstExprEmitter
254254
}
255255

256256
mlir::Attribute VisitStringLiteral(StringLiteral *e, QualType t) {
257-
cgm.errorNYI(e->getBeginLoc(), "ConstExprEmitter::VisitStringLiteral");
258-
return {};
257+
// This is a string literal initializing an array in an initializer.
258+
return cgm.getConstantArrayFromStringLiteral(e);
259259
}
260260

261261
mlir::Attribute VisitObjCEncodeExpr(ObjCEncodeExpr *e, QualType t) {

clang/test/CIR/CodeGen/string-literals.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,18 @@
55
// RUN: %clang_cc1 -triple aarch64-none-linux-android21 -emit-llvm %s -o %t.ll
66
// RUN: FileCheck --check-prefix=OGCG --input-file=%t.ll %s
77

8+
char g_str[] = "1234";
9+
10+
// CIR: cir.global external @g_str = #cir.const_array<"1234\00" : !cir.array<!s8i x 5>> : !cir.array<!s8i x 5>
11+
12+
char g_oversized[100] = "123";
13+
14+
// CIR: cir.global external @g_oversized = #cir.const_array<"123" : !cir.array<!s8i x 3>, trailing_zeros> : !cir.array<!s8i x 100>
15+
16+
char g_exact[4] = "123";
17+
18+
// CIR: cir.global external @g_exact = #cir.const_array<"123\00" : !cir.array<!s8i x 4>> : !cir.array<!s8i x 4>
19+
820
// CIR: cir.global "private" cir_private dsolocal @[[STR1_GLOBAL:.*]] = #cir.const_array<"1\00" : !cir.array<!s8i x 2>> : !cir.array<!s8i x 2>
921
// CIR: cir.global "private" cir_private dsolocal @[[STR2_GLOBAL:.*]] = #cir.zero : !cir.array<!s8i x 1>
1022
// CIR: cir.global "private" cir_private dsolocal @[[STR3_GLOBAL:.*]] = #cir.zero : !cir.array<!s8i x 2>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// RUN: %clang_cc1 -triple aarch64-none-linux-android21 -fclangir -emit-cir %s -o %t.cir
2+
// RUN: FileCheck --check-prefix=CIR --input-file=%t.cir %s
3+
// RUN: %clang_cc1 -triple aarch64-none-linux-android21 -fclangir -emit-llvm %s -o %t-cir.ll
4+
// RUN: FileCheck --check-prefix=LLVM --input-file=%t-cir.ll %s
5+
// RUN: %clang_cc1 -triple aarch64-none-linux-android21 -emit-llvm %s -o %t.ll
6+
// RUN: FileCheck --check-prefix=OGCG --input-file=%t.ll %s
7+
8+
// CIR: cir.global "private" cir_private dsolocal @[[STR1_GLOBAL:.*]] = #cir.const_array<"abcd\00" : !cir.array<!s8i x 5>> : !cir.array<!s8i x 5>
9+
10+
// LLVM: @[[STR1_GLOBAL:.*]] = private global [5 x i8] c"abcd\00"
11+
12+
// OGCG: @[[STR1_GLOBAL:.*]] = private unnamed_addr constant [5 x i8] c"abcd\00"
13+
14+
decltype(auto) returns_literal() {
15+
return "abcd";
16+
}
17+
18+
// CIR: cir.func{{.*}} @_Z15returns_literalv() -> !cir.ptr<!cir.array<!s8i x 5>>
19+
// CIR: %[[RET_ADDR:.*]] = cir.alloca !cir.ptr<!cir.array<!s8i x 5>>, !cir.ptr<!cir.ptr<!cir.array<!s8i x 5>>>, ["__retval"]
20+
// CIR: %[[STR_ADDR:.*]] = cir.get_global @[[STR1_GLOBAL]] : !cir.ptr<!cir.array<!s8i x 5>>
21+
// CIR: cir.store{{.*}} %[[STR_ADDR]], %[[RET_ADDR]]
22+
// CIR: %[[RET:.*]] = cir.load %[[RET_ADDR]]
23+
// CIR: cir.return %[[RET]]

0 commit comments

Comments
 (0)