@@ -100,7 +100,7 @@ class RISCVAsmPrinter : public AsmPrinter {
100100  bool  emitDirectiveOptionArch ();
101101
102102private: 
103-   void  emitAttributes ();
103+   void  emitAttributes (const  MCSubtargetInfo &SubtargetInfo );
104104
105105  void  emitNTLHint (const  MachineInstr *MI);
106106
@@ -385,8 +385,32 @@ void RISCVAsmPrinter::emitStartOfAsmFile(Module &M) {
385385  if  (const  MDString *ModuleTargetABI =
386386          dyn_cast_or_null<MDString>(M.getModuleFlag (" target-abi" 
387387    RTS.setTargetABI (RISCVABI::getTargetABI (ModuleTargetABI->getString ()));
388+ 
389+   MCSubtargetInfo SubtargetInfo = *TM.getMCSubtargetInfo ();
390+ 
391+   //  Use module flag to update feature bits.
392+   if  (auto  *MD = dyn_cast_or_null<MDNode>(M.getModuleFlag (" riscv-isa" 
393+     for  (auto  &ISA : MD->operands ()) {
394+       if  (auto  *ISAString = dyn_cast_or_null<MDString>(ISA)) {
395+         auto  ParseResult = llvm::RISCVISAInfo::parseArchString (
396+             ISAString->getString (), /* EnableExperimentalExtension=*/ true ,
397+             /* ExperimentalExtensionVersionCheck=*/ true );
398+         if  (!errorToBool (ParseResult.takeError ())) {
399+           auto  &ISAInfo = *ParseResult;
400+           for  (const  auto  &Feature : RISCVFeatureKV) {
401+             if  (ISAInfo->hasExtension (Feature.Key ) &&
402+                 !SubtargetInfo.hasFeature (Feature.Value ))
403+               SubtargetInfo.ToggleFeature (Feature.Key );
404+           }
405+         }
406+       }
407+     }
408+ 
409+     RTS.setFlagsFromFeatures (SubtargetInfo);
410+   }
411+ 
388412  if  (TM.getTargetTriple ().isOSBinFormatELF ())
389-     emitAttributes ();
413+     emitAttributes (SubtargetInfo );
390414}
391415
392416void  RISCVAsmPrinter::emitEndOfAsmFile (Module &M) {
@@ -398,13 +422,13 @@ void RISCVAsmPrinter::emitEndOfAsmFile(Module &M) {
398422  EmitHwasanMemaccessSymbols (M);
399423}
400424
401- void  RISCVAsmPrinter::emitAttributes () {
425+ void  RISCVAsmPrinter::emitAttributes (const  MCSubtargetInfo &SubtargetInfo ) {
402426  RISCVTargetStreamer &RTS =
403427      static_cast <RISCVTargetStreamer &>(*OutStreamer->getTargetStreamer ());
404428  //  Use MCSubtargetInfo from TargetMachine. Individual functions may have
405429  //  attributes that differ from other functions in the module and we have no
406430  //  way to know which function is correct.
407-   RTS.emitTargetAttributes (*TM. getMCSubtargetInfo () , /* EmitStackAlign*/ true );
431+   RTS.emitTargetAttributes (SubtargetInfo , /* EmitStackAlign*/ true );
408432}
409433
410434void  RISCVAsmPrinter::emitFunctionEntryLabel () {
0 commit comments