================
@@ -3374,10 +3379,93 @@ void CIRGenModule::release() {
 
   emitLLVMUsed();
 
+  // Classic codegen calls `checkAliases` here to validate any alias
+  // definitions emitted during codegen.
+  assert(!cir::MissingFeatures::checkAliases());
+
   // There's a lot of code that is not implemented yet.
   assert(!cir::MissingFeatures::cgmRelease());
 }
 
+void CIRGenModule::emitAliasDefinition(GlobalDecl gd) {
+  const auto *d = cast<ValueDecl>(gd.getDecl());
+  const AliasAttr *aa = d->getAttr<AliasAttr>();
+  assert(aa && "Not an alias?");
+
+  StringRef mangledName = getMangledName(gd);
+
+  if (aa->getAliasee() == mangledName) {
+    diags.Report(aa->getLocation(), diag::err_cyclic_alias) << 0;
+    return;
+  }
+
+  // If there is a definition in the module, then it wins over the alias.
+  // This is dubious, but allow it to be safe. Just ignore the alias.
+  mlir::Operation *entry = getGlobalValue(mangledName);
+  if (entry) {
+    auto entryGV = mlir::dyn_cast<cir::CIRGlobalValueInterface>(entry);
+    if (entryGV && !entryGV.isDeclaration())
+      return;
+  }
+
+  // Classic codegen pushes the alias onto an `Aliases` list at this point so
+  // that `checkAliases` can later validate the alias and recover on error.
+  assert(!cir::MissingFeatures::checkAliases());
+
+  mlir::Location loc = getLoc(d->getSourceRange());
+  const bool isFunction = isa<FunctionDecl>(d);
+
+  // Get the linkage and the type of the alias.
+  cir::GlobalLinkageKind linkage;
+  mlir::Type declTy;
+  if (isFunction) {
+    declTy = getTypes().getFunctionType(gd);
+    linkage = getFunctionLinkage(gd);
+  } else {
+    declTy = getTypes().convertTypeForMem(d->getType());
+    if (const auto *vd = dyn_cast<VarDecl>(d))
+      linkage = getCIRLinkageVarDefinition(vd);
+    else
+      linkage = getFunctionLinkage(gd);
+  }
+
+  // Aliases that target weak symbols must themselves be marked weak.
+  if (d->hasAttr<WeakAttr>() || d->hasAttr<WeakRefAttr>() ||
+      d->isWeakImported())
+    linkage = cir::GlobalLinkageKind::WeakAnyLinkage;
+
+  // Create the alias op. If there is an existing declaration with the same
+  // name, erase it: any references to it via flat symbol reference will
+  // automatically resolve to the new alias.
+  if (entry) {
+    eraseGlobalSymbol(entry);
+    entry->erase();
+  }
+
+  // Aliases are always definitions, so the MLIR visibility should match the
+  // linkage rather than defaulting to private.
+  mlir::SymbolTable::Visibility visibility =
+      getMLIRVisibilityFromCIRLinkage(linkage);
+
+  if (isFunction) {
+    cir::FuncType fnType = mlir::cast<cir::FuncType>(declTy);
----------------
erichkeane wrote:

The two branches here being textually identical outside of which gets 'created' 
is pretty chilling. Could we replace this with something like:

```
auto createAlias = [&](auto alias) {
    alias.setAliasee(aa->getAliasee());
    alias.setLinkage(linkage);
    mlir::SymbolTable::setSymbolVisibility(alias, visibility);
    setCommonAttributes(gd, alias);
};

if (isFunction)
  createAlias(createCIRFunction(loc, mangledName, 
mlir::cast<cir::FuncType>(declTy), cast<FunctionDecl>(d));
else
  createAlias(getGlobalOp(*this, loc, mangledName, declTy));

```

https://github.com/llvm/llvm-project/pull/195972
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to