@@ -2264,6 +2264,30 @@ void CodeGenModule::EmitDeferred() {
22642264 CurDeclsToEmit.swap (DeferredDeclsToEmit);
22652265
22662266 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+ }
22672291 // We should call GetAddrOfGlobal with IsForDefinition set to true in order
22682292 // to get GlobalValue with exactly the type we need, not something that
22692293 // might had been created for another decl with the same mangled name but
@@ -2296,6 +2320,20 @@ void CodeGenModule::EmitDeferred() {
22962320 // Otherwise, emit the definition and move on to the next one.
22972321 EmitGlobalDefinition (D, GV);
22982322
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+
22992337 // If we found out that we need to emit more decls, do that recursively.
23002338 // This has the advantage that the decls are emitted in a DFS and related
23012339 // ones are close together, which is convenient for testing.
@@ -2619,9 +2657,19 @@ void CodeGenModule::EmitGlobal(GlobalDecl GD) {
26192657 return ;
26202658
26212659 // 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+ }
26252673
26262674 // IFunc like an alias whose value is resolved at runtime by calling resolver.
26272675 if (Global->hasAttr <IFuncAttr>())
@@ -4870,8 +4918,9 @@ void CodeGenModule::EmitAliasDefinition(GlobalDecl GD) {
48704918 // Remove it and replace uses of it with the alias.
48714919 GA->takeName (Entry);
48724920
4873- Entry->replaceAllUsesWith (llvm::ConstantExpr::getBitCast (GA,
4874- Entry->getType ()));
4921+ Entry->replaceAllUsesWith (
4922+ llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast (GA,
4923+ Entry->getType ()));
48754924 Entry->eraseFromParent ();
48764925 } else {
48774926 GA->setName (MangledName);
0 commit comments