Skip to content
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

[SimplifyCFG][Attributes] Enabling sinking calls with differing number of attrsets #110896

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions llvm/lib/IR/Attributes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/FoldingSet.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallSet.h"
goldsteinn marked this conversation as resolved.
Show resolved Hide resolved
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/StringRef.h"
Expand Down Expand Up @@ -1800,12 +1801,10 @@ AttributeList::intersectWith(LLVMContext &C, AttributeList Other) const {
if (*this == Other)
return *this;

// At least for now, only intersect lists with same number of params.
if (getNumAttrSets() != Other.getNumAttrSets())
return std::nullopt;

SmallVector<std::pair<unsigned, AttributeSet>> IntersectedAttrs;
for (unsigned Idx : indexes()) {
auto IndexIt =
index_iterator(std::max(getNumAttrSets(), Other.getNumAttrSets()));
for (unsigned Idx : IndexIt) {
auto IntersectedAS =
getAttributes(Idx).intersectWith(C, Other.getAttributes(Idx));
// If any index fails to intersect, fail.
Expand Down
3 changes: 2 additions & 1 deletion llvm/lib/IR/Instruction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -875,7 +875,8 @@ bool Instruction::isIdenticalToWhenDefined(const Instruction *I,

// If both instructions have no operands, they are identical.
if (getNumOperands() == 0 && I->getNumOperands() == 0)
return this->hasSameSpecialState(I);
return this->hasSameSpecialState(I, /*IgnoreAlignment=*/false,
IntersectAttrs);

// We have two instructions of identical opcode and #operands. Check to see
// if all operands are the same.
Expand Down
94 changes: 75 additions & 19 deletions llvm/test/Transforms/SimplifyCFG/sink-cb-diff-attrs.ll
Original file line number Diff line number Diff line change
@@ -1,10 +1,65 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --function-signature --check-attributes --check-globals
; RUN: opt < %s -passes='simplifycfg<sink-common-insts>' -simplifycfg-require-and-preserve-domtree=1 -S | FileCheck %s

declare ptr @foo0()
declare ptr @foo(ptr %p, i64 %x)
declare ptr @foo2(ptr %p, ptr %p2, i64 %x)
declare void @side.effect()

define ptr @test_sink_no_args_oneside(i1 %c) {
; CHECK-LABEL: define {{[^@]+}}@test_sink_no_args_oneside
; CHECK-SAME: (i1 [[C:%.*]]) {
; CHECK-NEXT: br i1 [[C]], label [[IF:%.*]], label [[END:%.*]]
; CHECK: if:
; CHECK-NEXT: call void @side.effect()
; CHECK-NEXT: br label [[END]]
; CHECK: end:
; CHECK-NEXT: [[R2:%.*]] = call ptr @foo0()
; CHECK-NEXT: ret ptr [[R2]]
;
br i1 %c, label %if, label %else
if:
call void @side.effect()
%r = call ptr @foo0()
br label %end

else:
%r2 = call ptr @foo0() readonly
br label %end
end:
%pr = phi ptr [ %r, %if], [%r2, %else]
ret ptr %pr
}

define ptr @test_sink_no_args_oneside_fail(i1 %c) {
; CHECK-LABEL: define {{[^@]+}}@test_sink_no_args_oneside_fail
; CHECK-SAME: (i1 [[C:%.*]]) {
; CHECK-NEXT: br i1 [[C]], label [[IF:%.*]], label [[ELSE:%.*]]
; CHECK: if:
; CHECK-NEXT: call void @side.effect()
; CHECK-NEXT: [[R:%.*]] = call ptr @foo0()
; CHECK-NEXT: br label [[END:%.*]]
; CHECK: else:
; CHECK-NEXT: [[R2:%.*]] = call ptr @foo0() #[[ATTR0:[0-9]+]]
; CHECK-NEXT: br label [[END]]
; CHECK: end:
; CHECK-NEXT: [[PR:%.*]] = phi ptr [ [[R]], [[IF]] ], [ [[R2]], [[ELSE]] ]
; CHECK-NEXT: ret ptr [[PR]]
;
br i1 %c, label %if, label %else
if:
call void @side.effect()
%r = call ptr @foo0()
br label %end

else:
%r2 = call ptr @foo0() readonly alwaysinline
br label %end
end:
%pr = phi ptr [ %r, %if], [%r2, %else]
ret ptr %pr
}

define ptr @test_sink_int_attrs(i1 %c, ptr %p, ptr %p2, i64 %x) {
; CHECK-LABEL: define {{[^@]+}}@test_sink_int_attrs
; CHECK-SAME: (i1 [[C:%.*]], ptr [[P:%.*]], ptr [[P2:%.*]], i64 [[X:%.*]]) {
Expand All @@ -13,7 +68,7 @@ define ptr @test_sink_int_attrs(i1 %c, ptr %p, ptr %p2, i64 %x) {
; CHECK-NEXT: call void @side.effect()
; CHECK-NEXT: br label [[END]]
; CHECK: end:
; CHECK-NEXT: [[R2:%.*]] = call ptr @foo2(ptr align 32 dereferenceable_or_null(100) [[P]], ptr align 32 dereferenceable(50) [[P2]], i64 range(i64 10, 100000) [[X]]) #[[ATTR0:[0-9]+]]
; CHECK-NEXT: [[R2:%.*]] = call ptr @foo2(ptr align 32 dereferenceable_or_null(100) [[P]], ptr align 32 dereferenceable(50) [[P2]], i64 range(i64 10, 100000) [[X]]) #[[ATTR1:[0-9]+]]
; CHECK-NEXT: ret ptr [[R2]]
;
br i1 %c, label %if, label %else
Expand All @@ -38,7 +93,7 @@ define ptr @test_sink_int_attrs2(i1 %c, ptr %p, i64 %x) {
; CHECK-NEXT: call void @side.effect()
; CHECK-NEXT: br label [[END]]
; CHECK: end:
; CHECK-NEXT: [[R2:%.*]] = call ptr @foo(ptr dereferenceable(50) [[P]], i64 range(i64 10, 1000) [[X]]) #[[ATTR1:[0-9]+]]
; CHECK-NEXT: [[R2:%.*]] = call ptr @foo(ptr dereferenceable(50) [[P]], i64 range(i64 10, 1000) [[X]]) #[[ATTR2:[0-9]+]]
; CHECK-NEXT: ret ptr [[R2]]
;
br i1 %c, label %if, label %else
Expand All @@ -63,7 +118,7 @@ define ptr @test_sink_bool_attrs2(i1 %c, ptr %p, i64 %x) {
; CHECK-NEXT: call void @side.effect()
; CHECK-NEXT: br label [[END]]
; CHECK: end:
; CHECK-NEXT: [[R2:%.*]] = call noundef ptr @foo(ptr nonnull [[P]], i64 noundef [[X]]) #[[ATTR2:[0-9]+]]
; CHECK-NEXT: [[R2:%.*]] = call noundef ptr @foo(ptr nonnull [[P]], i64 noundef [[X]]) #[[ATTR3:[0-9]+]]
; CHECK-NEXT: ret ptr [[R2]]
;
br i1 %c, label %if, label %else
Expand All @@ -88,7 +143,7 @@ define ptr @test_sink_bool_attrs3(i1 %c, ptr %p, i64 %x) {
; CHECK-NEXT: call void @side.effect()
; CHECK-NEXT: br label [[END]]
; CHECK: end:
; CHECK-NEXT: [[R2:%.*]] = call nonnull ptr @foo(ptr [[P]], i64 noundef [[X]]) #[[ATTR3:[0-9]+]]
; CHECK-NEXT: [[R2:%.*]] = call nonnull ptr @foo(ptr [[P]], i64 noundef [[X]]) #[[ATTR4:[0-9]+]]
; CHECK-NEXT: ret ptr [[R2]]
;
br i1 %c, label %if, label %else
Expand All @@ -111,10 +166,10 @@ define ptr @test_sink_bool_attrs_fail_non_droppable(i1 %c, ptr %p, i64 %x) {
; CHECK-NEXT: br i1 [[C]], label [[IF:%.*]], label [[ELSE:%.*]]
; CHECK: if:
; CHECK-NEXT: call void @side.effect()
; CHECK-NEXT: [[R:%.*]] = call nonnull ptr @foo(ptr noundef readonly [[P]], i64 noundef [[X]]) #[[ATTR4:[0-9]+]]
; CHECK-NEXT: [[R:%.*]] = call nonnull ptr @foo(ptr noundef readonly [[P]], i64 noundef [[X]]) #[[ATTR5:[0-9]+]]
; CHECK-NEXT: br label [[END:%.*]]
; CHECK: else:
; CHECK-NEXT: [[R2:%.*]] = call noundef nonnull ptr @foo(ptr nonnull writeonly [[P]], i64 noundef [[X]]) #[[ATTR5:[0-9]+]]
; CHECK-NEXT: [[R2:%.*]] = call noundef nonnull ptr @foo(ptr nonnull writeonly [[P]], i64 noundef [[X]]) #[[ATTR6:[0-9]+]]
; CHECK-NEXT: br label [[END]]
; CHECK: end:
; CHECK-NEXT: [[PR:%.*]] = phi ptr [ [[R]], [[IF]] ], [ [[R2]], [[ELSE]] ]
Expand All @@ -140,10 +195,10 @@ define ptr @test_sink_bool_attrs_fail_non_droppable2(i1 %c, ptr %p, i64 %x) {
; CHECK-NEXT: br i1 [[C]], label [[IF:%.*]], label [[ELSE:%.*]]
; CHECK: if:
; CHECK-NEXT: call void @side.effect()
; CHECK-NEXT: [[R:%.*]] = call nonnull ptr @foo(ptr noundef readonly [[P]], i64 noundef [[X]]) #[[ATTR6:[0-9]+]]
; CHECK-NEXT: [[R:%.*]] = call nonnull ptr @foo(ptr noundef readonly [[P]], i64 noundef [[X]]) #[[ATTR7:[0-9]+]]
; CHECK-NEXT: br label [[END:%.*]]
; CHECK: else:
; CHECK-NEXT: [[R2:%.*]] = call noundef nonnull ptr @foo(ptr nonnull writeonly byval(i64) [[P]], i64 noundef [[X]]) #[[ATTR5]]
; CHECK-NEXT: [[R2:%.*]] = call noundef nonnull ptr @foo(ptr nonnull writeonly byval(i64) [[P]], i64 noundef [[X]]) #[[ATTR6]]
; CHECK-NEXT: br label [[END]]
; CHECK: end:
; CHECK-NEXT: [[PR:%.*]] = phi ptr [ [[R]], [[IF]] ], [ [[R2]], [[ELSE]] ]
Expand All @@ -169,10 +224,10 @@ define ptr @test_sink_bool_attrs_fail_non_droppable3(i1 %c, ptr %p, i64 %x) {
; CHECK-NEXT: br i1 [[C]], label [[IF:%.*]], label [[ELSE:%.*]]
; CHECK: if:
; CHECK-NEXT: call void @side.effect()
; CHECK-NEXT: [[R:%.*]] = call nonnull ptr @foo(ptr noundef readonly byval(i32) [[P]], i64 noundef [[X]]) #[[ATTR6]]
; CHECK-NEXT: [[R:%.*]] = call nonnull ptr @foo(ptr noundef readonly byval(i32) [[P]], i64 noundef [[X]]) #[[ATTR7]]
; CHECK-NEXT: br label [[END:%.*]]
; CHECK: else:
; CHECK-NEXT: [[R2:%.*]] = call noundef nonnull ptr @foo(ptr nonnull writeonly byval(i64) [[P]], i64 noundef [[X]]) #[[ATTR7:[0-9]+]]
; CHECK-NEXT: [[R2:%.*]] = call noundef nonnull ptr @foo(ptr nonnull writeonly byval(i64) [[P]], i64 noundef [[X]]) #[[ATTR8:[0-9]+]]
; CHECK-NEXT: br label [[END]]
; CHECK: end:
; CHECK-NEXT: [[PR:%.*]] = phi ptr [ [[R]], [[IF]] ], [ [[R2]], [[ELSE]] ]
Expand Down Expand Up @@ -200,7 +255,7 @@ define ptr @test_sink_bool_attrs4(i1 %c, ptr %p, i64 %x) {
; CHECK-NEXT: call void @side.effect()
; CHECK-NEXT: br label [[END]]
; CHECK: end:
; CHECK-NEXT: [[R2:%.*]] = call nonnull ptr @foo(ptr byval(i64) [[P]], i64 noundef [[X]]) #[[ATTR7]]
; CHECK-NEXT: [[R2:%.*]] = call nonnull ptr @foo(ptr byval(i64) [[P]], i64 noundef [[X]]) #[[ATTR8]]
; CHECK-NEXT: ret ptr [[R2]]
;
br i1 %c, label %if, label %else
Expand All @@ -218,12 +273,13 @@ end:
}

;.
; CHECK: attributes #[[ATTR0]] = { memory(readwrite) }
; CHECK: attributes #[[ATTR1]] = { memory(read) }
; CHECK: attributes #[[ATTR2]] = { mustprogress nocallback nofree willreturn }
; CHECK: attributes #[[ATTR3]] = { alwaysinline nosync willreturn }
; CHECK: attributes #[[ATTR4]] = { alwaysinline cold nocallback nofree nosync willreturn }
; CHECK: attributes #[[ATTR5]] = { nosync willreturn }
; CHECK: attributes #[[ATTR6]] = { cold nocallback nofree nosync willreturn }
; CHECK: attributes #[[ATTR7]] = { nosync }
; CHECK: attributes #[[ATTR0]] = { alwaysinline memory(read) }
; CHECK: attributes #[[ATTR1]] = { memory(readwrite) }
; CHECK: attributes #[[ATTR2]] = { memory(read) }
; CHECK: attributes #[[ATTR3]] = { mustprogress nocallback nofree willreturn }
; CHECK: attributes #[[ATTR4]] = { alwaysinline nosync willreturn }
; CHECK: attributes #[[ATTR5]] = { alwaysinline cold nocallback nofree nosync willreturn }
; CHECK: attributes #[[ATTR6]] = { nosync willreturn }
; CHECK: attributes #[[ATTR7]] = { cold nocallback nofree nosync willreturn }
; CHECK: attributes #[[ATTR8]] = { nosync }
;.
53 changes: 50 additions & 3 deletions llvm/unittests/IR/AttributesTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,38 @@ TEST(Attributes, SetIntersectByValAlign) {
}
}

TEST(Attributes, ListIntersectDifferingMustPreserve) {
LLVMContext C;
std::optional<AttributeList> Res;
{
AttributeList AL0;
AttributeList AL1;
AL1 = AL1.addFnAttribute(C, Attribute::ReadOnly);
AL0 = AL0.addParamAttribute(C, 0, Attribute::SExt);
Res = AL0.intersectWith(C, AL1);
ASSERT_FALSE(Res.has_value());
Res = AL1.intersectWith(C, AL0);
ASSERT_FALSE(Res.has_value());
}
{
AttributeList AL0;
AttributeList AL1;
AL1 = AL1.addFnAttribute(C, Attribute::AlwaysInline);
AL0 = AL0.addParamAttribute(C, 0, Attribute::ReadOnly);
Res = AL0.intersectWith(C, AL1);
ASSERT_FALSE(Res.has_value());
Res = AL1.intersectWith(C, AL0);
ASSERT_FALSE(Res.has_value());

AL0 = AL0.addFnAttribute(C, Attribute::AlwaysInline);
AL1 = AL1.addParamAttribute(C, 1, Attribute::SExt);
Res = AL0.intersectWith(C, AL1);
ASSERT_FALSE(Res.has_value());
Res = AL1.intersectWith(C, AL0);
ASSERT_FALSE(Res.has_value());
}
}

TEST(Attributes, ListIntersect) {
LLVMContext C;
AttributeList AL0;
Expand All @@ -610,11 +642,16 @@ TEST(Attributes, ListIntersect) {

AL0 = AL0.addParamAttribute(C, 1, Attribute::NoUndef);
Res = AL0.intersectWith(C, AL1);
ASSERT_FALSE(Res.has_value());
ASSERT_TRUE(Res.has_value());
ASSERT_TRUE(Res->hasRetAttr(Attribute::NoUndef));
ASSERT_FALSE(Res->hasParamAttr(1, Attribute::NoUndef));

AL1 = AL1.addParamAttribute(C, 2, Attribute::NoUndef);
Res = AL0.intersectWith(C, AL1);
ASSERT_FALSE(Res.has_value());
ASSERT_TRUE(Res.has_value());
ASSERT_TRUE(Res->hasRetAttr(Attribute::NoUndef));
ASSERT_FALSE(Res->hasParamAttr(1, Attribute::NoUndef));
ASSERT_FALSE(Res->hasParamAttr(2, Attribute::NoUndef));

AL0 = AL0.addParamAttribute(C, 2, Attribute::NoUndef);
AL1 = AL1.addParamAttribute(C, 1, Attribute::NoUndef);
Expand Down Expand Up @@ -692,7 +729,17 @@ TEST(Attributes, ListIntersect) {

AL1 = AL1.addParamAttribute(C, 3, Attribute::ReadNone);
Res = AL0.intersectWith(C, AL1);
ASSERT_FALSE(Res.has_value());
ASSERT_TRUE(Res.has_value());
ASSERT_TRUE(Res.has_value());
ASSERT_TRUE(Res->hasFnAttr(Attribute::AlwaysInline));
ASSERT_TRUE(Res->hasFnAttr(Attribute::ReadOnly));
ASSERT_TRUE(Res->hasRetAttr(Attribute::NoUndef));
ASSERT_FALSE(Res->hasRetAttr(Attribute::NonNull));
ASSERT_TRUE(Res->hasParamAttr(1, Attribute::NoUndef));
ASSERT_TRUE(Res->hasParamAttr(2, Attribute::NoUndef));
ASSERT_FALSE(Res->hasParamAttr(2, Attribute::NonNull));
ASSERT_FALSE(Res->hasParamAttr(2, Attribute::ReadNone));
ASSERT_FALSE(Res->hasParamAttr(3, Attribute::ReadNone));

AL0 = AL0.addParamAttribute(C, 3, Attribute::ReadNone);
Res = AL0.intersectWith(C, AL1);
Expand Down
Loading