Skip to content

Commit 0989ff5

Browse files
mmhaandykaylor
andauthored
[CIR] Add constant attribute to GlobalOp (#154359)
This patch adds the constant attribute to cir.global, the appropriate lowering to LLVM constant and updates the tests. --------- Co-authored-by: Andy Kaylor <[email protected]>
1 parent 3b96648 commit 0989ff5

File tree

11 files changed

+31
-24
lines changed

11 files changed

+31
-24
lines changed

clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,11 +252,11 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
252252
[[nodiscard]] cir::GlobalOp createGlobal(mlir::ModuleOp mlirModule,
253253
mlir::Location loc,
254254
mlir::StringRef name,
255-
mlir::Type type,
255+
mlir::Type type, bool isConstant,
256256
cir::GlobalLinkageKind linkage) {
257257
mlir::OpBuilder::InsertionGuard guard(*this);
258258
setInsertionPointToStart(mlirModule.getBody());
259-
return create<cir::GlobalOp>(loc, name, type, linkage);
259+
return cir::GlobalOp::create(*this, loc, name, type, isConstant, linkage);
260260
}
261261

262262
cir::GetMemberOp createGetMember(mlir::Location loc, mlir::Type resultTy,

clang/include/clang/CIR/Dialect/IR/CIROps.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1702,12 +1702,14 @@ def CIR_GlobalOp : CIR_Op<"global", [
17021702
CIR_GlobalLinkageKind:$linkage,
17031703
OptionalAttr<AnyAttr>:$initial_value,
17041704
UnitAttr:$comdat,
1705+
UnitAttr:$constant,
17051706
UnitAttr:$dso_local,
17061707
OptionalAttr<I64Attr>:$alignment);
17071708

17081709
let assemblyFormat = [{
17091710
($sym_visibility^)?
17101711
(`` $global_visibility^)?
1712+
(`constant` $constant^)?
17111713
$linkage
17121714
(`comdat` $comdat^)?
17131715
(`dso_local` $dso_local^)?
@@ -1726,6 +1728,7 @@ def CIR_GlobalOp : CIR_Op<"global", [
17261728
let builders = [OpBuilder<(ins
17271729
"llvm::StringRef":$sym_name,
17281730
"mlir::Type":$sym_type,
1731+
CArg<"bool", "false">:$isConstant,
17291732
// CIR defaults to external linkage.
17301733
CArg<"cir::GlobalLinkageKind",
17311734
"cir::GlobalLinkageKind::ExternalLinkage">:$linkage)>];

clang/lib/CIR/CodeGen/CIRGenBuilder.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ class CIRGenBuilderTy : public cir::CIRBaseBuilderTy {
439439
/// for its name so it can be referenced correctly.
440440
[[nodiscard]] cir::GlobalOp
441441
createVersionedGlobal(mlir::ModuleOp module, mlir::Location loc,
442-
mlir::StringRef name, mlir::Type type,
442+
mlir::StringRef name, mlir::Type type, bool isConstant,
443443
cir::GlobalLinkageKind linkage) {
444444
// Create a unique name if the given name is already taken.
445445
std::string uniqueName;
@@ -448,7 +448,7 @@ class CIRGenBuilderTy : public cir::CIRBaseBuilderTy {
448448
else
449449
uniqueName = name.str();
450450

451-
return createGlobal(module, loc, uniqueName, type, linkage);
451+
return createGlobal(module, loc, uniqueName, type, isConstant, linkage);
452452
}
453453

454454
mlir::Value createSetBitfield(mlir::Location loc, mlir::Type resultType,

clang/lib/CIR/CodeGen/CIRGenDecl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ CIRGenModule::getOrCreateStaticVarDecl(const VarDecl &d,
297297
mlir::Attribute init = builder.getZeroInitAttr(convertType(ty));
298298

299299
cir::GlobalOp gv = builder.createVersionedGlobal(
300-
getModule(), getLoc(d.getLocation()), name, lty, linkage);
300+
getModule(), getLoc(d.getLocation()), name, lty, false, linkage);
301301
// TODO(cir): infer visibility from linkage in global op builder.
302302
gv.setVisibility(getMLIRVisibilityFromCIRLinkage(linkage));
303303
gv.setInitialValueAttr(init);

clang/lib/CIR/CodeGen/CIRGenModule.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ mlir::Operation *CIRGenModule::getGlobalValue(StringRef name) {
458458

459459
cir::GlobalOp CIRGenModule::createGlobalOp(CIRGenModule &cgm,
460460
mlir::Location loc, StringRef name,
461-
mlir::Type t,
461+
mlir::Type t, bool isConstant,
462462
mlir::Operation *insertPoint) {
463463
cir::GlobalOp g;
464464
CIRGenBuilderTy &builder = cgm.getBuilder();
@@ -479,7 +479,7 @@ cir::GlobalOp CIRGenModule::createGlobalOp(CIRGenModule &cgm,
479479
builder.setInsertionPointToStart(cgm.getModule().getBody());
480480
}
481481

482-
g = builder.create<cir::GlobalOp>(loc, name, t);
482+
g = builder.create<cir::GlobalOp>(loc, name, t, isConstant);
483483
if (!insertPoint)
484484
cgm.lastGlobalOp = g;
485485

@@ -581,7 +581,7 @@ CIRGenModule::getOrCreateCIRGlobal(StringRef mangledName, mlir::Type ty,
581581
// mlir::SymbolTable::Visibility::Public is the default, no need to explicitly
582582
// mark it as such.
583583
cir::GlobalOp gv =
584-
CIRGenModule::createGlobalOp(*this, loc, mangledName, ty,
584+
CIRGenModule::createGlobalOp(*this, loc, mangledName, ty, false,
585585
/*insertPoint=*/entry.getOperation());
586586

587587
// This is the first use or definition of a mangled name. If there is a
@@ -1239,8 +1239,8 @@ generateStringLiteral(mlir::Location loc, mlir::TypedAttr c,
12391239

12401240
// Create a global variable for this string
12411241
// FIXME(cir): check for insertion point in module level.
1242-
cir::GlobalOp gv =
1243-
CIRGenModule::createGlobalOp(cgm, loc, globalName, c.getType());
1242+
cir::GlobalOp gv = CIRGenModule::createGlobalOp(
1243+
cgm, loc, globalName, c.getType(), !cgm.getLangOpts().WritableStrings);
12441244

12451245
// Set up extra information and add to the module
12461246
gv.setAlignmentAttr(cgm.getSize(alignment));

clang/lib/CIR/CodeGen/CIRGenModule.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ class CIRGenModule : public CIRGenTypeCache {
150150

151151
static cir::GlobalOp createGlobalOp(CIRGenModule &cgm, mlir::Location loc,
152152
llvm::StringRef name, mlir::Type t,
153+
bool isConstant = false,
153154
mlir::Operation *insertPoint = nullptr);
154155

155156
llvm::StringMap<unsigned> cgGlobalNames;

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1358,11 +1358,14 @@ mlir::LogicalResult cir::GlobalOp::verify() {
13581358

13591359
void cir::GlobalOp::build(OpBuilder &odsBuilder, OperationState &odsState,
13601360
llvm::StringRef sym_name, mlir::Type sym_type,
1361-
cir::GlobalLinkageKind linkage) {
1361+
bool isConstant, cir::GlobalLinkageKind linkage) {
13621362
odsState.addAttribute(getSymNameAttrName(odsState.name),
13631363
odsBuilder.getStringAttr(sym_name));
13641364
odsState.addAttribute(getSymTypeAttrName(odsState.name),
13651365
mlir::TypeAttr::get(sym_type));
1366+
if (isConstant)
1367+
odsState.addAttribute(getConstantAttrName(odsState.name),
1368+
odsBuilder.getUnitAttr());
13661369

13671370
cir::GlobalLinkageKindAttr linkageAttr =
13681371
cir::GlobalLinkageKindAttr::get(odsBuilder.getContext(), linkage);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1523,7 +1523,7 @@ void CIRToLLVMGlobalOpLowering::setupRegionInitializedLLVMGlobalOp(
15231523
// in CIRToLLVMGlobalOpLowering::matchAndRewrite() but that will go
15241524
// away when the placeholders are no longer needed.
15251525
assert(!cir::MissingFeatures::opGlobalConstant());
1526-
const bool isConst = false;
1526+
const bool isConst = op.getConstant();
15271527
assert(!cir::MissingFeatures::addressSpace());
15281528
const unsigned addrSpace = 0;
15291529
const bool isDsoLocal = op.getDsoLocal();

clang/test/CIR/CodeGen/builtin_printf.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
// RUN: %clang_cc1 -std=c++11 -triple x86_64-unknown-linux-gnu -Wno-unused-value -emit-llvm %s -o %t.ll
66
// RUN: FileCheck --input-file=%t.ll %s -check-prefix=OGCG
77

8-
// CIR: cir.global "private" cir_private dso_local @".str" = #cir.const_array<"%s\00" : !cir.array<!s8i x 3>> : !cir.array<!s8i x 3>
9-
// CIR: cir.global "private" cir_private dso_local @".str.1" = #cir.const_array<"%s %d\0A\00" : !cir.array<!s8i x 7>> : !cir.array<!s8i x 7>
10-
// LLVM: @.str = private global [3 x i8] c"%s\00"
11-
// LLVM: @.str.1 = private global [7 x i8] c"%s %d\0A\00"
8+
// CIR: cir.global "private" constant cir_private dso_local @".str" = #cir.const_array<"%s\00" : !cir.array<!s8i x 3>> : !cir.array<!s8i x 3>
9+
// CIR: cir.global "private" constant cir_private dso_local @".str.1" = #cir.const_array<"%s %d\0A\00" : !cir.array<!s8i x 7>> : !cir.array<!s8i x 7>
10+
// LLVM: @.str = private constant [3 x i8] c"%s\00"
11+
// LLVM: @.str.1 = private constant [7 x i8] c"%s %d\0A\00"
1212
// OGCG: @.str = private unnamed_addr constant [3 x i8] c"%s\00"
1313
// OGCG: @.str.1 = private unnamed_addr constant [7 x i8] c"%s %d\0A\00"
1414

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ char g_exact[4] = "123";
1717

1818
// CIR: cir.global external @g_exact = #cir.const_array<"123\00" : !cir.array<!s8i x 4>> : !cir.array<!s8i x 4>
1919

20-
// CIR: cir.global "private" cir_private dso_local @[[STR1_GLOBAL:.*]] = #cir.const_array<"1\00" : !cir.array<!s8i x 2>> : !cir.array<!s8i x 2>
21-
// CIR: cir.global "private" cir_private dso_local @[[STR2_GLOBAL:.*]] = #cir.zero : !cir.array<!s8i x 1>
22-
// CIR: cir.global "private" cir_private dso_local @[[STR3_GLOBAL:.*]] = #cir.zero : !cir.array<!s8i x 2>
20+
// CIR: cir.global "private" constant cir_private dso_local @[[STR1_GLOBAL:.*]] = #cir.const_array<"1\00" : !cir.array<!s8i x 2>> : !cir.array<!s8i x 2>
21+
// CIR: cir.global "private" constant cir_private dso_local @[[STR2_GLOBAL:.*]] = #cir.zero : !cir.array<!s8i x 1>
22+
// CIR: cir.global "private" constant cir_private dso_local @[[STR3_GLOBAL:.*]] = #cir.zero : !cir.array<!s8i x 2>
2323

24-
// LLVM: @[[STR1_GLOBAL:.*]] = private global [2 x i8] c"1\00"
25-
// LLVM: @[[STR2_GLOBAL:.*]] = private global [1 x i8] zeroinitializer
26-
// LLVM: @[[STR3_GLOBAL:.*]] = private global [2 x i8] zeroinitializer
24+
// LLVM: @[[STR1_GLOBAL:.*]] = private constant [2 x i8] c"1\00"
25+
// LLVM: @[[STR2_GLOBAL:.*]] = private constant [1 x i8] zeroinitializer
26+
// LLVM: @[[STR3_GLOBAL:.*]] = private constant [2 x i8] zeroinitializer
2727

2828
// OGCG: @[[STR1_GLOBAL:.*]] = private unnamed_addr constant [2 x i8] c"1\00"
2929
// OGCG: @[[STR2_GLOBAL:.*]] = private unnamed_addr constant [1 x i8] zeroinitializer

0 commit comments

Comments
 (0)