xref: /llvm-project/llvm/utils/TableGen/Basic/CodeGenIntrinsics.cpp (revision 62e2c7fb2d18b43149a07526f6a3c0563d50e2fa)
1 //===- CodeGenIntrinsics.cpp - Intrinsic Class Wrapper --------------------===//
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 defines a wrapper class for the 'Intrinsic' TableGen class.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "CodeGenIntrinsics.h"
14 #include "llvm/ADT/ArrayRef.h"
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/ADT/Twine.h"
17 #include "llvm/Support/ErrorHandling.h"
18 #include "llvm/TableGen/Error.h"
19 #include "llvm/TableGen/Record.h"
20 #include <algorithm>
21 #include <cassert>
22 using namespace llvm;
23 
24 //===----------------------------------------------------------------------===//
25 // CodeGenIntrinsic Implementation
26 //===----------------------------------------------------------------------===//
27 
28 CodeGenIntrinsicContext::CodeGenIntrinsicContext(const RecordKeeper &RC) {
29   for (const Record *Rec : RC.getAllDerivedDefinitions("IntrinsicProperty"))
30     if (Rec->getValueAsBit("IsDefault"))
31       DefaultProperties.push_back(Rec);
32 
33   // The maximum number of values that an intrinsic can return is the size of
34   // of `IIT_RetNumbers` list - 1 (since we index into this list using the
35   // number of return values as the index).
36   const auto *IIT_RetNumbers =
37       dyn_cast_or_null<ListInit>(RC.getGlobal("IIT_RetNumbers"));
38   if (!IIT_RetNumbers)
39     PrintFatalError("unable to find 'IIT_RetNumbers' list");
40   MaxNumReturn = IIT_RetNumbers->size() - 1;
41 }
42 
43 CodeGenIntrinsicTable::CodeGenIntrinsicTable(const RecordKeeper &RC) {
44   CodeGenIntrinsicContext Ctx(RC);
45 
46   ArrayRef<const Record *> Defs = RC.getAllDerivedDefinitions("Intrinsic");
47   Intrinsics.reserve(Defs.size());
48 
49   for (const Record *Def : Defs)
50     Intrinsics.emplace_back(CodeGenIntrinsic(Def, Ctx));
51 
52   llvm::sort(Intrinsics,
53              [](const CodeGenIntrinsic &LHS, const CodeGenIntrinsic &RHS) {
54                // Order target independent intrinsics before target dependent
55                // ones.
56                bool LHSHasTarget = !LHS.TargetPrefix.empty();
57                bool RHSHasTarget = !RHS.TargetPrefix.empty();
58 
59                // To ensure deterministic sorted order when duplicates are
60                // present, use record ID as a tie-breaker similar to
61                // sortAndReportDuplicates in Utils.cpp.
62                unsigned LhsID = LHS.TheDef->getID();
63                unsigned RhsID = RHS.TheDef->getID();
64 
65                return std::tie(LHSHasTarget, LHS.Name, LhsID) <
66                       std::tie(RHSHasTarget, RHS.Name, RhsID);
67              });
68 
69   Targets.push_back({"", 0, 0});
70   for (size_t I = 0, E = Intrinsics.size(); I < E; ++I)
71     if (Intrinsics[I].TargetPrefix != Targets.back().Name) {
72       Targets.back().Count = I - Targets.back().Offset;
73       Targets.push_back({Intrinsics[I].TargetPrefix, I, 0});
74     }
75   Targets.back().Count = Intrinsics.size() - Targets.back().Offset;
76 
77   CheckDuplicateIntrinsics();
78   CheckTargetIndependentIntrinsics();
79   CheckOverloadSuffixConflicts();
80 }
81 
82 // Check for duplicate intrinsic names.
83 void CodeGenIntrinsicTable::CheckDuplicateIntrinsics() const {
84   // Since the Intrinsics vector is already sorted by name, if there are 2 or
85   // more intrinsics with duplicate names, they will appear adjacent in sorted
86   // order. Note that if the intrinsic name was derived from the record name
87   // there cannot be be duplicate as TableGen parser would have flagged that.
88   // However, if the name was specified in the intrinsic definition, then its
89   // possible to have duplicate names.
90   auto I = std::adjacent_find(
91       Intrinsics.begin(), Intrinsics.end(),
92       [](const CodeGenIntrinsic &Int1, const CodeGenIntrinsic &Int2) {
93         return Int1.Name == Int2.Name;
94       });
95   if (I == Intrinsics.end())
96     return;
97 
98   // Found a duplicate intrinsics.
99   const CodeGenIntrinsic &First = *I;
100   const CodeGenIntrinsic &Second = *(I + 1);
101   PrintError(Second.TheDef,
102              Twine("Intrinsic `") + First.Name + "` is already defined");
103   PrintFatalNote(First.TheDef, "Previous definition here");
104 }
105 
106 // For target independent intrinsics, check that their second dotted component
107 // does not match any target name.
108 void CodeGenIntrinsicTable::CheckTargetIndependentIntrinsics() const {
109   SmallDenseSet<StringRef> TargetNames;
110   for (const auto &Target : ArrayRef(Targets).drop_front())
111     TargetNames.insert(Target.Name);
112 
113   // Set of target independent intrinsics.
114   const auto &Set = Targets[0];
115   for (const auto &Int : ArrayRef(&Intrinsics[Set.Offset], Set.Count)) {
116     StringRef Name = Int.Name;
117     StringRef Prefix = Name.drop_front(5).split('.').first;
118     if (!TargetNames.contains(Prefix))
119       continue;
120     PrintFatalError(Int.TheDef,
121                     "target independent intrinsic `" + Name +
122                         "' has prefix `llvm." + Prefix +
123                         "` that conflicts with intrinsics for target `" +
124                         Prefix + "`");
125   }
126 }
127 
128 // Return true if the given Suffix looks like a mangled type. Note that this
129 // check is conservative, but allows all existing LLVM intrinsic suffixes to be
130 // considered as not looking like a mangling suffix.
131 static bool doesSuffixLookLikeMangledType(StringRef Suffix) {
132   // Try to match against possible mangling suffixes for various types.
133   // See getMangledTypeStr() for the mangling suffixes possible. It includes
134   //  pointer       : p[0-9]+
135   //  array         : a[0-9]+.+
136   //  struct:       : s_/sl_.+
137   //  function      : f_.+
138   //  vector        : v/nxv[0-9]+.+
139   //  target type   : t.+
140   //  integer       : i[0-9]+
141   //  named types   : See `NamedTypes` below.
142 
143   // Match anything with an _, so match function and struct types.
144   if (Suffix.contains('_'))
145     return true;
146 
147   // [av][0-9]+.+, simplified to [av][0-9].+
148   if (Suffix.size() >= 2 && is_contained("av", Suffix[0]) && isDigit(Suffix[1]))
149     return true;
150 
151   // nxv[0-9]+.+, simplified to nxv[0-9].+
152   if (Suffix.size() >= 4 && Suffix.starts_with("nxv") && isDigit(Suffix[3]))
153     return true;
154 
155   // t.+
156   if (Suffix.size() > 1 && Suffix.starts_with('t'))
157     return false;
158 
159   // [pi][0-9]+
160   if (is_contained("pi", Suffix[0]) && all_of(Suffix.drop_front(), isDigit))
161     return true;
162 
163   // Match one of the named types.
164   static constexpr StringLiteral NamedTypes[] = {
165       "isVoid", "Metadata", "f16",  "f32",     "f64",
166       "f80",    "f128",     "bf16", "ppcf128", "x86amx"};
167   return is_contained(NamedTypes, Suffix);
168 }
169 
170 // Check for conflicts with overloaded intrinsics. If there exists an overloaded
171 // intrinsic with base name `llvm.target.foo`, LLVM will add a mangling suffix
172 // to it to encode the overload types. This mangling suffix is 1 or more .
173 // prefixed mangled type string as defined in `getMangledTypeStr`. If there
174 // exists another intrinsic `llvm.target.foo[.<suffixN>]+`, which has the same
175 // prefix as the overloaded intrinsic, its possible that there may be a name
176 // conflict with the overloaded intrinsic and either one may interfere with name
177 // lookup for the other, leading to wrong intrinsic ID being assigned.
178 //
179 // The actual name lookup in the intrinsic name table is done by a search
180 // on each successive '.' separted component of the intrinsic name (see
181 // `lookupLLVMIntrinsicByName`). Consider first the case where there exists a
182 // non-overloaded intrinsic `llvm.target.foo[.suffix]+`. For the non-overloaded
183 // intrinsics, the name lookup is an exact match, so the presence of the
184 // overloaded intrinsic with the same prefix will not interfere with the
185 // search. However, a lookup intended to match the overloaded intrinsic might be
186 // affected by the presence of another entry in the name table with the same
187 // prefix.
188 //
189 // Since LLVM's name lookup first selects the target specific (or target
190 // independent) slice of the name table to look into, intrinsics in 2 different
191 // targets cannot conflict with each other. Within a specific target,
192 // if we have an overloaded intrinsic with name `llvm.target.foo` and another
193 // one with same prefix and one or more suffixes `llvm.target.foo[.<suffixN>]+`,
194 // then the name search will try to first match against suffix0, then suffix1
195 // etc. If suffix0 can match a mangled type, then the search for an
196 // `llvm.target.foo` with a mangling suffix can match against suffix0,
197 // preventing a match with `llvm.target.foo`. If suffix0 cannot match a mangled
198 // type, then that cannot happen, so we do not need to check for later suffixes.
199 //
200 // Generalizing, the `llvm.target.foo[.suffixN]+` will cause a conflict if the
201 // first suffix (.suffix0) can match a mangled type (and then we do not need to
202 // check later suffixes) and will not cause a conflict if it cannot (and then
203 // again, we do not need to check for later suffixes).
204 void CodeGenIntrinsicTable::CheckOverloadSuffixConflicts() const {
205   for (const TargetSet &Set : Targets) {
206     const CodeGenIntrinsic *Overloaded = nullptr;
207     for (const CodeGenIntrinsic &Int : (*this)[Set]) {
208       // If we do not have an overloaded intrinsic to check against, nothing
209       // to do except potentially identifying this as a candidate for checking
210       // against in future iteration.
211       if (!Overloaded) {
212         if (Int.isOverloaded)
213           Overloaded = &Int;
214         continue;
215       }
216 
217       StringRef Name = Int.Name;
218       StringRef OverloadName = Overloaded->Name;
219       // If we have an overloaded intrinsic to check again, check if its name is
220       // a proper prefix of this intrinsic.
221       if (Name.starts_with(OverloadName) && Name[OverloadName.size()] == '.') {
222         // If yes, verify suffixes and flag an error.
223         StringRef Suffixes = Name.drop_front(OverloadName.size() + 1);
224 
225         // Only need to look at the first suffix.
226         StringRef Suffix0 = Suffixes.split('.').first;
227 
228         if (!doesSuffixLookLikeMangledType(Suffix0))
229           continue;
230 
231         unsigned SuffixSize = OverloadName.size() + 1 + Suffix0.size();
232         // If suffix looks like mangling suffix, flag it as an error.
233         PrintError(Int.TheDef->getLoc(),
234                    "intrinsic `" + Name + "` cannot share prefix `" +
235                        Name.take_front(SuffixSize) +
236                        "` with another overloaded intrinsic `" + OverloadName +
237                        "`");
238         PrintNote(Overloaded->TheDef->getLoc(),
239                   "Overloaded intrinsic `" + OverloadName + "` defined here");
240         continue;
241       }
242 
243       // If we find an intrinsic that is not a proper prefix, any later
244       // intrinsic is also not going to be a proper prefix, so invalidate the
245       // overloaded to check against.
246       Overloaded = nullptr;
247     }
248   }
249 }
250 
251 const CodeGenIntrinsic &CodeGenIntrinsicMap::operator[](const Record *Record) {
252   if (!Record->isSubClassOf("Intrinsic"))
253     PrintFatalError("Intrinsic defs should be subclass of 'Intrinsic' class");
254 
255   auto [Iter, Inserted] = Map.try_emplace(Record);
256   if (Inserted)
257     Iter->second = std::make_unique<CodeGenIntrinsic>(Record, Ctx);
258   return *Iter->second;
259 }
260 
261 CodeGenIntrinsic::CodeGenIntrinsic(const Record *R,
262                                    const CodeGenIntrinsicContext &Ctx)
263     : TheDef(R) {
264   StringRef DefName = TheDef->getName();
265   ArrayRef<SMLoc> DefLoc = R->getLoc();
266 
267   if (!DefName.starts_with("int_"))
268     PrintFatalError(DefLoc,
269                     "Intrinsic '" + DefName + "' does not start with 'int_'!");
270 
271   EnumName = DefName.substr(4);
272 
273   // Ignore a missing ClangBuiltinName field.
274   ClangBuiltinName =
275       R->getValueAsOptionalString("ClangBuiltinName").value_or("");
276   // Ignore a missing MSBuiltinName field.
277   MSBuiltinName = R->getValueAsOptionalString("MSBuiltinName").value_or("");
278 
279   TargetPrefix = R->getValueAsString("TargetPrefix");
280   Name = R->getValueAsString("LLVMName").str();
281 
282   if (Name == "") {
283     // If an explicit name isn't specified, derive one from the DefName.
284     Name = "llvm." + EnumName.str();
285     llvm::replace(Name, '_', '.');
286   } else {
287     // Verify it starts with "llvm.".
288     if (!StringRef(Name).starts_with("llvm."))
289       PrintFatalError(DefLoc, "Intrinsic '" + DefName +
290                                   "'s name does not start with 'llvm.'!");
291   }
292 
293   // If TargetPrefix is specified, make sure that Name starts with
294   // "llvm.<targetprefix>.".
295   if (!TargetPrefix.empty()) {
296     StringRef Prefix = StringRef(Name).drop_front(5); // Drop llvm.
297     if (!Prefix.consume_front(TargetPrefix) || !Prefix.starts_with('.'))
298       PrintFatalError(DefLoc, "Intrinsic '" + DefName +
299                                   "' does not start with 'llvm." +
300                                   TargetPrefix + ".'!");
301   }
302 
303   unsigned NumRet = R->getValueAsListInit("RetTypes")->size();
304   if (NumRet > Ctx.MaxNumReturn)
305     PrintFatalError(DefLoc, "intrinsics can only return upto " +
306                                 Twine(Ctx.MaxNumReturn) + " values, '" +
307                                 DefName + "' returns " + Twine(NumRet) +
308                                 " values");
309 
310   const Record *TypeInfo = R->getValueAsDef("TypeInfo");
311   if (!TypeInfo->isSubClassOf("TypeInfoGen"))
312     PrintFatalError(DefLoc, "TypeInfo field in " + DefName +
313                                 " should be of subclass of TypeInfoGen!");
314 
315   isOverloaded = TypeInfo->getValueAsBit("isOverloaded");
316   const ListInit *TypeList = TypeInfo->getValueAsListInit("Types");
317 
318   // Types field is a concatenation of Return types followed by Param types.
319   unsigned Idx = 0;
320   for (; Idx < NumRet; ++Idx)
321     IS.RetTys.push_back(TypeList->getElementAsRecord(Idx));
322 
323   for (unsigned E = TypeList->size(); Idx < E; ++Idx)
324     IS.ParamTys.push_back(TypeList->getElementAsRecord(Idx));
325 
326   // Parse the intrinsic properties.
327   const ListInit *PropList = R->getValueAsListInit("IntrProperties");
328   for (unsigned i = 0, e = PropList->size(); i != e; ++i) {
329     const Record *Property = PropList->getElementAsRecord(i);
330     assert(Property->isSubClassOf("IntrinsicProperty") &&
331            "Expected a property!");
332 
333     setProperty(Property);
334   }
335 
336   // Set default properties to true.
337   setDefaultProperties(Ctx.DefaultProperties);
338 
339   // Also record the SDPatternOperator Properties.
340   Properties = parseSDPatternOperatorProperties(R);
341 
342   // Sort the argument attributes for later benefit.
343   for (auto &Attrs : ArgumentAttributes)
344     llvm::sort(Attrs);
345 }
346 
347 void CodeGenIntrinsic::setDefaultProperties(
348     ArrayRef<const Record *> DefaultProperties) {
349   // opt-out of using default attributes.
350   if (TheDef->getValueAsBit("DisableDefaultAttributes"))
351     return;
352 
353   for (const Record *Rec : DefaultProperties)
354     setProperty(Rec);
355 }
356 
357 void CodeGenIntrinsic::setProperty(const Record *R) {
358   if (R->getName() == "IntrNoMem")
359     ME = MemoryEffects::none();
360   else if (R->getName() == "IntrReadMem") {
361     if (ME.onlyWritesMemory())
362       PrintFatalError(TheDef->getLoc(),
363                       Twine("IntrReadMem cannot be used after IntrNoMem or "
364                             "IntrWriteMem. Default is ReadWrite"));
365     ME &= MemoryEffects::readOnly();
366   } else if (R->getName() == "IntrWriteMem") {
367     if (ME.onlyReadsMemory())
368       PrintFatalError(TheDef->getLoc(),
369                       Twine("IntrWriteMem cannot be used after IntrNoMem or "
370                             "IntrReadMem. Default is ReadWrite"));
371     ME &= MemoryEffects::writeOnly();
372   } else if (R->getName() == "IntrArgMemOnly")
373     ME &= MemoryEffects::argMemOnly();
374   else if (R->getName() == "IntrInaccessibleMemOnly")
375     ME &= MemoryEffects::inaccessibleMemOnly();
376   else if (R->getName() == "IntrInaccessibleMemOrArgMemOnly")
377     ME &= MemoryEffects::inaccessibleOrArgMemOnly();
378   else if (R->getName() == "Commutative")
379     isCommutative = true;
380   else if (R->getName() == "Throws")
381     canThrow = true;
382   else if (R->getName() == "IntrNoDuplicate")
383     isNoDuplicate = true;
384   else if (R->getName() == "IntrNoMerge")
385     isNoMerge = true;
386   else if (R->getName() == "IntrConvergent")
387     isConvergent = true;
388   else if (R->getName() == "IntrNoReturn")
389     isNoReturn = true;
390   else if (R->getName() == "IntrNoCallback")
391     isNoCallback = true;
392   else if (R->getName() == "IntrNoSync")
393     isNoSync = true;
394   else if (R->getName() == "IntrNoFree")
395     isNoFree = true;
396   else if (R->getName() == "IntrWillReturn")
397     isWillReturn = !isNoReturn;
398   else if (R->getName() == "IntrCold")
399     isCold = true;
400   else if (R->getName() == "IntrSpeculatable")
401     isSpeculatable = true;
402   else if (R->getName() == "IntrHasSideEffects")
403     hasSideEffects = true;
404   else if (R->getName() == "IntrStrictFP")
405     isStrictFP = true;
406   else if (R->isSubClassOf("NoCapture")) {
407     unsigned ArgNo = R->getValueAsInt("ArgNo");
408     addArgAttribute(ArgNo, NoCapture);
409   } else if (R->isSubClassOf("NoAlias")) {
410     unsigned ArgNo = R->getValueAsInt("ArgNo");
411     addArgAttribute(ArgNo, NoAlias);
412   } else if (R->isSubClassOf("NoUndef")) {
413     unsigned ArgNo = R->getValueAsInt("ArgNo");
414     addArgAttribute(ArgNo, NoUndef);
415   } else if (R->isSubClassOf("NonNull")) {
416     unsigned ArgNo = R->getValueAsInt("ArgNo");
417     addArgAttribute(ArgNo, NonNull);
418   } else if (R->isSubClassOf("Returned")) {
419     unsigned ArgNo = R->getValueAsInt("ArgNo");
420     addArgAttribute(ArgNo, Returned);
421   } else if (R->isSubClassOf("ReadOnly")) {
422     unsigned ArgNo = R->getValueAsInt("ArgNo");
423     addArgAttribute(ArgNo, ReadOnly);
424   } else if (R->isSubClassOf("WriteOnly")) {
425     unsigned ArgNo = R->getValueAsInt("ArgNo");
426     addArgAttribute(ArgNo, WriteOnly);
427   } else if (R->isSubClassOf("ReadNone")) {
428     unsigned ArgNo = R->getValueAsInt("ArgNo");
429     addArgAttribute(ArgNo, ReadNone);
430   } else if (R->isSubClassOf("ImmArg")) {
431     unsigned ArgNo = R->getValueAsInt("ArgNo");
432     addArgAttribute(ArgNo, ImmArg);
433   } else if (R->isSubClassOf("Align")) {
434     unsigned ArgNo = R->getValueAsInt("ArgNo");
435     uint64_t Align = R->getValueAsInt("Align");
436     addArgAttribute(ArgNo, Alignment, Align);
437   } else if (R->isSubClassOf("Dereferenceable")) {
438     unsigned ArgNo = R->getValueAsInt("ArgNo");
439     uint64_t Bytes = R->getValueAsInt("Bytes");
440     addArgAttribute(ArgNo, Dereferenceable, Bytes);
441   } else
442     llvm_unreachable("Unknown property!");
443 }
444 
445 bool CodeGenIntrinsic::isParamAPointer(unsigned ParamIdx) const {
446   if (ParamIdx >= IS.ParamTys.size())
447     return false;
448   return (IS.ParamTys[ParamIdx]->isSubClassOf("LLVMQualPointerType") ||
449           IS.ParamTys[ParamIdx]->isSubClassOf("LLVMAnyPointerType"));
450 }
451 
452 bool CodeGenIntrinsic::isParamImmArg(unsigned ParamIdx) const {
453   // Convert argument index to attribute index starting from `FirstArgIndex`.
454   ++ParamIdx;
455   if (ParamIdx >= ArgumentAttributes.size())
456     return false;
457   ArgAttribute Val{ImmArg, 0};
458   return std::binary_search(ArgumentAttributes[ParamIdx].begin(),
459                             ArgumentAttributes[ParamIdx].end(), Val);
460 }
461 
462 void CodeGenIntrinsic::addArgAttribute(unsigned Idx, ArgAttrKind AK,
463                                        uint64_t V) {
464   if (Idx >= ArgumentAttributes.size())
465     ArgumentAttributes.resize(Idx + 1);
466   ArgumentAttributes[Idx].emplace_back(AK, V);
467 }
468