Skip to content

Commit 4467c78

Browse files
committed
[clang][RISCV] Set vscale_range attribute based on presence of "v" extension
This follows the path that AArch64 SVE has taken. Doing this via a function attribute set in the frontend is basically a workaround for the fact that several analyzes which need the information (i.e. known bits, lvi, scev) can't easily use TTI without significant amounts of plumbing changes. This patch hard codes "v" numbers, and directly follows the SVE precedent as a result. In a follow up, I hope to drive this from RISCVISAInfo.h/cpp instead, but the MinVLen number being returned from that interface seemed to always be 0 (which is wrong), and I haven't figured out what's going wrong there. Differential Revision: https://reviews.llvm.org/D135894
1 parent d0a4741 commit 4467c78

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

clang/lib/Basic/Targets/RISCV.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,19 @@ bool RISCVTargetInfo::initFeatureMap(
246246
return TargetInfo::initFeatureMap(Features, Diags, CPU, ImpliedFeatures);
247247
}
248248

249+
Optional<std::pair<unsigned, unsigned>>
250+
RISCVTargetInfo::getVScaleRange(const LangOptions &LangOpts) const {
251+
if (LangOpts.VScaleMin || LangOpts.VScaleMax)
252+
return std::pair<unsigned, unsigned>(
253+
LangOpts.VScaleMin ? LangOpts.VScaleMin : 1, LangOpts.VScaleMax);
254+
255+
if (hasFeature("v"))
256+
// Minimum VLEN=128, Maximum VLEN=64k, and RISCV::RVVBitsPerBlock is 64.
257+
return std::pair<unsigned, unsigned>(2, 1024);
258+
259+
return None;
260+
}
261+
249262
/// Return true if has this feature, need to sync with handleTargetFeatures.
250263
bool RISCVTargetInfo::hasFeature(StringRef Feature) const {
251264
bool Is64Bit = getTriple().getArch() == llvm::Triple::riscv64;

clang/lib/Basic/Targets/RISCV.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ class RISCVTargetInfo : public TargetInfo {
9090
StringRef CPU,
9191
const std::vector<std::string> &FeaturesVec) const override;
9292

93+
Optional<std::pair<unsigned, unsigned>>
94+
getVScaleRange(const LangOptions &LangOpts) const override;
95+
9396
bool hasFeature(StringRef Feature) const override;
9497

9598
bool handleTargetFeatures(std::vector<std::string> &Features,
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// RUN: %clang_cc1 -triple riscv64-none-linux-gnu -target-feature +v -mvscale-min=1 -mvscale-max=1 -S -emit-llvm -o - %s | FileCheck %s -D#VBITS=1
2+
// RUN: %clang_cc1 -triple riscv64-none-linux-gnu -target-feature +v -mvscale-min=2 -mvscale-max=2 -S -emit-llvm -o - %s | FileCheck %s -D#VBITS=2
3+
// RUN: %clang_cc1 -triple riscv64-none-linux-gnu -target-feature +v -mvscale-min=4 -mvscale-max=4 -S -emit-llvm -o - %s | FileCheck %s -D#VBITS=4
4+
// RUN: %clang_cc1 -triple riscv64-none-linux-gnu -target-feature +v -mvscale-min=8 -mvscale-max=8 -S -emit-llvm -o - %s | FileCheck %s -D#VBITS=8
5+
// RUN: %clang_cc1 -triple riscv64-none-linux-gnu -target-feature +v -mvscale-min=16 -mvscale-max=16 -S -emit-llvm -o - %s | FileCheck %s -D#VBITS=16
6+
// RUN: %clang_cc1 -triple riscv64-none-linux-gnu -target-feature +v -mvscale-min=1 -S -emit-llvm -o - %s | FileCheck %s -D#VBITS=1 --check-prefix=CHECK-NOMAX
7+
// RUN: %clang_cc1 -triple riscv64-none-linux-gnu -target-feature +v -mvscale-min=2 -S -emit-llvm -o - %s | FileCheck %s -D#VBITS=2 --check-prefix=CHECK-NOMAX
8+
// RUN: %clang_cc1 -triple riscv64-none-linux-gnu -target-feature +v -mvscale-min=4 -S -emit-llvm -o - %s | FileCheck %s -D#VBITS=4 --check-prefix=CHECK-NOMAX
9+
// RUN: %clang_cc1 -triple riscv64-none-linux-gnu -target-feature +v -mvscale-min=8 -S -emit-llvm -o - %s | FileCheck %s -D#VBITS=8 --check-prefix=CHECK-NOMAX
10+
// RUN: %clang_cc1 -triple riscv64-none-linux-gnu -target-feature +v -mvscale-min=16 -S -emit-llvm -o - %s | FileCheck %s -D#VBITS=16 --check-prefix=CHECK-NOMAX
11+
// RUN: %clang_cc1 -triple riscv64-none-linux-gnu -target-feature +v -mvscale-min=1 -mvscale-max=0 -S -emit-llvm -o - %s | FileCheck %s --check-prefix=CHECK-UNBOUNDED
12+
// RUN: %clang_cc1 -triple riscv64-none-linux-gnu -target-feature +v -S -emit-llvm -o - %s | FileCheck %s --check-prefix=CHECK-NONE
13+
14+
// CHECK-LABEL: @func() #0
15+
// CHECK: attributes #0 = { {{.*}} vscale_range([[#VBITS]],[[#VBITS]]) {{.*}} }
16+
// CHECK-NOMAX: attributes #0 = { {{.*}} vscale_range([[#VBITS]],0) {{.*}} }
17+
// CHECK-UNBOUNDED: attributes #0 = { {{.*}} vscale_range(1,0) {{.*}} }
18+
// CHECK-NONE: attributes #0 = { {{.*}} vscale_range(2,1024) {{.*}} }
19+
void func(void) {}

0 commit comments

Comments
 (0)