Skip to content

[MLIR:LLVM] Add UWTableKind attribute #135811

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
Apr 17, 2025

Conversation

WillFroom
Copy link
Contributor

Add UWTableKind enum and corresponding attribute to llvm.func including translation to llvm::Function attribute.

@llvmbot
Copy link
Member

llvmbot commented Apr 15, 2025

@llvm/pr-subscribers-mlir-llvm

Author: Will Froom (WillFroom)

Changes

Add UWTableKind enum and corresponding attribute to llvm.func including translation to llvm::Function attribute.


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

5 Files Affected:

  • (modified) mlir/include/mlir/Dialect/LLVMIR/LLVMAttrDefs.td (+10)
  • (modified) mlir/include/mlir/Dialect/LLVMIR/LLVMEnums.td (+23)
  • (modified) mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td (+2-1)
  • (modified) mlir/lib/Target/LLVMIR/ModuleTranslation.cpp (+2)
  • (added) mlir/test/Target/LLVMIR/uwtable.mlir (+8)
diff --git a/mlir/include/mlir/Dialect/LLVMIR/LLVMAttrDefs.td b/mlir/include/mlir/Dialect/LLVMIR/LLVMAttrDefs.td
index 690243525ede4..e2bea7f8d3d23 100644
--- a/mlir/include/mlir/Dialect/LLVMIR/LLVMAttrDefs.td
+++ b/mlir/include/mlir/Dialect/LLVMIR/LLVMAttrDefs.td
@@ -1365,4 +1365,14 @@ def LLVM_DependentLibrariesAttr
   let assemblyFormat = "`<` $libs `>`";
 }
 
+//===----------------------------------------------------------------------===//
+// UWTableKindAttr
+//===----------------------------------------------------------------------===//
+
+def UWTableKindAttr : LLVM_Attr<"UWTableKind", "uwtableKind"> {
+  let parameters = (ins "uwtable::UWTableKind":$uwtableKind);
+  let assemblyFormat = "`<` $uwtableKind `>`";
+}
+
+
 #endif // LLVMIR_ATTRDEFS
diff --git a/mlir/include/mlir/Dialect/LLVMIR/LLVMEnums.td b/mlir/include/mlir/Dialect/LLVMIR/LLVMEnums.td
index 34a30a00790ea..52bce96c8a990 100644
--- a/mlir/include/mlir/Dialect/LLVMIR/LLVMEnums.td
+++ b/mlir/include/mlir/Dialect/LLVMIR/LLVMEnums.td
@@ -855,4 +855,27 @@ def ModFlagBehaviorAttr : LLVM_EnumAttr<
   let cppNamespace = "::mlir::LLVM";
 }
 
+//===----------------------------------------------------------------------===//
+// UWTableKind
+//===----------------------------------------------------------------------===//
+
+def UWTableKindNone
+    : LLVM_EnumAttrCase<"None", "none", "None", 0>;
+def UWTableKindSync
+    : LLVM_EnumAttrCase<"Sync", "sync", "Sync", 1>;
+def UWTableKindAsync
+    : LLVM_EnumAttrCase<"Async", "async", "Async", 2>;
+def UWTableKindDefault
+    : LLVM_EnumAttrCase<"Default", "default", "Default", 2>;
+
+// UWTableKindDefault is unsupported as the llvm enum value is the same as async  
+// which the generated enum converters can't deal with.
+def UWTableKindEnum : LLVM_EnumAttr<
+    "UWTableKind",
+    "::llvm::UWTableKind",
+    "LLVM Unwind Behavior",
+    [UWTableKindNone, UWTableKindSync, UWTableKindAsync]> {
+  let cppNamespace = "::mlir::LLVM::uwtable";
+}
+
 #endif // LLVMIR_ENUMS
diff --git a/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td b/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td
index e89e78aec7147..76a2ec47b3a22 100644
--- a/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td
+++ b/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td
@@ -1842,7 +1842,8 @@ def LLVM_LLVMFuncOp : LLVM_Op<"func", [
     OptionalAttr<LLVM_VecTypeHintAttr>:$vec_type_hint,
     OptionalAttr<DenseI32ArrayAttr>:$work_group_size_hint,
     OptionalAttr<DenseI32ArrayAttr>:$reqd_work_group_size,
-    OptionalAttr<I32Attr>:$intel_reqd_sub_group_size
+    OptionalAttr<I32Attr>:$intel_reqd_sub_group_size,
+    OptionalAttr<UWTableKindAttr>:$uwtable_kind
   );
 
   let regions = (region AnyRegion:$body);
diff --git a/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp b/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
index ee7dc3a5231f4..6531b233b94a0 100644
--- a/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
+++ b/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
@@ -1602,6 +1602,8 @@ static void convertFunctionAttributes(LLVMFuncOp func,
   if (FramePointerKindAttr fpAttr = func.getFramePointerAttr())
     llvmFunc->addFnAttr("frame-pointer", stringifyFramePointerKind(
                                              fpAttr.getFramePointerKind()));
+  if (UWTableKindAttr uwTableKindAttr = func.getUwtableKindAttr())
+    llvmFunc->setUWTableKind(convertUWTableKindToLLVM(uwTableKindAttr.getUwtableKind()));
   convertFunctionMemoryAttributes(func, llvmFunc);
 }
 
