Skip to content

Commit c4cfe7a

Browse files
committed
[Attributor] Ignore read accesses to constant memory
The old function attribute deduction pass ignores reads of constant memory and we need to copy this behavior to replace the pass completely. First step are constant globals. TBAA can also describe constant accesses and there are other possibilities. We might want to consider asking the alias analyses that are available but for now this is simpler and cheaper.
1 parent 3f540c0 commit c4cfe7a

File tree

4 files changed

+33
-7
lines changed

4 files changed

+33
-7
lines changed

llvm/lib/Transforms/IPO/AttributorAttributes.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6605,6 +6605,7 @@ void AAMemoryLocationImpl::categorizePtrValue(
66056605
auto VisitValueCB = [&](Value &V, const Instruction *,
66066606
AAMemoryLocation::StateType &T,
66076607
bool Stripped) -> bool {
6608+
// TODO: recognize the TBAA used for constant accesses.
66086609
MemoryLocationsKind MLK = NO_LOCATIONS;
66096610
assert(!isa<GEPOperator>(V) && "GEPs should have been stripped.");
66106611
if (isa<UndefValue>(V))
@@ -6615,6 +6616,13 @@ void AAMemoryLocationImpl::categorizePtrValue(
66156616
else
66166617
MLK = NO_ARGUMENT_MEM;
66176618
} else if (auto *GV = dyn_cast<GlobalValue>(&V)) {
6619+
// Reading constant memory is not treated as a read "effect" by the
6620+
// function attr pass so we won't neither. Constants defined by TBAA are
6621+
// similar. (We know we do not write it because it is constant.)
6622+
if (auto *GVar = dyn_cast<GlobalVariable>(GV))
6623+
if (GVar->isConstant())
6624+
return true;
6625+
66186626
if (GV->hasLocalLinkage())
66196627
MLK = NO_GLOBAL_INTERNAL_MEM;
66206628
else

llvm/test/Transforms/Attributor/ArgumentPromotion/aggregate-promote.ll

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
@G = constant %T { i32 0, i32 0, i32 17, i32 25 }
99

1010
define internal i32 @test(%T* %p) {
11-
; IS__TUNIT____: Function Attrs: nofree nosync nounwind readonly willreturn
11+
; IS__TUNIT____: Function Attrs: nofree nosync nounwind readnone willreturn
1212
; IS__TUNIT____-LABEL: define {{[^@]+}}@test
1313
; IS__TUNIT____-SAME: () [[ATTR0:#.*]] {
1414
; IS__TUNIT____-NEXT: entry:
@@ -19,7 +19,7 @@ define internal i32 @test(%T* %p) {
1919
; IS__TUNIT____-NEXT: [[V:%.*]] = add i32 [[A]], [[B]]
2020
; IS__TUNIT____-NEXT: ret i32 [[V]]
2121
;
22-
; IS__CGSCC____: Function Attrs: nofree norecurse nosync nounwind readonly willreturn
22+
; IS__CGSCC____: Function Attrs: nofree norecurse nosync nounwind readnone willreturn
2323
; IS__CGSCC____-LABEL: define {{[^@]+}}@test
2424
; IS__CGSCC____-SAME: () [[ATTR0:#.*]] {
2525
; IS__CGSCC____-NEXT: entry:
@@ -40,14 +40,14 @@ entry:
4040
}
4141

4242
define i32 @caller() {
43-
; IS__TUNIT____: Function Attrs: nofree nosync nounwind readonly willreturn
43+
; IS__TUNIT____: Function Attrs: nofree nosync nounwind readnone willreturn
4444
; IS__TUNIT____-LABEL: define {{[^@]+}}@caller
4545
; IS__TUNIT____-SAME: () [[ATTR0]] {
4646
; IS__TUNIT____-NEXT: entry:
4747
; IS__TUNIT____-NEXT: [[V:%.*]] = call i32 @test() [[ATTR0]]
4848
; IS__TUNIT____-NEXT: ret i32 [[V]]
4949
;
50-
; IS__CGSCC____: Function Attrs: nofree norecurse nosync nounwind readonly willreturn
50+
; IS__CGSCC____: Function Attrs: nofree norecurse nosync nounwind readnone willreturn
5151
; IS__CGSCC____-LABEL: define {{[^@]+}}@caller
5252
; IS__CGSCC____-SAME: () [[ATTR0]] {
5353
; IS__CGSCC____-NEXT: entry:

llvm/test/Transforms/Attributor/ArgumentPromotion/invalidation.ll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
@G = constant i32 0
1313

1414
define internal i32 @a(i32* %x) {
15-
; CHECK: Function Attrs: nofree nosync nounwind readonly willreturn
15+
; CHECK: Function Attrs: nofree nosync nounwind readnone willreturn
1616
; CHECK-LABEL: define {{[^@]+}}@a
1717
; CHECK-SAME: () [[ATTR0:#.*]] {
1818
; CHECK-NEXT: entry:
@@ -25,7 +25,7 @@ entry:
2525
}
2626

2727
define i32 @b() {
28-
; CHECK: Function Attrs: nofree nosync nounwind readonly willreturn
28+
; CHECK: Function Attrs: nofree nosync nounwind readnone willreturn
2929
; CHECK-LABEL: define {{[^@]+}}@b
3030
; CHECK-SAME: () [[ATTR0]] {
3131
; CHECK-NEXT: entry:
@@ -38,7 +38,7 @@ entry:
3838
}
3939

4040
define i32 @c() {
41-
; CHECK: Function Attrs: nofree nosync nounwind readonly willreturn
41+
; CHECK: Function Attrs: nofree nosync nounwind readnone willreturn
4242
; CHECK-LABEL: define {{[^@]+}}@c
4343
; CHECK-SAME: () [[ATTR0]] {
4444
; CHECK-NEXT: entry:

llvm/test/Transforms/Attributor/readattrs.ll

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,3 +449,21 @@ define void @ptr_use_chain(i8* %ptr) {
449449
call void @escape_i8(i8* %abc9)
450450
ret void
451451
}
452+
453+
@constant_mem = external dso_local constant i32, align 4
454+
define i32 @read_only_constant_mem() {
455+
; IS__TUNIT____: Function Attrs: nofree nosync nounwind readnone willreturn
456+
; IS__TUNIT____-LABEL: define {{[^@]+}}@read_only_constant_mem
457+
; IS__TUNIT____-SAME: () [[ATTR1]] {
458+
; IS__TUNIT____-NEXT: [[L:%.*]] = load i32, i32* @constant_mem, align 4
459+
; IS__TUNIT____-NEXT: ret i32 [[L]]
460+
;
461+
; IS__CGSCC____: Function Attrs: nofree norecurse nosync nounwind readnone willreturn
462+
; IS__CGSCC____-LABEL: define {{[^@]+}}@read_only_constant_mem
463+
; IS__CGSCC____-SAME: () [[ATTR1]] {
464+
; IS__CGSCC____-NEXT: [[L:%.*]] = load i32, i32* @constant_mem, align 4
465+
; IS__CGSCC____-NEXT: ret i32 [[L]]
466+
;
467+
%l = load i32, i32* @constant_mem
468+
ret i32 %l
469+
}

0 commit comments

Comments
 (0)