[flang-commits] [flang] [Flang][RISCV] Set vscale_range based off zvl*b (PR #77277)

Luke Lau via flang-commits flang-commits at lists.llvm.org
Thu Jan 11 00:45:53 PST 2024


================
@@ -712,13 +714,55 @@ void CodeGenAction::lowerHLFIRToFIR() {
 static std::optional<std::pair<unsigned, unsigned>>
 getVScaleRange(CompilerInstance &ci) {
   const auto &langOpts = ci.getInvocation().getLangOpts();
-  if (langOpts.VScaleMin || langOpts.VScaleMax)
-    return std::pair<unsigned, unsigned>(
-        langOpts.VScaleMin ? langOpts.VScaleMin : 1, langOpts.VScaleMax);
+  const auto targetOpts = ci.getInvocation().getTargetOpts();
+  const llvm::Triple triple(targetOpts.triple);
+
+  if (triple.isAArch64()) {
+    if (langOpts.VScaleMin || langOpts.VScaleMax)
+      return std::pair<unsigned, unsigned>(
+          langOpts.VScaleMin ? langOpts.VScaleMin : 1, langOpts.VScaleMax);
+
+    std::string featuresStr = ci.getTargetFeatures();
+    if (featuresStr.find("+sve") != std::string::npos)
+      return std::pair<unsigned, unsigned>(1, 16);
+  } else if (triple.isRISCV()) {
+    auto parseResult = llvm::RISCVISAInfo::parseFeatures(
+        triple.isRISCV64() ? 64 : 32, targetOpts.featuresAsWritten);
+    if (!parseResult) {
+      std::string buffer;
+      llvm::raw_string_ostream outputErrMsg(buffer);
+      handleAllErrors(parseResult.takeError(), [&](llvm::StringError &errMsg) {
+        outputErrMsg << errMsg.getMessage();
+      });
+      ci.getDiagnostics().Report(clang::diag::err_invalid_feature_combination)
+          << outputErrMsg.str();
+      return std::nullopt;
+    }
+
+    llvm::RISCVISAInfo *const isaInfo = parseResult->get();
 
-  std::string featuresStr = ci.getTargetFeatures();
-  if (featuresStr.find("+sve") != std::string::npos)
-    return std::pair<unsigned, unsigned>(1, 16);
+    // RISCV::RVVBitsPerBlock is 64.
+    unsigned vscaleMin = isaInfo->getMinVLen() / llvm::RISCV::RVVBitsPerBlock;
+
+    if (langOpts.VScaleMin || langOpts.VScaleMax) {
+      // Treat Zvl*b as a lower bound on vscale.
+      vscaleMin = std::max(vscaleMin, langOpts.VScaleMin);
+      unsigned vscaleMax = langOpts.VScaleMax;
+      if (vscaleMax != 0 && vscaleMax < vscaleMin)
----------------
lukel97 wrote:

Agreed. I also thought it's strange that when `-mvscale-min` is used, we put a bound on it with `Zvl*b`, but we don't cap `-mvscale-max` with `RISCVISAInfo::getMaxVLen()`. But also for another patch.

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


More information about the flang-commits mailing list