Skip to content

Commit

Permalink
AMDGPU/NFC: Minor clean ups in HSA metadata
Browse files Browse the repository at this point in the history
  - Use HSA metadata streamer directly from AMDGPUAsmPrinter
  - Make naming consistent with PAL metadata

Differential Revision: https://reviews.llvm.org/D38746


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@315526 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
kzhuravl committed Oct 11, 2017
1 parent 2578287 commit 6eb80ad
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 140 deletions.
12 changes: 5 additions & 7 deletions include/llvm/Support/AMDGPUMetadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -406,15 +406,13 @@ struct Metadata final {

/// \brief Default constructor.
Metadata() = default;
};

/// \brief Converts \p YamlString to \p HSAMetadata.
static std::error_code fromYamlString(std::string YamlString,
Metadata &HSAMetadata);
/// \brief Converts \p String to \p HSAMetadata.
std::error_code fromString(std::string String, Metadata &HSAMetadata);

/// \brief Converts \p HSAMetadata to \p YamlString.
static std::error_code toYamlString(Metadata HSAMetadata,
std::string &YamlString);
};
/// \brief Converts \p HSAMetadata to \p String.
std::error_code toString(Metadata HSAMetadata, std::string &String);

} // end namespace HSAMD

Expand Down
12 changes: 4 additions & 8 deletions lib/Support/AMDGPUMetadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,18 +196,14 @@ struct MappingTraits<HSAMD::Metadata> {
namespace AMDGPU {
namespace HSAMD {

/* static */
std::error_code Metadata::fromYamlString(
std::string YamlString, Metadata &HSAMetadata) {
yaml::Input YamlInput(YamlString);
std::error_code fromString(std::string String, Metadata &HSAMetadata) {
yaml::Input YamlInput(String);
YamlInput >> HSAMetadata;
return YamlInput.error();
}

/* static */
std::error_code Metadata::toYamlString(
Metadata HSAMetadata, std::string &YamlString) {
raw_string_ostream YamlStream(YamlString);
std::error_code toString(Metadata HSAMetadata, std::string &String) {
raw_string_ostream YamlStream(String);
yaml::Output YamlOutput(YamlStream, nullptr, std::numeric_limits<int>::max());
YamlOutput << HSAMetadata;
return std::error_code();
Expand Down
9 changes: 6 additions & 3 deletions lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ void AMDGPUAsmPrinter::EmitStartOfAsmFile(Module &M) {
getTargetStreamer().EmitDirectiveHSACodeObjectVersion(2, 1);
getTargetStreamer().EmitDirectiveHSACodeObjectISA(
ISA.Major, ISA.Minor, ISA.Stepping, "AMD", "AMDGPU");
getTargetStreamer().EmitStartOfHSAMetadata(M);

HSAMetadataStream.begin(M);
}

void AMDGPUAsmPrinter::EmitEndOfAsmFile(Module &M) {
Expand All @@ -145,7 +146,8 @@ void AMDGPUAsmPrinter::EmitEndOfAsmFile(Module &M) {
if (TM.getTargetTriple().getOS() != Triple::AMDHSA)
return;

getTargetStreamer().EmitEndOfHSAMetadata();
HSAMetadataStream.end();
getTargetStreamer().EmitHSAMetadata(HSAMetadataStream.getHSAMetadata());
}

bool AMDGPUAsmPrinter::isBlockOnlyReachableByFallthrough(
Expand Down Expand Up @@ -178,7 +180,8 @@ void AMDGPUAsmPrinter::EmitFunctionBodyStart() {

if (TM.getTargetTriple().getOS() != Triple::AMDHSA)
return;
getTargetStreamer().EmitKernelHSAMetadata(*MF->getFunction(), KernelCode);

HSAMetadataStream.emitKernel(*MF->getFunction(), KernelCode);
}

void AMDGPUAsmPrinter::EmitFunctionEntryLabel() {
Expand Down
3 changes: 3 additions & 0 deletions lib/Target/AMDGPU/AMDGPUAsmPrinter.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include "AMDGPU.h"
#include "AMDKernelCodeT.h"
#include "MCTargetDesc/AMDGPUHSAMetadataStreamer.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/CodeGen/AsmPrinter.h"
#include <cstddef>
Expand Down Expand Up @@ -112,6 +113,8 @@ class AMDGPUAsmPrinter final : public AsmPrinter {

SIProgramInfo CurrentProgramInfo;
DenseMap<const Function *, SIFunctionResourceInfo> CallGraphResourceInfo;

AMDGPU::HSAMD::MetadataStreamer HSAMetadataStream;
std::map<uint32_t, uint32_t> PALMetadataMap;

uint64_t getFunctionCodeSize(const MachineFunction &MF) const;
Expand Down
94 changes: 47 additions & 47 deletions lib/Target/AMDGPU/AsmParser/AMDGPUAsmParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -827,12 +827,12 @@ class AMDGPUAsmParser : public MCTargetAsmParser {
bool ParseDirectiveMajorMinor(uint32_t &Major, uint32_t &Minor);
bool ParseDirectiveHSACodeObjectVersion();
bool ParseDirectiveHSACodeObjectISA();
bool ParseDirectiveHSAMetadata();
bool ParseAMDKernelCodeTValue(StringRef ID, amd_kernel_code_t &Header);
bool ParseDirectiveAMDKernelCodeT();
bool subtargetHasRegister(const MCRegisterInfo &MRI, unsigned RegNo) const;
bool ParseDirectiveAMDGPUHsaKernel();

bool ParseDirectiveHSAMetadata();
bool ParseDirectivePALMetadata();

bool AddNextRegisterToList(unsigned& Reg, unsigned& RegWidth,
Expand Down Expand Up @@ -2400,49 +2400,6 @@ bool AMDGPUAsmParser::ParseDirectiveHSACodeObjectISA() {
return false;
}

bool AMDGPUAsmParser::ParseDirectiveHSAMetadata() {
std::string YamlString;
raw_string_ostream YamlStream(YamlString);

getLexer().setSkipSpace(false);

bool FoundEnd = false;
while (!getLexer().is(AsmToken::Eof)) {
while (getLexer().is(AsmToken::Space)) {
YamlStream << getLexer().getTok().getString();
Lex();
}

if (getLexer().is(AsmToken::Identifier)) {
StringRef ID = getLexer().getTok().getIdentifier();
if (ID == AMDGPU::HSAMD::AssemblerDirectiveEnd) {
Lex();
FoundEnd = true;
break;
}
}

YamlStream << Parser.parseStringToEndOfStatement()
<< getContext().getAsmInfo()->getSeparatorString();

Parser.eatToEndOfStatement();
}

getLexer().setSkipSpace(true);

if (getLexer().is(AsmToken::Eof) && !FoundEnd) {
return TokError(
"expected directive .end_amd_amdgpu_hsa_metadata not found");
}

YamlStream.flush();

if (!getTargetStreamer().EmitHSAMetadata(YamlString))
return Error(getParser().getTok().getLoc(), "invalid code object metadata");

return false;
}

bool AMDGPUAsmParser::ParseAMDKernelCodeTValue(StringRef ID,
amd_kernel_code_t &Header) {
SmallString<40> ErrStr;
Expand Down Expand Up @@ -2495,6 +2452,49 @@ bool AMDGPUAsmParser::ParseDirectiveAMDGPUHsaKernel() {
return false;
}

bool AMDGPUAsmParser::ParseDirectiveHSAMetadata() {
std::string HSAMetadataString;
raw_string_ostream YamlStream(HSAMetadataString);

getLexer().setSkipSpace(false);

bool FoundEnd = false;
while (!getLexer().is(AsmToken::Eof)) {
while (getLexer().is(AsmToken::Space)) {
YamlStream << getLexer().getTok().getString();
Lex();
}

if (getLexer().is(AsmToken::Identifier)) {
StringRef ID = getLexer().getTok().getIdentifier();
if (ID == AMDGPU::HSAMD::AssemblerDirectiveEnd) {
Lex();
FoundEnd = true;
break;
}
}

YamlStream << Parser.parseStringToEndOfStatement()
<< getContext().getAsmInfo()->getSeparatorString();

Parser.eatToEndOfStatement();
}

getLexer().setSkipSpace(true);

if (getLexer().is(AsmToken::Eof) && !FoundEnd) {
return TokError(Twine("expected directive ") +
Twine(HSAMD::AssemblerDirectiveEnd) + Twine("not found"));
}

YamlStream.flush();

if (!getTargetStreamer().EmitHSAMetadata(HSAMetadataString))
return Error(getParser().getTok().getLoc(), "invalid HSA metadata");

return false;
}

bool AMDGPUAsmParser::ParseDirectivePALMetadata() {
PALMD::Metadata PALMetadata;
for (;;) {
Expand All @@ -2521,15 +2521,15 @@ bool AMDGPUAsmParser::ParseDirective(AsmToken DirectiveID) {
if (IDVal == ".hsa_code_object_isa")
return ParseDirectiveHSACodeObjectISA();

if (IDVal == AMDGPU::HSAMD::AssemblerDirectiveBegin)
return ParseDirectiveHSAMetadata();

if (IDVal == ".amd_kernel_code_t")
return ParseDirectiveAMDKernelCodeT();

if (IDVal == ".amdgpu_hsa_kernel")
return ParseDirectiveAMDGPUHsaKernel();

if (IDVal == AMDGPU::HSAMD::AssemblerDirectiveBegin)
return ParseDirectiveHSAMetadata();

if (IDVal == PALMD::AssemblerDirective)
return ParseDirectivePALMetadata();

Expand Down
54 changes: 23 additions & 31 deletions lib/Target/AMDGPU/MCTargetDesc/AMDGPUHSAMetadataStreamer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,29 +32,30 @@ static cl::opt<bool> VerifyHSAMetadata(
namespace AMDGPU {
namespace HSAMD {

void MetadataStreamer::dump(StringRef YamlString) const {
errs() << "AMDGPU HSA Metadata:\n" << YamlString << '\n';
void MetadataStreamer::dump(StringRef HSAMetadataString) const {
errs() << "AMDGPU HSA Metadata:\n" << HSAMetadataString << '\n';
}

void MetadataStreamer::verify(StringRef YamlString) const {
void MetadataStreamer::verify(StringRef HSAMetadataString) const {
errs() << "AMDGPU HSA Metadata Parser Test: ";

HSAMD::Metadata FromYamlString;
if (Metadata::fromYamlString(YamlString, FromYamlString)) {
HSAMD::Metadata FromHSAMetadataString;
if (fromString(HSAMetadataString, FromHSAMetadataString)) {
errs() << "FAIL\n";
return;
}

std::string ToYamlString;
if (Metadata::toYamlString(FromYamlString, ToYamlString)) {
std::string ToHSAMetadataString;
if (toString(FromHSAMetadataString, ToHSAMetadataString)) {
errs() << "FAIL\n";
return;
}

errs() << (YamlString == ToYamlString ? "PASS" : "FAIL") << '\n';
if (YamlString != ToYamlString) {
errs() << "Original input: " << YamlString << '\n'
<< "Produced output: " << ToYamlString << '\n';
errs() << (HSAMetadataString == ToHSAMetadataString ? "PASS" : "FAIL")
<< '\n';
if (HSAMetadataString != ToHSAMetadataString) {
errs() << "Original input: " << HSAMetadataString << '\n'
<< "Produced output: " << ToHSAMetadataString << '\n';
}
}

Expand Down Expand Up @@ -395,6 +396,17 @@ void MetadataStreamer::begin(const Module &Mod) {
emitPrintf(Mod);
}

void MetadataStreamer::end() {
std::string HSAMetadataString;
if (auto Error = toString(HSAMetadata, HSAMetadataString))
return;

if (DumpHSAMetadata)
dump(HSAMetadataString);
if (VerifyHSAMetadata)
verify(HSAMetadataString);
}

void MetadataStreamer::emitKernel(const Function &Func,
const amd_kernel_code_t &KernelCode) {
if (Func.getCallingConv() != CallingConv::AMDGPU_KERNEL)
Expand All @@ -411,26 +423,6 @@ void MetadataStreamer::emitKernel(const Function &Func,
emitKernelDebugProps(KernelCode);
}

ErrorOr<std::string> MetadataStreamer::toYamlString() {
std::string ToYamlString;
if (auto Error = Metadata::toYamlString(HSAMetadata, ToYamlString))
return Error;

if (DumpHSAMetadata)
dump(ToYamlString);
if (VerifyHSAMetadata)
verify(ToYamlString);

return ToYamlString;
}

ErrorOr<std::string> MetadataStreamer::toYamlString(StringRef YamlString) {
if (auto Error = Metadata::fromYamlString(YamlString, HSAMetadata))
return Error;

return toYamlString();
}

} // end namespace HSAMD
} // end namespace AMDGPU
} // end namespace llvm
15 changes: 7 additions & 8 deletions lib/Target/AMDGPU/MCTargetDesc/AMDGPUHSAMetadataStreamer.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#include "AMDKernelCodeT.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/AMDGPUMetadata.h"
#include "llvm/Support/ErrorOr.h"

namespace llvm {

Expand All @@ -39,9 +38,9 @@ class MetadataStreamer final {
Metadata HSAMetadata;
AMDGPUAS AMDGPUASI;

void dump(StringRef YamlString) const;
void dump(StringRef HSAMetadataString) const;

void verify(StringRef YamlString) const;
void verify(StringRef HSAMetadataString) const;

AccessQualifier getAccessQualifier(StringRef AccQual) const;

Expand Down Expand Up @@ -81,15 +80,15 @@ class MetadataStreamer final {
MetadataStreamer() = default;
~MetadataStreamer() = default;

const Metadata &getHSAMetadata() const {
return HSAMetadata;
}

void begin(const Module &Mod);

void end() {}
void end();

void emitKernel(const Function &Func, const amd_kernel_code_t &KernelCode);

ErrorOr<std::string> toYamlString();

ErrorOr<std::string> toYamlString(StringRef YamlString);
};

} // end namespace HSAMD
Expand Down
Loading

0 comments on commit 6eb80ad

Please sign in to comment.