Skip to content

Commit

Permalink
[Attributor][FIX] Properly check for accesses to globals
Browse files Browse the repository at this point in the history
The check if globals were accessed was not always working because two
bits are set for NO_GLOBAL_MEM. The new check works also if only on kind
of globals (internal/external) is accessed.
  • Loading branch information
jdoerfert committed Apr 16, 2020
1 parent ad9c284 commit 253d6be
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 5 deletions.
7 changes: 4 additions & 3 deletions llvm/lib/Transforms/IPO/AttributorAttributes.cpp
Expand Up @@ -6251,8 +6251,9 @@ AAMemoryLocationImpl::categorizeAccessedLocations(Attributor &A, Instruction &I,
nullptr, Changed);
}

// Now handle global memory if it might be accessed.
bool HasGlobalAccesses = !(ICSAssumedNotAccessedLocs & NO_GLOBAL_MEM);
// Now handle global memory if it might be accessed. This is slightly tricky
// as NO_GLOBAL_MEM has multiple bits set.
bool HasGlobalAccesses = ((~ICSAssumedNotAccessedLocs) & NO_GLOBAL_MEM);
if (HasGlobalAccesses) {
auto AccessPred = [&](const Instruction *, const Value *Ptr,
AccessKind Kind, MemoryLocationsKind MLK) {
Expand All @@ -6270,7 +6271,7 @@ AAMemoryLocationImpl::categorizeAccessedLocations(Attributor &A, Instruction &I,
<< getMemoryLocationsAsStr(AccessedLocs.getAssumed()) << "\n");

// Now handle argument memory if it might be accessed.
bool HasArgAccesses = !(ICSAssumedNotAccessedLocs & NO_ARGUMENT_MEM);
bool HasArgAccesses = ((~ICSAssumedNotAccessedLocs) & NO_ARGUMENT_MEM);
if (HasArgAccesses) {
for (unsigned ArgNo = 0, e = ICS.getNumArgOperands(); ArgNo < e;
++ArgNo) {
Expand Down
44 changes: 44 additions & 0 deletions llvm/test/Transforms/Attributor/memory_locations.ll
Expand Up @@ -397,3 +397,47 @@ define void @callerE(i8* %arg) {
ret void
}

@G = external dso_local global i32, align 4

; CHECK: Function Attrs:
; CHECK-SAME: writeonly
define void @write_global() {
; CHECK-LABEL: define {{[^@]+}}@write_global()
; CHECK-NEXT: store i32 0, i32* @G, align 4
; CHECK-NEXT: ret void
;
store i32 0, i32* @G, align 4
ret void
}
; CHECK: Function Attrs: argmemonly
; CHECK-SAME: writeonly
define void @write_global_via_arg(i32* %GPtr) {
; CHECK-LABEL: define {{[^@]+}}@write_global_via_arg
; CHECK-SAME: (i32* nocapture nofree nonnull writeonly align 4 dereferenceable(4) [[GPTR:%.*]])
; CHECK-NEXT: store i32 0, i32* [[GPTR]], align 4
; CHECK-NEXT: ret void
;
store i32 0, i32* %GPtr, align 4
ret void
}

; CHECK: Function Attrs:
; CHECK-SAME: writeonly
define void @writeonly_global() {
; CHECK-LABEL: define {{[^@]+}}@writeonly_global()
; CHECK-NEXT: call void @write_global()
; CHECK-NEXT: ret void
;
call void @write_global()
ret void
}
; CHECK: Function Attrs:
; CHECK-SAME: writeonly
define void @writeonly_global_via_arg() {
; CHECK-LABEL: define {{[^@]+}}@writeonly_global_via_arg()
; CHECK-NEXT: call void @write_global_via_arg(i32* nofree nonnull writeonly align 4 dereferenceable(4) @G)
; CHECK-NEXT: ret void
;
call void @write_global_via_arg(i32* @G)
ret void
}
4 changes: 2 additions & 2 deletions llvm/test/Transforms/Attributor/nocapture-1.ll
@@ -1,6 +1,6 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --function-signature --scrub-attributes
; RUN: opt -attributor -attributor-manifest-internal -attributor-disable=false -attributor-max-iterations-verify -attributor-annotate-decl-cs -attributor-max-iterations=4 -S < %s | FileCheck %s --check-prefixes=CHECK,NOT_CGSCC_NPM,NOT_CGSCC_OPM,NOT_TUNIT_NPM,IS__TUNIT____,IS________OPM,IS__TUNIT_OPM
; RUN: opt -aa-pipeline=basic-aa -passes=attributor -attributor-manifest-internal -attributor-disable=false -attributor-max-iterations-verify -attributor-annotate-decl-cs -attributor-max-iterations=4 -S < %s | FileCheck %s --check-prefixes=CHECK,NOT_CGSCC_OPM,NOT_CGSCC_NPM,NOT_TUNIT_OPM,IS__TUNIT____,IS________NPM,IS__TUNIT_NPM
; RUN: opt -attributor -attributor-manifest-internal -attributor-disable=false -attributor-max-iterations-verify -attributor-annotate-decl-cs -attributor-max-iterations=6 -S < %s | FileCheck %s --check-prefixes=CHECK,NOT_CGSCC_NPM,NOT_CGSCC_OPM,NOT_TUNIT_NPM,IS__TUNIT____,IS________OPM,IS__TUNIT_OPM
; RUN: opt -aa-pipeline=basic-aa -passes=attributor -attributor-manifest-internal -attributor-disable=false -attributor-max-iterations-verify -attributor-annotate-decl-cs -attributor-max-iterations=6 -S < %s | FileCheck %s --check-prefixes=CHECK,NOT_CGSCC_OPM,NOT_CGSCC_NPM,NOT_TUNIT_OPM,IS__TUNIT____,IS________NPM,IS__TUNIT_NPM
; RUN: opt -attributor-cgscc -attributor-manifest-internal -attributor-disable=false -attributor-annotate-decl-cs -S < %s | FileCheck %s --check-prefixes=CHECK,NOT_TUNIT_NPM,NOT_TUNIT_OPM,NOT_CGSCC_NPM,IS__CGSCC____,IS________OPM,IS__CGSCC_OPM
; RUN: opt -aa-pipeline=basic-aa -passes=attributor-cgscc -attributor-manifest-internal -attributor-disable=false -attributor-annotate-decl-cs -S < %s | FileCheck %s --check-prefixes=CHECK,NOT_TUNIT_NPM,NOT_TUNIT_OPM,NOT_CGSCC_OPM,IS__CGSCC____,IS________NPM,IS__CGSCC_NPM

Expand Down

0 comments on commit 253d6be

Please sign in to comment.