From 6eb80ad92a54507ae4467099cdb531cb257f2a42 Mon Sep 17 00:00:00 2001 From: Konstantin Zhuravlyov Date: Wed, 11 Oct 2017 22:59:35 +0000 Subject: [PATCH] AMDGPU/NFC: Minor clean ups in HSA metadata - 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 --- include/llvm/Support/AMDGPUMetadata.h | 12 +-- lib/Support/AMDGPUMetadata.cpp | 12 +-- lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp | 9 +- lib/Target/AMDGPU/AMDGPUAsmPrinter.h | 3 + .../AMDGPU/AsmParser/AMDGPUAsmParser.cpp | 94 +++++++++---------- .../AMDGPUHSAMetadataStreamer.cpp | 54 +++++------ .../MCTargetDesc/AMDGPUHSAMetadataStreamer.h | 15 ++- .../MCTargetDesc/AMDGPUTargetStreamer.cpp | 43 ++++----- .../MCTargetDesc/AMDGPUTargetStreamer.h | 17 ++-- 9 files changed, 119 insertions(+), 140 deletions(-) diff --git a/include/llvm/Support/AMDGPUMetadata.h b/include/llvm/Support/AMDGPUMetadata.h index 349d8f807e3..b8084cef1c1 100644 --- a/include/llvm/Support/AMDGPUMetadata.h +++ b/include/llvm/Support/AMDGPUMetadata.h @@ -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 diff --git a/lib/Support/AMDGPUMetadata.cpp b/lib/Support/AMDGPUMetadata.cpp index aa3af653cf7..00fa91a7cf0 100644 --- a/lib/Support/AMDGPUMetadata.cpp +++ b/lib/Support/AMDGPUMetadata.cpp @@ -196,18 +196,14 @@ struct MappingTraits { 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(); diff --git a/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp b/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp index 1cd570d3c0e..142543cc47e 100644 --- a/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp +++ b/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp @@ -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) { @@ -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( @@ -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() { diff --git a/lib/Target/AMDGPU/AMDGPUAsmPrinter.h b/lib/Target/AMDGPU/AMDGPUAsmPrinter.h index fd27d105651..88503e75dd2 100644 --- a/lib/Target/AMDGPU/AMDGPUAsmPrinter.h +++ b/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 @@ private: SIProgramInfo CurrentProgramInfo; DenseMap CallGraphResourceInfo; + + AMDGPU::HSAMD::MetadataStreamer HSAMetadataStream; std::map PALMetadataMap; uint64_t getFunctionCodeSize(const MachineFunction &MF) const; diff --git a/lib/Target/AMDGPU/AsmParser/AMDGPUAsmParser.cpp b/lib/Target/AMDGPU/AsmParser/AMDGPUAsmParser.cpp index 046132b7260..519c24b7c45 100644 --- a/lib/Target/AMDGPU/AsmParser/AMDGPUAsmParser.cpp +++ b/lib/Target/AMDGPU/AsmParser/AMDGPUAsmParser.cpp @@ -827,12 +827,12 @@ private: 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, @@ -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; @@ -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 (;;) { @@ -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(); diff --git a/lib/Target/AMDGPU/MCTargetDesc/AMDGPUHSAMetadataStreamer.cpp b/lib/Target/AMDGPU/MCTargetDesc/AMDGPUHSAMetadataStreamer.cpp index 78eda2ac9a3..1f5c05df7f9 100644 --- a/lib/Target/AMDGPU/MCTargetDesc/AMDGPUHSAMetadataStreamer.cpp +++ b/lib/Target/AMDGPU/MCTargetDesc/AMDGPUHSAMetadataStreamer.cpp @@ -32,29 +32,30 @@ static cl::opt 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'; } } @@ -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) @@ -411,26 +423,6 @@ void MetadataStreamer::emitKernel(const Function &Func, 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 diff --git a/lib/Target/AMDGPU/MCTargetDesc/AMDGPUHSAMetadataStreamer.h b/lib/Target/AMDGPU/MCTargetDesc/AMDGPUHSAMetadataStreamer.h index beb198a76a1..6a101d12914 100644 --- a/lib/Target/AMDGPU/MCTargetDesc/AMDGPUHSAMetadataStreamer.h +++ b/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 @@ private: 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 @@ public: 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 diff --git a/lib/Target/AMDGPU/MCTargetDesc/AMDGPUTargetStreamer.cpp b/lib/Target/AMDGPU/MCTargetDesc/AMDGPUTargetStreamer.cpp index 2cbd7932a3b..0a87fbe8092 100644 --- a/lib/Target/AMDGPU/MCTargetDesc/AMDGPUTargetStreamer.cpp +++ b/lib/Target/AMDGPU/MCTargetDesc/AMDGPUTargetStreamer.cpp @@ -42,18 +42,12 @@ using namespace llvm::AMDGPU; 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 @@ void AMDGPUTargetAsmStreamer::EmitAMDGPUSymbolType(StringRef SymbolName, } } -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 @@ void AMDGPUTargetELFStreamer::EmitAMDGPUSymbolType(StringRef SymbolName, 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 @@ bool AMDGPUTargetELFStreamer::EmitHSAMetadata(StringRef YamlString) { ElfNote::NT_AMDGPU_HSA_CODE_OBJECT_METADATA, [&](MCELFStreamer &OS) { OS.EmitLabel(DescBegin); - OS.EmitBytes(VerifiedYamlString.get()); + OS.EmitBytes(HSAMetadataString); OS.EmitLabel(DescEnd); } ); - return true; } diff --git a/lib/Target/AMDGPU/MCTargetDesc/AMDGPUTargetStreamer.h b/lib/Target/AMDGPU/MCTargetDesc/AMDGPUTargetStreamer.h index 911b8733a7c..d923d67bc00 100644 --- a/lib/Target/AMDGPU/MCTargetDesc/AMDGPUTargetStreamer.h +++ b/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 Type; class AMDGPUTargetStreamer : public MCTargetStreamer { protected: - AMDGPU::HSAMD::MetadataStreamer HSAMetadataStreamer; MCContext &getContext() const { return Streamer.getContext(); } public: @@ -44,15 +43,11 @@ 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; @@ -74,7 +69,7 @@ public: 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 @@ public: 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; -- 2.40.0