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