Following discussions in #110443, and the following earlier discussions in https://lists.llvm.org/pipermail/llvm-dev/2017-October/117907.html, https://reviews.llvm.org/D38482, https://reviews.llvm.org/D38489, this PR attempts to overhaul the `TargetMachine` and `LLVMTargetMachine` interface classes. More specifically: 1. Makes `TargetMachine` the only class implemented under `TargetMachine.h` in the `Target` library. 2. `TargetMachine` contains target-specific interface functions that relate to IR/CodeGen/MC constructs, whereas before (at least on paper) it was supposed to have only IR/MC constructs. Any Target that doesn't want to use the independent code generator simply does not implement them, and returns either `false` or `nullptr`. 3. Renames `LLVMTargetMachine` to `CodeGenCommonTMImpl`. This renaming aims to make the purpose of `LLVMTargetMachine` clearer. Its interface was moved under the CodeGen library, to further emphasis its usage in Targets that use CodeGen directly. 4. Makes `TargetMachine` the only interface used across LLVM and its projects. With these changes, `CodeGenCommonTMImpl` is simply a set of shared function implementations of `TargetMachine`, and CodeGen users don't need to static cast to `LLVMTargetMachine` every time they need a CodeGen-specific feature of the `TargetMachine`. 5. More importantly, does not change any requirements regarding library linking. cc @arsenm @aeubanks
244 lines
9.5 KiB
C++
244 lines
9.5 KiB
C++
//===- AMDGPUMCResourceInfo.cpp --- MC Resource Info ----------------------===//
|
|
//
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
/// \file
|
|
/// \brief MC infrastructure to propagate the function level resource usage
|
|
/// info.
|
|
///
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "AMDGPUMCResourceInfo.h"
|
|
#include "Utils/AMDGPUBaseInfo.h"
|
|
#include "llvm/ADT/StringRef.h"
|
|
#include "llvm/MC/MCContext.h"
|
|
#include "llvm/MC/MCSymbol.h"
|
|
#include "llvm/Target/TargetMachine.h"
|
|
|
|
using namespace llvm;
|
|
|
|
MCSymbol *MCResourceInfo::getSymbol(StringRef FuncName, ResourceInfoKind RIK,
|
|
MCContext &OutContext) {
|
|
auto GOCS = [FuncName, &OutContext](StringRef Suffix) {
|
|
return OutContext.getOrCreateSymbol(FuncName + Twine(Suffix));
|
|
};
|
|
switch (RIK) {
|
|
case RIK_NumVGPR:
|
|
return GOCS(".num_vgpr");
|
|
case RIK_NumAGPR:
|
|
return GOCS(".num_agpr");
|
|
case RIK_NumSGPR:
|
|
return GOCS(".numbered_sgpr");
|
|
case RIK_PrivateSegSize:
|
|
return GOCS(".private_seg_size");
|
|
case RIK_UsesVCC:
|
|
return GOCS(".uses_vcc");
|
|
case RIK_UsesFlatScratch:
|
|
return GOCS(".uses_flat_scratch");
|
|
case RIK_HasDynSizedStack:
|
|
return GOCS(".has_dyn_sized_stack");
|
|
case RIK_HasRecursion:
|
|
return GOCS(".has_recursion");
|
|
case RIK_HasIndirectCall:
|
|
return GOCS(".has_indirect_call");
|
|
}
|
|
llvm_unreachable("Unexpected ResourceInfoKind.");
|
|
}
|
|
|
|
const MCExpr *MCResourceInfo::getSymRefExpr(StringRef FuncName,
|
|
ResourceInfoKind RIK,
|
|
MCContext &Ctx) {
|
|
return MCSymbolRefExpr::create(getSymbol(FuncName, RIK, Ctx), Ctx);
|
|
}
|
|
|
|
void MCResourceInfo::assignMaxRegs(MCContext &OutContext) {
|
|
// Assign expression to get the max register use to the max_num_Xgpr symbol.
|
|
MCSymbol *MaxVGPRSym = getMaxVGPRSymbol(OutContext);
|
|
MCSymbol *MaxAGPRSym = getMaxAGPRSymbol(OutContext);
|
|
MCSymbol *MaxSGPRSym = getMaxSGPRSymbol(OutContext);
|
|
|
|
auto assignMaxRegSym = [&OutContext](MCSymbol *Sym, int32_t RegCount) {
|
|
const MCExpr *MaxExpr = MCConstantExpr::create(RegCount, OutContext);
|
|
Sym->setVariableValue(MaxExpr);
|
|
};
|
|
|
|
assignMaxRegSym(MaxVGPRSym, MaxVGPR);
|
|
assignMaxRegSym(MaxAGPRSym, MaxAGPR);
|
|
assignMaxRegSym(MaxSGPRSym, MaxSGPR);
|
|
}
|
|
|
|
void MCResourceInfo::reset() { *this = MCResourceInfo(); }
|
|
|
|
void MCResourceInfo::finalize(MCContext &OutContext) {
|
|
assert(!Finalized && "Cannot finalize ResourceInfo again.");
|
|
Finalized = true;
|
|
assignMaxRegs(OutContext);
|
|
}
|
|
|
|
MCSymbol *MCResourceInfo::getMaxVGPRSymbol(MCContext &OutContext) {
|
|
return OutContext.getOrCreateSymbol("amdgpu.max_num_vgpr");
|
|
}
|
|
|
|
MCSymbol *MCResourceInfo::getMaxAGPRSymbol(MCContext &OutContext) {
|
|
return OutContext.getOrCreateSymbol("amdgpu.max_num_agpr");
|
|
}
|
|
|
|
MCSymbol *MCResourceInfo::getMaxSGPRSymbol(MCContext &OutContext) {
|
|
return OutContext.getOrCreateSymbol("amdgpu.max_num_sgpr");
|
|
}
|
|
|
|
void MCResourceInfo::assignResourceInfoExpr(
|
|
int64_t LocalValue, ResourceInfoKind RIK, AMDGPUMCExpr::VariantKind Kind,
|
|
const MachineFunction &MF, const SmallVectorImpl<const Function *> &Callees,
|
|
MCContext &OutContext) {
|
|
const TargetMachine &TM = MF.getTarget();
|
|
MCSymbol *FnSym = TM.getSymbol(&MF.getFunction());
|
|
const MCConstantExpr *LocalConstExpr =
|
|
MCConstantExpr::create(LocalValue, OutContext);
|
|
const MCExpr *SymVal = LocalConstExpr;
|
|
if (!Callees.empty()) {
|
|
SmallVector<const MCExpr *, 8> ArgExprs;
|
|
// Avoid recursive symbol assignment.
|
|
SmallPtrSet<const Function *, 8> Seen;
|
|
ArgExprs.push_back(LocalConstExpr);
|
|
const Function &F = MF.getFunction();
|
|
Seen.insert(&F);
|
|
|
|
for (const Function *Callee : Callees) {
|
|
if (!Seen.insert(Callee).second)
|
|
continue;
|
|
MCSymbol *CalleeFnSym = TM.getSymbol(&Callee->getFunction());
|
|
MCSymbol *CalleeValSym =
|
|
getSymbol(CalleeFnSym->getName(), RIK, OutContext);
|
|
ArgExprs.push_back(MCSymbolRefExpr::create(CalleeValSym, OutContext));
|
|
}
|
|
SymVal = AMDGPUMCExpr::create(Kind, ArgExprs, OutContext);
|
|
}
|
|
MCSymbol *Sym = getSymbol(FnSym->getName(), RIK, OutContext);
|
|
Sym->setVariableValue(SymVal);
|
|
}
|
|
|
|
void MCResourceInfo::gatherResourceInfo(
|
|
const MachineFunction &MF,
|
|
const AMDGPUResourceUsageAnalysis::SIFunctionResourceInfo &FRI,
|
|
MCContext &OutContext) {
|
|
// Worst case VGPR use for non-hardware-entrypoints.
|
|
MCSymbol *MaxVGPRSym = getMaxVGPRSymbol(OutContext);
|
|
MCSymbol *MaxAGPRSym = getMaxAGPRSymbol(OutContext);
|
|
MCSymbol *MaxSGPRSym = getMaxSGPRSymbol(OutContext);
|
|
|
|
if (!AMDGPU::isEntryFunctionCC(MF.getFunction().getCallingConv())) {
|
|
addMaxVGPRCandidate(FRI.NumVGPR);
|
|
addMaxAGPRCandidate(FRI.NumAGPR);
|
|
addMaxSGPRCandidate(FRI.NumExplicitSGPR);
|
|
}
|
|
|
|
const TargetMachine &TM = MF.getTarget();
|
|
MCSymbol *FnSym = TM.getSymbol(&MF.getFunction());
|
|
|
|
auto SetMaxReg = [&](MCSymbol *MaxSym, int32_t numRegs,
|
|
ResourceInfoKind RIK) {
|
|
if (!FRI.HasIndirectCall) {
|
|
assignResourceInfoExpr(numRegs, RIK, AMDGPUMCExpr::AGVK_Max, MF,
|
|
FRI.Callees, OutContext);
|
|
} else {
|
|
const MCExpr *SymRef = MCSymbolRefExpr::create(MaxSym, OutContext);
|
|
MCSymbol *LocalNumSym = getSymbol(FnSym->getName(), RIK, OutContext);
|
|
const MCExpr *MaxWithLocal = AMDGPUMCExpr::createMax(
|
|
{MCConstantExpr::create(numRegs, OutContext), SymRef}, OutContext);
|
|
LocalNumSym->setVariableValue(MaxWithLocal);
|
|
}
|
|
};
|
|
|
|
SetMaxReg(MaxVGPRSym, FRI.NumVGPR, RIK_NumVGPR);
|
|
SetMaxReg(MaxAGPRSym, FRI.NumAGPR, RIK_NumAGPR);
|
|
SetMaxReg(MaxSGPRSym, FRI.NumExplicitSGPR, RIK_NumSGPR);
|
|
|
|
{
|
|
// The expression for private segment size should be: FRI.PrivateSegmentSize
|
|
// + max(FRI.Callees, FRI.CalleeSegmentSize)
|
|
SmallVector<const MCExpr *, 8> ArgExprs;
|
|
if (FRI.CalleeSegmentSize)
|
|
ArgExprs.push_back(
|
|
MCConstantExpr::create(FRI.CalleeSegmentSize, OutContext));
|
|
|
|
SmallPtrSet<const Function *, 8> Seen;
|
|
Seen.insert(&MF.getFunction());
|
|
for (const Function *Callee : FRI.Callees) {
|
|
if (!Seen.insert(Callee).second)
|
|
continue;
|
|
if (!Callee->isDeclaration()) {
|
|
MCSymbol *CalleeFnSym = TM.getSymbol(&Callee->getFunction());
|
|
MCSymbol *calleeValSym =
|
|
getSymbol(CalleeFnSym->getName(), RIK_PrivateSegSize, OutContext);
|
|
ArgExprs.push_back(MCSymbolRefExpr::create(calleeValSym, OutContext));
|
|
}
|
|
}
|
|
const MCExpr *localConstExpr =
|
|
MCConstantExpr::create(FRI.PrivateSegmentSize, OutContext);
|
|
if (!ArgExprs.empty()) {
|
|
const AMDGPUMCExpr *transitiveExpr =
|
|
AMDGPUMCExpr::createMax(ArgExprs, OutContext);
|
|
localConstExpr =
|
|
MCBinaryExpr::createAdd(localConstExpr, transitiveExpr, OutContext);
|
|
}
|
|
getSymbol(FnSym->getName(), RIK_PrivateSegSize, OutContext)
|
|
->setVariableValue(localConstExpr);
|
|
}
|
|
|
|
auto SetToLocal = [&](int64_t LocalValue, ResourceInfoKind RIK) {
|
|
MCSymbol *Sym = getSymbol(FnSym->getName(), RIK, OutContext);
|
|
Sym->setVariableValue(MCConstantExpr::create(LocalValue, OutContext));
|
|
};
|
|
|
|
if (!FRI.HasIndirectCall) {
|
|
assignResourceInfoExpr(FRI.UsesVCC, ResourceInfoKind::RIK_UsesVCC,
|
|
AMDGPUMCExpr::AGVK_Or, MF, FRI.Callees, OutContext);
|
|
assignResourceInfoExpr(FRI.UsesFlatScratch,
|
|
ResourceInfoKind::RIK_UsesFlatScratch,
|
|
AMDGPUMCExpr::AGVK_Or, MF, FRI.Callees, OutContext);
|
|
assignResourceInfoExpr(FRI.HasDynamicallySizedStack,
|
|
ResourceInfoKind::RIK_HasDynSizedStack,
|
|
AMDGPUMCExpr::AGVK_Or, MF, FRI.Callees, OutContext);
|
|
assignResourceInfoExpr(FRI.HasRecursion, ResourceInfoKind::RIK_HasRecursion,
|
|
AMDGPUMCExpr::AGVK_Or, MF, FRI.Callees, OutContext);
|
|
assignResourceInfoExpr(FRI.HasIndirectCall,
|
|
ResourceInfoKind::RIK_HasIndirectCall,
|
|
AMDGPUMCExpr::AGVK_Or, MF, FRI.Callees, OutContext);
|
|
} else {
|
|
SetToLocal(FRI.UsesVCC, ResourceInfoKind::RIK_UsesVCC);
|
|
SetToLocal(FRI.UsesFlatScratch, ResourceInfoKind::RIK_UsesFlatScratch);
|
|
SetToLocal(FRI.HasDynamicallySizedStack,
|
|
ResourceInfoKind::RIK_HasDynSizedStack);
|
|
SetToLocal(FRI.HasRecursion, ResourceInfoKind::RIK_HasRecursion);
|
|
SetToLocal(FRI.HasIndirectCall, ResourceInfoKind::RIK_HasIndirectCall);
|
|
}
|
|
}
|
|
|
|
const MCExpr *MCResourceInfo::createTotalNumVGPRs(const MachineFunction &MF,
|
|
MCContext &Ctx) {
|
|
const TargetMachine &TM = MF.getTarget();
|
|
MCSymbol *FnSym = TM.getSymbol(&MF.getFunction());
|
|
return AMDGPUMCExpr::createTotalNumVGPR(
|
|
getSymRefExpr(FnSym->getName(), RIK_NumAGPR, Ctx),
|
|
getSymRefExpr(FnSym->getName(), RIK_NumVGPR, Ctx), Ctx);
|
|
}
|
|
|
|
const MCExpr *MCResourceInfo::createTotalNumSGPRs(const MachineFunction &MF,
|
|
bool hasXnack,
|
|
MCContext &Ctx) {
|
|
const TargetMachine &TM = MF.getTarget();
|
|
MCSymbol *FnSym = TM.getSymbol(&MF.getFunction());
|
|
return MCBinaryExpr::createAdd(
|
|
getSymRefExpr(FnSym->getName(), RIK_NumSGPR, Ctx),
|
|
AMDGPUMCExpr::createExtraSGPRs(
|
|
getSymRefExpr(FnSym->getName(), RIK_UsesVCC, Ctx),
|
|
getSymRefExpr(FnSym->getName(), RIK_UsesFlatScratch, Ctx), hasXnack,
|
|
Ctx),
|
|
Ctx);
|
|
}
|