Skip to content

Commit efbd366

Browse files
yugrlanza
authored andcommitted
[CIR][CIRGen][Lowering] Generate GlobalViewAttr for string literals (#242).
1 parent c6a355f commit efbd366

File tree

11 files changed

+135
-133
lines changed

11 files changed

+135
-133
lines changed

clang/lib/CIR/CodeGen/CIRGenBuilder.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,9 @@ class CIRGenBuilderTy : public mlir::OpBuilder {
223223
if (attr.isa<mlir::cir::ZeroAttr, mlir::cir::NullAttr>())
224224
return true;
225225

226+
if (attr.isa<mlir::cir::GlobalViewAttr>())
227+
return false;
228+
226229
// TODO(cir): introduce char type in CIR and check for that instead.
227230
if (const auto intVal = attr.dyn_cast<mlir::cir::IntAttr>())
228231
return intVal.isNullValue();

clang/lib/CIR/CodeGen/CIRGenExpr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1326,7 +1326,7 @@ LValue CIRGenFunction::buildArraySubscriptExpr(const ArraySubscriptExpr *E,
13261326
}
13271327

13281328
LValue CIRGenFunction::buildStringLiteralLValue(const StringLiteral *E) {
1329-
auto sym = CGM.getAddrOfConstantStringFromLiteral(E);
1329+
auto sym = CGM.getAddrOfConstantStringFromLiteral(E).getSymbol();
13301330

13311331
auto cstGlobal = mlir::SymbolTable::lookupSymbolIn(CGM.getModule(), sym);
13321332
assert(cstGlobal && "Expected global");

clang/lib/CIR/CodeGen/CIRGenExprConst.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1008,7 +1008,7 @@ struct ConstantLValue {
10081008
/*implicit*/ ConstantLValue(mlir::Value value, bool hasOffsetApplied = false)
10091009
: Value(value), HasOffsetApplied(hasOffsetApplied) {}
10101010

1011-
/*implicit*/ ConstantLValue(mlir::SymbolRefAttr address) : Value(address) {}
1011+
/*implicit*/ ConstantLValue(mlir::cir::GlobalViewAttr address) : Value(address) {}
10121012

10131013
ConstantLValue(std::nullptr_t) : ConstantLValue({}, false) {}
10141014
ConstantLValue(mlir::Attribute value) : Value(value) {}

clang/lib/CIR/CodeGen/CIRGenModule.cpp

Lines changed: 43 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,78 +1097,62 @@ generateStringLiteral(mlir::Location loc, mlir::TypedAttr C,
10971097
return GV;
10981098
}
10991099

1100-
// In address space agnostic languages, string literals are in default address
1101-
// space in AST. However, certain targets (e.g. amdgcn) request them to be
1102-
// emitted in constant address space in LLVM IR. To be consistent with other
1103-
// parts of AST, string literal global variables in constant address space
1104-
// need to be casted to default address space before being put into address
1105-
// map and referenced by other part of CodeGen.
1106-
// In OpenCL, string literals are in constant address space in AST, therefore
1107-
// they should not be casted to default address space.
1108-
static mlir::StringAttr
1109-
castStringLiteralToDefaultAddressSpace(CIRGenModule &CGM, mlir::StringAttr GV) {
1110-
if (!CGM.getLangOpts().OpenCL) {
1111-
auto AS = CGM.getGlobalConstantAddressSpace();
1112-
if (AS != LangAS::Default)
1113-
assert(0 && "not implemented");
1114-
}
1115-
return GV;
1116-
}
1117-
11181100
/// Return a pointer to a constant array for the given string literal.
1119-
mlir::SymbolRefAttr
1101+
mlir::cir::GlobalViewAttr
11201102
CIRGenModule::getAddrOfConstantStringFromLiteral(const StringLiteral *S,
11211103
StringRef Name) {
11221104
CharUnits Alignment =
11231105
astCtx.getAlignOfGlobalVarInChars(S->getType(), /*VD=*/nullptr);
11241106

11251107
mlir::Attribute C = getConstantArrayFromStringLiteral(S);
1126-
mlir::cir::GlobalOp Entry;
1127-
if (!getLangOpts().WritableStrings) {
1128-
if (ConstantStringMap.count(C)) {
1129-
auto g = ConstantStringMap[C];
1130-
// The bigger alignment always wins.
1131-
if (!g.getAlignment() ||
1132-
uint64_t(Alignment.getQuantity()) > *g.getAlignment())
1133-
g.setAlignmentAttr(getSize(Alignment));
1134-
return mlir::SymbolRefAttr::get(
1135-
castStringLiteralToDefaultAddressSpace(*this, g.getSymNameAttr()));
1136-
}
1137-
}
11381108

1139-
SmallString<256> StringNameBuffer = Name;
1140-
llvm::raw_svector_ostream Out(StringNameBuffer);
1141-
if (StringLiteralCnt)
1142-
Out << StringLiteralCnt;
1143-
Name = Out.str();
1144-
StringLiteralCnt++;
1109+
mlir::cir::GlobalOp GV;
1110+
if (!getLangOpts().WritableStrings && ConstantStringMap.count(C)) {
1111+
GV = ConstantStringMap[C];
1112+
// The bigger alignment always wins.
1113+
if (!GV.getAlignment() ||
1114+
uint64_t(Alignment.getQuantity()) > *GV.getAlignment())
1115+
GV.setAlignmentAttr(getSize(Alignment));
1116+
} else {
1117+
SmallString<256> StringNameBuffer = Name;
1118+
llvm::raw_svector_ostream Out(StringNameBuffer);
1119+
if (StringLiteralCnt)
1120+
Out << StringLiteralCnt;
1121+
Name = Out.str();
1122+
StringLiteralCnt++;
1123+
1124+
SmallString<256> MangledNameBuffer;
1125+
StringRef GlobalVariableName;
1126+
auto LT = mlir::cir::GlobalLinkageKind::ExternalLinkage;
1127+
1128+
// Mangle the string literal if that's how the ABI merges duplicate strings.
1129+
// Don't do it if they are writable, since we don't want writes in one TU to
1130+
// affect strings in another.
1131+
if (getCXXABI().getMangleContext().shouldMangleStringLiteral(S) &&
1132+
!getLangOpts().WritableStrings) {
1133+
assert(0 && "not implemented");
1134+
} else {
1135+
LT = mlir::cir::GlobalLinkageKind::InternalLinkage;
1136+
GlobalVariableName = Name;
1137+
}
11451138

1146-
SmallString<256> MangledNameBuffer;
1147-
StringRef GlobalVariableName;
1148-
auto LT = mlir::cir::GlobalLinkageKind::ExternalLinkage;
1139+
auto loc = getLoc(S->getSourceRange());
1140+
auto typedC = llvm::dyn_cast<mlir::TypedAttr>(C);
1141+
if (!typedC)
1142+
llvm_unreachable("this should never be untyped at this point");
1143+
GV = generateStringLiteral(loc, typedC, LT, *this, GlobalVariableName,
1144+
Alignment);
1145+
ConstantStringMap[C] = GV;
11491146

1150-
// Mangle the string literal if that's how the ABI merges duplicate strings.
1151-
// Don't do it if they are writable, since we don't want writes in one TU to
1152-
// affect strings in another.
1153-
if (getCXXABI().getMangleContext().shouldMangleStringLiteral(S) &&
1154-
!getLangOpts().WritableStrings) {
1155-
assert(0 && "not implemented");
1156-
} else {
1157-
LT = mlir::cir::GlobalLinkageKind::InternalLinkage;
1158-
GlobalVariableName = Name;
1147+
assert(!cir::UnimplementedFeature::reportGlobalToASan() && "NYI");
11591148
}
11601149

1161-
auto loc = getLoc(S->getSourceRange());
1162-
auto typedC = llvm::dyn_cast<mlir::TypedAttr>(C);
1163-
if (!typedC)
1164-
llvm_unreachable("this should never be untyped at this point");
1165-
auto GV = generateStringLiteral(loc, typedC, LT, *this, GlobalVariableName,
1166-
Alignment);
1167-
ConstantStringMap[C] = GV;
1150+
auto ArrayTy = GV.getSymType().dyn_cast<mlir::cir::ArrayType>();
1151+
assert(ArrayTy && "String literal must be array");
1152+
auto PtrTy =
1153+
mlir::cir::PointerType::get(builder.getContext(), ArrayTy.getEltType());
11681154

1169-
assert(!cir::UnimplementedFeature::reportGlobalToASan() && "NYI");
1170-
return mlir::SymbolRefAttr::get(
1171-
castStringLiteralToDefaultAddressSpace(*this, GV.getSymNameAttr()));
1155+
return builder.getGlobalViewAttr(PtrTy, GV);
11721156
}
11731157

11741158
void CIRGenModule::buildDeclContext(const DeclContext *DC) {

clang/lib/CIR/CodeGen/CIRGenModule.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ class CIRGenModule : public CIRGenTypeCache {
304304

305305
/// Return a global symbol reference to a constant array for the given string
306306
/// literal.
307-
mlir::SymbolRefAttr
307+
mlir::cir::GlobalViewAttr
308308
getAddrOfConstantStringFromLiteral(const StringLiteral *S,
309309
StringRef Name = ".str");
310310
unsigned StringLiteralCnt = 0;

clang/lib/CIR/Dialect/IR/CIRDialect.cpp

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1343,8 +1343,6 @@ static void printGlobalOpTypeAndInitialValue(OpAsmPrinter &p, GlobalOp op,
13431343
// This also prints the type...
13441344
if (initAttr)
13451345
printConstant(p, initAttr);
1346-
if (initAttr.isa<SymbolRefAttr>())
1347-
printType();
13481346
}
13491347

13501348
if (!dtorRegion.empty()) {
@@ -1395,16 +1393,10 @@ static ParseResult parseGlobalOpTypeAndInitialValue(OpAsmParser &parser,
13951393
if (parseConstantValue(parser, initialValueAttr).failed())
13961394
return failure();
13971395

1398-
if (auto sra = initialValueAttr.dyn_cast<SymbolRefAttr>()) {
1399-
if (parser.parseColonType(opTy))
1400-
return failure();
1401-
} else {
1402-
// Handle StringAttrs
1403-
assert(initialValueAttr.isa<mlir::TypedAttr>() &&
1404-
"Non-typed attrs shouldn't appear here.");
1405-
auto typedAttr = initialValueAttr.cast<mlir::TypedAttr>();
1406-
opTy = typedAttr.getType();
1407-
}
1396+
assert(initialValueAttr.isa<mlir::TypedAttr>() &&
1397+
"Non-typed attrs shouldn't appear here.");
1398+
auto typedAttr = initialValueAttr.cast<mlir::TypedAttr>();
1399+
opTy = typedAttr.getType();
14081400
}
14091401

14101402
// Parse destructor, example:

0 commit comments

Comments
 (0)