Skip to content

Commit

Permalink
AMDGPU: Simplify early exit handling for libcall simplify
Browse files Browse the repository at this point in the history
Early exit on intrinsics and don't duplicate indirect call
checks. Also let the IRBuilder constructor figure out the insert point
rather than doing it manually. Also avoid debug print about trying to
simplify calls in more unhandled scenarios.
  • Loading branch information
arsenm committed Jul 31, 2023
1 parent d74c89f commit ab6cd2d
Showing 1 changed file with 24 additions and 57 deletions.
81 changes: 24 additions & 57 deletions llvm/lib/Target/AMDGPU/AMDGPULibCalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,8 @@ bool AMDGPULibCalls::sincosUseNative(CallInst *aCI, const FuncInfo &FInfo) {
bool AMDGPULibCalls::useNative(CallInst *aCI) {
CI = aCI;
Function *Callee = aCI->getCalledFunction();
if (!Callee)
return false;

FuncInfo FInfo;
if (!parseFunctionName(Callee->getName(), FInfo) || !FInfo.isMangled() ||
Expand Down Expand Up @@ -587,27 +589,18 @@ bool AMDGPULibCalls::fold_read_write_pipe(CallInst *CI, IRBuilder<> &B,
bool AMDGPULibCalls::fold(CallInst *CI, AliasAnalysis *AA) {
this->CI = CI;
Function *Callee = CI->getCalledFunction();

// Ignore indirect calls.
if (Callee == nullptr)
if (!Callee)
return false;

BasicBlock *BB = CI->getParent();
LLVMContext &Context = CI->getParent()->getContext();
IRBuilder<> B(Context);

// Set the builder to the instruction after the call.
B.SetInsertPoint(BB, CI->getIterator());

// Copy fast flags from the original call.
if (const FPMathOperator *FPOp = dyn_cast<const FPMathOperator>(CI))
B.setFastMathFlags(FPOp->getFastMathFlags());

IRBuilder<> B(CI);
switch (Callee->getIntrinsicID()) {
default:
case Intrinsic::not_intrinsic:
break;
case Intrinsic::amdgcn_wavefrontsize:
return !EnablePreLink && fold_wavefrontsize(CI, B);
default:
return false;
}

FuncInfo FInfo;
Expand All @@ -618,6 +611,8 @@ bool AMDGPULibCalls::fold(CallInst *CI, AliasAnalysis *AA) {
if (CI->arg_size() != FInfo.getNumArgs())
return false;

LLVM_DEBUG(dbgs() << "AMDIC: try folding " << *CI << '\n');

if (TDOFold(CI, FInfo))
return true;

Expand All @@ -627,6 +622,10 @@ bool AMDGPULibCalls::fold(CallInst *CI, AliasAnalysis *AA) {
if (isUnsafeMath(CI) && evaluateCall(CI, FInfo))
return true;

// Copy fast flags from the original call.
if (const FPMathOperator *FPOp = dyn_cast<const FPMathOperator>(CI))
B.setFastMathFlags(FPOp->getFastMathFlags());

// Specialized optimizations for each function call
switch (FInfo.getId()) {
case AMDGPULibFunc::EI_RECIP:
Expand Down Expand Up @@ -1664,19 +1663,10 @@ bool AMDGPUSimplifyLibCalls::runOnFunction(Function &F) {
// Ignore non-calls.
CallInst *CI = dyn_cast<CallInst>(I);
++I;
// Ignore intrinsics that do not become real instructions.
if (!CI || isa<DbgInfoIntrinsic>(CI) || CI->isLifetimeStartOrEnd())
continue;

// Ignore indirect calls.
Function *Callee = CI->getCalledFunction();
if (Callee == nullptr)
continue;

LLVM_DEBUG(dbgs() << "AMDIC: try folding " << *CI << "\n";
dbgs().flush());
if(Simplifier.fold(CI, AA))
Changed = true;
if (CI) {
if (Simplifier.fold(CI, AA))
Changed = true;
}
}
}
return Changed;
Expand All @@ -1698,19 +1688,11 @@ PreservedAnalyses AMDGPUSimplifyLibCallsPass::run(Function &F,
// Ignore non-calls.
CallInst *CI = dyn_cast<CallInst>(I);
++I;
// Ignore intrinsics that do not become real instructions.
if (!CI || isa<DbgInfoIntrinsic>(CI) || CI->isLifetimeStartOrEnd())
continue;

// Ignore indirect calls.
Function *Callee = CI->getCalledFunction();
if (Callee == nullptr)
continue;

LLVM_DEBUG(dbgs() << "AMDIC: try folding " << *CI << "\n";
dbgs().flush());
if (Simplifier.fold(CI, AA))
Changed = true;

if (CI) {
if (Simplifier.fold(CI, AA))
Changed = true;
}
}
}
return Changed ? PreservedAnalyses::none() : PreservedAnalyses::all();
Expand All @@ -1726,14 +1708,7 @@ bool AMDGPUUseNativeCalls::runOnFunction(Function &F) {
// Ignore non-calls.
CallInst *CI = dyn_cast<CallInst>(I);
++I;
if (!CI) continue;

// Ignore indirect calls.
Function *Callee = CI->getCalledFunction();
if (Callee == nullptr)
continue;

if (Simplifier.useNative(CI))
if (CI && Simplifier.useNative(CI))
Changed = true;
}
}
Expand All @@ -1754,15 +1729,7 @@ PreservedAnalyses AMDGPUUseNativeCallsPass::run(Function &F,
// Ignore non-calls.
CallInst *CI = dyn_cast<CallInst>(I);
++I;
if (!CI)
continue;

// Ignore indirect calls.
Function *Callee = CI->getCalledFunction();
if (Callee == nullptr)
continue;

if (Simplifier.useNative(CI))
if (CI && Simplifier.useNative(CI))
Changed = true;
}
}
Expand Down

0 comments on commit ab6cd2d

Please sign in to comment.