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