[Mlir-commits] [mlir] [MLIR][LLVM] Implement LLVM dialect support for global aliases (PR #125295)

Tobias Gysi llvmlistbot at llvm.org
Sat Feb 1 10:17:55 PST 2025


================
@@ -1251,6 +1253,63 @@ LogicalResult ModuleTranslation::convertGlobals() {
   return success();
 }
 
+/// Convert aliases.
+LogicalResult ModuleTranslation::convertAliases() {
+  for (auto op : getModuleBody(mlirModule).getOps<LLVM::AliasOp>()) {
+    llvm::Type *type = convertType(op.getType());
+    llvm::Constant *cst = nullptr;
+    auto linkage = convertLinkageToLLVM(op.getLinkage());
+    llvm::Module &llvmMod = *llvmModule;
+
+    llvm::GlobalAlias *var = llvm::GlobalAlias::create(
+        type, op.getAddrSpace(), linkage, op.getSymName(), cst, &llvmMod);
+
+    var->setThreadLocalMode(op.getThreadLocal_()
+                                ? llvm::GlobalAlias::GeneralDynamicTLSModel
+                                : llvm::GlobalAlias::NotThreadLocal);
+
+    // Note there is no need to setup the comdat because GlobalAlias calls into
+    // the aliasee comdat information automatically.
+
+    if (op.getUnnamedAddr().has_value())
+      var->setUnnamedAddr(convertUnnamedAddrToLLVM(*op.getUnnamedAddr()));
+
+    var->setVisibility(convertVisibilityToLLVM(op.getVisibility_()));
+
+    aliasesMapping.try_emplace(op, var);
+  }
+
+  // Convert global aliases. This is done after all global aliases
+  // have been created in LLVM IR because a global body may refer to another
+  // global alias. So all aliases need to be mapped first.
----------------
gysit wrote:

This sounds a bit like we need to first create both globals and the aliases before we can start converting the bodies of the two? So maybe we need to fuse this logic with convertGlobals. Or can the body of a global variable not reference an alias? That seems unlikely.

https://github.com/llvm/llvm-project/pull/125295


More information about the Mlir-commits mailing list