diff --git a/mlir/test/Target/LLVMIR/uwtable.mlir b/mlir/test/Target/LLVMIR/uwtable.mlir
new file mode 100644
index 0000000000000..3e1d5b566f01b
--- /dev/null
+++ b/mlir/test/Target/LLVMIR/uwtable.mlir
@@ -0,0 +1,8 @@
+// RUN: mlir-translate -mlir-to-llvmir %s | FileCheck %s
+
+// CHECK-LABEL: define void @uwtable_func() 
+// CHECK-SAME: #[[ATTRS:[0-9]+]]
+llvm.func @uwtable_func() attributes {uwtable_kind = #llvm.uwtableKind<"sync">}  {
+  llvm.return
+}
+// CHECK: attributes #[[ATTRS]] = { uwtable(sync) }

@llvmbot
Copy link
Member

llvmbot commented Apr 15, 2025

@llvm/pr-subscribers-mlir

Author: Will Froom (WillFroom)

Changes

Add UWTableKind enum and corresponding attribute to llvm.func including translation to llvm::Function attribute.


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

5 Files Affected:

  • (modified) mlir/include/mlir/Dialect/LLVMIR/LLVMAttrDefs.td (+10)
  • (modified) mlir/include/mlir/Dialect/LLVMIR/LLVMEnums.td (+23)
  • (modified) mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td (+2-1)
  • (modified) mlir/lib/Target/LLVMIR/ModuleTranslation.cpp (+2)
  • (added) mlir/test/Target/LLVMIR/uwtable.mlir (+8)
diff --git a/mlir/include/mlir/Dialect/LLVMIR/LLVMAttrDefs.td b/mlir/include/mlir/Dialect/LLVMIR/LLVMAttrDefs.td
index 690243525ede4..e2bea7f8d3d23 100644
--- a/mlir/include/mlir/Dialect/LLVMIR/LLVMAttrDefs.td
+++ b/mlir/include/mlir/Dialect/LLVMIR/LLVMAttrDefs.td
@@ -1365,4 +1365,14 @@ def LLVM_DependentLibrariesAttr
   let assemblyFormat = "`<` $libs `>`";
 }
 
+//===----------------------------------------------------------------------===//
+// UWTableKindAttr
+//===----------------------------------------------------------------------===//
+
+def UWTableKindAttr : LLVM_Attr<"UWTableKind", "uwtableKind"> {
+  let parameters = (ins "uwtable::UWTableKind":$uwtableKind);
+  let assemblyFormat = "`<` $uwtableKind `>`";
+}
+
+
 #endif // LLVMIR_ATTRDEFS
diff --git a/mlir/include/mlir/Dialect/LLVMIR/LLVMEnums.td b/mlir/include/mlir/Dialect/LLVMIR/LLVMEnums.td
index 34a30a00790ea..52bce96c8a990 100644
--- a/mlir/include/mlir/Dialect/LLVMIR/LLVMEnums.td
+++ b/mlir/include/mlir/Dialect/LLVMIR/LLVMEnums.td
@@ -855,4 +855,27 @@ def ModFlagBehaviorAttr : LLVM_EnumAttr<
   let cppNamespace = "::mlir::LLVM";
 }
 
+//===----------------------------------------------------------------------===//
+// UWTableKind
+//===----------------------------------------------------------------------===//
+
+def UWTableKindNone
+    : LLVM_EnumAttrCase<"None", "none", "None", 0>;
+def UWTableKindSync
+    : LLVM_EnumAttrCase<"Sync", "sync", "Sync", 1>;
+def UWTableKindAsync
+    : LLVM_EnumAttrCase<"Async", "async", "Async", 2>;
+def UWTableKindDefault
+    : LLVM_EnumAttrCase<"Default", "default", "Default", 2>;
+
+// UWTableKindDefault is unsupported as the llvm enum value is the same as async  
+// which the generated enum converters can't deal with.
+def UWTableKindEnum : LLVM_EnumAttr<
+    "UWTableKind",
+    "::llvm::UWTableKind",
+    "LLVM Unwind Behavior",
+    [UWTableKindNone, UWTableKindSync, UWTableKindAsync]> {
+  let cppNamespace = "::mlir::LLVM::uwtable";
+}
+
 #endif // LLVMIR_ENUMS
diff --git a/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td b/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td
index e89e78aec7147..76a2ec47b3a22 100644
--- a/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td
+++ b/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td
@@ -1842,7 +1842,8 @@ def LLVM_LLVMFuncOp : LLVM_Op<"func", [
     OptionalAttr<LLVM_VecTypeHintAttr>:$vec_type_hint,
     OptionalAttr<DenseI32ArrayAttr>:$work_group_size_hint,
     OptionalAttr<DenseI32ArrayAttr>:$reqd_work_group_size,
-    OptionalAttr<I32Attr>:$intel_reqd_sub_group_size
+    OptionalAttr<I32Attr>:$intel_reqd_sub_group_size,
+    OptionalAttr<UWTableKindAttr>:$uwtable_kind
   );
 
   let regions = (region AnyRegion:$body);
