Skip to content

[Hexagon] ELF attributes for Hexagon #85359

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions clang/lib/Driver/ToolChains/Clang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8481,6 +8481,14 @@ void ClangAs::ConstructJob(Compilation &C, const JobAction &JA,
case llvm::Triple::riscv64:
AddRISCVTargetArgs(Args, CmdArgs);
break;

case llvm::Triple::hexagon:
if (Args.hasFlag(options::OPT_mdefault_build_attributes,
options::OPT_mno_default_build_attributes, true)) {
CmdArgs.push_back("-mllvm");
CmdArgs.push_back("-hexagon-add-build-attributes");
}
break;
}

// Consume all the warning flags. Usually this would be handled more
Expand Down
21 changes: 21 additions & 0 deletions clang/test/Driver/hexagon-default-build-attributes.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/// Enabled by default for assembly
// RUN: %clang --target=hexagon-unknown-elf -### %s 2>&1 \
// RUN: | FileCheck %s -check-prefix CHECK-ENABLED

/// Can be forced on or off for assembly.
// RUN: %clang --target=hexagon-unknown-elf -### %s 2>&1 -mno-default-build-attributes \
// RUN: | FileCheck %s -check-prefix CHECK-DISABLED
// RUN: %clang --target=hexagon-unknown-elf -### %s 2>&1 -mdefault-build-attributes \
// RUN: | FileCheck %s -check-prefix CHECK-ENABLED

/// Option ignored C/C++ (since we always emit hardware and ABI build attributes
/// during codegen).
// RUN: %clang --target=hexagon-unknown-elf -### -x c %s -mdefault-build-attributes 2>&1 \
// RUN: | FileCheck %s -check-prefix CHECK-DISABLED-C
// RUN: %clang --target=hexagon-unknown-elf -### -x c++ %s -mdefault-build-attributes 2>&1 \
// RUN: | FileCheck %s -check-prefix CHECK-DISABLED-C

