Skip to content

Commit

Permalink
[SPARC] Add v8plus feature bit
Browse files Browse the repository at this point in the history
Implement handling for `v8plus` feature bit to allow the user to switch
between V8 and V8+ mode with 32-bit code.

This is done as a prerequisite for `-mv8plus` flag on clang (#98713).
  • Loading branch information
koachan committed Jul 31, 2024
1 parent 0628446 commit ec0450f
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 12 deletions.
4 changes: 3 additions & 1 deletion llvm/lib/Target/Sparc/MCTargetDesc/SparcAsmBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ namespace {
class SparcAsmBackend : public MCAsmBackend {
protected:
bool Is64Bit;
bool IsV8Plus;
bool HasV9;

public:
Expand All @@ -140,6 +141,7 @@ namespace {
? llvm::endianness::little
: llvm::endianness::big),
Is64Bit(STI.getTargetTriple().isArch64Bit()),
IsV8Plus(STI.hasFeature(Sparc::FeatureV8Plus)),
HasV9(STI.hasFeature(Sparc::FeatureV9)) {}

unsigned getNumFixupKinds() const override {
Expand Down Expand Up @@ -359,7 +361,7 @@ namespace {
std::unique_ptr<MCObjectTargetWriter>
createObjectTargetWriter() const override {
uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(OSType);
return createSparcELFObjectWriter(Is64Bit, HasV9, OSABI);
return createSparcELFObjectWriter(Is64Bit, IsV8Plus, HasV9, OSABI);
}
};

Expand Down
17 changes: 12 additions & 5 deletions llvm/lib/Target/Sparc/MCTargetDesc/SparcELFObjectWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,16 @@ using namespace llvm;
namespace {
class SparcELFObjectWriter : public MCELFObjectTargetWriter {
public:
SparcELFObjectWriter(bool Is64Bit, bool HasV9, uint8_t OSABI)
SparcELFObjectWriter(bool Is64Bit, bool IsV8Plus, bool HasV9, uint8_t OSABI)
: MCELFObjectTargetWriter(
Is64Bit, OSABI,
Is64Bit ? ELF::EM_SPARCV9
: (HasV9 ? ELF::EM_SPARC32PLUS : ELF::EM_SPARC),
Is64Bit
? ELF::EM_SPARCV9
// Note that we still need to emit an EM_SPARC32PLUS object
// even when V8+ isn't explicitly requested, if we're
// targeting a V9-capable CPU. This matches GAS behavior upon
// encountering any V9 instructions in its input.
: ((IsV8Plus || HasV9) ? ELF::EM_SPARC32PLUS : ELF::EM_SPARC),
/*HasRelocationAddend*/ true) {}

~SparcELFObjectWriter() override = default;
Expand Down Expand Up @@ -148,6 +153,8 @@ bool SparcELFObjectWriter::needsRelocateWithSymbol(const MCValue &,
}

std::unique_ptr<MCObjectTargetWriter>
llvm::createSparcELFObjectWriter(bool Is64Bit, bool HasV9, uint8_t OSABI) {
return std::make_unique<SparcELFObjectWriter>(Is64Bit, HasV9, OSABI);
llvm::createSparcELFObjectWriter(bool Is64Bit, bool IsV8Plus, bool HasV9,
uint8_t OSABI) {
return std::make_unique<SparcELFObjectWriter>(Is64Bit, IsV8Plus, HasV9,
OSABI);
}
6 changes: 4 additions & 2 deletions llvm/lib/Target/Sparc/MCTargetDesc/SparcMCTargetDesc.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ MCCodeEmitter *createSparcMCCodeEmitter(const MCInstrInfo &MCII,
MCAsmBackend *createSparcAsmBackend(const Target &T, const MCSubtargetInfo &STI,
const MCRegisterInfo &MRI,
const MCTargetOptions &Options);
std::unique_ptr<MCObjectTargetWriter>
createSparcELFObjectWriter(bool Is64Bit, bool HasV9, uint8_t OSABI);
std::unique_ptr<MCObjectTargetWriter> createSparcELFObjectWriter(bool Is64Bit,
bool IsV8Plus,
bool HasV9,
uint8_t OSABI);

// Defines symbolic names for Sparc v9 ASI tag names.
namespace SparcASITag {
Expand Down
3 changes: 3 additions & 0 deletions llvm/lib/Target/Sparc/Sparc.td
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ def FeatureNoFMULS
def FeatureV9
: SubtargetFeature<"v9", "IsV9", "true",
"Enable SPARC-V9 instructions">;
def FeatureV8Plus
: SubtargetFeature<"v8plus", "IsV8Plus", "true",
"Enable V8+ mode, allowing use of 64-bit V9 instructions in 32-bit code">;
def FeatureV8Deprecated
: SubtargetFeature<"deprecated-v8", "UseV8DeprecatedInsts", "true",
"Enable deprecated V8 instructions in V9 mode">;
Expand Down
9 changes: 5 additions & 4 deletions llvm/test/MC/Sparc/elf-sparc-machine-type.s
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
## Emit correct machine type depending on triple and cpu options.
## - `-triple sparc` emits an object of type EM_SPARC;
## - `-triple sparc -mcpu=v9` emits EM_SPARC32PLUS; and
## - `-triple sparc -mcpu=v9` or `-triple sparc -mattr=+v8plus` emits EM_SPARC32PLUS; and
## - `-triple sparcv9` emits EM_SPARCV9.

# RUN: llvm-mc -filetype=obj -triple sparc %s -o - | llvm-readobj -h - | FileCheck --check-prefixes=SPARC %s
# RUN: llvm-mc -filetype=obj -triple sparc -mcpu=v9 %s -o - | llvm-readobj -h - | FileCheck --check-prefixes=SPARC32PLUS %s
# RUN: llvm-mc -filetype=obj -triple sparcv9 %s -o - | llvm-readobj -h - | FileCheck --check-prefixes=SPARCV9 %s
# RUN: llvm-mc -filetype=obj -triple sparc %s -o - | llvm-readobj -h - | FileCheck --check-prefixes=SPARC %s
# RUN: llvm-mc -filetype=obj -triple sparc -mcpu=v9 %s -o - | llvm-readobj -h - | FileCheck --check-prefixes=SPARC32PLUS %s
# RUN: llvm-mc -filetype=obj -triple sparc -mattr=+v8plus %s -o - | llvm-readobj -h - | FileCheck --check-prefixes=SPARC32PLUS %s
# RUN: llvm-mc -filetype=obj -triple sparcv9 %s -o - | llvm-readobj -h - | FileCheck --check-prefixes=SPARCV9 %s

# SPARC: Machine: EM_SPARC (0x2)
# SPARC32PLUS: Machine: EM_SPARC32PLUS (0x12)
Expand Down

0 comments on commit ec0450f

Please sign in to comment.