Skip to content

[MergeFunc] Handle ConstantRange attributes. #88584

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 2 commits into from
Apr 13, 2024
Merged
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
12 changes: 12 additions & 0 deletions llvm/lib/Transforms/Utils/FunctionComparator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,18 @@ int FunctionComparator::cmpAttrs(const AttributeList L,
if (int Res = cmpNumbers((uint64_t)TyL, (uint64_t)TyR))
return Res;
continue;
} else if (LA.isConstantRangeAttribute() &&
RA.isConstantRangeAttribute()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This still needs the getKindAsEnum check (in case there are more ConstantRange attributes in the future).

Might make sense to just move that check outside the isTypeAttribute branch and always perform it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

nice catch. added the check. it needs to be inside the if branch as otherwise string attribute will not work

if (LA.getKindAsEnum() != RA.getKindAsEnum())
return cmpNumbers(LA.getKindAsEnum(), RA.getKindAsEnum());

ConstantRange LCR = LA.getRange();
ConstantRange RCR = RA.getRange();
if (int Res = cmpAPInts(LCR.getLower(), RCR.getLower()))
return Res;
if (int Res = cmpAPInts(LCR.getUpper(), RCR.getUpper()))
return Res;
continue;
}
if (LA < RA)
return -1;
Expand Down
111 changes: 111 additions & 0 deletions llvm/test/Transforms/MergeFunc/call-and-invoke-with-ranges-attr.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
; RUN: opt -passes=mergefunc -S < %s | FileCheck %s

define i8 @call_with_range_attr(i8 range(i8 0, 2) %v) {
%out = call i8 @dummy2(i8 %v)
ret i8 %out
}

define i8 @call_no_range_attr(i8 %v) {
; CHECK-LABEL: @call_no_range_attr
; CHECK-NEXT: %out = call i8 @dummy2(i8 %v)
; CHECK-NEXT: ret i8 %out
%out = call i8 @dummy2(i8 %v)
ret i8 %out
}

define i8 @call_different_range_attr(i8 range(i8 5, 7) %v) {
; CHECK-LABEL: @call_different_range_attr
; CHECK-NEXT: %out = call i8 @dummy2(i8 %v)
; CHECK-NEXT: ret i8 %out
%out = call i8 @dummy2(i8 %v)
ret i8 %out
}

define i8 @call_with_range() {
%out = call range(i8 0, 2) i8 @dummy()
ret i8 %out
}

define i8 @call_no_range() {
; CHECK-LABEL: @call_no_range
; CHECK-NEXT: %out = call i8 @dummy()
; CHECK-NEXT: ret i8 %out
%out = call i8 @dummy()
ret i8 %out
}

define i8 @call_different_range() {
; CHECK-LABEL: @call_different_range
; CHECK-NEXT: %out = call range(i8 5, 7) i8 @dummy()
; CHECK-NEXT: ret i8 %out
%out = call range(i8 5, 7) i8 @dummy()
ret i8 %out
}

define i8 @invoke_with_range() personality ptr undef {
%out = invoke range(i8 0, 2) i8 @dummy() to label %next unwind label %lpad

next:
ret i8 %out

lpad:
%pad = landingpad { ptr, i32 } cleanup
resume { ptr, i32 } zeroinitializer
}

define i8 @invoke_no_range() personality ptr undef {
; CHECK-LABEL: @invoke_no_range()
; CHECK-NEXT: invoke i8 @dummy
%out = invoke i8 @dummy() to label %next unwind label %lpad

next:
ret i8 %out

lpad:
%pad = landingpad { ptr, i32 } cleanup
resume { ptr, i32 } zeroinitializer
}

define i8 @invoke_different_range() personality ptr undef {
; CHECK-LABEL: @invoke_different_range()
; CHECK-NEXT: invoke range(i8 5, 7) i8 @dummy
%out = invoke range(i8 5, 7) i8 @dummy() to label %next unwind label %lpad

next:
ret i8 %out

lpad:
%pad = landingpad { ptr, i32 } cleanup
resume { ptr, i32 } zeroinitializer
}

define i8 @invoke_with_same_range() personality ptr undef {
; CHECK-LABEL: @invoke_with_same_range()
; CHECK: tail call i8 @invoke_with_range()
%out = invoke range(i8 0, 2) i8 @dummy() to label %next unwind label %lpad

next:
ret i8 %out

lpad:
%pad = landingpad { ptr, i32 } cleanup
resume { ptr, i32 } zeroinitializer
}

define i8 @call_with_same_range() {
; CHECK-LABEL: @call_with_same_range
; CHECK: tail call i8 @call_with_range
%out = call range(i8 0, 2) i8 @dummy()
ret i8 %out
}

define i8 @call_with_same_range_attr(i8 range(i8 0, 2) %v) {
; CHECK-LABEL: @call_with_same_range_attr
; CHECK: tail call i8 @call_with_range_attr
%out = call i8 @dummy2(i8 %v)
ret i8 %out
}

declare i8 @dummy();
declare i8 @dummy2(i8);
declare i32 @__gxx_personality_v0(...)