Skip to content

Commit

Permalink
[AMDGPU] Convert ErrorOr to Expected + needed changes for rL287081
Browse files Browse the repository at this point in the history
  • Loading branch information
kzhuravl committed Nov 17, 2016
1 parent 93fbfac commit 047be2b
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 61 deletions.
75 changes: 44 additions & 31 deletions lib/Target/AMDGPU/Disassembler/CodeObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,23 @@ const ELFNote* getNext(const ELFNote &N) {
N.getDesc().data() + alignTo(N.descsz, ELFNote::ALIGN));
}

ErrorOr<const amd_kernel_code_t *> KernelSym::getAmdKernelCodeT(
Expected<const amd_kernel_code_t *> KernelSym::getAmdKernelCodeT(
const HSACodeObject *CodeObject) const {
auto TextOr = CodeObject->getTextSection();
if (!TextOr) {
return TextOr.getError();
return TextOr.takeError();
}

auto ArrayOr = CodeObject->getELFFile()->getSectionContentsAsArray<uint8_t>(*TextOr);
if (!ArrayOr)
return ArrayOr.getError();
return ArrayOr.takeError();

return reinterpret_cast<const amd_kernel_code_t *>((*ArrayOr).data() + getValue());
}

ErrorOr<const KernelSym *> KernelSym::asKernelSym(const HSACodeObject::Elf_Sym *Sym) {
Expected<const KernelSym *> KernelSym::asKernelSym(const HSACodeObject::Elf_Sym *Sym) {
if (Sym->getType() != ELF::STT_AMDGPU_HSA_KERNEL)
return std::error_code();
return createError("invalid symbol type");

return static_cast<const KernelSym *>(Sym);
}
Expand All @@ -51,7 +51,13 @@ void HSACodeObject::InitMarkers() const {
KernelMarkers.push_back((*TextSecOr)->sh_size);

for (const auto &Sym : kernels()) {
auto Kernel = KernelSym::asKernelSym(getSymbol(Sym.getRawDataRefImpl())).get();
auto ExpectedKernel = KernelSym::asKernelSym(getSymbol(Sym.getRawDataRefImpl()));
if (!ExpectedKernel) {
consumeError(ExpectedKernel.takeError());
report_fatal_error("invalid kernel symbol");
}

auto Kernel = ExpectedKernel.get();
KernelMarkers.push_back(Kernel->st_value);
if (auto KernelCodeOr = Kernel->getAmdKernelCodeT(this)) {
KernelMarkers.push_back(Kernel->st_value + (*KernelCodeOr)->kernel_code_entry_byte_offset);
Expand Down Expand Up @@ -86,8 +92,15 @@ kernel_sym_iterator HSACodeObject::kernels_begin() const {
auto TextIdx = TextIdxOr.get();
return kernel_sym_iterator(symbol_begin(), symbol_end(),
[this, TextIdx](const SymbolRef &Sym)->bool {
auto KernelOr = KernelSym::asKernelSym(getSymbol(Sym.getRawDataRefImpl()));
if (!KernelOr || (*KernelOr)->st_shndx != TextIdx)

auto ExpectedKernel = KernelSym::asKernelSym(getSymbol(Sym.getRawDataRefImpl()));
if (!ExpectedKernel) {
consumeError(ExpectedKernel.takeError());
return false;
}

auto Kernel = ExpectedKernel.get();
if (Kernel->st_shndx != TextIdx)
return false;

return true;
Expand All @@ -103,18 +116,18 @@ iterator_range<kernel_sym_iterator> HSACodeObject::kernels() const {
return make_range(kernels_begin(), kernels_end());
}

ErrorOr<ArrayRef<uint8_t>> HSACodeObject::getKernelCode(const KernelSym *Kernel) const {
Expected<ArrayRef<uint8_t>> HSACodeObject::getKernelCode(const KernelSym *Kernel) const {
auto KernelCodeTOr = Kernel->getAmdKernelCodeT(this);
if (!KernelCodeTOr)
return KernelCodeTOr.getError();
return KernelCodeTOr.takeError();

auto TextOr = getTextSection();
if (!TextOr)
return TextOr.getError();
return TextOr.takeError();

auto SecBytesOr = getELFFile()->getSectionContentsAsArray<uint8_t>(*TextOr);
if (!SecBytesOr)
return SecBytesOr.getError();
return SecBytesOr.takeError();

uint64_t CodeStart = Kernel->getValue() +
(*KernelCodeTOr)->kernel_code_entry_byte_offset;
Expand All @@ -128,68 +141,68 @@ ErrorOr<ArrayRef<uint8_t>> HSACodeObject::getKernelCode(const KernelSym *Kernel)
return SecBytesOr->slice(CodeStart, CodeEnd - CodeStart);
}

ErrorOr<const HSACodeObject::Elf_Shdr *>
Expected<const HSACodeObject::Elf_Shdr *>
HSACodeObject::getSectionByName(StringRef Name) const {
auto ELF = getELFFile();
auto SectionsOr = ELF->sections();
if (!SectionsOr)
return SectionsOr.getError();
return SectionsOr.takeError();

for (const auto &Sec : *SectionsOr) {
auto SecNameOr = ELF->getSectionName(&Sec);
if (std::error_code EC = SecNameOr.getError()) {
return EC;
if (!SecNameOr) {
return SecNameOr.takeError();
} else if (*SecNameOr == Name) {
return ErrorOr<const Elf_Shdr *>(&Sec);
return Expected<const Elf_Shdr *>(&Sec);
}
}
return object_error::invalid_section_index;
return createError("invalid section index");
}

ErrorOr<uint32_t> HSACodeObject::getSectionIdxByName(StringRef Name) const {
Expected<uint32_t> HSACodeObject::getSectionIdxByName(StringRef Name) const {
auto ELF = getELFFile();
uint32_t Idx = 0;
auto SectionsOr = ELF->sections();
if (!SectionsOr)
return SectionsOr.getError();
return SectionsOr.takeError();

for (const auto &Sec : *SectionsOr) {
auto SecNameOr = ELF->getSectionName(&Sec);
if (std::error_code EC = SecNameOr.getError()) {
return EC;
if (!SecNameOr) {
return SecNameOr.takeError();
} else if (*SecNameOr == Name) {
return Idx;
}
++Idx;
}
return object_error::invalid_section_index;
return createError("invalid section index");
}

ErrorOr<uint32_t> HSACodeObject::getTextSectionIdx() const {
Expected<uint32_t> HSACodeObject::getTextSectionIdx() const {
if (auto IdxOr = getSectionIdxByName(".text")) {
auto SecOr = getELFFile()->getSection(*IdxOr);
if (SecOr || isSectionText(toDRI(*SecOr)))
return IdxOr;
}
return object_error::invalid_section_index;
return createError("invalid section index");
}

ErrorOr<uint32_t> HSACodeObject::getNoteSectionIdx() const {
Expected<uint32_t> HSACodeObject::getNoteSectionIdx() const {
return getSectionIdxByName(".note");
}

ErrorOr<const HSACodeObject::Elf_Shdr *> HSACodeObject::getTextSection() const {
Expected<const HSACodeObject::Elf_Shdr *> HSACodeObject::getTextSection() const {
if (auto IdxOr = getTextSectionIdx())
return getELFFile()->getSection(*IdxOr);

return object_error::invalid_section_index;
return createError("invalid section index");
}

ErrorOr<const HSACodeObject::Elf_Shdr *> HSACodeObject::getNoteSection() const {
Expected<const HSACodeObject::Elf_Shdr *> HSACodeObject::getNoteSection() const {
if (auto IdxOr = getNoteSectionIdx())
return getELFFile()->getSection(*IdxOr);

return object_error::invalid_section_index;
return createError("invalid section index");
}

} // namespace llvm
} // namespace llvm
36 changes: 20 additions & 16 deletions lib/Target/AMDGPU/Disassembler/CodeObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,11 @@ struct ELFNote {
return sizeof(*this) + alignTo(namesz, ALIGN) + alignTo(descsz, ALIGN);
}

template <typename D> ErrorOr<const D*> as() const {
if (descsz < sizeof(D))
return object::object_error::parse_failed;
template <typename D> Expected<const D*> as() const {
if (descsz < sizeof(D)) {
return make_error<StringError>("invalid descsz",
object::object_error::parse_failed);
}

return reinterpret_cast<const D*>(getDesc().data());
}
Expand Down Expand Up @@ -111,9 +113,11 @@ class const_varsize_item_iterator :
return Ref.size() >= sizeof(Item) && Ref.size() >= getItemPadSize();
}

ErrorOr<const Item&> operator*() const {
if (!valid())
return object::object_error::parse_failed;
Expected<const Item&> operator*() const {
if (!valid()) {
return make_error<StringError>("invalid item",
object::object_error::parse_failed);
}

return *item();
}
Expand Down Expand Up @@ -143,10 +147,10 @@ class HSACodeObject;

class KernelSym : public object::ELF64LEObjectFile::Elf_Sym {
public:
ErrorOr<const amd_kernel_code_t *> getAmdKernelCodeT(
Expected<const amd_kernel_code_t *> getAmdKernelCodeT(
const HSACodeObject * CodeObject) const;

static ErrorOr<const KernelSym *> asKernelSym(
static Expected<const KernelSym *> asKernelSym(
const object::ELF64LEObjectFile::Elf_Sym *Sym);
};

Expand Down Expand Up @@ -221,17 +225,17 @@ class HSACodeObject : public object::ELF64LEObjectFile {
kernel_sym_iterator kernels_end() const;
iterator_range<kernel_sym_iterator> kernels() const;

ErrorOr<ArrayRef<uint8_t>> getKernelCode(const KernelSym *Kernel) const;
Expected<ArrayRef<uint8_t>> getKernelCode(const KernelSym *Kernel) const;

ErrorOr<const Elf_Shdr *> getSectionByName(StringRef Name) const;
Expected<const Elf_Shdr *> getSectionByName(StringRef Name) const;

ErrorOr<uint32_t> getSectionIdxByName(StringRef) const;
ErrorOr<uint32_t> getTextSectionIdx() const;
ErrorOr<uint32_t> getNoteSectionIdx() const;
ErrorOr<const Elf_Shdr *> getTextSection() const;
ErrorOr<const Elf_Shdr *> getNoteSection() const;
Expected<uint32_t> getSectionIdxByName(StringRef) const;
Expected<uint32_t> getTextSectionIdx() const;
Expected<uint32_t> getNoteSectionIdx() const;
Expected<const Elf_Shdr *> getTextSection() const;
Expected<const Elf_Shdr *> getNoteSection() const;
};

} // namespace llvm

#endif
#endif
28 changes: 16 additions & 12 deletions lib/Target/AMDGPU/Disassembler/CodeObjectDisassembler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,17 @@ CodeObjectDisassembler::CodeObjectDisassembler(MCContext *C,
: Ctx(C), TripleName(TN), InstPrinter(IP),
AsmStreamer(static_cast<AMDGPUTargetStreamer *>(TS)) {}

ErrorOr<CodeObjectDisassembler::SymbolsTy>
Expected<CodeObjectDisassembler::SymbolsTy>
CodeObjectDisassembler::CollectSymbols(const HSACodeObject *CodeObject) {
SymbolsTy Symbols;
for (const auto &Symbol : CodeObject->symbols()) {
auto AddressOr = Symbol.getAddress();
if (!AddressOr)
return object::object_error::parse_failed;
return AddressOr.takeError();

auto NameOr = Symbol.getName();
if (!NameOr)
return object::object_error::parse_failed;
return NameOr.takeError();
if (NameOr->empty())
continue;

Expand All @@ -64,15 +64,15 @@ CodeObjectDisassembler::CollectSymbols(const HSACodeObject *CodeObject) {
}

std::error_code CodeObjectDisassembler::printNotes(const HSACodeObject *CodeObject) {
for (const auto &Note: CodeObject->notes()) {
for (auto Note : CodeObject->notes()) {
if (!Note)
return Note.getError();
return errorToErrorCode(Note.takeError());

switch (Note->type) {
case NT_AMDGPU_HSA_CODE_OBJECT_VERSION: {
auto VersionOr = Note->as<amdgpu_hsa_code_object_version>();
if (!VersionOr)
return VersionOr.getError();
return errorToErrorCode(VersionOr.takeError());

auto *Version = *VersionOr;
AsmStreamer->EmitDirectiveHSACodeObjectVersion(
Expand All @@ -85,7 +85,7 @@ std::error_code CodeObjectDisassembler::printNotes(const HSACodeObject *CodeObje
case NT_AMDGPU_HSA_ISA: {
auto IsaOr = Note->as<amdgpu_hsa_isa>();
if (!IsaOr)
return IsaOr.getError();
return errorToErrorCode(IsaOr.takeError());

auto *Isa = *IsaOr;
AsmStreamer->EmitDirectiveHSACodeObjectISA(
Expand All @@ -103,7 +103,7 @@ std::error_code CodeObjectDisassembler::printNotes(const HSACodeObject *CodeObje
}

static std::string getCPUName(const HSACodeObject *CodeObject) {
for (const auto &Note : CodeObject->notes()) {
for (auto Note : CodeObject->notes()) {
if (!Note)
return "";

Expand All @@ -127,7 +127,7 @@ std::error_code CodeObjectDisassembler::printKernels(const HSACodeObject *CodeOb
// setup disassembler
auto SymbolsOr = CollectSymbols(CodeObject);
if (!SymbolsOr)
return SymbolsOr.getError();
return errorToErrorCode(SymbolsOr.takeError());

const auto &Target = getTheGCNTarget();
std::unique_ptr<MCSubtargetInfo> STI(
Expand All @@ -152,18 +152,22 @@ std::error_code CodeObjectDisassembler::printKernels(const HSACodeObject *CodeOb

// print kernels
for (const auto &Sym : CodeObject->kernels()) {
auto Kernel = KernelSym::asKernelSym(CodeObject->getSymbol(Sym.getRawDataRefImpl())).get();
auto ExpectedKernel = KernelSym::asKernelSym(CodeObject->getSymbol(Sym.getRawDataRefImpl()));
if (!ExpectedKernel)
return errorToErrorCode(ExpectedKernel.takeError());

auto NameEr = Sym.getName();
if (!NameEr)
return object::object_error::parse_failed;

auto Kernel = ExpectedKernel.get();
auto KernelCodeTOr = Kernel->getAmdKernelCodeT(CodeObject);
if (!KernelCodeTOr)
return KernelCodeTOr.getError();
return errorToErrorCode(KernelCodeTOr.takeError());

auto CodeOr = CodeObject->getKernelCode(Kernel);
if (!CodeOr)
return CodeOr.getError();
return errorToErrorCode(CodeOr.takeError());

AsmStreamer->EmitAMDGPUSymbolType(*NameEr, Kernel->getType());
AsmStreamer->getStreamer().EmitRawText("");
Expand Down
5 changes: 3 additions & 2 deletions lib/Target/AMDGPU/Disassembler/CodeObjectDisassembler.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "llvm/Support/MemoryBuffer.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/Object/ELFObjectFile.h"
#include <vector>

namespace llvm {
Expand All @@ -42,7 +43,7 @@ class CodeObjectDisassembler {

typedef std::vector<std::tuple<uint64_t, StringRef, uint8_t>> SymbolsTy;

ErrorOr<SymbolsTy> CollectSymbols(const HSACodeObject *CodeObject);
Expected<SymbolsTy> CollectSymbols(const HSACodeObject *CodeObject);

std::error_code printNotes(const HSACodeObject *CodeObject);
std::error_code printKernels(const HSACodeObject *CodeObject, raw_ostream &ES);
Expand All @@ -59,4 +60,4 @@ class CodeObjectDisassembler {
};
} // namespace llvm

#endif
#endif

0 comments on commit 047be2b

Please sign in to comment.