// CHECK-DISABLED-NOT: "-hexagon-add-build-attributes"
// CHECK-DISABLED-C-NOT: "-hexagon-add-build-attributes"
// CHECK-ENABLED: "-hexagon-add-build-attributes"
// CHECK-DISABLED-C: argument unused during compilation: '-mdefault-build-attributes'
2 changes: 2 additions & 0 deletions llvm/include/llvm/BinaryFormat/ELF.h
Original file line number Diff line number Diff line change
Expand Up @@ -1141,6 +1141,8 @@ enum : unsigned {

SHT_CSKY_ATTRIBUTES = 0x70000001U,

SHT_HEXAGON_ATTRIBUTES = 0x70000003U,

SHT_HIPROC = 0x7fffffff, // Highest processor arch-specific type.
SHT_LOUSER = 0x80000000, // Lowest type reserved for applications.
SHT_HIUSER = 0xffffffff // Highest type reserved for applications.
Expand Down
4 changes: 4 additions & 0 deletions llvm/include/llvm/Object/ELFObjectFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class ELFObjectFileBase : public ObjectFile {

SubtargetFeatures getMIPSFeatures() const;
SubtargetFeatures getARMFeatures() const;
SubtargetFeatures getHexagonFeatures() const;
Expected<SubtargetFeatures> getRISCVFeatures() const;
SubtargetFeatures getLoongArchFeatures() const;

Expand Down Expand Up @@ -397,6 +398,9 @@ template <class ELFT> class ELFObjectFile : public ELFObjectFileBase {
case ELF::EM_RISCV:
Type = ELF::SHT_RISCV_ATTRIBUTES;
break;
case ELF::EM_HEXAGON:
Type = ELF::SHT_HEXAGON_ATTRIBUTES;
break;
default:
return Error::success();
}
Expand Down
37 changes: 37 additions & 0 deletions llvm/include/llvm/Support/HexagonAttributeParser.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//===-- HexagonAttributeParser.h - Hexagon Attribute Parser -----*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_SUPPORT_HEXAGONATTRIBUTEPARSER_H
#define LLVM_SUPPORT_HEXAGONATTRIBUTEPARSER_H

#include "llvm/Support/ELFAttributeParser.h"
#include "llvm/Support/HexagonAttributes.h"

namespace llvm {
class HexagonAttributeParser : public ELFAttributeParser {
struct DisplayHandler {
HexagonAttrs::AttrType Attribute;
Error (HexagonAttributeParser::*Routine)(unsigned);
};

static const DisplayHandler DisplayRoutines[];

Error handler(uint64_t Tag, bool &Handled) override;

public:
HexagonAttributeParser(ScopedPrinter *SP)
: ELFAttributeParser(SP, HexagonAttrs::getHexagonAttributeTags(),
"hexagon") {}
HexagonAttributeParser()
: ELFAttributeParser(HexagonAttrs::getHexagonAttributeTags(), "hexagon") {
}
};

} // namespace llvm

#endif
32 changes: 32 additions & 0 deletions llvm/include/llvm/Support/HexagonAttributes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//===-- HexagonAttributes.h - Qualcomm Hexagon Attributes -----------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_SUPPORT_HEXAGONATTRIBUTES_H
#define LLVM_SUPPORT_HEXAGONATTRIBUTES_H

#include "llvm/Support/ELFAttributes.h"

namespace llvm {
namespace HexagonAttrs {

const TagNameMap &getHexagonAttributeTags();

enum AttrType : unsigned {
ARCH = 4,
HVXARCH = 5,
HVXIEEEFP = 6,
HVXQFLOAT = 7,
ZREG = 8,
AUDIO = 9,
CABAC = 10
};

} // namespace HexagonAttrs
} // namespace llvm

#endif
5 changes: 4 additions & 1 deletion llvm/lib/Object/ELF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,10 @@ StringRef llvm::object::getELFSectionTypeName(uint32_t Machine, unsigned Type) {
}
break;
case ELF::EM_HEXAGON:
switch (Type) { STRINGIFY_ENUM_CASE(ELF, SHT_HEX_ORDERED); }
switch (Type) {
STRINGIFY_ENUM_CASE(ELF, SHT_HEX_ORDERED);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a test to llvm/test/tools/llvm-readobj/ELF/machine-specific-section-types.test

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added

STRINGIFY_ENUM_CASE(ELF, SHT_HEXAGON_ATTRIBUTES);
}
break;
case ELF::EM_X86_64:
switch (Type) { STRINGIFY_ENUM_CASE(ELF, SHT_X86_64_UNWIND); }
Expand Down
78 changes: 78 additions & 0 deletions llvm/lib/Object/ELFObjectFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "llvm/Support/ARMAttributeParser.h"
#include "llvm/Support/ARMBuildAttributes.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/HexagonAttributeParser.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/RISCVAttributeParser.h"
#include "llvm/Support/RISCVAttributes.h"
Expand Down Expand Up @@ -287,6 +288,81 @@ SubtargetFeatures ELFObjectFileBase::getARMFeatures() const {
return Features;
}

static std::optional<std::string> hexagonAttrToFeatureString(unsigned Attr) {
switch (Attr) {
case 5:
return "v5";
case 55:
return "v55";
case 60:
return "v60";
case 62:
return "v62";
case 65:
return "v65";
case 67:
return "v67";
case 68:
return "v68";
case 69:
return "v69";
case 71:
return "v71";
case 73:
return "v73";
default:
return {};
}
}

SubtargetFeatures ELFObjectFileBase::getHexagonFeatures() const {
SubtargetFeatures Features;
HexagonAttributeParser Parser;
if (Error E = getBuildAttributes(Parser)) {
// Return no attributes if none can be read.
// This behavior is important for backwards compatibility.
consumeError(std::move(E));
return Features;
}
std::optional<unsigned> Attr;

if ((Attr = Parser.getAttributeValue(HexagonAttrs::ARCH))) {
if (std::optional<std::string> FeatureString =
hexagonAttrToFeatureString(*Attr))
Features.AddFeature(*FeatureString);
}

if ((Attr = Parser.getAttributeValue(HexagonAttrs::HVXARCH))) {
std::optional<std::string> FeatureString =
hexagonAttrToFeatureString(*Attr);
// There is no corresponding hvx arch for v5 and v55.
if (FeatureString && *Attr >= 60)
Features.AddFeature("hvx" + *FeatureString);
}

if ((Attr = Parser.getAttributeValue(HexagonAttrs::HVXIEEEFP)))
if (*Attr)
Features.AddFeature("hvx-ieee-fp");

if ((Attr = Parser.getAttributeValue(HexagonAttrs::HVXQFLOAT)))
if (*Attr)
Features.AddFeature("hvx-qfloat");

if ((Attr = Parser.getAttributeValue(HexagonAttrs::ZREG)))
if (*Attr)
Features.AddFeature("zreg");

if ((Attr = Parser.getAttributeValue(HexagonAttrs::AUDIO)))
if (*Attr)
Features.AddFeature("audio");

if ((Attr = Parser.getAttributeValue(HexagonAttrs::CABAC)))
if (*Attr)
Features.AddFeature("cabac");

return Features;
}

Expected<SubtargetFeatures> ELFObjectFileBase::getRISCVFeatures() const {
SubtargetFeatures Features;
unsigned PlatformFlags = getPlatformFlags();
Expand Down Expand Up @@ -349,6 +425,8 @@ Expected<SubtargetFeatures> ELFObjectFileBase::getFeatures() const {
return getRISCVFeatures();
case ELF::EM_LOONGARCH:
return getLoongArchFeatures();
case ELF::EM_HEXAGON:
return getHexagonFeatures();
default:
return SubtargetFeatures();
}
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/ObjectYAML/ELFYAML.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,7 @@ void ScalarEnumerationTraits<ELFYAML::ELF_SHT>::enumeration(
break;
case ELF::EM_HEXAGON:
ECase(SHT_HEX_ORDERED);
ECase(SHT_HEXAGON_ATTRIBUTES);
break;
case ELF::EM_X86_64:
ECase(SHT_X86_64_UNWIND);
Expand Down
2 changes: 2 additions & 0 deletions llvm/lib/Support/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ add_llvm_component_library(LLVMSupport
GlobPattern.cpp
GraphWriter.cpp
Hashing.cpp
HexagonAttributeParser.cpp
HexagonAttributes.cpp
InitLLVM.cpp
InstructionCost.cpp
IntEqClasses.cpp
Expand Down
55 changes: 55 additions & 0 deletions llvm/lib/Support/HexagonAttributeParser.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//===-- HexagonAttributeParser.cpp - Hexagon Attribute Parser -------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "llvm/Support/HexagonAttributeParser.h"

using namespace llvm;

const HexagonAttributeParser::DisplayHandler
HexagonAttributeParser::DisplayRoutines[] = {
{
HexagonAttrs::ARCH,
&ELFAttributeParser::integerAttribute,
},
{
HexagonAttrs::HVXARCH,
&ELFAttributeParser::integerAttribute,
},
{
HexagonAttrs::HVXIEEEFP,
&ELFAttributeParser::integerAttribute,
},
{
HexagonAttrs::HVXQFLOAT,
&ELFAttributeParser::integerAttribute,
},
{
HexagonAttrs::ZREG,
&ELFAttributeParser::integerAttribute,
},
{
HexagonAttrs::AUDIO,
&ELFAttributeParser::integerAttribute,
},
{
HexagonAttrs::CABAC,
&ELFAttributeParser::integerAttribute,
}};

Error HexagonAttributeParser::handler(uint64_t Tag, bool &Handled) {
Handled = false;
for (const auto &R : DisplayRoutines) {
if (uint64_t(R.Attribute) == Tag) {
if (Error E = (this->*R.Routine)(Tag))
return E;
Handled = true;
break;
}
}
return Error::success();
}
27 changes: 27 additions & 0 deletions llvm/lib/Support/HexagonAttributes.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//===-- HexagonAttributes.cpp - Qualcomm Hexagon Attributes ---------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "llvm/Support/HexagonAttributes.h"

using namespace llvm;
using namespace llvm::HexagonAttrs;

static constexpr TagNameItem TagData[] = {
{ARCH, "Tag_arch"},
{HVXARCH, "Tag_hvx_arch"},
{HVXIEEEFP, "Tag_hvx_ieeefp"},
{HVXQFLOAT, "Tag_hvx_qfloat"},
{ZREG, "Tag_zreg"},
{AUDIO, "Tag_audio"},
{CABAC, "Tag_cabac"},
};

constexpr TagNameMap HexagonAttributeTags{TagData};
const TagNameMap &llvm::HexagonAttrs::getHexagonAttributeTags() {
return HexagonAttributeTags;
}
Loading