LLVM 24.0.0git
DXILBitcodeWriter.cpp
Go to the documentation of this file.
1//===- Bitcode/Writer/DXILBitcodeWriter.cpp - DXIL Bitcode Writer ---------===//
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// Bitcode writer implementation.
10//
11//===----------------------------------------------------------------------===//
12
13#include "DXILBitcodeWriter.h"
14#include "DXILDebugInfoMap.h"
15#include "DXILValueEnumerator.h"
17#include "llvm/ADT/STLExtras.h"
23#include "llvm/IR/Attributes.h"
24#include "llvm/IR/BasicBlock.h"
25#include "llvm/IR/Comdat.h"
26#include "llvm/IR/Constant.h"
27#include "llvm/IR/Constants.h"
29#include "llvm/IR/DebugLoc.h"
31#include "llvm/IR/Function.h"
32#include "llvm/IR/GlobalAlias.h"
33#include "llvm/IR/GlobalIFunc.h"
35#include "llvm/IR/GlobalValue.h"
37#include "llvm/IR/InlineAsm.h"
38#include "llvm/IR/InstrTypes.h"
39#include "llvm/IR/Instruction.h"
41#include "llvm/IR/LLVMContext.h"
42#include "llvm/IR/Metadata.h"
43#include "llvm/IR/Module.h"
45#include "llvm/IR/Operator.h"
46#include "llvm/IR/Type.h"
48#include "llvm/IR/Value.h"
52#include "llvm/Support/ModRef.h"
53#include "llvm/Support/SHA1.h"
55
56namespace llvm {
57namespace dxil {
58
59// Generates an enum to use as an index in the Abbrev array of Metadata record.
60enum MetadataAbbrev : unsigned {
61#define HANDLE_MDNODE_LEAF(CLASS) CLASS##AbbrevID,
62#include "llvm/IR/Metadata.def"
64};
65
67
68 /// These are manifest constants used by the bitcode writer. They do not need
69 /// to be kept in sync with the reader, but need to be consistent within this
70 /// file.
71 enum {
72 // VALUE_SYMTAB_BLOCK abbrev id's.
73 VST_ENTRY_8_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
74 VST_ENTRY_7_ABBREV,
75 VST_ENTRY_6_ABBREV,
76 VST_BBENTRY_6_ABBREV,
77
78 // CONSTANTS_BLOCK abbrev id's.
79 CONSTANTS_SETTYPE_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
80 CONSTANTS_INTEGER_ABBREV,
81 CONSTANTS_CE_CAST_Abbrev,
82 CONSTANTS_NULL_Abbrev,
83
84 // FUNCTION_BLOCK abbrev id's.
85 FUNCTION_INST_LOAD_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
86 FUNCTION_INST_BINOP_ABBREV,
87 FUNCTION_INST_BINOP_FLAGS_ABBREV,
88 FUNCTION_INST_CAST_ABBREV,
89 FUNCTION_INST_RET_VOID_ABBREV,
90 FUNCTION_INST_RET_VAL_ABBREV,
91 FUNCTION_INST_UNREACHABLE_ABBREV,
92 FUNCTION_INST_GEP_ABBREV,
93 };
94
95 // Cache some types
96 Type *I8Ty;
97 Type *I8PtrTy;
98
99 /// The stream created and owned by the client.
100 BitstreamWriter &Stream;
101
102 StringTableBuilder &StrtabBuilder;
103
104 /// The Module to write to bitcode.
105 const Module &M;
106
107 /// Enumerates ids for all values in the module.
109
110 /// Map that holds the correspondence between GUIDs in the summary index,
111 /// that came from indirect call profiles, and a value id generated by this
112 /// class to use in the VST and summary block records.
113 std::map<GlobalValue::GUID, unsigned> GUIDToValueIdMap;
114
115 /// Tracks the last value id recorded in the GUIDToValueMap.
116 unsigned GlobalValueId;
117
118 /// Pointer to the buffer allocated by caller for bitcode writing.
119 const SmallVectorImpl<char> &Buffer;
120
121 /// The start bit of the identification block.
122 uint64_t BitcodeStartBit;
123
124 /// This maps values to their typed pointers
125 PointerTypeMap PointerMap;
126
127 /// Tracks debug info metadata.
128 const DXILDebugInfoMap &DebugInfo;
129
130public:
131 /// Constructs a ModuleBitcodeWriter object for the given Module,
132 /// writing to the provided \p Buffer.
134 StringTableBuilder &StrtabBuilder, BitstreamWriter &Stream,
135 const DXILDebugInfoMap &DebugInfo)
136 : I8Ty(Type::getInt8Ty(M.getContext())),
137 I8PtrTy(TypedPointerType::get(I8Ty, 0)), Stream(Stream),
138 StrtabBuilder(StrtabBuilder), M(M), VE(M, I8PtrTy, DebugInfo),
139 Buffer(Buffer), BitcodeStartBit(Stream.GetCurrentBitNo()),
140 PointerMap(PointerTypeAnalysis::run(M)), DebugInfo(DebugInfo) {
141 GlobalValueId = VE.getValues().size();
142 // Enumerate the typed pointers
143 for (auto El : PointerMap)
144 VE.EnumerateType(El.second);
145 }
146
147 /// Emit the current module to the bitstream.
148 void write();
149
150 static uint64_t getAttrKindEncoding(Attribute::AttrKind Kind);
151 static void writeStringRecord(BitstreamWriter &Stream, unsigned Code,
152 StringRef Str, unsigned AbbrevToUse);
153 static void emitSignedInt64(SmallVectorImpl<uint64_t> &Vals, uint64_t V);
154 static void emitWideAPInt(SmallVectorImpl<uint64_t> &Vals, const APInt &A);
155
156 static unsigned getEncodedComdatSelectionKind(const Comdat &C);
157 static unsigned getEncodedLinkage(const GlobalValue::LinkageTypes Linkage);
158 static unsigned getEncodedLinkage(const GlobalValue &GV);
159 static unsigned getEncodedVisibility(const GlobalValue &GV);
160 static unsigned getEncodedThreadLocalMode(const GlobalValue &GV);
161 static unsigned getEncodedDLLStorageClass(const GlobalValue &GV);
162 static unsigned getEncodedCastOpcode(unsigned Opcode);
163 static unsigned getEncodedUnaryOpcode(unsigned Opcode);
164 static unsigned getEncodedBinaryOpcode(unsigned Opcode);
166 static unsigned getEncodedOrdering(AtomicOrdering Ordering);
167 static uint64_t getOptimizationFlags(const Value *V);
168
169private:
170 void writeModuleVersion();
171 void writePerModuleGlobalValueSummary();
172
173 void assignValueId(GlobalValue::GUID ValGUID) {
174 GUIDToValueIdMap[ValGUID] = ++GlobalValueId;
175 }
176
177 unsigned getValueId(GlobalValue::GUID ValGUID) {
178 const auto &VMI = GUIDToValueIdMap.find(ValGUID);
179 // Expect that any GUID value had a value Id assigned by an
180 // earlier call to assignValueId.
181 assert(VMI != GUIDToValueIdMap.end() &&
182 "GUID does not have assigned value Id");
183 return VMI->second;
184 }
185
186 // Helper to get the valueId for the type of value recorded in VI.
187 unsigned getValueId(ValueInfo VI) {
188 if (!VI.haveGVs() || !VI.getValue())
189 return getValueId(VI.getGUID());
190 return VE.getValueID(VI.getValue());
191 }
192
193 std::map<GlobalValue::GUID, unsigned> &valueIds() { return GUIDToValueIdMap; }
194
195 uint64_t bitcodeStartBit() { return BitcodeStartBit; }
196
197 unsigned createDILocationAbbrev();
198 unsigned createGenericDINodeAbbrev();
199
200 void writeAttributeGroupTable();
201 void writeAttributeTable();
202 void writeTypeTable();
203 void writeComdats();
204 void writeValueSymbolTableForwardDecl();
205 void writeModuleInfo();
206 void writeValueAsMetadata(const ValueAsMetadata *MD,
207 SmallVectorImpl<uint64_t> &Record);
208 void writeMDTuple(const MDTuple *N, SmallVectorImpl<uint64_t> &Record,
209 unsigned Abbrev);
210 void writeDILocation(const DILocation *N, SmallVectorImpl<uint64_t> &Record,
211 unsigned &Abbrev);
212 void writeGenericDINode(const GenericDINode *N,
213 SmallVectorImpl<uint64_t> &Record, unsigned &Abbrev) {
214 llvm_unreachable("DXIL cannot contain GenericDI Nodes");
215 }
216 void writeDISubrange(const DISubrange *N, SmallVectorImpl<uint64_t> &Record,
217 unsigned Abbrev);
218 void writeDIGenericSubrange(const DIGenericSubrange *N,
219 SmallVectorImpl<uint64_t> &Record,
220 unsigned Abbrev) {
221 llvm_unreachable("DXIL cannot contain DIGenericSubrange Nodes");
222 }
223 void writeDIEnumerator(const DIEnumerator *N,
224 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
225 void writeDIBasicType(const DIBasicType *N, SmallVectorImpl<uint64_t> &Record,
226 unsigned Abbrev);
227 void writeDIFixedPointType(const DIFixedPointType *N,
228 SmallVectorImpl<uint64_t> &Record,
229 unsigned Abbrev) {
230 llvm_unreachable("DXIL cannot contain DIFixedPointType Nodes");
231 }
232 void writeDIStringType(const DIStringType *N,
233 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev) {
234 llvm_unreachable("DXIL cannot contain DIStringType Nodes");
235 }
236 void writeDIDerivedType(const DIDerivedType *N,
237 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
238 void writeDISubrangeType(const DISubrangeType *N,
239 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev) {
240 llvm_unreachable("DXIL cannot contain DISubrangeType Nodes");
241 }
242 void writeDICompositeType(const DICompositeType *N,
243 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
244 void writeDISubroutineType(const DISubroutineType *N,
245 SmallVectorImpl<uint64_t> &Record,
246 unsigned Abbrev);
247 void writeDIFile(const DIFile *N, SmallVectorImpl<uint64_t> &Record,
248 unsigned Abbrev);
249 void writeDICompileUnit(const DICompileUnit *N,
250 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
251 void writeDISubprogram(const DISubprogram *N,
252 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
253 void writeDILexicalBlock(const DILexicalBlock *N,
254 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
255 void writeDILexicalBlockFile(const DILexicalBlockFile *N,
256 SmallVectorImpl<uint64_t> &Record,
257 unsigned Abbrev);
258 void writeDICommonBlock(const DICommonBlock *N,
259 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev) {
260 llvm_unreachable("DXIL cannot contain DICommonBlock Nodes");
261 }
262 void writeDINamespace(const DINamespace *N, SmallVectorImpl<uint64_t> &Record,
263 unsigned Abbrev);
264 void writeDIMacro(const DIMacro *N, SmallVectorImpl<uint64_t> &Record,
265 unsigned Abbrev) {
266 llvm_unreachable("DXIL cannot contain DIMacro Nodes");
267 }
268 void writeDIMacroFile(const DIMacroFile *N, SmallVectorImpl<uint64_t> &Record,
269 unsigned Abbrev) {
270 llvm_unreachable("DXIL cannot contain DIMacroFile Nodes");
271 }
272 void writeDIArgList(const DIArgList *N, SmallVectorImpl<uint64_t> &Record,
273 unsigned Abbrev) {
274 llvm_unreachable("DXIL cannot contain DIArgList Nodes");
275 }
276 void writeDIAssignID(const DIAssignID *N, SmallVectorImpl<uint64_t> &Record,
277 unsigned Abbrev) {
278 // DIAssignID is experimental feature to track variable location in IR..
279 // FIXME: translate DIAssignID to debug info DXIL supports.
280 // See https://github.com/llvm/llvm-project/issues/58989
281 llvm_unreachable("DXIL cannot contain DIAssignID Nodes");
282 }
283 void writeDILayerLoc(const DILayerLoc *N, SmallVectorImpl<uint64_t> &Record,
284 unsigned Abbrev) {
285 llvm_unreachable("DXIL cannot contain DILayerLoc Nodes");
286 }
287 void writeDILayerLocList(const DILayerLocList *N,
288 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev) {
289 llvm_unreachable("DXIL cannot contain DILayerLocList Nodes");
290 }
291 void writeDIModule(const DIModule *N, SmallVectorImpl<uint64_t> &Record,
292 unsigned Abbrev);
293 void writeDITemplateTypeParameter(const DITemplateTypeParameter *N,
294 SmallVectorImpl<uint64_t> &Record,
295 unsigned Abbrev);
296 void writeDITemplateValueParameter(const DITemplateValueParameter *N,
297 SmallVectorImpl<uint64_t> &Record,
298 unsigned Abbrev);
299 void writeDIGlobalVariable(const DIGlobalVariable *N,
300 SmallVectorImpl<uint64_t> &Record,
301 unsigned Abbrev);
302 void writeDILocalVariable(const DILocalVariable *N,
303 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
304 void writeDILabel(const DILabel *N, SmallVectorImpl<uint64_t> &Record,
305 unsigned Abbrev) {
306 llvm_unreachable("DXIL cannot contain DILabel Nodes");
307 }
308 void writeDIExpression(const DIExpression *N,
309 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
310 void writeDIGlobalVariableExpression(const DIGlobalVariableExpression *N,
311 SmallVectorImpl<uint64_t> &Record,
312 unsigned Abbrev) {
313 llvm_unreachable("DXIL cannot contain GlobalVariableExpression Nodes");
314 }
315 void writeDIObjCProperty(const DIObjCProperty *N,
316 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
317 void writeDIProperty(const DIProperty *N, SmallVectorImpl<uint64_t> &Record,
318 unsigned Abbrev) {
319 llvm_unreachable("DXIL cannot contain DIProperty Nodes");
320 }
321 void writeDIImportedEntity(const DIImportedEntity *N,
322 SmallVectorImpl<uint64_t> &Record,
323 unsigned Abbrev);
324 void writeMDFunctionLocalContext(const MDFunctionLocalContext *N,
325 SmallVectorImpl<uint64_t> &Record,
326 unsigned &Abbrev) {
327 llvm_unreachable("DXIL cannot contain MDFunctionLocalContext Nodes");
328 }
329 unsigned createMetadataStringsAbbrev();
330 void writeMetadataStrings(ArrayRef<const Metadata *> Strings,
331 SmallVectorImpl<uint64_t> &Record);
332 void writeMetadataRecords(ArrayRef<const Metadata *> MDs,
333 SmallVectorImpl<uint64_t> &Record,
334 std::vector<unsigned> *MDAbbrevs = nullptr,
335 std::vector<uint64_t> *IndexPos = nullptr);
336 void writeModuleMetadata();
337 void writeFunctionMetadata(const Function &F);
338 void writeFunctionMetadataAttachment(const Function &F);
339 void writeModuleMetadataKinds();
340 void writeConstants(unsigned FirstVal, unsigned LastVal, bool isGlobal);
341 void writeModuleConstants();
342 bool pushValueAndType(const Value *V, unsigned InstID,
343 SmallVectorImpl<unsigned> &Vals);
344 void pushValue(const Value *V, unsigned InstID,
345 SmallVectorImpl<unsigned> &Vals);
346 void pushValueSigned(const Value *V, unsigned InstID,
347 SmallVectorImpl<uint64_t> &Vals);
348 void writeInstruction(const Instruction &I, unsigned InstID,
349 SmallVectorImpl<unsigned> &Vals);
350 void writeFunctionLevelValueSymbolTable(const ValueSymbolTable &VST);
351 void writeFunction(const Function &F);
352 void writeBlockInfo();
353
354 unsigned getEncodedSyncScopeID(SyncScope::ID SSID) { return unsigned(SSID); }
355
356 unsigned getEncodedAlign(MaybeAlign Alignment) { return encode(Alignment); }
357
358 unsigned getTypeID(Type *T, const Value *V = nullptr);
359 /// getGlobalObjectValueTypeID - returns the element type for a GlobalObject
360 ///
361 /// GlobalObject types are saved by PointerTypeAnalysis as pointers to the
362 /// GlobalObject, but in the bitcode writer we need the pointer element type.
363 unsigned getGlobalObjectValueTypeID(Type *T, const GlobalObject *G);
364};
365
366} // namespace dxil
367} // namespace llvm
368
369using namespace llvm;
370using namespace llvm::dxil;
371
372////////////////////////////////////////////////////////////////////////////////
373/// Begin dxil::BitcodeWriter Implementation
374////////////////////////////////////////////////////////////////////////////////
375
377 : Buffer(Buffer), Stream(new BitstreamWriter(Buffer)) {
378 // Emit the file header.
379 Stream->Emit((unsigned)'B', 8);
380 Stream->Emit((unsigned)'C', 8);
381 Stream->Emit(0x0, 4);
382 Stream->Emit(0xC, 4);
383 Stream->Emit(0xE, 4);
384 Stream->Emit(0xD, 4);
385}
386
388
389/// Write the specified module to the specified output stream.
392 Buffer.reserve(256 * 1024);
393
394 // If this is darwin or another generic macho target, reserve space for the
395 // header.
396 Triple TT(M.getTargetTriple());
397 if (TT.isOSDarwin() || TT.isOSBinFormatMachO())
398 Buffer.insert(Buffer.begin(), BWH_HeaderSize, 0);
399
401 BitcodeWriter Writer(Buffer);
402 Writer.writeModule(M, DebugInfo);
403
404 // Write the generated bitstream to "Out".
405 if (!Buffer.empty())
406 Out.write((char *)&Buffer.front(), Buffer.size());
407}
408
409void BitcodeWriter::writeBlob(unsigned Block, unsigned Record, StringRef Blob) {
410 Stream->EnterSubblock(Block, 3);
411
412 auto Abbv = std::make_shared<BitCodeAbbrev>();
413 Abbv->Add(BitCodeAbbrevOp(Record));
415 auto AbbrevNo = Stream->EmitAbbrev(std::move(Abbv));
416
417 Stream->EmitRecordWithBlob(AbbrevNo, ArrayRef<uint64_t>{Record}, Blob);
418
419 Stream->ExitBlock();
420}
421
424
425 // The Mods vector is used by irsymtab::build, which requires non-const
426 // Modules in case it needs to materialize metadata. But the bitcode writer
427 // requires that the module is materialized, so we can cast to non-const here,
428 // after checking that it is in fact materialized.
429 assert(M.isMaterialized());
430 Mods.push_back(const_cast<Module *>(&M));
431
432 DXILBitcodeWriter ModuleWriter(M, Buffer, StrtabBuilder, *Stream, DebugInfo);
433 ModuleWriter.write();
434}
435
436////////////////////////////////////////////////////////////////////////////////
437/// Begin dxil::BitcodeWriterBase Implementation
438////////////////////////////////////////////////////////////////////////////////
439
441 switch (Opcode) {
442 default:
443 llvm_unreachable("Unknown cast instruction!");
444 case Instruction::Trunc:
445 return bitc::CAST_TRUNC;
446 case Instruction::ZExt:
447 return bitc::CAST_ZEXT;
448 case Instruction::SExt:
449 return bitc::CAST_SEXT;
450 case Instruction::FPToUI:
451 return bitc::CAST_FPTOUI;
452 case Instruction::FPToSI:
453 return bitc::CAST_FPTOSI;
454 case Instruction::UIToFP:
455 return bitc::CAST_UITOFP;
456 case Instruction::SIToFP:
457 return bitc::CAST_SITOFP;
458 case Instruction::FPTrunc:
459 return bitc::CAST_FPTRUNC;
460 case Instruction::FPExt:
461 return bitc::CAST_FPEXT;
462 case Instruction::PtrToInt:
463 return bitc::CAST_PTRTOINT;
464 case Instruction::IntToPtr:
465 return bitc::CAST_INTTOPTR;
466 case Instruction::BitCast:
467 return bitc::CAST_BITCAST;
468 case Instruction::AddrSpaceCast:
470 }
471}
472
474 switch (Opcode) {
475 default:
476 llvm_unreachable("Unknown binary instruction!");
477 case Instruction::FNeg:
478 return bitc::UNOP_FNEG;
479 }
480}
481
483 switch (Opcode) {
484 default:
485 llvm_unreachable("Unknown binary instruction!");
486 case Instruction::Add:
487 case Instruction::FAdd:
488 return bitc::BINOP_ADD;
489 case Instruction::Sub:
490 case Instruction::FSub:
491 return bitc::BINOP_SUB;
492 case Instruction::Mul:
493 case Instruction::FMul:
494 return bitc::BINOP_MUL;
495 case Instruction::UDiv:
496 return bitc::BINOP_UDIV;
497 case Instruction::FDiv:
498 case Instruction::SDiv:
499 return bitc::BINOP_SDIV;
500 case Instruction::URem:
501 return bitc::BINOP_UREM;
502 case Instruction::FRem:
503 case Instruction::SRem:
504 return bitc::BINOP_SREM;
505 case Instruction::Shl:
506 return bitc::BINOP_SHL;
507 case Instruction::LShr:
508 return bitc::BINOP_LSHR;
509 case Instruction::AShr:
510 return bitc::BINOP_ASHR;
511 case Instruction::And:
512 return bitc::BINOP_AND;
513 case Instruction::Or:
514 return bitc::BINOP_OR;
515 case Instruction::Xor:
516 return bitc::BINOP_XOR;
517 }
518}
519
520unsigned DXILBitcodeWriter::getTypeID(Type *T, const Value *V) {
521 // For Constant, always check PointerMap to make sure OpaquePointer in
522 // things like constant struct/array works.
523 if (!T->isPointerTy() && !isa_and_nonnull<Constant>(V))
524 return VE.getTypeID(T);
525 auto It = PointerMap.find(V);
526 if (It != PointerMap.end())
527 return VE.getTypeID(It->second);
528 // FIXME: support ConstantPointerNull and UndefValue which could map to more
529 // than one TypedPointerType.
530 // See https://github.com/llvm/llvm-project/issues/57942.
531 if (T->isPointerTy())
532 return VE.getTypeID(I8PtrTy);
533 return VE.getTypeID(T);
534}
535
536unsigned DXILBitcodeWriter::getGlobalObjectValueTypeID(Type *T,
537 const GlobalObject *G) {
538 auto It = PointerMap.find(G);
539 if (It != PointerMap.end()) {
540 TypedPointerType *PtrTy = cast<TypedPointerType>(It->second);
541 return VE.getTypeID(PtrTy->getElementType());
542 }
543 return VE.getTypeID(T);
544}
545
547 switch (Op) {
548 default:
549 llvm_unreachable("Unknown RMW operation!");
551 return bitc::RMW_XCHG;
553 return bitc::RMW_ADD;
555 return bitc::RMW_SUB;
557 return bitc::RMW_AND;
559 return bitc::RMW_NAND;
561 return bitc::RMW_OR;
563 return bitc::RMW_XOR;
565 return bitc::RMW_MAX;
567 return bitc::RMW_MIN;
569 return bitc::RMW_UMAX;
571 return bitc::RMW_UMIN;
573 return bitc::RMW_FADD;
575 return bitc::RMW_FSUB;
577 return bitc::RMW_FMAX;
579 return bitc::RMW_FMIN;
580 }
581}
582
602
604 unsigned Code, StringRef Str,
605 unsigned AbbrevToUse) {
607
608 // Code: [strchar x N]
609 for (char C : Str) {
610 if (AbbrevToUse && !BitCodeAbbrevOp::isChar6(C))
611 AbbrevToUse = 0;
612 Vals.push_back(C);
613 }
614
615 // Emit the finished record.
616 Stream.EmitRecord(Code, Vals, AbbrevToUse);
617}
618
620 switch (Kind) {
621 case Attribute::Alignment:
623 case Attribute::AlwaysInline:
625 case Attribute::Builtin:
627 case Attribute::ByVal:
629 case Attribute::Convergent:
631 case Attribute::InAlloca:
633 case Attribute::Cold:
635 case Attribute::InlineHint:
637 case Attribute::InReg:
639 case Attribute::JumpTable:
641 case Attribute::MinSize:
643 case Attribute::Naked:
645 case Attribute::Nest:
647 case Attribute::NoAlias:
649 case Attribute::NoBuiltin:
651 case Attribute::NoDuplicate:
653 case Attribute::NoImplicitFloat:
655 case Attribute::NoInline:
657 case Attribute::NonLazyBind:
659 case Attribute::NonNull:
661 case Attribute::Dereferenceable:
663 case Attribute::DereferenceableOrNull:
665 case Attribute::NoRedZone:
667 case Attribute::NoReturn:
669 case Attribute::NoUnwind:
671 case Attribute::OptimizeForSize:
673 case Attribute::OptimizeNone:
675 case Attribute::ReadNone:
677 case Attribute::ReadOnly:
679 case Attribute::Returned:
681 case Attribute::ReturnsTwice:
683 case Attribute::SExt:
685 case Attribute::StackAlignment:
687 case Attribute::StackProtect:
689 case Attribute::StackProtectReq:
691 case Attribute::StackProtectStrong:
693 case Attribute::SafeStack:
695 case Attribute::StructRet:
697 case Attribute::SanitizeAddress:
699 case Attribute::SanitizeThread:
701 case Attribute::SanitizeMemory:
703 case Attribute::UWTable:
705 case Attribute::ZExt:
708 llvm_unreachable("Can not encode end-attribute kinds marker.");
709 case Attribute::None:
710 llvm_unreachable("Can not encode none-attribute.");
713 llvm_unreachable("Trying to encode EmptyKey/TombstoneKey");
714 default:
715 llvm_unreachable("Trying to encode attribute not supported by DXIL. These "
716 "should be stripped in DXILPrepare");
717 }
718
719 llvm_unreachable("Trying to encode unknown attribute");
720}
721
723 uint64_t V) {
724 if ((int64_t)V >= 0)
725 Vals.push_back(V << 1);
726 else
727 Vals.push_back((-V << 1) | 1);
728}
729
731 const APInt &A) {
732 // We have an arbitrary precision integer value to write whose
733 // bit width is > 64. However, in canonical unsigned integer
734 // format it is likely that the high bits are going to be zero.
735 // So, we only write the number of active words.
736 unsigned NumWords = A.getActiveWords();
737 const uint64_t *RawData = A.getRawData();
738 for (unsigned i = 0; i < NumWords; i++)
739 emitSignedInt64(Vals, RawData[i]);
740}
741
743 uint64_t Flags = 0;
744
745 if (const auto *OBO = dyn_cast<OverflowingBinaryOperator>(V)) {
746 if (OBO->hasNoSignedWrap())
747 Flags |= 1 << bitc::OBO_NO_SIGNED_WRAP;
748 if (OBO->hasNoUnsignedWrap())
749 Flags |= 1 << bitc::OBO_NO_UNSIGNED_WRAP;
750 } else if (const auto *PEO = dyn_cast<PossiblyExactOperator>(V)) {
751 if (PEO->isExact())
752 Flags |= 1 << bitc::PEO_EXACT;
753 } else if (const auto *FPMO = dyn_cast<FPMathOperator>(V)) {
754 if (FPMO->hasAllowReassoc() || FPMO->hasAllowContract())
755 Flags |= bitc::UnsafeAlgebra;
756 if (FPMO->hasNoNaNs())
757 Flags |= bitc::NoNaNs;
758 if (FPMO->hasNoInfs())
759 Flags |= bitc::NoInfs;
760 if (FPMO->hasNoSignedZeros())
761 Flags |= bitc::NoSignedZeros;
762 if (FPMO->hasAllowReciprocal())
763 Flags |= bitc::AllowReciprocal;
764 }
765
766 return Flags;
767}
768
769unsigned
771 switch (Linkage) {
773 return 0;
775 return 16;
777 return 2;
779 return 3;
781 return 18;
783 return 7;
785 return 8;
787 return 9;
789 return 17;
791 return 19;
793 return 12;
794 }
795 llvm_unreachable("Invalid linkage");
796}
797
801
803 switch (GV.getVisibility()) {
805 return 0;
807 return 1;
809 return 2;
810 }
811 llvm_unreachable("Invalid visibility");
812}
813
815 switch (GV.getDLLStorageClass()) {
817 return 0;
819 return 1;
821 return 2;
822 }
823 llvm_unreachable("Invalid DLL storage class");
824}
825
827 switch (GV.getThreadLocalMode()) {
829 return 0;
831 return 1;
833 return 2;
835 return 3;
837 return 4;
838 }
839 llvm_unreachable("Invalid TLS model");
840}
841
857
858////////////////////////////////////////////////////////////////////////////////
859/// Begin DXILBitcodeWriter Implementation
860////////////////////////////////////////////////////////////////////////////////
861
862void DXILBitcodeWriter::writeAttributeGroupTable() {
863 const std::vector<ValueEnumerator::IndexAndAttrSet> &AttrGrps =
864 VE.getAttributeGroups();
865 if (AttrGrps.empty())
866 return;
867
869
871 for (ValueEnumerator::IndexAndAttrSet Pair : AttrGrps) {
872 unsigned AttrListIndex = Pair.first;
873 AttributeSet AS = Pair.second;
874 Record.push_back(VE.getAttributeGroupID(Pair));
875 Record.push_back(AttrListIndex);
876
877 for (Attribute Attr : AS) {
878 if (Attr.isEnumAttribute()) {
879 uint64_t Val = getAttrKindEncoding(Attr.getKindAsEnum());
881 "DXIL does not support attributes above ATTR_KIND_ARGMEMONLY");
882 Record.push_back(0);
883 Record.push_back(Val);
884 } else if (Attr.isIntAttribute()) {
885 if (Attr.getKindAsEnum() == Attribute::AttrKind::Memory) {
886 MemoryEffects ME = Attr.getMemoryEffects();
887 if (ME.doesNotAccessMemory()) {
888 Record.push_back(0);
890 } else {
891 if (ME.onlyReadsMemory()) {
892 Record.push_back(0);
894 }
895 if (ME.onlyAccessesArgPointees()) {
896 Record.push_back(0);
898 }
899 }
900 } else {
901 uint64_t Val = getAttrKindEncoding(Attr.getKindAsEnum());
903 "DXIL does not support attributes above ATTR_KIND_ARGMEMONLY");
904 Record.push_back(1);
905 Record.push_back(Val);
906 Record.push_back(Attr.getValueAsInt());
907 }
908 } else {
909 StringRef Kind = Attr.getKindAsString();
910 StringRef Val = Attr.getValueAsString();
911
912 Record.push_back(Val.empty() ? 3 : 4);
913 Record.append(Kind.begin(), Kind.end());
914 Record.push_back(0);
915 if (!Val.empty()) {
916 Record.append(Val.begin(), Val.end());
917 Record.push_back(0);
918 }
919 }
920 }
921
922 Stream.EmitRecord(bitc::PARAMATTR_GRP_CODE_ENTRY, Record);
923 Record.clear();
924 }
925
926 Stream.ExitBlock();
927}
928
929void DXILBitcodeWriter::writeAttributeTable() {
930 const std::vector<AttributeList> &Attrs = VE.getAttributeLists();
931 if (Attrs.empty())
932 return;
933
934 Stream.EnterSubblock(bitc::PARAMATTR_BLOCK_ID, 3);
935
936 SmallVector<uint64_t, 64> Record;
937 for (AttributeList AL : Attrs) {
938 for (unsigned i : AL.indexes()) {
939 AttributeSet AS = AL.getAttributes(i);
940 if (AS.hasAttributes())
941 Record.push_back(VE.getAttributeGroupID({i, AS}));
942 }
943
944 Stream.EmitRecord(bitc::PARAMATTR_CODE_ENTRY, Record);
945 Record.clear();
946 }
947
948 Stream.ExitBlock();
949}
950
951/// WriteTypeTable - Write out the type table for a module.
952void DXILBitcodeWriter::writeTypeTable() {
953 const ValueEnumerator::TypeList &TypeList = VE.getTypes();
954
955 Stream.EnterSubblock(bitc::TYPE_BLOCK_ID_NEW, 4 /*count from # abbrevs */);
956 SmallVector<uint64_t, 64> TypeVals;
957
958 uint64_t NumBits = VE.computeBitsRequiredForTypeIndices();
959
960 // Abbrev for TYPE_CODE_POINTER.
961 auto Abbv = std::make_shared<BitCodeAbbrev>();
962 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_POINTER));
963 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
964 Abbv->Add(BitCodeAbbrevOp(0)); // Addrspace = 0
965 unsigned PtrAbbrev = Stream.EmitAbbrev(std::move(Abbv));
966
967 // Abbrev for TYPE_CODE_FUNCTION.
968 Abbv = std::make_shared<BitCodeAbbrev>();
969 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_FUNCTION));
970 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isvararg
971 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
972 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
973 unsigned FunctionAbbrev = Stream.EmitAbbrev(std::move(Abbv));
974
975 // Abbrev for TYPE_CODE_STRUCT_ANON.
976 Abbv = std::make_shared<BitCodeAbbrev>();
977 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_ANON));
978 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ispacked
979 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
980 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
981 unsigned StructAnonAbbrev = Stream.EmitAbbrev(std::move(Abbv));
982
983 // Abbrev for TYPE_CODE_STRUCT_NAME.
984 Abbv = std::make_shared<BitCodeAbbrev>();
985 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_NAME));
986 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
987 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
988 unsigned StructNameAbbrev = Stream.EmitAbbrev(std::move(Abbv));
989
990 // Abbrev for TYPE_CODE_STRUCT_NAMED.
991 Abbv = std::make_shared<BitCodeAbbrev>();
992 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_NAMED));
993 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ispacked
994 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
995 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
996 unsigned StructNamedAbbrev = Stream.EmitAbbrev(std::move(Abbv));
997
998 // Abbrev for TYPE_CODE_ARRAY.
999 Abbv = std::make_shared<BitCodeAbbrev>();
1000 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_ARRAY));
1001 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // size
1002 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
1003 unsigned ArrayAbbrev = Stream.EmitAbbrev(std::move(Abbv));
1004
1005 // Emit an entry count so the reader can reserve space.
1006 TypeVals.push_back(TypeList.size());
1007 Stream.EmitRecord(bitc::TYPE_CODE_NUMENTRY, TypeVals);
1008 TypeVals.clear();
1009
1010 // Loop over all of the types, emitting each in turn.
1011 for (Type *T : TypeList) {
1012 int AbbrevToUse = 0;
1013 unsigned Code = 0;
1014
1015 switch (T->getTypeID()) {
1016 case Type::BFloatTyID:
1017 case Type::X86_AMXTyID:
1018 case Type::TokenTyID:
1020 llvm_unreachable("These should never be used!!!");
1021 break;
1022 case Type::VoidTyID:
1024 break;
1025 case Type::HalfTyID:
1027 break;
1028 case Type::FloatTyID:
1030 break;
1031 case Type::DoubleTyID:
1033 break;
1034 case Type::X86_FP80TyID:
1036 break;
1037 case Type::FP128TyID:
1039 break;
1042 break;
1043 case Type::LabelTyID:
1045 break;
1046 case Type::MetadataTyID:
1048 break;
1049 case Type::ByteTyID:
1050 // BYTE: [width]
1051 // Note: we downgrade by converting to the equivalent integer.
1053 TypeVals.push_back(T->getByteBitWidth());
1054 break;
1055 case Type::IntegerTyID:
1056 // INTEGER: [width]
1059 break;
1061 TypedPointerType *PTy = cast<TypedPointerType>(T);
1062 // POINTER: [pointee type, address space]
1064 TypeVals.push_back(getTypeID(PTy->getElementType()));
1065 unsigned AddressSpace = PTy->getAddressSpace();
1066 TypeVals.push_back(AddressSpace);
1067 if (AddressSpace == 0)
1068 AbbrevToUse = PtrAbbrev;
1069 break;
1070 }
1071 case Type::PointerTyID: {
1072 // POINTER: [pointee type, address space]
1073 // Emitting an empty struct type for the pointer's type allows this to be
1074 // order-independent. Non-struct types must be emitted in bitcode before
1075 // they can be referenced.
1076 TypeVals.push_back(false);
1079 "dxilOpaquePtrReservedName", StructNameAbbrev);
1080 break;
1081 }
1082 case Type::FunctionTyID: {
1083 FunctionType *FT = cast<FunctionType>(T);
1084 // FUNCTION: [isvararg, retty, paramty x N]
1086 TypeVals.push_back(FT->isVarArg());
1087 TypeVals.push_back(getTypeID(FT->getReturnType()));
1088 for (Type *PTy : FT->params())
1089 TypeVals.push_back(getTypeID(PTy));
1090 AbbrevToUse = FunctionAbbrev;
1091 break;
1092 }
1093 case Type::StructTyID: {
1094 StructType *ST = cast<StructType>(T);
1095 // STRUCT: [ispacked, eltty x N]
1096 TypeVals.push_back(ST->isPacked());
1097 // Output all of the element types.
1098 for (Type *ElTy : ST->elements())
1099 TypeVals.push_back(getTypeID(ElTy));
1100
1101 if (ST->isLiteral()) {
1103 AbbrevToUse = StructAnonAbbrev;
1104 } else {
1105 if (ST->isOpaque()) {
1107 } else {
1109 AbbrevToUse = StructNamedAbbrev;
1110 }
1111
1112 // Emit the name if it is present.
1113 if (!ST->getName().empty())
1115 StructNameAbbrev);
1116 }
1117 break;
1118 }
1119 case Type::ArrayTyID: {
1121 // ARRAY: [numelts, eltty]
1123 TypeVals.push_back(AT->getNumElements());
1124 TypeVals.push_back(getTypeID(AT->getElementType()));
1125 AbbrevToUse = ArrayAbbrev;
1126 break;
1127 }
1131 // VECTOR [numelts, eltty]
1133 TypeVals.push_back(VT->getElementCount().getKnownMinValue());
1134 TypeVals.push_back(getTypeID(VT->getElementType()));
1135 break;
1136 }
1137 }
1138
1139 // Emit the finished record.
1140 Stream.EmitRecord(Code, TypeVals, AbbrevToUse);
1141 TypeVals.clear();
1142 }
1143
1144 Stream.ExitBlock();
1145}
1146
1147void DXILBitcodeWriter::writeComdats() {
1149 for (const Comdat *C : VE.getComdats()) {
1150 // COMDAT: [selection_kind, name]
1152 size_t Size = C->getName().size();
1154 Vals.push_back(Size);
1155 for (char Chr : C->getName())
1156 Vals.push_back((unsigned char)Chr);
1157 Stream.EmitRecord(bitc::MODULE_CODE_COMDAT, Vals, /*AbbrevToUse=*/0);
1158 Vals.clear();
1159 }
1160}
1161
1162void DXILBitcodeWriter::writeValueSymbolTableForwardDecl() {}
1163
1164/// Emit top-level description of module, including target triple, inline asm,
1165/// descriptors for global variables, and function prototype info.
1166/// Returns the bit offset to backpatch with the location of the real VST.
1167void DXILBitcodeWriter::writeModuleInfo() {
1168 // Emit various pieces of data attached to a module.
1169
1170 // We need to hardcode a triple and datalayout that's compatible with the
1171 // historical DXIL triple and datalayout from DXC.
1172 StringRef Triple = "dxil-ms-dx";
1173 StringRef DL = "e-m:e-p:32:32-i1:32-i8:8-i16:16-i32:32-i64:64-"
1174 "f16:16-f32:32-f64:64-n8:16:32:64";
1175 writeStringRecord(Stream, bitc::MODULE_CODE_TRIPLE, Triple, 0 /*TODO*/);
1177
1178 // The original bitcode writer wrote inline assembly here. Inline assembly
1179 // isn't valid in DXIL, so this is removed.
1180
1181 // Emit information about sections and GC, computing how many there are. Also
1182 // compute the maximum alignment value.
1183 std::map<std::string, unsigned> SectionMap;
1184 std::map<std::string, unsigned> GCMap;
1185 MaybeAlign MaxAlignment;
1186 unsigned MaxGlobalType = 0;
1187 const auto UpdateMaxAlignment = [&MaxAlignment](const MaybeAlign A) {
1188 if (A)
1189 MaxAlignment = !MaxAlignment ? *A : std::max(*MaxAlignment, *A);
1190 };
1191 for (const GlobalVariable &GV : M.globals()) {
1192 UpdateMaxAlignment(GV.getAlign());
1193 // Use getGlobalObjectValueTypeID to look up the enumerated type ID for
1194 // Global Variable types.
1195 MaxGlobalType = std::max(
1196 MaxGlobalType, getGlobalObjectValueTypeID(GV.getValueType(), &GV));
1197 if (GV.hasSection()) {
1198 // Give section names unique ID's.
1199 unsigned &Entry = SectionMap[std::string(GV.getSection())];
1200 if (!Entry) {
1202 GV.getSection(), 0 /*TODO*/);
1203 Entry = SectionMap.size();
1204 }
1205 }
1206 }
1207 for (const Function &F : M) {
1208 UpdateMaxAlignment(F.getAlign());
1209 if (F.hasSection()) {
1210 // Give section names unique ID's.
1211 unsigned &Entry = SectionMap[std::string(F.getSection())];
1212 if (!Entry) {
1214 0 /*TODO*/);
1215 Entry = SectionMap.size();
1216 }
1217 }
1218 if (F.hasGC()) {
1219 // Same for GC names.
1220 unsigned &Entry = GCMap[F.getGC()];
1221 if (!Entry) {
1223 0 /*TODO*/);
1224 Entry = GCMap.size();
1225 }
1226 }
1227 }
1228
1229 // Emit abbrev for globals, now that we know # sections and max alignment.
1230 unsigned SimpleGVarAbbrev = 0;
1231 if (!M.global_empty()) {
1232 // Add an abbrev for common globals with no visibility or thread
1233 // localness.
1234 auto Abbv = std::make_shared<BitCodeAbbrev>();
1235 Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_GLOBALVAR));
1236 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
1237 Log2_32_Ceil(MaxGlobalType + 1)));
1238 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // AddrSpace << 2
1239 //| explicitType << 1
1240 //| constant
1241 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Initializer.
1242 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 5)); // Linkage.
1243 if (!MaxAlignment) // Alignment.
1244 Abbv->Add(BitCodeAbbrevOp(0));
1245 else {
1246 unsigned MaxEncAlignment = getEncodedAlign(MaxAlignment);
1247 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
1248 Log2_32_Ceil(MaxEncAlignment + 1)));
1249 }
1250 if (SectionMap.empty()) // Section.
1251 Abbv->Add(BitCodeAbbrevOp(0));
1252 else
1253 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
1254 Log2_32_Ceil(SectionMap.size() + 1)));
1255 // Don't bother emitting vis + thread local.
1256 SimpleGVarAbbrev = Stream.EmitAbbrev(std::move(Abbv));
1257 }
1258
1259 // Emit the global variable information.
1261 for (const GlobalVariable &GV : M.globals()) {
1262 unsigned AbbrevToUse = 0;
1263
1264 // GLOBALVAR: [type, isconst, initid,
1265 // linkage, alignment, section, visibility, threadlocal,
1266 // unnamed_addr, externally_initialized, dllstorageclass,
1267 // comdat]
1268 Vals.push_back(getGlobalObjectValueTypeID(GV.getValueType(), &GV));
1269 Vals.push_back(
1270 GV.getType()->getAddressSpace() << 2 | 2 |
1271 (GV.isConstant() ? 1 : 0)); // HLSL Change - bitwise | was used with
1272 // unsigned int and bool
1273 Vals.push_back(
1274 GV.isDeclaration() ? 0 : (VE.getValueID(GV.getInitializer()) + 1));
1275 Vals.push_back(getEncodedLinkage(GV));
1276 Vals.push_back(getEncodedAlign(GV.getAlign()));
1277 Vals.push_back(GV.hasSection() ? SectionMap[std::string(GV.getSection())]
1278 : 0);
1279 if (GV.isThreadLocal() ||
1280 GV.getVisibility() != GlobalValue::DefaultVisibility ||
1281 GV.getUnnamedAddr() != GlobalValue::UnnamedAddr::None ||
1282 GV.isExternallyInitialized() ||
1283 GV.getDLLStorageClass() != GlobalValue::DefaultStorageClass ||
1284 GV.hasComdat()) {
1287 Vals.push_back(GV.getUnnamedAddr() != GlobalValue::UnnamedAddr::None);
1288 Vals.push_back(GV.isExternallyInitialized());
1290 Vals.push_back(GV.hasComdat() ? VE.getComdatID(GV.getComdat()) : 0);
1291 } else {
1292 AbbrevToUse = SimpleGVarAbbrev;
1293 }
1294
1295 Stream.EmitRecord(bitc::MODULE_CODE_GLOBALVAR, Vals, AbbrevToUse);
1296 Vals.clear();
1297 }
1298
1299 // Emit the function proto information.
1300 for (const Function &OrigF : M) {
1301 const Function &F = VE.getDXILFunction(OrigF);
1302
1303 // FUNCTION: [type, callingconv, isproto, linkage, paramattrs, alignment,
1304 // section, visibility, gc, unnamed_addr, prologuedata,
1305 // dllstorageclass, comdat, prefixdata, personalityfn]
1306 Vals.push_back(getGlobalObjectValueTypeID(F.getFunctionType(), &F));
1307 Vals.push_back(F.getCallingConv());
1308 Vals.push_back(F.isDeclaration());
1310 Vals.push_back(VE.getAttributeListID(F.getAttributes()));
1311 Vals.push_back(getEncodedAlign(F.getAlign()));
1312 Vals.push_back(F.hasSection() ? SectionMap[std::string(F.getSection())]
1313 : 0);
1315 Vals.push_back(F.hasGC() ? GCMap[F.getGC()] : 0);
1316 Vals.push_back(F.getUnnamedAddr() != GlobalValue::UnnamedAddr::None);
1317 Vals.push_back(
1318 F.hasPrologueData() ? (VE.getValueID(F.getPrologueData()) + 1) : 0);
1320 Vals.push_back(F.hasComdat() ? VE.getComdatID(F.getComdat()) : 0);
1321 Vals.push_back(F.hasPrefixData() ? (VE.getValueID(F.getPrefixData()) + 1)
1322 : 0);
1323 Vals.push_back(
1324 F.hasPersonalityFn() ? (VE.getValueID(F.getPersonalityFn()) + 1) : 0);
1325
1326 unsigned AbbrevToUse = 0;
1327 Stream.EmitRecord(bitc::MODULE_CODE_FUNCTION, Vals, AbbrevToUse);
1328 Vals.clear();
1329 }
1330
1331 // Emit the alias information.
1332 for (const GlobalAlias &A : M.aliases()) {
1333 // ALIAS: [alias type, aliasee val#, linkage, visibility]
1334 Vals.push_back(getTypeID(A.getValueType(), &A));
1335 Vals.push_back(VE.getValueID(A.getAliasee()));
1340 Vals.push_back(A.getUnnamedAddr() != GlobalValue::UnnamedAddr::None);
1341 unsigned AbbrevToUse = 0;
1342 Stream.EmitRecord(bitc::MODULE_CODE_ALIAS_OLD, Vals, AbbrevToUse);
1343 Vals.clear();
1344 }
1345}
1346
1347void DXILBitcodeWriter::writeValueAsMetadata(
1348 const ValueAsMetadata *MD, SmallVectorImpl<uint64_t> &Record) {
1349 // Mimic an MDNode with a value as one operand.
1350 Value *V = MD->getValue();
1351 Type *Ty = V->getType();
1353 Ty = TypedPointerType::get(F->getFunctionType(), F->getAddressSpace());
1354 else if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
1355 Ty = TypedPointerType::get(GV->getValueType(), GV->getAddressSpace());
1356 Record.push_back(getTypeID(Ty, V));
1357 Record.push_back(VE.getValueID(V));
1358 Stream.EmitRecord(bitc::METADATA_VALUE, Record, 0);
1359 Record.clear();
1360}
1361
1362void DXILBitcodeWriter::writeMDTuple(const MDTuple *N,
1363 SmallVectorImpl<uint64_t> &Record,
1364 unsigned Abbrev) {
1365 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
1366 Metadata *MD = N->getOperand(i);
1367 assert(!(MD && isa<LocalAsMetadata>(MD)) &&
1368 "Unexpected function-local metadata");
1369 Record.push_back(VE.getMetadataOrNullID(MD));
1370 }
1371 Stream.EmitRecord(N->isDistinct() ? bitc::METADATA_DISTINCT_NODE
1373 Record, Abbrev);
1374 Record.clear();
1375}
1376
1377void DXILBitcodeWriter::writeDILocation(const DILocation *N,
1378 SmallVectorImpl<uint64_t> &Record,
1379 unsigned &Abbrev) {
1380 if (!Abbrev)
1381 Abbrev = createDILocationAbbrev();
1382 Record.push_back(N->isDistinct());
1383 Record.push_back(N->getLine());
1384 Record.push_back(N->getColumn());
1385 Record.push_back(VE.getMetadataID(N->getScope()));
1386 Record.push_back(VE.getMetadataOrNullID(N->getInlinedAt()));
1387
1388 Stream.EmitRecord(bitc::METADATA_LOCATION, Record, Abbrev);
1389 Record.clear();
1390}
1391
1393 int64_t I = Val.getSExtValue();
1394 uint64_t U = I;
1395 return I < 0 ? ~(U << 1) : U << 1;
1396}
1397
1398void DXILBitcodeWriter::writeDISubrange(const DISubrange *N,
1399 SmallVectorImpl<uint64_t> &Record,
1400 unsigned Abbrev) {
1401 Record.push_back(N->isDistinct());
1402
1403 // Count may be a reference to a DILocalVariable or DIGlobalVariable
1404 // in case of C99 VLA. Non-constant count It is not supported by
1405 // DXIL, so we emit a subrange of -1 (empty).
1406 if (ConstantInt *Count = dyn_cast<ConstantInt *>(N->getCount())) {
1407 Record.push_back(Count->getValue().getSExtValue());
1408 } else {
1409 Record.push_back(-1);
1410 }
1411
1412 // Similarly, non constant lower bound is not allowed here.
1413 DISubrange::BoundType LowerBound = N->getLowerBound();
1414 if (!LowerBound.isNull() && isa<ConstantInt *>(LowerBound)) {
1415 Record.push_back(rotateSign(cast<ConstantInt *>(LowerBound)->getValue()));
1416 } else {
1417 Record.push_back(0);
1418 }
1419
1420 Stream.EmitRecord(bitc::METADATA_SUBRANGE, Record, Abbrev);
1421 Record.clear();
1422}
1423
1424void DXILBitcodeWriter::writeDIEnumerator(const DIEnumerator *N,
1425 SmallVectorImpl<uint64_t> &Record,
1426 unsigned Abbrev) {
1427 Record.push_back(N->isDistinct());
1428 Record.push_back(rotateSign(N->getValue()));
1429 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1430
1431 Stream.EmitRecord(bitc::METADATA_ENUMERATOR, Record, Abbrev);
1432 Record.clear();
1433}
1434
1435void DXILBitcodeWriter::writeDIBasicType(const DIBasicType *N,
1436 SmallVectorImpl<uint64_t> &Record,
1437 unsigned Abbrev) {
1438 Record.push_back(N->isDistinct());
1439 Record.push_back(N->getTag());
1440 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1441 Record.push_back(N->getSizeInBits());
1442 Record.push_back(N->getAlignInBits());
1443 Record.push_back(N->getEncoding());
1444
1445 Stream.EmitRecord(bitc::METADATA_BASIC_TYPE, Record, Abbrev);
1446 Record.clear();
1447}
1448
1449void DXILBitcodeWriter::writeDIDerivedType(const DIDerivedType *N,
1450 SmallVectorImpl<uint64_t> &Record,
1451 unsigned Abbrev) {
1452 Record.push_back(N->isDistinct());
1453 Record.push_back(N->getTag());
1454 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1455 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1456 Record.push_back(N->getLine());
1457 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1458 Record.push_back(VE.getMetadataOrNullID(N->getBaseType()));
1459 Record.push_back(N->getSizeInBits());
1460 Record.push_back(N->getAlignInBits());
1461 Record.push_back(N->getOffsetInBits());
1462 Record.push_back(N->getFlags());
1463 Record.push_back(VE.getMetadataOrNullID(N->getExtraData()));
1464
1465 Stream.EmitRecord(bitc::METADATA_DERIVED_TYPE, Record, Abbrev);
1466 Record.clear();
1467}
1468
1469void DXILBitcodeWriter::writeDICompositeType(const DICompositeType *N,
1470 SmallVectorImpl<uint64_t> &Record,
1471 unsigned Abbrev) {
1472 Record.push_back(N->isDistinct());
1473 Record.push_back(N->getTag());
1474 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1475 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1476 Record.push_back(N->getLine());
1477 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1478 Record.push_back(VE.getMetadataOrNullID(N->getBaseType()));
1479 Record.push_back(N->getSizeInBits());
1480 Record.push_back(N->getAlignInBits());
1481 Record.push_back(N->getOffsetInBits());
1482 Record.push_back(N->getFlags());
1483 Record.push_back(VE.getMetadataOrNullID(N->getElements().get()));
1484 Record.push_back(N->getRuntimeLang());
1485 Record.push_back(VE.getMetadataOrNullID(N->getVTableHolder()));
1486 Record.push_back(VE.getMetadataOrNullID(N->getTemplateParams().get()));
1487 Record.push_back(VE.getMetadataOrNullID(N->getRawIdentifier()));
1488
1489 Stream.EmitRecord(bitc::METADATA_COMPOSITE_TYPE, Record, Abbrev);
1490 Record.clear();
1491}
1492
1493void DXILBitcodeWriter::writeDISubroutineType(const DISubroutineType *N,
1494 SmallVectorImpl<uint64_t> &Record,
1495 unsigned Abbrev) {
1496 Record.push_back(N->isDistinct());
1497 Record.push_back(N->getFlags());
1498 Record.push_back(VE.getMetadataOrNullID(N->getTypeArray().get()));
1499
1500 Stream.EmitRecord(bitc::METADATA_SUBROUTINE_TYPE, Record, Abbrev);
1501 Record.clear();
1502}
1503
1504void DXILBitcodeWriter::writeDIFile(const DIFile *N,
1505 SmallVectorImpl<uint64_t> &Record,
1506 unsigned Abbrev) {
1507 Record.push_back(N->isDistinct());
1508 Record.push_back(VE.getMetadataOrNullID(N->getRawFilename()));
1509 Record.push_back(VE.getMetadataOrNullID(N->getRawDirectory()));
1510
1511 Stream.EmitRecord(bitc::METADATA_FILE, Record, Abbrev);
1512 Record.clear();
1513}
1514
1515void DXILBitcodeWriter::writeDICompileUnit(const DICompileUnit *N,
1516 SmallVectorImpl<uint64_t> &Record,
1517 unsigned Abbrev) {
1518 Record.push_back(N->isDistinct());
1519 Record.push_back(N->getSourceLanguage().getUnversionedName());
1520 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1521 Record.push_back(VE.getMetadataOrNullID(N->getRawProducer()));
1522 Record.push_back(N->isOptimized());
1523 Record.push_back(VE.getMetadataOrNullID(N->getRawFlags()));
1524 Record.push_back(N->getRuntimeVersion());
1525 Record.push_back(VE.getMetadataOrNullID(N->getRawSplitDebugFilename()));
1526 Record.push_back(N->getEmissionKind());
1527 Record.push_back(VE.getMetadataOrNullID(N->getEnumTypes().get()));
1528 Record.push_back(VE.getMetadataOrNullID(N->getRetainedTypes().get()));
1529 Record.push_back(VE.getMetadataOrNullID(DebugInfo.MDExtra.lookup(N)));
1530 Record.push_back(VE.getMetadataOrNullID(N->getGlobalVariables().get()));
1531 Record.push_back(VE.getMetadataOrNullID(N->getImportedEntities().get()));
1532 Record.push_back(N->getDWOId());
1533
1534 Stream.EmitRecord(bitc::METADATA_COMPILE_UNIT, Record, Abbrev);
1535 Record.clear();
1536}
1537
1538void DXILBitcodeWriter::writeDISubprogram(const DISubprogram *N,
1539 SmallVectorImpl<uint64_t> &Record,
1540 unsigned Abbrev) {
1541 Record.push_back(N->isDistinct());
1542 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1543 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1544 Record.push_back(VE.getMetadataOrNullID(N->getRawLinkageName()));
1545 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1546 Record.push_back(N->getLine());
1547 Record.push_back(VE.getMetadataOrNullID(N->getType()));
1548 Record.push_back(N->isLocalToUnit());
1549 Record.push_back(N->isDefinition());
1550 Record.push_back(N->getScopeLine());
1551 Record.push_back(VE.getMetadataOrNullID(N->getContainingType()));
1552 Record.push_back(N->getVirtuality());
1553 Record.push_back(N->getVirtualIndex());
1554 Record.push_back(N->getFlags());
1555 Record.push_back(N->isOptimized());
1556 Record.push_back(VE.getMetadataOrNullID(DebugInfo.MDExtra.lookup(N)));
1557 Record.push_back(VE.getMetadataOrNullID(N->getTemplateParams().get()));
1558 Record.push_back(VE.getMetadataOrNullID(N->getDeclaration()));
1559 Record.push_back(VE.getMetadataOrNullID(N->getRetainedNodes().get()));
1560
1561 Stream.EmitRecord(bitc::METADATA_SUBPROGRAM, Record, Abbrev);
1562 Record.clear();
1563}
1564
1565void DXILBitcodeWriter::writeDILexicalBlock(const DILexicalBlock *N,
1566 SmallVectorImpl<uint64_t> &Record,
1567 unsigned Abbrev) {
1568 Record.push_back(N->isDistinct());
1569 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1570 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1571 Record.push_back(N->getLine());
1572 Record.push_back(N->getColumn());
1573
1574 Stream.EmitRecord(bitc::METADATA_LEXICAL_BLOCK, Record, Abbrev);
1575 Record.clear();
1576}
1577
1578void DXILBitcodeWriter::writeDILexicalBlockFile(
1579 const DILexicalBlockFile *N, SmallVectorImpl<uint64_t> &Record,
1580 unsigned Abbrev) {
1581 Record.push_back(N->isDistinct());
1582 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1583 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1584 Record.push_back(N->getDiscriminator());
1585
1586 Stream.EmitRecord(bitc::METADATA_LEXICAL_BLOCK_FILE, Record, Abbrev);
1587 Record.clear();
1588}
1589
1590void DXILBitcodeWriter::writeDINamespace(const DINamespace *N,
1591 SmallVectorImpl<uint64_t> &Record,
1592 unsigned Abbrev) {
1593 Record.push_back(N->isDistinct());
1594 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1595 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1596 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1597 Record.push_back(/* line number */ 0);
1598
1599 Stream.EmitRecord(bitc::METADATA_NAMESPACE, Record, Abbrev);
1600 Record.clear();
1601}
1602
1603void DXILBitcodeWriter::writeDIModule(const DIModule *N,
1604 SmallVectorImpl<uint64_t> &Record,
1605 unsigned Abbrev) {
1606 Record.push_back(N->isDistinct());
1607 for (auto &I : N->operands())
1608 Record.push_back(VE.getMetadataOrNullID(I));
1609
1610 Stream.EmitRecord(bitc::METADATA_MODULE, Record, Abbrev);
1611 Record.clear();
1612}
1613
1614void DXILBitcodeWriter::writeDITemplateTypeParameter(
1615 const DITemplateTypeParameter *N, SmallVectorImpl<uint64_t> &Record,
1616 unsigned Abbrev) {
1617 Record.push_back(N->isDistinct());
1618 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1619 Record.push_back(VE.getMetadataOrNullID(N->getType()));
1620
1621 Stream.EmitRecord(bitc::METADATA_TEMPLATE_TYPE, Record, Abbrev);
1622 Record.clear();
1623}
1624
1625void DXILBitcodeWriter::writeDITemplateValueParameter(
1626 const DITemplateValueParameter *N, SmallVectorImpl<uint64_t> &Record,
1627 unsigned Abbrev) {
1628 Record.push_back(N->isDistinct());
1629 Record.push_back(N->getTag());
1630 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1631 Record.push_back(VE.getMetadataOrNullID(N->getType()));
1632 Record.push_back(VE.getMetadataOrNullID(N->getValue()));
1633
1634 Stream.EmitRecord(bitc::METADATA_TEMPLATE_VALUE, Record, Abbrev);
1635 Record.clear();
1636}
1637
1638void DXILBitcodeWriter::writeDIGlobalVariable(const DIGlobalVariable *N,
1639 SmallVectorImpl<uint64_t> &Record,
1640 unsigned Abbrev) {
1641 Record.push_back(N->isDistinct());
1642 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1643 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1644 Record.push_back(VE.getMetadataOrNullID(N->getRawLinkageName()));
1645 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1646 Record.push_back(N->getLine());
1647 Record.push_back(VE.getMetadataOrNullID(N->getType()));
1648 Record.push_back(N->isLocalToUnit());
1649 Record.push_back(N->isDefinition());
1650 Record.push_back(VE.getMetadataOrNullID(DebugInfo.MDExtra.lookup(N)));
1651 Record.push_back(VE.getMetadataOrNullID(N->getStaticDataMemberDeclaration()));
1652
1653 Stream.EmitRecord(bitc::METADATA_GLOBAL_VAR, Record, Abbrev);
1654 Record.clear();
1655}
1656
1657void DXILBitcodeWriter::writeDILocalVariable(const DILocalVariable *N,
1658 SmallVectorImpl<uint64_t> &Record,
1659 unsigned Abbrev) {
1660 constexpr unsigned DW_TAG_auto_variable = 0x0100;
1661 constexpr unsigned DW_TAG_arg_variable = 0x0101;
1662 Record.push_back(N->isDistinct());
1663 assert(N->getTag() == dwarf::DW_TAG_variable);
1664 Record.push_back(N->getArg() ? DW_TAG_arg_variable : DW_TAG_auto_variable);
1665 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1666 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1667 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1668 Record.push_back(N->getLine());
1669 Record.push_back(VE.getMetadataOrNullID(N->getType()));
1670 Record.push_back(N->getArg());
1671 Record.push_back(N->getFlags());
1672
1673 Stream.EmitRecord(bitc::METADATA_LOCAL_VAR, Record, Abbrev);
1674 Record.clear();
1675}
1676
1677void DXILBitcodeWriter::writeDIExpression(const DIExpression *N,
1678 SmallVectorImpl<uint64_t> &Record,
1679 unsigned Abbrev) {
1680 Record.reserve(N->getElements().size() + 1);
1681
1682 Record.push_back(N->isDistinct());
1683 Record.append(N->elements_begin(), N->elements_end());
1684
1685 Stream.EmitRecord(bitc::METADATA_EXPRESSION, Record, Abbrev);
1686 Record.clear();
1687}
1688
1689void DXILBitcodeWriter::writeDIObjCProperty(const DIObjCProperty *N,
1690 SmallVectorImpl<uint64_t> &Record,
1691 unsigned Abbrev) {
1692 llvm_unreachable("DXIL does not support objc!!!");
1693}
1694
1695void DXILBitcodeWriter::writeDIImportedEntity(const DIImportedEntity *N,
1696 SmallVectorImpl<uint64_t> &Record,
1697 unsigned Abbrev) {
1698 Record.push_back(N->isDistinct());
1699 Record.push_back(N->getTag());
1700 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1701 Record.push_back(VE.getMetadataOrNullID(N->getEntity()));
1702 Record.push_back(N->getLine());
1703 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1704
1705 Stream.EmitRecord(bitc::METADATA_IMPORTED_ENTITY, Record, Abbrev);
1706 Record.clear();
1707}
1708
1709unsigned DXILBitcodeWriter::createDILocationAbbrev() {
1710 // Abbrev for METADATA_LOCATION.
1711 //
1712 // Assume the column is usually under 128, and always output the inlined-at
1713 // location (it's never more expensive than building an array size 1).
1714 std::shared_ptr<BitCodeAbbrev> Abbv = std::make_shared<BitCodeAbbrev>();
1715 Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_LOCATION));
1716 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
1717 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1718 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1719 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1720 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1721 return Stream.EmitAbbrev(std::move(Abbv));
1722}
1723
1724unsigned DXILBitcodeWriter::createGenericDINodeAbbrev() {
1725 // Abbrev for METADATA_GENERIC_DEBUG.
1726 //
1727 // Assume the column is usually under 128, and always output the inlined-at
1728 // location (it's never more expensive than building an array size 1).
1729 std::shared_ptr<BitCodeAbbrev> Abbv = std::make_shared<BitCodeAbbrev>();
1730 Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_GENERIC_DEBUG));
1731 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
1732 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1733 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
1734 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1735 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1736 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1737 return Stream.EmitAbbrev(std::move(Abbv));
1738}
1739
1740void DXILBitcodeWriter::writeMetadataRecords(ArrayRef<const Metadata *> MDs,
1741 SmallVectorImpl<uint64_t> &Record,
1742 std::vector<unsigned> *MDAbbrevs,
1743 std::vector<uint64_t> *IndexPos) {
1744 if (MDs.empty())
1745 return;
1746
1747 // Initialize MDNode abbreviations.
1748#define HANDLE_MDNODE_LEAF(CLASS) unsigned CLASS##Abbrev = 0;
1749#include "llvm/IR/Metadata.def"
1750
1751 for (const Metadata *MD : MDs) {
1752 if (IndexPos)
1753 IndexPos->push_back(Stream.GetCurrentBitNo());
1754 if (const MDNode *N = dyn_cast<MDNode>(MD)) {
1755 assert(N->isResolved() && "Expected forward references to be resolved");
1756
1757 switch (N->getMetadataID()) {
1758 default:
1759 llvm_unreachable("Invalid MDNode subclass");
1760#define HANDLE_MDNODE_LEAF(CLASS) \
1761 case Metadata::CLASS##Kind: \
1762 if (MDAbbrevs) \
1763 write##CLASS(cast<CLASS>(N), Record, \
1764 (*MDAbbrevs)[MetadataAbbrev::CLASS##AbbrevID]); \
1765 else \
1766 write##CLASS(cast<CLASS>(N), Record, CLASS##Abbrev); \
1767 continue;
1768#include "llvm/IR/Metadata.def"
1769 }
1770 }
1771 writeValueAsMetadata(cast<ValueAsMetadata>(MD), Record);
1772 }
1773}
1774
1775unsigned DXILBitcodeWriter::createMetadataStringsAbbrev() {
1776 auto Abbv = std::make_shared<BitCodeAbbrev>();
1777 Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_STRING_OLD));
1778 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1779 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
1780 return Stream.EmitAbbrev(std::move(Abbv));
1781}
1782
1783void DXILBitcodeWriter::writeMetadataStrings(
1784 ArrayRef<const Metadata *> Strings, SmallVectorImpl<uint64_t> &Record) {
1785 if (Strings.empty())
1786 return;
1787
1788 unsigned MDSAbbrev = createMetadataStringsAbbrev();
1789
1790 for (const Metadata *MD : Strings) {
1791 const MDString *MDS = cast<MDString>(MD);
1792 // Code: [strchar x N]
1793 Record.append(MDS->bytes_begin(), MDS->bytes_end());
1794
1795 // Emit the finished record.
1796 Stream.EmitRecord(bitc::METADATA_STRING_OLD, Record, MDSAbbrev);
1797 Record.clear();
1798 }
1799}
1800
1801void DXILBitcodeWriter::writeModuleMetadata() {
1802 if (!VE.hasMDs() && M.named_metadata_empty())
1803 return;
1804
1805 Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 5);
1806
1807 // Emit all abbrevs upfront, so that the reader can jump in the middle of the
1808 // block and load any metadata.
1809 std::vector<unsigned> MDAbbrevs;
1810
1811 MDAbbrevs.resize(MetadataAbbrev::LastPlusOne);
1812 MDAbbrevs[MetadataAbbrev::DILocationAbbrevID] = createDILocationAbbrev();
1813 MDAbbrevs[MetadataAbbrev::GenericDINodeAbbrevID] =
1814 createGenericDINodeAbbrev();
1815
1816 unsigned NameAbbrev = 0;
1817 if (!M.named_metadata_empty()) {
1818 // Abbrev for METADATA_NAME.
1819 std::shared_ptr<BitCodeAbbrev> Abbv = std::make_shared<BitCodeAbbrev>();
1820 Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_NAME));
1821 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1822 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
1823 NameAbbrev = Stream.EmitAbbrev(std::move(Abbv));
1824 }
1825
1826 SmallVector<uint64_t, 64> Record;
1827 writeMetadataStrings(VE.getMDStrings(), Record);
1828
1829 std::vector<uint64_t> IndexPos;
1830 IndexPos.reserve(VE.getNonMDStrings().size());
1831 writeMetadataRecords(VE.getNonMDStrings(), Record, &MDAbbrevs, &IndexPos);
1832
1833 // Write named metadata.
1834 for (const NamedMDNode &NMD : M.named_metadata()) {
1835 // Write name.
1836 StringRef Str = NMD.getName();
1837 Record.append(Str.bytes_begin(), Str.bytes_end());
1838 Stream.EmitRecord(bitc::METADATA_NAME, Record, NameAbbrev);
1839 Record.clear();
1840
1841 // Write named metadata operands.
1842 for (const MDNode *N : NMD.operands())
1843 Record.push_back(VE.getMetadataID(N));
1844 Stream.EmitRecord(bitc::METADATA_NAMED_NODE, Record, 0);
1845 Record.clear();
1846 }
1847
1848 Stream.ExitBlock();
1849}
1850
1851void DXILBitcodeWriter::writeFunctionMetadata(const Function &F) {
1852 if (!VE.hasMDs())
1853 return;
1854
1855 Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 4);
1856 SmallVector<uint64_t, 64> Record;
1857 writeMetadataStrings(VE.getMDStrings(), Record);
1858 writeMetadataRecords(VE.getNonMDStrings(), Record);
1859 Stream.ExitBlock();
1860}
1861
1862void DXILBitcodeWriter::writeFunctionMetadataAttachment(const Function &F) {
1863 Stream.EnterSubblock(bitc::METADATA_ATTACHMENT_ID, 3);
1864
1865 SmallVector<uint64_t, 64> Record;
1866
1867 // Write metadata attachments
1868 // METADATA_ATTACHMENT - [m x [value, [n x [id, mdnode]]]
1870 F.getAllMetadata(MDs);
1871 if (!MDs.empty()) {
1872 for (const auto &I : MDs) {
1873 if (I.first == LLVMContext::MD_dbg)
1874 continue;
1875 Record.push_back(I.first);
1876 Record.push_back(VE.getMetadataID(I.second));
1877 }
1878 }
1879 if (!Record.empty()) {
1880 Stream.EmitRecord(bitc::METADATA_ATTACHMENT, Record, 0);
1881 Record.clear();
1882 }
1883
1884 for (const BasicBlock &BB : F)
1885 for (const Instruction &OrigI : BB) {
1886 const Instruction &I = VE.getDXILInstruction(OrigI);
1887
1888 MDs.clear();
1889 I.getAllMetadataOtherThanDebugLoc(MDs);
1890
1891 // If no metadata, ignore instruction.
1892 if (MDs.empty())
1893 continue;
1894
1895 Record.push_back(VE.getInstructionID(&I));
1896
1897 for (unsigned i = 0, e = MDs.size(); i != e; ++i) {
1898 Record.push_back(MDs[i].first);
1899 Record.push_back(VE.getMetadataID(MDs[i].second));
1900 }
1901 Stream.EmitRecord(bitc::METADATA_ATTACHMENT, Record, 0);
1902 Record.clear();
1903 }
1904
1905 Stream.ExitBlock();
1906}
1907
1908void DXILBitcodeWriter::writeModuleMetadataKinds() {
1909 SmallVector<uint64_t, 64> Record;
1910
1911 // Write metadata kinds
1912 // METADATA_KIND - [n x [id, name]]
1914 M.getMDKindNames(Names);
1915
1916 if (Names.empty())
1917 return;
1918
1919 Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
1920
1921 for (unsigned MDKindID = 0, e = Names.size(); MDKindID != e; ++MDKindID) {
1922 Record.push_back(MDKindID);
1923 StringRef KName = Names[MDKindID];
1924 Record.append(KName.begin(), KName.end());
1925
1926 Stream.EmitRecord(bitc::METADATA_KIND, Record, 0);
1927 Record.clear();
1928 }
1929
1930 Stream.ExitBlock();
1931}
1932
1933void DXILBitcodeWriter::writeConstants(unsigned FirstVal, unsigned LastVal,
1934 bool isGlobal) {
1935 if (FirstVal == LastVal)
1936 return;
1937
1938 Stream.EnterSubblock(bitc::CONSTANTS_BLOCK_ID, 4);
1939
1940 unsigned AggregateAbbrev = 0;
1941 unsigned String8Abbrev = 0;
1942 unsigned CString7Abbrev = 0;
1943 unsigned CString6Abbrev = 0;
1944 // If this is a constant pool for the module, emit module-specific abbrevs.
1945 if (isGlobal) {
1946 // Abbrev for CST_CODE_AGGREGATE.
1947 auto Abbv = std::make_shared<BitCodeAbbrev>();
1948 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_AGGREGATE));
1949 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1950 Abbv->Add(
1951 BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, Log2_32_Ceil(LastVal + 1)));
1952 AggregateAbbrev = Stream.EmitAbbrev(std::move(Abbv));
1953
1954 // Abbrev for CST_CODE_STRING.
1955 Abbv = std::make_shared<BitCodeAbbrev>();
1956 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_STRING));
1957 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1958 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
1959 String8Abbrev = Stream.EmitAbbrev(std::move(Abbv));
1960 // Abbrev for CST_CODE_CSTRING.
1961 Abbv = std::make_shared<BitCodeAbbrev>();
1962 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING));
1963 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1964 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
1965 CString7Abbrev = Stream.EmitAbbrev(std::move(Abbv));
1966 // Abbrev for CST_CODE_CSTRING.
1967 Abbv = std::make_shared<BitCodeAbbrev>();
1968 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING));
1969 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1970 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
1971 CString6Abbrev = Stream.EmitAbbrev(std::move(Abbv));
1972 }
1973
1974 SmallVector<uint64_t, 64> Record;
1975
1976 const ValueEnumerator::ValueList &Vals = VE.getValues();
1977 Type *LastTy = nullptr;
1978 for (unsigned i = FirstVal; i != LastVal; ++i) {
1979 const Value *V = Vals[i].first;
1980 // If we need to switch types, do so now.
1981 if (V->getType() != LastTy) {
1982 LastTy = V->getType();
1983 Record.push_back(getTypeID(LastTy, V));
1984 Stream.EmitRecord(bitc::CST_CODE_SETTYPE, Record,
1985 CONSTANTS_SETTYPE_ABBREV);
1986 Record.clear();
1987 }
1988
1989 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
1990 Record.push_back(unsigned(IA->hasSideEffects()) |
1991 unsigned(IA->isAlignStack()) << 1 |
1992 unsigned(IA->getDialect() & 1) << 2);
1993
1994 // Add the asm string.
1995 StringRef AsmStr = IA->getAsmString();
1996 Record.push_back(AsmStr.size());
1997 Record.append(AsmStr.begin(), AsmStr.end());
1998
1999 // Add the constraint string.
2000 StringRef ConstraintStr = IA->getConstraintString();
2001 Record.push_back(ConstraintStr.size());
2002 Record.append(ConstraintStr.begin(), ConstraintStr.end());
2003 Stream.EmitRecord(bitc::CST_CODE_INLINEASM, Record);
2004 Record.clear();
2005 continue;
2006 }
2007 const Constant *C = cast<Constant>(V);
2008 unsigned Code = -1U;
2009 unsigned AbbrevToUse = 0;
2010 if (C->isNullValue()) {
2012 } else if (isa<UndefValue>(C)) {
2014 } else if (const ConstantInt *IV = dyn_cast<ConstantInt>(C)) {
2015 if (IV->getBitWidth() <= 64) {
2016 uint64_t V = IV->getSExtValue();
2017 emitSignedInt64(Record, V);
2019 AbbrevToUse = CONSTANTS_INTEGER_ABBREV;
2020 } else { // Wide integers, > 64 bits in size.
2021 // We have an arbitrary precision integer value to write whose
2022 // bit width is > 64. However, in canonical unsigned integer
2023 // format it is likely that the high bits are going to be zero.
2024 // So, we only write the number of active words.
2025 unsigned NWords = IV->getValue().getActiveWords();
2026 const uint64_t *RawWords = IV->getValue().getRawData();
2027 for (unsigned i = 0; i != NWords; ++i) {
2028 emitSignedInt64(Record, RawWords[i]);
2029 }
2031 }
2032 } else if (const ConstantByte *BV = dyn_cast<ConstantByte>(C)) {
2033 // Note: we downgrade by converting to the equivalent integer - this logic
2034 // should match the `ConstantInt` case above.
2035 if (BV->getBitWidth() <= 64) {
2036 uint64_t V = BV->getSExtValue();
2037 emitSignedInt64(Record, V);
2039 AbbrevToUse = CONSTANTS_INTEGER_ABBREV;
2040 } else { // Wide bytes, > 64 bits in size.
2041 unsigned NWords = BV->getValue().getActiveWords();
2042 const uint64_t *RawWords = BV->getValue().getRawData();
2043 for (unsigned i = 0; i != NWords; ++i) {
2044 emitSignedInt64(Record, RawWords[i]);
2045 }
2047 }
2048 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
2050 Type *Ty = CFP->getType()->getScalarType();
2051 if (Ty->isHalfTy() || Ty->isFloatTy() || Ty->isDoubleTy()) {
2052 Record.push_back(CFP->getValueAPF().bitcastToAPInt().getZExtValue());
2053 } else if (Ty->isX86_FP80Ty()) {
2054 // api needed to prevent premature destruction
2055 // bits are not in the same order as a normal i80 APInt, compensate.
2056 APInt api = CFP->getValueAPF().bitcastToAPInt();
2057 const uint64_t *p = api.getRawData();
2058 Record.push_back((p[1] << 48) | (p[0] >> 16));
2059 Record.push_back(p[0] & 0xffffLL);
2060 } else if (Ty->isFP128Ty() || Ty->isPPC_FP128Ty()) {
2061 APInt api = CFP->getValueAPF().bitcastToAPInt();
2062 const uint64_t *p = api.getRawData();
2063 Record.push_back(p[0]);
2064 Record.push_back(p[1]);
2065 } else {
2066 assert(0 && "Unknown FP type!");
2067 }
2068 } else if (isa<ConstantDataSequential>(C) &&
2069 cast<ConstantDataSequential>(C)->isString()) {
2070 const ConstantDataSequential *Str = cast<ConstantDataSequential>(C);
2071 // Emit constant strings specially.
2072 unsigned NumElts = Str->getNumElements();
2073 // If this is a null-terminated string, use the denser CSTRING encoding.
2074 if (Str->isCString()) {
2076 --NumElts; // Don't encode the null, which isn't allowed by char6.
2077 } else {
2079 AbbrevToUse = String8Abbrev;
2080 }
2081 bool isCStr7 = Code == bitc::CST_CODE_CSTRING;
2082 bool isCStrChar6 = Code == bitc::CST_CODE_CSTRING;
2083 for (unsigned i = 0; i != NumElts; ++i) {
2084 unsigned char V = Str->getElementAsInteger(i);
2085 Record.push_back(V);
2086 isCStr7 &= (V & 128) == 0;
2087 if (isCStrChar6)
2088 isCStrChar6 = BitCodeAbbrevOp::isChar6(V);
2089 }
2090
2091 if (isCStrChar6)
2092 AbbrevToUse = CString6Abbrev;
2093 else if (isCStr7)
2094 AbbrevToUse = CString7Abbrev;
2095 } else if (const ConstantDataSequential *CDS =
2098 Type *EltTy = CDS->getElementType();
2099 if (isa<IntegerType>(EltTy) || isa<ByteType>(EltTy)) {
2100 for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i)
2101 Record.push_back(CDS->getElementAsInteger(i));
2102 } else if (EltTy->isFloatTy()) {
2103 for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) {
2104 union {
2105 float F;
2106 uint32_t I;
2107 };
2108 F = CDS->getElementAsFloat(i);
2109 Record.push_back(I);
2110 }
2111 } else {
2112 assert(EltTy->isDoubleTy() && "Unknown ConstantData element type");
2113 for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) {
2114 union {
2115 double F;
2116 uint64_t I;
2117 };
2118 F = CDS->getElementAsDouble(i);
2119 Record.push_back(I);
2120 }
2121 }
2122 } else if (isa<ConstantArray>(C) || isa<ConstantStruct>(C) ||
2125 for (const Value *Op : C->operands())
2126 Record.push_back(VE.getValueID(Op));
2127 AbbrevToUse = AggregateAbbrev;
2128 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
2129 switch (CE->getOpcode()) {
2130 default:
2131 if (Instruction::isCast(CE->getOpcode())) {
2133 Record.push_back(getEncodedCastOpcode(CE->getOpcode()));
2134 Record.push_back(
2135 getTypeID(C->getOperand(0)->getType(), C->getOperand(0)));
2136 Record.push_back(VE.getValueID(C->getOperand(0)));
2137 AbbrevToUse = CONSTANTS_CE_CAST_Abbrev;
2138 } else {
2139 assert(CE->getNumOperands() == 2 && "Unknown constant expr!");
2141 Record.push_back(getEncodedBinaryOpcode(CE->getOpcode()));
2142 Record.push_back(VE.getValueID(C->getOperand(0)));
2143 Record.push_back(VE.getValueID(C->getOperand(1)));
2145 if (Flags != 0)
2146 Record.push_back(Flags);
2147 }
2148 break;
2149 case Instruction::GetElementPtr: {
2151 const auto *GO = cast<GEPOperator>(C);
2152 if (GO->isInBounds())
2154 Record.push_back(getTypeID(GO->getSourceElementType()));
2155 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) {
2156 Record.push_back(
2157 getTypeID(C->getOperand(i)->getType(), C->getOperand(i)));
2158 Record.push_back(VE.getValueID(C->getOperand(i)));
2159 }
2160 break;
2161 }
2162 case Instruction::Select:
2164 Record.push_back(VE.getValueID(C->getOperand(0)));
2165 Record.push_back(VE.getValueID(C->getOperand(1)));
2166 Record.push_back(VE.getValueID(C->getOperand(2)));
2167 break;
2168 case Instruction::ExtractElement:
2170 Record.push_back(getTypeID(C->getOperand(0)->getType()));
2171 Record.push_back(VE.getValueID(C->getOperand(0)));
2172 Record.push_back(getTypeID(C->getOperand(1)->getType()));
2173 Record.push_back(VE.getValueID(C->getOperand(1)));
2174 break;
2175 case Instruction::InsertElement:
2177 Record.push_back(VE.getValueID(C->getOperand(0)));
2178 Record.push_back(VE.getValueID(C->getOperand(1)));
2179 Record.push_back(getTypeID(C->getOperand(2)->getType()));
2180 Record.push_back(VE.getValueID(C->getOperand(2)));
2181 break;
2182 case Instruction::ShuffleVector:
2183 // If the return type and argument types are the same, this is a
2184 // standard shufflevector instruction. If the types are different,
2185 // then the shuffle is widening or truncating the input vectors, and
2186 // the argument type must also be encoded.
2187 if (C->getType() == C->getOperand(0)->getType()) {
2189 } else {
2191 Record.push_back(getTypeID(C->getOperand(0)->getType()));
2192 }
2193 Record.push_back(VE.getValueID(C->getOperand(0)));
2194 Record.push_back(VE.getValueID(C->getOperand(1)));
2195 Record.push_back(VE.getValueID(C->getOperand(2)));
2196 break;
2197 }
2198 } else if (const BlockAddress *BA = dyn_cast<BlockAddress>(C)) {
2200 Record.push_back(getTypeID(BA->getFunction()->getType()));
2201 Record.push_back(VE.getValueID(BA->getFunction()));
2202 Record.push_back(VE.getGlobalBasicBlockID(BA->getBasicBlock()));
2203 } else {
2204#ifndef NDEBUG
2205 C->dump();
2206#endif
2207 llvm_unreachable("Unknown constant!");
2208 }
2209 Stream.EmitRecord(Code, Record, AbbrevToUse);
2210 Record.clear();
2211 }
2212
2213 Stream.ExitBlock();
2214}
2215
2216void DXILBitcodeWriter::writeModuleConstants() {
2217 const ValueEnumerator::ValueList &Vals = VE.getValues();
2218
2219 // Find the first constant to emit, which is the first non-globalvalue value.
2220 // We know globalvalues have been emitted by WriteModuleInfo.
2221 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
2222 if (!isa<GlobalValue>(Vals[i].first)) {
2223 writeConstants(i, Vals.size(), true);
2224 return;
2225 }
2226 }
2227}
2228
2229/// pushValueAndType - The file has to encode both the value and type id for
2230/// many values, because we need to know what type to create for forward
2231/// references. However, most operands are not forward references, so this type
2232/// field is not needed.
2233///
2234/// This function adds V's value ID to Vals. If the value ID is higher than the
2235/// instruction ID, then it is a forward reference, and it also includes the
2236/// type ID. The value ID that is written is encoded relative to the InstID.
2237bool DXILBitcodeWriter::pushValueAndType(const Value *V, unsigned InstID,
2238 SmallVectorImpl<unsigned> &Vals) {
2239 unsigned ValID = VE.getValueID(V);
2240 // Make encoding relative to the InstID.
2241 Vals.push_back(InstID - ValID);
2242 if (ValID >= InstID) {
2243 Vals.push_back(getTypeID(V->getType(), V));
2244 return true;
2245 }
2246 return false;
2247}
2248
2249/// pushValue - Like pushValueAndType, but where the type of the value is
2250/// omitted (perhaps it was already encoded in an earlier operand).
2251void DXILBitcodeWriter::pushValue(const Value *V, unsigned InstID,
2252 SmallVectorImpl<unsigned> &Vals) {
2253 unsigned ValID = VE.getValueID(V);
2254 Vals.push_back(InstID - ValID);
2255}
2256
2257void DXILBitcodeWriter::pushValueSigned(const Value *V, unsigned InstID,
2258 SmallVectorImpl<uint64_t> &Vals) {
2259 unsigned ValID = VE.getValueID(V);
2260 int64_t diff = ((int32_t)InstID - (int32_t)ValID);
2261 emitSignedInt64(Vals, diff);
2262}
2263
2264/// WriteInstruction - Emit an instruction
2265void DXILBitcodeWriter::writeInstruction(const Instruction &I, unsigned InstID,
2266 SmallVectorImpl<unsigned> &Vals) {
2267 unsigned Code = 0;
2268 unsigned AbbrevToUse = 0;
2269 VE.setInstructionID(&I);
2270 switch (I.getOpcode()) {
2271 default:
2272 if (Instruction::isCast(I.getOpcode())) {
2274 if (!pushValueAndType(I.getOperand(0), InstID, Vals))
2275 AbbrevToUse = (unsigned)FUNCTION_INST_CAST_ABBREV;
2276 Vals.push_back(getTypeID(I.getType(), &I));
2277 Vals.push_back(getEncodedCastOpcode(I.getOpcode()));
2278 } else {
2279 assert(isa<BinaryOperator>(I) && "Unknown instruction!");
2281 if (!pushValueAndType(I.getOperand(0), InstID, Vals))
2282 AbbrevToUse = (unsigned)FUNCTION_INST_BINOP_ABBREV;
2283 pushValue(I.getOperand(1), InstID, Vals);
2284 Vals.push_back(getEncodedBinaryOpcode(I.getOpcode()));
2286 if (Flags != 0) {
2287 if (AbbrevToUse == (unsigned)FUNCTION_INST_BINOP_ABBREV)
2288 AbbrevToUse = (unsigned)FUNCTION_INST_BINOP_FLAGS_ABBREV;
2289 Vals.push_back(Flags);
2290 }
2291 }
2292 break;
2293
2294 case Instruction::GetElementPtr: {
2296 AbbrevToUse = (unsigned)FUNCTION_INST_GEP_ABBREV;
2297 auto &GEPInst = cast<GetElementPtrInst>(I);
2298 Vals.push_back(GEPInst.isInBounds());
2299 Vals.push_back(getTypeID(GEPInst.getSourceElementType()));
2300 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
2301 pushValueAndType(I.getOperand(i), InstID, Vals);
2302 break;
2303 }
2304 case Instruction::ExtractValue: {
2306 pushValueAndType(I.getOperand(0), InstID, Vals);
2307 const ExtractValueInst *EVI = cast<ExtractValueInst>(&I);
2308 Vals.append(EVI->idx_begin(), EVI->idx_end());
2309 break;
2310 }
2311 case Instruction::InsertValue: {
2313 pushValueAndType(I.getOperand(0), InstID, Vals);
2314 pushValueAndType(I.getOperand(1), InstID, Vals);
2315 const InsertValueInst *IVI = cast<InsertValueInst>(&I);
2316 Vals.append(IVI->idx_begin(), IVI->idx_end());
2317 break;
2318 }
2319 case Instruction::Select:
2321 pushValueAndType(I.getOperand(1), InstID, Vals);
2322 pushValue(I.getOperand(2), InstID, Vals);
2323 pushValueAndType(I.getOperand(0), InstID, Vals);
2324 break;
2325 case Instruction::ExtractElement:
2327 pushValueAndType(I.getOperand(0), InstID, Vals);
2328 pushValueAndType(I.getOperand(1), InstID, Vals);
2329 break;
2330 case Instruction::InsertElement:
2332 pushValueAndType(I.getOperand(0), InstID, Vals);
2333 pushValue(I.getOperand(1), InstID, Vals);
2334 pushValueAndType(I.getOperand(2), InstID, Vals);
2335 break;
2336 case Instruction::ShuffleVector:
2338 pushValueAndType(I.getOperand(0), InstID, Vals);
2339 pushValue(I.getOperand(1), InstID, Vals);
2340 pushValue(cast<ShuffleVectorInst>(&I)->getShuffleMaskForBitcode(), InstID,
2341 Vals);
2342 break;
2343 case Instruction::ICmp:
2344 case Instruction::FCmp: {
2345 // compare returning Int1Ty or vector of Int1Ty
2347 pushValueAndType(I.getOperand(0), InstID, Vals);
2348 pushValue(I.getOperand(1), InstID, Vals);
2351 if (Flags != 0)
2352 Vals.push_back(Flags);
2353 break;
2354 }
2355
2356 case Instruction::Ret: {
2358 unsigned NumOperands = I.getNumOperands();
2359 if (NumOperands == 0)
2360 AbbrevToUse = (unsigned)FUNCTION_INST_RET_VOID_ABBREV;
2361 else if (NumOperands == 1) {
2362 if (!pushValueAndType(I.getOperand(0), InstID, Vals))
2363 AbbrevToUse = (unsigned)FUNCTION_INST_RET_VAL_ABBREV;
2364 } else {
2365 for (unsigned i = 0, e = NumOperands; i != e; ++i)
2366 pushValueAndType(I.getOperand(i), InstID, Vals);
2367 }
2368 } break;
2369 case Instruction::UncondBr:
2371 Vals.push_back(VE.getValueID(cast<UncondBrInst>(I).getSuccessor()));
2372 break;
2373 case Instruction::CondBr: {
2375 const CondBrInst &II = cast<CondBrInst>(I);
2376 Vals.push_back(VE.getValueID(II.getSuccessor(0)));
2377 Vals.push_back(VE.getValueID(II.getSuccessor(1)));
2378 pushValue(II.getCondition(), InstID, Vals);
2379 } break;
2380 case Instruction::Switch: {
2382 const SwitchInst &SI = cast<SwitchInst>(I);
2383 Vals.push_back(getTypeID(SI.getCondition()->getType()));
2384 pushValue(SI.getCondition(), InstID, Vals);
2385 Vals.push_back(VE.getValueID(SI.getDefaultDest()));
2386 for (auto Case : SI.cases()) {
2387 Vals.push_back(VE.getValueID(Case.getCaseValue()));
2388 Vals.push_back(VE.getValueID(Case.getCaseSuccessor()));
2389 }
2390 } break;
2391 case Instruction::IndirectBr:
2393 Vals.push_back(getTypeID(I.getOperand(0)->getType()));
2394 // Encode the address operand as relative, but not the basic blocks.
2395 pushValue(I.getOperand(0), InstID, Vals);
2396 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i)
2397 Vals.push_back(VE.getValueID(I.getOperand(i)));
2398 break;
2399
2400 case Instruction::Invoke: {
2401 const InvokeInst *II = cast<InvokeInst>(&I);
2402 const Value *Callee = II->getCalledOperand();
2403 FunctionType *FTy = II->getFunctionType();
2405
2406 Vals.push_back(VE.getAttributeListID(II->getAttributes()));
2407 Vals.push_back(II->getCallingConv() | 1 << 13);
2408 Vals.push_back(VE.getValueID(II->getNormalDest()));
2409 Vals.push_back(VE.getValueID(II->getUnwindDest()));
2410 Vals.push_back(getTypeID(FTy));
2411 pushValueAndType(Callee, InstID, Vals);
2412
2413 // Emit value #'s for the fixed parameters.
2414 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
2415 pushValue(I.getOperand(i), InstID, Vals); // fixed param.
2416
2417 // Emit type/value pairs for varargs params.
2418 if (FTy->isVarArg()) {
2419 for (unsigned i = FTy->getNumParams(), e = I.getNumOperands() - 3; i != e;
2420 ++i)
2421 pushValueAndType(I.getOperand(i), InstID, Vals); // vararg
2422 }
2423 break;
2424 }
2425 case Instruction::Resume:
2427 pushValueAndType(I.getOperand(0), InstID, Vals);
2428 break;
2429 case Instruction::Unreachable:
2431 AbbrevToUse = (unsigned)FUNCTION_INST_UNREACHABLE_ABBREV;
2432 break;
2433
2434 case Instruction::PHI: {
2435 const PHINode &PN = cast<PHINode>(I);
2437 // With the newer instruction encoding, forward references could give
2438 // negative valued IDs. This is most common for PHIs, so we use
2439 // signed VBRs.
2441 Vals64.push_back(getTypeID(PN.getType()));
2442 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
2443 pushValueSigned(PN.getIncomingValue(i), InstID, Vals64);
2444 Vals64.push_back(VE.getValueID(PN.getIncomingBlock(i)));
2445 }
2446 // Emit a Vals64 vector and exit.
2447 Stream.EmitRecord(Code, Vals64, AbbrevToUse);
2448 Vals64.clear();
2449 return;
2450 }
2451
2452 case Instruction::LandingPad: {
2453 const LandingPadInst &LP = cast<LandingPadInst>(I);
2455 Vals.push_back(getTypeID(LP.getType()));
2456 Vals.push_back(LP.isCleanup());
2457 Vals.push_back(LP.getNumClauses());
2458 for (unsigned I = 0, E = LP.getNumClauses(); I != E; ++I) {
2459 if (LP.isCatch(I))
2461 else
2463 pushValueAndType(LP.getClause(I), InstID, Vals);
2464 }
2465 break;
2466 }
2467
2468 case Instruction::Alloca: {
2470 const AllocaInst &AI = cast<AllocaInst>(I);
2471 Vals.push_back(getTypeID(AI.getAllocatedType()));
2472 Vals.push_back(getTypeID(I.getOperand(0)->getType()));
2473 Vals.push_back(VE.getValueID(I.getOperand(0))); // size.
2474 unsigned AlignRecord = Log2_32(AI.getAlign().value()) + 1;
2475 assert(AlignRecord < 1 << 5 && "alignment greater than 1 << 64");
2476 AlignRecord |= AI.isUsedWithInAlloca() << 5;
2477 AlignRecord |= 1 << 6;
2478 Vals.push_back(AlignRecord);
2479 break;
2480 }
2481
2482 case Instruction::Load:
2483 if (cast<LoadInst>(I).isAtomic()) {
2485 pushValueAndType(I.getOperand(0), InstID, Vals);
2486 } else {
2488 if (!pushValueAndType(I.getOperand(0), InstID, Vals)) // ptr
2489 AbbrevToUse = (unsigned)FUNCTION_INST_LOAD_ABBREV;
2490 }
2491 Vals.push_back(getTypeID(I.getType()));
2492 Vals.push_back(Log2(cast<LoadInst>(I).getAlign()) + 1);
2493 Vals.push_back(cast<LoadInst>(I).isVolatile());
2494 if (cast<LoadInst>(I).isAtomic()) {
2495 Vals.push_back(getEncodedOrdering(cast<LoadInst>(I).getOrdering()));
2496 Vals.push_back(getEncodedSyncScopeID(cast<LoadInst>(I).getSyncScopeID()));
2497 }
2498 break;
2499 case Instruction::Store:
2500 if (cast<StoreInst>(I).isAtomic())
2502 else
2504 pushValueAndType(I.getOperand(1), InstID, Vals); // ptrty + ptr
2505 pushValueAndType(I.getOperand(0), InstID, Vals); // valty + val
2506 Vals.push_back(Log2(cast<StoreInst>(I).getAlign()) + 1);
2507 Vals.push_back(cast<StoreInst>(I).isVolatile());
2508 if (cast<StoreInst>(I).isAtomic()) {
2509 Vals.push_back(getEncodedOrdering(cast<StoreInst>(I).getOrdering()));
2510 Vals.push_back(
2511 getEncodedSyncScopeID(cast<StoreInst>(I).getSyncScopeID()));
2512 }
2513 break;
2514 case Instruction::AtomicCmpXchg:
2516 pushValueAndType(I.getOperand(0), InstID, Vals); // ptrty + ptr
2517 pushValueAndType(I.getOperand(1), InstID, Vals); // cmp.
2518 pushValue(I.getOperand(2), InstID, Vals); // newval.
2519 Vals.push_back(cast<AtomicCmpXchgInst>(I).isVolatile());
2520 Vals.push_back(
2521 getEncodedOrdering(cast<AtomicCmpXchgInst>(I).getSuccessOrdering()));
2522 Vals.push_back(
2523 getEncodedSyncScopeID(cast<AtomicCmpXchgInst>(I).getSyncScopeID()));
2524 Vals.push_back(
2525 getEncodedOrdering(cast<AtomicCmpXchgInst>(I).getFailureOrdering()));
2526 Vals.push_back(cast<AtomicCmpXchgInst>(I).isWeak());
2527 break;
2528 case Instruction::AtomicRMW:
2530 pushValueAndType(I.getOperand(0), InstID, Vals); // ptrty + ptr
2531 pushValue(I.getOperand(1), InstID, Vals); // val.
2532 Vals.push_back(
2534 Vals.push_back(cast<AtomicRMWInst>(I).isVolatile());
2535 Vals.push_back(getEncodedOrdering(cast<AtomicRMWInst>(I).getOrdering()));
2536 Vals.push_back(
2537 getEncodedSyncScopeID(cast<AtomicRMWInst>(I).getSyncScopeID()));
2538 break;
2539 case Instruction::Fence:
2541 Vals.push_back(getEncodedOrdering(cast<FenceInst>(I).getOrdering()));
2542 Vals.push_back(getEncodedSyncScopeID(cast<FenceInst>(I).getSyncScopeID()));
2543 break;
2544 case Instruction::Call: {
2545 const CallInst &CI = cast<CallInst>(I);
2546 FunctionType *FTy = CI.getFunctionType();
2547
2549
2550 Vals.push_back(VE.getAttributeListID(CI.getAttributes()));
2551 Vals.push_back((CI.getCallingConv() << 1) | unsigned(CI.isTailCall()) |
2552 unsigned(CI.isMustTailCall()) << 14 | 1 << 15);
2553 Vals.push_back(getGlobalObjectValueTypeID(FTy, CI.getCalledFunction()));
2554 pushValueAndType(CI.getCalledOperand(), InstID, Vals); // Callee
2555
2556 // Emit value #'s for the fixed parameters.
2557 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) {
2558 // Check for labels (can happen with asm labels).
2559 if (FTy->getParamType(i)->isLabelTy())
2560 Vals.push_back(VE.getValueID(CI.getArgOperand(i)));
2561 else
2562 pushValue(CI.getArgOperand(i), InstID, Vals); // fixed param.
2563 }
2564
2565 // Emit type/value pairs for varargs params.
2566 if (FTy->isVarArg()) {
2567 for (unsigned i = FTy->getNumParams(), e = CI.arg_size(); i != e; ++i)
2568 pushValueAndType(CI.getArgOperand(i), InstID, Vals); // varargs
2569 }
2570 break;
2571 }
2572 case Instruction::VAArg:
2574 Vals.push_back(getTypeID(I.getOperand(0)->getType())); // valistty
2575 pushValue(I.getOperand(0), InstID, Vals); // valist.
2576 Vals.push_back(getTypeID(I.getType())); // restype.
2577 break;
2578 }
2579
2580 Stream.EmitRecord(Code, Vals, AbbrevToUse);
2581 Vals.clear();
2582}
2583
2584// Emit names for globals/functions etc.
2585void DXILBitcodeWriter::writeFunctionLevelValueSymbolTable(
2586 const ValueSymbolTable &VST) {
2587 if (VST.empty())
2588 return;
2589 Stream.EnterSubblock(bitc::VALUE_SYMTAB_BLOCK_ID, 4);
2590
2592
2593 // HLSL Change
2594 // Read the named values from a sorted list instead of the original list
2595 // to ensure the binary is the same no matter what values ever existed.
2597
2598 for (auto &VI : VST) {
2599 const Value &V = VE.getDXILValue(*VI.second);
2600 SortedTable.push_back(V.getValueName());
2601 }
2602 // The keys are unique, so there shouldn't be stability issues.
2603 llvm::sort(SortedTable, [](const ValueName *A, const ValueName *B) {
2604 return A->first() < B->first();
2605 });
2606
2607 for (const ValueName *SI : SortedTable) {
2608 auto &Name = *SI;
2609
2610 // Figure out the encoding to use for the name.
2611 bool is7Bit = true;
2612 bool isChar6 = true;
2613 for (const char *C = Name.getKeyData(), *E = C + Name.getKeyLength();
2614 C != E; ++C) {
2615 if (isChar6)
2616 isChar6 = BitCodeAbbrevOp::isChar6(*C);
2617 if ((unsigned char)*C & 128) {
2618 is7Bit = false;
2619 break; // don't bother scanning the rest.
2620 }
2621 }
2622
2623 unsigned AbbrevToUse = VST_ENTRY_8_ABBREV;
2624
2625 // VST_ENTRY: [valueid, namechar x N]
2626 // VST_BBENTRY: [bbid, namechar x N]
2627 unsigned Code;
2628 if (isa<BasicBlock>(SI->getValue())) {
2630 if (isChar6)
2631 AbbrevToUse = VST_BBENTRY_6_ABBREV;
2632 } else {
2634 if (isChar6)
2635 AbbrevToUse = VST_ENTRY_6_ABBREV;
2636 else if (is7Bit)
2637 AbbrevToUse = VST_ENTRY_7_ABBREV;
2638 }
2639
2640 NameVals.push_back(VE.getValueID(SI->getValue()));
2641 for (const char *P = Name.getKeyData(),
2642 *E = Name.getKeyData() + Name.getKeyLength();
2643 P != E; ++P)
2644 NameVals.push_back((unsigned char)*P);
2645
2646 // Emit the finished record.
2647 Stream.EmitRecord(Code, NameVals, AbbrevToUse);
2648 NameVals.clear();
2649 }
2650 Stream.ExitBlock();
2651}
2652
2653/// Emit a function body to the module stream.
2654void DXILBitcodeWriter::writeFunction(const Function &F) {
2655 Stream.EnterSubblock(bitc::FUNCTION_BLOCK_ID, 4);
2656 VE.incorporateFunction(F);
2657
2659
2660 // Emit the number of basic blocks, so the reader can create them ahead of
2661 // time.
2662 Vals.push_back(VE.getBasicBlocks().size());
2663 Stream.EmitRecord(bitc::FUNC_CODE_DECLAREBLOCKS, Vals);
2664 Vals.clear();
2665
2666 // If there are function-local constants, emit them now.
2667 unsigned CstStart, CstEnd;
2668 VE.getFunctionConstantRange(CstStart, CstEnd);
2669 writeConstants(CstStart, CstEnd, false);
2670
2671 // If there is function-local metadata, emit it now.
2672 writeFunctionMetadata(F);
2673
2674 // Keep a running idea of what the instruction ID is.
2675 unsigned InstID = CstEnd;
2676
2677 bool NeedsMetadataAttachment = F.hasMetadata();
2678
2679 DILocation *LastDL = nullptr;
2680
2681 // Finally, emit all the instructions, in order.
2682 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
2683 for (BasicBlock::const_iterator It = BB->begin(), E = BB->end(); It != E;
2684 ++It) {
2685 const Instruction &I = VE.getDXILInstruction(*It);
2686
2687 writeInstruction(I, InstID, Vals);
2688
2689 if (!I.getType()->isVoidTy())
2690 ++InstID;
2691
2692 // If the instruction has metadata, write a metadata attachment later.
2693 NeedsMetadataAttachment |= I.hasMetadataOtherThanDebugLoc();
2694
2695 // If the instruction has a debug location, emit it.
2696 DILocation *DL = I.getDebugLoc();
2697 if (!DL)
2698 continue;
2699
2700 if (DL == LastDL) {
2701 // Just repeat the same debug loc as last time.
2702 Stream.EmitRecord(bitc::FUNC_CODE_DEBUG_LOC_AGAIN, Vals);
2703 continue;
2704 }
2705
2706 Vals.push_back(DL->getLine());
2707 Vals.push_back(DL->getColumn());
2708 Vals.push_back(VE.getMetadataOrNullID(DL->getScope()));
2709 Vals.push_back(VE.getMetadataOrNullID(DL->getInlinedAt()));
2710 Stream.EmitRecord(bitc::FUNC_CODE_DEBUG_LOC, Vals);
2711 Vals.clear();
2712
2713 LastDL = DL;
2714 }
2715
2716 // Emit names for all the instructions etc.
2717 if (auto *Symtab = F.getValueSymbolTable())
2718 writeFunctionLevelValueSymbolTable(*Symtab);
2719
2720 if (NeedsMetadataAttachment)
2721 writeFunctionMetadataAttachment(F);
2722
2723 VE.purgeFunction();
2724 Stream.ExitBlock();
2725}
2726
2727// Emit blockinfo, which defines the standard abbreviations etc.
2728void DXILBitcodeWriter::writeBlockInfo() {
2729 // We only want to emit block info records for blocks that have multiple
2730 // instances: CONSTANTS_BLOCK, FUNCTION_BLOCK and VALUE_SYMTAB_BLOCK.
2731 // Other blocks can define their abbrevs inline.
2732 Stream.EnterBlockInfoBlock();
2733
2734 { // 8-bit fixed-width VST_ENTRY/VST_BBENTRY strings.
2735 auto Abbv = std::make_shared<BitCodeAbbrev>();
2736 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3));
2737 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
2738 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2739 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
2740 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
2741 std::move(Abbv)) != VST_ENTRY_8_ABBREV)
2742 assert(false && "Unexpected abbrev ordering!");
2743 }
2744
2745 { // 7-bit fixed width VST_ENTRY strings.
2746 auto Abbv = std::make_shared<BitCodeAbbrev>();
2747 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
2748 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
2749 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2750 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
2751 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
2752 std::move(Abbv)) != VST_ENTRY_7_ABBREV)
2753 assert(false && "Unexpected abbrev ordering!");
2754 }
2755 { // 6-bit char6 VST_ENTRY strings.
2756 auto Abbv = std::make_shared<BitCodeAbbrev>();
2757 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
2758 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
2759 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2760 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
2761 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
2762 std::move(Abbv)) != VST_ENTRY_6_ABBREV)
2763 assert(false && "Unexpected abbrev ordering!");
2764 }
2765 { // 6-bit char6 VST_BBENTRY strings.
2766 auto Abbv = std::make_shared<BitCodeAbbrev>();
2767 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_BBENTRY));
2768 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
2769 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2770 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
2771 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
2772 std::move(Abbv)) != VST_BBENTRY_6_ABBREV)
2773 assert(false && "Unexpected abbrev ordering!");
2774 }
2775
2776 { // SETTYPE abbrev for CONSTANTS_BLOCK.
2777 auto Abbv = std::make_shared<BitCodeAbbrev>();
2778 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_SETTYPE));
2779 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
2780 VE.computeBitsRequiredForTypeIndices()));
2781 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, std::move(Abbv)) !=
2782 CONSTANTS_SETTYPE_ABBREV)
2783 assert(false && "Unexpected abbrev ordering!");
2784 }
2785
2786 { // INTEGER abbrev for CONSTANTS_BLOCK.
2787 auto Abbv = std::make_shared<BitCodeAbbrev>();
2788 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_INTEGER));
2789 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
2790 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, std::move(Abbv)) !=
2791 CONSTANTS_INTEGER_ABBREV)
2792 assert(false && "Unexpected abbrev ordering!");
2793 }
2794
2795 { // CE_CAST abbrev for CONSTANTS_BLOCK.
2796 auto Abbv = std::make_shared<BitCodeAbbrev>();
2797 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CE_CAST));
2798 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // cast opc
2799 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // typeid
2800 VE.computeBitsRequiredForTypeIndices()));
2801 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id
2802
2803 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, std::move(Abbv)) !=
2804 CONSTANTS_CE_CAST_Abbrev)
2805 assert(false && "Unexpected abbrev ordering!");
2806 }
2807 { // NULL abbrev for CONSTANTS_BLOCK.
2808 auto Abbv = std::make_shared<BitCodeAbbrev>();
2809 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_NULL));
2810 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, std::move(Abbv)) !=
2811 CONSTANTS_NULL_Abbrev)
2812 assert(false && "Unexpected abbrev ordering!");
2813 }
2814
2815 // FIXME: This should only use space for first class types!
2816
2817 { // INST_LOAD abbrev for FUNCTION_BLOCK.
2818 auto Abbv = std::make_shared<BitCodeAbbrev>();
2819 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_LOAD));
2820 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Ptr
2821 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // dest ty
2822 VE.computeBitsRequiredForTypeIndices()));
2823 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // Align
2824 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // volatile
2825 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, std::move(Abbv)) !=
2826 (unsigned)FUNCTION_INST_LOAD_ABBREV)
2827 assert(false && "Unexpected abbrev ordering!");
2828 }
2829 { // INST_BINOP abbrev for FUNCTION_BLOCK.
2830 auto Abbv = std::make_shared<BitCodeAbbrev>();
2831 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_BINOP));
2832 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS
2833 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // RHS
2834 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
2835 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, std::move(Abbv)) !=
2836 (unsigned)FUNCTION_INST_BINOP_ABBREV)
2837 assert(false && "Unexpected abbrev ordering!");
2838 }
2839 { // INST_BINOP_FLAGS abbrev for FUNCTION_BLOCK.
2840 auto Abbv = std::make_shared<BitCodeAbbrev>();
2841 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_BINOP));
2842 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS
2843 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // RHS
2844 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
2845 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7)); // flags
2846 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, std::move(Abbv)) !=
2847 (unsigned)FUNCTION_INST_BINOP_FLAGS_ABBREV)
2848 assert(false && "Unexpected abbrev ordering!");
2849 }
2850 { // INST_CAST abbrev for FUNCTION_BLOCK.
2851 auto Abbv = std::make_shared<BitCodeAbbrev>();
2852 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_CAST));
2853 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // OpVal
2854 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // dest ty
2855 VE.computeBitsRequiredForTypeIndices()));
2856 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
2857 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, std::move(Abbv)) !=
2858 (unsigned)FUNCTION_INST_CAST_ABBREV)
2859 assert(false && "Unexpected abbrev ordering!");
2860 }
2861
2862 { // INST_RET abbrev for FUNCTION_BLOCK.
2863 auto Abbv = std::make_shared<BitCodeAbbrev>();
2864 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET));
2865 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, std::move(Abbv)) !=
2866 (unsigned)FUNCTION_INST_RET_VOID_ABBREV)
2867 assert(false && "Unexpected abbrev ordering!");
2868 }
2869 { // INST_RET abbrev for FUNCTION_BLOCK.
2870 auto Abbv = std::make_shared<BitCodeAbbrev>();
2871 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET));
2872 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ValID
2873 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, std::move(Abbv)) !=
2874 (unsigned)FUNCTION_INST_RET_VAL_ABBREV)
2875 assert(false && "Unexpected abbrev ordering!");
2876 }
2877 { // INST_UNREACHABLE abbrev for FUNCTION_BLOCK.
2878 auto Abbv = std::make_shared<BitCodeAbbrev>();
2879 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_UNREACHABLE));
2880 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, std::move(Abbv)) !=
2881 (unsigned)FUNCTION_INST_UNREACHABLE_ABBREV)
2882 assert(false && "Unexpected abbrev ordering!");
2883 }
2884 {
2885 auto Abbv = std::make_shared<BitCodeAbbrev>();
2886 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_GEP));
2887 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
2888 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // dest ty
2889 Log2_32_Ceil(VE.getTypes().size() + 1)));
2890 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2891 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
2892 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, std::move(Abbv)) !=
2893 (unsigned)FUNCTION_INST_GEP_ABBREV)
2894 assert(false && "Unexpected abbrev ordering!");
2895 }
2896
2897 Stream.ExitBlock();
2898}
2899
2900void DXILBitcodeWriter::writeModuleVersion() {
2901 // VERSION: [version#]
2902 Stream.EmitRecord(bitc::MODULE_CODE_VERSION, ArrayRef<unsigned>{1});
2903}
2904
2905/// WriteModule - Emit the specified module to the bitstream.
2907 // The identification block is new since llvm-3.7, but the old bitcode reader
2908 // will skip it.
2909 // writeIdentificationBlock(Stream);
2910
2911 Stream.EnterSubblock(bitc::MODULE_BLOCK_ID, 3);
2912
2913 // It is redundant to fully-specify this here, but nice to make it explicit
2914 // so that it is clear the DXIL module version is different.
2915 DXILBitcodeWriter::writeModuleVersion();
2916
2917 // Emit blockinfo, which defines the standard abbreviations etc.
2918 writeBlockInfo();
2919
2920 // Emit information about attribute groups.
2921 writeAttributeGroupTable();
2922
2923 // Emit information about parameter attributes.
2924 writeAttributeTable();
2925
2926 // Emit information describing all of the types in the module.
2927 writeTypeTable();
2928
2929 writeComdats();
2930
2931 // Emit top-level description of module, including target triple, inline asm,
2932 // descriptors for global variables, and function prototype info.
2933 writeModuleInfo();
2934
2935 // Emit constants.
2936 writeModuleConstants();
2937
2938 // Emit metadata.
2939 writeModuleMetadataKinds();
2940
2941 // Emit metadata.
2942 writeModuleMetadata();
2943
2944 // Emit names for globals/functions etc.
2945 // DXIL uses the same format for module-level value symbol table as for the
2946 // function level table.
2947 writeFunctionLevelValueSymbolTable(M.getValueSymbolTable());
2948
2949 // Emit function bodies.
2950 for (const Function &F : M)
2951 if (!F.isDeclaration())
2952 writeFunction(F);
2953
2954 Stream.ExitBlock();
2955}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
uint64_t uint64_t
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
This file contains the simple types necessary to represent the attributes associated with functions a...
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
static uint64_t rotateSign(APInt Val)
dxil translate DXIL Translate Metadata
This file contains constants used for implementing Dwarf debug support.
This file contains the declaration of the GlobalIFunc class, which represents a single indirect funct...
static MaybeAlign getAlign(Value *Ptr)
Module.h This file contains the declarations for the Module class.
static constexpr Value * getValue(Ty &ValueOrUse)
static bool isAtomic(MachineBasicBlock::iterator I)
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
#define G(x, y, z)
Definition MD5.cpp:55
This file contains the declarations for metadata subclasses.
#define T
ModuleSummaryIndex.h This file contains the declarations the classes that hold the module index and s...
uint64_t IntrinsicInst * II
#define P(N)
Func getContext().diagnose(DiagnosticInfoUnsupported(Func
This file contains some templates that are useful if you are working with the STL at all.
static unsigned getBitWidth(Type *Ty, const DataLayout &DL)
Returns the bitwidth of the given scalar or pointer type.
static const uint32_t IV[8]
Definition blake3_impl.h:83
Class for arbitrary precision integers.
Definition APInt.h:78
const uint64_t * getRawData() const
This function returns a pointer to the internal storage of the APInt.
Definition APInt.h:571
int64_t getSExtValue() const
Get sign extended value.
Definition APInt.h:1582
Align getAlign() const
Return the alignment of the memory that is being allocated by the instruction.
Type * getAllocatedType() const
Return the type that is being allocated by the instruction.
bool isUsedWithInAlloca() const
Return true if this alloca is used as an inalloca argument to a call.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
bool empty() const
Check if the array is empty.
Definition ArrayRef.h:136
BinOp
This enumeration lists the possible modifications atomicrmw can make.
@ Add
*p = old + v
@ FAdd
*p = old + v
@ Min
*p = old <signed v ? old : v
@ Sub
*p = old - v
@ And
*p = old & v
@ Xor
*p = old ^ v
@ FSub
*p = old - v
@ Max
*p = old >signed v ? old : v
@ UMin
*p = old <unsigned v ? old : v
@ FMin
*p = minnum(old, v) minnum matches the behavior of llvm.minnum.
@ UMax
*p = old >unsigned v ? old : v
@ FMax
*p = maxnum(old, v) maxnum matches the behavior of llvm.maxnum.
@ Nand
*p = ~(old & v)
This class holds the attributes for a particular argument, parameter, function, or return value.
Definition Attributes.h:448
bool hasAttributes() const
Return true if attributes exists in this set.
Definition Attributes.h:529
Functions, function parameters, and return types can have attributes to indicate how they should be t...
Definition Attributes.h:107
AttrKind
This enumeration lists the attributes that can be associated with parameters, function results,...
Definition Attributes.h:126
@ TombstoneKey
Use as Tombstone key for DenseMap of AttrKind.
Definition Attributes.h:133
@ None
No attributes have been set.
Definition Attributes.h:128
@ EmptyKey
Use as Empty key for DenseMap of AttrKind.
Definition Attributes.h:132
@ EndAttrKinds
Sentinel value useful for loops.
Definition Attributes.h:131
InstListType::const_iterator const_iterator
Definition BasicBlock.h:171
BitCodeAbbrevOp - This describes one or more operands in an abbreviation.
Definition BitCodes.h:34
static bool isChar6(char C)
isChar6 - Return true if this character is legal in the Char6 encoding.
Definition BitCodes.h:88
unsigned EmitAbbrev(std::shared_ptr< BitCodeAbbrev > Abbv)
Emits the abbreviation Abbv to the stream.
void EmitRecordWithBlob(unsigned Abbrev, const Container &Vals, StringRef Blob)
EmitRecordWithBlob - Emit the specified record to the stream, using an abbrev that includes a blob at...
void EnterSubblock(unsigned BlockID, unsigned CodeLen)
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation or the function signa...
CallingConv::ID getCallingConv() const
Value * getCalledOperand() const
Value * getArgOperand(unsigned i) const
FunctionType * getFunctionType() const
unsigned arg_size() const
AttributeList getAttributes() const
Return the attributes for this call.
bool isTailCall() const
bool isMustTailCall() const
@ Largest
The linker will choose the largest COMDAT.
Definition Comdat.h:39
@ SameSize
The data referenced by the COMDAT must be the same size.
Definition Comdat.h:41
@ Any
The linker may choose any COMDAT.
Definition Comdat.h:37
@ NoDeduplicate
No deduplication is performed.
Definition Comdat.h:40
@ ExactMatch
The data referenced by the COMDAT must be the same.
Definition Comdat.h:38
iterator find(const_arg_type_t< KeyT > Val)
Definition DenseMap.h:766
iterator end()
Definition DenseMap.h:686
idx_iterator idx_end() const
idx_iterator idx_begin() const
BasicBlockListType::const_iterator const_iterator
Definition Function.h:71
VisibilityTypes getVisibility() const
LinkageTypes getLinkage() const
uint64_t GUID
Declare a type to represent a global unique identifier for a global value.
ThreadLocalMode getThreadLocalMode() const
@ DLLExportStorageClass
Function to be accessible from DLL.
Definition GlobalValue.h:77
@ DLLImportStorageClass
Function to be imported from DLL.
Definition GlobalValue.h:76
@ DefaultVisibility
The GV is visible.
Definition GlobalValue.h:68
@ HiddenVisibility
The GV is hidden.
Definition GlobalValue.h:69
@ ProtectedVisibility
The GV is protected.
Definition GlobalValue.h:70
LinkageTypes
An enumeration for the kinds of linkage for global values.
Definition GlobalValue.h:52
@ PrivateLinkage
Like Internal, but omit from symbol table.
Definition GlobalValue.h:61
@ CommonLinkage
Tentative definitions.
Definition GlobalValue.h:63
@ InternalLinkage
Rename collisions when linking (static functions).
Definition GlobalValue.h:60
@ LinkOnceAnyLinkage
Keep one copy of function when linking (inline)
Definition GlobalValue.h:55
@ WeakODRLinkage
Same, but only replaced by something equivalent.
Definition GlobalValue.h:58
@ ExternalLinkage
Externally visible function.
Definition GlobalValue.h:53
@ WeakAnyLinkage
Keep one copy of named function when linking (weak)
Definition GlobalValue.h:57
@ AppendingLinkage
Special purpose, only applies to global arrays.
Definition GlobalValue.h:59
@ AvailableExternallyLinkage
Available for inspection, not emission.
Definition GlobalValue.h:54
@ ExternalWeakLinkage
ExternalWeak linkage description.
Definition GlobalValue.h:62
@ LinkOnceODRLinkage
Same, but only replaced by something equivalent.
Definition GlobalValue.h:56
DLLStorageClassTypes getDLLStorageClass() const
idx_iterator idx_end() const
idx_iterator idx_begin() const
bool isCast() const
bool isCleanup() const
Return 'true' if this landingpad instruction is a cleanup.
unsigned getNumClauses() const
Get the number of clauses for this landing pad.
bool isCatch(unsigned Idx) const
Return 'true' if the clause and index Idx is a catch clause.
Constant * getClause(unsigned Idx) const
Get the value of the clause at index Idx.
const unsigned char * bytes_begin() const
Definition Metadata.h:764
const unsigned char * bytes_end() const
Definition Metadata.h:765
bool doesNotAccessMemory() const
Whether this function accesses no memory.
Definition ModRef.h:246
bool onlyAccessesArgPointees() const
Whether this function only (at most) accesses argument memory.
Definition ModRef.h:255
bool onlyReadsMemory() const
Whether this function only (at most) reads memory.
Definition ModRef.h:249
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:68
BasicBlock * getIncomingBlock(unsigned i) const
Return incoming basic block number i.
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
unsigned getNumIncomingValues() const
Return the number of incoming edges.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void reserve(size_type N)
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
iterator insert(iterator I, T &&Elt)
void push_back(const T &Elt)
LLVM_NO_SANITIZE_CFI_UNRELATED_CAST iterator begin()
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
constexpr bool empty() const
Check if the string is empty.
Definition StringRef.h:141
iterator begin() const
Definition StringRef.h:114
constexpr size_t size() const
Get the string size.
Definition StringRef.h:144
iterator end() const
Definition StringRef.h:116
Utility for building string tables with deduplicated suffixes.
Triple - Helper class for working with autoconf configuration names.
Definition Triple.h:48
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
bool isX86_FP80Ty() const
Return true if this is x86 long double.
Definition Type.h:161
bool isFloatTy() const
Return true if this is 'float', a 32-bit IEEE fp type.
Definition Type.h:155
@ X86_AMXTyID
AMX vectors (8192 bits, X86 specific)
Definition Type.h:67
@ FunctionTyID
Functions.
Definition Type.h:73
@ ArrayTyID
Arrays.
Definition Type.h:76
@ TypedPointerTyID
Typed pointer used by some GPU targets.
Definition Type.h:79
@ HalfTyID
16-bit floating point type
Definition Type.h:57
@ TargetExtTyID
Target extension type.
Definition Type.h:80
@ VoidTyID
type with no size
Definition Type.h:64
@ ScalableVectorTyID
Scalable SIMD vector type.
Definition Type.h:78
@ LabelTyID
Labels.
Definition Type.h:65
@ FloatTyID
32-bit floating point type
Definition Type.h:59
@ StructTyID
Structures.
Definition Type.h:75
@ IntegerTyID
Arbitrary bit width integers.
Definition Type.h:71
@ FixedVectorTyID
Fixed width SIMD vector type.
Definition Type.h:77
@ BFloatTyID
16-bit floating point type (7-bit significand)
Definition Type.h:58
@ DoubleTyID
64-bit floating point type
Definition Type.h:60
@ X86_FP80TyID
80-bit floating point type (X87)
Definition Type.h:61
@ PPC_FP128TyID
128-bit floating point type (two 64-bits, PowerPC)
Definition Type.h:63
@ MetadataTyID
Metadata.
Definition Type.h:66
@ TokenTyID
Tokens.
Definition Type.h:68
@ ByteTyID
Arbitrary bit width bytes.
Definition Type.h:72
@ PointerTyID
Pointers.
Definition Type.h:74
@ FP128TyID
128-bit floating point type (112-bit significand)
Definition Type.h:62
bool isPPC_FP128Ty() const
Return true if this is powerpc long double.
Definition Type.h:167
bool isFP128Ty() const
Return true if this is 'fp128'.
Definition Type.h:164
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition Type.h:370
bool isHalfTy() const
Return true if this is 'half', a 16-bit IEEE fp type.
Definition Type.h:144
bool isDoubleTy() const
Return true if this is 'double', a 64-bit IEEE fp type.
Definition Type.h:158
A few GPU targets, such as DXIL and SPIR-V, have typed pointers.
Type * getElementType() const
static LLVM_ABI TypedPointerType * get(Type *ElementType, unsigned AddressSpace)
This constructs a pointer to an object of the specified type in a numbered address space.
unsigned getAddressSpace() const
Return the address space of the Pointer type.
Value * getValue() const
Definition Metadata.h:511
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:257
BitcodeWriter(SmallVectorImpl< char > &Buffer)
Create a BitcodeWriter that writes to Buffer.
void writeModule(const Module &M, const DXILDebugInfoMap &DebugInfo)
Write the specified module to the buffer specified at construction time.
static void emitWideAPInt(SmallVectorImpl< uint64_t > &Vals, const APInt &A)
static unsigned getEncodedThreadLocalMode(const GlobalValue &GV)
static unsigned getEncodedCastOpcode(unsigned Opcode)
Begin dxil::BitcodeWriterBase Implementation.
static void writeStringRecord(BitstreamWriter &Stream, unsigned Code, StringRef Str, unsigned AbbrevToUse)
static uint64_t getAttrKindEncoding(Attribute::AttrKind Kind)
static unsigned getEncodedDLLStorageClass(const GlobalValue &GV)
static unsigned getEncodedOrdering(AtomicOrdering Ordering)
static unsigned getEncodedLinkage(const GlobalValue::LinkageTypes Linkage)
static unsigned getEncodedVisibility(const GlobalValue &GV)
void write()
Emit the current module to the bitstream.
static unsigned getEncodedBinaryOpcode(unsigned Opcode)
static void emitSignedInt64(SmallVectorImpl< uint64_t > &Vals, uint64_t V)
static unsigned getEncodedUnaryOpcode(unsigned Opcode)
DXILBitcodeWriter(const Module &M, SmallVectorImpl< char > &Buffer, StringTableBuilder &StrtabBuilder, BitstreamWriter &Stream, const DXILDebugInfoMap &DebugInfo)
Constructs a ModuleBitcodeWriter object for the given Module, writing to the provided Buffer.
static unsigned getEncodedRMWOperation(AtomicRMWInst::BinOp Op)
static unsigned getEncodedComdatSelectionKind(const Comdat &C)
static uint64_t getOptimizationFlags(const Value *V)
std::pair< unsigned, AttributeSet > IndexAndAttrSet
Attribute groups as encoded in bitcode are almost AttributeSets, but they include the AttributeList i...
std::vector< std::pair< const Value *, unsigned > > ValueList
std::vector< Type * > TypeList
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
This file contains the declaration of the Comdat class, which represents a single COMDAT in LLVM.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char Attrs[]
Key for Kernel::Metadata::mAttrs.
@ Entry
Definition COFF.h:862
Predicate getPredicate(unsigned Condition, unsigned Hint)
Return predicate consisting of specified condition and hint bits.
@ CE
Windows NT (Windows on ARM)
Definition MCAsmInfo.h:51
@ TYPE_CODE_STRUCT_ANON
@ TYPE_CODE_STRUCT_NAME
@ TYPE_CODE_STRUCT_NAMED
@ METADATA_TEMPLATE_VALUE
@ METADATA_LEXICAL_BLOCK_FILE
@ METADATA_LEXICAL_BLOCK
@ METADATA_SUBROUTINE_TYPE
@ METADATA_IMPORTED_ENTITY
@ METADATA_COMPILE_UNIT
@ METADATA_COMPOSITE_TYPE
@ METADATA_DERIVED_TYPE
@ METADATA_TEMPLATE_TYPE
@ METADATA_DISTINCT_NODE
@ METADATA_GENERIC_DEBUG
@ CST_CODE_CE_INBOUNDS_GEP
@ CST_CODE_BLOCKADDRESS
@ CST_CODE_CE_SHUFVEC_EX
@ CST_CODE_CE_EXTRACTELT
@ CST_CODE_CE_SHUFFLEVEC
@ CST_CODE_WIDE_INTEGER
@ CST_CODE_CE_INSERTELT
@ COMDAT_SELECTION_KIND_LARGEST
@ COMDAT_SELECTION_KIND_ANY
@ COMDAT_SELECTION_KIND_SAME_SIZE
@ COMDAT_SELECTION_KIND_EXACT_MATCH
@ COMDAT_SELECTION_KIND_NO_DUPLICATES
@ ATTR_KIND_STACK_PROTECT
@ ATTR_KIND_STACK_PROTECT_STRONG
@ ATTR_KIND_SANITIZE_MEMORY
@ ATTR_KIND_OPTIMIZE_FOR_SIZE
@ ATTR_KIND_SANITIZE_ADDRESS
@ ATTR_KIND_NO_IMPLICIT_FLOAT
@ ATTR_KIND_STACK_ALIGNMENT
@ ATTR_KIND_STACK_PROTECT_REQ
@ ATTR_KIND_INLINE_HINT
@ ATTR_KIND_RETURNS_TWICE
@ ATTR_KIND_NO_DUPLICATE
@ ATTR_KIND_NON_LAZY_BIND
@ ATTR_KIND_DEREFERENCEABLE
@ ATTR_KIND_OPTIMIZE_NONE
@ ATTR_KIND_NO_RED_ZONE
@ ATTR_KIND_DEREFERENCEABLE_OR_NULL
@ ATTR_KIND_ALWAYS_INLINE
@ ATTR_KIND_SANITIZE_THREAD
@ PARAMATTR_GROUP_BLOCK_ID
@ METADATA_ATTACHMENT_ID
@ VALUE_SYMTAB_BLOCK_ID
@ MODULE_CODE_VERSION
@ MODULE_CODE_SECTIONNAME
@ MODULE_CODE_DATALAYOUT
@ MODULE_CODE_GLOBALVAR
@ MODULE_CODE_ALIAS_OLD
@ FUNC_CODE_INST_ATOMICRMW_OLD
@ FUNC_CODE_INST_LANDINGPAD
@ FUNC_CODE_INST_EXTRACTVAL
@ FUNC_CODE_INST_RESUME
@ FUNC_CODE_INST_VSELECT
@ FUNC_CODE_INST_LOADATOMIC
@ FUNC_CODE_INST_STOREATOMIC
@ FUNC_CODE_DEBUG_LOC_AGAIN
@ FUNC_CODE_INST_EXTRACTELT
@ FUNC_CODE_INST_INDIRECTBR
@ FUNC_CODE_INST_INVOKE
@ FUNC_CODE_INST_INSERTVAL
@ FUNC_CODE_DECLAREBLOCKS
@ FUNC_CODE_INST_SWITCH
@ FUNC_CODE_INST_ALLOCA
@ FUNC_CODE_INST_INSERTELT
@ FUNC_CODE_INST_SHUFFLEVEC
@ FUNC_CODE_INST_CMPXCHG
@ FUNC_CODE_INST_UNREACHABLE
@ FIRST_APPLICATION_ABBREV
@ PARAMATTR_GRP_CODE_ENTRY
An analysis to compute the PointerTypes for pointers in a Module.
DenseMap< const Value *, Type * > PointerTypeMap
void WriteDXILToFile(Module &M, raw_ostream &Out)
Write the specified module to the specified raw output stream.
DXILDebugInfoMap collectDXILDebugInfo(Module &M)
constexpr double e
NodeAddr< CodeNode * > Code
Definition RDFGraph.h:388
friend class Instruction
Iterator for Instructions in a `BasicBlock.
Definition BasicBlock.h:73
This is an optimization pass for GlobalISel generic memory operations.
unsigned Log2_32_Ceil(uint32_t Value)
Return the ceil log base 2 of the specified value, 32 if the value is zero.
Definition MathExtras.h:339
StringMapEntry< Value * > ValueName
Definition Value.h:56
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition STLExtras.h:1685
unsigned encode(MaybeAlign A)
Returns a representation of the alignment that encodes undefined as 0.
Definition Alignment.h:211
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
@ BWH_HeaderSize
MemoryEffectsBase< IRMemLocation > MemoryEffects
Summary of how a function affects memory in the program.
Definition ModRef.h:356
bool isa_and_nonnull(const Y &Val)
Definition Casting.h:676
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
Definition InstrProf.h:143
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition MathExtras.h:326
decltype(auto) get(const PointerIntPair< PointerTy, IntBits, IntType, PtrTraits, Info > &Pair)
void sort(IteratorTy Start, IteratorTy End)
Definition STLExtras.h:1652
constexpr bool isUInt(uint64_t x)
Checks if an unsigned integer fits into the given bit width.
Definition MathExtras.h:190
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
AtomicOrdering
Atomic ordering for LLVM's memory model.
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Count
Definition InstrProf.h:145
DWARFExpression::Operation Op
ArrayRef(const T &OneElt) -> ArrayRef< T >
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
unsigned Log2(Align A)
Returns the log2 of the alignment.
Definition Alignment.h:202
#define N
constexpr uint64_t value() const
This is a hole in the type system and should not be abused.
Definition Alignment.h:82
Struct that holds a reference to a particular GUID in a global value summary.