Skip to content
Open
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
11 changes: 11 additions & 0 deletions llvm/lib/Transforms/IPO/FunctionAttrs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,16 @@ static void addLocAccess(MemoryEffects &ME, const MemoryLocation &Loc,
ME |= MemoryEffects::argMemOnly(MR);
return;
}
if (auto *CI = dyn_cast<CallInst>(UO)) {
if (auto *Callee = CI->getCalledFunction(); Callee && Callee->hasName()) {
static constexpr auto ErrnoFnNames = {"__errno_location", "_errno",
"__errno", "___errno"};
Copy link
Contributor

Choose a reason for hiding this comment

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

This needs to go through TLI.

if (is_contained(ErrnoFnNames, Callee->getName())) {
ME |= MemoryEffects::errnoMemOnly(MR);
return;
}
}
}

// If it's not an identified object, it might be an argument.
if (!isIdentifiedObject(UO))
Expand Down Expand Up @@ -258,6 +268,7 @@ checkFunctionMemoryAccess(Function &F, bool ThisBody, AAResults &AAR,
if (I.isVolatile())
ME |= MemoryEffects::inaccessibleMemOnly(MR);

// Refine memory effects for the given location.
addLocAccess(ME, *Loc, MR, AAR);
}

Expand Down
56 changes: 56 additions & 0 deletions llvm/test/Transforms/FunctionAttrs/read-write-errno.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
; RUN: opt -passes=function-attrs -S < %s | FileCheck --check-prefixes=FNATTRS %s
; RUN: opt -passes=attributor-light -S < %s | FileCheck --check-prefixes=ATTRIBUTOR %s

define i32 @test_read_errno() {
; FNATTRS-LABEL: define i32 @test_read_errno() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like this isn't actually testing the inferred attributes. You need to pass --function-attrs or something like that to UTC.

; FNATTRS-NEXT: [[CALL:%.*]] = call ptr @__errno_location()
; FNATTRS-NEXT: [[ERRNO:%.*]] = load i32, ptr [[CALL]], align 4
; FNATTRS-NEXT: ret i32 [[ERRNO]]
;
; ATTRIBUTOR-LABEL: define i32 @test_read_errno() {
; ATTRIBUTOR-NEXT: [[CALL:%.*]] = call ptr @__errno_location()
; ATTRIBUTOR-NEXT: [[ERRNO:%.*]] = load i32, ptr [[CALL]], align 4
; ATTRIBUTOR-NEXT: ret i32 [[ERRNO]]
;
%call = call ptr @__errno_location()
%errno = load i32, ptr %call
ret i32 %errno
}

define ptr @test_write_errno() {
; FNATTRS-LABEL: define noundef ptr @test_write_errno() {
; FNATTRS-NEXT: [[CALL:%.*]] = call ptr @__errno_location()
; FNATTRS-NEXT: store i32 0, ptr [[CALL]], align 4
; FNATTRS-NEXT: ret ptr [[CALL]]
;
; ATTRIBUTOR-LABEL: define ptr @test_write_errno() {
; ATTRIBUTOR-NEXT: [[CALL:%.*]] = call ptr @__errno_location()
; ATTRIBUTOR-NEXT: store i32 0, ptr [[CALL]], align 4
; ATTRIBUTOR-NEXT: ret ptr [[CALL]]
;
%call = call ptr @__errno_location()
store i32 0, ptr %call
ret ptr %call
}

define i32 @test_readwrite_errno() {
; FNATTRS-LABEL: define i32 @test_readwrite_errno() {
; FNATTRS-NEXT: [[CALL:%.*]] = call ptr @__errno_location()
; FNATTRS-NEXT: store i32 0, ptr [[CALL]], align 4
; FNATTRS-NEXT: [[ERRNO:%.*]] = load i32, ptr [[CALL]], align 4
; FNATTRS-NEXT: ret i32 [[ERRNO]]
;
; ATTRIBUTOR-LABEL: define i32 @test_readwrite_errno() {
; ATTRIBUTOR-NEXT: [[CALL:%.*]] = call ptr @__errno_location()
; ATTRIBUTOR-NEXT: store i32 0, ptr [[CALL]], align 4
; ATTRIBUTOR-NEXT: [[ERRNO:%.*]] = load i32, ptr [[CALL]], align 4
; ATTRIBUTOR-NEXT: ret i32 [[ERRNO]]
;
%call = call ptr @__errno_location()
store i32 0, ptr %call
%errno = load i32, ptr %call
ret i32 %errno
}

declare ptr @__errno_location()