Skip to content

Commit b01f059

Browse files
authored
[CIR] Add support for string literal lvalues in ConstantLValueEmitter (#154514)
1 parent 8b12838 commit b01f059

File tree

4 files changed

+50
-6
lines changed

4 files changed

+50
-6
lines changed

clang/lib/CIR/CodeGen/CIRGenExprConstant.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -578,8 +578,7 @@ ConstantLValueEmitter::VisitCompoundLiteralExpr(const CompoundLiteralExpr *e) {
578578

579579
ConstantLValue
580580
ConstantLValueEmitter::VisitStringLiteral(const StringLiteral *e) {
581-
cgm.errorNYI(e->getSourceRange(), "ConstantLValueEmitter: string literal");
582-
return {};
581+
return cgm.getAddrOfConstantStringFromLiteral(e);
583582
}
584583

585584
ConstantLValue

clang/lib/CIR/CodeGen/CIRGenModule.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1318,6 +1318,19 @@ cir::GlobalOp CIRGenModule::getGlobalForStringLiteral(const StringLiteral *s,
13181318
return gv;
13191319
}
13201320

1321+
/// Return a pointer to a constant array for the given string literal.
1322+
cir::GlobalViewAttr
1323+
CIRGenModule::getAddrOfConstantStringFromLiteral(const StringLiteral *s,
1324+
StringRef name) {
1325+
cir::GlobalOp gv = getGlobalForStringLiteral(s, name);
1326+
auto arrayTy = mlir::dyn_cast<cir::ArrayType>(gv.getSymType());
1327+
assert(arrayTy && "String literal must be array");
1328+
assert(!cir::MissingFeatures::addressSpace());
1329+
cir::PointerType ptrTy = getBuilder().getPointerTo(arrayTy.getElementType());
1330+
1331+
return builder.getGlobalViewAttr(ptrTy, gv);
1332+
}
1333+
13211334
void CIRGenModule::emitExplicitCastExprType(const ExplicitCastExpr *e,
13221335
CIRGenFunction *cgf) {
13231336
if (cgf && e->getType()->isVariablyModifiedType())

clang/lib/CIR/CodeGen/CIRGenModule.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,12 @@ class CIRGenModule : public CIRGenTypeCache {
198198
cir::GlobalOp getGlobalForStringLiteral(const StringLiteral *s,
199199
llvm::StringRef name = ".str");
200200

201+
/// Return a global symbol reference to a constant array for the given string
202+
/// literal.
203+
cir::GlobalViewAttr
204+
getAddrOfConstantStringFromLiteral(const StringLiteral *s,
205+
llvm::StringRef name = ".str");
206+
201207
/// Set attributes which are common to any form of a global definition (alias,
202208
/// Objective-C method, function, global variable).
203209
///

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

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,45 @@
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-
// CIR: cir.global "private" constant cir_private dso_local @[[STR1_GLOBAL:.*]] = #cir.const_array<"abcd\00" : !cir.array<!s8i x 5>> : !cir.array<!s8i x 5>
98

10-
// LLVM: @[[STR1_GLOBAL:.*]] = private constant [5 x i8] c"abcd\00"
9+
char const *array[] {
10+
"my", "hands", "are", "typing", "words"
11+
};
1112

12-
// OGCG: @[[STR1_GLOBAL:.*]] = private unnamed_addr constant [5 x i8] c"abcd\00"
13+
// CIR: cir.global "private" constant cir_private dso_local @"[[STR:.+]]" = #cir.const_array<"my\00" : !cir.array<!s8i x 3>> : !cir.array<!s8i x 3>
14+
// CIR: cir.global "private" constant cir_private dso_local @"[[STR1:.+]]" = #cir.const_array<"hands\00" : !cir.array<!s8i x 6>> : !cir.array<!s8i x 6>
15+
// CIR: cir.global "private" constant cir_private dso_local @"[[STR2:.+]]" = #cir.const_array<"are\00" : !cir.array<!s8i x 4>> : !cir.array<!s8i x 4>
16+
// CIR: cir.global "private" constant cir_private dso_local @"[[STR3:.+]]" = #cir.const_array<"typing\00" : !cir.array<!s8i x 7>> : !cir.array<!s8i x 7>
17+
// CIR: cir.global "private" constant cir_private dso_local @"[[STR4:.+]]" = #cir.const_array<"words\00" : !cir.array<!s8i x 6>> : !cir.array<!s8i x 6>
18+
// CIR: cir.global external @array = #cir.const_array<[#cir.global_view<@"[[STR]]"> : !cir.ptr<!s8i>, #cir.global_view<@"[[STR1]]"> : !cir.ptr<!s8i>, #cir.global_view<@"[[STR2]]"> : !cir.ptr<!s8i>, #cir.global_view<@"[[STR3]]"> : !cir.ptr<!s8i>, #cir.global_view<@"[[STR4]]"> : !cir.ptr<!s8i>]> : !cir.array<!cir.ptr<!s8i> x 5>
19+
20+
// LLVM: @[[STR:.+]] = private constant [3 x i8] c"my\00"
21+
// LLVM: @[[STR1:.+]] = private constant [6 x i8] c"hands\00"
22+
// LLVM: @[[STR2:.+]] = private constant [4 x i8] c"are\00"
23+
// LLVM: @[[STR3:.+]] = private constant [7 x i8] c"typing\00"
24+
// LLVM: @[[STR4:.+]] = private constant [6 x i8] c"words\00"
25+
// LLVM: @array = global [5 x ptr] [ptr @[[STR]], ptr @[[STR1]], ptr @[[STR2]], ptr @[[STR3]], ptr @[[STR4]]]
26+
27+
// OGCG: @[[STR:.+]] = private unnamed_addr constant [3 x i8] c"my\00"
28+
// OGCG: @[[STR1:.+]] = private unnamed_addr constant [6 x i8] c"hands\00"
29+
// OGCG: @[[STR2:.+]] = private unnamed_addr constant [4 x i8] c"are\00"
30+
// OGCG: @[[STR3:.+]] = private unnamed_addr constant [7 x i8] c"typing\00"
31+
// OGCG: @[[STR4:.+]] = private unnamed_addr constant [6 x i8] c"words\00"
32+
// OGCG: @array = global [5 x ptr] [ptr @[[STR]], ptr @[[STR1]], ptr @[[STR2]], ptr @[[STR3]], ptr @[[STR4]]]
33+
34+
// CIR: cir.global "private" constant cir_private dso_local @[[STR5_GLOBAL:.*]] = #cir.const_array<"abcd\00" : !cir.array<!s8i x 5>> : !cir.array<!s8i x 5>
35+
36+
// LLVM: @[[STR5_GLOBAL:.*]] = private constant [5 x i8] c"abcd\00"
37+
38+
// OGCG: @[[STR5_GLOBAL:.*]] = private unnamed_addr constant [5 x i8] c"abcd\00"
1339

1440
decltype(auto) returns_literal() {
1541
return "abcd";
1642
}
1743

1844
// CIR: cir.func{{.*}} @_Z15returns_literalv() -> !cir.ptr<!cir.array<!s8i x 5>>
1945
// 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>>
46+
// CIR: %[[STR_ADDR:.*]] = cir.get_global @[[STR5_GLOBAL]] : !cir.ptr<!cir.array<!s8i x 5>>
2147
// CIR: cir.store{{.*}} %[[STR_ADDR]], %[[RET_ADDR]]
2248
// CIR: %[[RET:.*]] = cir.load %[[RET_ADDR]]
2349
// CIR: cir.return %[[RET]]

0 commit comments

Comments
 (0)