Skip to content

Commit

Permalink
[inline] Clone return range attribute on the callsite into inlined call
Browse files Browse the repository at this point in the history
  • Loading branch information
andjo403 committed May 28, 2024
1 parent 1bc922a commit 4472fc7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
6 changes: 3 additions & 3 deletions clang/test/Headers/__clang_hip_math.hip
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ extern "C" __device__ uint64_t test___make_mantissa(const char *p) {

// CHECK-LABEL: @test_abs(
// CHECK-NEXT: entry:
// CHECK-NEXT: [[TMP0:%.*]] = tail call noundef i32 @llvm.abs.i32(i32 [[X:%.*]], i1 true)
// CHECK-NEXT: [[TMP0:%.*]] = tail call noundef range(i32 0, -2147483648) i32 @llvm.abs.i32(i32 [[X:%.*]], i1 true)
// CHECK-NEXT: ret i32 [[TMP0]]
//
extern "C" __device__ int test_abs(int x) {
Expand All @@ -240,7 +240,7 @@ extern "C" __device__ int test_abs(int x) {

// CHECK-LABEL: @test_labs(
// CHECK-NEXT: entry:
// CHECK-NEXT: [[TMP0:%.*]] = tail call noundef i64 @llvm.abs.i64(i64 [[X:%.*]], i1 true)
// CHECK-NEXT: [[TMP0:%.*]] = tail call noundef range(i64 0, -9223372036854775808) i64 @llvm.abs.i64(i64 [[X:%.*]], i1 true)
// CHECK-NEXT: ret i64 [[TMP0]]
//
extern "C" __device__ long test_labs(long x) {
Expand All @@ -249,7 +249,7 @@ extern "C" __device__ long test_labs(long x) {

// CHECK-LABEL: @test_llabs(
// CHECK-NEXT: entry:
// CHECK-NEXT: [[TMP0:%.*]] = tail call noundef i64 @llvm.abs.i64(i64 [[X:%.*]], i1 true)
// CHECK-NEXT: [[TMP0:%.*]] = tail call noundef range(i64 0, -9223372036854775808) i64 @llvm.abs.i64(i64 [[X:%.*]], i1 true)
// CHECK-NEXT: ret i64 [[TMP0]]
//
extern "C" __device__ long long test_llabs(long x) {
Expand Down
13 changes: 12 additions & 1 deletion llvm/lib/Transforms/Utils/InlineFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@
#include "llvm/Analysis/ProfileSummaryInfo.h"
#include "llvm/Analysis/ValueTracking.h"
#include "llvm/Analysis/VectorUtils.h"
#include "llvm/IR/AttributeMask.h"
#include "llvm/IR/Argument.h"
#include "llvm/IR/AttributeMask.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/CFG.h"
#include "llvm/IR/Constant.h"
#include "llvm/IR/ConstantRange.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/DebugInfo.h"
Expand Down Expand Up @@ -1450,6 +1451,8 @@ static AttrBuilder IdentifyValidPoisonGeneratingAttributes(CallBase &CB) {
Valid.addAttribute(Attribute::NonNull);
if (CB.hasRetAttr(Attribute::Alignment))
Valid.addAlignmentAttr(CB.getRetAlign());
if (std::optional<ConstantRange> Range = CB.getRange())
Valid.addRangeAttr(*Range);
return Valid;
}

Expand Down Expand Up @@ -1541,6 +1544,14 @@ static void AddReturnAttributes(CallBase &CB, ValueToValueMapTy &VMap) {
if (ValidPG.getAlignment().valueOrOne() < AL.getRetAlignment().valueOrOne())
ValidPG.removeAttribute(Attribute::Alignment);
if (ValidPG.hasAttributes()) {
Attribute CBRange = ValidPG.getAttribute(Attribute::Range);
if (CBRange.isValid()) {
Attribute NewRange = AL.getRetAttr(Attribute::Range);
if (NewRange.isValid()) {
ValidPG.addRangeAttr(
CBRange.getRange().intersectWith(NewRange.getRange()));
}
}
// Three checks.
// If the callsite has `noundef`, then a poison due to violating the
// return attribute will create UB anyways so we can always propagate.
Expand Down
6 changes: 3 additions & 3 deletions llvm/test/Transforms/Inline/ret_attr_align_and_noundef.ll
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ define i8 @callee13() {

define i8 @caller13_okay_use_after_poison_anyways() {
; CHECK-LABEL: define i8 @caller13_okay_use_after_poison_anyways() {
; CHECK-NEXT: [[R_I:%.*]] = call i8 @val8()
; CHECK-NEXT: [[R_I:%.*]] = call range(i8 0, 10) i8 @val8()
; CHECK-NEXT: call void @use.val(i8 [[R_I]])
; CHECK-NEXT: ret i8 [[R_I]]
;
Expand Down Expand Up @@ -382,7 +382,7 @@ define i8 @caller14_fail_creates_ub() {

define i8 @caller14_okay_is_ub_anyways() {
; CHECK-LABEL: define i8 @caller14_okay_is_ub_anyways() {
; CHECK-NEXT: [[R_I:%.*]] = call noundef i8 @val8()
; CHECK-NEXT: [[R_I:%.*]] = call noundef range(i8 0, 10) i8 @val8()
; CHECK-NEXT: call void @use.val(i8 [[R_I]])
; CHECK-NEXT: ret i8 [[R_I]]
;
Expand All @@ -402,7 +402,7 @@ define i8 @callee15() {

define i8 @caller15_okay_intersect_ranges() {
; CHECK-LABEL: define i8 @caller15_okay_intersect_ranges() {
; CHECK-NEXT: [[R_I:%.*]] = call range(i8 5, 10) i8 @val8()
; CHECK-NEXT: [[R_I:%.*]] = call range(i8 5, 7) i8 @val8()
; CHECK-NEXT: call void @use.val(i8 [[R_I]])
; CHECK-NEXT: ret i8 [[R_I]]
;
Expand Down

0 comments on commit 4472fc7

Please sign in to comment.