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