[clang][modules] Don't prevent translation of FW_Private includes when explicitly...
[llvm-project.git] / clang / lib / Sema / SemaRISCVVectorLookup.cpp
blobacdf3260007bc3a6e9e145b892663709e2d83743
1 //==- SemaRISCVVectorLookup.cpp - Name Lookup for RISC-V Vector Intrinsic -==//
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 //
9 // This file implements name lookup for RISC-V vector intrinsic.
11 //===----------------------------------------------------------------------===//
13 #include "clang/AST/ASTContext.h"
14 #include "clang/AST/Decl.h"
15 #include "clang/Basic/Builtins.h"
16 #include "clang/Basic/TargetInfo.h"
17 #include "clang/Lex/Preprocessor.h"
18 #include "clang/Sema/Lookup.h"
19 #include "clang/Sema/RISCVIntrinsicManager.h"
20 #include "clang/Sema/Sema.h"
21 #include "clang/Support/RISCVVIntrinsicUtils.h"
22 #include "llvm/ADT/SmallVector.h"
23 #include <optional>
24 #include <string>
25 #include <vector>
27 using namespace llvm;
28 using namespace clang;
29 using namespace clang::RISCV;
31 using IntrinsicKind = sema::RISCVIntrinsicManager::IntrinsicKind;
33 namespace {
35 // Function definition of a RVV intrinsic.
36 struct RVVIntrinsicDef {
37 /// Full function name with suffix, e.g. vadd_vv_i32m1.
38 std::string Name;
40 /// Overloaded function name, e.g. vadd.
41 std::string OverloadName;
43 /// Mapping to which clang built-in function, e.g. __builtin_rvv_vadd.
44 std::string BuiltinName;
46 /// Function signature, first element is return type.
47 RVVTypes Signature;
50 struct RVVOverloadIntrinsicDef {
51 // Indexes of RISCVIntrinsicManagerImpl::IntrinsicList.
52 SmallVector<size_t, 8> Indexes;
55 } // namespace
57 static const PrototypeDescriptor RVVSignatureTable[] = {
58 #define DECL_SIGNATURE_TABLE
59 #include "clang/Basic/riscv_vector_builtin_sema.inc"
60 #undef DECL_SIGNATURE_TABLE
63 static const PrototypeDescriptor RVSiFiveVectorSignatureTable[] = {
64 #define DECL_SIGNATURE_TABLE
65 #include "clang/Basic/riscv_sifive_vector_builtin_sema.inc"
66 #undef DECL_SIGNATURE_TABLE
69 static const RVVIntrinsicRecord RVVIntrinsicRecords[] = {
70 #define DECL_INTRINSIC_RECORDS
71 #include "clang/Basic/riscv_vector_builtin_sema.inc"
72 #undef DECL_INTRINSIC_RECORDS
75 static const RVVIntrinsicRecord RVSiFiveVectorIntrinsicRecords[] = {
76 #define DECL_INTRINSIC_RECORDS
77 #include "clang/Basic/riscv_sifive_vector_builtin_sema.inc"
78 #undef DECL_INTRINSIC_RECORDS
81 // Get subsequence of signature table.
82 static ArrayRef<PrototypeDescriptor>
83 ProtoSeq2ArrayRef(IntrinsicKind K, uint16_t Index, uint8_t Length) {
84 switch (K) {
85 case IntrinsicKind::RVV:
86 return ArrayRef(&RVVSignatureTable[Index], Length);
87 case IntrinsicKind::SIFIVE_VECTOR:
88 return ArrayRef(&RVSiFiveVectorSignatureTable[Index], Length);
90 llvm_unreachable("Unhandled IntrinsicKind");
93 static QualType RVVType2Qual(ASTContext &Context, const RVVType *Type) {
94 QualType QT;
95 switch (Type->getScalarType()) {
96 case ScalarTypeKind::Void:
97 QT = Context.VoidTy;
98 break;
99 case ScalarTypeKind::Size_t:
100 QT = Context.getSizeType();
101 break;
102 case ScalarTypeKind::Ptrdiff_t:
103 QT = Context.getPointerDiffType();
104 break;
105 case ScalarTypeKind::UnsignedLong:
106 QT = Context.UnsignedLongTy;
107 break;
108 case ScalarTypeKind::SignedLong:
109 QT = Context.LongTy;
110 break;
111 case ScalarTypeKind::Boolean:
112 QT = Context.BoolTy;
113 break;
114 case ScalarTypeKind::SignedInteger:
115 QT = Context.getIntTypeForBitwidth(Type->getElementBitwidth(), true);
116 break;
117 case ScalarTypeKind::UnsignedInteger:
118 QT = Context.getIntTypeForBitwidth(Type->getElementBitwidth(), false);
119 break;
120 case ScalarTypeKind::Float:
121 switch (Type->getElementBitwidth()) {
122 case 64:
123 QT = Context.DoubleTy;
124 break;
125 case 32:
126 QT = Context.FloatTy;
127 break;
128 case 16:
129 QT = Context.Float16Ty;
130 break;
131 default:
132 llvm_unreachable("Unsupported floating point width.");
134 break;
135 case Invalid:
136 case Undefined:
137 llvm_unreachable("Unhandled type.");
139 if (Type->isVector()) {
140 if (Type->isTuple())
141 QT = Context.getScalableVectorType(QT, *Type->getScale(), Type->getNF());
142 else
143 QT = Context.getScalableVectorType(QT, *Type->getScale());
146 if (Type->isConstant())
147 QT = Context.getConstType(QT);
149 // Transform the type to a pointer as the last step, if necessary.
150 if (Type->isPointer())
151 QT = Context.getPointerType(QT);
153 return QT;
156 namespace {
157 class RISCVIntrinsicManagerImpl : public sema::RISCVIntrinsicManager {
158 private:
159 Sema &S;
160 ASTContext &Context;
161 RVVTypeCache TypeCache;
162 bool ConstructedRISCVVBuiltins;
163 bool ConstructedRISCVSiFiveVectorBuiltins;
165 // List of all RVV intrinsic.
166 std::vector<RVVIntrinsicDef> IntrinsicList;
167 // Mapping function name to index of IntrinsicList.
168 StringMap<size_t> Intrinsics;
169 // Mapping function name to RVVOverloadIntrinsicDef.
170 StringMap<RVVOverloadIntrinsicDef> OverloadIntrinsics;
173 // Create RVVIntrinsicDef.
174 void InitRVVIntrinsic(const RVVIntrinsicRecord &Record, StringRef SuffixStr,
175 StringRef OverloadedSuffixStr, bool IsMask,
176 RVVTypes &Types, bool HasPolicy, Policy PolicyAttrs);
178 // Create FunctionDecl for a vector intrinsic.
179 void CreateRVVIntrinsicDecl(LookupResult &LR, IdentifierInfo *II,
180 Preprocessor &PP, unsigned Index,
181 bool IsOverload);
183 void ConstructRVVIntrinsics(ArrayRef<RVVIntrinsicRecord> Recs,
184 IntrinsicKind K);
186 public:
187 RISCVIntrinsicManagerImpl(clang::Sema &S) : S(S), Context(S.Context) {
188 ConstructedRISCVVBuiltins = false;
189 ConstructedRISCVSiFiveVectorBuiltins = false;
192 // Initialize IntrinsicList
193 void InitIntrinsicList() override;
195 // Create RISC-V vector intrinsic and insert into symbol table if found, and
196 // return true, otherwise return false.
197 bool CreateIntrinsicIfFound(LookupResult &LR, IdentifierInfo *II,
198 Preprocessor &PP) override;
200 } // namespace
202 void RISCVIntrinsicManagerImpl::ConstructRVVIntrinsics(
203 ArrayRef<RVVIntrinsicRecord> Recs, IntrinsicKind K) {
204 const TargetInfo &TI = Context.getTargetInfo();
205 static const std::pair<const char *, RVVRequire> FeatureCheckList[] = {
206 {"64bit", RVV_REQ_RV64},
207 {"xsfvcp", RVV_REQ_Xsfvcp},
208 {"xsfvqmaccdod", RVV_REQ_Xsfvqmaccdod},
209 {"xsfvqmaccqoq", RVV_REQ_Xsfvqmaccqoq},
210 {"experimental-zvbb", RVV_REQ_Zvbb},
211 {"experimental-zvbc", RVV_REQ_Zvbc},
212 {"experimental-zvkb", RVV_REQ_Zvkb},
213 {"experimental-zvkg", RVV_REQ_Zvkg},
214 {"experimental-zvkned", RVV_REQ_Zvkned},
215 {"experimental-zvknha", RVV_REQ_Zvknha},
216 {"experimental-zvksed", RVV_REQ_Zvksed},
217 {"experimental-zvksh", RVV_REQ_Zvksh}};
219 // Construction of RVVIntrinsicRecords need to sync with createRVVIntrinsics
220 // in RISCVVEmitter.cpp.
221 for (auto &Record : Recs) {
222 // Check requirements.
223 if (llvm::any_of(FeatureCheckList, [&](const auto &Item) {
224 return (Record.RequiredExtensions & Item.second) == Item.second &&
225 !TI.hasFeature(Item.first);
227 continue;
229 // Create Intrinsics for each type and LMUL.
230 BasicType BaseType = BasicType::Unknown;
231 ArrayRef<PrototypeDescriptor> BasicProtoSeq =
232 ProtoSeq2ArrayRef(K, Record.PrototypeIndex, Record.PrototypeLength);
233 ArrayRef<PrototypeDescriptor> SuffixProto =
234 ProtoSeq2ArrayRef(K, Record.SuffixIndex, Record.SuffixLength);
235 ArrayRef<PrototypeDescriptor> OverloadedSuffixProto = ProtoSeq2ArrayRef(
236 K, Record.OverloadedSuffixIndex, Record.OverloadedSuffixSize);
238 PolicyScheme UnMaskedPolicyScheme =
239 static_cast<PolicyScheme>(Record.UnMaskedPolicyScheme);
240 PolicyScheme MaskedPolicyScheme =
241 static_cast<PolicyScheme>(Record.MaskedPolicyScheme);
243 const Policy DefaultPolicy;
245 llvm::SmallVector<PrototypeDescriptor> ProtoSeq =
246 RVVIntrinsic::computeBuiltinTypes(
247 BasicProtoSeq, /*IsMasked=*/false,
248 /*HasMaskedOffOperand=*/false, Record.HasVL, Record.NF,
249 UnMaskedPolicyScheme, DefaultPolicy, Record.IsTuple);
251 llvm::SmallVector<PrototypeDescriptor> ProtoMaskSeq;
252 if (Record.HasMasked)
253 ProtoMaskSeq = RVVIntrinsic::computeBuiltinTypes(
254 BasicProtoSeq, /*IsMasked=*/true, Record.HasMaskedOffOperand,
255 Record.HasVL, Record.NF, MaskedPolicyScheme, DefaultPolicy,
256 Record.IsTuple);
258 bool UnMaskedHasPolicy = UnMaskedPolicyScheme != PolicyScheme::SchemeNone;
259 bool MaskedHasPolicy = MaskedPolicyScheme != PolicyScheme::SchemeNone;
260 SmallVector<Policy> SupportedUnMaskedPolicies =
261 RVVIntrinsic::getSupportedUnMaskedPolicies();
262 SmallVector<Policy> SupportedMaskedPolicies =
263 RVVIntrinsic::getSupportedMaskedPolicies(Record.HasTailPolicy,
264 Record.HasMaskPolicy);
266 for (unsigned int TypeRangeMaskShift = 0;
267 TypeRangeMaskShift <= static_cast<unsigned int>(BasicType::MaxOffset);
268 ++TypeRangeMaskShift) {
269 unsigned int BaseTypeI = 1 << TypeRangeMaskShift;
270 BaseType = static_cast<BasicType>(BaseTypeI);
272 if ((BaseTypeI & Record.TypeRangeMask) != BaseTypeI)
273 continue;
275 if (BaseType == BasicType::Float16) {
276 if ((Record.RequiredExtensions & RVV_REQ_ZvfhminOrZvfh) ==
277 RVV_REQ_ZvfhminOrZvfh) {
278 if (!TI.hasFeature("zvfh") && !TI.hasFeature("zvfhmin"))
279 continue;
280 } else if (!TI.hasFeature("zvfh")) {
281 continue;
285 // Expanded with different LMUL.
286 for (int Log2LMUL = -3; Log2LMUL <= 3; Log2LMUL++) {
287 if (!(Record.Log2LMULMask & (1 << (Log2LMUL + 3))))
288 continue;
290 std::optional<RVVTypes> Types =
291 TypeCache.computeTypes(BaseType, Log2LMUL, Record.NF, ProtoSeq);
293 // Ignored to create new intrinsic if there are any illegal types.
294 if (!Types.has_value())
295 continue;
297 std::string SuffixStr = RVVIntrinsic::getSuffixStr(
298 TypeCache, BaseType, Log2LMUL, SuffixProto);
299 std::string OverloadedSuffixStr = RVVIntrinsic::getSuffixStr(
300 TypeCache, BaseType, Log2LMUL, OverloadedSuffixProto);
302 // Create non-masked intrinsic.
303 InitRVVIntrinsic(Record, SuffixStr, OverloadedSuffixStr, false, *Types,
304 UnMaskedHasPolicy, DefaultPolicy);
306 // Create non-masked policy intrinsic.
307 if (Record.UnMaskedPolicyScheme != PolicyScheme::SchemeNone) {
308 for (auto P : SupportedUnMaskedPolicies) {
309 llvm::SmallVector<PrototypeDescriptor> PolicyPrototype =
310 RVVIntrinsic::computeBuiltinTypes(
311 BasicProtoSeq, /*IsMasked=*/false,
312 /*HasMaskedOffOperand=*/false, Record.HasVL, Record.NF,
313 UnMaskedPolicyScheme, P, Record.IsTuple);
314 std::optional<RVVTypes> PolicyTypes = TypeCache.computeTypes(
315 BaseType, Log2LMUL, Record.NF, PolicyPrototype);
316 InitRVVIntrinsic(Record, SuffixStr, OverloadedSuffixStr,
317 /*IsMask=*/false, *PolicyTypes, UnMaskedHasPolicy,
321 if (!Record.HasMasked)
322 continue;
323 // Create masked intrinsic.
324 std::optional<RVVTypes> MaskTypes =
325 TypeCache.computeTypes(BaseType, Log2LMUL, Record.NF, ProtoMaskSeq);
326 InitRVVIntrinsic(Record, SuffixStr, OverloadedSuffixStr, true,
327 *MaskTypes, MaskedHasPolicy, DefaultPolicy);
328 if (Record.MaskedPolicyScheme == PolicyScheme::SchemeNone)
329 continue;
330 // Create masked policy intrinsic.
331 for (auto P : SupportedMaskedPolicies) {
332 llvm::SmallVector<PrototypeDescriptor> PolicyPrototype =
333 RVVIntrinsic::computeBuiltinTypes(
334 BasicProtoSeq, /*IsMasked=*/true, Record.HasMaskedOffOperand,
335 Record.HasVL, Record.NF, MaskedPolicyScheme, P,
336 Record.IsTuple);
337 std::optional<RVVTypes> PolicyTypes = TypeCache.computeTypes(
338 BaseType, Log2LMUL, Record.NF, PolicyPrototype);
339 InitRVVIntrinsic(Record, SuffixStr, OverloadedSuffixStr,
340 /*IsMask=*/true, *PolicyTypes, MaskedHasPolicy, P);
342 } // End for different LMUL
343 } // End for different TypeRange
347 void RISCVIntrinsicManagerImpl::InitIntrinsicList() {
349 if (S.DeclareRISCVVBuiltins && !ConstructedRISCVVBuiltins) {
350 ConstructedRISCVVBuiltins = true;
351 ConstructRVVIntrinsics(RVVIntrinsicRecords,
352 IntrinsicKind::RVV);
354 if (S.DeclareRISCVSiFiveVectorBuiltins &&
355 !ConstructedRISCVSiFiveVectorBuiltins) {
356 ConstructedRISCVSiFiveVectorBuiltins = true;
357 ConstructRVVIntrinsics(RVSiFiveVectorIntrinsicRecords,
358 IntrinsicKind::SIFIVE_VECTOR);
362 // Compute name and signatures for intrinsic with practical types.
363 void RISCVIntrinsicManagerImpl::InitRVVIntrinsic(
364 const RVVIntrinsicRecord &Record, StringRef SuffixStr,
365 StringRef OverloadedSuffixStr, bool IsMasked, RVVTypes &Signature,
366 bool HasPolicy, Policy PolicyAttrs) {
367 // Function name, e.g. vadd_vv_i32m1.
368 std::string Name = Record.Name;
369 if (!SuffixStr.empty())
370 Name += "_" + SuffixStr.str();
372 // Overloaded function name, e.g. vadd.
373 std::string OverloadedName;
374 if (!Record.OverloadedName)
375 OverloadedName = StringRef(Record.Name).split("_").first.str();
376 else
377 OverloadedName = Record.OverloadedName;
378 if (!OverloadedSuffixStr.empty())
379 OverloadedName += "_" + OverloadedSuffixStr.str();
381 // clang built-in function name, e.g. __builtin_rvv_vadd.
382 std::string BuiltinName = "__builtin_rvv_" + std::string(Record.Name);
384 RVVIntrinsic::updateNamesAndPolicy(IsMasked, HasPolicy, Name, BuiltinName,
385 OverloadedName, PolicyAttrs,
386 Record.HasFRMRoundModeOp);
388 // Put into IntrinsicList.
389 size_t Index = IntrinsicList.size();
390 IntrinsicList.push_back({Name, OverloadedName, BuiltinName, Signature});
392 // Creating mapping to Intrinsics.
393 Intrinsics.insert({Name, Index});
395 // Get the RVVOverloadIntrinsicDef.
396 RVVOverloadIntrinsicDef &OverloadIntrinsicDef =
397 OverloadIntrinsics[OverloadedName];
399 // And added the index.
400 OverloadIntrinsicDef.Indexes.push_back(Index);
403 void RISCVIntrinsicManagerImpl::CreateRVVIntrinsicDecl(LookupResult &LR,
404 IdentifierInfo *II,
405 Preprocessor &PP,
406 unsigned Index,
407 bool IsOverload) {
408 ASTContext &Context = S.Context;
409 RVVIntrinsicDef &IDef = IntrinsicList[Index];
410 RVVTypes Sigs = IDef.Signature;
411 size_t SigLength = Sigs.size();
412 RVVType *ReturnType = Sigs[0];
413 QualType RetType = RVVType2Qual(Context, ReturnType);
414 SmallVector<QualType, 8> ArgTypes;
415 QualType BuiltinFuncType;
417 // Skip return type, and convert RVVType to QualType for arguments.
418 for (size_t i = 1; i < SigLength; ++i)
419 ArgTypes.push_back(RVVType2Qual(Context, Sigs[i]));
421 FunctionProtoType::ExtProtoInfo PI(
422 Context.getDefaultCallingConvention(false, false, true));
424 PI.Variadic = false;
426 SourceLocation Loc = LR.getNameLoc();
427 BuiltinFuncType = Context.getFunctionType(RetType, ArgTypes, PI);
428 DeclContext *Parent = Context.getTranslationUnitDecl();
430 FunctionDecl *RVVIntrinsicDecl = FunctionDecl::Create(
431 Context, Parent, Loc, Loc, II, BuiltinFuncType, /*TInfo=*/nullptr,
432 SC_Extern, S.getCurFPFeatures().isFPConstrained(),
433 /*isInlineSpecified*/ false,
434 /*hasWrittenPrototype*/ true);
436 // Create Decl objects for each parameter, adding them to the
437 // FunctionDecl.
438 const auto *FP = cast<FunctionProtoType>(BuiltinFuncType);
439 SmallVector<ParmVarDecl *, 8> ParmList;
440 for (unsigned IParm = 0, E = FP->getNumParams(); IParm != E; ++IParm) {
441 ParmVarDecl *Parm =
442 ParmVarDecl::Create(Context, RVVIntrinsicDecl, Loc, Loc, nullptr,
443 FP->getParamType(IParm), nullptr, SC_None, nullptr);
444 Parm->setScopeInfo(0, IParm);
445 ParmList.push_back(Parm);
447 RVVIntrinsicDecl->setParams(ParmList);
449 // Add function attributes.
450 if (IsOverload)
451 RVVIntrinsicDecl->addAttr(OverloadableAttr::CreateImplicit(Context));
453 // Setup alias to __builtin_rvv_*
454 IdentifierInfo &IntrinsicII = PP.getIdentifierTable().get(IDef.BuiltinName);
455 RVVIntrinsicDecl->addAttr(
456 BuiltinAliasAttr::CreateImplicit(S.Context, &IntrinsicII));
458 // Add to symbol table.
459 LR.addDecl(RVVIntrinsicDecl);
462 bool RISCVIntrinsicManagerImpl::CreateIntrinsicIfFound(LookupResult &LR,
463 IdentifierInfo *II,
464 Preprocessor &PP) {
465 StringRef Name = II->getName();
467 // Lookup the function name from the overload intrinsics first.
468 auto OvIItr = OverloadIntrinsics.find(Name);
469 if (OvIItr != OverloadIntrinsics.end()) {
470 const RVVOverloadIntrinsicDef &OvIntrinsicDef = OvIItr->second;
471 for (auto Index : OvIntrinsicDef.Indexes)
472 CreateRVVIntrinsicDecl(LR, II, PP, Index,
473 /*IsOverload*/ true);
475 // If we added overloads, need to resolve the lookup result.
476 LR.resolveKind();
477 return true;
480 // Lookup the function name from the intrinsics.
481 auto Itr = Intrinsics.find(Name);
482 if (Itr != Intrinsics.end()) {
483 CreateRVVIntrinsicDecl(LR, II, PP, Itr->second,
484 /*IsOverload*/ false);
485 return true;
488 // It's not an RVV intrinsics.
489 return false;
492 namespace clang {
493 std::unique_ptr<clang::sema::RISCVIntrinsicManager>
494 CreateRISCVIntrinsicManager(Sema &S) {
495 return std::make_unique<RISCVIntrinsicManagerImpl>(S);
497 } // namespace clang