[RISCV] Add an option to emit the Tag_RISCV_arch attribute based on the assembler's subtarget

This adds an option to emit the command line -mattr/-march into the
attributes of an object file. This can be useful to get objdump to
disassemble instructions that aren't in the base without forcing
users to add a .attribute to the assembly file.

The binutils assembler does this by default.

Similar option exists for ARM. I will wire it to a clang option in
another patch. Similar to https://reviews.llvm.org/D31813

Reviewed By: asb, kito-cheng

Differential Revision: https://reviews.llvm.org/D148782
This commit is contained in:
Craig Topper 2023-04-20 09:57:18 -07:00
parent 4a17ded97f
commit 328cfa840d
6 changed files with 39 additions and 4 deletions

View File

@ -35,6 +35,7 @@
#include "llvm/MC/MCValue.h"
#include "llvm/MC/TargetRegistry.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/RISCVAttributes.h"
#include "llvm/Support/RISCVISAInfo.h"
@ -48,6 +49,9 @@ using namespace llvm;
STATISTIC(RISCVNumInstrsCompressed,
"Number of RISC-V Compressed instructions emitted");
static cl::opt<bool> AddBuildAttributes("riscv-add-build-attributes",
cl::init(false));
namespace llvm {
extern const SubtargetFeatureKV RISCVFeatureKV[RISCV::NumSubtargetFeatures];
} // namespace llvm
@ -240,6 +244,8 @@ public:
RISCVAsmParser(const MCSubtargetInfo &STI, MCAsmParser &Parser,
const MCInstrInfo &MII, const MCTargetOptions &Options)
: MCTargetAsmParser(Options, STI, MII) {
MCAsmParserExtension::Initialize(Parser);
Parser.addAliasForDirective(".half", ".2byte");
Parser.addAliasForDirective(".hword", ".2byte");
Parser.addAliasForDirective(".word", ".4byte");
@ -265,6 +271,9 @@ public:
const MCObjectFileInfo *MOFI = Parser.getContext().getObjectFileInfo();
ParserOptions.IsPicEnabled = MOFI->isPositionIndependent();
if (AddBuildAttributes)
getTargetStreamer().emitTargetAttributes(STI, /*EmitStackAlign*/ false);
}
};

View File

@ -46,11 +46,13 @@ void RISCVTargetStreamer::setTargetABI(RISCVABI::ABI ABI) {
TargetABI = ABI;
}
void RISCVTargetStreamer::emitTargetAttributes(const MCSubtargetInfo &STI) {
void RISCVTargetStreamer::emitTargetAttributes(const MCSubtargetInfo &STI,
bool EmitStackAlign) {
if (STI.hasFeature(RISCV::FeatureRVE))
report_fatal_error("Codegen not yet implemented for RVE");
emitAttribute(RISCVAttrs::STACK_ALIGN, RISCVAttrs::ALIGN_16);
if (EmitStackAlign)
emitAttribute(RISCVAttrs::STACK_ALIGN, RISCVAttrs::ALIGN_16);
auto ParseResult = RISCVFeatures::parseFeatureBits(
STI.hasFeature(RISCV::Feature64Bit), STI.getFeatureBits());

View File

@ -40,7 +40,7 @@ public:
virtual void emitIntTextAttribute(unsigned Attribute, unsigned IntValue,
StringRef StringValue);
void emitTargetAttributes(const MCSubtargetInfo &STI);
void emitTargetAttributes(const MCSubtargetInfo &STI, bool EmitStackAlign);
void setTargetABI(RISCVABI::ABI ABI);
RISCVABI::ABI getTargetABI() const { return TargetABI; }
};

View File

@ -229,7 +229,7 @@ void RISCVAsmPrinter::emitAttributes() {
// Use MCSubtargetInfo from TargetMachine. Individual functions may have
// attributes that differ from other functions in the module and we have no
// way to know which function is correct.
RTS.emitTargetAttributes(*TM.getMCSubtargetInfo());
RTS.emitTargetAttributes(*TM.getMCSubtargetInfo(), /*EmitStackAlign*/ true);
}
void RISCVAsmPrinter::emitFunctionEntryLabel() {

View File

@ -2,6 +2,10 @@
# RUN: llvm-mc %s -triple=riscv32 -filetype=asm | FileCheck %s
# RUN: llvm-mc %s -triple=riscv64 -filetype=asm | FileCheck %s
# RUN: llvm-mc %s -triple=riscv32 -filetype=asm -riscv-add-build-attributes \
# RUN: | FileCheck %s
# RUN: llvm-mc %s -triple=riscv64 -filetype=asm -riscv-add-build-attributes \
# RUN: | FileCheck %s
.attribute stack_align, 16
# CHECK: attribute 4, 16

View File

@ -0,0 +1,20 @@
# RUN: llvm-mc %s -triple=riscv32 -filetype=asm -riscv-add-build-attributes \
# RUN: | FileCheck %s --check-prefixes=RV32
# RUN: llvm-mc %s -triple=riscv64 -filetype=asm -riscv-add-build-attributes \
# RUN: | FileCheck %s --check-prefixes=RV64
# RUN: llvm-mc %s -triple=riscv32 -filetype=asm -riscv-add-build-attributes \
# RUN: -mattr=+m | FileCheck %s --check-prefixes=RV32M
# RUN: llvm-mc %s -triple=riscv64 -filetype=asm -riscv-add-build-attributes \
# RUN: -mattr=+m | FileCheck %s --check-prefixes=RV64M
# RV32-NOT: attribute 4
# RV32: attribute 5, "rv32i2p1"
# RV64-NOT: attribute 4
# RV64: attribute 5, "rv64i2p1"
# RV32M-NOT: attribute 4
# RV32M: attribute 5, "rv32i2p1_m2p0"
# RV64M-NOT: attribute 4
# RV64M: attribute 5, "rv64i2p1_m2p0"