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 @@ -70,6 +70,7 @@ private: void emitAttributes(); + void emitDirectiveOption(); }; } @@ -178,8 +179,10 @@ } void RISCVAsmPrinter::emitStartOfAsmFile(Module &M) { - if (TM.getTargetTriple().isOSBinFormatELF()) - emitAttributes(); + if (!TM.getTargetTriple().isOSBinFormatELF()) + return; + emitAttributes(); + emitDirectiveOption(); } void RISCVAsmPrinter::emitEndOfAsmFile(Module &M) { @@ -196,6 +199,20 @@ RTS.emitTargetAttributes(*STI); } +void RISCVAsmPrinter::emitDirectiveOption() { + RISCVTargetStreamer &RTS = + static_cast(*OutStreamer->getTargetStreamer()); + + if (!isPositionIndependent()) + RTS.emitDirectiveOptionNoPIC(); + + if (!STI->hasFeature(RISCV::FeatureRelax)) + RTS.emitDirectiveOptionNoRelax(); + + if (!STI->hasFeature(RISCV::FeatureStdExtC)) + RTS.emitDirectiveOptionNoRVC(); +} + // Force static initialization. extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeRISCVAsmPrinter() { RegisterAsmPrinter X(getTheRISCV32Target()); diff --git a/llvm/test/CodeGen/RISCV/option-directive.ll b/llvm/test/CodeGen/RISCV/option-directive.ll new file mode 100644 --- /dev/null +++ b/llvm/test/CodeGen/RISCV/option-directive.ll @@ -0,0 +1,18 @@ +; RUN: llc -mtriple=riscv32 -mattr=-relax %s -o - | FileCheck --check-prefix=RV32-NO-RELAX %s +; RUN: llc -mtriple=riscv32 -mattr=-c %s -o - | FileCheck --check-prefix=RV32-NO-C-EXTENSION %s +; RUN: llc -mtriple=riscv32 %s -o - | FileCheck --check-prefix=RV32-NON-POSITION-INDEPENDENT %s +; RUN: llc -mtriple=riscv64 -mattr=-relax %s -o - | FileCheck --check-prefix=RV64-NO-RELAX %s +; RUN: llc -mtriple=riscv64 -mattr=-c %s -o - | FileCheck --check-prefix=RV64-NO-C-EXTENSION %s +; RUN: llc -mtriple=riscv64 %s -o - | FileCheck --check-prefix=RV64-NON-POSITION-INDEPENDENT %s + +; RV32-NO-RELAX: .option norelax +; RV32-NO-C-EXTENSION: .option norvc +; RV32-NON-POSITION-INDEPENDENT: .option nopic +; RV64-NO-RELAX: .option norelax +; RV64-NO-C-EXTENSION: .option norvc +; RV64-NON-POSITION-INDEPENDENT: .option nopic + +define i32 @addi(i32 %a) { + %1 = add i32 %a, 1 + ret i32 %1 +}