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 @@ -89,7 +89,7 @@ void emitFunctionEntryLabel() override; void emitDirectiveOptionArch(); - bool isSameAttribute(); + bool isSameSupportedAttribute(); private: void emitAttributes(); @@ -272,16 +272,25 @@ RTS.emitDirectiveOptionArch(NeedEmitStdOptionArgs); } -bool RISCVAsmPrinter::isSameAttribute() { +bool RISCVAsmPrinter::isSameSupportedAttribute() { const MCSubtargetInfo &MCSTI = *TM.getMCSubtargetInfo(); - return MCSTI.getFeatureBits() == STI->getFeatureBits(); + for (const auto &Feature : RISCVFeatureKV) { + if (STI->hasFeature(Feature.Value) == MCSTI.hasFeature(Feature.Value)) + continue; + + if (!llvm::RISCVISAInfo::isSupportedExtensionFeature(Feature.Key)) + continue; + + return false; + } + return true; } bool RISCVAsmPrinter::runOnMachineFunction(MachineFunction &MF) { STI = &MF.getSubtarget(); RISCVTargetStreamer &RTS = static_cast(*OutStreamer->getTargetStreamer()); - if (!isSameAttribute()) { + if (!isSameSupportedAttribute()) { RTS.emitDirectiveOptionPush(); emitDirectiveOptionArch(); } @@ -289,7 +298,7 @@ SetupMachineFunction(MF); emitFunctionBody(); - if (!isSameAttribute()) + if (!isSameSupportedAttribute()) RTS.emitDirectiveOptionPop(); return false; } diff --git a/llvm/test/CodeGen/RISCV/riscv-func-target-feature.ll b/llvm/test/CodeGen/RISCV/riscv-func-target-feature.ll --- a/llvm/test/CodeGen/RISCV/riscv-func-target-feature.ll +++ b/llvm/test/CodeGen/RISCV/riscv-func-target-feature.ll @@ -35,10 +35,10 @@ ret void } -; CHECK: .option push +; CHECK-NOT: .option push define void @test5() "target-features"="+unaligned-scalar-mem" { ; CHECK-LABEL: test5 -; CHECK: .option pop +; CHECK-NOT: .option pop entry: ret void }