@@ -1223,6 +1223,17 @@ CGOpenMPRuntime::CGOpenMPRuntime(CodeGenModule &CGM, StringRef FirstSeparator,
1223
1223
1224
1224
void CGOpenMPRuntime::clear () {
1225
1225
InternalVars.clear ();
1226
+ // Clean non-target variable declarations possibly used only in debug info.
1227
+ for (const auto &Data : EmittedNonTargetVariables) {
1228
+ if (!Data.getValue ().pointsToAliveValue ())
1229
+ continue ;
1230
+ auto *GV = dyn_cast<llvm::GlobalVariable>(Data.getValue ());
1231
+ if (!GV)
1232
+ continue ;
1233
+ if (!GV->isDeclaration () || GV->getNumUses () > 0 )
1234
+ continue ;
1235
+ GV->eraseFromParent ();
1236
+ }
1226
1237
}
1227
1238
1228
1239
std::string CGOpenMPRuntime::getName (ArrayRef<StringRef> Parts) const {
@@ -2501,8 +2512,7 @@ llvm::Function *CGOpenMPRuntime::emitThreadPrivateVarDefinition(
2501
2512
return nullptr ;
2502
2513
2503
2514
VD = VD->getDefinition (CGM.getContext ());
2504
- if (VD && ThreadPrivateWithDefinition.count (VD) == 0 ) {
2505
- ThreadPrivateWithDefinition.insert (VD);
2515
+ if (VD && ThreadPrivateWithDefinition.insert (CGM.getMangledName (VD)).second ) {
2506
2516
QualType ASTTy = VD->getType ();
2507
2517
2508
2518
llvm::Value *Ctor = nullptr , *CopyCtor = nullptr , *Dtor = nullptr ;
@@ -2648,7 +2658,7 @@ bool CGOpenMPRuntime::emitDeclareTargetVarDefinition(const VarDecl *VD,
2648
2658
if (!Res || *Res == OMPDeclareTargetDeclAttr::MT_Link)
2649
2659
return CGM.getLangOpts ().OpenMPIsDevice ;
2650
2660
VD = VD->getDefinition (CGM.getContext ());
2651
- if (VD && !DeclareTargetWithDefinition.insert (VD ).second )
2661
+ if (VD && !DeclareTargetWithDefinition.insert (CGM. getMangledName (VD) ).second )
2652
2662
return CGM.getLangOpts ().OpenMPIsDevice ;
2653
2663
2654
2664
QualType ASTTy = VD->getType ();
@@ -3924,6 +3934,8 @@ void CGOpenMPRuntime::createOffloadEntriesAndInfoMetadata() {
3924
3934
llvm::LLVMContext &C = M.getContext ();
3925
3935
SmallVector<const OffloadEntriesInfoManagerTy::OffloadEntryInfo *, 16 >
3926
3936
OrderedEntries (OffloadEntriesInfoManager.size ());
3937
+ llvm::SmallVector<StringRef, 16 > ParentFunctions (
3938
+ OffloadEntriesInfoManager.size ());
3927
3939
3928
3940
// Auxiliary methods to create metadata values and strings.
3929
3941
auto &&GetMDInt = [this ](unsigned V) {
@@ -3938,7 +3950,7 @@ void CGOpenMPRuntime::createOffloadEntriesAndInfoMetadata() {
3938
3950
3939
3951
// Create function that emits metadata for each target region entry;
3940
3952
auto &&TargetRegionMetadataEmitter =
3941
- [&C, MD, &OrderedEntries, &GetMDInt, &GetMDString](
3953
+ [&C, MD, &OrderedEntries, &ParentFunctions, & GetMDInt, &GetMDString](
3942
3954
unsigned DeviceID, unsigned FileID, StringRef ParentName,
3943
3955
unsigned Line,
3944
3956
const OffloadEntriesInfoManagerTy::OffloadEntryInfoTargetRegion &E) {
@@ -3958,6 +3970,7 @@ void CGOpenMPRuntime::createOffloadEntriesAndInfoMetadata() {
3958
3970
3959
3971
// Save this entry in the right position of the ordered entries array.
3960
3972
OrderedEntries[E.getOrder ()] = &E;
3973
+ ParentFunctions[E.getOrder ()] = ParentName;
3961
3974
3962
3975
// Add metadata to the named metadata node.
3963
3976
MD->addOperand (llvm::MDNode::get (C, Ops));
@@ -3999,6 +4012,10 @@ void CGOpenMPRuntime::createOffloadEntriesAndInfoMetadata() {
3999
4012
dyn_cast<OffloadEntriesInfoManagerTy::OffloadEntryInfoTargetRegion>(
4000
4013
E)) {
4001
4014
if (!CE->getID () || !CE->getAddress ()) {
4015
+ // Do not blame the entry if the parent funtion is not emitted.
4016
+ StringRef FnName = ParentFunctions[CE->getOrder ()];
4017
+ if (!CGM.GetGlobalValue (FnName))
4018
+ continue ;
4002
4019
unsigned DiagID = CGM.getDiags ().getCustomDiagID (
4003
4020
DiagnosticsEngine::Error,
4004
4021
" Offloading entry for target region is incorrect: either the "
@@ -8425,14 +8442,15 @@ bool CGOpenMPRuntime::emitTargetFunctions(GlobalDecl GD) {
8425
8442
if (!CGM.getLangOpts ().OpenMPIsDevice )
8426
8443
return false ;
8427
8444
8428
- // Try to detect target regions in the function.
8429
8445
const ValueDecl *VD = cast<ValueDecl>(GD.getDecl ());
8446
+ StringRef Name = CGM.getMangledName (GD);
8447
+ // Try to detect target regions in the function.
8430
8448
if (const auto *FD = dyn_cast<FunctionDecl>(VD))
8431
- scanForTargetRegionsFunctions (FD->getBody (), CGM. getMangledName (GD) );
8449
+ scanForTargetRegionsFunctions (FD->getBody (), Name );
8432
8450
8433
8451
// Do not to emit function if it is not marked as declare target.
8434
8452
return !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration (VD) &&
8435
- AlreadyEmittedTargetFunctions.count (VD-> getCanonicalDecl () ) == 0 ;
8453
+ AlreadyEmittedTargetFunctions.count (Name ) == 0 ;
8436
8454
}
8437
8455
8438
8456
bool CGOpenMPRuntime::emitTargetGlobalVariable (GlobalDecl GD) {
@@ -8469,54 +8487,62 @@ bool CGOpenMPRuntime::emitTargetGlobalVariable(GlobalDecl GD) {
8469
8487
8470
8488
void CGOpenMPRuntime::registerTargetGlobalVariable (const VarDecl *VD,
8471
8489
llvm::Constant *Addr) {
8472
- if (llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
8473
- OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration (VD)) {
8474
- OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryKind Flags;
8475
- StringRef VarName;
8476
- CharUnits VarSize;
8477
- llvm::GlobalValue::LinkageTypes Linkage;
8478
- switch (*Res) {
8479
- case OMPDeclareTargetDeclAttr::MT_To:
8480
- Flags = OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryTo;
8481
- VarName = CGM.getMangledName (VD);
8482
- if (VD->hasDefinition (CGM.getContext ()) != VarDecl::DeclarationOnly) {
8483
- VarSize = CGM.getContext ().getTypeSizeInChars (VD->getType ());
8484
- assert (!VarSize.isZero () && " Expected non-zero size of the variable" );
8485
- } else {
8486
- VarSize = CharUnits::Zero ();
8487
- }
8488
- Linkage = CGM.getLLVMLinkageVarDefinition (VD, /* IsConstant=*/ false );
8489
- // Temp solution to prevent optimizations of the internal variables.
8490
- if (CGM.getLangOpts ().OpenMPIsDevice && !VD->isExternallyVisible ()) {
8491
- std::string RefName = getName ({VarName, " ref" });
8492
- if (!CGM.GetGlobalValue (RefName)) {
8493
- llvm::Constant *AddrRef =
8494
- getOrCreateInternalVariable (Addr->getType (), RefName);
8495
- auto *GVAddrRef = cast<llvm::GlobalVariable>(AddrRef);
8496
- GVAddrRef->setConstant (/* Val=*/ true );
8497
- GVAddrRef->setLinkage (llvm::GlobalValue::InternalLinkage);
8498
- GVAddrRef->setInitializer (Addr);
8499
- CGM.addCompilerUsedGlobal (GVAddrRef);
8500
- }
8501
- }
8502
- break ;
8503
- case OMPDeclareTargetDeclAttr::MT_Link:
8504
- Flags = OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryLink;
8505
- if (CGM.getLangOpts ().OpenMPIsDevice ) {
8506
- VarName = Addr->getName ();
8507
- Addr = nullptr ;
8508
- } else {
8509
- VarName = getAddrOfDeclareTargetLink (VD).getName ();
8510
- Addr =
8511
- cast<llvm::Constant>(getAddrOfDeclareTargetLink (VD).getPointer ());
8490
+ llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
8491
+ OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration (VD);
8492
+ if (!Res) {
8493
+ if (CGM.getLangOpts ().OpenMPIsDevice ) {
8494
+ // Register non-target variables being emitted in device code (debug info
8495
+ // may cause this).
8496
+ StringRef VarName = CGM.getMangledName (VD);
8497
+ EmittedNonTargetVariables.try_emplace (VarName, Addr);
8498
+ }
8499
+ return ;
8500
+ }
8501
+ // Register declare target variables.
8502
+ OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryKind Flags;
8503
+ StringRef VarName;
8504
+ CharUnits VarSize;
8505
+ llvm::GlobalValue::LinkageTypes Linkage;
8506
+ switch (*Res) {
8507
+ case OMPDeclareTargetDeclAttr::MT_To:
8508
+ Flags = OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryTo;
8509
+ VarName = CGM.getMangledName (VD);
8510
+ if (VD->hasDefinition (CGM.getContext ()) != VarDecl::DeclarationOnly) {
8511
+ VarSize = CGM.getContext ().getTypeSizeInChars (VD->getType ());
8512
+ assert (!VarSize.isZero () && " Expected non-zero size of the variable" );
8513
+ } else {
8514
+ VarSize = CharUnits::Zero ();
8515
+ }
8516
+ Linkage = CGM.getLLVMLinkageVarDefinition (VD, /* IsConstant=*/ false );
8517
+ // Temp solution to prevent optimizations of the internal variables.
8518
+ if (CGM.getLangOpts ().OpenMPIsDevice && !VD->isExternallyVisible ()) {
8519
+ std::string RefName = getName ({VarName, " ref" });
8520
+ if (!CGM.GetGlobalValue (RefName)) {
8521
+ llvm::Constant *AddrRef =
8522
+ getOrCreateInternalVariable (Addr->getType (), RefName);
8523
+ auto *GVAddrRef = cast<llvm::GlobalVariable>(AddrRef);
8524
+ GVAddrRef->setConstant (/* Val=*/ true );
8525
+ GVAddrRef->setLinkage (llvm::GlobalValue::InternalLinkage);
8526
+ GVAddrRef->setInitializer (Addr);
8527
+ CGM.addCompilerUsedGlobal (GVAddrRef);
8512
8528
}
8513
- VarSize = CGM.getPointerSize ();
8514
- Linkage = llvm::GlobalValue::WeakAnyLinkage;
8515
- break ;
8516
8529
}
8517
- OffloadEntriesInfoManager.registerDeviceGlobalVarEntryInfo (
8518
- VarName, Addr, VarSize, Flags, Linkage);
8530
+ break ;
8531
+ case OMPDeclareTargetDeclAttr::MT_Link:
8532
+ Flags = OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryLink;
8533
+ if (CGM.getLangOpts ().OpenMPIsDevice ) {
8534
+ VarName = Addr->getName ();
8535
+ Addr = nullptr ;
8536
+ } else {
8537
+ VarName = getAddrOfDeclareTargetLink (VD).getName ();
8538
+ Addr = cast<llvm::Constant>(getAddrOfDeclareTargetLink (VD).getPointer ());
8539
+ }
8540
+ VarSize = CGM.getPointerSize ();
8541
+ Linkage = llvm::GlobalValue::WeakAnyLinkage;
8542
+ break ;
8519
8543
}
8544
+ OffloadEntriesInfoManager.registerDeviceGlobalVarEntryInfo (
8545
+ VarName, Addr, VarSize, Flags, Linkage);
8520
8546
}
8521
8547
8522
8548
bool CGOpenMPRuntime::emitTargetGlobal (GlobalDecl GD) {
@@ -8567,21 +8593,20 @@ bool CGOpenMPRuntime::markAsGlobalTarget(GlobalDecl GD) {
8567
8593
if (!CGM.getLangOpts ().OpenMPIsDevice || !ShouldMarkAsGlobal)
8568
8594
return true ;
8569
8595
8596
+ StringRef Name = CGM.getMangledName (GD);
8570
8597
const auto *D = cast<FunctionDecl>(GD.getDecl ());
8571
- const FunctionDecl *FD = D->getCanonicalDecl ();
8572
8598
// Do not to emit function if it is marked as declare target as it was already
8573
8599
// emitted.
8574
8600
if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration (D)) {
8575
- if (D->hasBody () && AlreadyEmittedTargetFunctions.count (FD) == 0 ) {
8576
- if (auto *F = dyn_cast_or_null<llvm::Function>(
8577
- CGM.GetGlobalValue (CGM.getMangledName (GD))))
8601
+ if (D->hasBody () && AlreadyEmittedTargetFunctions.count (Name) == 0 ) {
8602
+ if (auto *F = dyn_cast_or_null<llvm::Function>(CGM.GetGlobalValue (Name)))
8578
8603
return !F->isDeclaration ();
8579
8604
return false ;
8580
8605
}
8581
8606
return true ;
8582
8607
}
8583
8608
8584
- return !AlreadyEmittedTargetFunctions.insert (FD ).second ;
8609
+ return !AlreadyEmittedTargetFunctions.insert (Name ).second ;
8585
8610
}
8586
8611
8587
8612
llvm::Function *CGOpenMPRuntime::emitRegistrationFunction () {
0 commit comments