Skip to content

Commit 79a48a6

Browse files
committed
Address review feedback
1 parent 7cbcfde commit 79a48a6

File tree

4 files changed

+37
-7
lines changed

4 files changed

+37
-7
lines changed

clang/include/clang/CIR/MissingFeatures.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ struct MissingFeatures {
169169
static bool maybeHandleStaticInExternC() { return false; }
170170
static bool constEmitterArrayILE() { return false; }
171171
static bool constEmitterVectorILE() { return false; }
172+
static bool needsGlobalCtorDtor() { return false; }
172173

173174
// Missing types
174175
static bool dataMemberType() { return false; }

clang/lib/CIR/CodeGen/CIRGenConstantEmitter.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ class ConstantEmitter {
3131
private:
3232
bool abstract = false;
3333

34+
#ifndef NDEBUG
35+
// Variables used for asserting state consistency.
36+
3437
/// Whether non-abstract components of the emitter have been initialized.
3538
bool initializedNonAbstract = false;
3639

@@ -39,6 +42,7 @@ class ConstantEmitter {
3942

4043
/// Whether the constant-emission failed.
4144
bool failed = false;
45+
#endif // NDEBUG
4246

4347
/// Whether we're in a constant context.
4448
bool inConstantContext = false;
@@ -101,6 +105,7 @@ class ConstantEmitter {
101105
mlir::Attribute tryEmitPrivateForMemory(const APValue &value, QualType t);
102106

103107
private:
108+
#ifndef NDEBUG
104109
void initializeNonAbstract() {
105110
assert(!initializedNonAbstract);
106111
initializedNonAbstract = true;
@@ -111,6 +116,10 @@ class ConstantEmitter {
111116
failed = true;
112117
return init;
113118
}
119+
#else
120+
void initializeNonAbstract() {}
121+
mlir::Attribute markIfFailed(mlir::Attribute init) { return init; }
122+
#endif // NDEBUG
114123

115124
class AbstractStateRAII {
116125
ConstantEmitter &emitter;

clang/lib/CIR/CodeGen/CIRGenExprConstant.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,6 @@ class ConstExprEmitter
9696
Expr *subExpr = e->getSubExpr();
9797

9898
switch (e->getCastKind()) {
99-
case CK_HLSLArrayRValue:
100-
case CK_HLSLVectorTruncation:
101-
case CK_HLSLElementwiseCast:
102-
case CK_HLSLAggregateSplatCast:
10399
case CK_ToUnion:
104100
case CK_AddressSpaceConversion:
105101
case CK_ReinterpretMemberPointer:
@@ -181,6 +177,10 @@ class ConstExprEmitter
181177
case CK_IntegralToFixedPoint:
182178
case CK_ZeroToOCLOpaqueType:
183179
case CK_MatrixCast:
180+
case CK_HLSLArrayRValue:
181+
case CK_HLSLVectorTruncation:
182+
case CK_HLSLElementwiseCast:
183+
case CK_HLSLAggregateSplatCast:
184184
return {};
185185
}
186186
llvm_unreachable("Invalid CastKind");
@@ -220,8 +220,10 @@ class ConstExprEmitter
220220
return {};
221221
}
222222

223-
if (ile->getType()->isRecordType())
223+
if (ile->getType()->isRecordType()) {
224224
cgm.errorNYI(ile->getBeginLoc(), "ConstExprEmitter: record ILE");
225+
return {};
226+
}
225227

226228
if (ile->getType()->isVectorType()) {
227229
// If we return null here, the non-constant initializer will take care of
@@ -339,9 +341,10 @@ void ConstantEmitter::finalize(cir::GlobalOp gv) {
339341
"finalizing emitter that was used for abstract emission?");
340342
assert(!finalized && "finalizing emitter multiple times");
341343
assert(!gv.isDeclaration());
342-
344+
#ifndef NDEBUG
343345
// Note that we might also be Failed.
344346
finalized = true;
347+
#endif // NDEBUG
345348
}
346349

347350
mlir::Attribute

clang/lib/CIR/CodeGen/CIRGenModule.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,23 @@ void CIRGenModule::emitGlobalVarDefinition(const clang::VarDecl *vd,
412412
return;
413413
}
414414

415+
// Whether the definition of the variable is available externally.
416+
// If yes, we shouldn't emit the GloablCtor and GlobalDtor for the variable
417+
// since this is the job for its original source.
418+
bool isDefinitionAvailableExternally =
419+
astContext.GetGVALinkageForVariable(vd) == GVA_AvailableExternally;
420+
assert(!cir::MissingFeatures::needsGlobalCtorDtor());
421+
422+
// It is useless to emit the definition for an available_externally variable
423+
// which can't be marked as const.
424+
if (isDefinitionAvailableExternally &&
425+
(!vd->hasConstantInitialization() ||
426+
// TODO: Update this when we have interface to check constexpr
427+
// destructor.
428+
vd->needsDestruction(astContext) ||
429+
!vd->getType().isConstantStorage(astContext, true, true)))
430+
return;
431+
415432
mlir::Attribute init;
416433
const VarDecl *initDecl;
417434
const Expr *initExpr = vd->getAnyInitializer(initDecl);
@@ -437,7 +454,7 @@ void CIRGenModule::emitGlobalVarDefinition(const clang::VarDecl *vd,
437454
init = builder.getZeroInitAttr(convertType(vd->getType()));
438455
} else {
439456
emitter.emplace(*this);
440-
auto initializer = emitter->tryEmitForInitializer(*initDecl);
457+
mlir::Attribute initializer = emitter->tryEmitForInitializer(*initDecl);
441458
if (!initializer) {
442459
QualType qt = initExpr->getType();
443460
if (vd->getType()->isReferenceType())

0 commit comments

Comments
 (0)