Skip to content

[IR] Do not set none for function uwtable #93387

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 3 commits into from
Jun 2, 2024
Merged

Conversation

caojoshua
Copy link
Contributor

This avoids the pitfall where we set the uwtable to none:

func.setUWTableKind(llvm::UWTableKind::None)

Attribute::getAsString() would see an unknown attribute and fail an assertion. In this patch, we assert that we do not see a None uwtable kind.

This also skips the check of UWTableKind::Async. It is dominated by the check of UWTableKind::Default, which has the same enum value (nfc).

This avoids the pitfall where we set the uwtable to none:
```
func.setUWTableKind(llvm::UWTableKind::None)
```
`Attribute::getAsString()` would see an unknown attribute and fail an
assertion. In this patch, we assert that we do not see a None uwtable
kind.

This also skips the check of `UWTableKind::Async`. It is dominated by
the check of `UWTableKind::Default`, which has the same enum value
(nfc).
@llvmbot
Copy link
Member

llvmbot commented May 25, 2024

@llvm/pr-subscribers-llvm-ir

Author: Joshua Cao (caojoshua)

Changes

This avoids the pitfall where we set the uwtable to none:

func.setUWTableKind(llvm::UWTableKind::None)

Attribute::getAsString() would see an unknown attribute and fail an assertion. In this patch, we assert that we do not see a None uwtable kind.

This also skips the check of UWTableKind::Async. It is dominated by the check of UWTableKind::Default, which has the same enum value (nfc).


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

3 Files Affected:

  • (modified) llvm/include/llvm/IR/Function.h (+2-1)
  • (modified) llvm/lib/CodeGen/MachineOutliner.cpp (+1-2)
  • (modified) llvm/lib/IR/Attributes.cpp (+3-7)
diff --git a/llvm/include/llvm/IR/Function.h b/llvm/include/llvm/IR/Function.h
index cb514cde95b51..be745a0083cb7 100644
--- a/llvm/include/llvm/IR/Function.h
+++ b/llvm/include/llvm/IR/Function.h
@@ -654,7 +654,8 @@ class LLVM_EXTERNAL_VISIBILITY Function : public GlobalObject,
     return getUWTableKind() != UWTableKind::None;
   }
   void setUWTableKind(UWTableKind K) {
-    addFnAttr(Attribute::getWithUWTableKind(getContext(), K));
+    if (K != UWTableKind::None)
+      addFnAttr(Attribute::getWithUWTableKind(getContext(), K));
   }
   /// True if this function needs an unwind table.
   bool needsUnwindTableEntry() const {
diff --git a/llvm/lib/CodeGen/MachineOutliner.cpp b/llvm/lib/CodeGen/MachineOutliner.cpp
index dc2f5ef15206e..f174dd857def0 100644
--- a/llvm/lib/CodeGen/MachineOutliner.cpp
+++ b/llvm/lib/CodeGen/MachineOutliner.cpp
@@ -717,8 +717,7 @@ MachineFunction *MachineOutliner::createOutlinedFunction(
       [](UWTableKind K, const outliner::Candidate &C) {
         return std::max(K, C.getMF()->getFunction().getUWTableKind());
       });
-  if (UW != UWTableKind::None)
-    F->setUWTableKind(UW);
+  F->setUWTableKind(UW);
 
   BasicBlock *EntryBB = BasicBlock::Create(C, "entry", F);
   IRBuilder<> Builder(EntryBB);
diff --git a/llvm/lib/IR/Attributes.cpp b/llvm/lib/IR/Attributes.cpp
index c8d6bdd423878..cc3a2ceb6de84 100644
--- a/llvm/lib/IR/Attributes.cpp
+++ b/llvm/lib/IR/Attributes.cpp
@@ -526,13 +526,9 @@ std::string Attribute::getAsString(bool InAttrGrp) const {
 
   if (hasAttribute(Attribute::UWTable)) {
     UWTableKind Kind = getUWTableKind();
-    if (Kind != UWTableKind::None) {
-      return Kind == UWTableKind::Default
-                 ? "uwtable"
-                 : ("uwtable(" +
-                    Twine(Kind == UWTableKind::Sync ? "sync" : "async") + ")")
-                       .str();
-    }
+    assert(Kind != UWTableKind::None &&
+           "uwtable attribute should not be none");
+    return Kind == UWTableKind::Default ? "uwtable" : "uwtable(sync)";
   }
 
   if (hasAttribute(Attribute::AllocKind)) {

@caojoshua
Copy link
Contributor Author

caojoshua commented May 25, 2024

IREE sets a None uwtable kind. With a IREE build with assertions, when printing LLVM IR with --print-after-all, I get

Unknown attribute
UNREACHABLE executed at /home/josh/src/iree/third_party/llvm-project/llvm/lib/IR/Attributes.cpp:643!

Copy link

github-actions bot commented May 25, 2024

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

Twine(Kind == UWTableKind::Sync ? "sync" : "async") + ")")
.str();
}
assert(Kind != UWTableKind::None &&
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure if the Kind is always expected to not be UWTableKind::None, but the current code seems to make that assumption. If it is None, we fall through to the llvm_unreachable at the bottom of the function.

Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

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

LGTM

@caojoshua caojoshua merged commit ab08df2 into llvm:main Jun 2, 2024
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants