Skip to content

Commit ab08df2

Browse files
authored
[IR] Do not set none for function uwtable (#93387)
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).
1 parent 4ce6542 commit ab08df2

File tree

4 files changed

+30
-10
lines changed

4 files changed

+30
-10
lines changed

llvm/include/llvm/IR/Function.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,10 @@ class LLVM_EXTERNAL_VISIBILITY Function : public GlobalObject,
654654
return getUWTableKind() != UWTableKind::None;
655655
}
656656
void setUWTableKind(UWTableKind K) {
657-
addFnAttr(Attribute::getWithUWTableKind(getContext(), K));
657+
if (K == UWTableKind::None)
658+
removeFnAttr(Attribute::UWTable);
659+
else
660+
addFnAttr(Attribute::getWithUWTableKind(getContext(), K));
658661
}
659662
/// True if this function needs an unwind table.
660663
bool needsUnwindTableEntry() const {

llvm/lib/CodeGen/MachineOutliner.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -717,8 +717,7 @@ MachineFunction *MachineOutliner::createOutlinedFunction(
717717
[](UWTableKind K, const outliner::Candidate &C) {
718718
return std::max(K, C.getMF()->getFunction().getUWTableKind());
719719
});
720-
if (UW != UWTableKind::None)
721-
F->setUWTableKind(UW);
720+
F->setUWTableKind(UW);
722721

723722
BasicBlock *EntryBB = BasicBlock::Create(C, "entry", F);
724723
IRBuilder<> Builder(EntryBB);

llvm/lib/IR/Attributes.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -526,13 +526,8 @@ std::string Attribute::getAsString(bool InAttrGrp) const {
526526

527527
if (hasAttribute(Attribute::UWTable)) {
528528
UWTableKind Kind = getUWTableKind();
529-
if (Kind != UWTableKind::None) {
530-
return Kind == UWTableKind::Default
531-
? "uwtable"
532-
: ("uwtable(" +
533-
Twine(Kind == UWTableKind::Sync ? "sync" : "async") + ")")
534-
.str();
535-
}
529+
assert(Kind != UWTableKind::None && "uwtable attribute should not be none");
530+
return Kind == UWTableKind::Default ? "uwtable" : "uwtable(sync)";
536531
}
537532

538533
if (hasAttribute(Attribute::AllocKind)) {

llvm/unittests/IR/FunctionTest.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,4 +486,27 @@ TEST(FunctionTest, EraseBBs) {
486486
It = F->erase(F->begin(), F->end());
487487
EXPECT_EQ(F->size(), 0u);
488488
}
489+
490+
TEST(FunctionTest, UWTable) {
491+
LLVMContext Ctx;
492+
std::unique_ptr<Module> M = parseIR(Ctx, R"(
493+
define void @foo() {
494+
bb1:
495+
ret void
496+
}
497+
)");
498+
499+
Function &F = *M->getFunction("foo");
500+
501+
EXPECT_FALSE(F.hasUWTable());
502+
EXPECT_TRUE(F.getUWTableKind() == UWTableKind::None);
503+
504+
F.setUWTableKind(UWTableKind::Async);
505+
EXPECT_TRUE(F.hasUWTable());
506+
EXPECT_TRUE(F.getUWTableKind() == UWTableKind::Async);
507+
508+
F.setUWTableKind(UWTableKind::None);
509+
EXPECT_FALSE(F.hasUWTable());
510+
EXPECT_TRUE(F.getUWTableKind() == UWTableKind::None);
511+
}
489512
} // end namespace

0 commit comments

Comments
 (0)