diff --git a/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp b/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
index ee7dc3a5231f4..6531b233b94a0 100644
--- a/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
+++ b/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
@@ -1602,6 +1602,8 @@ static void convertFunctionAttributes(LLVMFuncOp func,
   if (FramePointerKindAttr fpAttr = func.getFramePointerAttr())
     llvmFunc->addFnAttr("frame-pointer", stringifyFramePointerKind(
                                              fpAttr.getFramePointerKind()));
+  if (UWTableKindAttr uwTableKindAttr = func.getUwtableKindAttr())
+    llvmFunc->setUWTableKind(convertUWTableKindToLLVM(uwTableKindAttr.getUwtableKind()));
   convertFunctionMemoryAttributes(func, llvmFunc);
 }
 
diff --git a/mlir/test/Target/LLVMIR/uwtable.mlir b/mlir/test/Target/LLVMIR/uwtable.mlir
new file mode 100644
index 0000000000000..3e1d5b566f01b
--- /dev/null
+++ b/mlir/test/Target/LLVMIR/uwtable.mlir
@@ -0,0 +1,8 @@
+// RUN: mlir-translate -mlir-to-llvmir %s | FileCheck %s
+
+// CHECK-LABEL: define void @uwtable_func() 
+// CHECK-SAME: #[[ATTRS:[0-9]+]]
+llvm.func @uwtable_func() attributes {uwtable_kind = #llvm.uwtableKind<"sync">}  {
+  llvm.return
+}
+// CHECK: attributes #[[ATTRS]] = { uwtable(sync) }

Copy link

github-actions bot commented Apr 15, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

Comment on lines 871 to 870
// UWTableKindDefault is unsupported as the llvm enum value is the same as async
// which the generated enum converters can't deal with.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure what the best way of dealing with this is, or if we are happy with this behavior (the issue is duplicate cases in the switch in the generated converter)

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah lets drop the default kind above 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.

done

@WillFroom WillFroom force-pushed the mlir_add_uwtable_attr branch from 882829b to 0359620 Compare April 15, 2025 16:42
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.

Thanks!

Can you add the attribute to ModuleImport.cpp as well so that we can import it from LLVM IR. The easiest way is to search for frame-pointer in ModuleImport.cpp and perform the same changes for the new attribute. A small import test makes sense as well.

: LLVM_EnumAttrCase<"Sync", "sync", "Sync", 1>;
def UWTableKindAsync
: LLVM_EnumAttrCase<"Async", "async", "Async", 2>;
def UWTableKindDefault
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you delete UWTableKindDefault since it seems to be unused?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

Comment on lines 871 to 870
// UWTableKindDefault is unsupported as the llvm enum value is the same as async
// which the generated enum converters can't deal with.
Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah lets drop the default kind above then?

@WillFroom WillFroom force-pushed the mlir_add_uwtable_attr branch 3 times, most recently from 0a2695d to d157475 Compare April 16, 2025 09:12
@WillFroom
Copy link
Contributor Author

Thanks!

Can you add the attribute to ModuleImport.cpp as well so that we can import it from LLVM IR. The easiest way is to search for frame-pointer in ModuleImport.cpp and perform the same changes for the new attribute. A small import test makes sense as well.

Done & added test

@WillFroom
Copy link
Contributor Author

Thanks for the review @gysit, feel free to merge if you are happy

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 modulo some last nits.

Especially, keeping kExplicitAttributes sorted would be great. I am happy to land the PR once the last nits have been addressed.

Comment on lines 1376 to 1377


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

ultra nit: drop extra newline.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@@ -2077,6 +2077,7 @@ static constexpr std::array kExplicitAttributes{
StringLiteral("unsafe-fp-math"),
StringLiteral("vscale_range"),
StringLiteral("willreturn"),
StringLiteral("uwtable"),
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Can you sort in alphabetical order?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done


; CHECK-LABEL: llvm.func @uwtable_func
; CHECK-SAME: attributes {uwtable_kind = #llvm.uwtableKind<sync>}

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

ultra nit: drop extra newline

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@WillFroom WillFroom force-pushed the mlir_add_uwtable_attr branch from d157475 to 30bd5a3 Compare April 17, 2025 08:43
@basioli-k basioli-k merged commit 046a1e6 into llvm:main Apr 17, 2025
6 of 11 checks passed
@WillFroom
Copy link
Contributor Author

Thanks again @gysit for the review, I had @basioli-k land it, hope that's ok

@gysit
Copy link
Contributor

gysit commented Apr 17, 2025

I had @basioli-k land it, hope that's ok

of course no problem!

var-const pushed a commit to ldionne/llvm-project that referenced this pull request Apr 17, 2025
Add `UWTableKind` enum and corresponding attribute to `llvm.func`
including translation to `llvm::Function` attribute.
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