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 (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
941     AttributeList AL = Attrs[i];
942     for (unsigned i : AL.indexes()) {
943       AttributeSet AS = AL.getAttributes(i);
944       if (AS.hasAttributes())
945         Record.push_back(VE.getAttributeGroupID({i, AS}));
946     }
947 
948     Stream.EmitRecord(bitc::PARAMATTR_CODE_ENTRY, Record);
949     Record.clear();
950   }
951 
952   Stream.ExitBlock();
953 }
954 
955 /// WriteTypeTable - Write out the type table for a module.
956 void DXILBitcodeWriter::writeTypeTable() {
957   const ValueEnumerator::TypeList &TypeList = VE.getTypes();
958 
959   Stream.EnterSubblock(bitc::TYPE_BLOCK_ID_NEW, 4 /*count from # abbrevs */);
960   SmallVector<uint64_t, 64> TypeVals;
961 
962   uint64_t NumBits = VE.computeBitsRequiredForTypeIndicies();
963 
964   // Abbrev for TYPE_CODE_POINTER.
965   auto Abbv = std::make_shared<BitCodeAbbrev>();
966   Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_POINTER));
967   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
968   Abbv->Add(BitCodeAbbrevOp(0)); // Addrspace = 0
969   unsigned PtrAbbrev = Stream.EmitAbbrev(std::move(Abbv));
970 
971   // Abbrev for TYPE_CODE_FUNCTION.
972   Abbv = std::make_shared<BitCodeAbbrev>();
973   Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_FUNCTION));
974   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isvararg
975   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
976   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
977   unsigned FunctionAbbrev = Stream.EmitAbbrev(std::move(Abbv));
978 
979   // Abbrev for TYPE_CODE_STRUCT_ANON.
980   Abbv = std::make_shared<BitCodeAbbrev>();
981   Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_ANON));
982   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ispacked
983   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
984   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
985   unsigned StructAnonAbbrev = Stream.EmitAbbrev(std::move(Abbv));
986 
987   // Abbrev for TYPE_CODE_STRUCT_NAME.
988   Abbv = std::make_shared<BitCodeAbbrev>();
989   Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_NAME));
990   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
991   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
992   unsigned StructNameAbbrev = Stream.EmitAbbrev(std::move(Abbv));
993 
994   // Abbrev for TYPE_CODE_STRUCT_NAMED.
995   Abbv = std::make_shared<BitCodeAbbrev>();
996   Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_NAMED));
997   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ispacked
998   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
999   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
1000   unsigned StructNamedAbbrev = Stream.EmitAbbrev(std::move(Abbv));
1001 
1002   // Abbrev for TYPE_CODE_ARRAY.
1003   Abbv = std::make_shared<BitCodeAbbrev>();
1004   Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_ARRAY));
1005   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // size
1006   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
1007   unsigned ArrayAbbrev = Stream.EmitAbbrev(std::move(Abbv));
1008 
1009   // Emit an entry count so the reader can reserve space.
1010   TypeVals.push_back(TypeList.size());
1011   Stream.EmitRecord(bitc::TYPE_CODE_NUMENTRY, TypeVals);
1012   TypeVals.clear();
1013 
1014   // Loop over all of the types, emitting each in turn.
1015   for (Type *T : TypeList) {
1016     int AbbrevToUse = 0;
1017     unsigned Code = 0;
1018 
1019     switch (T->getTypeID()) {
1020     case Type::BFloatTyID:
1021     case Type::X86_AMXTyID:
1022     case Type::TokenTyID:
1023     case Type::TargetExtTyID:
1024       llvm_unreachable("These should never be used!!!");
1025       break;
1026     case Type::VoidTyID:
1027       Code = bitc::TYPE_CODE_VOID;
1028       break;
1029     case Type::HalfTyID:
1030       Code = bitc::TYPE_CODE_HALF;
1031       break;
1032     case Type::FloatTyID:
1033       Code = bitc::TYPE_CODE_FLOAT;
1034       break;
1035     case Type::DoubleTyID:
1036       Code = bitc::TYPE_CODE_DOUBLE;
1037       break;
1038     case Type::X86_FP80TyID:
1039       Code = bitc::TYPE_CODE_X86_FP80;
1040       break;
1041     case Type::FP128TyID:
1042       Code = bitc::TYPE_CODE_FP128;
1043       break;
1044     case Type::PPC_FP128TyID:
1045       Code = bitc::TYPE_CODE_PPC_FP128;
1046       break;
1047     case Type::LabelTyID:
1048       Code = bitc::TYPE_CODE_LABEL;
1049       break;
1050     case Type::MetadataTyID:
1051       Code = bitc::TYPE_CODE_METADATA;
1052       break;
1053     case Type::X86_MMXTyID:
1054       Code = bitc::TYPE_CODE_X86_MMX;
1055       break;
1056     case Type::IntegerTyID:
1057       // INTEGER: [width]
1058       Code = bitc::TYPE_CODE_INTEGER;
1059       TypeVals.push_back(cast<IntegerType>(T)->getBitWidth());
1060       break;
1061     case Type::TypedPointerTyID: {
1062       TypedPointerType *PTy = cast<TypedPointerType>(T);
1063       // POINTER: [pointee type, address space]
1064       Code = bitc::TYPE_CODE_POINTER;
1065       TypeVals.push_back(getTypeID(PTy->getElementType()));
1066       unsigned AddressSpace = PTy->getAddressSpace();
1067       TypeVals.push_back(AddressSpace);
1068       if (AddressSpace == 0)
1069         AbbrevToUse = PtrAbbrev;
1070       break;
1071     }
1072     case Type::PointerTyID: {
1073       // POINTER: [pointee type, address space]
1074       // Emitting an empty struct type for the pointer's type allows this to be
1075       // order-independent. Non-struct types must be emitted in bitcode before
1076       // they can be referenced.
1077       TypeVals.push_back(false);
1078       Code = bitc::TYPE_CODE_OPAQUE;
1079       writeStringRecord(Stream, bitc::TYPE_CODE_STRUCT_NAME,
1080                         "dxilOpaquePtrReservedName", StructNameAbbrev);
1081       break;
1082     }
1083     case Type::FunctionTyID: {
1084       FunctionType *FT = cast<FunctionType>(T);
1085       // FUNCTION: [isvararg, retty, paramty x N]
1086       Code = bitc::TYPE_CODE_FUNCTION;
1087       TypeVals.push_back(FT->isVarArg());
1088       TypeVals.push_back(getTypeID(FT->getReturnType()));
1089       for (Type *PTy : FT->params())
1090         TypeVals.push_back(getTypeID(PTy));
1091       AbbrevToUse = FunctionAbbrev;
1092       break;
1093     }
1094     case Type::StructTyID: {
1095       StructType *ST = cast<StructType>(T);
1096       // STRUCT: [ispacked, eltty x N]
1097       TypeVals.push_back(ST->isPacked());
1098       // Output all of the element types.
1099       for (Type *ElTy : ST->elements())
1100         TypeVals.push_back(getTypeID(ElTy));
1101 
1102       if (ST->isLiteral()) {
1103         Code = bitc::TYPE_CODE_STRUCT_ANON;
1104         AbbrevToUse = StructAnonAbbrev;
1105       } else {
1106         if (ST->isOpaque()) {
1107           Code = bitc::TYPE_CODE_OPAQUE;
1108         } else {
1109           Code = bitc::TYPE_CODE_STRUCT_NAMED;
1110           AbbrevToUse = StructNamedAbbrev;
1111         }
1112 
1113         // Emit the name if it is present.
1114         if (!ST->getName().empty())
1115           writeStringRecord(Stream, bitc::TYPE_CODE_STRUCT_NAME, ST->getName(),
1116                             StructNameAbbrev);
1117       }
1118       break;
1119     }
1120     case Type::ArrayTyID: {
1121       ArrayType *AT = cast<ArrayType>(T);
1122       // ARRAY: [numelts, eltty]
1123       Code = bitc::TYPE_CODE_ARRAY;
1124       TypeVals.push_back(AT->getNumElements());
1125       TypeVals.push_back(getTypeID(AT->getElementType()));
1126       AbbrevToUse = ArrayAbbrev;
1127       break;
1128     }
1129     case Type::FixedVectorTyID:
1130     case Type::ScalableVectorTyID: {
1131       VectorType *VT = cast<VectorType>(T);
1132       // VECTOR [numelts, eltty]
1133       Code = bitc::TYPE_CODE_VECTOR;
1134       TypeVals.push_back(VT->getElementCount().getKnownMinValue());
1135       TypeVals.push_back(getTypeID(VT->getElementType()));
1136       break;
1137     }
1138     }
1139 
1140     // Emit the finished record.
1141     Stream.EmitRecord(Code, TypeVals, AbbrevToUse);
1142     TypeVals.clear();
1143   }
1144 
1145   Stream.ExitBlock();
1146 }
1147 
1148 void DXILBitcodeWriter::writeComdats() {
1149   SmallVector<uint16_t, 64> Vals;
1150   for (const Comdat *C : VE.getComdats()) {
1151     // COMDAT: [selection_kind, name]
1152     Vals.push_back(getEncodedComdatSelectionKind(*C));
1153     size_t Size = C->getName().size();
1154     assert(isUInt<16>(Size));
1155     Vals.push_back(Size);
1156     for (char Chr : C->getName())
1157       Vals.push_back((unsigned char)Chr);
1158     Stream.EmitRecord(bitc::MODULE_CODE_COMDAT, Vals, /*AbbrevToUse=*/0);
1159     Vals.clear();
1160   }
1161 }
1162 
1163 void DXILBitcodeWriter::writeValueSymbolTableForwardDecl() {}
1164 
1165 /// Emit top-level description of module, including target triple, inline asm,
1166 /// descriptors for global variables, and function prototype info.
1167 /// Returns the bit offset to backpatch with the location of the real VST.
1168 void DXILBitcodeWriter::writeModuleInfo() {
1169   // Emit various pieces of data attached to a module.
1170   if (!M.getTargetTriple().empty())
1171     writeStringRecord(Stream, bitc::MODULE_CODE_TRIPLE, M.getTargetTriple(),
1172                       0 /*TODO*/);
1173   const std::string &DL = M.getDataLayoutStr();
1174   if (!DL.empty())
1175     writeStringRecord(Stream, bitc::MODULE_CODE_DATALAYOUT, DL, 0 /*TODO*/);
1176   if (!M.getModuleInlineAsm().empty())
1177     writeStringRecord(Stream, bitc::MODULE_CODE_ASM, M.getModuleInlineAsm(),
1178                       0 /*TODO*/);
1179 
1180   // Emit information about sections and GC, computing how many there are. Also
1181   // compute the maximum alignment value.
1182   std::map<std::string, unsigned> SectionMap;
1183   std::map<std::string, unsigned> GCMap;
1184   MaybeAlign MaxAlignment;
1185   unsigned MaxGlobalType = 0;
1186   const auto UpdateMaxAlignment = [&MaxAlignment](const MaybeAlign A) {
1187     if (A)
1188       MaxAlignment = !MaxAlignment ? *A : std::max(*MaxAlignment, *A);
1189   };
1190   for (const GlobalVariable &GV : M.globals()) {
1191     UpdateMaxAlignment(GV.getAlign());
1192     // Use getGlobalObjectValueTypeID to look up the enumerated type ID for
1193     // Global Variable types.
1194     MaxGlobalType = std::max(
1195         MaxGlobalType, getGlobalObjectValueTypeID(GV.getValueType(), &GV));
1196     if (GV.hasSection()) {
1197       // Give section names unique ID's.
1198       unsigned &Entry = SectionMap[std::string(GV.getSection())];
1199       if (!Entry) {
1200         writeStringRecord(Stream, bitc::MODULE_CODE_SECTIONNAME,
1201                           GV.getSection(), 0 /*TODO*/);
1202         Entry = SectionMap.size();
1203       }
1204     }
1205   }
1206   for (const Function &F : M) {
1207     UpdateMaxAlignment(F.getAlign());
1208     if (F.hasSection()) {
1209       // Give section names unique ID's.
1210       unsigned &Entry = SectionMap[std::string(F.getSection())];
1211       if (!Entry) {
1212         writeStringRecord(Stream, bitc::MODULE_CODE_SECTIONNAME, F.getSection(),
1213                           0 /*TODO*/);
1214         Entry = SectionMap.size();
1215       }
1216     }
1217     if (F.hasGC()) {
1218       // Same for GC names.
1219       unsigned &Entry = GCMap[F.getGC()];
1220       if (!Entry) {
1221         writeStringRecord(Stream, bitc::MODULE_CODE_GCNAME, F.getGC(),
1222                           0 /*TODO*/);
1223         Entry = GCMap.size();
1224       }
1225     }
1226   }
1227 
1228   // Emit abbrev for globals, now that we know # sections and max alignment.
1229   unsigned SimpleGVarAbbrev = 0;
1230   if (!M.global_empty()) {
1231     // Add an abbrev for common globals with no visibility or thread
1232     // localness.
1233     auto Abbv = std::make_shared<BitCodeAbbrev>();
1234     Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_GLOBALVAR));
1235     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
1236                               Log2_32_Ceil(MaxGlobalType + 1)));
1237     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // AddrSpace << 2
1238                                                            //| explicitType << 1
1239                                                            //| constant
1240     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // Initializer.
1241     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 5)); // Linkage.
1242     if (!MaxAlignment)                                     // Alignment.
1243       Abbv->Add(BitCodeAbbrevOp(0));
1244     else {
1245       unsigned MaxEncAlignment = getEncodedAlign(MaxAlignment);
1246       Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
1247                                 Log2_32_Ceil(MaxEncAlignment + 1)));
1248     }
1249     if (SectionMap.empty()) // Section.
1250       Abbv->Add(BitCodeAbbrevOp(0));
1251     else
1252       Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
1253                                 Log2_32_Ceil(SectionMap.size() + 1)));
1254     // Don't bother emitting vis + thread local.
1255     SimpleGVarAbbrev = Stream.EmitAbbrev(std::move(Abbv));
1256   }
1257 
1258   // Emit the global variable information.
1259   SmallVector<unsigned, 64> Vals;
1260   for (const GlobalVariable &GV : M.globals()) {
1261     unsigned AbbrevToUse = 0;
1262 
1263     // GLOBALVAR: [type, isconst, initid,
1264     //             linkage, alignment, section, visibility, threadlocal,
1265     //             unnamed_addr, externally_initialized, dllstorageclass,
1266     //             comdat]
1267     Vals.push_back(getGlobalObjectValueTypeID(GV.getValueType(), &GV));
1268     Vals.push_back(
1269         GV.getType()->getAddressSpace() << 2 | 2 |
1270         (GV.isConstant() ? 1 : 0)); // HLSL Change - bitwise | was used with
1271                                     // unsigned int and bool
1272     Vals.push_back(
1273         GV.isDeclaration() ? 0 : (VE.getValueID(GV.getInitializer()) + 1));
1274     Vals.push_back(getEncodedLinkage(GV));
1275     Vals.push_back(getEncodedAlign(GV.getAlign()));
1276     Vals.push_back(GV.hasSection() ? SectionMap[std::string(GV.getSection())]
1277                                    : 0);
1278     if (GV.isThreadLocal() ||
1279         GV.getVisibility() != GlobalValue::DefaultVisibility ||
1280         GV.getUnnamedAddr() != GlobalValue::UnnamedAddr::None ||
1281         GV.isExternallyInitialized() ||
1282         GV.getDLLStorageClass() != GlobalValue::DefaultStorageClass ||
1283         GV.hasComdat()) {
1284       Vals.push_back(getEncodedVisibility(GV));
1285       Vals.push_back(getEncodedThreadLocalMode(GV));
1286       Vals.push_back(GV.getUnnamedAddr() != GlobalValue::UnnamedAddr::None);
1287       Vals.push_back(GV.isExternallyInitialized());
1288       Vals.push_back(getEncodedDLLStorageClass(GV));
1289       Vals.push_back(GV.hasComdat() ? VE.getComdatID(GV.getComdat()) : 0);
1290     } else {
1291       AbbrevToUse = SimpleGVarAbbrev;
1292     }
1293 
1294     Stream.EmitRecord(bitc::MODULE_CODE_GLOBALVAR, Vals, AbbrevToUse);
1295     Vals.clear();
1296   }
1297 
1298   // Emit the function proto information.
1299   for (const Function &F : M) {
1300     // FUNCTION:  [type, callingconv, isproto, linkage, paramattrs, alignment,
1301     //             section, visibility, gc, unnamed_addr, prologuedata,
1302     //             dllstorageclass, comdat, prefixdata, personalityfn]
1303     Vals.push_back(getGlobalObjectValueTypeID(F.getFunctionType(), &F));
1304     Vals.push_back(F.getCallingConv());
1305     Vals.push_back(F.isDeclaration());
1306     Vals.push_back(getEncodedLinkage(F));
1307     Vals.push_back(VE.getAttributeListID(F.getAttributes()));
1308     Vals.push_back(getEncodedAlign(F.getAlign()));
1309     Vals.push_back(F.hasSection() ? SectionMap[std::string(F.getSection())]
1310                                   : 0);
1311     Vals.push_back(getEncodedVisibility(F));
1312     Vals.push_back(F.hasGC() ? GCMap[F.getGC()] : 0);
1313     Vals.push_back(F.getUnnamedAddr() != GlobalValue::UnnamedAddr::None);
1314     Vals.push_back(
1315         F.hasPrologueData() ? (VE.getValueID(F.getPrologueData()) + 1) : 0);
1316     Vals.push_back(getEncodedDLLStorageClass(F));
1317     Vals.push_back(F.hasComdat() ? VE.getComdatID(F.getComdat()) : 0);
1318     Vals.push_back(F.hasPrefixData() ? (VE.getValueID(F.getPrefixData()) + 1)
1319                                      : 0);
1320     Vals.push_back(
1321         F.hasPersonalityFn() ? (VE.getValueID(F.getPersonalityFn()) + 1) : 0);
1322 
1323     unsigned AbbrevToUse = 0;
1324     Stream.EmitRecord(bitc::MODULE_CODE_FUNCTION, Vals, AbbrevToUse);
1325     Vals.clear();
1326   }
1327 
1328   // Emit the alias information.
1329   for (const GlobalAlias &A : M.aliases()) {
1330     // ALIAS: [alias type, aliasee val#, linkage, visibility]
1331     Vals.push_back(getTypeID(A.getValueType(), &A));
1332     Vals.push_back(VE.getValueID(A.getAliasee()));
1333     Vals.push_back(getEncodedLinkage(A));
1334     Vals.push_back(getEncodedVisibility(A));
1335     Vals.push_back(getEncodedDLLStorageClass(A));
1336     Vals.push_back(getEncodedThreadLocalMode(A));
1337     Vals.push_back(A.getUnnamedAddr() != GlobalValue::UnnamedAddr::None);
1338     unsigned AbbrevToUse = 0;
1339     Stream.EmitRecord(bitc::MODULE_CODE_ALIAS_OLD, Vals, AbbrevToUse);
1340     Vals.clear();
1341   }
1342 }
1343 
1344 void DXILBitcodeWriter::writeValueAsMetadata(
1345     const ValueAsMetadata *MD, SmallVectorImpl<uint64_t> &Record) {
1346   // Mimic an MDNode with a value as one operand.
1347   Value *V = MD->getValue();
1348   Type *Ty = V->getType();
1349   if (Function *F = dyn_cast<Function>(V))
1350     Ty = TypedPointerType::get(F->getFunctionType(), F->getAddressSpace());
1351   else if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
1352     Ty = TypedPointerType::get(GV->getValueType(), GV->getAddressSpace());
1353   Record.push_back(getTypeID(Ty));
1354   Record.push_back(VE.getValueID(V));
1355   Stream.EmitRecord(bitc::METADATA_VALUE, Record, 0);
1356   Record.clear();
1357 }
1358 
1359 void DXILBitcodeWriter::writeMDTuple(const MDTuple *N,
1360                                      SmallVectorImpl<uint64_t> &Record,
1361                                      unsigned Abbrev) {
1362   for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
1363     Metadata *MD = N->getOperand(i);
1364     assert(!(MD && isa<LocalAsMetadata>(MD)) &&
1365            "Unexpected function-local metadata");
1366     Record.push_back(VE.getMetadataOrNullID(MD));
1367   }
1368   Stream.EmitRecord(N->isDistinct() ? bitc::METADATA_DISTINCT_NODE
1369                                     : bitc::METADATA_NODE,
1370                     Record, Abbrev);
1371   Record.clear();
1372 }
1373 
1374 void DXILBitcodeWriter::writeDILocation(const DILocation *N,
1375                                         SmallVectorImpl<uint64_t> &Record,
1376                                         unsigned &Abbrev) {
1377   if (!Abbrev)
1378     Abbrev = createDILocationAbbrev();
1379   Record.push_back(N->isDistinct());
1380   Record.push_back(N->getLine());
1381   Record.push_back(N->getColumn());
1382   Record.push_back(VE.getMetadataID(N->getScope()));
1383   Record.push_back(VE.getMetadataOrNullID(N->getInlinedAt()));
1384 
1385   Stream.EmitRecord(bitc::METADATA_LOCATION, Record, Abbrev);
1386   Record.clear();
1387 }
1388 
1389 static uint64_t rotateSign(APInt Val) {
1390   int64_t I = Val.getSExtValue();
1391   uint64_t U = I;
1392   return I < 0 ? ~(U << 1) : U << 1;
1393 }
1394 
1395 static uint64_t rotateSign(DISubrange::BoundType Val) {
1396   return rotateSign(Val.get<ConstantInt *>()->getValue());
1397 }
1398 
1399 void DXILBitcodeWriter::writeDISubrange(const DISubrange *N,
1400                                         SmallVectorImpl<uint64_t> &Record,
1401                                         unsigned Abbrev) {
1402   Record.push_back(N->isDistinct());
1403   Record.push_back(
1404       N->getCount().get<ConstantInt *>()->getValue().getSExtValue());
1405   Record.push_back(rotateSign(N->getLowerBound()));
1406 
1407   Stream.EmitRecord(bitc::METADATA_SUBRANGE, Record, Abbrev);
1408   Record.clear();
1409 }
1410 
1411 void DXILBitcodeWriter::writeDIEnumerator(const DIEnumerator *N,
1412                                           SmallVectorImpl<uint64_t> &Record,
1413                                           unsigned Abbrev) {
1414   Record.push_back(N->isDistinct());
1415   Record.push_back(rotateSign(N->getValue()));
1416   Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1417 
1418   Stream.EmitRecord(bitc::METADATA_ENUMERATOR, Record, Abbrev);
1419   Record.clear();
1420 }
1421 
1422 void DXILBitcodeWriter::writeDIBasicType(const DIBasicType *N,
1423                                          SmallVectorImpl<uint64_t> &Record,
1424                                          unsigned Abbrev) {
1425   Record.push_back(N->isDistinct());
1426   Record.push_back(N->getTag());
1427   Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1428   Record.push_back(N->getSizeInBits());
1429   Record.push_back(N->getAlignInBits());
1430   Record.push_back(N->getEncoding());
1431 
1432   Stream.EmitRecord(bitc::METADATA_BASIC_TYPE, Record, Abbrev);
1433   Record.clear();
1434 }
1435 
1436 void DXILBitcodeWriter::writeDIDerivedType(const DIDerivedType *N,
1437                                            SmallVectorImpl<uint64_t> &Record,
1438                                            unsigned Abbrev) {
1439   Record.push_back(N->isDistinct());
1440   Record.push_back(N->getTag());
1441   Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1442   Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1443   Record.push_back(N->getLine());
1444   Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1445   Record.push_back(VE.getMetadataOrNullID(N->getBaseType()));
1446   Record.push_back(N->getSizeInBits());
1447   Record.push_back(N->getAlignInBits());
1448   Record.push_back(N->getOffsetInBits());
1449   Record.push_back(N->getFlags());
1450   Record.push_back(VE.getMetadataOrNullID(N->getExtraData()));
1451 
1452   Stream.EmitRecord(bitc::METADATA_DERIVED_TYPE, Record, Abbrev);
1453   Record.clear();
1454 }
1455 
1456 void DXILBitcodeWriter::writeDICompositeType(const DICompositeType *N,
1457                                              SmallVectorImpl<uint64_t> &Record,
1458                                              unsigned Abbrev) {
1459   Record.push_back(N->isDistinct());
1460   Record.push_back(N->getTag());
1461   Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1462   Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1463   Record.push_back(N->getLine());
1464   Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1465   Record.push_back(VE.getMetadataOrNullID(N->getBaseType()));
1466   Record.push_back(N->getSizeInBits());
1467   Record.push_back(N->getAlignInBits());
1468   Record.push_back(N->getOffsetInBits());
1469   Record.push_back(N->getFlags());
1470   Record.push_back(VE.getMetadataOrNullID(N->getElements().get()));
1471   Record.push_back(N->getRuntimeLang());
1472   Record.push_back(VE.getMetadataOrNullID(N->getVTableHolder()));
1473   Record.push_back(VE.getMetadataOrNullID(N->getTemplateParams().get()));
1474   Record.push_back(VE.getMetadataOrNullID(N->getRawIdentifier()));
1475 
1476   Stream.EmitRecord(bitc::METADATA_COMPOSITE_TYPE, Record, Abbrev);
1477   Record.clear();
1478 }
1479 
1480 void DXILBitcodeWriter::writeDISubroutineType(const DISubroutineType *N,
1481                                               SmallVectorImpl<uint64_t> &Record,
1482                                               unsigned Abbrev) {
1483   Record.push_back(N->isDistinct());
1484   Record.push_back(N->getFlags());
1485   Record.push_back(VE.getMetadataOrNullID(N->getTypeArray().get()));
1486 
1487   Stream.EmitRecord(bitc::METADATA_SUBROUTINE_TYPE, Record, Abbrev);
1488   Record.clear();
1489 }
1490 
1491 void DXILBitcodeWriter::writeDIFile(const DIFile *N,
1492                                     SmallVectorImpl<uint64_t> &Record,
1493                                     unsigned Abbrev) {
1494   Record.push_back(N->isDistinct());
1495   Record.push_back(VE.getMetadataOrNullID(N->getRawFilename()));
1496   Record.push_back(VE.getMetadataOrNullID(N->getRawDirectory()));
1497 
1498   Stream.EmitRecord(bitc::METADATA_FILE, Record, Abbrev);
1499   Record.clear();
1500 }
1501 
1502 void DXILBitcodeWriter::writeDICompileUnit(const DICompileUnit *N,
1503                                            SmallVectorImpl<uint64_t> &Record,
1504                                            unsigned Abbrev) {
1505   Record.push_back(N->isDistinct());
1506   Record.push_back(N->getSourceLanguage());
1507   Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1508   Record.push_back(VE.getMetadataOrNullID(N->getRawProducer()));
1509   Record.push_back(N->isOptimized());
1510   Record.push_back(VE.getMetadataOrNullID(N->getRawFlags()));
1511   Record.push_back(N->getRuntimeVersion());
1512   Record.push_back(VE.getMetadataOrNullID(N->getRawSplitDebugFilename()));
1513   Record.push_back(N->getEmissionKind());
1514   Record.push_back(VE.getMetadataOrNullID(N->getEnumTypes().get()));
1515   Record.push_back(VE.getMetadataOrNullID(N->getRetainedTypes().get()));
1516   Record.push_back(/* subprograms */ 0);
1517   Record.push_back(VE.getMetadataOrNullID(N->getGlobalVariables().get()));
1518   Record.push_back(VE.getMetadataOrNullID(N->getImportedEntities().get()));
1519   Record.push_back(N->getDWOId());
1520 
1521   Stream.EmitRecord(bitc::METADATA_COMPILE_UNIT, Record, Abbrev);
1522   Record.clear();
1523 }
1524 
1525 void DXILBitcodeWriter::writeDISubprogram(const DISubprogram *N,
1526                                           SmallVectorImpl<uint64_t> &Record,
1527                                           unsigned Abbrev) {
1528   Record.push_back(N->isDistinct());
1529   Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1530   Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1531   Record.push_back(VE.getMetadataOrNullID(N->getRawLinkageName()));
1532   Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1533   Record.push_back(N->getLine());
1534   Record.push_back(VE.getMetadataOrNullID(N->getType()));
1535   Record.push_back(N->isLocalToUnit());
1536   Record.push_back(N->isDefinition());
1537   Record.push_back(N->getScopeLine());
1538   Record.push_back(VE.getMetadataOrNullID(N->getContainingType()));
1539   Record.push_back(N->getVirtuality());
1540   Record.push_back(N->getVirtualIndex());
1541   Record.push_back(N->getFlags());
1542   Record.push_back(N->isOptimized());
1543   Record.push_back(VE.getMetadataOrNullID(N->getRawUnit()));
1544   Record.push_back(VE.getMetadataOrNullID(N->getTemplateParams().get()));
1545   Record.push_back(VE.getMetadataOrNullID(N->getDeclaration()));
1546   Record.push_back(VE.getMetadataOrNullID(N->getRetainedNodes().get()));
1547 
1548   Stream.EmitRecord(bitc::METADATA_SUBPROGRAM, Record, Abbrev);
1549   Record.clear();
1550 }
1551 
1552 void DXILBitcodeWriter::writeDILexicalBlock(const DILexicalBlock *N,
1553                                             SmallVectorImpl<uint64_t> &Record,
1554                                             unsigned Abbrev) {
1555   Record.push_back(N->isDistinct());
1556   Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1557   Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1558   Record.push_back(N->getLine());
1559   Record.push_back(N->getColumn());
1560 
1561   Stream.EmitRecord(bitc::METADATA_LEXICAL_BLOCK, Record, Abbrev);
1562   Record.clear();
1563 }
1564 
1565 void DXILBitcodeWriter::writeDILexicalBlockFile(
1566     const DILexicalBlockFile *N, SmallVectorImpl<uint64_t> &Record,
1567     unsigned Abbrev) {
1568   Record.push_back(N->isDistinct());
1569   Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1570   Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1571   Record.push_back(N->getDiscriminator());
1572 
1573   Stream.EmitRecord(bitc::METADATA_LEXICAL_BLOCK_FILE, Record, Abbrev);
1574   Record.clear();
1575 }
1576 
1577 void DXILBitcodeWriter::writeDINamespace(const DINamespace *N,
1578                                          SmallVectorImpl<uint64_t> &Record,
1579                                          unsigned Abbrev) {
1580   Record.push_back(N->isDistinct());
1581   Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1582   Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1583   Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1584   Record.push_back(/* line number */ 0);
1585 
1586   Stream.EmitRecord(bitc::METADATA_NAMESPACE, Record, Abbrev);
1587   Record.clear();
1588 }
1589 
1590 void DXILBitcodeWriter::writeDIModule(const DIModule *N,
1591                                       SmallVectorImpl<uint64_t> &Record,
1592                                       unsigned Abbrev) {
1593   Record.push_back(N->isDistinct());
1594   for (auto &I : N->operands())
1595     Record.push_back(VE.getMetadataOrNullID(I));
1596 
1597   Stream.EmitRecord(bitc::METADATA_MODULE, Record, Abbrev);
1598   Record.clear();
1599 }
1600 
1601 void DXILBitcodeWriter::writeDITemplateTypeParameter(
1602     const DITemplateTypeParameter *N, SmallVectorImpl<uint64_t> &Record,
1603     unsigned Abbrev) {
1604   Record.push_back(N->isDistinct());
1605   Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1606   Record.push_back(VE.getMetadataOrNullID(N->getType()));
1607 
1608   Stream.EmitRecord(bitc::METADATA_TEMPLATE_TYPE, Record, Abbrev);
1609   Record.clear();
1610 }
1611 
1612 void DXILBitcodeWriter::writeDITemplateValueParameter(
1613     const DITemplateValueParameter *N, SmallVectorImpl<uint64_t> &Record,
1614     unsigned Abbrev) {
1615   Record.push_back(N->isDistinct());
1616   Record.push_back(N->getTag());
1617   Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1618   Record.push_back(VE.getMetadataOrNullID(N->getType()));
1619   Record.push_back(VE.getMetadataOrNullID(N->getValue()));
1620 
1621   Stream.EmitRecord(bitc::METADATA_TEMPLATE_VALUE, Record, Abbrev);
1622   Record.clear();
1623 }
1624 
1625 void DXILBitcodeWriter::writeDIGlobalVariable(const DIGlobalVariable *N,
1626                                               SmallVectorImpl<uint64_t> &Record,
1627                                               unsigned Abbrev) {
1628   Record.push_back(N->isDistinct());
1629   Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1630   Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1631   Record.push_back(VE.getMetadataOrNullID(N->getRawLinkageName()));
1632   Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1633   Record.push_back(N->getLine());
1634   Record.push_back(VE.getMetadataOrNullID(N->getType()));
1635   Record.push_back(N->isLocalToUnit());
1636   Record.push_back(N->isDefinition());
1637   Record.push_back(/* N->getRawVariable() */ 0);
1638   Record.push_back(VE.getMetadataOrNullID(N->getStaticDataMemberDeclaration()));
1639 
1640   Stream.EmitRecord(bitc::METADATA_GLOBAL_VAR, Record, Abbrev);
1641   Record.clear();
1642 }
1643 
1644 void DXILBitcodeWriter::writeDILocalVariable(const DILocalVariable *N,
1645                                              SmallVectorImpl<uint64_t> &Record,
1646                                              unsigned Abbrev) {
1647   Record.push_back(N->isDistinct());
1648   Record.push_back(N->getTag());
1649   Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1650   Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1651   Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1652   Record.push_back(N->getLine());
1653   Record.push_back(VE.getMetadataOrNullID(N->getType()));
1654   Record.push_back(N->getArg());
1655   Record.push_back(N->getFlags());
1656 
1657   Stream.EmitRecord(bitc::METADATA_LOCAL_VAR, Record, Abbrev);
1658   Record.clear();
1659 }
1660 
1661 void DXILBitcodeWriter::writeDIExpression(const DIExpression *N,
1662                                           SmallVectorImpl<uint64_t> &Record,
1663                                           unsigned Abbrev) {
1664   Record.reserve(N->getElements().size() + 1);
1665 
1666   Record.push_back(N->isDistinct());
1667   Record.append(N->elements_begin(), N->elements_end());
1668 
1669   Stream.EmitRecord(bitc::METADATA_EXPRESSION, Record, Abbrev);
1670   Record.clear();
1671 }
1672 
1673 void DXILBitcodeWriter::writeDIObjCProperty(const DIObjCProperty *N,
1674                                             SmallVectorImpl<uint64_t> &Record,
1675                                             unsigned Abbrev) {
1676   llvm_unreachable("DXIL does not support objc!!!");
1677 }
1678 
1679 void DXILBitcodeWriter::writeDIImportedEntity(const DIImportedEntity *N,
1680                                               SmallVectorImpl<uint64_t> &Record,
1681                                               unsigned Abbrev) {
1682   Record.push_back(N->isDistinct());
1683   Record.push_back(N->getTag());
1684   Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1685   Record.push_back(VE.getMetadataOrNullID(N->getEntity()));
1686   Record.push_back(N->getLine());
1687   Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1688 
1689   Stream.EmitRecord(bitc::METADATA_IMPORTED_ENTITY, Record, Abbrev);
1690   Record.clear();
1691 }
1692 
1693 unsigned DXILBitcodeWriter::createDILocationAbbrev() {
1694   // Abbrev for METADATA_LOCATION.
1695   //
1696   // Assume the column is usually under 128, and always output the inlined-at
1697   // location (it's never more expensive than building an array size 1).
1698   std::shared_ptr<BitCodeAbbrev> Abbv = std::make_shared<BitCodeAbbrev>();
1699   Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_LOCATION));
1700   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
1701   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1702   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1703   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1704   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1705   return Stream.EmitAbbrev(std::move(Abbv));
1706 }
1707 
1708 unsigned DXILBitcodeWriter::createGenericDINodeAbbrev() {
1709   // Abbrev for METADATA_GENERIC_DEBUG.
1710   //
1711   // Assume the column is usually under 128, and always output the inlined-at
1712   // location (it's never more expensive than building an array size 1).
1713   std::shared_ptr<BitCodeAbbrev> Abbv = std::make_shared<BitCodeAbbrev>();
1714   Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_GENERIC_DEBUG));
1715   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
1716   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1717   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
1718   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1719   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1720   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1721   return Stream.EmitAbbrev(std::move(Abbv));
1722 }
1723 
1724 void DXILBitcodeWriter::writeMetadataRecords(ArrayRef<const Metadata *> MDs,
1725                                              SmallVectorImpl<uint64_t> &Record,
1726                                              std::vector<unsigned> *MDAbbrevs,
1727                                              std::vector<uint64_t> *IndexPos) {
1728   if (MDs.empty())
1729     return;
1730 
1731     // Initialize MDNode abbreviations.
1732 #define HANDLE_MDNODE_LEAF(CLASS) unsigned CLASS##Abbrev = 0;
1733 #include "llvm/IR/Metadata.def"
1734 
1735   for (const Metadata *MD : MDs) {
1736     if (IndexPos)
1737       IndexPos->push_back(Stream.GetCurrentBitNo());
1738     if (const MDNode *N = dyn_cast<MDNode>(MD)) {
1739       assert(N->isResolved() && "Expected forward references to be resolved");
1740 
1741       switch (N->getMetadataID()) {
1742       default:
1743         llvm_unreachable("Invalid MDNode subclass");
1744 #define HANDLE_MDNODE_LEAF(CLASS)                                              \
1745   case Metadata::CLASS##Kind:                                                  \
1746     if (MDAbbrevs)                                                             \
1747       write##CLASS(cast<CLASS>(N), Record,                                     \
1748                    (*MDAbbrevs)[MetadataAbbrev::CLASS##AbbrevID]);             \
1749     else                                                                       \
1750       write##CLASS(cast<CLASS>(N), Record, CLASS##Abbrev);                     \
1751     continue;
1752 #include "llvm/IR/Metadata.def"
1753       }
1754     }
1755     writeValueAsMetadata(cast<ValueAsMetadata>(MD), Record);
1756   }
1757 }
1758 
1759 unsigned DXILBitcodeWriter::createMetadataStringsAbbrev() {
1760   auto Abbv = std::make_shared<BitCodeAbbrev>();
1761   Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_STRING_OLD));
1762   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1763   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
1764   return Stream.EmitAbbrev(std::move(Abbv));
1765 }
1766 
1767 void DXILBitcodeWriter::writeMetadataStrings(
1768     ArrayRef<const Metadata *> Strings, SmallVectorImpl<uint64_t> &Record) {
1769   for (const Metadata *MD : Strings) {
1770     const MDString *MDS = cast<MDString>(MD);
1771     // Code: [strchar x N]
1772     Record.append(MDS->bytes_begin(), MDS->bytes_end());
1773 
1774     // Emit the finished record.
1775     Stream.EmitRecord(bitc::METADATA_STRING_OLD, Record,
1776                       createMetadataStringsAbbrev());
1777     Record.clear();
1778   }
1779 }
1780 
1781 void DXILBitcodeWriter::writeModuleMetadata() {
1782   if (!VE.hasMDs() && M.named_metadata_empty())
1783     return;
1784 
1785   Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 5);
1786 
1787   // Emit all abbrevs upfront, so that the reader can jump in the middle of the
1788   // block and load any metadata.
1789   std::vector<unsigned> MDAbbrevs;
1790 
1791   MDAbbrevs.resize(MetadataAbbrev::LastPlusOne);
1792   MDAbbrevs[MetadataAbbrev::DILocationAbbrevID] = createDILocationAbbrev();
1793   MDAbbrevs[MetadataAbbrev::GenericDINodeAbbrevID] =
1794       createGenericDINodeAbbrev();
1795 
1796   unsigned NameAbbrev = 0;
1797   if (!M.named_metadata_empty()) {
1798     // Abbrev for METADATA_NAME.
1799     std::shared_ptr<BitCodeAbbrev> Abbv = std::make_shared<BitCodeAbbrev>();
1800     Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_NAME));
1801     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1802     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
1803     NameAbbrev = Stream.EmitAbbrev(std::move(Abbv));
1804   }
1805 
1806   SmallVector<uint64_t, 64> Record;
1807   writeMetadataStrings(VE.getMDStrings(), Record);
1808 
1809   std::vector<uint64_t> IndexPos;
1810   IndexPos.reserve(VE.getNonMDStrings().size());
1811   writeMetadataRecords(VE.getNonMDStrings(), Record, &MDAbbrevs, &IndexPos);
1812 
1813   // Write named metadata.
1814   for (const NamedMDNode &NMD : M.named_metadata()) {
1815     // Write name.
1816     StringRef Str = NMD.getName();
1817     Record.append(Str.bytes_begin(), Str.bytes_end());
1818     Stream.EmitRecord(bitc::METADATA_NAME, Record, NameAbbrev);
1819     Record.clear();
1820 
1821     // Write named metadata operands.
1822     for (const MDNode *N : NMD.operands())
1823       Record.push_back(VE.getMetadataID(N));
1824     Stream.EmitRecord(bitc::METADATA_NAMED_NODE, Record, 0);
1825     Record.clear();
1826   }
1827 
1828   Stream.ExitBlock();
1829 }
1830 
1831 void DXILBitcodeWriter::writeFunctionMetadata(const Function &F) {
1832   if (!VE.hasMDs())
1833     return;
1834 
1835   Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 4);
1836   SmallVector<uint64_t, 64> Record;
1837   writeMetadataStrings(VE.getMDStrings(), Record);
1838   writeMetadataRecords(VE.getNonMDStrings(), Record);
1839   Stream.ExitBlock();
1840 }
1841 
1842 void DXILBitcodeWriter::writeFunctionMetadataAttachment(const Function &F) {
1843   Stream.EnterSubblock(bitc::METADATA_ATTACHMENT_ID, 3);
1844 
1845   SmallVector<uint64_t, 64> Record;
1846 
1847   // Write metadata attachments
1848   // METADATA_ATTACHMENT - [m x [value, [n x [id, mdnode]]]
1849   SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
1850   F.getAllMetadata(MDs);
1851   if (!MDs.empty()) {
1852     for (const auto &I : MDs) {
1853       Record.push_back(I.first);
1854       Record.push_back(VE.getMetadataID(I.second));
1855     }
1856     Stream.EmitRecord(bitc::METADATA_ATTACHMENT, Record, 0);
1857     Record.clear();
1858   }
1859 
1860   for (const BasicBlock &BB : F)
1861     for (const Instruction &I : BB) {
1862       MDs.clear();
1863       I.getAllMetadataOtherThanDebugLoc(MDs);
1864 
1865       // If no metadata, ignore instruction.
1866       if (MDs.empty())
1867         continue;
1868 
1869       Record.push_back(VE.getInstructionID(&I));
1870 
1871       for (unsigned i = 0, e = MDs.size(); i != e; ++i) {
1872         Record.push_back(MDs[i].first);
1873         Record.push_back(VE.getMetadataID(MDs[i].second));
1874       }
1875       Stream.EmitRecord(bitc::METADATA_ATTACHMENT, Record, 0);
1876       Record.clear();
1877     }
1878 
1879   Stream.ExitBlock();
1880 }
1881 
1882 void DXILBitcodeWriter::writeModuleMetadataKinds() {
1883   SmallVector<uint64_t, 64> Record;
1884 
1885   // Write metadata kinds
1886   // METADATA_KIND - [n x [id, name]]
1887   SmallVector<StringRef, 8> Names;
1888   M.getMDKindNames(Names);
1889 
1890   if (Names.empty())
1891     return;
1892 
1893   Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
1894 
1895   for (unsigned MDKindID = 0, e = Names.size(); MDKindID != e; ++MDKindID) {
1896     Record.push_back(MDKindID);
1897     StringRef KName = Names[MDKindID];
1898     Record.append(KName.begin(), KName.end());
1899 
1900     Stream.EmitRecord(bitc::METADATA_KIND, Record, 0);
1901     Record.clear();
1902   }
1903 
1904   Stream.ExitBlock();
1905 }
1906 
1907 void DXILBitcodeWriter::writeConstants(unsigned FirstVal, unsigned LastVal,
1908                                        bool isGlobal) {
1909   if (FirstVal == LastVal)
1910     return;
1911 
1912   Stream.EnterSubblock(bitc::CONSTANTS_BLOCK_ID, 4);
1913 
1914   unsigned AggregateAbbrev = 0;
1915   unsigned String8Abbrev = 0;
1916   unsigned CString7Abbrev = 0;
1917   unsigned CString6Abbrev = 0;
1918   // If this is a constant pool for the module, emit module-specific abbrevs.
1919   if (isGlobal) {
1920     // Abbrev for CST_CODE_AGGREGATE.
1921     auto Abbv = std::make_shared<BitCodeAbbrev>();
1922     Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_AGGREGATE));
1923     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1924     Abbv->Add(
1925         BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, Log2_32_Ceil(LastVal + 1)));
1926     AggregateAbbrev = Stream.EmitAbbrev(std::move(Abbv));
1927 
1928     // Abbrev for CST_CODE_STRING.
1929     Abbv = std::make_shared<BitCodeAbbrev>();
1930     Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_STRING));
1931     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1932     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
1933     String8Abbrev = Stream.EmitAbbrev(std::move(Abbv));
1934     // Abbrev for CST_CODE_CSTRING.
1935     Abbv = std::make_shared<BitCodeAbbrev>();
1936     Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING));
1937     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1938     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
1939     CString7Abbrev = Stream.EmitAbbrev(std::move(Abbv));
1940     // Abbrev for CST_CODE_CSTRING.
1941     Abbv = std::make_shared<BitCodeAbbrev>();
1942     Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING));
1943     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1944     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
1945     CString6Abbrev = Stream.EmitAbbrev(std::move(Abbv));
1946   }
1947 
1948   SmallVector<uint64_t, 64> Record;
1949 
1950   const ValueEnumerator::ValueList &Vals = VE.getValues();
1951   Type *LastTy = nullptr;
1952   for (unsigned i = FirstVal; i != LastVal; ++i) {
1953     const Value *V = Vals[i].first;
1954     // If we need to switch types, do so now.
1955     if (V->getType() != LastTy) {
1956       LastTy = V->getType();
1957       Record.push_back(getTypeID(LastTy, V));
1958       Stream.EmitRecord(bitc::CST_CODE_SETTYPE, Record,
1959                         CONSTANTS_SETTYPE_ABBREV);
1960       Record.clear();
1961     }
1962 
1963     if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
1964       Record.push_back(unsigned(IA->hasSideEffects()) |
1965                        unsigned(IA->isAlignStack()) << 1 |
1966                        unsigned(IA->getDialect() & 1) << 2);
1967 
1968       // Add the asm string.
1969       const std::string &AsmStr = IA->getAsmString();
1970       Record.push_back(AsmStr.size());
1971       Record.append(AsmStr.begin(), AsmStr.end());
1972 
1973       // Add the constraint string.
1974       const std::string &ConstraintStr = IA->getConstraintString();
1975       Record.push_back(ConstraintStr.size());
1976       Record.append(ConstraintStr.begin(), ConstraintStr.end());
1977       Stream.EmitRecord(bitc::CST_CODE_INLINEASM, Record);
1978       Record.clear();
1979       continue;
1980     }
1981     const Constant *C = cast<Constant>(V);
1982     unsigned Code = -1U;
1983     unsigned AbbrevToUse = 0;
1984     if (C->isNullValue()) {
1985       Code = bitc::CST_CODE_NULL;
1986     } else if (isa<UndefValue>(C)) {
1987       Code = bitc::CST_CODE_UNDEF;
1988     } else if (const ConstantInt *IV = dyn_cast<ConstantInt>(C)) {
1989       if (IV->getBitWidth() <= 64) {
1990         uint64_t V = IV->getSExtValue();
1991         emitSignedInt64(Record, V);
1992         Code = bitc::CST_CODE_INTEGER;
1993         AbbrevToUse = CONSTANTS_INTEGER_ABBREV;
1994       } else { // Wide integers, > 64 bits in size.
1995         // We have an arbitrary precision integer value to write whose
1996         // bit width is > 64. However, in canonical unsigned integer
1997         // format it is likely that the high bits are going to be zero.
1998         // So, we only write the number of active words.
1999         unsigned NWords = IV->getValue().getActiveWords();
2000         const uint64_t *RawWords = IV->getValue().getRawData();
2001         for (unsigned i = 0; i != NWords; ++i) {
2002           emitSignedInt64(Record, RawWords[i]);
2003         }
2004         Code = bitc::CST_CODE_WIDE_INTEGER;
2005       }
2006     } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
2007       Code = bitc::CST_CODE_FLOAT;
2008       Type *Ty = CFP->getType();
2009       if (Ty->isHalfTy() || Ty->isFloatTy() || Ty->isDoubleTy()) {
2010         Record.push_back(CFP->getValueAPF().bitcastToAPInt().getZExtValue());
2011       } else if (Ty->isX86_FP80Ty()) {
2012         // api needed to prevent premature destruction
2013         // bits are not in the same order as a normal i80 APInt, compensate.
2014         APInt api = CFP->getValueAPF().bitcastToAPInt();
2015         const uint64_t *p = api.getRawData();
2016         Record.push_back((p[1] << 48) | (p[0] >> 16));
2017         Record.push_back(p[0] & 0xffffLL);
2018       } else if (Ty->isFP128Ty() || Ty->isPPC_FP128Ty()) {
2019         APInt api = CFP->getValueAPF().bitcastToAPInt();
2020         const uint64_t *p = api.getRawData();
2021         Record.push_back(p[0]);
2022         Record.push_back(p[1]);
2023       } else {
2024         assert(0 && "Unknown FP type!");
2025       }
2026     } else if (isa<ConstantDataSequential>(C) &&
2027                cast<ConstantDataSequential>(C)->isString()) {
2028       const ConstantDataSequential *Str = cast<ConstantDataSequential>(C);
2029       // Emit constant strings specially.
2030       unsigned NumElts = Str->getNumElements();
2031       // If this is a null-terminated string, use the denser CSTRING encoding.
2032       if (Str->isCString()) {
2033         Code = bitc::CST_CODE_CSTRING;
2034         --NumElts; // Don't encode the null, which isn't allowed by char6.
2035       } else {
2036         Code = bitc::CST_CODE_STRING;
2037         AbbrevToUse = String8Abbrev;
2038       }
2039       bool isCStr7 = Code == bitc::CST_CODE_CSTRING;
2040       bool isCStrChar6 = Code == bitc::CST_CODE_CSTRING;
2041       for (unsigned i = 0; i != NumElts; ++i) {
2042         unsigned char V = Str->getElementAsInteger(i);
2043         Record.push_back(V);
2044         isCStr7 &= (V & 128) == 0;
2045         if (isCStrChar6)
2046           isCStrChar6 = BitCodeAbbrevOp::isChar6(V);
2047       }
2048 
2049       if (isCStrChar6)
2050         AbbrevToUse = CString6Abbrev;
2051       else if (isCStr7)
2052         AbbrevToUse = CString7Abbrev;
2053     } else if (const ConstantDataSequential *CDS =
2054                    dyn_cast<ConstantDataSequential>(C)) {
2055       Code = bitc::CST_CODE_DATA;
2056       Type *EltTy = CDS->getElementType();
2057       if (isa<IntegerType>(EltTy)) {
2058         for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i)
2059           Record.push_back(CDS->getElementAsInteger(i));
2060       } else if (EltTy->isFloatTy()) {
2061         for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) {
2062           union {
2063             float F;
2064             uint32_t I;
2065           };
2066           F = CDS->getElementAsFloat(i);
2067           Record.push_back(I);
2068         }
2069       } else {
2070         assert(EltTy->isDoubleTy() && "Unknown ConstantData element type");
2071         for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) {
2072           union {
2073             double F;
2074             uint64_t I;
2075           };
2076           F = CDS->getElementAsDouble(i);
2077           Record.push_back(I);
2078         }
2079       }
2080     } else if (isa<ConstantArray>(C) || isa<ConstantStruct>(C) ||
2081                isa<ConstantVector>(C)) {
2082       Code = bitc::CST_CODE_AGGREGATE;
2083       for (const Value *Op : C->operands())
2084         Record.push_back(VE.getValueID(Op));
2085       AbbrevToUse = AggregateAbbrev;
2086     } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
2087       switch (CE->getOpcode()) {
2088       default:
2089         if (Instruction::isCast(CE->getOpcode())) {
2090           Code = bitc::CST_CODE_CE_CAST;
2091           Record.push_back(getEncodedCastOpcode(CE->getOpcode()));
2092           Record.push_back(
2093               getTypeID(C->getOperand(0)->getType(), C->getOperand(0)));
2094           Record.push_back(VE.getValueID(C->getOperand(0)));
2095           AbbrevToUse = CONSTANTS_CE_CAST_Abbrev;
2096         } else {
2097           assert(CE->getNumOperands() == 2 && "Unknown constant expr!");
2098           Code = bitc::CST_CODE_CE_BINOP;
2099           Record.push_back(getEncodedBinaryOpcode(CE->getOpcode()));
2100           Record.push_back(VE.getValueID(C->getOperand(0)));
2101           Record.push_back(VE.getValueID(C->getOperand(1)));
2102           uint64_t Flags = getOptimizationFlags(CE);
2103           if (Flags != 0)
2104             Record.push_back(Flags);
2105         }
2106         break;
2107       case Instruction::GetElementPtr: {
2108         Code = bitc::CST_CODE_CE_GEP;
2109         const auto *GO = cast<GEPOperator>(C);
2110         if (GO->isInBounds())
2111           Code = bitc::CST_CODE_CE_INBOUNDS_GEP;
2112         Record.push_back(getTypeID(GO->getSourceElementType()));
2113         for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) {
2114           Record.push_back(
2115               getTypeID(C->getOperand(i)->getType(), C->getOperand(i)));
2116           Record.push_back(VE.getValueID(C->getOperand(i)));
2117         }
2118         break;
2119       }
2120       case Instruction::Select:
2121         Code = bitc::CST_CODE_CE_SELECT;
2122         Record.push_back(VE.getValueID(C->getOperand(0)));
2123         Record.push_back(VE.getValueID(C->getOperand(1)));
2124         Record.push_back(VE.getValueID(C->getOperand(2)));
2125         break;
2126       case Instruction::ExtractElement:
2127         Code = bitc::CST_CODE_CE_EXTRACTELT;
2128         Record.push_back(getTypeID(C->getOperand(0)->getType()));
2129         Record.push_back(VE.getValueID(C->getOperand(0)));
2130         Record.push_back(getTypeID(C->getOperand(1)->getType()));
2131         Record.push_back(VE.getValueID(C->getOperand(1)));
2132         break;
2133       case Instruction::InsertElement:
2134         Code = bitc::CST_CODE_CE_INSERTELT;
2135         Record.push_back(VE.getValueID(C->getOperand(0)));
2136         Record.push_back(VE.getValueID(C->getOperand(1)));
2137         Record.push_back(getTypeID(C->getOperand(2)->getType()));
2138         Record.push_back(VE.getValueID(C->getOperand(2)));
2139         break;
2140       case Instruction::ShuffleVector:
2141         // If the return type and argument types are the same, this is a
2142         // standard shufflevector instruction.  If the types are different,
2143         // then the shuffle is widening or truncating the input vectors, and
2144         // the argument type must also be encoded.
2145         if (C->getType() == C->getOperand(0)->getType()) {
2146           Code = bitc::CST_CODE_CE_SHUFFLEVEC;
2147         } else {
2148           Code = bitc::CST_CODE_CE_SHUFVEC_EX;
2149           Record.push_back(getTypeID(C->getOperand(0)->getType()));
2150         }
2151         Record.push_back(VE.getValueID(C->getOperand(0)));
2152         Record.push_back(VE.getValueID(C->getOperand(1)));
2153         Record.push_back(VE.getValueID(C->getOperand(2)));
2154         break;
2155       case Instruction::ICmp:
2156       case Instruction::FCmp:
2157         Code = bitc::CST_CODE_CE_CMP;
2158         Record.push_back(getTypeID(C->getOperand(0)->getType()));
2159         Record.push_back(VE.getValueID(C->getOperand(0)));
2160         Record.push_back(VE.getValueID(C->getOperand(1)));
2161         Record.push_back(CE->getPredicate());
2162         break;
2163       }
2164     } else if (const BlockAddress *BA = dyn_cast<BlockAddress>(C)) {
2165       Code = bitc::CST_CODE_BLOCKADDRESS;
2166       Record.push_back(getTypeID(BA->getFunction()->getType()));
2167       Record.push_back(VE.getValueID(BA->getFunction()));
2168       Record.push_back(VE.getGlobalBasicBlockID(BA->getBasicBlock()));
2169     } else {
2170 #ifndef NDEBUG
2171       C->dump();
2172 #endif
2173       llvm_unreachable("Unknown constant!");
2174     }
2175     Stream.EmitRecord(Code, Record, AbbrevToUse);
2176     Record.clear();
2177   }
2178 
2179   Stream.ExitBlock();
2180 }
2181 
2182 void DXILBitcodeWriter::writeModuleConstants() {
2183   const ValueEnumerator::ValueList &Vals = VE.getValues();
2184 
2185   // Find the first constant to emit, which is the first non-globalvalue value.
2186   // We know globalvalues have been emitted by WriteModuleInfo.
2187   for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
2188     if (!isa<GlobalValue>(Vals[i].first)) {
2189       writeConstants(i, Vals.size(), true);
2190       return;
2191     }
2192   }
2193 }
2194 
2195 /// pushValueAndType - The file has to encode both the value and type id for
2196 /// many values, because we need to know what type to create for forward
2197 /// references.  However, most operands are not forward references, so this type
2198 /// field is not needed.
2199 ///
2200 /// This function adds V's value ID to Vals.  If the value ID is higher than the
2201 /// instruction ID, then it is a forward reference, and it also includes the
2202 /// type ID.  The value ID that is written is encoded relative to the InstID.
2203 bool DXILBitcodeWriter::pushValueAndType(const Value *V, unsigned InstID,
2204                                          SmallVectorImpl<unsigned> &Vals) {
2205   unsigned ValID = VE.getValueID(V);
2206   // Make encoding relative to the InstID.
2207   Vals.push_back(InstID - ValID);
2208   if (ValID >= InstID) {
2209     Vals.push_back(getTypeID(V->getType(), V));
2210     return true;
2211   }
2212   return false;
2213 }
2214 
2215 /// pushValue - Like pushValueAndType, but where the type of the value is
2216 /// omitted (perhaps it was already encoded in an earlier operand).
2217 void DXILBitcodeWriter::pushValue(const Value *V, unsigned InstID,
2218                                   SmallVectorImpl<unsigned> &Vals) {
2219   unsigned ValID = VE.getValueID(V);
2220   Vals.push_back(InstID - ValID);
2221 }
2222 
2223 void DXILBitcodeWriter::pushValueSigned(const Value *V, unsigned InstID,
2224                                         SmallVectorImpl<uint64_t> &Vals) {
2225   unsigned ValID = VE.getValueID(V);
2226   int64_t diff = ((int32_t)InstID - (int32_t)ValID);
2227   emitSignedInt64(Vals, diff);
2228 }
2229 
2230 /// WriteInstruction - Emit an instruction
2231 void DXILBitcodeWriter::writeInstruction(const Instruction &I, unsigned InstID,
2232                                          SmallVectorImpl<unsigned> &Vals) {
2233   unsigned Code = 0;
2234   unsigned AbbrevToUse = 0;
2235   VE.setInstructionID(&I);
2236   switch (I.getOpcode()) {
2237   default:
2238     if (Instruction::isCast(I.getOpcode())) {
2239       Code = bitc::FUNC_CODE_INST_CAST;
2240       if (!pushValueAndType(I.getOperand(0), InstID, Vals))
2241         AbbrevToUse = (unsigned)FUNCTION_INST_CAST_ABBREV;
2242       Vals.push_back(getTypeID(I.getType(), &I));
2243       Vals.push_back(getEncodedCastOpcode(I.getOpcode()));
2244     } else {
2245       assert(isa<BinaryOperator>(I) && "Unknown instruction!");
2246       Code = bitc::FUNC_CODE_INST_BINOP;
2247       if (!pushValueAndType(I.getOperand(0), InstID, Vals))
2248         AbbrevToUse = (unsigned)FUNCTION_INST_BINOP_ABBREV;
2249       pushValue(I.getOperand(1), InstID, Vals);
2250       Vals.push_back(getEncodedBinaryOpcode(I.getOpcode()));
2251       uint64_t Flags = getOptimizationFlags(&I);
2252       if (Flags != 0) {
2253         if (AbbrevToUse == (unsigned)FUNCTION_INST_BINOP_ABBREV)
2254           AbbrevToUse = (unsigned)FUNCTION_INST_BINOP_FLAGS_ABBREV;
2255         Vals.push_back(Flags);
2256       }
2257     }
2258     break;
2259 
2260   case Instruction::GetElementPtr: {
2261     Code = bitc::FUNC_CODE_INST_GEP;
2262     AbbrevToUse = (unsigned)FUNCTION_INST_GEP_ABBREV;
2263     auto &GEPInst = cast<GetElementPtrInst>(I);
2264     Vals.push_back(GEPInst.isInBounds());
2265     Vals.push_back(getTypeID(GEPInst.getSourceElementType()));
2266     for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
2267       pushValueAndType(I.getOperand(i), InstID, Vals);
2268     break;
2269   }
2270   case Instruction::ExtractValue: {
2271     Code = bitc::FUNC_CODE_INST_EXTRACTVAL;
2272     pushValueAndType(I.getOperand(0), InstID, Vals);
2273     const ExtractValueInst *EVI = cast<ExtractValueInst>(&I);
2274     Vals.append(EVI->idx_begin(), EVI->idx_end());
2275     break;
2276   }
2277   case Instruction::InsertValue: {
2278     Code = bitc::FUNC_CODE_INST_INSERTVAL;
2279     pushValueAndType(I.getOperand(0), InstID, Vals);
2280     pushValueAndType(I.getOperand(1), InstID, Vals);
2281     const InsertValueInst *IVI = cast<InsertValueInst>(&I);
2282     Vals.append(IVI->idx_begin(), IVI->idx_end());
2283     break;
2284   }
2285   case Instruction::Select:
2286     Code = bitc::FUNC_CODE_INST_VSELECT;
2287     pushValueAndType(I.getOperand(1), InstID, Vals);
2288     pushValue(I.getOperand(2), InstID, Vals);
2289     pushValueAndType(I.getOperand(0), InstID, Vals);
2290     break;
2291   case Instruction::ExtractElement:
2292     Code = bitc::FUNC_CODE_INST_EXTRACTELT;
2293     pushValueAndType(I.getOperand(0), InstID, Vals);
2294     pushValueAndType(I.getOperand(1), InstID, Vals);
2295     break;
2296   case Instruction::InsertElement:
2297     Code = bitc::FUNC_CODE_INST_INSERTELT;
2298     pushValueAndType(I.getOperand(0), InstID, Vals);
2299     pushValue(I.getOperand(1), InstID, Vals);
2300     pushValueAndType(I.getOperand(2), InstID, Vals);
2301     break;
2302   case Instruction::ShuffleVector:
2303     Code = bitc::FUNC_CODE_INST_SHUFFLEVEC;
2304     pushValueAndType(I.getOperand(0), InstID, Vals);
2305     pushValue(I.getOperand(1), InstID, Vals);
2306     pushValue(cast<ShuffleVectorInst>(&I)->getShuffleMaskForBitcode(), InstID,
2307               Vals);
2308     break;
2309   case Instruction::ICmp:
2310   case Instruction::FCmp: {
2311     // compare returning Int1Ty or vector of Int1Ty
2312     Code = bitc::FUNC_CODE_INST_CMP2;
2313     pushValueAndType(I.getOperand(0), InstID, Vals);
2314     pushValue(I.getOperand(1), InstID, Vals);
2315     Vals.push_back(cast<CmpInst>(I).getPredicate());
2316     uint64_t Flags = getOptimizationFlags(&I);
2317     if (Flags != 0)
2318       Vals.push_back(Flags);
2319     break;
2320   }
2321 
2322   case Instruction::Ret: {
2323     Code = bitc::FUNC_CODE_INST_RET;
2324     unsigned NumOperands = I.getNumOperands();
2325     if (NumOperands == 0)
2326       AbbrevToUse = (unsigned)FUNCTION_INST_RET_VOID_ABBREV;
2327     else if (NumOperands == 1) {
2328       if (!pushValueAndType(I.getOperand(0), InstID, Vals))
2329         AbbrevToUse = (unsigned)FUNCTION_INST_RET_VAL_ABBREV;
2330     } else {
2331       for (unsigned i = 0, e = NumOperands; i != e; ++i)
2332         pushValueAndType(I.getOperand(i), InstID, Vals);
2333     }
2334   } break;
2335   case Instruction::Br: {
2336     Code = bitc::FUNC_CODE_INST_BR;
2337     const BranchInst &II = cast<BranchInst>(I);
2338     Vals.push_back(VE.getValueID(II.getSuccessor(0)));
2339     if (II.isConditional()) {
2340       Vals.push_back(VE.getValueID(II.getSuccessor(1)));
2341       pushValue(II.getCondition(), InstID, Vals);
2342     }
2343   } break;
2344   case Instruction::Switch: {
2345     Code = bitc::FUNC_CODE_INST_SWITCH;
2346     const SwitchInst &SI = cast<SwitchInst>(I);
2347     Vals.push_back(getTypeID(SI.getCondition()->getType()));
2348     pushValue(SI.getCondition(), InstID, Vals);
2349     Vals.push_back(VE.getValueID(SI.getDefaultDest()));
2350     for (auto Case : SI.cases()) {
2351       Vals.push_back(VE.getValueID(Case.getCaseValue()));
2352       Vals.push_back(VE.getValueID(Case.getCaseSuccessor()));
2353     }
2354   } break;
2355   case Instruction::IndirectBr:
2356     Code = bitc::FUNC_CODE_INST_INDIRECTBR;
2357     Vals.push_back(getTypeID(I.getOperand(0)->getType()));
2358     // Encode the address operand as relative, but not the basic blocks.
2359     pushValue(I.getOperand(0), InstID, Vals);
2360     for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i)
2361       Vals.push_back(VE.getValueID(I.getOperand(i)));
2362     break;
2363 
2364   case Instruction::Invoke: {
2365     const InvokeInst *II = cast<InvokeInst>(&I);
2366     const Value *Callee = II->getCalledOperand();
2367     FunctionType *FTy = II->getFunctionType();
2368     Code = bitc::FUNC_CODE_INST_INVOKE;
2369 
2370     Vals.push_back(VE.getAttributeListID(II->getAttributes()));
2371     Vals.push_back(II->getCallingConv() | 1 << 13);
2372     Vals.push_back(VE.getValueID(II->getNormalDest()));
2373     Vals.push_back(VE.getValueID(II->getUnwindDest()));
2374     Vals.push_back(getTypeID(FTy));
2375     pushValueAndType(Callee, InstID, Vals);
2376 
2377     // Emit value #'s for the fixed parameters.
2378     for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
2379       pushValue(I.getOperand(i), InstID, Vals); // fixed param.
2380 
2381     // Emit type/value pairs for varargs params.
2382     if (FTy->isVarArg()) {
2383       for (unsigned i = FTy->getNumParams(), e = I.getNumOperands() - 3; i != e;
2384            ++i)
2385         pushValueAndType(I.getOperand(i), InstID, Vals); // vararg
2386     }
2387     break;
2388   }
2389   case Instruction::Resume:
2390     Code = bitc::FUNC_CODE_INST_RESUME;
2391     pushValueAndType(I.getOperand(0), InstID, Vals);
2392     break;
2393   case Instruction::Unreachable:
2394     Code = bitc::FUNC_CODE_INST_UNREACHABLE;
2395     AbbrevToUse = (unsigned)FUNCTION_INST_UNREACHABLE_ABBREV;
2396     break;
2397 
2398   case Instruction::PHI: {
2399     const PHINode &PN = cast<PHINode>(I);
2400     Code = bitc::FUNC_CODE_INST_PHI;
2401     // With the newer instruction encoding, forward references could give
2402     // negative valued IDs.  This is most common for PHIs, so we use
2403     // signed VBRs.
2404     SmallVector<uint64_t, 128> Vals64;
2405     Vals64.push_back(getTypeID(PN.getType()));
2406     for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
2407       pushValueSigned(PN.getIncomingValue(i), InstID, Vals64);
2408       Vals64.push_back(VE.getValueID(PN.getIncomingBlock(i)));
2409     }
2410     // Emit a Vals64 vector and exit.
2411     Stream.EmitRecord(Code, Vals64, AbbrevToUse);
2412     Vals64.clear();
2413     return;
2414   }
2415 
2416   case Instruction::LandingPad: {
2417     const LandingPadInst &LP = cast<LandingPadInst>(I);
2418     Code = bitc::FUNC_CODE_INST_LANDINGPAD;
2419     Vals.push_back(getTypeID(LP.getType()));
2420     Vals.push_back(LP.isCleanup());
2421     Vals.push_back(LP.getNumClauses());
2422     for (unsigned I = 0, E = LP.getNumClauses(); I != E; ++I) {
2423       if (LP.isCatch(I))
2424         Vals.push_back(LandingPadInst::Catch);
2425       else
2426         Vals.push_back(LandingPadInst::Filter);
2427       pushValueAndType(LP.getClause(I), InstID, Vals);
2428     }
2429     break;
2430   }
2431 
2432   case Instruction::Alloca: {
2433     Code = bitc::FUNC_CODE_INST_ALLOCA;
2434     const AllocaInst &AI = cast<AllocaInst>(I);
2435     Vals.push_back(getTypeID(AI.getAllocatedType()));
2436     Vals.push_back(getTypeID(I.getOperand(0)->getType()));
2437     Vals.push_back(VE.getValueID(I.getOperand(0))); // size.
2438     unsigned AlignRecord = Log2_32(AI.getAlign().value()) + 1;
2439     assert(AlignRecord < 1 << 5 && "alignment greater than 1 << 64");
2440     AlignRecord |= AI.isUsedWithInAlloca() << 5;
2441     AlignRecord |= 1 << 6;
2442     Vals.push_back(AlignRecord);
2443     break;
2444   }
2445 
2446   case Instruction::Load:
2447     if (cast<LoadInst>(I).isAtomic()) {
2448       Code = bitc::FUNC_CODE_INST_LOADATOMIC;
2449       pushValueAndType(I.getOperand(0), InstID, Vals);
2450     } else {
2451       Code = bitc::FUNC_CODE_INST_LOAD;
2452       if (!pushValueAndType(I.getOperand(0), InstID, Vals)) // ptr
2453         AbbrevToUse = (unsigned)FUNCTION_INST_LOAD_ABBREV;
2454     }
2455     Vals.push_back(getTypeID(I.getType()));
2456     Vals.push_back(Log2(cast<LoadInst>(I).getAlign()) + 1);
2457     Vals.push_back(cast<LoadInst>(I).isVolatile());
2458     if (cast<LoadInst>(I).isAtomic()) {
2459       Vals.push_back(getEncodedOrdering(cast<LoadInst>(I).getOrdering()));
2460       Vals.push_back(getEncodedSyncScopeID(cast<LoadInst>(I).getSyncScopeID()));
2461     }
2462     break;
2463   case Instruction::Store:
2464     if (cast<StoreInst>(I).isAtomic())
2465       Code = bitc::FUNC_CODE_INST_STOREATOMIC;
2466     else
2467       Code = bitc::FUNC_CODE_INST_STORE;
2468     pushValueAndType(I.getOperand(1), InstID, Vals); // ptrty + ptr
2469     pushValueAndType(I.getOperand(0), InstID, Vals); // valty + val
2470     Vals.push_back(Log2(cast<StoreInst>(I).getAlign()) + 1);
2471     Vals.push_back(cast<StoreInst>(I).isVolatile());
2472     if (cast<StoreInst>(I).isAtomic()) {
2473       Vals.push_back(getEncodedOrdering(cast<StoreInst>(I).getOrdering()));
2474       Vals.push_back(
2475           getEncodedSyncScopeID(cast<StoreInst>(I).getSyncScopeID()));
2476     }
2477     break;
2478   case Instruction::AtomicCmpXchg:
2479     Code = bitc::FUNC_CODE_INST_CMPXCHG;
2480     pushValueAndType(I.getOperand(0), InstID, Vals); // ptrty + ptr
2481     pushValueAndType(I.getOperand(1), InstID, Vals); // cmp.
2482     pushValue(I.getOperand(2), InstID, Vals);        // newval.
2483     Vals.push_back(cast<AtomicCmpXchgInst>(I).isVolatile());
2484     Vals.push_back(
2485         getEncodedOrdering(cast<AtomicCmpXchgInst>(I).getSuccessOrdering()));
2486     Vals.push_back(
2487         getEncodedSyncScopeID(cast<AtomicCmpXchgInst>(I).getSyncScopeID()));
2488     Vals.push_back(
2489         getEncodedOrdering(cast<AtomicCmpXchgInst>(I).getFailureOrdering()));
2490     Vals.push_back(cast<AtomicCmpXchgInst>(I).isWeak());
2491     break;
2492   case Instruction::AtomicRMW:
2493     Code = bitc::FUNC_CODE_INST_ATOMICRMW;
2494     pushValueAndType(I.getOperand(0), InstID, Vals); // ptrty + ptr
2495     pushValue(I.getOperand(1), InstID, Vals);        // val.
2496     Vals.push_back(
2497         getEncodedRMWOperation(cast<AtomicRMWInst>(I).getOperation()));
2498     Vals.push_back(cast<AtomicRMWInst>(I).isVolatile());
2499     Vals.push_back(getEncodedOrdering(cast<AtomicRMWInst>(I).getOrdering()));
2500     Vals.push_back(
2501         getEncodedSyncScopeID(cast<AtomicRMWInst>(I).getSyncScopeID()));
2502     break;
2503   case Instruction::Fence:
2504     Code = bitc::FUNC_CODE_INST_FENCE;
2505     Vals.push_back(getEncodedOrdering(cast<FenceInst>(I).getOrdering()));
2506     Vals.push_back(getEncodedSyncScopeID(cast<FenceInst>(I).getSyncScopeID()));
2507     break;
2508   case Instruction::Call: {
2509     const CallInst &CI = cast<CallInst>(I);
2510     FunctionType *FTy = CI.getFunctionType();
2511 
2512     Code = bitc::FUNC_CODE_INST_CALL;
2513 
2514     Vals.push_back(VE.getAttributeListID(CI.getAttributes()));
2515     Vals.push_back((CI.getCallingConv() << 1) | unsigned(CI.isTailCall()) |
2516                    unsigned(CI.isMustTailCall()) << 14 | 1 << 15);
2517     Vals.push_back(getGlobalObjectValueTypeID(FTy, CI.getCalledFunction()));
2518     pushValueAndType(CI.getCalledOperand(), InstID, Vals); // Callee
2519 
2520     // Emit value #'s for the fixed parameters.
2521     for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) {
2522       // Check for labels (can happen with asm labels).
2523       if (FTy->getParamType(i)->isLabelTy())
2524         Vals.push_back(VE.getValueID(CI.getArgOperand(i)));
2525       else
2526         pushValue(CI.getArgOperand(i), InstID, Vals); // fixed param.
2527     }
2528 
2529     // Emit type/value pairs for varargs params.
2530     if (FTy->isVarArg()) {
2531       for (unsigned i = FTy->getNumParams(), e = CI.arg_size(); i != e; ++i)
2532         pushValueAndType(CI.getArgOperand(i), InstID, Vals); // varargs
2533     }
2534     break;
2535   }
2536   case Instruction::VAArg:
2537     Code = bitc::FUNC_CODE_INST_VAARG;
2538     Vals.push_back(getTypeID(I.getOperand(0)->getType())); // valistty
2539     pushValue(I.getOperand(0), InstID, Vals);              // valist.
2540     Vals.push_back(getTypeID(I.getType()));                // restype.
2541     break;
2542   }
2543 
2544   Stream.EmitRecord(Code, Vals, AbbrevToUse);
2545   Vals.clear();
2546 }
2547 
2548 // Emit names for globals/functions etc.
2549 void DXILBitcodeWriter::writeFunctionLevelValueSymbolTable(
2550     const ValueSymbolTable &VST) {
2551   if (VST.empty())
2552     return;
2553   Stream.EnterSubblock(bitc::VALUE_SYMTAB_BLOCK_ID, 4);
2554 
2555   SmallVector<unsigned, 64> NameVals;
2556 
2557   // HLSL Change
2558   // Read the named values from a sorted list instead of the original list
2559   // to ensure the binary is the same no matter what values ever existed.
2560   SmallVector<const ValueName *, 16> SortedTable;
2561 
2562   for (auto &VI : VST) {
2563     SortedTable.push_back(VI.second->getValueName());
2564   }
2565   // The keys are unique, so there shouldn't be stability issues.
2566   llvm::sort(SortedTable, [](const ValueName *A, const ValueName *B) {
2567     return A->first() < B->first();
2568   });
2569 
2570   for (const ValueName *SI : SortedTable) {
2571     auto &Name = *SI;
2572 
2573     // Figure out the encoding to use for the name.
2574     bool is7Bit = true;
2575     bool isChar6 = true;
2576     for (const char *C = Name.getKeyData(), *E = C + Name.getKeyLength();
2577          C != E; ++C) {
2578       if (isChar6)
2579         isChar6 = BitCodeAbbrevOp::isChar6(*C);
2580       if ((unsigned char)*C & 128) {
2581         is7Bit = false;
2582         break; // don't bother scanning the rest.
2583       }
2584     }
2585 
2586     unsigned AbbrevToUse = VST_ENTRY_8_ABBREV;
2587 
2588     // VST_ENTRY:   [valueid, namechar x N]
2589     // VST_BBENTRY: [bbid, namechar x N]
2590     unsigned Code;
2591     if (isa<BasicBlock>(SI->getValue())) {
2592       Code = bitc::VST_CODE_BBENTRY;
2593       if (isChar6)
2594         AbbrevToUse = VST_BBENTRY_6_ABBREV;
2595     } else {
2596       Code = bitc::VST_CODE_ENTRY;
2597       if (isChar6)
2598         AbbrevToUse = VST_ENTRY_6_ABBREV;
2599       else if (is7Bit)
2600         AbbrevToUse = VST_ENTRY_7_ABBREV;
2601     }
2602 
2603     NameVals.push_back(VE.getValueID(SI->getValue()));
2604     for (const char *P = Name.getKeyData(),
2605                     *E = Name.getKeyData() + Name.getKeyLength();
2606          P != E; ++P)
2607       NameVals.push_back((unsigned char)*P);
2608 
2609     // Emit the finished record.
2610     Stream.EmitRecord(Code, NameVals, AbbrevToUse);
2611     NameVals.clear();
2612   }
2613   Stream.ExitBlock();
2614 }
2615 
2616 /// Emit a function body to the module stream.
2617 void DXILBitcodeWriter::writeFunction(const Function &F) {
2618   Stream.EnterSubblock(bitc::FUNCTION_BLOCK_ID, 4);
2619   VE.incorporateFunction(F);
2620 
2621   SmallVector<unsigned, 64> Vals;
2622 
2623   // Emit the number of basic blocks, so the reader can create them ahead of
2624   // time.
2625   Vals.push_back(VE.getBasicBlocks().size());
2626   Stream.EmitRecord(bitc::FUNC_CODE_DECLAREBLOCKS, Vals);
2627   Vals.clear();
2628 
2629   // If there are function-local constants, emit them now.
2630   unsigned CstStart, CstEnd;
2631   VE.getFunctionConstantRange(CstStart, CstEnd);
2632   writeConstants(CstStart, CstEnd, false);
2633 
2634   // If there is function-local metadata, emit it now.
2635   writeFunctionMetadata(F);
2636 
2637   // Keep a running idea of what the instruction ID is.
2638   unsigned InstID = CstEnd;
2639 
2640   bool NeedsMetadataAttachment = F.hasMetadata();
2641 
2642   DILocation *LastDL = nullptr;
2643 
2644   // Finally, emit all the instructions, in order.
2645   for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
2646     for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E;
2647          ++I) {
2648       writeInstruction(*I, InstID, Vals);
2649 
2650       if (!I->getType()->isVoidTy())
2651         ++InstID;
2652 
2653       // If the instruction has metadata, write a metadata attachment later.
2654       NeedsMetadataAttachment |= I->hasMetadataOtherThanDebugLoc();
2655 
2656       // If the instruction has a debug location, emit it.
2657       DILocation *DL = I->getDebugLoc();
2658       if (!DL)
2659         continue;
2660 
2661       if (DL == LastDL) {
2662         // Just repeat the same debug loc as last time.
2663         Stream.EmitRecord(bitc::FUNC_CODE_DEBUG_LOC_AGAIN, Vals);
2664         continue;
2665       }
2666 
2667       Vals.push_back(DL->getLine());
2668       Vals.push_back(DL->getColumn());
2669       Vals.push_back(VE.getMetadataOrNullID(DL->getScope()));
2670       Vals.push_back(VE.getMetadataOrNullID(DL->getInlinedAt()));
2671       Stream.EmitRecord(bitc::FUNC_CODE_DEBUG_LOC, Vals);
2672       Vals.clear();
2673 
2674       LastDL = DL;
2675     }
2676 
2677   // Emit names for all the instructions etc.
2678   if (auto *Symtab = F.getValueSymbolTable())
2679     writeFunctionLevelValueSymbolTable(*Symtab);
2680 
2681   if (NeedsMetadataAttachment)
2682     writeFunctionMetadataAttachment(F);
2683 
2684   VE.purgeFunction();
2685   Stream.ExitBlock();
2686 }
2687 
2688 // Emit blockinfo, which defines the standard abbreviations etc.
2689 void DXILBitcodeWriter::writeBlockInfo() {
2690   // We only want to emit block info records for blocks that have multiple
2691   // instances: CONSTANTS_BLOCK, FUNCTION_BLOCK and VALUE_SYMTAB_BLOCK.
2692   // Other blocks can define their abbrevs inline.
2693   Stream.EnterBlockInfoBlock();
2694 
2695   { // 8-bit fixed-width VST_ENTRY/VST_BBENTRY strings.
2696     auto Abbv = std::make_shared<BitCodeAbbrev>();
2697     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3));
2698     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
2699     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2700     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
2701     if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
2702                                    std::move(Abbv)) != VST_ENTRY_8_ABBREV)
2703       assert(false && "Unexpected abbrev ordering!");
2704   }
2705 
2706   { // 7-bit fixed width VST_ENTRY strings.
2707     auto Abbv = std::make_shared<BitCodeAbbrev>();
2708     Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
2709     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
2710     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2711     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
2712     if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
2713                                    std::move(Abbv)) != VST_ENTRY_7_ABBREV)
2714       assert(false && "Unexpected abbrev ordering!");
2715   }
2716   { // 6-bit char6 VST_ENTRY strings.
2717     auto Abbv = std::make_shared<BitCodeAbbrev>();
2718     Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
2719     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
2720     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2721     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
2722     if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
2723                                    std::move(Abbv)) != VST_ENTRY_6_ABBREV)
2724       assert(false && "Unexpected abbrev ordering!");
2725   }
2726   { // 6-bit char6 VST_BBENTRY strings.
2727     auto Abbv = std::make_shared<BitCodeAbbrev>();
2728     Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_BBENTRY));
2729     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
2730     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2731     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
2732     if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
2733                                    std::move(Abbv)) != VST_BBENTRY_6_ABBREV)
2734       assert(false && "Unexpected abbrev ordering!");
2735   }
2736 
2737   { // SETTYPE abbrev for CONSTANTS_BLOCK.
2738     auto Abbv = std::make_shared<BitCodeAbbrev>();
2739     Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_SETTYPE));
2740     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
2741                               VE.computeBitsRequiredForTypeIndicies()));
2742     if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, std::move(Abbv)) !=
2743         CONSTANTS_SETTYPE_ABBREV)
2744       assert(false && "Unexpected abbrev ordering!");
2745   }
2746 
2747   { // INTEGER abbrev for CONSTANTS_BLOCK.
2748     auto Abbv = std::make_shared<BitCodeAbbrev>();
2749     Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_INTEGER));
2750     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
2751     if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, std::move(Abbv)) !=
2752         CONSTANTS_INTEGER_ABBREV)
2753       assert(false && "Unexpected abbrev ordering!");
2754   }
2755 
2756   { // CE_CAST abbrev for CONSTANTS_BLOCK.
2757     auto Abbv = std::make_shared<BitCodeAbbrev>();
2758     Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CE_CAST));
2759     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // cast opc
2760     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,      // typeid
2761                               VE.computeBitsRequiredForTypeIndicies()));
2762     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id
2763 
2764     if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, std::move(Abbv)) !=
2765         CONSTANTS_CE_CAST_Abbrev)
2766       assert(false && "Unexpected abbrev ordering!");
2767   }
2768   { // NULL abbrev for CONSTANTS_BLOCK.
2769     auto Abbv = std::make_shared<BitCodeAbbrev>();
2770     Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_NULL));
2771     if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, std::move(Abbv)) !=
2772         CONSTANTS_NULL_Abbrev)
2773       assert(false && "Unexpected abbrev ordering!");
2774   }
2775 
2776   // FIXME: This should only use space for first class types!
2777 
2778   { // INST_LOAD abbrev for FUNCTION_BLOCK.
2779     auto Abbv = std::make_shared<BitCodeAbbrev>();
2780     Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_LOAD));
2781     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Ptr
2782     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,    // dest ty
2783                               VE.computeBitsRequiredForTypeIndicies()));
2784     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4));   // Align
2785     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // volatile
2786     if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, std::move(Abbv)) !=
2787         (unsigned)FUNCTION_INST_LOAD_ABBREV)
2788       assert(false && "Unexpected abbrev ordering!");
2789   }
2790   { // INST_BINOP abbrev for FUNCTION_BLOCK.
2791     auto Abbv = std::make_shared<BitCodeAbbrev>();
2792     Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_BINOP));
2793     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // LHS
2794     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // RHS
2795     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
2796     if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, std::move(Abbv)) !=
2797         (unsigned)FUNCTION_INST_BINOP_ABBREV)
2798       assert(false && "Unexpected abbrev ordering!");
2799   }
2800   { // INST_BINOP_FLAGS abbrev for FUNCTION_BLOCK.
2801     auto Abbv = std::make_shared<BitCodeAbbrev>();
2802     Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_BINOP));
2803     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // LHS
2804     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // RHS
2805     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
2806     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7)); // flags
2807     if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, std::move(Abbv)) !=
2808         (unsigned)FUNCTION_INST_BINOP_FLAGS_ABBREV)
2809       assert(false && "Unexpected abbrev ordering!");
2810   }
2811   { // INST_CAST abbrev for FUNCTION_BLOCK.
2812     auto Abbv = std::make_shared<BitCodeAbbrev>();
2813     Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_CAST));
2814     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // OpVal
2815     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,    // dest ty
2816                               VE.computeBitsRequiredForTypeIndicies()));
2817     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
2818     if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, std::move(Abbv)) !=
2819         (unsigned)FUNCTION_INST_CAST_ABBREV)
2820       assert(false && "Unexpected abbrev ordering!");
2821   }
2822 
2823   { // INST_RET abbrev for FUNCTION_BLOCK.
2824     auto Abbv = std::make_shared<BitCodeAbbrev>();
2825     Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET));
2826     if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, std::move(Abbv)) !=
2827         (unsigned)FUNCTION_INST_RET_VOID_ABBREV)
2828       assert(false && "Unexpected abbrev ordering!");
2829   }
2830   { // INST_RET abbrev for FUNCTION_BLOCK.
2831     auto Abbv = std::make_shared<BitCodeAbbrev>();
2832     Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET));
2833     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ValID
2834     if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, std::move(Abbv)) !=
2835         (unsigned)FUNCTION_INST_RET_VAL_ABBREV)
2836       assert(false && "Unexpected abbrev ordering!");
2837   }
2838   { // INST_UNREACHABLE abbrev for FUNCTION_BLOCK.
2839     auto Abbv = std::make_shared<BitCodeAbbrev>();
2840     Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_UNREACHABLE));
2841     if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, std::move(Abbv)) !=
2842         (unsigned)FUNCTION_INST_UNREACHABLE_ABBREV)
2843       assert(false && "Unexpected abbrev ordering!");
2844   }
2845   {
2846     auto Abbv = std::make_shared<BitCodeAbbrev>();
2847     Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_GEP));
2848     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
2849     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // dest ty
2850                               Log2_32_Ceil(VE.getTypes().size() + 1)));
2851     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2852     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
2853     if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, std::move(Abbv)) !=
2854         (unsigned)FUNCTION_INST_GEP_ABBREV)
2855       assert(false && "Unexpected abbrev ordering!");
2856   }
2857 
2858   Stream.ExitBlock();
2859 }
2860 
2861 void DXILBitcodeWriter::writeModuleVersion() {
2862   // VERSION: [version#]
2863   Stream.EmitRecord(bitc::MODULE_CODE_VERSION, ArrayRef<unsigned>{1});
2864 }
2865 
2866 /// WriteModule - Emit the specified module to the bitstream.
2867 void DXILBitcodeWriter::write() {
2868   // The identification block is new since llvm-3.7, but the old bitcode reader
2869   // will skip it.
2870   // writeIdentificationBlock(Stream);
2871 
2872   Stream.EnterSubblock(bitc::MODULE_BLOCK_ID, 3);
2873 
2874   // It is redundant to fully-specify this here, but nice to make it explicit
2875   // so that it is clear the DXIL module version is different.
2876   DXILBitcodeWriter::writeModuleVersion();
2877 
2878   // Emit blockinfo, which defines the standard abbreviations etc.
2879   writeBlockInfo();
2880 
2881   // Emit information about attribute groups.
2882   writeAttributeGroupTable();
2883 
2884   // Emit information about parameter attributes.
2885   writeAttributeTable();
2886 
2887   // Emit information describing all of the types in the module.
2888   writeTypeTable();
2889 
2890   writeComdats();
2891 
2892   // Emit top-level description of module, including target triple, inline asm,
2893   // descriptors for global variables, and function prototype info.
2894   writeModuleInfo();
2895 
2896   // Emit constants.
2897   writeModuleConstants();
2898 
2899   // Emit metadata.
2900   writeModuleMetadataKinds();
2901 
2902   // Emit metadata.
2903   writeModuleMetadata();
2904 
2905   // Emit names for globals/functions etc.
2906   // DXIL uses the same format for module-level value symbol table as for the
2907   // function level table.
2908   writeFunctionLevelValueSymbolTable(M.getValueSymbolTable());
2909 
2910   // Emit function bodies.
2911   for (const Function &F : M)
2912     if (!F.isDeclaration())
2913       writeFunction(F);
2914 
2915   Stream.ExitBlock();
2916 }
2917