17
17
#include " CIRGenModule.h"
18
18
19
19
#include " clang/AST/GlobalDecl.h"
20
+ #include " clang/CIR/MissingFeatures.h"
20
21
#include " llvm/Support/ErrorHandling.h"
21
22
#include " llvm/Support/SaveAndRestore.h"
22
23
#include < cassert>
@@ -335,11 +336,6 @@ void CIRGenModule::emitCXXGlobalVarDeclInit(const VarDecl *varDecl,
335
336
// expects "this" in the "generic" address space.
336
337
assert (!cir::MissingFeatures::addressSpace ());
337
338
338
- if (getLangOpts ().OpenMP && !getLangOpts ().OpenMPSimd &&
339
- varDecl->hasAttr <OMPThreadPrivateDeclAttr>()) {
340
- llvm_unreachable (" NYI" );
341
- }
342
-
343
339
assert (varDecl && " Expected a global declaration!" );
344
340
CIRGenFunction cgf{*this , builder, true };
345
341
llvm::SaveAndRestore<CIRGenFunction *> savedCGF (CurCGF, &cgf);
@@ -350,42 +346,15 @@ void CIRGenModule::emitCXXGlobalVarDeclInit(const VarDecl *varDecl,
350
346
351
347
addr.setAstAttr (cir::ASTVarDeclAttr::get (&getMLIRContext (), varDecl));
352
348
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" );
380
353
}
381
- builder.setInsertionPointToEnd (block);
382
- builder.create <cir::YieldOp>(addr->getLoc ());
383
- } else {
354
+
384
355
bool needsDtor = varDecl->needsDestruction (getASTContext ()) ==
385
356
QualType::DK_cxx_destructor;
386
357
// PerformInit, constant store invariant / destroy handled below.
387
- bool isConstantStorage =
388
- varDecl->getType ().isConstantStorage (getASTContext (), true , !needsDtor);
389
358
if (performInit) {
390
359
mlir::OpBuilder::InsertionGuard guard (builder);
391
360
auto *block = builder.createBlock (&addr.getCtorRegion ());
@@ -401,9 +370,10 @@ void CIRGenModule::emitCXXGlobalVarDeclInit(const VarDecl *varDecl,
401
370
builder.create <cir::YieldOp>(addr->getLoc ());
402
371
}
403
372
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.
407
377
emitDeclInvariant (cgf, varDecl);
408
378
} else {
409
379
// If not constant storage we'll emit this regardless of NeedsDtor value.
@@ -423,5 +393,36 @@ void CIRGenModule::emitCXXGlobalVarDeclInit(const VarDecl *varDecl,
423
393
} else
424
394
builder.create <cir::YieldOp>(addr->getLoc ());
425
395
}
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);
426
425
}
426
+ builder.setInsertionPointToEnd (block);
427
+ builder.create <cir::YieldOp>(addr->getLoc ());
427
428
}
0 commit comments