/// \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
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();
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) {
if (TM.getTargetTriple().getOS() != Triple::AMDHSA)
return;
- getTargetStreamer().EmitEndOfHSAMetadata();
+ HSAMetadataStream.end();
+ getTargetStreamer().EmitHSAMetadata(HSAMetadataStream.getHSAMetadata());
}
bool AMDGPUAsmPrinter::isBlockOnlyReachableByFallthrough(
if (TM.getTargetTriple().getOS() != Triple::AMDHSA)
return;
- getTargetStreamer().EmitKernelHSAMetadata(*MF->getFunction(), KernelCode);
+
+ HSAMetadataStream.emitKernel(*MF->getFunction(), KernelCode);
}
void AMDGPUAsmPrinter::EmitFunctionEntryLabel() {
#include "AMDGPU.h"
#include "AMDKernelCodeT.h"
+#include "MCTargetDesc/AMDGPUHSAMetadataStreamer.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/CodeGen/AsmPrinter.h"
#include <cstddef>
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;
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,
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;
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 (;;) {
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();
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';
}
}
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)
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
#include "AMDKernelCodeT.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/AMDGPUMetadata.h"
-#include "llvm/Support/ErrorOr.h"
namespace llvm {
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;
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
AMDGPUTargetStreamer::AMDGPUTargetStreamer(MCStreamer &S)
: MCTargetStreamer(S) {}
-void AMDGPUTargetStreamer::EmitStartOfHSAMetadata(const Module &Mod) {
- HSAMetadataStreamer.begin(Mod);
-}
-
-void AMDGPUTargetStreamer::EmitKernelHSAMetadata(
- const Function &Func, const amd_kernel_code_t &KernelCode) {
- HSAMetadataStreamer.emitKernel(Func, KernelCode);
-}
+bool AMDGPUTargetStreamer::EmitHSAMetadata(StringRef HSAMetadataString) {
+ HSAMD::Metadata HSAMetadata;
+ if (auto Error = HSAMD::fromString(HSAMetadataString, HSAMetadata))
+ return false;
-void AMDGPUTargetStreamer::EmitEndOfHSAMetadata() {
- HSAMetadataStreamer.end();
- EmitHSAMetadata(HSAMetadataStreamer.toYamlString().get());
+ return EmitHSAMetadata(HSAMetadata);
}
//===----------------------------------------------------------------------===//
}
}
-bool AMDGPUTargetAsmStreamer::EmitHSAMetadata(StringRef YamlString) {
- auto VerifiedYamlString = HSAMetadataStreamer.toYamlString(YamlString);
- if (!VerifiedYamlString)
+bool AMDGPUTargetAsmStreamer::EmitHSAMetadata(
+ const AMDGPU::HSAMD::Metadata &HSAMetadata) {
+ std::string HSAMetadataString;
+ if (auto Error = HSAMD::toString(HSAMetadata, HSAMetadataString))
return false;
- OS << '\t' << AMDGPU::HSAMD::AssemblerDirectiveBegin << '\n';
- OS << VerifiedYamlString.get();
- OS << '\t' << AMDGPU::HSAMD::AssemblerDirectiveEnd << '\n';
-
+ OS << '\t' << HSAMD::AssemblerDirectiveBegin << '\n';
+ OS << HSAMetadataString << '\n';
+ OS << '\t' << HSAMD::AssemblerDirectiveEnd << '\n';
return true;
}
bool AMDGPUTargetAsmStreamer::EmitPALMetadata(
const PALMD::Metadata &PALMetadata) {
std::string PALMetadataString;
- auto Error = PALMD::toString(PALMetadata, PALMetadataString);
- if (Error)
+ if (auto Error = PALMD::toString(PALMetadata, PALMetadataString))
return false;
OS << '\t' << PALMD::AssemblerDirective << PALMetadataString << '\n';
Symbol->setType(ELF::STT_AMDGPU_HSA_KERNEL);
}
-bool AMDGPUTargetELFStreamer::EmitHSAMetadata(StringRef YamlString) {
- auto VerifiedYamlString = HSAMetadataStreamer.toYamlString(YamlString);
- if (!VerifiedYamlString)
+bool AMDGPUTargetELFStreamer::EmitHSAMetadata(
+ const AMDGPU::HSAMD::Metadata &HSAMetadata) {
+ std::string HSAMetadataString;
+ if (auto Error = HSAMD::toString(HSAMetadata, HSAMetadataString))
return false;
// Create two labels to mark the beginning and end of the desc field
ElfNote::NT_AMDGPU_HSA_CODE_OBJECT_METADATA,
[&](MCELFStreamer &OS) {
OS.EmitLabel(DescBegin);
- OS.EmitBytes(VerifiedYamlString.get());
+ OS.EmitBytes(HSAMetadataString);
OS.EmitLabel(DescEnd);
}
);
-
return true;
}
#ifndef LLVM_LIB_TARGET_AMDGPU_MCTARGETDESC_AMDGPUTARGETSTREAMER_H
#define LLVM_LIB_TARGET_AMDGPU_MCTARGETDESC_AMDGPUTARGETSTREAMER_H
-#include "AMDGPUHSAMetadataStreamer.h"
#include "AMDKernelCodeT.h"
#include "llvm/MC/MCStreamer.h"
+#include "llvm/Support/AMDGPUMetadata.h"
namespace llvm {
#include "AMDGPUPTNote.h"
class AMDGPUTargetStreamer : public MCTargetStreamer {
protected:
- AMDGPU::HSAMD::MetadataStreamer HSAMetadataStreamer;
MCContext &getContext() const { return Streamer.getContext(); }
public:
virtual void EmitAMDGPUSymbolType(StringRef SymbolName, unsigned Type) = 0;
- virtual void EmitStartOfHSAMetadata(const Module &Mod);
-
- virtual void EmitKernelHSAMetadata(
- const Function &Func, const amd_kernel_code_t &KernelCode);
-
- virtual void EmitEndOfHSAMetadata();
+ /// \returns True on success, false on failure.
+ virtual bool EmitHSAMetadata(StringRef HSAMetadataString);
/// \returns True on success, false on failure.
- virtual bool EmitHSAMetadata(StringRef YamlString) = 0;
+ virtual bool EmitHSAMetadata(const AMDGPU::HSAMD::Metadata &HSAMetadata) = 0;
/// \returns True on success, false on failure.
virtual bool EmitPALMetadata(const AMDGPU::PALMD::Metadata &PALMetadata) = 0;
void EmitAMDGPUSymbolType(StringRef SymbolName, unsigned Type) override;
/// \returns True on success, false on failure.
- bool EmitHSAMetadata(StringRef YamlString) override;
+ bool EmitHSAMetadata(const AMDGPU::HSAMD::Metadata &HSAMetadata) override;
/// \returns True on success, false on failure.
bool EmitPALMetadata(const AMDGPU::PALMD::Metadata &PALMetadata) override;
void EmitAMDGPUSymbolType(StringRef SymbolName, unsigned Type) override;
/// \returns True on success, false on failure.
- bool EmitHSAMetadata(StringRef YamlString) override;
+ bool EmitHSAMetadata(const AMDGPU::HSAMD::Metadata &HSAMetadata) override;
/// \returns True on success, false on failure.
bool EmitPALMetadata(const AMDGPU::PALMD::Metadata &PALMetadata) override;