Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[AMDGPU] Update code object metadata for kernarg preload #134666

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
Loading
from
Open
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
2 changes: 1 addition & 1 deletion 2 llvm/include/llvm/Support/AMDGPUMetadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ constexpr uint32_t VersionMinorV5 = 2;
/// HSA metadata major version for code object V6.
constexpr uint32_t VersionMajorV6 = 1;
/// HSA metadata minor version for code object V6.
constexpr uint32_t VersionMinorV6 = 2;
constexpr uint32_t VersionMinorV6 = 3;

/// Old HSA metadata beginning assembler directive for V2. This is only used for
/// diagnostics now.
Expand Down
34 changes: 34 additions & 0 deletions 34 llvm/lib/Target/AMDGPU/AMDGPUArgumentUsageInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@
#include "llvm/Support/raw_ostream.h"

using namespace llvm;
using namespace llvm::KernArgPreload;

#define DEBUG_TYPE "amdgpu-argument-reg-usage-info"

INITIALIZE_PASS(AMDGPUArgumentUsageInfo, DEBUG_TYPE,
"Argument Register Usage Information Storage", false, true)

constexpr HiddenArgInfo HiddenArgUtils::HiddenArgs[END_HIDDEN_ARGS];

void ArgDescriptor::print(raw_ostream &OS,
const TargetRegisterInfo *TRI) const {
if (!isSet()) {
Expand Down Expand Up @@ -176,6 +179,37 @@ AMDGPUFunctionArgInfo AMDGPUFunctionArgInfo::fixedABILayout() {
return AI;
}

SmallVector<const KernArgPreloadDescriptor *, 4>
AMDGPUFunctionArgInfo::getPreloadDescriptorsForArgIdx(unsigned ArgIdx) const {
SmallVector<const KernArgPreloadDescriptor *, 4> Results;
for (const auto &KV : PreloadKernArgs) {
if (KV.second.OrigArgIdx == ArgIdx)
Results.push_back(&KV.second);
}

stable_sort(Results, [](const KernArgPreloadDescriptor *A,
const KernArgPreloadDescriptor *B) {
return A->PartIdx < B->PartIdx;
});

return Results;
}

const KernArgPreloadDescriptor *
AMDGPUFunctionArgInfo::getHiddenArgPreloadDescriptor(HiddenArg HA) const {
assert(HA < END_HIDDEN_ARGS);

auto HiddenArgIt = PreloadHiddenArgsIndexMap.find(HA);
if (HiddenArgIt == PreloadHiddenArgsIndexMap.end())
return nullptr;

auto KernArgIt = PreloadKernArgs.find(HiddenArgIt->second);
if (KernArgIt == PreloadKernArgs.end())
return nullptr;

return &KernArgIt->second;
}

const AMDGPUFunctionArgInfo &
AMDGPUArgumentUsageInfo::lookupFuncArgInfo(const Function &F) const {
auto I = ArgInfoMap.find(&F);
Expand Down
92 changes: 88 additions & 4 deletions 92 llvm/lib/Target/AMDGPU/AMDGPUArgumentUsageInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@

#include "MCTargetDesc/AMDGPUMCTargetDesc.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/Analysis/ValueTracking.h"
#include "llvm/CodeGen/Register.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Type.h"
#include "llvm/Pass.h"

namespace llvm {
Expand Down Expand Up @@ -95,11 +98,79 @@ inline raw_ostream &operator<<(raw_ostream &OS, const ArgDescriptor &Arg) {
return OS;
}

struct KernArgPreloadDescriptor : public ArgDescriptor {
KernArgPreloadDescriptor() {}
SmallVector<MCRegister> Regs;
namespace KernArgPreload {

enum HiddenArg {
HIDDEN_BLOCK_COUNT_X,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:

Suggested change
HIDDEN_BLOCK_COUNT_X,
HIDDEN_BLOCK_COUNT_X = 0,

HIDDEN_BLOCK_COUNT_Y,
HIDDEN_BLOCK_COUNT_Z,
HIDDEN_GROUP_SIZE_X,
HIDDEN_GROUP_SIZE_Y,
HIDDEN_GROUP_SIZE_Z,
HIDDEN_REMAINDER_X,
HIDDEN_REMAINDER_Y,
HIDDEN_REMAINDER_Z,
END_HIDDEN_ARGS
};

// Stores information about a specific hidden argument.
struct HiddenArgInfo {
// Offset in bytes from the location in the kernearg segment pointed to by
// the implicitarg pointer.
uint8_t Offset;
// The size of the hidden argument in bytes.
uint8_t Size;
// The name of the hidden argument in the kernel signature.
const char *Name;
};
Comment on lines +117 to +125
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:

Suggested change
struct HiddenArgInfo {
// Offset in bytes from the location in the kernearg segment pointed to by
// the implicitarg pointer.
uint8_t Offset;
// The size of the hidden argument in bytes.
uint8_t Size;
// The name of the hidden argument in the kernel signature.
const char *Name;
};
struct HiddenArgInfo {
// Offset in bytes from the location in the kernearg segment pointed to by
// the implicitarg pointer.
uint8_t Offset = 0;
// The size of the hidden argument in bytes.
uint8_t Size = 0;
// The name of the hidden argument in the kernel signature.
const char *Name = nullptr;
};


struct HiddenArgUtils {
static constexpr HiddenArgInfo HiddenArgs[END_HIDDEN_ARGS] = {
{0, 4, "_hidden_block_count_x"}, {4, 4, "_hidden_block_count_y"},
{8, 4, "_hidden_block_count_z"}, {12, 2, "_hidden_group_size_x"},
{14, 2, "_hidden_group_size_y"}, {16, 2, "_hidden_group_size_z"},
{18, 2, "_hidden_remainder_x"}, {20, 2, "_hidden_remainder_y"},
{22, 2, "_hidden_remainder_z"}};

static HiddenArg getHiddenArgFromOffset(unsigned Offset) {
for (unsigned I = 0; I < END_HIDDEN_ARGS; ++I) {
if (HiddenArgs[I].Offset == Offset)
return static_cast<HiddenArg>(I);
}

return END_HIDDEN_ARGS;
}

static Type *getHiddenArgType(LLVMContext &Ctx, HiddenArg HA) {
if (HA < END_HIDDEN_ARGS)
return Type::getIntNTy(Ctx, HiddenArgs[HA].Size * 8);

llvm_unreachable("unexpected hidden argument");
}

static const char *getHiddenArgName(HiddenArg HA) {
if (HA < END_HIDDEN_ARGS)
return HiddenArgs[HA].Name;

llvm_unreachable("unexpected hidden argument");
}
};

struct KernArgPreloadDescriptor {
// Id of the original argument in the IR kernel function argument list.
unsigned OrigArgIdx = 0;

// If this IR argument was split into multiple parts, this is the index of the
// part in the original argument.
unsigned PartIdx = 0;

// The registers that the argument is preloaded into. The argument may be
// split accross multilpe registers.
SmallVector<MCRegister, 2> Regs;
};

} // namespace KernArgPreload

struct AMDGPUFunctionArgInfo {
// clang-format off
enum PreloadedValue {
Expand Down Expand Up @@ -161,14 +232,27 @@ struct AMDGPUFunctionArgInfo {
ArgDescriptor WorkItemIDZ;

// Map the index of preloaded kernel arguments to its descriptor.
SmallDenseMap<int, KernArgPreloadDescriptor> PreloadKernArgs{};
SmallDenseMap<int, KernArgPreload::KernArgPreloadDescriptor>
PreloadKernArgs{};
// Map hidden argument to the index of it's descriptor.
SmallDenseMap<KernArgPreload::HiddenArg, int> PreloadHiddenArgsIndexMap{};
// The first user SGPR allocated for kernarg preloading.
Register FirstKernArgPreloadReg;

std::tuple<const ArgDescriptor *, const TargetRegisterClass *, LLT>
getPreloadedValue(PreloadedValue Value) const;

static AMDGPUFunctionArgInfo fixedABILayout();

// Returns preload argument descriptors for an IR argument index. Isel may
// split IR arguments into multiple parts, the return vector holds all parts
// associated with an IR argument in the kernel signature.
SmallVector<const KernArgPreload::KernArgPreloadDescriptor *, 4>
getPreloadDescriptorsForArgIdx(unsigned ArgIdx) const;

// Returns the hidden arguments `KernArgPreloadDescriptor` if it is preloaded.
const KernArgPreload::KernArgPreloadDescriptor *
getHiddenArgPreloadDescriptor(KernArgPreload::HiddenArg HA) const;
};

class AMDGPUArgumentUsageInfo : public ImmutablePass {
Expand Down
Loading
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.