[clang] [llvm] [RISCV] Update TargetAttr target-cpu override rule (PR #75804)

Craig Topper via cfe-commits cfe-commits at lists.llvm.org
Wed Jan 17 15:56:04 PST 2024


================
@@ -482,5 +482,35 @@ ParsedTargetAttr RISCVTargetInfo::parseTargetAttr(StringRef Features) const {
       Ret.Tune = AttrString;
     }
   }
+
+  StringRef MCPU = this->getTargetOpts().CPU;
+  StringRef MTune = this->getTargetOpts().TuneCPU;
+
+  // attr-cpu override march only if arch isn't present.
+  if (FoundArch) {
+    // If tune-cpu infer from CPU, then try to keep it.
+    // Otherwise, just use current tune option.
+    if (Ret.Tune.empty() && MTune.empty()) {
+      if (!Ret.CPU.empty())
+        Ret.Tune = Ret.CPU; // Keep attr-cpu in tune-cpu
+      else if (!MCPU.empty())
+        Ret.Tune = MCPU; // Keep mcpu in tune-cpu
+    }
+
+    // Reassign mcpu due to attr-arch=<Adding-Extension> need
+    // target-feature from mcpu/march.
+    // Use attr-cpu will affect target-feature.
+    Ret.CPU = MCPU;
+
+    // arch=<full-arch-string> need keep target feature clean,
+    // use the baseline cpu.
+    if (llvm::find(Ret.Features, "__RISCV_TargetAttrNeedOverride") !=
+        Ret.Features.end())
+      Ret.CPU =
----------------
topperc wrote:

`RISCVProcessorModel` defines names that can be used for -mcpu and -mtune. `RISCVTuneProcessorModel` defines name that can only be used for -mtune. These just associate scheduler models with strings. You need to look at C++ code to see how -mcpu and -mtune are propagated.

This code gets the TuneCPU from the tune-cpu function attribute if present. Otherwise its the same as the CPU. Where CPU either comes from the target-cpu attribute or the TargetCPU in TargetMachine. There is no TuneCPU in TargetMachine so it can only be set differently through the tune-cpu attribute.

```
const RISCVSubtarget *                                                           
RISCVTargetMachine::getSubtargetImpl(const Function &F) const {                  
  Attribute CPUAttr = F.getFnAttribute("target-cpu");                            
  Attribute TuneAttr = F.getFnAttribute("tune-cpu");                             
  Attribute FSAttr = F.getFnAttribute("target-features");                        
                                                                                 
  std::string CPU =                                                              
      CPUAttr.isValid() ? CPUAttr.getValueAsString().str() : TargetCPU;          
  std::string TuneCPU =                                                          
      TuneAttr.isValid() ? TuneAttr.getValueAsString().str() : CPU;
```

The lookup function for the scheduler is here
```
void MCSubtargetInfo::InitMCProcessorInfo(StringRef CPU, StringRef TuneCPU,      
                                          StringRef FS) {                        
  FeatureBits = getFeatures(CPU, TuneCPU, FS, ProcDesc, ProcFeatures);           
  FeatureString = std::string(FS);                                               
                                                                                 
  if (!TuneCPU.empty())                                                          
    CPUSchedModel = &getSchedModelForCPU(TuneCPU);                               
  else                                                                           
    CPUSchedModel = &MCSchedModel::Default;                                      
} 
```

https://github.com/llvm/llvm-project/pull/75804


More information about the cfe-commits mailing list