Skip to content

[AMDGPU] MCExpr-ify AMDGPU HSAMetadata #94788

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 3 commits into from
Jun 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
60 changes: 32 additions & 28 deletions llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,15 +136,6 @@ void AMDGPUAsmPrinter::initTargetStreamer(Module &M) {
getTargetStreamer()->getPALMetadata()->readFromIR(M);
}

uint64_t AMDGPUAsmPrinter::getMCExprValue(const MCExpr *Value, MCContext &Ctx) {
int64_t Val;
if (!Value->evaluateAsAbsolute(Val)) {
Ctx.reportError(SMLoc(), "could not resolve expression when required.");
return 0;
}
return static_cast<uint64_t>(Val);
}

void AMDGPUAsmPrinter::emitEndOfAsmFile(Module &M) {
// Init target streamer if it has not yet happened
if (!IsTargetStreamerInitialized)
Expand Down Expand Up @@ -249,14 +240,14 @@ void AMDGPUAsmPrinter::emitFunctionBodyEnd() {
getNameWithPrefix(KernelName, &MF->getFunction());
getTargetStreamer()->EmitAmdhsaKernelDescriptor(
STM, KernelName, getAmdhsaKernelDescriptor(*MF, CurrentProgramInfo),
getMCExprValue(CurrentProgramInfo.NumVGPRsForWavesPerEU, Context),
getMCExprValue(CurrentProgramInfo.NumSGPRsForWavesPerEU, Context) -
IsaInfo::getNumExtraSGPRs(
&STM, getMCExprValue(CurrentProgramInfo.VCCUsed, Context),
getMCExprValue(CurrentProgramInfo.FlatUsed, Context),
getTargetStreamer()->getTargetID()->isXnackOnOrAny()),
getMCExprValue(CurrentProgramInfo.VCCUsed, Context),
getMCExprValue(CurrentProgramInfo.FlatUsed, Context));
CurrentProgramInfo.NumVGPRsForWavesPerEU,
MCBinaryExpr::createSub(
CurrentProgramInfo.NumSGPRsForWavesPerEU,
AMDGPUMCExpr::createExtraSGPRs(
CurrentProgramInfo.VCCUsed, CurrentProgramInfo.FlatUsed,
getTargetStreamer()->getTargetID()->isXnackOnOrAny(), Context),
Context),
CurrentProgramInfo.VCCUsed, CurrentProgramInfo.FlatUsed);

Streamer.popSection();
}
Expand Down Expand Up @@ -431,9 +422,10 @@ void AMDGPUAsmPrinter::emitCommonFunctionComments(
false);
}

uint16_t AMDGPUAsmPrinter::getAmdhsaKernelCodeProperties(
const MCExpr *AMDGPUAsmPrinter::getAmdhsaKernelCodeProperties(
const MachineFunction &MF) const {
const SIMachineFunctionInfo &MFI = *MF.getInfo<SIMachineFunctionInfo>();
MCContext &Ctx = MF.getContext();
uint16_t KernelCodeProperties = 0;
const GCNUserSGPRUsageInfo &UserSGPRInfo = MFI.getUserSGPRInfo();

Expand Down Expand Up @@ -470,11 +462,19 @@ uint16_t AMDGPUAsmPrinter::getAmdhsaKernelCodeProperties(
amdhsa::KERNEL_CODE_PROPERTY_ENABLE_WAVEFRONT_SIZE32;
}

if (getMCExprValue(CurrentProgramInfo.DynamicCallStack, MF.getContext()) &&
CodeObjectVersion >= AMDGPU::AMDHSA_COV5)
KernelCodeProperties |= amdhsa::KERNEL_CODE_PROPERTY_USES_DYNAMIC_STACK;

return KernelCodeProperties;
// CurrentProgramInfo.DynamicCallStack is a MCExpr and could be
// un-evaluatable at this point so it cannot be conditionally checked here.
// Instead, we'll directly shift the possibly unknown MCExpr into its place
// and bitwise-or it into KernelCodeProperties.
const MCExpr *KernelCodePropExpr =
MCConstantExpr::create(KernelCodeProperties, Ctx);
const MCExpr *OrValue = MCConstantExpr::create(
amdhsa::KERNEL_CODE_PROPERTY_USES_DYNAMIC_STACK_SHIFT, Ctx);
OrValue = MCBinaryExpr::createShl(CurrentProgramInfo.DynamicCallStack,
OrValue, Ctx);
KernelCodePropExpr = MCBinaryExpr::createOr(KernelCodePropExpr, OrValue, Ctx);

return KernelCodePropExpr;
}

MCKernelDescriptor
Expand All @@ -497,11 +497,15 @@ AMDGPUAsmPrinter::getAmdhsaKernelDescriptor(const MachineFunction &MF,

KernelDescriptor.compute_pgm_rsrc1 = PI.getComputePGMRSrc1(STM, Ctx);
KernelDescriptor.compute_pgm_rsrc2 = PI.getComputePGMRSrc2(Ctx);
KernelDescriptor.kernel_code_properties =
MCConstantExpr::create(getAmdhsaKernelCodeProperties(MF), Ctx);

assert(STM.hasGFX90AInsts() ||
getMCExprValue(CurrentProgramInfo.ComputePGMRSrc3GFX90A, Ctx) == 0);
KernelDescriptor.kernel_code_properties = getAmdhsaKernelCodeProperties(MF);

int64_t PGRM_Rsrc3 = 1;
bool EvaluatableRsrc3 =
CurrentProgramInfo.ComputePGMRSrc3GFX90A->evaluateAsAbsolute(PGRM_Rsrc3);
(void)PGRM_Rsrc3;
(void)EvaluatableRsrc3;
assert(STM.hasGFX90AInsts() || !EvaluatableRsrc3 ||
static_cast<uint64_t>(PGRM_Rsrc3) == 0);
KernelDescriptor.compute_pgm_rsrc3 = CurrentProgramInfo.ComputePGMRSrc3GFX90A;

KernelDescriptor.kernarg_preload = MCConstantExpr::create(
Expand Down
4 changes: 1 addition & 3 deletions llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,14 @@ class AMDGPUAsmPrinter final : public AsmPrinter {
const SIProgramInfo &CurrentProgramInfo,
bool isModuleEntryFunction, bool hasMAIInsts);

uint16_t getAmdhsaKernelCodeProperties(
const MachineFunction &MF) const;
const MCExpr *getAmdhsaKernelCodeProperties(const MachineFunction &MF) const;

AMDGPU::MCKernelDescriptor
getAmdhsaKernelDescriptor(const MachineFunction &MF,
const SIProgramInfo &PI) const;

void initTargetStreamer(Module &M);

static uint64_t getMCExprValue(const MCExpr *Value, MCContext &Ctx);
SmallString<128> getMCExprStr(const MCExpr *Value);

public:
Expand Down
34 changes: 14 additions & 20 deletions llvm/lib/Target/AMDGPU/AMDGPUHSAMetadataStreamer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -464,28 +464,19 @@ MetadataStreamerMsgPackV4::getHSAKernelProps(const MachineFunction &MF,
const SIMachineFunctionInfo &MFI = *MF.getInfo<SIMachineFunctionInfo>();
const Function &F = MF.getFunction();

auto GetMCExprValue = [&MF](const MCExpr *Value) {
int64_t Val;
if (!Value->evaluateAsAbsolute(Val)) {
MCContext &Ctx = MF.getContext();
Ctx.reportError(SMLoc(), "could not resolve expression when required.");
Val = 0;
}
return static_cast<uint64_t>(Val);
};

auto Kern = HSAMetadataDoc->getMapNode();

Align MaxKernArgAlign;
Kern[".kernarg_segment_size"] = Kern.getDocument()->getNode(
STM.getKernArgSegmentSize(F, MaxKernArgAlign));
Kern[".group_segment_fixed_size"] =
Kern.getDocument()->getNode(ProgramInfo.LDSSize);
Kern[".private_segment_fixed_size"] =
Kern.getDocument()->getNode(GetMCExprValue(ProgramInfo.ScratchSize));
DelayedExprs->assignDocNode(Kern[".private_segment_fixed_size"],
msgpack::Type::UInt, ProgramInfo.ScratchSize);
if (CodeObjectVersion >= AMDGPU::AMDHSA_COV5) {
Kern[".uses_dynamic_stack"] = Kern.getDocument()->getNode(
static_cast<bool>(GetMCExprValue(ProgramInfo.DynamicCallStack)));
DelayedExprs->assignDocNode(Kern[".uses_dynamic_stack"],
msgpack::Type::Boolean,
ProgramInfo.DynamicCallStack);
}

if (CodeObjectVersion >= AMDGPU::AMDHSA_COV5 && STM.supportsWGP())
Expand All @@ -497,15 +488,15 @@ MetadataStreamerMsgPackV4::getHSAKernelProps(const MachineFunction &MF,
Kern.getDocument()->getNode(std::max(Align(4), MaxKernArgAlign).value());
Kern[".wavefront_size"] =
Kern.getDocument()->getNode(STM.getWavefrontSize());
Kern[".sgpr_count"] =
Kern.getDocument()->getNode(GetMCExprValue(ProgramInfo.NumSGPR));
Kern[".vgpr_count"] =
Kern.getDocument()->getNode(GetMCExprValue(ProgramInfo.NumVGPR));
DelayedExprs->assignDocNode(Kern[".sgpr_count"], msgpack::Type::UInt,
ProgramInfo.NumSGPR);
DelayedExprs->assignDocNode(Kern[".vgpr_count"], msgpack::Type::UInt,
ProgramInfo.NumVGPR);

// Only add AGPR count to metadata for supported devices
if (STM.hasMAIInsts()) {
Kern[".agpr_count"] =
Kern.getDocument()->getNode(GetMCExprValue(ProgramInfo.NumAccVGPR));
DelayedExprs->assignDocNode(Kern[".agpr_count"], msgpack::Type::UInt,
ProgramInfo.NumAccVGPR);
}

Kern[".max_flat_workgroup_size"] =
Expand All @@ -527,6 +518,7 @@ MetadataStreamerMsgPackV4::getHSAKernelProps(const MachineFunction &MF,
}

bool MetadataStreamerMsgPackV4::emitTo(AMDGPUTargetStreamer &TargetStreamer) {
DelayedExprs->resolveDelayedExpressions();
return TargetStreamer.EmitHSAMetadata(*HSAMetadataDoc, true);
}

Expand All @@ -536,9 +528,11 @@ void MetadataStreamerMsgPackV4::begin(const Module &Mod,
emitTargetID(TargetID);
emitPrintf(Mod);
getRootMetadata("amdhsa.kernels") = HSAMetadataDoc->getArrayNode();
DelayedExprs->clear();
}

void MetadataStreamerMsgPackV4::end() {
DelayedExprs->resolveDelayedExpressions();
std::string HSAMetadataString;
raw_string_ostream StrOS(HSAMetadataString);
HSAMetadataDoc->toYAML(StrOS);
Expand Down
4 changes: 4 additions & 0 deletions llvm/lib/Target/AMDGPU/AMDGPUHSAMetadataStreamer.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#ifndef LLVM_LIB_TARGET_AMDGPU_MCTARGETDESC_AMDGPUHSAMETADATASTREAMER_H
#define LLVM_LIB_TARGET_AMDGPU_MCTARGETDESC_AMDGPUHSAMETADATASTREAMER_H

#include "Utils/AMDGPUDelayedMCExpr.h"
#include "llvm/BinaryFormat/MsgPackDocument.h"
#include "llvm/Support/AMDGPUMetadata.h"
#include "llvm/Support/Alignment.h"
Expand Down Expand Up @@ -65,6 +66,9 @@ class MetadataStreamer {
class LLVM_EXTERNAL_VISIBILITY MetadataStreamerMsgPackV4
: public MetadataStreamer {
protected:
std::unique_ptr<DelayedMCExprs> DelayedExprs =
std::make_unique<DelayedMCExprs>();

std::unique_ptr<msgpack::Document> HSAMetadataDoc =
std::make_unique<msgpack::Document>();

Expand Down
Loading
Loading