Skip to content

[MLIR][LLVM] Refactor globals insertion point in import #127490

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 18, 2025

Conversation

xlauko
Copy link
Contributor

@xlauko xlauko commented Feb 17, 2025

Unifies imports to use a single insertion point, globalInsertionOp, for global values.
Refactors insertion point setup into setGlobalInsertionPoint, which sets insertion point after globalInsertionOp or defaults to the start of the module if it is not set.

@xlauko xlauko requested review from gysit and Dinistro February 17, 2025 13:33
@xlauko xlauko self-assigned this Feb 17, 2025
@llvmbot
Copy link
Member

llvmbot commented Feb 17, 2025

@llvm/pr-subscribers-mlir-llvm

@llvm/pr-subscribers-mlir

Author: Henrich Lauko (xlauko)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/127490.diff

2 Files Affected:

  • (modified) mlir/include/mlir/Target/LLVMIR/ModuleImport.h (+4)
  • (modified) mlir/lib/Target/LLVMIR/ModuleImport.cpp (+13-15)
diff --git a/mlir/include/mlir/Target/LLVMIR/ModuleImport.h b/mlir/include/mlir/Target/LLVMIR/ModuleImport.h
index 4642d58760ca8..a44d2d3bd6ae5 100644
--- a/mlir/include/mlir/Target/LLVMIR/ModuleImport.h
+++ b/mlir/include/mlir/Target/LLVMIR/ModuleImport.h
@@ -407,6 +407,10 @@ class ModuleImport {
   /// always requires a symbol name.
   FlatSymbolRefAttr
   getOrCreateNamelessSymbolName(llvm::GlobalVariable *globalVar);
+  /// Returns the global insertion point for the next global operation. If an
+  /// operation is set, the insertion point is placed after the specified
+  /// operation. Otherwise, it defaults to the start of the module.
+  OpBuilder::InsertionGuard setGlobalInsertionPoint(Operation *op);
 
   /// Builder pointing at where the next instruction should be generated.
   OpBuilder builder;
diff --git a/mlir/lib/Target/LLVMIR/ModuleImport.cpp b/mlir/lib/Target/LLVMIR/ModuleImport.cpp
index fd0283b856b6b..8d053b7bfb814 100644
--- a/mlir/lib/Target/LLVMIR/ModuleImport.cpp
+++ b/mlir/lib/Target/LLVMIR/ModuleImport.cpp
@@ -962,13 +962,18 @@ ModuleImport::getOrCreateNamelessSymbolName(llvm::GlobalVariable *globalVar) {
   return symbolRef;
 }
 
-LogicalResult ModuleImport::convertAlias(llvm::GlobalAlias *alias) {
-  // Insert the global after the last one or at the start of the module.
+OpBuilder::InsertionGuard ModuleImport::setGlobalInsertionPoint(Operation *op) {
   OpBuilder::InsertionGuard guard(builder);
-  if (!aliasInsertionOp)
-    builder.setInsertionPointToStart(mlirModule.getBody());
+  if (op)
+    builder.setInsertionPointAfter(op);
   else
-    builder.setInsertionPointAfter(aliasInsertionOp);
+    builder.setInsertionPointToStart(mlirModule.getBody());
+  return guard;
+}
+
+LogicalResult ModuleImport::convertAlias(llvm::GlobalAlias *alias) {
+  // Insert the alias after the last one or at the start of the module.
+  OpBuilder::InsertionGuard guard = setGlobalInsertionPoint(aliasInsertionOp);
 
   Type type = convertType(alias->getValueType());
   AliasOp aliasOp = builder.create<AliasOp>(
@@ -996,11 +1001,7 @@ LogicalResult ModuleImport::convertAlias(llvm::GlobalAlias *alias) {
 
 LogicalResult ModuleImport::convertGlobal(llvm::GlobalVariable *globalVar) {
   // Insert the global after the last one or at the start of the module.
-  OpBuilder::InsertionGuard guard(builder);
-  if (!globalInsertionOp)
-    builder.setInsertionPointToStart(mlirModule.getBody());
-  else
-    builder.setInsertionPointAfter(globalInsertionOp);
+  OpBuilder::InsertionGuard guard = setGlobalInsertionPoint(globalInsertionOp);
 
   Attribute valueAttr;
   if (globalVar->hasInitializer())
@@ -1096,11 +1097,8 @@ ModuleImport::convertGlobalCtorsAndDtors(llvm::GlobalVariable *globalVar) {
     priorities.push_back(priority->getValue().getZExtValue());
   }
 
-  OpBuilder::InsertionGuard guard(builder);
-  if (!globalInsertionOp)
-    builder.setInsertionPointToStart(mlirModule.getBody());
-  else
-    builder.setInsertionPointAfter(globalInsertionOp);
+  // Insert the global after the last one or at the start of the module.
+  OpBuilder::InsertionGuard guard = setGlobalInsertionPoint(globalInsertionOp);
 
   if (globalVar->getName() == getGlobalCtorsVarName()) {
     globalInsertionOp = builder.create<LLVM::GlobalCtorsOp>(

@xlauko xlauko changed the title [MLIR][LLVM] Refactor globals insertion point translation (NFC) [MLIR][LLVM] Refactor globals insertion point in translation (NFC) Feb 17, 2025
@xlauko xlauko changed the title [MLIR][LLVM] Refactor globals insertion point in translation (NFC) [MLIR][LLVM] Refactor globals insertion point in import (NFC) Feb 17, 2025
@xlauko
Copy link
Contributor Author

xlauko commented Feb 17, 2025

I am also not sure whether import needs to have separate insertion operation for globals and aliases (globalInsertionOp, aliasInsertionOp)?

FYI, I am working on GlobalIFunc import which uses similar insertion.

@@ -407,6 +407,10 @@ class ModuleImport {
/// always requires a symbol name.
FlatSymbolRefAttr
getOrCreateNamelessSymbolName(llvm::GlobalVariable *globalVar);
/// Returns the global insertion point for the next global operation. If an
/// operation is set, the insertion point is placed after the specified
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// operation is set, the insertion point is placed after the specified
/// operation is provided, the insertion point is placed after the specified

Ultra nit: "set" can be a bit misleading/confusing

builder.setInsertionPointToStart(mlirModule.getBody());
else
builder.setInsertionPointAfter(globalInsertionOp);
OpBuilder::InsertionGuard guard = setGlobalInsertionPoint(globalInsertionOp);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Considering that this is always called with globalInsertionOp, it might be acceptable to fold this into the function and avoid passing it at all?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not. That was my question whether aliasInsertionOp is actually needed? I don't see a reason, but it was duplicated with aliases. I think globalInsertionOp might be enough. This will result to emit aliases after globals, which I guess desired anyway. Same applies for IFunc I wanted to add.

If this is true, I would inline that as well.

Though there is similar functionality with constantInsertionOp, which would require insertionBlock as argument as well.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe having just one globalInsertionOp makes sense. I even wonder why we introduced the insertions ops in the first place. It may also be possible to convert comdats/globals/aliases/functions etc in the desired order and insert them always at the end of the module. However, having one globalInsertionOp sounds like a good step forward.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Except for an unexpected order in tests, everything seems to pass. I'll polish this further then. :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be fixed.

@xlauko xlauko changed the title [MLIR][LLVM] Refactor globals insertion point in import (NFC) [MLIR][LLVM] Refactor globals insertion point in import Feb 18, 2025
@xlauko xlauko force-pushed the xlauko/mlir-llvm-global-insertion-point branch from 7e9c44a to 5fdd364 Compare February 18, 2025 15:44
@xlauko xlauko force-pushed the xlauko/mlir-llvm-global-insertion-point branch from 5fdd364 to 03b49b3 Compare February 18, 2025 15:49
Copy link
Contributor

@gysit gysit left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

LGTM

@xlauko xlauko merged commit 6682753 into llvm:main Feb 18, 2025
8 checks passed
@xlauko xlauko deleted the xlauko/mlir-llvm-global-insertion-point branch February 18, 2025 16:44
wldfngrs pushed a commit to wldfngrs/llvm-project that referenced this pull request Feb 19, 2025
Unifies imports to use a single insertion point, `globalInsertionOp`,
for global values.
Refactors insertion point setup into `setGlobalInsertionPoint`, which
sets insertion point after `globalInsertionOp` or defaults to the start
of the module if it is not set.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants