Skip to content

Commit

Permalink
[Attributor] AAPointerInfo can model non-escaping call uses
Browse files Browse the repository at this point in the history
If a call base use will not capture a pointer we can approximate the
effects. This is important especially for readnone/only uses. Even
may-write uses are not too bad with reachability in place. Capturing
is the problem as we loose track of update sides.
  • Loading branch information
jdoerfert committed Oct 5, 2022
1 parent 1940c49 commit 93e51fa
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
34 changes: 28 additions & 6 deletions llvm/lib/Transforms/IPO/AttributorAttributes.cpp
Expand Up @@ -1530,13 +1530,35 @@ struct AAPointerInfoCallSiteArgument final : AAPointerInfoFloating {
// sense to specialize attributes for call sites arguments instead of
// redirecting requests to the callee argument.
Argument *Arg = getAssociatedArgument();
if (!Arg)
if (Arg) {
const IRPosition &ArgPos = IRPosition::argument(*Arg);
auto &ArgAA =
A.getAAFor<AAPointerInfo>(*this, ArgPos, DepClassTy::REQUIRED);
if (ArgAA.getState().isValidState())
return translateAndAddState(A, ArgAA, 0, *cast<CallBase>(getCtxI()),
/* FromCallee */ true);
if (!Arg->getParent()->isDeclaration())
return indicatePessimisticFixpoint();
}

const auto &NoCaptureAA =
A.getAAFor<AANoCapture>(*this, getIRPosition(), DepClassTy::OPTIONAL);

if (!NoCaptureAA.isAssumedNoCapture())
return indicatePessimisticFixpoint();
const IRPosition &ArgPos = IRPosition::argument(*Arg);
auto &ArgAA =
A.getAAFor<AAPointerInfo>(*this, ArgPos, DepClassTy::REQUIRED);
return translateAndAddState(A, ArgAA, 0, *cast<CallBase>(getCtxI()),
/* FromCallee */ true);

bool IsKnown = false;
if (AA::isAssumedReadNone(A, getIRPosition(), *this, IsKnown))
return ChangeStatus::UNCHANGED;
bool ReadOnly = AA::isAssumedReadOnly(A, getIRPosition(), *this, IsKnown);

ChangeStatus Changed = ChangeStatus::UNCHANGED;
handleAccess(A, *getCtxI(), getAssociatedValue(), nullptr,
ReadOnly ? AccessKind::AK_MAY_READ
: AccessKind::AK_MAY_READ_WRITE,
AA::OffsetAndSize::Unknown, Changed, nullptr,
AA::OffsetAndSize::Unknown);
return Changed;
}

/// See AbstractAttribute::trackStatistics()
Expand Down
1 change: 1 addition & 0 deletions llvm/test/Transforms/Attributor/heap_to_stack_gpu.ll
Expand Up @@ -350,6 +350,7 @@ define void @test11() {

; TEST 12
define i32 @irreducible_cfg(i32 %0) {
;
; CHECK-LABEL: define {{[^@]+}}@irreducible_cfg
; CHECK-SAME: (i32 [[TMP0:%.*]]) {
; CHECK-NEXT: [[TMP2:%.*]] = call noalias i8* @malloc(i64 noundef 4)
Expand Down
Expand Up @@ -29,13 +29,15 @@ target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16
;
@GlobalS = internal constant %struct.S { i32 42, double 3.140000e+00, ptr null }, align 8

declare void @harmless_use(ptr nocapture readonly) nofree norecurse nosync nounwind readnone willreturn nocallback

;.
; CHECK: @[[GLOBALS:[a-zA-Z0-9_$"\\.-]+]] = internal constant [[STRUCT_S:%.*]] { i32 42, double 3.140000e+00, ptr null }, align 8
;.
define i32 @testOneFieldGlobalS() {
; CHECK: Function Attrs: nofree norecurse nosync nounwind readnone willreturn
; CHECK-LABEL: define {{[^@]+}}@testOneFieldGlobalS
; CHECK-SAME: () #[[ATTR0:[0-9]+]] {
; CHECK-SAME: () #[[ATTR1:[0-9]+]] {
; CHECK-NEXT: entry:
; CHECK-NEXT: br label [[IF_END:%.*]]
; CHECK: if.then:
Expand All @@ -54,6 +56,7 @@ define i32 @testOneFieldGlobalS() {
;
entry:
%i = load i32, ptr @GlobalS, align 8
call void @harmless_use(ptr @GlobalS)
%cmp = icmp ne i32 %i, 42
br i1 %cmp, label %if.then, label %if.end

Expand Down Expand Up @@ -89,7 +92,7 @@ if.end7: ; preds = %if.then5, %if.end4
define i32 @testOneFieldGlobalS_type_mismatch() {
; CHECK: Function Attrs: nofree norecurse nosync nounwind readnone willreturn
; CHECK-LABEL: define {{[^@]+}}@testOneFieldGlobalS_type_mismatch
; CHECK-SAME: () #[[ATTR0]] {
; CHECK-SAME: () #[[ATTR1]] {
; CHECK-NEXT: entry:
; CHECK-NEXT: [[I:%.*]] = load double, ptr @GlobalS, align 8
; CHECK-NEXT: [[IC:%.*]] = fptosi double [[I]] to i32
Expand Down Expand Up @@ -155,7 +158,7 @@ if.end7: ; preds = %if.then5, %if.end4
define i32 @testOneFieldGlobalS_byte_offset_wrong() {
; CHECK: Function Attrs: nofree norecurse nosync nounwind readnone willreturn
; CHECK-LABEL: define {{[^@]+}}@testOneFieldGlobalS_byte_offset_wrong
; CHECK-SAME: () #[[ATTR0]] {
; CHECK-SAME: () #[[ATTR1]] {
; CHECK-NEXT: entry:
; CHECK-NEXT: [[I:%.*]] = load i32, ptr getelementptr inbounds (i32, ptr @GlobalS, i32 1), align 8
; CHECK-NEXT: [[CMP:%.*]] = icmp ne i32 [[I]], 42
Expand Down Expand Up @@ -217,5 +220,6 @@ if.end7: ; preds = %if.then5, %if.end4
ret i32 %r.2
}
;.
; CHECK: attributes #[[ATTR0]] = { nofree norecurse nosync nounwind readnone willreturn }
; CHECK: attributes #[[ATTR0:[0-9]+]] = { nocallback nofree norecurse nosync nounwind readnone willreturn }
; CHECK: attributes #[[ATTR1]] = { nofree norecurse nosync nounwind readnone willreturn }
;.

0 comments on commit 93e51fa

Please sign in to comment.