1//===- IntrinsicEmitter.cpp - Generate intrinsic information --------------===//
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 tablegen backend emits information about intrinsic functions.
10//
11//===----------------------------------------------------------------------===//
12
13#include "AttributeTableEmitter.h"
14#include "CodeGenIntrinsics.h"
15#include "SequenceToOffsetTable.h"
16#include "llvm/ADT/STLExtras.h"
17#include "llvm/ADT/SmallVector.h"
18#include "llvm/ADT/StringRef.h"
19#include "llvm/ADT/Twine.h"
20#include "llvm/Support/CommandLine.h"
21#include "llvm/Support/ErrorHandling.h"
22#include "llvm/Support/FormatVariadic.h"
23#include "llvm/Support/ModRef.h"
24#include "llvm/Support/SourceMgr.h"
25#include "llvm/Support/raw_ostream.h"
26#include "llvm/TableGen/CodeGenHelpers.h"
27#include "llvm/TableGen/Error.h"
28#include "llvm/TableGen/Record.h"
29#include "llvm/TableGen/StringToOffsetTable.h"
30#include "llvm/TableGen/TableGenBackend.h"
31#include <algorithm>
32#include <array>
33#include <cassert>
34#include <map>
35#include <optional>
36#include <string>
37#include <utility>
38#include <vector>
39using namespace llvm;
40
41static cl::OptionCategory GenIntrinsicCat("Options for -gen-intrinsic-enums");
42static cl::opt<std::string>
43 IntrinsicPrefix("intrinsic-prefix",
44 cl::desc("Generate intrinsics with this target prefix"),
45 cl::value_desc("target prefix"), cl::cat(GenIntrinsicCat));
46
47namespace {
48class IntrinsicEmitter {
49 const RecordKeeper &Records;
50
51public:
52 IntrinsicEmitter(const RecordKeeper &R) : Records(R) {}
53
54 void run(raw_ostream &OS, bool Enums);
55
56 void EmitEnumInfo(const CodeGenIntrinsicTable &Ints, raw_ostream &OS);
57 void EmitAnyKindEnums(raw_ostream &OS);
58 void EmitIITInfo(raw_ostream &OS);
59 void EmitTargetInfo(const CodeGenIntrinsicTable &Ints, raw_ostream &OS);
60 void EmitIntrinsicToNameTable(const CodeGenIntrinsicTable &Ints,
61 raw_ostream &OS);
62 void EmitIntrinsicToTargetFeaturesTable(const CodeGenIntrinsicTable &Ints,
63 raw_ostream &OS);
64 void EmitIntrinsicToOverloadTable(const CodeGenIntrinsicTable &Ints,
65 raw_ostream &OS);
66 void EmitIntrinsicToScalarizableTable(const CodeGenIntrinsicTable &Ints,
67 raw_ostream &OS);
68 void EmitIntrinsicToPrettyPrintTable(const CodeGenIntrinsicTable &Ints,
69 raw_ostream &OS);
70 void EmitIntrinsicBitTable(
71 const CodeGenIntrinsicTable &Ints, raw_ostream &OS, StringRef Guard,
72 StringRef TableName, StringRef Comment,
73 function_ref<bool(const CodeGenIntrinsic &Int)> GetProperty);
74 void EmitGenerator(const CodeGenIntrinsicTable &Ints, raw_ostream &OS);
75 void EmitAttributes(const CodeGenIntrinsicTable &Ints, raw_ostream &OS);
76 void EmitImmArgRangeSetChecks(const CodeGenIntrinsicTable &Ints,
77 raw_ostream &OS);
78 void EmitPrettyPrintArguments(const CodeGenIntrinsicTable &Ints,
79 raw_ostream &OS);
80 void EmitDefaultArgValuesTable(const CodeGenIntrinsicTable &Ints,
81 raw_ostream &OS);
82 void EmitIntrinsicToBuiltinMap(const CodeGenIntrinsicTable &Ints,
83 bool IsClang, raw_ostream &OS);
84};
85
86// Helper class to use with `TableGen::Emitter::OptClass`.
87template <bool Enums> class IntrinsicEmitterOpt : public IntrinsicEmitter {
88public:
89 IntrinsicEmitterOpt(const RecordKeeper &R) : IntrinsicEmitter(R) {}
90 void run(raw_ostream &OS) { IntrinsicEmitter::run(OS, Enums); }
91};
92
93} // End anonymous namespace
94
95//===----------------------------------------------------------------------===//
96// IntrinsicEmitter Implementation
97//===----------------------------------------------------------------------===//
98
99void IntrinsicEmitter::run(raw_ostream &OS, bool Enums) {
100 emitSourceFileHeader(Desc: "Intrinsic Function Source Fragment", OS);
101
102 CodeGenIntrinsicTable Ints(Records);
103
104 if (Enums) {
105 // Emit the enum information.
106 EmitEnumInfo(Ints, OS);
107
108 // Emit AnyKind enums for Intrinsics.h.
109 EmitAnyKindEnums(OS);
110 } else {
111 // Emit IIT_Info constants.
112 EmitIITInfo(OS);
113
114 // Emit the target metadata.
115 EmitTargetInfo(Ints, OS);
116
117 // Emit the intrinsic ID -> name table.
118 EmitIntrinsicToNameTable(Ints, OS);
119
120 // Emit the intrinsic ID -> required target features table.
121 EmitIntrinsicToTargetFeaturesTable(Ints, OS);
122
123 // Emit the intrinsic ID -> overload table.
124 EmitIntrinsicToOverloadTable(Ints, OS);
125
126 // Emit the intrinsic ID -> trivially scalarizable table.
127 EmitIntrinsicToScalarizableTable(Ints, OS);
128
129 // Emit the intrinsic declaration generator.
130 EmitGenerator(Ints, OS);
131
132 // Emit the intrinsic parameter attributes.
133 EmitAttributes(Ints, OS);
134
135 // Emit immediate argument range-set checks.
136 EmitImmArgRangeSetChecks(Ints, OS);
137
138 // Emit the intrinsic ID -> pretty print table.
139 EmitIntrinsicToPrettyPrintTable(Ints, OS);
140
141 // Emit Pretty Print attribute.
142 EmitPrettyPrintArguments(Ints, OS);
143
144 // Emit the default-argument values table and lookup function.
145 EmitDefaultArgValuesTable(Ints, OS);
146
147 // Emit code to translate Clang builtins into LLVM intrinsics.
148 EmitIntrinsicToBuiltinMap(Ints, IsClang: true, OS);
149
150 // Emit code to translate MS builtins into LLVM intrinsics.
151 EmitIntrinsicToBuiltinMap(Ints, IsClang: false, OS);
152 }
153}
154
155void IntrinsicEmitter::EmitEnumInfo(const CodeGenIntrinsicTable &Ints,
156 raw_ostream &OS) {
157 // Find the TargetSet for which to generate enums. There will be an initial
158 // set with an empty target prefix which will include target independent
159 // intrinsics like dbg.value.
160 using TargetSet = CodeGenIntrinsicTable::TargetSet;
161 const TargetSet *Set = nullptr;
162 for (const auto &Target : Ints.getTargets()) {
163 if (Target.Name == IntrinsicPrefix) {
164 Set = &Target;
165 break;
166 }
167 }
168 if (!Set) {
169 // The first entry is for target independent intrinsics, so drop it.
170 auto KnowTargets = Ints.getTargets().drop_front();
171 PrintFatalError(PrintMsg: [KnowTargets](raw_ostream &OS) {
172 OS << "tried to generate intrinsics for unknown target "
173 << IntrinsicPrefix << "\nKnown targets are: ";
174 interleaveComma(c: KnowTargets, os&: OS,
175 each_fn: [&OS](const TargetSet &Target) { OS << Target.Name; });
176 OS << '\n';
177 });
178 }
179
180 // Generate a complete header for target specific intrinsics.
181 std::optional<IfDefEmitter> IfDef;
182 std::optional<IncludeGuardEmitter> IncGuard;
183 std::optional<NamespaceEmitter> NS;
184
185 if (IntrinsicPrefix.empty()) {
186 IfDef.emplace(args&: OS, args: "GET_INTRINSIC_ENUM_VALUES");
187 } else {
188 std::string UpperPrefix = StringRef(IntrinsicPrefix).upper();
189 IncGuard.emplace(
190 args&: OS, args: formatv(Fmt: "LLVM_IR_INTRINSIC_{}_ENUMS_H", Vals&: UpperPrefix).str());
191 NS.emplace(args&: OS, args: "llvm::Intrinsic");
192 OS << formatv(Fmt: "enum {}Intrinsics : unsigned {{\n", Vals&: UpperPrefix);
193 }
194
195 OS << "// Enum values for intrinsics.\n";
196 bool First = true;
197 for (const CodeGenIntrinsic &Int : Ints[*Set]) {
198 OS << " " << Int.EnumName;
199
200 // Assign a value to the first intrinsic in this target set so that all
201 // intrinsic ids are distinct.
202 if (First) {
203 OS << " = " << Set->Offset + 1;
204 First = false;
205 }
206
207 OS << ", ";
208 if (Int.EnumName.size() < 40)
209 OS.indent(NumSpaces: 40 - Int.EnumName.size());
210 OS << formatv(
211 Fmt: " // {} ({})\n", Vals: Int.Name,
212 Vals: SrcMgr.getFormattedLocationNoOffset(Loc: Int.TheDef->getLoc().front()));
213 }
214
215 // Emit num_intrinsics into the target neutral enum.
216 if (IntrinsicPrefix.empty())
217 OS << formatv(Fmt: " num_intrinsics = {}\n", Vals: Ints.size() + 1);
218 else
219 OS << "}; // enum\n";
220}
221
222void IntrinsicEmitter::EmitAnyKindEnums(raw_ostream &OS) {
223 if (!IntrinsicPrefix.empty())
224 return;
225 IfDefEmitter IfDef(OS, "GET_INTRINSIC_ANYKIND_ENUMS");
226
227 auto GenerateAnyKindEnums = [&OS, this](StringRef EnumName,
228 StringRef Prefix) {
229 OS << "// llvm::Intrinsic::IITDescriptor::" << EnumName << "\n";
230 if (const Record *EnumDef = Records.getDef(Name: EnumName)) {
231 OS << "enum " << EnumName << " {\n";
232 for (const auto &RV : EnumDef->getValues())
233 OS << " " << Prefix << RV.getName() << " = " << *RV.getValue()
234 << ",\n";
235 OS << "}; // " << EnumName << "\n\n";
236 } else {
237 OS << "#error \"" << EnumName << " is not defined\"\n";
238 }
239 };
240 GenerateAnyKindEnums("AnyKindVectorConstraint", "VC_");
241 GenerateAnyKindEnums("AnyKindElementConstraint", "EC_");
242}
243
244void IntrinsicEmitter::EmitIITInfo(raw_ostream &OS) {
245 IfDefEmitter IfDef(OS, "GET_INTRINSIC_IITINFO");
246 std::array<StringRef, 256> RecsByNumber;
247 auto IIT_Base = Records.getAllDerivedDefinitionsIfDefined(ClassName: "IIT_Base");
248 for (const Record *Rec : IIT_Base) {
249 auto Number = Rec->getValueAsInt(FieldName: "Number");
250 assert(0 <= Number && Number < (int)RecsByNumber.size() &&
251 "IIT_Info.Number should be uint8_t");
252 assert(RecsByNumber[Number].empty() && "Duplicate IIT_Info.Number");
253 RecsByNumber[Number] = Rec->getName();
254 }
255 if (IIT_Base.size() > 0) {
256 if (RecsByNumber[0] != "IIT_Done")
257 PrintFatalError(Msg: "IIT_Done expected to have value 0");
258 for (unsigned I = 0, E = RecsByNumber.size(); I < E; ++I)
259 if (!RecsByNumber[I].empty())
260 OS << " " << RecsByNumber[I] << " = " << I << ",\n";
261 } else {
262 OS << "#error \"class IIT_Base is not defined\"\n";
263 }
264}
265
266void IntrinsicEmitter::EmitTargetInfo(const CodeGenIntrinsicTable &Ints,
267 raw_ostream &OS) {
268 IfDefEmitter IfDef(OS, "GET_INTRINSIC_TARGET_DATA");
269 OS << R"(// Target mapping.
270struct IntrinsicTargetInfo {
271 StringLiteral Name;
272 size_t Offset;
273 size_t Count;
274};
275static constexpr IntrinsicTargetInfo TargetInfos[] = {
276)";
277 for (const auto [Name, Offset, Count] : Ints.getTargets())
278 OS << formatv(Fmt: " {{\"{}\", {}, {}},\n", Vals: Name, Vals: Offset, Vals: Count);
279 OS << "};\n";
280}
281
282/// Helper function to emit a bit table for intrinsic properties.
283/// This is used for both overload and pretty print bit tables.
284void IntrinsicEmitter::EmitIntrinsicBitTable(
285 const CodeGenIntrinsicTable &Ints, raw_ostream &OS, StringRef Guard,
286 StringRef TableName, StringRef Comment,
287 function_ref<bool(const CodeGenIntrinsic &Int)> GetProperty) {
288 IfDefEmitter IfDef(OS, Guard);
289 OS << formatv(Fmt: "// {}\n", Vals&: Comment);
290 OS << formatv(Fmt: "static constexpr uint8_t {}[] = {{\n", Vals&: TableName);
291 OS << " 0\n ";
292 for (auto [I, Int] : enumerate(First: Ints)) {
293 // Add one to the index so we emit a null bit for the invalid #0 intrinsic.
294 size_t Idx = I + 1;
295 if (Idx % 8 == 0)
296 OS << ",\n 0";
297 if (GetProperty(Int))
298 OS << " | (1<<" << Idx % 8 << ')';
299 }
300 OS << "\n};\n\n";
301 OS << formatv(Fmt: "return ({}[id/8] & (1 << (id%8))) != 0;\n", Vals&: TableName);
302}
303
304void IntrinsicEmitter::EmitIntrinsicToNameTable(
305 const CodeGenIntrinsicTable &Ints, raw_ostream &OS) {
306 // Built up a table of the intrinsic names.
307 constexpr StringLiteral NotIntrinsic = "not_intrinsic";
308 StringToOffsetTable Table;
309 Table.GetOrAddStringOffset(Str: NotIntrinsic);
310 for (const auto &Int : Ints)
311 Table.GetOrAddStringOffset(Str: Int.Name);
312
313 IfDefEmitter IfDef(OS, "GET_INTRINSIC_NAME_TABLE");
314 OS << R"(// Intrinsic ID to name table.
315// Note that entry #0 is the invalid intrinsic!
316
317)";
318
319 Table.EmitStringTableDef(OS, Name: "IntrinsicNameTable");
320
321 OS << R"(
322static constexpr unsigned IntrinsicNameOffsetTable[] = {
323)";
324
325 OS << formatv(Fmt: " {}, // {}\n", Vals: Table.GetStringOffset(Str: NotIntrinsic),
326 Vals: NotIntrinsic);
327 for (const auto &Int : Ints)
328 OS << formatv(Fmt: " {}, // {}\n", Vals: Table.GetStringOffset(Str: Int.Name), Vals: Int.Name);
329
330 OS << "\n}; // IntrinsicNameOffsetTable\n";
331}
332
333void IntrinsicEmitter::EmitIntrinsicToTargetFeaturesTable(
334 const CodeGenIntrinsicTable &Ints, raw_ostream &OS) {
335 StringToOffsetTable Table;
336 for (const CodeGenIntrinsic &Int : Ints)
337 Table.GetOrAddStringOffset(Str: Int.TargetFeatures);
338
339 IfDefEmitter IfDef(OS, "GET_INTRINSIC_TARGET_FEATURES_TABLE");
340 OS << R"(// Intrinsic ID to required target features table.
341// Note that entry #0 is the invalid intrinsic!
342
343)";
344
345 Table.EmitStringTableDef(OS, Name: "IntrinsicTargetFeaturesTable");
346
347 OS << R"(
348static constexpr unsigned IntrinsicTargetFeaturesOffsetTable[] = {
349)";
350
351 OS << " 0, // not_intrinsic\n";
352 for (const CodeGenIntrinsic &Int : Ints) {
353 OS << formatv(Fmt: " {}, // {}\n", Vals: *Table.GetStringOffset(Str: Int.TargetFeatures),
354 Vals: Int.Name);
355 }
356
357 OS << "\n}; // IntrinsicTargetFeaturesOffsetTable\n";
358}
359
360void IntrinsicEmitter::EmitIntrinsicToOverloadTable(
361 const CodeGenIntrinsicTable &Ints, raw_ostream &OS) {
362 EmitIntrinsicBitTable(
363 Ints, OS, Guard: "GET_INTRINSIC_OVERLOAD_TABLE", TableName: "OTable",
364 Comment: "Intrinsic ID to overload bitset.",
365 GetProperty: [](const CodeGenIntrinsic &Int) { return Int.isOverloaded; });
366}
367
368void IntrinsicEmitter::EmitIntrinsicToScalarizableTable(
369 const CodeGenIntrinsicTable &Ints, raw_ostream &OS) {
370 EmitIntrinsicBitTable(
371 Ints, OS, Guard: "GET_INTRINSIC_SCALARIZABLE_TABLE", TableName: "STable",
372 Comment: "Intrinsic ID to trivially scalarizable bitset.",
373 GetProperty: [](const CodeGenIntrinsic &Int) { return Int.isTriviallyScalarizable; });
374}
375
376using TypeSigTy = SmallVector<unsigned char>;
377
378/// Computes type signature of the intrinsic \p Int.
379static TypeSigTy ComputeTypeSignature(const CodeGenIntrinsic &Int) {
380 TypeSigTy TypeSig;
381 const Record *TypeInfo = Int.TheDef->getValueAsDef(FieldName: "TypeInfo");
382 const ListInit *TypeList = TypeInfo->getValueAsListInit(FieldName: "TypeSig");
383
384 for (const auto *TypeListEntry : TypeList->getElements()) {
385 int64_t Value = cast<IntInit>(Val: TypeListEntry)->getValue();
386 if (Value < 0 || Value > 255)
387 PrintFatalError(Rec: Int.TheDef, Msg: "Unresolved type signature");
388 TypeSig.emplace_back(Args&: Value);
389 }
390 return TypeSig;
391}
392
393// Note: the code below can be switched to use 32-bit fixed encoding by
394// flipping the flag below.
395constexpr bool Use16BitFixedEncoding = true;
396using FixedEncodingTy =
397 std::conditional_t<Use16BitFixedEncoding, uint16_t, uint32_t>;
398
399// Pack the type signature into 16/32-bit fixed encoding word, where each byte
400// in the type signature is packed into a nibble (4 bits) if possible.
401static std::optional<FixedEncodingTy> encodePacked(const TypeSigTy &TypeSig) {
402 constexpr size_t NUM_NIBBLES = sizeof(FixedEncodingTy) * 2;
403 if (TypeSig.size() > NUM_NIBBLES)
404 return std::nullopt;
405
406 FixedEncodingTy Result = 0;
407 for (unsigned char C : reverse(C: TypeSig)) {
408 if (C > 15)
409 return std::nullopt;
410 Result = (Result << 4) | C;
411 }
412 return Result;
413}
414
415void IntrinsicEmitter::EmitGenerator(const CodeGenIntrinsicTable &Ints,
416 raw_ostream &OS) {
417 constexpr unsigned FixedEncodingBits = sizeof(FixedEncodingTy) * CHAR_BIT;
418 constexpr unsigned MSBPosition = FixedEncodingBits - 1;
419 // Mask with all bits 1 except the most significant bit.
420 constexpr FixedEncodingTy Mask = (1U << MSBPosition) - 1;
421 StringRef FixedEncodingTypeName =
422 Use16BitFixedEncoding ? "uint16_t" : "uint32_t";
423
424 // If we can compute a 16/32-bit fixed encoding for this intrinsic, do so and
425 // capture it in this vector, otherwise store a ~0U.
426 std::vector<FixedEncodingTy> FixedEncodings;
427
428 // Each IIT encoding sequence in the long encoding table is terminated by
429 // IIT_Done(=0) token.
430 constexpr unsigned char IIT_Done = 0;
431 SequenceToOffsetTable<TypeSigTy> LongEncodingTable(IIT_Done);
432
433 FixedEncodings.reserve(n: Ints.size());
434
435 // Compute the unique argument type info.
436 for (const CodeGenIntrinsic &Int : Ints) {
437 // Get the signature for the intrinsic.
438 TypeSigTy TypeSig = ComputeTypeSignature(Int);
439
440 // Check to see if we can encode it into a 16/32 bit word.
441 std::optional<FixedEncodingTy> Result = encodePacked(TypeSig);
442 if (Result && (*Result & Mask) == *Result) {
443 FixedEncodings.push_back(x: *Result);
444 continue;
445 }
446
447 LongEncodingTable.add(Seq: TypeSig);
448
449 // This is a placehold that we'll replace after the table is laid out.
450 FixedEncodings.push_back(x: static_cast<FixedEncodingTy>(~0U));
451 }
452
453 LongEncodingTable.layout();
454
455 IfDefEmitter IfDef(OS, "GET_INTRINSIC_GENERATOR_GLOBAL");
456 OS << formatv(Fmt: R"(// Global intrinsic function declaration type table.
457using FixedEncodingTy = {};
458static constexpr FixedEncodingTy IIT_Table[] = {{
459 )",
460 Vals&: FixedEncodingTypeName);
461
462 unsigned MaxOffset = 0;
463 for (auto [Idx, FixedEncoding, Int] : enumerate(First&: FixedEncodings, Rest: Ints)) {
464 if ((Idx & 7) == 7)
465 OS << "\n ";
466
467 // If the entry fit in the table, just emit it.
468 if ((FixedEncoding & Mask) == FixedEncoding) {
469 OS << "0x" << Twine::utohexstr(Val: FixedEncoding) << ", ";
470 continue;
471 }
472
473 TypeSigTy TypeSig = ComputeTypeSignature(Int);
474 unsigned Offset = LongEncodingTable.get(Seq: TypeSig);
475 MaxOffset = std::max(a: MaxOffset, b: Offset);
476
477 // Otherwise, emit the offset into the long encoding table. We emit it this
478 // way so that it is easier to read the offset in the .def file.
479 OS << formatv(Fmt: "(1U<<{}) | {}, ", Vals: MSBPosition, Vals&: Offset);
480 }
481
482 OS << "0\n};\n\n";
483
484 // verify that all offsets will fit in 16/32 bits.
485 if ((MaxOffset & Mask) != MaxOffset)
486 PrintFatalError(Msg: "Offset of long encoding table exceeds encoding bits");
487
488 // Emit the shared table of register lists.
489 OS << "static constexpr unsigned char IIT_LongEncodingTable[] = {\n";
490 if (!LongEncodingTable.empty())
491 LongEncodingTable.emit(
492 OS, Print: [](raw_ostream &OS, unsigned char C) { OS << (unsigned)C; });
493 OS << " 255\n};\n";
494}
495
496/// Returns the effective MemoryEffects for intrinsic \p Int.
497static MemoryEffects getEffectiveME(const CodeGenIntrinsic &Int) {
498 MemoryEffects ME = Int.ME;
499 // TODO: IntrHasSideEffects should affect not only readnone intrinsics.
500 if (ME.doesNotAccessMemory() && Int.hasSideEffects)
501 ME = MemoryEffects::unknown();
502 return ME;
503}
504
505/// Returns true if \p Int has a non-empty set of function attributes.
506static bool hasFnAttributes(const CodeGenIntrinsic &Int) {
507 return !Int.FnAttributes.empty() ||
508 getEffectiveME(Int) != MemoryEffects::unknown();
509}
510
511using ArgAttrSet = SmallVector<CodeGenIntrinsic::ArgAttribute, 0>;
512
513namespace {
514/// The function attributes and effective memory effects of an intrinsic.
515struct FnAttrSet {
516 SmallVector<const Record *, 8> Attrs;
517 MemoryEffects ME;
518
519 explicit FnAttrSet(const CodeGenIntrinsic &Int)
520 : Attrs(Int.FnAttributes), ME(getEffectiveME(Int)) {}
521
522 bool operator<(const FnAttrSet &RHS) const {
523 if (Attrs != RHS.Attrs) {
524 return std::lexicographical_compare(first1: Attrs.begin(), last1: Attrs.end(),
525 first2: RHS.Attrs.begin(), last2: RHS.Attrs.end(),
526 comp: LessRecordByID());
527 }
528
529 return ME.toIntValue() < RHS.ME.toIntValue();
530 }
531
532 bool empty() const { return Attrs.empty() && ME == MemoryEffects::unknown(); }
533};
534} // namespace
535
536static void printArgAttributes(raw_ostream &OS, const ArgAttrSet &Attrs) {
537 assert(is_sorted(Attrs) && "Argument attributes are not sorted");
538 for (const CodeGenIntrinsic::ArgAttribute &Attr : Attrs) {
539 StringRef AttrName = Attr.Attr->getName();
540 if (Attr.Attr->isSubClassOf(Name: "IntAttr")) {
541 OS.indent(NumSpaces: 6) << formatv(
542 Fmt: "Attribute::get(C, Attribute::{}, uint64_t({})),\n", Vals&: AttrName,
543 Vals: Attr.Value);
544 } else if (Attr.Attr->isSubClassOf(Name: "ConstantRangeAttr")) {
545 // This allows implicitTrunc because the range may only fit the
546 // type based on rules implemented in the IR verifier. E.g. the
547 // [-1, 1] range for ucmp/scmp intrinsics requires a minimum i2 type.
548 // Give the verifier a chance to diagnose this instead of asserting
549 // here.
550 OS.indent(NumSpaces: 6) << formatv(
551 Fmt: "Attribute::get(C, Attribute::{}, "
552 "ConstantRange(APInt(BitWidth, {}, /*isSigned=*/true, "
553 "/*implicitTrunc=*/true), APInt(BitWidth, {}, "
554 "/*isSigned=*/true, /*implicitTrunc=*/true))),\n",
555 Vals&: AttrName, Vals: (int64_t)Attr.Value, Vals: (int64_t)Attr.Value2);
556 } else {
557 OS.indent(NumSpaces: 6) << formatv(Fmt: "Attribute::get(C, Attribute::{}),\n", Vals&: AttrName);
558 }
559 }
560}
561
562static void printFnAttributes(raw_ostream &OS, const FnAttrSet &FnAttrs) {
563 for (const Record *Attr : FnAttrs.Attrs) {
564 OS.indent(NumSpaces: 6) << formatv(Fmt: "Attribute::get(C, Attribute::{}),\n",
565 Vals: Attr->getName());
566 }
567
568 if (FnAttrs.ME != MemoryEffects::unknown()) {
569 OS.indent(NumSpaces: 6) << formatv(Fmt: "// {}\n", Vals: FnAttrs.ME);
570 OS.indent(NumSpaces: 6) << formatv(Fmt: "Attribute::getWithMemoryEffects(C, "
571 "MemoryEffects::createFromIntValue({})),\n",
572 Vals: FnAttrs.ME.toIntValue());
573 }
574}
575
576/// EmitAttributes - This emits the Intrinsic::getAttributes method.
577void IntrinsicEmitter::EmitAttributes(const CodeGenIntrinsicTable &Ints,
578 raw_ostream &OS) {
579 IfDefEmitter IfDef(OS, "GET_INTRINSIC_ATTRIBUTES");
580
581 // Compute unique argument attribute sets, function attribute sets, and the
582 // argument attribute sequence of each intrinsic.
583 //
584 // For a given intrinsic, its attributes are constructed by populating the
585 // local array `AS` below with its non-empty argument attributes followed by
586 // function attributes if any. Each argument attribute is constructed as:
587 //
588 // getIntrinsicArgAttributeSet(C, ArgAttrID, FT->getContainedType(ArgNo));
589 //
590 // The sequences for all intrinsics are concatenated into one large table,
591 // and for each intrinsic we remember the starting index and number of
592 // entries of its slice, so that we can build the attribute list for an
593 // intrinsic without using a switch-case.
594 using ArgNoAttrIDPair = std::pair<uint16_t, uint16_t>;
595 AttributeSetTable<ArgAttrSet> ArgAttrSets;
596 AttributeSetTable<FnAttrSet> FnAttrSets;
597 std::vector<StringRef> FnAttrSetNames;
598 AttributeSequenceTable<ArgNoAttrIDPair> ArgAttrSeqs;
599 std::vector<std::pair<unsigned, unsigned>> IntrinsicAttrIDs;
600
601 // Find the max number of attributes to create the local array.
602 unsigned MaxNumAttrs = 0;
603 for (const CodeGenIntrinsic &Int : Ints) {
604 AttributeSequenceTable<ArgNoAttrIDPair>::Sequence Seq;
605 for (const auto &[ArgNo, Attrs] : enumerate(First: Int.ArgumentAttributes)) {
606 if (Attrs.empty())
607 continue;
608
609 uint16_t ArgAttrID = ArgAttrSets.add(Attrs: ArgAttrSet(Attrs));
610 Seq.emplace_back(Args: (uint16_t)ArgNo, Args&: ArgAttrID);
611 }
612
613 unsigned NumAttrs = Seq.size() + hasFnAttributes(Int);
614 MaxNumAttrs = std::max(a: MaxNumAttrs, b: NumAttrs);
615 unsigned SeqID = ArgAttrSeqs.add(Seq: std::move(Seq));
616 unsigned FnAttrID = ~0U;
617 if (hasFnAttributes(Int)) {
618 FnAttrID = FnAttrSets.add(Attrs: FnAttrSet(Int));
619 if (FnAttrID == FnAttrSetNames.size())
620 FnAttrSetNames.push_back(x: Int.Name);
621 }
622
623 IntrinsicAttrIDs.emplace_back(args&: FnAttrID, args&: SeqID);
624 }
625
626 ArgAttrSeqs.layout();
627
628 if (ArgAttrSeqs.size() >= std::numeric_limits<uint16_t>::max())
629 PrintFatalError(Msg: "Size of ArgAttrIdTable exceeds supported limit");
630
631 OS << R"(// Add parameter attributes that are not common to all intrinsics.
632static AttributeSet getIntrinsicArgAttributeSet(LLVMContext &C, unsigned ID,
633 Type *ArgType) {
634 unsigned BitWidth = ArgType->getScalarSizeInBits();
635 switch (ID) {
636 default: llvm_unreachable("Invalid attribute set number");)";
637 ArgAttrSets.emitCases(OS, Print: printArgAttributes);
638 OS << R"(
639 }
640} // getIntrinsicArgAttributeSet
641)";
642
643 OS << R"(
644static AttributeSet getIntrinsicFnAttributeSet(LLVMContext &C, unsigned ID) {
645 switch (ID) {
646 default: llvm_unreachable("Invalid attribute set number");)";
647 FnAttrSets.emitCases(
648 OS, Print: printFnAttributes,
649 WriteComment: [&](raw_ostream &OS, unsigned ID) { OS << FnAttrSetNames[ID]; });
650 OS << R"(
651 }
652} // getIntrinsicFnAttributeSet)";
653
654 const uint8_t UniqAttributesBitSize =
655 Log2_32_Ceil(Value: ArgAttrSeqs.numSequences());
656 // Note, max value is used to indicate no function attributes.
657 const uint8_t UniqFnAttributesBitSize = Log2_32_Ceil(Value: FnAttrSets.size() + 1);
658 const uint32_t NoFunctionAttrsID =
659 maskTrailingOnes<uint32_t>(N: UniqFnAttributesBitSize);
660 uint8_t AttributesMapDataBitSize =
661 PowerOf2Ceil(A: UniqAttributesBitSize + UniqFnAttributesBitSize);
662 if (AttributesMapDataBitSize < 8)
663 AttributesMapDataBitSize = 8;
664 else if (AttributesMapDataBitSize > 64)
665 PrintFatalError(Msg: "Packed ID of IntrinsicsToAttributesMap exceeds 64b!");
666
667 // Assign a packed ID for each intrinsic. The lower bits will be its
668 // argument attribute sequence ID and the upper bits its function attribute
669 // set ID.
670 OS << formatv(Fmt: "\nstatic constexpr uint{}_t IntrinsicsToAttributesMap[] = {{",
671 Vals&: AttributesMapDataBitSize);
672 for (const auto &[Int, IDs] : zip_equal(t: Ints, u&: IntrinsicAttrIDs)) {
673 auto [FnAttrID, SeqID] = IDs;
674 uint32_t FnAttrIndex = FnAttrID == ~0U ? NoFunctionAttrsID : FnAttrID;
675 OS << formatv(Fmt: "\n {} << {} | {}, // {}", Vals&: FnAttrIndex,
676 Vals: UniqAttributesBitSize, Vals&: SeqID, Vals: Int.Name);
677 }
678
679 OS << R"(
680}; // IntrinsicsToAttributesMap
681)";
682
683 // Emit the 2 tables (flattened ArgNo, ArgAttrID) and ArgAttributesInfoTable.
684 OS << formatv(Fmt: R"(
685namespace {{
686struct ArgNoAttrIDPair {{
687 uint16_t ArgNo, ArgAttrID;
688};
689} // namespace
690
691// Number of entries: {}
692static constexpr ArgNoAttrIDPair ArgAttrIdTable[] = {{
693)",
694 Vals: ArgAttrSeqs.size());
695
696 ArgAttrSeqs.emitEntries(OS, Print: [](raw_ostream &OS, ArgNoAttrIDPair Elem) {
697 OS << formatv(Fmt: "{{{}, {}}", Vals&: Elem.first, Vals&: Elem.second);
698 });
699
700 OS << formatv(Fmt: R"(}; // ArgAttrIdTable
701
702namespace {{
703struct ArgAttributesInfo {{
704 uint16_t StartIndex;
705 uint16_t NumAttrs;
706};
707} // namespace
708
709// Number of entries: {}
710static constexpr ArgAttributesInfo ArgAttributesInfoTable[] = {{
711)",
712 Vals: ArgAttrSeqs.numSequences());
713
714 ArgAttrSeqs.emitInfo(OS);
715 OS << "}; // ArgAttributesInfoTable\n";
716
717 // Now emit the Intrinsic::getAttributes function. This will first map
718 // from intrinsic ID -> argument attribute sequence ID and function attribute
719 // set ID (using the IntrinsicsToAttributesMap table). Then it will use the
720 // sequence ID to construct all the argument attributes (using the
721 // ArgAttributesInfoTable and ArgAttrIdTable) and then add on the function
722 // attributes if any.
723 OS << formatv(Fmt: R"(
724
725template <typename IDTy>
726inline std::pair<uint32_t, uint32_t> unpackID(const IDTy PackedID) {{
727 constexpr uint8_t UniqAttributesBitSize = {};
728 const uint32_t FnAttrID = PackedID >> UniqAttributesBitSize;
729 const uint32_t ArgAttrID = PackedID &
730 maskTrailingOnes<uint32_t>(UniqAttributesBitSize);
731 return {{FnAttrID, ArgAttrID};
732}
733
734AttributeList Intrinsic::getAttributes(LLVMContext &C, ID id,
735 FunctionType *FT) {{
736 if (id == 0)
737 return AttributeList();
738 auto [FnAttrID, ArgAttrID] = unpackID(IntrinsicsToAttributesMap[id - 1]);
739 using PairTy = std::pair<unsigned, AttributeSet>;
740 alignas(PairTy) char ASStorage[sizeof(PairTy) * {}];
741 PairTy *AS = reinterpret_cast<PairTy *>(ASStorage);
742
743 // Construct an ArrayRef for easier range checking.
744 ArrayRef<ArgAttributesInfo> ArgAttributesInfoTableAR(ArgAttributesInfoTable);
745 if (ArgAttrID >= ArgAttributesInfoTableAR.size())
746 llvm_unreachable("Invalid arguments attribute ID");
747
748 auto [StartIndex, NumAttrs] = ArgAttributesInfoTableAR[ArgAttrID];
749 for (unsigned Idx = 0; Idx < NumAttrs; ++Idx) {{
750 auto [ArgNo, ArgAttrID] = ArgAttrIdTable[StartIndex + Idx];
751 AS[Idx] = {{ArgNo,
752 getIntrinsicArgAttributeSet(C, ArgAttrID, FT->getContainedType(ArgNo))};
753 }
754 if (FnAttrID != {}) {
755 AS[NumAttrs++] = {{AttributeList::FunctionIndex,
756 getIntrinsicFnAttributeSet(C, FnAttrID)};
757 }
758 return AttributeList::get(C, ArrayRef(AS, NumAttrs));
759}
760
761AttributeSet Intrinsic::getFnAttributes(LLVMContext &C, ID id) {{
762 if (id == 0)
763 return AttributeSet();
764 auto [FnAttrID, _] = unpackID(IntrinsicsToAttributesMap[id - 1]);
765 if (FnAttrID == {})
766 return AttributeSet();
767 return getIntrinsicFnAttributeSet(C, FnAttrID);
768}
769)",
770 Vals: UniqAttributesBitSize, Vals&: MaxNumAttrs, Vals: NoFunctionAttrsID,
771 Vals: NoFunctionAttrsID);
772}
773
774void IntrinsicEmitter::EmitImmArgRangeSetChecks(
775 const CodeGenIntrinsicTable &Ints, raw_ostream &OS) {
776 using ImmArgRangeSet = CodeGenIntrinsic::ImmArgRangeSet;
777 struct RangeSetInfo {
778 uint32_t ArgNo;
779 uint32_t RangesStart;
780 uint32_t NumRanges;
781 };
782 struct RangeSetListInfo {
783 uint32_t RangeSetsStart;
784 uint32_t NumRangeSets;
785 };
786
787 SequenceToOffsetTable<ImmArgRangeSet::RangeList> RangeTable(
788 /*Terminator=*/std::nullopt);
789
790 for (const CodeGenIntrinsic &Int : Ints) {
791 for (const ImmArgRangeSet &RangeSet : Int.ImmArgRangeSets)
792 RangeTable.add(Seq: RangeSet.Ranges);
793 }
794
795 IfDefEmitter IfDef(OS, "GET_INTRINSIC_IMMARG_RANGE_SET_CHECKS");
796 if (RangeTable.empty()) {
797 OS << R"(
798bool Intrinsic::isImmArgValueInRangeSet(ID IID, unsigned ArgIdx,
799 const APInt &Value) {
800 return true;
801}
802)";
803 return;
804 }
805
806 RangeTable.layout();
807
808 std::vector<RangeSetInfo> RangeSetInfos;
809 std::vector<RangeSetListInfo> PerIntrinsicRangeSetInfos;
810 PerIntrinsicRangeSetInfos.reserve(n: Ints.size() + 1);
811 PerIntrinsicRangeSetInfos.push_back(x: {.RangeSetsStart: 0, .NumRangeSets: 0}); // not_intrinsic
812
813 for (const CodeGenIntrinsic &Int : Ints) {
814 uint32_t Start = static_cast<uint32_t>(RangeSetInfos.size());
815 for (const ImmArgRangeSet &RangeSet : Int.ImmArgRangeSets)
816 RangeSetInfos.push_back(x: {.ArgNo: RangeSet.ArgNo, .RangesStart: RangeTable.get(Seq: RangeSet.Ranges),
817 .NumRanges: static_cast<uint32_t>(RangeSet.Ranges.size())});
818 PerIntrinsicRangeSetInfos.push_back(
819 x: {.RangeSetsStart: Start, .NumRangeSets: static_cast<uint32_t>(Int.ImmArgRangeSets.size())});
820 }
821
822 OS << R"(
823namespace {
824struct ImmArgRange {
825 int64_t Lower, Upper;
826};
827
828struct ImmArgRangeSet {
829 uint32_t ArgIdx;
830 uint32_t RangesStart;
831 uint32_t NumRanges;
832};
833
834struct ImmArgRangeSetList {
835 uint32_t RangeSetsStart;
836 uint32_t NumRangeSets;
837};
838} // namespace
839
840static constexpr ImmArgRange ImmArgRangeTable[] = {
841)";
842 RangeTable.emit(OS, Print: [](raw_ostream &OS, ImmArgRangeSet::Range Elem) {
843 OS << "{" << Elem.first << ", " << Elem.second << "}";
844 });
845 OS << R"(}; // ImmArgRangeTable
846
847static constexpr ImmArgRangeSet ImmArgRangeSetTable[] = {
848)";
849 for (const RangeSetInfo &Info : RangeSetInfos)
850 OS << " {" << Info.ArgNo << ", " << Info.RangesStart << ", "
851 << Info.NumRanges << "},\n";
852 OS << R"(}; // ImmArgRangeSetTable
853
854static constexpr ImmArgRangeSetList ImmArgRangeSetListTable[] = {
855)";
856 for (const auto &[Idx, Infos] : enumerate(First&: PerIntrinsicRangeSetInfos)) {
857 OS << " {" << Infos.RangeSetsStart << ", " << Infos.NumRangeSets << "},";
858 if (Idx == 0)
859 OS << " // not_intrinsic\n";
860 else
861 OS << " // " << Ints[Idx - 1].Name << "\n";
862 }
863 OS << R"(}; // ImmArgRangeSetListTable
864
865static bool isValueInImmArgRange(const ImmArgRange &Range,
866 const APInt &Value) {
867 unsigned BitWidth = Value.getBitWidth();
868 return ConstantRange(
869 APInt(BitWidth, Range.Lower, /*isSigned=*/true,
870 /*implicitTrunc=*/true),
871 APInt(BitWidth, Range.Upper, /*isSigned=*/true,
872 /*implicitTrunc=*/true))
873 .contains(Value);
874}
875
876bool Intrinsic::isImmArgValueInRangeSet(ID IID, unsigned ArgIdx,
877 const APInt &Value) {
878 if (IID >= Intrinsic::num_intrinsics)
879 return true;
880
881 const ImmArgRangeSetList &List = ImmArgRangeSetListTable[IID];
882 ArrayRef<ImmArgRangeSet> RangeSets(
883 ImmArgRangeSetTable + List.RangeSetsStart, List.NumRangeSets);
884 for (const ImmArgRangeSet &RangeSet : RangeSets) {
885 if (RangeSet.ArgIdx != ArgIdx)
886 continue;
887
888 ArrayRef<ImmArgRange> Ranges(ImmArgRangeTable + RangeSet.RangesStart,
889 RangeSet.NumRanges);
890 for (const ImmArgRange &Range : Ranges) {
891 if (isValueInImmArgRange(Range, Value))
892 return true;
893 }
894 return false;
895 }
896 return true;
897}
898)";
899}
900
901void IntrinsicEmitter::EmitIntrinsicToPrettyPrintTable(
902 const CodeGenIntrinsicTable &Ints, raw_ostream &OS) {
903 EmitIntrinsicBitTable(Ints, OS, Guard: "GET_INTRINSIC_PRETTY_PRINT_TABLE", TableName: "PPTable",
904 Comment: "Intrinsic ID to pretty print bitset.",
905 GetProperty: [](const CodeGenIntrinsic &Int) {
906 return !Int.PrettyPrintFunctions.empty();
907 });
908}
909
910void IntrinsicEmitter::EmitPrettyPrintArguments(
911 const CodeGenIntrinsicTable &Ints, raw_ostream &OS) {
912 IfDefEmitter IfDef(OS, "GET_INTRINSIC_PRETTY_PRINT_ARGUMENTS");
913 OS << R"(
914void Intrinsic::printImmArg(ID IID, unsigned ArgIdx, raw_ostream &OS, const Constant *ImmArgVal) {
915 using namespace Intrinsic;
916 switch (IID) {
917)";
918
919 for (const CodeGenIntrinsic &Int : Ints) {
920 if (Int.PrettyPrintFunctions.empty())
921 continue;
922
923 OS << " case " << Int.EnumName << ":\n";
924 OS << " switch (ArgIdx) {\n";
925 for (const auto [ArgIdx, ArgName, FuncName] : Int.PrettyPrintFunctions) {
926 OS << " case " << ArgIdx << ":\n";
927 if (!ArgName.empty())
928 OS << " OS << \"" << ArgName << "=\";\n";
929 if (!FuncName.empty()) {
930 OS << " ";
931 if (!Int.TargetPrefix.empty())
932 OS << Int.TargetPrefix << "::";
933 OS << FuncName << "(OS, ImmArgVal);\n";
934 }
935 OS << " return;\n";
936 }
937 OS << " }\n";
938 OS << " break;\n";
939 }
940 OS << R"( default:
941 break;
942 }
943})";
944}
945
946void IntrinsicEmitter::EmitDefaultArgValuesTable(
947 const CodeGenIntrinsicTable &Ints, raw_ostream &OS) {
948 // Sequences are [NumDefaults << 32 | FirstDefault, values...].
949 using Sequence = SmallVector<uint64_t, 8>;
950
951 SequenceToOffsetTable<Sequence> Table;
952 SmallVector<Sequence> PerIntrinsic;
953 PerIntrinsic.reserve(N: Ints.size());
954
955 for (const CodeGenIntrinsic &Int : Ints) {
956 if (Int.ParamDefaultValues.empty()) {
957 PerIntrinsic.push_back(Elt: {});
958 continue;
959 }
960
961 unsigned FirstDefault = 0;
962 for (size_t j = 0U, N = Int.ParamDefaultValues.size(); j < N; ++j) {
963 if (Int.ParamDefaultValues[j].has_value()) {
964 FirstDefault = j;
965 break;
966 }
967 }
968 unsigned NumDefaults =
969 static_cast<unsigned>(Int.ParamDefaultValues.size()) - FirstDefault;
970
971 Sequence Seq;
972 Seq.push_back(Elt: (static_cast<uint64_t>(NumDefaults) << 32) | FirstDefault);
973 for (size_t j = FirstDefault, N = Int.ParamDefaultValues.size(); j < N;
974 ++j) {
975 assert(Int.ParamDefaultValues[j].has_value() &&
976 "Default block must be contiguous");
977 Seq.push_back(Elt: *Int.ParamDefaultValues[j]);
978 }
979 Table.add(Seq);
980 PerIntrinsic.push_back(Elt: std::move(Seq));
981 }
982
983 Table.layout();
984
985 IfDefEmitter IfDef(OS, "GET_INTRINSIC_DEFAULT_ARG_VALUES");
986
987 OS << "static constexpr uint64_t DefaultArgValuesTable[] = {\n";
988 OS << " 0, // offset 0: sentinel for intrinsics without defaults\n";
989 Table.emit(OS, Print: [](raw_ostream &OS, uint64_t Val) { OS << " " << Val; });
990 OS << "};\n\n";
991
992 OS << "static constexpr uint32_t DefaultArgValuesTableOffset[] = {\n";
993 OS << " 0, // not_intrinsic\n";
994 for (const Sequence &Seq : PerIntrinsic) {
995 if (!Seq.empty())
996 OS << " " << (Table.get(Seq) + 1) << ",\n";
997 else
998 OS << " 0,\n";
999 }
1000 OS << "};\n\n";
1001
1002 OS << R"(
1003std::pair<unsigned, ArrayRef<uint64_t>>
1004Intrinsic::getAllDefaultArgValues(ID IID) {
1005 uint32_t Offset = DefaultArgValuesTableOffset[IID];
1006 uint64_t Header = DefaultArgValuesTable[Offset];
1007 uint32_t FirstDefault = Header & 0xFFFFFFFFu;
1008 uint32_t NumDefaults = (Header >> 32) & 0xFFFFFFFFu;
1009 if (NumDefaults == 0)
1010 return {0, {}};
1011 return {FirstDefault,
1012 ArrayRef(&DefaultArgValuesTable[Offset + 1], NumDefaults)};
1013}
1014)";
1015}
1016
1017void IntrinsicEmitter::EmitIntrinsicToBuiltinMap(
1018 const CodeGenIntrinsicTable &Ints, bool IsClang, raw_ostream &OS) {
1019 StringRef CompilerName = IsClang ? "Clang" : "MS";
1020 StringRef UpperCompilerName = IsClang ? "CLANG" : "MS";
1021
1022 // map<TargetPrefix, pair<map<BuiltinName, EnumName>, CommonPrefix>.
1023 // Note that we iterate over both the maps in the code below and both
1024 // iterations need to iterate in sorted key order. For the inner map, entries
1025 // need to be emitted in the sorted order of `BuiltinName` with `CommonPrefix`
1026 // rempved, because we use std::lower_bound to search these entries. For the
1027 // outer map as well, entries need to be emitted in sorter order of
1028 // `TargetPrefix` as we use std::lower_bound to search these entries.
1029 using BIMEntryTy =
1030 std::pair<std::map<StringRef, StringRef>, std::optional<StringRef>>;
1031 std::map<StringRef, BIMEntryTy> BuiltinMap;
1032
1033 for (const CodeGenIntrinsic &Int : Ints) {
1034 StringRef BuiltinName = IsClang ? Int.ClangBuiltinName : Int.MSBuiltinName;
1035 if (BuiltinName.empty())
1036 continue;
1037 // Get the map for this target prefix.
1038 auto &[Map, CommonPrefix] = BuiltinMap[Int.TargetPrefix];
1039
1040 if (!Map.try_emplace(k: BuiltinName, args: Int.EnumName).second)
1041 PrintFatalError(ErrorLoc: Int.TheDef->getLoc(),
1042 Msg: "Intrinsic '" + Int.TheDef->getName() + "': duplicate " +
1043 CompilerName + " builtin name!");
1044
1045 // Update common prefix.
1046 if (!CommonPrefix) {
1047 // For the first builtin for this target, initialize the common prefix.
1048 CommonPrefix = BuiltinName;
1049 continue;
1050 }
1051
1052 // Update the common prefix. Note that this assumes that `take_front` will
1053 // never set the `Data` pointer in CommonPrefix to nullptr.
1054 const char *Mismatch = mismatch(Range1&: *CommonPrefix, Range2&: BuiltinName).first;
1055 *CommonPrefix = CommonPrefix->take_front(N: Mismatch - CommonPrefix->begin());
1056 }
1057
1058 // Populate the string table with the names of all the builtins after
1059 // removing this common prefix.
1060 StringToOffsetTable Table;
1061 for (const auto &[TargetPrefix, Entry] : BuiltinMap) {
1062 auto &[Map, CommonPrefix] = Entry;
1063 for (auto &[BuiltinName, EnumName] : Map) {
1064 StringRef Suffix = BuiltinName.substr(Start: CommonPrefix->size());
1065 Table.GetOrAddStringOffset(Str: Suffix);
1066 }
1067 }
1068
1069 IfDefEmitter IfDef(
1070 OS,
1071 formatv(Fmt: "GET_LLVM_INTRINSIC_FOR_{}_BUILTIN", Vals&: UpperCompilerName).str());
1072 OS << formatv(Fmt: R"(
1073// Get the LLVM intrinsic that corresponds to a builtin. This is used by the
1074// C front-end. The builtin name is passed in as BuiltinName, and a target
1075// prefix (e.g. 'ppc') is passed in as TargetPrefix.
1076Intrinsic::ID
1077Intrinsic::getIntrinsicFor{}Builtin(StringRef TargetPrefix,
1078 StringRef BuiltinName) {{
1079 using namespace Intrinsic;
1080)",
1081 Vals&: CompilerName);
1082
1083 if (BuiltinMap.empty()) {
1084 OS << "return not_intrinsic;\n";
1085 return;
1086 }
1087
1088 if (!Table.empty()) {
1089 Table.EmitStringTableDef(OS, Name: "BuiltinNames");
1090
1091 OS << R"(
1092 struct BuiltinEntry {
1093 ID IntrinsicID;
1094 unsigned StrTabOffset;
1095 const char *getName() const { return BuiltinNames[StrTabOffset].data(); }
1096 bool operator<(StringRef RHS) const {
1097 return strncmp(getName(), RHS.data(), RHS.size()) < 0;
1098 }
1099 };
1100
1101)";
1102 }
1103
1104 // Emit a per target table of bultin names.
1105 bool HasTargetIndependentBuiltins = false;
1106 StringRef TargetIndepndentCommonPrefix;
1107 for (const auto &[TargetPrefix, Entry] : BuiltinMap) {
1108 const auto &[Map, CommonPrefix] = Entry;
1109 if (!TargetPrefix.empty()) {
1110 OS << formatv(Fmt: " // Builtins for {0}.\n", Vals: TargetPrefix);
1111 } else {
1112 OS << " // Target independent builtins.\n";
1113 HasTargetIndependentBuiltins = true;
1114 TargetIndepndentCommonPrefix = *CommonPrefix;
1115 }
1116
1117 // Emit the builtin table for this target prefix.
1118 OS << formatv(Fmt: " static constexpr BuiltinEntry {}Names[] = {{\n",
1119 Vals: TargetPrefix);
1120 for (const auto &[BuiltinName, EnumName] : Map) {
1121 StringRef Suffix = BuiltinName.substr(Start: CommonPrefix->size());
1122 OS << formatv(Fmt: " {{{}, {}}, // {}\n", Vals: EnumName,
1123 Vals: *Table.GetStringOffset(Str: Suffix), Vals: BuiltinName);
1124 }
1125 OS << formatv(Fmt: " }; // {}Names\n\n", Vals: TargetPrefix);
1126 }
1127
1128 // After emitting the builtin tables for all targets, emit a lookup table for
1129 // all targets. We will use binary search, similar to the table for builtin
1130 // names to lookup into this table.
1131 OS << R"(
1132 struct TargetEntry {
1133 StringLiteral TargetPrefix;
1134 ArrayRef<BuiltinEntry> Names;
1135 StringLiteral CommonPrefix;
1136 bool operator<(StringRef RHS) const {
1137 return TargetPrefix < RHS;
1138 };
1139 };
1140 static constexpr TargetEntry TargetTable[] = {
1141)";
1142
1143 for (const auto &[TargetPrefix, Entry] : BuiltinMap) {
1144 const auto &[Map, CommonPrefix] = Entry;
1145 if (TargetPrefix.empty())
1146 continue;
1147 OS << formatv(Fmt: R"( {{"{0}", {0}Names, "{1}"},)", Vals: TargetPrefix,
1148 Vals: CommonPrefix)
1149 << "\n";
1150 }
1151 OS << " };\n";
1152
1153 // Now for the actual lookup, first check the target independent table if
1154 // we emitted one.
1155 if (HasTargetIndependentBuiltins) {
1156 OS << formatv(Fmt: R"(
1157 // Check if it's a target independent builtin.
1158 // Copy the builtin name so we can use it in consume_front without clobbering
1159 // if for the lookup in the target specific table.
1160 StringRef Suffix = BuiltinName;
1161 if (Suffix.consume_front("{}")) {{
1162 auto II = lower_bound(Names, Suffix);
1163 if (II != std::end(Names) && II->getName() == Suffix)
1164 return II->IntrinsicID;
1165 }
1166)",
1167 Vals&: TargetIndepndentCommonPrefix);
1168 }
1169
1170 // If a target independent builtin was not found, lookup the target specific.
1171 OS << R"(
1172 auto TI = lower_bound(TargetTable, TargetPrefix);
1173 if (TI == std::end(TargetTable) || TI->TargetPrefix != TargetPrefix)
1174 return not_intrinsic;
1175 // This is the last use of BuiltinName, so no need to copy before using it in
1176 // consume_front.
1177 if (!BuiltinName.consume_front(TI->CommonPrefix))
1178 return not_intrinsic;
1179 auto II = lower_bound(TI->Names, BuiltinName);
1180 if (II == std::end(TI->Names) || II->getName() != BuiltinName)
1181 return not_intrinsic;
1182 return II->IntrinsicID;
1183}
1184)";
1185}
1186
1187static TableGen::Emitter::OptClass<IntrinsicEmitterOpt</*Enums=*/true>>
1188 X("gen-intrinsic-enums", "Generate intrinsic enums");
1189
1190static TableGen::Emitter::OptClass<IntrinsicEmitterOpt</*Enums=*/false>>
1191 Y("gen-intrinsic-impl", "Generate intrinsic implementation code");
1192