Skip to content

Commit a640a2e

Browse files
authored
[clang] Introduce SemaRISCV (#92682)
This patch moves `Sema` functions that are specific for RISC-V into the new `SemaRISCV` class. This continues previous efforts to split `Sema` up. Additional context can be found in #84184. This PR is somewhat different from previous PRs on this topic: 1. Splitting out target-specific functions wasn't previously discussed. It felt quite natural to do, though. 2. I had to make some static function in `SemaChecking.cpp` member functions of `Sema` in order to use them in `SemaRISCV`. 3. I dropped "RISCV" from identifiers, but decided to leave "RVV" (RISC-V "V" vector extensions) intact. I think it's an idiomatic abbreviation at this point, but I'm open to input from contributors in that area. 4. I repurposed `SemaRISCVVectorLookup.cpp` for `SemaRISCV`. I think this was a successful experiment, which both helps the goal of splitting `Sema` up, and shows a way to approach `SemaChecking.cpp`, which I wasn't sure how to approach before. As we move more target-specific function out of there, we'll gradually make the checking "framework" inside `SemaChecking.cpp` public, which is currently a whole bunch of static functions. This would enable us to move more functions outside of `SemaChecking.cpp`.
1 parent 058e445 commit a640a2e

File tree

10 files changed

+1104
-1051
lines changed

10 files changed

+1104
-1051
lines changed

clang/include/clang/Sema/Sema.h

Lines changed: 25 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ class SemaObjC;
175175
class SemaOpenACC;
176176
class SemaOpenMP;
177177
class SemaPseudoObject;
178+
class SemaRISCV;
178179
class SemaSYCL;
179180
class StandardConversionSequence;
180181
class Stmt;
@@ -491,7 +492,6 @@ class Sema final : public SemaBase {
491492
// 29. Constraints and Concepts (SemaConcept.cpp)
492493
// 30. Types (SemaType.cpp)
493494
// 31. FixIt Helpers (SemaFixItUtils.cpp)
494-
// 32. Name Lookup for RISC-V Vector Intrinsic (SemaRISCVVectorLookup.cpp)
495495

496496
/// \name Semantic Analysis
497497
/// Implementations are in Sema.cpp
@@ -1027,6 +1027,11 @@ class Sema final : public SemaBase {
10271027
return *PseudoObjectPtr;
10281028
}
10291029

1030+
SemaRISCV &RISCV() {
1031+
assert(RISCVPtr);
1032+
return *RISCVPtr;
1033+
}
1034+
10301035
SemaSYCL &SYCL() {
10311036
assert(SYCLPtr);
10321037
return *SYCLPtr;
@@ -1069,6 +1074,7 @@ class Sema final : public SemaBase {
10691074
std::unique_ptr<SemaOpenACC> OpenACCPtr;
10701075
std::unique_ptr<SemaOpenMP> OpenMPPtr;
10711076
std::unique_ptr<SemaPseudoObject> PseudoObjectPtr;
1077+
std::unique_ptr<SemaRISCV> RISCVPtr;
10721078
std::unique_ptr<SemaSYCL> SYCLPtr;
10731079

10741080
///@}
@@ -2044,6 +2050,23 @@ class Sema final : public SemaBase {
20442050

20452051
void CheckConstrainedAuto(const AutoType *AutoT, SourceLocation Loc);
20462052

2053+
bool BuiltinConstantArg(CallExpr *TheCall, int ArgNum, llvm::APSInt &Result);
2054+
bool BuiltinConstantArgRange(CallExpr *TheCall, int ArgNum, int Low, int High,
2055+
bool RangeIsError = true);
2056+
bool BuiltinConstantArgMultiple(CallExpr *TheCall, int ArgNum,
2057+
unsigned Multiple);
2058+
bool BuiltinConstantArgPower2(CallExpr *TheCall, int ArgNum);
2059+
bool BuiltinConstantArgShiftedByte(CallExpr *TheCall, int ArgNum,
2060+
unsigned ArgBits);
2061+
bool BuiltinConstantArgShiftedByteOrXXFF(CallExpr *TheCall, int ArgNum,
2062+
unsigned ArgBits);
2063+
2064+
bool checkArgCountAtLeast(CallExpr *Call, unsigned MinArgCount);
2065+
bool checkArgCountAtMost(CallExpr *Call, unsigned MaxArgCount);
2066+
bool checkArgCountRange(CallExpr *Call, unsigned MinArgCount,
2067+
unsigned MaxArgCount);
2068+
bool checkArgCount(CallExpr *Call, unsigned DesiredArgCount);
2069+
20472070
private:
20482071
void CheckArrayAccess(const Expr *BaseExpr, const Expr *IndexExpr,
20492072
const ArraySubscriptExpr *ASE = nullptr,
@@ -2112,11 +2135,7 @@ class Sema final : public SemaBase {
21122135
bool CheckPPCBuiltinFunctionCall(const TargetInfo &TI, unsigned BuiltinID,
21132136
CallExpr *TheCall);
21142137
bool CheckAMDGCNBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall);
2115-
bool CheckRISCVLMUL(CallExpr *TheCall, unsigned ArgNum);
2116-
bool CheckRISCVBuiltinFunctionCall(const TargetInfo &TI, unsigned BuiltinID,
2117-
CallExpr *TheCall);
2118-
void checkRVVTypeSupport(QualType Ty, SourceLocation Loc, Decl *D,
2119-
const llvm::StringMap<bool> &FeatureMap);
2138+
21202139
bool CheckLoongArchBuiltinFunctionCall(const TargetInfo &TI,
21212140
unsigned BuiltinID, CallExpr *TheCall);
21222141
bool CheckWebAssemblyBuiltinFunctionCall(const TargetInfo &TI,
@@ -2146,16 +2165,6 @@ class Sema final : public SemaBase {
21462165
ExprResult BuiltinNontemporalOverloaded(ExprResult TheCallResult);
21472166
ExprResult AtomicOpsOverloaded(ExprResult TheCallResult,
21482167
AtomicExpr::AtomicOp Op);
2149-
bool BuiltinConstantArg(CallExpr *TheCall, int ArgNum, llvm::APSInt &Result);
2150-
bool BuiltinConstantArgRange(CallExpr *TheCall, int ArgNum, int Low, int High,
2151-
bool RangeIsError = true);
2152-
bool BuiltinConstantArgMultiple(CallExpr *TheCall, int ArgNum,
2153-
unsigned Multiple);
2154-
bool BuiltinConstantArgPower2(CallExpr *TheCall, int ArgNum);
2155-
bool BuiltinConstantArgShiftedByte(CallExpr *TheCall, int ArgNum,
2156-
unsigned ArgBits);
2157-
bool BuiltinConstantArgShiftedByteOrXXFF(CallExpr *TheCall, int ArgNum,
2158-
unsigned ArgBits);
21592168
bool BuiltinARMSpecialReg(unsigned BuiltinID, CallExpr *TheCall, int ArgNum,
21602169
unsigned ExpectedFieldNum, bool AllowName);
21612170
bool BuiltinARMMemoryTaggingCall(unsigned BuiltinID, CallExpr *TheCall);
@@ -5890,7 +5899,6 @@ class Sema final : public SemaBase {
58905899
SourceLocation Loc, bool IsCompAssign);
58915900

58925901
bool isValidSveBitcast(QualType srcType, QualType destType);
5893-
bool isValidRVVBitcast(QualType srcType, QualType destType);
58945902

58955903
bool areMatrixTypesOfTheSameDimension(QualType srcTy, QualType destTy);
58965904

@@ -11686,27 +11694,6 @@ class Sema final : public SemaBase {
1168611694
/// Triggered by declaration-attribute processing.
1168711695
void ProcessAPINotes(Decl *D);
1168811696

11689-
///@}
11690-
//
11691-
//
11692-
// -------------------------------------------------------------------------
11693-
//
11694-
//
11695-
11696-
/// \name Name Lookup for RISC-V Vector Intrinsic
11697-
/// Implementations are in SemaRISCVVectorLookup.cpp
11698-
///@{
11699-
11700-
public:
11701-
/// Indicate RISC-V vector builtin functions enabled or not.
11702-
bool DeclareRISCVVBuiltins = false;
11703-
11704-
/// Indicate RISC-V SiFive vector builtin functions enabled or not.
11705-
bool DeclareRISCVSiFiveVectorBuiltins = false;
11706-
11707-
private:
11708-
std::unique_ptr<sema::RISCVIntrinsicManager> RVIntrinsicManager;
11709-
1171011697
///@}
1171111698
};
1171211699

@@ -11729,9 +11716,6 @@ void Sema::PragmaStack<Sema::AlignPackInfo>::Act(SourceLocation PragmaLocation,
1172911716
PragmaMsStackAction Action,
1173011717
llvm::StringRef StackSlotLabel,
1173111718
AlignPackInfo Value);
11732-
11733-
std::unique_ptr<sema::RISCVIntrinsicManager>
11734-
CreateRISCVIntrinsicManager(Sema &S);
1173511719
} // end namespace clang
1173611720

1173711721
#endif

clang/include/clang/Sema/SemaRISCV.h

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//===----- SemaRISCV.h ---- RISC-V target-specific routines ---*- C++ -*---===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
/// \file
9+
/// This file declares semantic analysis functions specific to RISC-V.
10+
///
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef LLVM_CLANG_SEMA_SEMARISCV_H
14+
#define LLVM_CLANG_SEMA_SEMARISCV_H
15+
16+
#include "clang/AST/DeclBase.h"
17+
#include "clang/AST/Expr.h"
18+
#include "clang/AST/Type.h"
19+
#include "clang/Basic/SourceLocation.h"
20+
#include "clang/Basic/TargetInfo.h"
21+
#include "clang/Sema/RISCVIntrinsicManager.h"
22+
#include "clang/Sema/SemaBase.h"
23+
#include "llvm/ADT/StringMap.h"
24+
#include <memory>
25+
26+
namespace clang {
27+
class SemaRISCV : public SemaBase {
28+
public:
29+
SemaRISCV(Sema &S);
30+
31+
bool CheckLMUL(CallExpr *TheCall, unsigned ArgNum);
32+
bool CheckBuiltinFunctionCall(const TargetInfo &TI, unsigned BuiltinID,
33+
CallExpr *TheCall);
34+
void checkRVVTypeSupport(QualType Ty, SourceLocation Loc, Decl *D,
35+
const llvm::StringMap<bool> &FeatureMap);
36+
37+
bool isValidRVVBitcast(QualType srcType, QualType destType);
38+
39+
/// Indicate RISC-V vector builtin functions enabled or not.
40+
bool DeclareRVVBuiltins = false;
41+
42+
/// Indicate RISC-V SiFive vector builtin functions enabled or not.
43+
bool DeclareSiFiveVectorBuiltins = false;
44+
45+
std::unique_ptr<sema::RISCVIntrinsicManager> IntrinsicManager;
46+
};
47+
48+
std::unique_ptr<sema::RISCVIntrinsicManager>
49+
CreateRISCVIntrinsicManager(Sema &S);
50+
} // namespace clang
51+
52+
#endif // LLVM_CLANG_SEMA_SEMARISCV_H

clang/lib/Parse/ParsePragma.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "clang/Sema/Scope.h"
2424
#include "clang/Sema/SemaCUDA.h"
2525
#include "clang/Sema/SemaCodeCompletion.h"
26+
#include "clang/Sema/SemaRISCV.h"
2627
#include "llvm/ADT/ArrayRef.h"
2728
#include "llvm/ADT/StringSwitch.h"
2829
#include <optional>
@@ -4154,7 +4155,7 @@ void PragmaRISCVHandler::HandlePragma(Preprocessor &PP,
41544155
}
41554156

41564157
if (II->isStr("vector"))
4157-
Actions.DeclareRISCVVBuiltins = true;
4158+
Actions.RISCV().DeclareRVVBuiltins = true;
41584159
else if (II->isStr("sifive_vector"))
4159-
Actions.DeclareRISCVSiFiveVectorBuiltins = true;
4160+
Actions.RISCV().DeclareSiFiveVectorBuiltins = true;
41604161
}

clang/lib/Sema/Sema.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
#include "clang/Sema/SemaOpenACC.h"
5151
#include "clang/Sema/SemaOpenMP.h"
5252
#include "clang/Sema/SemaPseudoObject.h"
53+
#include "clang/Sema/SemaRISCV.h"
5354
#include "clang/Sema/SemaSYCL.h"
5455
#include "clang/Sema/TemplateDeduction.h"
5556
#include "clang/Sema/TemplateInstCallback.h"
@@ -212,6 +213,7 @@ Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer,
212213
OpenACCPtr(std::make_unique<SemaOpenACC>(*this)),
213214
OpenMPPtr(std::make_unique<SemaOpenMP>(*this)),
214215
PseudoObjectPtr(std::make_unique<SemaPseudoObject>(*this)),
216+
RISCVPtr(std::make_unique<SemaRISCV>(*this)),
215217
SYCLPtr(std::make_unique<SemaSYCL>(*this)),
216218
MSPointerToMemberRepresentationMethod(
217219
LangOpts.getMSPointerToMemberRepresentationMethod()),
@@ -2051,7 +2053,7 @@ void Sema::checkTypeSupport(QualType Ty, SourceLocation Loc, ValueDecl *D) {
20512053
if (TI.hasRISCVVTypes() && Ty->isRVVSizelessBuiltinType() && FD) {
20522054
llvm::StringMap<bool> CallerFeatureMap;
20532055
Context.getFunctionFeatureMap(CallerFeatureMap, FD);
2054-
checkRVVTypeSupport(Ty, Loc, D, CallerFeatureMap);
2056+
RISCV().checkRVVTypeSupport(Ty, Loc, D, CallerFeatureMap);
20552057
}
20562058

20572059
// Don't allow SVE types in functions without a SVE target.

clang/lib/Sema/SemaCast.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "clang/Sema/Initialization.h"
2626
#include "clang/Sema/SemaInternal.h"
2727
#include "clang/Sema/SemaObjC.h"
28+
#include "clang/Sema/SemaRISCV.h"
2829
#include "llvm/ADT/SmallVector.h"
2930
#include "llvm/ADT/StringExtras.h"
3031
#include <set>
@@ -2391,7 +2392,7 @@ static TryCastResult TryReinterpretCast(Sema &Self, ExprResult &SrcExpr,
23912392
}
23922393

23932394
// Allow bitcasting between SVE VLATs and VLSTs, and vice-versa.
2394-
if (Self.isValidRVVBitcast(SrcType, DestType)) {
2395+
if (Self.RISCV().isValidRVVBitcast(SrcType, DestType)) {
23952396
Kind = CK_BitCast;
23962397
return TC_Success;
23972398
}
@@ -3002,7 +3003,7 @@ void CastOperation::CheckCStyleCast() {
30023003

30033004
// Allow bitcasting between compatible RVV vector types.
30043005
if ((SrcType->isVectorType() || DestType->isVectorType()) &&
3005-
Self.isValidRVVBitcast(SrcType, DestType)) {
3006+
Self.RISCV().isValidRVVBitcast(SrcType, DestType)) {
30063007
Kind = CK_BitCast;
30073008
return;
30083009
}

0 commit comments

Comments
 (0)