diff --git a/clang/lib/Basic/CMakeLists.txt b/clang/lib/Basic/CMakeLists.txt index 2e218ba7c84cc..8ab960c7212f8 100644 --- a/clang/lib/Basic/CMakeLists.txt +++ b/clang/lib/Basic/CMakeLists.txt @@ -2,6 +2,7 @@ set(LLVM_LINK_COMPONENTS Support TargetParser FrontendOpenMP + FrontendDriver ) find_first_existing_vc_file("${LLVM_MAIN_SRC_DIR}" llvm_vc) diff --git a/clang/lib/Basic/Targets/RISCV.cpp b/clang/lib/Basic/Targets/RISCV.cpp index 6bc57a83a2d5a..b090a9b167a20 100644 --- a/clang/lib/Basic/Targets/RISCV.cpp +++ b/clang/lib/Basic/Targets/RISCV.cpp @@ -15,6 +15,7 @@ #include "clang/Basic/MacroBuilder.h" #include "clang/Basic/TargetBuiltins.h" #include "llvm/ADT/StringSwitch.h" +#include "llvm/Frontend/Driver/RISCV.h" #include "llvm/Support/raw_ostream.h" #include "llvm/TargetParser/RISCVTargetParser.h" #include @@ -321,24 +322,8 @@ bool RISCVTargetInfo::initFeatureMap( std::optional> RISCVTargetInfo::getVScaleRange(const LangOptions &LangOpts) const { - // 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) - VScaleMax = VScaleMin; - return std::pair(VScaleMin ? VScaleMin : 1, VScaleMax); - } - - if (VScaleMin > 0) { - unsigned VScaleMax = ISAInfo->getMaxVLen() / llvm::RISCV::RVVBitsPerBlock; - return std::make_pair(VScaleMin, VScaleMax); - } - - return std::nullopt; + return llvm::driver::riscv::getVScaleRange(*ISAInfo, LangOpts.VScaleMin, + LangOpts.VScaleMax); } /// Return true if has this feature, need to sync with handleTargetFeatures. diff --git a/llvm/include/llvm/Frontend/Driver/RISCV.h b/llvm/include/llvm/Frontend/Driver/RISCV.h new file mode 100644 index 0000000000000..1f81f089087b5 --- /dev/null +++ b/llvm/include/llvm/Frontend/Driver/RISCV.h @@ -0,0 +1,27 @@ +//===--- RISCV.h ------------------------------------------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// This file defines RISC-V frontend logic common to clang and flang +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_FRONTEND_DRIVER_RISCV_H +#define LLVM_FRONTEND_DRIVER_RISCV_H + +#include "llvm/Support/RISCVISAInfo.h" +#include + +namespace llvm::driver::riscv { + +std::optional> +getVScaleRange(const RISCVISAInfo &ISAInfo, unsigned ExplicitMin, + unsigned ExplicitMax); + +} // namespace llvm::driver::riscv + +#endif diff --git a/llvm/lib/Frontend/Driver/CMakeLists.txt b/llvm/lib/Frontend/Driver/CMakeLists.txt index 23de4994a300d..ac0bc27a248a3 100644 --- a/llvm/lib/Frontend/Driver/CMakeLists.txt +++ b/llvm/lib/Frontend/Driver/CMakeLists.txt @@ -1,5 +1,6 @@ add_llvm_component_library(LLVMFrontendDriver CodeGenOptions.cpp + RISCV.cpp ADDITIONAL_HEADER_DIRS ${LLVM_MAIN_INCLUDE_DIR}/llvm/Frontend/Driver diff --git a/llvm/lib/Frontend/Driver/RISCV.cpp b/llvm/lib/Frontend/Driver/RISCV.cpp new file mode 100644 index 0000000000000..dfe07fb74550f --- /dev/null +++ b/llvm/lib/Frontend/Driver/RISCV.cpp @@ -0,0 +1,37 @@ +//===--- RISCV.cpp - Shared RISC-V frontend logic -------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "llvm/Frontend/Driver/RISCV.h" +#include "llvm/TargetParser/RISCVTargetParser.h" + +namespace llvm::driver::riscv { + +std::optional> +getVScaleRange(const RISCVISAInfo &ISAInfo, unsigned ExplicitMin, + unsigned ExplicitMax) { + // RISCV::RVVBitsPerBlock is 64. + unsigned VScaleMin = ISAInfo.getMinVLen() / RISCV::RVVBitsPerBlock; + + if (ExplicitMin || ExplicitMax) { + // Treat Zvl*b as a lower bound on vscale. + VScaleMin = std::max(VScaleMin, ExplicitMin); + unsigned VScaleMax = ExplicitMax; + if (VScaleMax != 0 && VScaleMax < VScaleMin) + VScaleMax = VScaleMin; + return std::pair(VScaleMin ? VScaleMin : 1, VScaleMax); + } + + if (VScaleMin > 0) { + unsigned VScaleMax = ISAInfo.getMaxVLen() / RISCV::RVVBitsPerBlock; + return std::make_pair(VScaleMin, VScaleMax); + } + + return std::nullopt; +} + +} // namespace llvm::driver::riscv