Skip to content

Commit 5168b82

Browse files
committed
[CIR][CodeGen][NFC] Refactor emitCXXGlobalVarDeclInit to match codegen
1 parent f1a29c6 commit 5168b82

File tree

1 file changed

+41
-40
lines changed

1 file changed

+41
-40
lines changed

clang/lib/CIR/CodeGen/CIRGenCXX.cpp

Lines changed: 41 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "CIRGenModule.h"
1818

1919
#include "clang/AST/GlobalDecl.h"
20+
#include "clang/CIR/MissingFeatures.h"
2021
#include "llvm/Support/ErrorHandling.h"
2122
#include "llvm/Support/SaveAndRestore.h"
2223
#include <cassert>
@@ -335,11 +336,6 @@ void CIRGenModule::emitCXXGlobalVarDeclInit(const VarDecl *varDecl,
335336
// expects "this" in the "generic" address space.
336337
assert(!cir::MissingFeatures::addressSpace());
337338

338-
if (getLangOpts().OpenMP && !getLangOpts().OpenMPSimd &&
339-
varDecl->hasAttr<OMPThreadPrivateDeclAttr>()) {
340-
llvm_unreachable("NYI");
341-
}
342-
343339
assert(varDecl && " Expected a global declaration!");
344340
CIRGenFunction cgf{*this, builder, true};
345341
llvm::SaveAndRestore<CIRGenFunction *> savedCGF(CurCGF, &cgf);
@@ -350,42 +346,15 @@ void CIRGenModule::emitCXXGlobalVarDeclInit(const VarDecl *varDecl,
350346

351347
addr.setAstAttr(cir::ASTVarDeclAttr::get(&getMLIRContext(), varDecl));
352348

353-
if (ty->isReferenceType()) {
354-
mlir::OpBuilder::InsertionGuard guard(builder);
355-
auto *block = builder.createBlock(&addr.getCtorRegion());
356-
CIRGenFunction::LexicalScope lexScope{*CurCGF, addr.getLoc(),
357-
builder.getInsertionBlock()};
358-
lexScope.setAsGlobalInit();
359-
builder.setInsertionPointToStart(block);
360-
auto getGlobal = builder.createGetGlobal(addr);
361-
362-
Address declAddr(getGlobal, getGlobal.getType(),
363-
getASTContext().getDeclAlign(varDecl));
364-
assert(performInit && "cannot have constant initializer which needs "
365-
"destruction for reference");
366-
RValue rv = cgf.emitReferenceBindingToExpr(init);
367-
{
368-
mlir::OpBuilder::InsertionGuard guard(builder);
369-
mlir::Operation *rvalueDefOp = rv.getScalarVal().getDefiningOp();
370-
if (rvalueDefOp && rvalueDefOp->getBlock()) {
371-
mlir::Block *rvalSrcBlock = rvalueDefOp->getBlock();
372-
if (!rvalSrcBlock->empty() && isa<cir::YieldOp>(rvalSrcBlock->back())) {
373-
auto &front = rvalSrcBlock->front();
374-
getGlobal.getDefiningOp()->moveBefore(&front);
375-
auto yield = cast<cir::YieldOp>(rvalSrcBlock->back());
376-
builder.setInsertionPoint(yield);
377-
}
378-
}
379-
cgf.emitStoreOfScalar(rv.getScalarVal(), declAddr, false, ty);
349+
if (!ty->isReferenceType()) {
350+
if (getLangOpts().OpenMP && !getLangOpts().OpenMPSimd &&
351+
varDecl->hasAttr<OMPThreadPrivateDeclAttr>()) {
352+
llvm_unreachable("NYI");
380353
}
381-
builder.setInsertionPointToEnd(block);
382-
builder.create<cir::YieldOp>(addr->getLoc());
383-
} else {
354+
384355
bool needsDtor = varDecl->needsDestruction(getASTContext()) ==
385356
QualType::DK_cxx_destructor;
386357
// PerformInit, constant store invariant / destroy handled below.
387-
bool isConstantStorage =
388-
varDecl->getType().isConstantStorage(getASTContext(), true, !needsDtor);
389358
if (performInit) {
390359
mlir::OpBuilder::InsertionGuard guard(builder);
391360
auto *block = builder.createBlock(&addr.getCtorRegion());
@@ -401,9 +370,10 @@ void CIRGenModule::emitCXXGlobalVarDeclInit(const VarDecl *varDecl,
401370
builder.create<cir::YieldOp>(addr->getLoc());
402371
}
403372

404-
if (isConstantStorage) {
405-
// TODO: this leads to a missing feature in the moment, probably also need
406-
// a LexicalScope to be inserted here.
373+
if (varDecl->getType().isConstantStorage(getASTContext(), true,
374+
!needsDtor)) {
375+
// TODO(CIR): this leads to a missing feature in the moment, probably also
376+
// need a LexicalScope to be inserted here.
407377
emitDeclInvariant(cgf, varDecl);
408378
} else {
409379
// If not constant storage we'll emit this regardless of NeedsDtor value.
@@ -423,5 +393,36 @@ void CIRGenModule::emitCXXGlobalVarDeclInit(const VarDecl *varDecl,
423393
} else
424394
builder.create<cir::YieldOp>(addr->getLoc());
425395
}
396+
return;
397+
}
398+
399+
mlir::OpBuilder::InsertionGuard guard(builder);
400+
auto *block = builder.createBlock(&addr.getCtorRegion());
401+
CIRGenFunction::LexicalScope lexScope{*CurCGF, addr.getLoc(),
402+
builder.getInsertionBlock()};
403+
lexScope.setAsGlobalInit();
404+
builder.setInsertionPointToStart(block);
405+
auto getGlobal = builder.createGetGlobal(addr);
406+
407+
Address declAddr(getGlobal, getGlobal.getType(),
408+
getASTContext().getDeclAlign(varDecl));
409+
assert(performInit && "cannot have constant initializer which needs "
410+
"destruction for reference");
411+
RValue rv = cgf.emitReferenceBindingToExpr(init);
412+
{
413+
mlir::OpBuilder::InsertionGuard guard(builder);
414+
mlir::Operation *rvalueDefOp = rv.getScalarVal().getDefiningOp();
415+
if (rvalueDefOp && rvalueDefOp->getBlock()) {
416+
mlir::Block *rvalSrcBlock = rvalueDefOp->getBlock();
417+
if (!rvalSrcBlock->empty() && isa<cir::YieldOp>(rvalSrcBlock->back())) {
418+
auto &front = rvalSrcBlock->front();
419+
getGlobal.getDefiningOp()->moveBefore(&front);
420+
auto yield = cast<cir::YieldOp>(rvalSrcBlock->back());
421+
builder.setInsertionPoint(yield);
422+
}
423+
}
424+
cgf.emitStoreOfScalar(rv.getScalarVal(), declAddr, false, ty);
426425
}
426+
builder.setInsertionPointToEnd(block);
427+
builder.create<cir::YieldOp>(addr->getLoc());
427428
}

0 commit comments

Comments
 (0)