Skip to content

Commit

Permalink
[Attributor] Ignore different kernels for kernel lifetime objects
Browse files Browse the repository at this point in the history
If a potential interfering access is in a different kernel and the
underlying object has kernel lifetime we can straight out ignore the
interfering access.
TODO: This should be made much stronger via "reaching kernels", which we
already track in AAKernelInfo.
  • Loading branch information
jdoerfert committed Oct 21, 2023
1 parent bb96093 commit ba87fba
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
21 changes: 18 additions & 3 deletions llvm/lib/Transforms/IPO/AttributorAttributes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1200,6 +1200,11 @@ struct AAPointerInfoImpl
A, this, IRPosition::function(Scope), DepClassTy::OPTIONAL,
IsKnownNoRecurse);

// TODO: Use reaching kernels from AAKernelInfo (or move it to
// AAExecutionDomain) such that we allow scopes other than kernels as long
// as the reaching kernels are disjoint.
bool InstInKernel = Scope.hasFnAttribute("kernel");
bool ObjHasKernelLifetime = false;
const bool UseDominanceReasoning =
FindInterferingWrites && IsKnownNoRecurse;
const DominatorTree *DT =
Expand Down Expand Up @@ -1232,6 +1237,7 @@ struct AAPointerInfoImpl
// If the alloca containing function is not recursive the alloca
// must be dead in the callee.
const Function *AIFn = AI->getFunction();
ObjHasKernelLifetime = AIFn->hasFnAttribute("kernel");
bool IsKnownNoRecurse;
if (AA::hasAssumedIRAttr<Attribute::NoRecurse>(
A, this, IRPosition::function(*AIFn), DepClassTy::OPTIONAL,
Expand All @@ -1241,7 +1247,8 @@ struct AAPointerInfoImpl
} else if (auto *GV = dyn_cast<GlobalValue>(&getAssociatedValue())) {
// If the global has kernel lifetime we can stop if we reach a kernel
// as it is "dead" in the (unknown) callees.
if (HasKernelLifetime(GV, *GV->getParent()))
ObjHasKernelLifetime = HasKernelLifetime(GV, *GV->getParent());
if (ObjHasKernelLifetime)
IsLiveInCalleeCB = [](const Function &Fn) {
return !Fn.hasFnAttribute("kernel");
};
Expand All @@ -1252,6 +1259,15 @@ struct AAPointerInfoImpl
AA::InstExclusionSetTy ExclusionSet;

auto AccessCB = [&](const Access &Acc, bool Exact) {
Function *AccScope = Acc.getRemoteInst()->getFunction();
bool AccInSameScope = AccScope == &Scope;

// If the object has kernel lifetime we can ignore accesses only reachable
// by other kernels. For now we only skip accesses *in* other kernels.
if (InstInKernel && ObjHasKernelLifetime && !AccInSameScope &&
AccScope->hasFnAttribute("kernel"))
return true;

if (Exact && Acc.isMustAccess() && Acc.getRemoteInst() != &I) {
if (Acc.isWrite() || (isa<LoadInst>(I) && Acc.isWriteOrAssumption()))
ExclusionSet.insert(Acc.getRemoteInst());
Expand All @@ -1262,8 +1278,7 @@ struct AAPointerInfoImpl
return true;

bool Dominates = FindInterferingWrites && DT && Exact &&
Acc.isMustAccess() &&
(Acc.getRemoteInst()->getFunction() == &Scope) &&
Acc.isMustAccess() && AccInSameScope &&
DT->dominates(Acc.getRemoteInst(), &I);
if (Dominates)
DominatingWrites.insert(&Acc);
Expand Down
6 changes: 2 additions & 4 deletions llvm/test/Transforms/Attributor/value-simplify-gpu.ll
Original file line number Diff line number Diff line change
Expand Up @@ -401,8 +401,7 @@ define dso_local void @kernel3(i32 %C) norecurse "kernel" {
; TUNIT-NEXT: [[I:%.*]] = icmp eq i32 [[C]], 42
; TUNIT-NEXT: br i1 [[I]], label [[T:%.*]], label [[F:%.*]]
; TUNIT: t:
; TUNIT-NEXT: [[L:%.*]] = load i32, ptr addrspace(3) @AS3OneKernelAtATime, align 4
; TUNIT-NEXT: call void @use(i32 noundef [[L]], i32 noundef [[L]], i32 noundef [[L]]) #[[ATTR7]]
; TUNIT-NEXT: call void @use(i32 noundef 42, i32 noundef 42, i32 noundef 42) #[[ATTR7]]
; TUNIT-NEXT: ret void
; TUNIT: f:
; TUNIT-NEXT: ret void
Expand All @@ -413,8 +412,7 @@ define dso_local void @kernel3(i32 %C) norecurse "kernel" {
; CGSCC-NEXT: [[I:%.*]] = icmp eq i32 [[C]], 42
; CGSCC-NEXT: br i1 [[I]], label [[T:%.*]], label [[F:%.*]]
; CGSCC: t:
; CGSCC-NEXT: [[L:%.*]] = load i32, ptr addrspace(3) @AS3OneKernelAtATime, align 4
; CGSCC-NEXT: call void @use(i32 noundef [[L]], i32 noundef [[L]], i32 noundef [[L]]) #[[ATTR4]]
; CGSCC-NEXT: call void @use(i32 noundef 42, i32 noundef 42, i32 noundef 42) #[[ATTR4]]
; CGSCC-NEXT: ret void
; CGSCC: f:
; CGSCC-NEXT: ret void
Expand Down

0 comments on commit ba87fba

Please sign in to comment.