Skip to content

[DSE] Consider the aliasing through global variable while checking clobber #120044

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from 5 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
20 changes: 20 additions & 0 deletions llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2261,6 +2261,19 @@ struct DSEState {
bool eliminateDeadDefs(const MemoryDefWrapper &KillingDefWrapper);
};

// Return true if "Arg" is function local and isn't captured before "CB" or
// if "Arg" is GEP whose base pointer is function local and isn't captured
// before "CB".
bool IsFuncLocalAndNotCaptured(Value *Arg, const CallBase *CB,
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
bool IsFuncLocalAndNotCaptured(Value *Arg, const CallBase *CB,
bool isFuncLocalAndNotCaptured(Value *Arg, const CallBase *CB,

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

EarliestEscapeAnalysis &EA) {
if (isIdentifiedFunctionLocal(Arg))
return EA.isNotCapturedBefore(Arg, CB, /*OrAt*/ true);
const auto *GEP = dyn_cast<GetElementPtrInst>(Arg);
Copy link
Contributor

Choose a reason for hiding this comment

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

Check the result of getUnderlyingObject instead, it will look through multiple GEPs.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, done. Thanks!

if (GEP && isIdentifiedFunctionLocal(GEP->getPointerOperand()))
return EA.isNotCapturedBefore(GEP->getPointerOperand(), CB, /*OrAt*/ true);
return false;
}

SmallVector<MemoryLocation, 1>
DSEState::getInitializesArgMemLoc(const Instruction *I) {
const CallBase *CB = dyn_cast<CallBase>(I);
Expand All @@ -2276,6 +2289,13 @@ DSEState::getInitializesArgMemLoc(const Instruction *I) {
Inits = InitializesAttr.getValueAsConstantRangeList();

Value *CurArg = CB->getArgOperand(Idx);
// Check whether "CurArg" could alias with global variables. We require
// either it's function local and isn't captured before or the "CB" only
// accesses arg or inaccessible mem.
if (!Inits.empty() && !IsFuncLocalAndNotCaptured(CurArg, CB, EA) &&
!CB->onlyAccessesInaccessibleMemOrArgMem())
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd check onlyAccessesInaccessibleMemOrArgMem before isFuncLocalAndNotCaptured as it's cheaper.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, thanks for the reminder! Done.

Inits = ConstantRangeList();

// We don't perform incorrect DSE on unwind edges in the current function,
// and use the "initializes" attribute to kill dead stores if:
// - The call does not throw exceptions, "CB->doesNotThrow()".
Expand Down
56 changes: 49 additions & 7 deletions llvm/test/Transforms/DeadStoreElimination/inter-procedural.ll
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,26 @@ define i16 @p1_write_then_read_caller() {
ret i16 %l
}

declare void @fn_capture(ptr noundef)
Copy link
Contributor

Choose a reason for hiding this comment

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

Drop noundef, it's not relevant here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

; Function Attrs: mustprogress nounwind uwtable
Copy link
Contributor

Choose a reason for hiding this comment

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

Drop inaccurate Function Attrs comment.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

define i16 @p1_write_then_read_caller_escape() {
; CHECK-LABEL: @p1_write_then_read_caller_escape(
; CHECK-NEXT: [[PTR:%.*]] = alloca i16, align 2
; CHECK-NEXT: store i16 0, ptr [[PTR]], align 2
; CHECK-NEXT: call void @fn_capture(ptr [[PTR]])
; CHECK-NEXT: call void @p1_write_then_read(ptr [[PTR]])
; CHECK-NEXT: [[L:%.*]] = load i16, ptr [[PTR]], align 2
; CHECK-NEXT: ret i16 [[L]]
;
%ptr = alloca i16
store i16 0, ptr %ptr
call void @fn_capture(ptr %ptr)
call void @p1_write_then_read(ptr %ptr)
%l = load i16, ptr %ptr
ret i16 %l
}


; Function Attrs: mustprogress nounwind uwtable
define i16 @p1_write_then_read_caller_with_clobber() {
; CHECK-LABEL: @p1_write_then_read_caller_with_clobber(
Expand Down Expand Up @@ -221,15 +241,17 @@ declare void @large_p2(ptr nocapture noundef initializes((0, 200)), ptr nocaptur
; Function Attrs: mustprogress nounwind uwtable
define i16 @large_p1_caller() {
; CHECK-LABEL: @large_p1_caller(
; CHECK-NEXT: [[PTR:%.*]] = alloca [200 x i8], align 1
; CHECK-NEXT: call void @large_p1(ptr [[PTR]])
; CHECK-NEXT: [[L:%.*]] = load i16, ptr [[PTR]], align 2
; CHECK-NEXT: [[PTR:%.*]] = alloca [300 x i8], align 1
; CHECK-NEXT: [[TMP:%.*]] = getelementptr i8, ptr [[PTR]], i64 100
; CHECK-NEXT: call void @large_p1(ptr [[TMP]])
; CHECK-NEXT: [[L:%.*]] = load i16, ptr [[TMP]], align 2
; CHECK-NEXT: ret i16 [[L]]
;
%ptr = alloca [200 x i8]
call void @llvm.memset.p0.i64(ptr %ptr, i8 42, i64 100, i1 false)
call void @large_p1(ptr %ptr)
%l = load i16, ptr %ptr
%ptr = alloca [300 x i8]
%tmp = getelementptr i8, ptr %ptr, i64 100
call void @llvm.memset.p0.i64(ptr %tmp, i8 42, i64 100, i1 false)
call void @large_p1(ptr %tmp)
%l = load i16, ptr %tmp
ret i16 %l
}

Expand Down Expand Up @@ -299,3 +321,23 @@ define i16 @large_p2_may_or_partial_alias_caller2(ptr %base1, ptr %base2) {
ret i16 %l
}

@g = global i16 123, align 2

declare void @read_global(ptr nocapture noundef initializes((0, 2))) nounwind
memory(read, argmem: write, inaccessiblemem: none) nounwind

define i16 @global_var_alias() {
; CHECK-LABEL: @global_var_alias(
; CHECK-NEXT: store i16 0, ptr @g, align 4
; CHECK-NEXT: [[G_ADDR:%.*]] = getelementptr i8, ptr @g, i64 1
; CHECK-NEXT: call void @read_global(ptr [[G_ADDR]])
; CHECK-NEXT: [[L:%.*]] = load i16, ptr @g, align 2
; CHECK-NEXT: ret i16 [[L]]
;
store i16 0, ptr @g, align 4
%g_addr = getelementptr i8, ptr @g, i64 1
call void @read_global(ptr %g_addr)
Copy link
Contributor

Choose a reason for hiding this comment

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

it seems like this case would still go out of bounds for the i16 global?
%g_addr = 1 byte from the start
then @read_global initializes 2 bytes from there?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ooops, removed the GEP and directly read/write @g. Thanks.

%l = load i16, ptr @g
ret i16 %l
}

Loading