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