Skip to content

[Target][AMDGPU] Fix TSan error on AMDGPU Target. #79529

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 2 commits into from
Jan 26, 2024
Merged
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
27 changes: 17 additions & 10 deletions llvm/lib/Target/AMDGPU/AMDGPUResourceUsageAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ char &llvm::AMDGPUResourceUsageAnalysisID = AMDGPUResourceUsageAnalysis::ID;
// In code object v4 and older, we need to tell the runtime some amount ahead of
// time if we don't know the true stack size. Assume a smaller number if this is
// only due to dynamic / non-entry block allocas.
static cl::opt<uint32_t> AssumedStackSizeForExternalCall(
static cl::opt<uint32_t> clAssumedStackSizeForExternalCall(
"amdgpu-assume-external-call-stack-size",
cl::desc("Assumed stack use of any external call (in bytes)"), cl::Hidden,
cl::init(16384));

static cl::opt<uint32_t> AssumedStackSizeForDynamicSizeObjects(
static cl::opt<uint32_t> clAssumedStackSizeForDynamicSizeObjects(
"amdgpu-assume-dynamic-stack-object-size",
cl::desc("Assumed extra stack use if there are any "
"variable sized objects (in bytes)"),
Expand Down Expand Up @@ -112,11 +112,14 @@ bool AMDGPUResourceUsageAnalysis::runOnModule(Module &M) {

// By default, for code object v5 and later, track only the minimum scratch
// size
uint32_t AssumedStackSizeForDynamicSizeObjects =
clAssumedStackSizeForDynamicSizeObjects;
uint32_t AssumedStackSizeForExternalCall = clAssumedStackSizeForExternalCall;
if (AMDGPU::getAMDHSACodeObjectVersion(M) >= AMDGPU::AMDHSA_COV5 ||
STI.getTargetTriple().getOS() == Triple::AMDPAL) {
if (!AssumedStackSizeForDynamicSizeObjects.getNumOccurrences())
if (clAssumedStackSizeForDynamicSizeObjects.getNumOccurrences() == 0)
AssumedStackSizeForDynamicSizeObjects = 0;
if (!AssumedStackSizeForExternalCall.getNumOccurrences())
if (clAssumedStackSizeForExternalCall.getNumOccurrences() == 0)
AssumedStackSizeForExternalCall = 0;
}

Expand All @@ -132,7 +135,8 @@ bool AMDGPUResourceUsageAnalysis::runOnModule(Module &M) {
CallGraphResourceInfo.insert(std::pair(F, SIFunctionResourceInfo()));
SIFunctionResourceInfo &Info = CI.first->second;
assert(CI.second && "should only be called once per function");
Info = analyzeResourceUsage(*MF, TM);
Info = analyzeResourceUsage(*MF, TM, AssumedStackSizeForDynamicSizeObjects,
AssumedStackSizeForExternalCall);
HasIndirectCall |= Info.HasIndirectCall;
}

Expand All @@ -152,7 +156,8 @@ bool AMDGPUResourceUsageAnalysis::runOnModule(Module &M) {
SIFunctionResourceInfo &Info = CI.first->second;
MachineFunction *MF = MMI.getMachineFunction(*F);
assert(MF && "function must have been generated already");
Info = analyzeResourceUsage(*MF, TM);
Info = analyzeResourceUsage(*MF, TM, AssumedStackSizeForDynamicSizeObjects,
AssumedStackSizeForExternalCall);
HasIndirectCall |= Info.HasIndirectCall;
}

Expand All @@ -164,7 +169,9 @@ bool AMDGPUResourceUsageAnalysis::runOnModule(Module &M) {

AMDGPUResourceUsageAnalysis::SIFunctionResourceInfo
AMDGPUResourceUsageAnalysis::analyzeResourceUsage(
const MachineFunction &MF, const TargetMachine &TM) const {
const MachineFunction &MF, const TargetMachine &TM,
uint32_t AssumedStackSizeForDynamicSizeObjects,
uint32_t AssumedStackSizeForExternalCall) const {
SIFunctionResourceInfo Info;

const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
Expand Down Expand Up @@ -541,9 +548,9 @@ AMDGPUResourceUsageAnalysis::analyzeResourceUsage(
// directly call the tail called function. If a kernel directly
// calls a tail recursive function, we'll assume maximum stack size
// based on the regular call instruction.
CalleeFrameSize =
std::max(CalleeFrameSize,
static_cast<uint64_t>(AssumedStackSizeForExternalCall));
CalleeFrameSize = std::max(
CalleeFrameSize,
static_cast<uint64_t>(AssumedStackSizeForExternalCall));
}
}

Expand Down
6 changes: 4 additions & 2 deletions llvm/lib/Target/AMDGPU/AMDGPUResourceUsageAnalysis.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,10 @@ struct AMDGPUResourceUsageAnalysis : public ModulePass {
}

private:
SIFunctionResourceInfo analyzeResourceUsage(const MachineFunction &MF,
const TargetMachine &TM) const;
SIFunctionResourceInfo
analyzeResourceUsage(const MachineFunction &MF, const TargetMachine &TM,
uint32_t AssumedStackSizeForDynamicSizeObjects,
uint32_t AssumedStackSizeForExternalCall) const;
void propagateIndirectCallRegisterUsage();

DenseMap<const Function *, SIFunctionResourceInfo> CallGraphResourceInfo;
Expand Down