Index: include/llvm/Support/AMDGPUMetadata.h =================================================================== --- include/llvm/Support/AMDGPUMetadata.h +++ include/llvm/Support/AMDGPUMetadata.h @@ -402,15 +402,13 @@ /// \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 Index: lib/Support/AMDGPUMetadata.cpp =================================================================== --- lib/Support/AMDGPUMetadata.cpp +++ lib/Support/AMDGPUMetadata.cpp @@ -194,18 +194,14 @@ 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::max()); YamlOutput << HSAMetadata; return std::error_code(); Index: lib/Target/AMDGPU/AMDGPUAsmPrinter.h =================================================================== --- lib/Target/AMDGPU/AMDGPUAsmPrinter.h +++ lib/Target/AMDGPU/AMDGPUAsmPrinter.h @@ -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 @@ -112,6 +113,8 @@ SIProgramInfo CurrentProgramInfo; DenseMap CallGraphResourceInfo; + + AMDGPU::HSAMD::MetadataStreamer HSAMetadataStream; std::map PALMetadataMap; uint64_t getFunctionCodeSize(const MachineFunction &MF) const; Index: lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp =================================================================== --- lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp +++ lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp @@ -127,7 +127,8 @@ 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) { @@ -145,7 +146,8 @@ if (TM.getTargetTriple().getOS() != Triple::AMDHSA) return; - getTargetStreamer().EmitEndOfHSAMetadata(); + HSAMetadataStream.end(); + getTargetStreamer().EmitHSAMetadata(HSAMetadataStream.getHSAMetadata()); } bool AMDGPUAsmPrinter::isBlockOnlyReachableByFallthrough( @@ -178,7 +180,8 @@ if (TM.getTargetTriple().getOS() != Triple::AMDHSA) return; - getTargetStreamer().EmitKernelHSAMetadata(*MF->getFunction(), KernelCode); + + HSAMetadataStream.emitKernel(*MF->getFunction(), KernelCode); } void AMDGPUAsmPrinter::EmitFunctionEntryLabel() { Index: lib/Target/AMDGPU/AsmParser/AMDGPUAsmParser.cpp =================================================================== --- lib/Target/AMDGPU/AsmParser/AMDGPUAsmParser.cpp +++ lib/Target/AMDGPU/AsmParser/AMDGPUAsmParser.cpp @@ -828,12 +828,12 @@ 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, @@ -2401,49 +2401,6 @@ 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; @@ -2496,6 +2453,49 @@ 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 (;;) { @@ -2522,15 +2522,15 @@ 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(); Index: lib/Target/AMDGPU/MCTargetDesc/AMDGPUHSAMetadataStreamer.h =================================================================== --- lib/Target/AMDGPU/MCTargetDesc/AMDGPUHSAMetadataStreamer.h +++ lib/Target/AMDGPU/MCTargetDesc/AMDGPUHSAMetadataStreamer.h @@ -20,7 +20,6 @@ #include "AMDKernelCodeT.h" #include "llvm/ADT/StringRef.h" #include "llvm/Support/AMDGPUMetadata.h" -#include "llvm/Support/ErrorOr.h" namespace llvm { @@ -39,9 +38,9 @@ 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; @@ -81,15 +80,15 @@ 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 toYamlString(); - - ErrorOr toYamlString(StringRef YamlString); }; } // end namespace HSAMD Index: lib/Target/AMDGPU/MCTargetDesc/AMDGPUHSAMetadataStreamer.cpp =================================================================== --- lib/Target/AMDGPU/MCTargetDesc/AMDGPUHSAMetadataStreamer.cpp +++ lib/Target/AMDGPU/MCTargetDesc/AMDGPUHSAMetadataStreamer.cpp @@ -32,29 +32,30 @@ 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'; } } @@ -391,6 +392,17 @@ 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) @@ -407,26 +419,6 @@ emitKernelDebugProps(KernelCode); } -ErrorOr 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 MetadataStreamer::toYamlString(StringRef YamlString) { - if (auto Error = Metadata::fromYamlString(YamlString, HSAMetadata)) - return Error; - - return toYamlString(); -} - } // end namespace HSAMD } // end namespace AMDGPU } // end namespace llvm Index: lib/Target/AMDGPU/MCTargetDesc/AMDGPUTargetStreamer.h =================================================================== --- lib/Target/AMDGPU/MCTargetDesc/AMDGPUTargetStreamer.h +++ lib/Target/AMDGPU/MCTargetDesc/AMDGPUTargetStreamer.h @@ -10,9 +10,9 @@ #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" @@ -27,7 +27,6 @@ class AMDGPUTargetStreamer : public MCTargetStreamer { protected: - AMDGPU::HSAMD::MetadataStreamer HSAMetadataStreamer; MCContext &getContext() const { return Streamer.getContext(); } public: @@ -44,15 +43,11 @@ 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; @@ -74,7 +69,7 @@ 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; @@ -104,7 +99,7 @@ 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; Index: lib/Target/AMDGPU/MCTargetDesc/AMDGPUTargetStreamer.cpp =================================================================== --- lib/Target/AMDGPU/MCTargetDesc/AMDGPUTargetStreamer.cpp +++ lib/Target/AMDGPU/MCTargetDesc/AMDGPUTargetStreamer.cpp @@ -42,18 +42,12 @@ 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); } //===----------------------------------------------------------------------===// @@ -100,23 +94,22 @@ } } -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'; @@ -215,9 +208,10 @@ 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 @@ -234,11 +228,10 @@ ElfNote::NT_AMDGPU_HSA_CODE_OBJECT_METADATA, [&](MCELFStreamer &OS) { OS.EmitLabel(DescBegin); - OS.EmitBytes(VerifiedYamlString.get()); + OS.EmitBytes(HSAMetadataString); OS.EmitLabel(DescEnd); } ); - return true; }