diff --git a/llvm/lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp b/llvm/lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp --- a/llvm/lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp +++ b/llvm/lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp @@ -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 @@ STATISTIC(RISCVNumInstrsCompressed, "Number of RISC-V Compressed instructions emitted"); +static cl::opt AddBuildAttributes("riscv-add-build-attributes", + cl::init(false)); + namespace llvm { extern const SubtargetFeatureKV RISCVFeatureKV[RISCV::NumSubtargetFeatures]; } // namespace llvm @@ -240,6 +244,8 @@ 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 @@ const MCObjectFileInfo *MOFI = Parser.getContext().getObjectFileInfo(); ParserOptions.IsPicEnabled = MOFI->isPositionIndependent(); + + if (AddBuildAttributes) + getTargetStreamer().emitTargetAttributes(STI, /*EmitStackAlign*/ false); } }; diff --git a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVTargetStreamer.h b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVTargetStreamer.h --- a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVTargetStreamer.h +++ b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVTargetStreamer.h @@ -40,7 +40,7 @@ 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; } }; diff --git a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVTargetStreamer.cpp b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVTargetStreamer.cpp --- a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVTargetStreamer.cpp +++ b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVTargetStreamer.cpp @@ -46,11 +46,13 @@ 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()); diff --git a/llvm/lib/Target/RISCV/RISCVAsmPrinter.cpp b/llvm/lib/Target/RISCV/RISCVAsmPrinter.cpp --- a/llvm/lib/Target/RISCV/RISCVAsmPrinter.cpp +++ b/llvm/lib/Target/RISCV/RISCVAsmPrinter.cpp @@ -229,7 +229,7 @@ // 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() { diff --git a/llvm/test/MC/RISCV/attribute.s b/llvm/test/MC/RISCV/attribute.s --- a/llvm/test/MC/RISCV/attribute.s +++ b/llvm/test/MC/RISCV/attribute.s @@ -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 diff --git a/llvm/test/MC/RISCV/default-build-attributes.s b/llvm/test/MC/RISCV/default-build-attributes.s new file mode 100644 --- /dev/null +++ b/llvm/test/MC/RISCV/default-build-attributes.s @@ -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"