@@ -2264,6 +2264,30 @@ void CodeGenModule::EmitDeferred() {
2264
2264
CurDeclsToEmit.swap (DeferredDeclsToEmit);
2265
2265
2266
2266
for (GlobalDecl &D : CurDeclsToEmit) {
2267
+ const ValueDecl *VD = cast<ValueDecl>(D.getDecl ());
2268
+ // If emitting for SYCL device, emit the deferred alias
2269
+ // as well as what it aliases.
2270
+ if (LangOpts.SYCLIsDevice ) {
2271
+ if (AliasAttr *Attr = VD->getAttr <AliasAttr>()) {
2272
+ StringRef AliaseeName = Attr->getAliasee ();
2273
+ auto DDI = DeferredDecls.find (AliaseeName);
2274
+ // Emit what is aliased first.
2275
+ if (DDI != DeferredDecls.end ()) {
2276
+ llvm::GlobalValue *AliaseeGV = dyn_cast<llvm::GlobalValue>(
2277
+ GetAddrOfGlobal (DDI->second , ForDefinition));
2278
+ if (!AliaseeGV)
2279
+ AliaseeGV = GetGlobalValue (getMangledName (DDI->second ));
2280
+ assert (AliaseeGV);
2281
+ EmitGlobalDefinition (DDI->second , AliaseeGV);
2282
+ // Remove the entry just added to the DeferredDeclsToEmit
2283
+ // since we have emitted it.
2284
+ DeferredDeclsToEmit.pop_back ();
2285
+ }
2286
+ // Now emit the alias itself.
2287
+ EmitAliasDefinition (D);
2288
+ continue ;
2289
+ }
2290
+ }
2267
2291
// We should call GetAddrOfGlobal with IsForDefinition set to true in order
2268
2292
// to get GlobalValue with exactly the type we need, not something that
2269
2293
// might had been created for another decl with the same mangled name but
@@ -2296,6 +2320,20 @@ void CodeGenModule::EmitDeferred() {
2296
2320
// Otherwise, emit the definition and move on to the next one.
2297
2321
EmitGlobalDefinition (D, GV);
2298
2322
2323
+ if (LangOpts.SYCLIsDevice ) {
2324
+ // If there are any aliases deferred for this, emit those now.
2325
+ for (auto It = DeferredAliases.begin (); It != DeferredAliases.end ();
2326
+ /* no increment*/ ) {
2327
+ const ValueDecl *Global = cast<ValueDecl>(It->second .getDecl ());
2328
+ if (It->first == getMangledName (D)) {
2329
+ EmitAliasDefinition (Global);
2330
+ It = DeferredAliases.erase (It);
2331
+ } else {
2332
+ ++It;
2333
+ }
2334
+ }
2335
+ }
2336
+
2299
2337
// If we found out that we need to emit more decls, do that recursively.
2300
2338
// This has the advantage that the decls are emitted in a DFS and related
2301
2339
// ones are close together, which is convenient for testing.
@@ -2619,9 +2657,19 @@ void CodeGenModule::EmitGlobal(GlobalDecl GD) {
2619
2657
return ;
2620
2658
2621
2659
// If this is an alias definition (which otherwise looks like a declaration)
2622
- // emit it now.
2623
- if (Global->hasAttr <AliasAttr>())
2624
- return EmitAliasDefinition (GD);
2660
+ // handle it now.
2661
+ if (AliasAttr *Attr = Global->getAttr <AliasAttr>()) {
2662
+ // Emit the alias here if it is not SYCL device compilation.
2663
+ if (!LangOpts.SYCLIsDevice )
2664
+ return EmitAliasDefinition (GD);
2665
+ // Defer for SYCL devices, until either the alias or what it aliases
2666
+ // is used.
2667
+ StringRef MangledName = getMangledName (GD);
2668
+ DeferredDecls[MangledName] = GD;
2669
+ StringRef AliaseeName = Attr->getAliasee ();
2670
+ DeferredAliases[AliaseeName] = GD;
2671
+ return ;
2672
+ }
2625
2673
2626
2674
// IFunc like an alias whose value is resolved at runtime by calling resolver.
2627
2675
if (Global->hasAttr <IFuncAttr>())
@@ -4836,20 +4884,21 @@ void CodeGenModule::EmitAliasDefinition(GlobalDecl GD) {
4836
4884
// if a deferred decl.
4837
4885
llvm::Constant *Aliasee;
4838
4886
llvm::GlobalValue::LinkageTypes LT;
4887
+ unsigned AS;
4839
4888
if (isa<llvm::FunctionType>(DeclTy)) {
4840
4889
Aliasee = GetOrCreateLLVMFunction (AA->getAliasee (), DeclTy, GD,
4841
4890
/* ForVTable=*/ false );
4842
4891
LT = getFunctionLinkage (GD);
4892
+ AS = Aliasee->getType ()->getPointerAddressSpace ();
4843
4893
} else {
4844
- Aliasee = GetOrCreateLLVMGlobal (AA-> getAliasee (),
4845
- llvm::PointerType::getUnqual ( DeclTy),
4894
+ AS = ArgInfoAddressSpace ( GetGlobalVarAddressSpace ( /* D= */ nullptr ));
4895
+ Aliasee = GetOrCreateLLVMGlobal (AA-> getAliasee (), DeclTy-> getPointerTo (AS ),
4846
4896
/* D=*/ nullptr );
4847
4897
LT = getLLVMLinkageVarDefinition (cast<VarDecl>(GD.getDecl ()),
4848
4898
D->getType ().isConstQualified ());
4849
4899
}
4850
4900
4851
4901
// Create the new alias itself, but don't set a name yet.
4852
- unsigned AS = Aliasee->getType ()->getPointerAddressSpace ();
4853
4902
auto *GA =
4854
4903
llvm::GlobalAlias::create (DeclTy, AS, LT, " " , Aliasee, &getModule ());
4855
4904
@@ -4870,8 +4919,8 @@ void CodeGenModule::EmitAliasDefinition(GlobalDecl GD) {
4870
4919
// Remove it and replace uses of it with the alias.
4871
4920
GA->takeName (Entry);
4872
4921
4873
- Entry->replaceAllUsesWith (llvm::ConstantExpr::getBitCast (GA,
4874
- Entry->getType ()));
4922
+ Entry->replaceAllUsesWith (
4923
+ llvm::ConstantExpr::getBitCast (GA, Entry->getType ()));
4875
4924
Entry->eraseFromParent ();
4876
4925
} else {
4877
4926
GA->setName (MangledName);
0 commit comments