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 unsigned createMetadataStringsAbbrev();
325 void writeMetadataStrings(ArrayRef<const Metadata *> Strings,
326 SmallVectorImpl<uint64_t> &Record);
327 void writeMetadataRecords(ArrayRef<const Metadata *> MDs,
328 SmallVectorImpl<uint64_t> &Record,
329 std::vector<unsigned> *MDAbbrevs = nullptr,
330 std::vector<uint64_t> *IndexPos = nullptr);
331 void writeModuleMetadata();
332 void writeFunctionMetadata(const Function &F);
333 void writeFunctionMetadataAttachment(const Function &F);
334 void writeModuleMetadataKinds();
335 void writeConstants(unsigned FirstVal, unsigned LastVal, bool isGlobal);
336 void writeModuleConstants();
337 bool pushValueAndType(const Value *V, unsigned InstID,
338 SmallVectorImpl<unsigned> &Vals);
339 void pushValue(const Value *V, unsigned InstID,
340 SmallVectorImpl<unsigned> &Vals);
341 void pushValueSigned(const Value *V, unsigned InstID,
342 SmallVectorImpl<uint64_t> &Vals);
343 void writeInstruction(const Instruction &I, unsigned InstID,
344 SmallVectorImpl<unsigned> &Vals);
345 void writeFunctionLevelValueSymbolTable(const ValueSymbolTable &VST);
346 void writeFunction(const Function &F);
347 void writeBlockInfo();
348
349 unsigned getEncodedSyncScopeID(SyncScope::ID SSID) { return unsigned(SSID); }
350
351 unsigned getEncodedAlign(MaybeAlign Alignment) { return encode(Alignment); }
352
353 unsigned getTypeID(Type *T, const Value *V = nullptr);
354 /// getGlobalObjectValueTypeID - returns the element type for a GlobalObject
355 ///
356 /// GlobalObject types are saved by PointerTypeAnalysis as pointers to the
357 /// GlobalObject, but in the bitcode writer we need the pointer element type.
358 unsigned getGlobalObjectValueTypeID(Type *T, const GlobalObject *G);
359};
360
361} // namespace dxil
362} // namespace llvm
363
364using namespace llvm;
365using namespace llvm::dxil;
366
367////////////////////////////////////////////////////////////////////////////////
368/// Begin dxil::BitcodeWriter Implementation
369////////////////////////////////////////////////////////////////////////////////
370
372 : Buffer(Buffer), Stream(new BitstreamWriter(Buffer)) {
373 // Emit the file header.
374 Stream->Emit((unsigned)'B', 8);
375 Stream->Emit((unsigned)'C', 8);
376 Stream->Emit(0x0, 4);
377 Stream->Emit(0xC, 4);
378 Stream->Emit(0xE, 4);
379 Stream->Emit(0xD, 4);
380}
381
383
384/// Write the specified module to the specified output stream.
387 Buffer.reserve(256 * 1024);
388
389 // If this is darwin or another generic macho target, reserve space for the
390 // header.
391 Triple TT(M.getTargetTriple());
392 if (TT.isOSDarwin() || TT.isOSBinFormatMachO())
393 Buffer.insert(Buffer.begin(), BWH_HeaderSize, 0);
394
396 BitcodeWriter Writer(Buffer);
397 Writer.writeModule(M, DebugInfo);
398
399 // Write the generated bitstream to "Out".
400 if (!Buffer.empty())
401 Out.write((char *)&Buffer.front(), Buffer.size());
402}
403
404void BitcodeWriter::writeBlob(unsigned Block, unsigned Record, StringRef Blob) {
405 Stream->EnterSubblock(Block, 3);
406
407 auto Abbv = std::make_shared<BitCodeAbbrev>();
408 Abbv->Add(BitCodeAbbrevOp(Record));
410 auto AbbrevNo = Stream->EmitAbbrev(std::move(Abbv));
411
412 Stream->EmitRecordWithBlob(AbbrevNo, ArrayRef<uint64_t>{Record}, Blob);
413
414 Stream->ExitBlock();
415}
416
419
420 // The Mods vector is used by irsymtab::build, which requires non-const
421 // Modules in case it needs to materialize metadata. But the bitcode writer
422 // requires that the module is materialized, so we can cast to non-const here,
423 // after checking that it is in fact materialized.
424 assert(M.isMaterialized());
425 Mods.push_back(const_cast<Module *>(&M));
426
427 DXILBitcodeWriter ModuleWriter(M, Buffer, StrtabBuilder, *Stream, DebugInfo);
428 ModuleWriter.write();
429}
430
431////////////////////////////////////////////////////////////////////////////////
432/// Begin dxil::BitcodeWriterBase Implementation
433////////////////////////////////////////////////////////////////////////////////
434
436 switch (Opcode) {
437 default:
438 llvm_unreachable("Unknown cast instruction!");
439 case Instruction::Trunc:
440 return bitc::CAST_TRUNC;
441 case Instruction::ZExt:
442 return bitc::CAST_ZEXT;
443 case Instruction::SExt:
444 return bitc::CAST_SEXT;
445 case Instruction::FPToUI:
446 return bitc::CAST_FPTOUI;
447 case Instruction::FPToSI:
448 return bitc::CAST_FPTOSI;
449 case Instruction::UIToFP:
450 return bitc::CAST_UITOFP;
451 case Instruction::SIToFP:
452 return bitc::CAST_SITOFP;
453 case Instruction::FPTrunc:
454 return bitc::CAST_FPTRUNC;
455 case Instruction::FPExt:
456 return bitc::CAST_FPEXT;
457 case Instruction::PtrToInt:
458 return bitc::CAST_PTRTOINT;
459 case Instruction::IntToPtr:
460 return bitc::CAST_INTTOPTR;
461 case Instruction::BitCast:
462 return bitc::CAST_BITCAST;
463 case Instruction::AddrSpaceCast:
465 }
466}
467
469 switch (Opcode) {
470 default:
471 llvm_unreachable("Unknown binary instruction!");
472 case Instruction::FNeg:
473 return bitc::UNOP_FNEG;
474 }
475}
476
478 switch (Opcode) {
479 default:
480 llvm_unreachable("Unknown binary instruction!");
481 case Instruction::Add:
482 case Instruction::FAdd:
483 return bitc::BINOP_ADD;
484 case Instruction::Sub:
485 case Instruction::FSub:
486 return bitc::BINOP_SUB;
487 case Instruction::Mul:
488 case Instruction::FMul:
489 return bitc::BINOP_MUL;
490 case Instruction::UDiv:
491 return bitc::BINOP_UDIV;
492 case Instruction::FDiv:
493 case Instruction::SDiv:
494 return bitc::BINOP_SDIV;
495 case Instruction::URem:
496 return bitc::BINOP_UREM;
497 case Instruction::FRem:
498 case Instruction::SRem:
499 return bitc::BINOP_SREM;
500 case Instruction::Shl:
501 return bitc::BINOP_SHL;
502 case Instruction::LShr:
503 return bitc::BINOP_LSHR;
504 case Instruction::AShr:
505 return bitc::BINOP_ASHR;
506 case Instruction::And:
507 return bitc::BINOP_AND;
508 case Instruction::Or:
509 return bitc::BINOP_OR;
510 case Instruction::Xor:
511 return bitc::BINOP_XOR;
512 }
513}
514
515unsigned DXILBitcodeWriter::getTypeID(Type *T, const Value *V) {
516 // For Constant, always check PointerMap to make sure OpaquePointer in
517 // things like constant struct/array works.
518 if (!T->isPointerTy() && !isa_and_nonnull<Constant>(V))
519 return VE.getTypeID(T);
520 auto It = PointerMap.find(V);
521 if (It != PointerMap.end())
522 return VE.getTypeID(It->second);
523 // FIXME: support ConstantPointerNull and UndefValue which could map to more
524 // than one TypedPointerType.
525 // See https://github.com/llvm/llvm-project/issues/57942.
526 if (T->isPointerTy())
527 return VE.getTypeID(I8PtrTy);
528 return VE.getTypeID(T);
529}
530
531unsigned DXILBitcodeWriter::getGlobalObjectValueTypeID(Type *T,
532 const GlobalObject *G) {
533 auto It = PointerMap.find(G);
534 if (It != PointerMap.end()) {
535 TypedPointerType *PtrTy = cast<TypedPointerType>(It->second);
536 return VE.getTypeID(PtrTy->getElementType());
537 }
538 return VE.getTypeID(T);
539}
540
542 switch (Op) {
543 default:
544 llvm_unreachable("Unknown RMW operation!");
546 return bitc::RMW_XCHG;
548 return bitc::RMW_ADD;
550 return bitc::RMW_SUB;
552 return bitc::RMW_AND;
554 return bitc::RMW_NAND;
556 return bitc::RMW_OR;
558 return bitc::RMW_XOR;
560 return bitc::RMW_MAX;
562 return bitc::RMW_MIN;
564 return bitc::RMW_UMAX;
566 return bitc::RMW_UMIN;
568 return bitc::RMW_FADD;
570 return bitc::RMW_FSUB;
572 return bitc::RMW_FMAX;
574 return bitc::RMW_FMIN;
575 }
576}
577
597
599 unsigned Code, StringRef Str,
600 unsigned AbbrevToUse) {
602
603 // Code: [strchar x N]
604 for (char C : Str) {
605 if (AbbrevToUse && !BitCodeAbbrevOp::isChar6(C))
606 AbbrevToUse = 0;
607 Vals.push_back(C);
608 }
609
610 // Emit the finished record.
611 Stream.EmitRecord(Code, Vals, AbbrevToUse);
612}
613
615 switch (Kind) {
616 case Attribute::Alignment:
618 case Attribute::AlwaysInline:
620 case Attribute::Builtin:
622 case Attribute::ByVal:
624 case Attribute::Convergent:
626 case Attribute::InAlloca:
628 case Attribute::Cold:
630 case Attribute::InlineHint:
632 case Attribute::InReg:
634 case Attribute::JumpTable:
636 case Attribute::MinSize:
638 case Attribute::Naked:
640 case Attribute::Nest:
642 case Attribute::NoAlias:
644 case Attribute::NoBuiltin:
646 case Attribute::NoDuplicate:
648 case Attribute::NoImplicitFloat:
650 case Attribute::NoInline:
652 case Attribute::NonLazyBind:
654 case Attribute::NonNull:
656 case Attribute::Dereferenceable:
658 case Attribute::DereferenceableOrNull:
660 case Attribute::NoRedZone:
662 case Attribute::NoReturn:
664 case Attribute::NoUnwind:
666 case Attribute::OptimizeForSize:
668 case Attribute::OptimizeNone:
670 case Attribute::ReadNone:
672 case Attribute::ReadOnly:
674 case Attribute::Returned:
676 case Attribute::ReturnsTwice:
678 case Attribute::SExt:
680 case Attribute::StackAlignment:
682 case Attribute::StackProtect:
684 case Attribute::StackProtectReq:
686 case Attribute::StackProtectStrong:
688 case Attribute::SafeStack:
690 case Attribute::StructRet:
692 case Attribute::SanitizeAddress:
694 case Attribute::SanitizeThread:
696 case Attribute::SanitizeMemory:
698 case Attribute::UWTable:
700 case Attribute::ZExt:
703 llvm_unreachable("Can not encode end-attribute kinds marker.");
704 case Attribute::None:
705 llvm_unreachable("Can not encode none-attribute.");
708 llvm_unreachable("Trying to encode EmptyKey/TombstoneKey");
709 default:
710 llvm_unreachable("Trying to encode attribute not supported by DXIL. These "
711 "should be stripped in DXILPrepare");
712 }
713
714 llvm_unreachable("Trying to encode unknown attribute");
715}
716
718 uint64_t V) {
719 if ((int64_t)V >= 0)
720 Vals.push_back(V << 1);
721 else
722 Vals.push_back((-V << 1) | 1);
723}
724
726 const APInt &A) {
727 // We have an arbitrary precision integer value to write whose
728 // bit width is > 64. However, in canonical unsigned integer
729 // format it is likely that the high bits are going to be zero.
730 // So, we only write the number of active words.
731 unsigned NumWords = A.getActiveWords();
732 const uint64_t *RawData = A.getRawData();
733 for (unsigned i = 0; i < NumWords; i++)
734 emitSignedInt64(Vals, RawData[i]);
735}
736
738 uint64_t Flags = 0;
739
740 if (const auto *OBO = dyn_cast<OverflowingBinaryOperator>(V)) {
741 if (OBO->hasNoSignedWrap())
742 Flags |= 1 << bitc::OBO_NO_SIGNED_WRAP;
743 if (OBO->hasNoUnsignedWrap())
744 Flags |= 1 << bitc::OBO_NO_UNSIGNED_WRAP;
745 } else if (const auto *PEO = dyn_cast<PossiblyExactOperator>(V)) {
746 if (PEO->isExact())
747 Flags |= 1 << bitc::PEO_EXACT;
748 } else if (const auto *FPMO = dyn_cast<FPMathOperator>(V)) {
749 if (FPMO->hasAllowReassoc() || FPMO->hasAllowContract())
750 Flags |= bitc::UnsafeAlgebra;
751 if (FPMO->hasNoNaNs())
752 Flags |= bitc::NoNaNs;
753 if (FPMO->hasNoInfs())
754 Flags |= bitc::NoInfs;
755 if (FPMO->hasNoSignedZeros())
756 Flags |= bitc::NoSignedZeros;
757 if (FPMO->hasAllowReciprocal())
758 Flags |= bitc::AllowReciprocal;
759 }
760
761 return Flags;
762}
763
764unsigned
766 switch (Linkage) {
768 return 0;
770 return 16;
772 return 2;
774 return 3;
776 return 18;
778 return 7;
780 return 8;
782 return 9;
784 return 17;
786 return 19;
788 return 12;
789 }
790 llvm_unreachable("Invalid linkage");
791}
792
796
798 switch (GV.getVisibility()) {
800 return 0;
802 return 1;
804 return 2;
805 }
806 llvm_unreachable("Invalid visibility");
807}
808
810 switch (GV.getDLLStorageClass()) {
812 return 0;
814 return 1;
816 return 2;
817 }
818 llvm_unreachable("Invalid DLL storage class");
819}
820
822 switch (GV.getThreadLocalMode()) {
824 return 0;
826 return 1;
828 return 2;
830 return 3;
832 return 4;
833 }
834 llvm_unreachable("Invalid TLS model");
835}
836
852
853////////////////////////////////////////////////////////////////////////////////
854/// Begin DXILBitcodeWriter Implementation
855////////////////////////////////////////////////////////////////////////////////
856
857void DXILBitcodeWriter::writeAttributeGroupTable() {
858 const std::vector<ValueEnumerator::IndexAndAttrSet> &AttrGrps =
859 VE.getAttributeGroups();
860 if (AttrGrps.empty())
861 return;
862
864
866 for (ValueEnumerator::IndexAndAttrSet Pair : AttrGrps) {
867 unsigned AttrListIndex = Pair.first;
868 AttributeSet AS = Pair.second;
869 Record.push_back(VE.getAttributeGroupID(Pair));
870 Record.push_back(AttrListIndex);
871
872 for (Attribute Attr : AS) {
873 if (Attr.isEnumAttribute()) {
874 uint64_t Val = getAttrKindEncoding(Attr.getKindAsEnum());
876 "DXIL does not support attributes above ATTR_KIND_ARGMEMONLY");
877 Record.push_back(0);
878 Record.push_back(Val);
879 } else if (Attr.isIntAttribute()) {
880 if (Attr.getKindAsEnum() == Attribute::AttrKind::Memory) {
881 MemoryEffects ME = Attr.getMemoryEffects();
882 if (ME.doesNotAccessMemory()) {
883 Record.push_back(0);
885 } else {
886 if (ME.onlyReadsMemory()) {
887 Record.push_back(0);
889 }
890 if (ME.onlyAccessesArgPointees()) {
891 Record.push_back(0);
893 }
894 }
895 } else {
896 uint64_t Val = getAttrKindEncoding(Attr.getKindAsEnum());
898 "DXIL does not support attributes above ATTR_KIND_ARGMEMONLY");
899 Record.push_back(1);
900 Record.push_back(Val);
901 Record.push_back(Attr.getValueAsInt());
902 }
903 } else {
904 StringRef Kind = Attr.getKindAsString();
905 StringRef Val = Attr.getValueAsString();
906
907 Record.push_back(Val.empty() ? 3 : 4);
908 Record.append(Kind.begin(), Kind.end());
909 Record.push_back(0);
910 if (!Val.empty()) {
911 Record.append(Val.begin(), Val.end());
912 Record.push_back(0);
913 }
914 }
915 }
916
917 Stream.EmitRecord(bitc::PARAMATTR_GRP_CODE_ENTRY, Record);
918 Record.clear();
919 }
920
921 Stream.ExitBlock();
922}
923
924void DXILBitcodeWriter::writeAttributeTable() {
925 const std::vector<AttributeList> &Attrs = VE.getAttributeLists();
926 if (Attrs.empty())
927 return;
928
929 Stream.EnterSubblock(bitc::PARAMATTR_BLOCK_ID, 3);
930
931 SmallVector<uint64_t, 64> Record;
932 for (AttributeList AL : Attrs) {
933 for (unsigned i : AL.indexes()) {
934 AttributeSet AS = AL.getAttributes(i);
935 if (AS.hasAttributes())
936 Record.push_back(VE.getAttributeGroupID({i, AS}));
937 }
938
939 Stream.EmitRecord(bitc::PARAMATTR_CODE_ENTRY, Record);
940 Record.clear();
941 }
942
943 Stream.ExitBlock();
944}
945
946/// WriteTypeTable - Write out the type table for a module.
947void DXILBitcodeWriter::writeTypeTable() {
948 const ValueEnumerator::TypeList &TypeList = VE.getTypes();
949
950 Stream.EnterSubblock(bitc::TYPE_BLOCK_ID_NEW, 4 /*count from # abbrevs */);
951 SmallVector<uint64_t, 64> TypeVals;
952
953 uint64_t NumBits = VE.computeBitsRequiredForTypeIndices();
954
955 // Abbrev for TYPE_CODE_POINTER.
956 auto Abbv = std::make_shared<BitCodeAbbrev>();
957 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_POINTER));
958 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
959 Abbv->Add(BitCodeAbbrevOp(0)); // Addrspace = 0
960 unsigned PtrAbbrev = Stream.EmitAbbrev(std::move(Abbv));
961
962 // Abbrev for TYPE_CODE_FUNCTION.
963 Abbv = std::make_shared<BitCodeAbbrev>();
964 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_FUNCTION));
965 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isvararg
966 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
967 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
968 unsigned FunctionAbbrev = Stream.EmitAbbrev(std::move(Abbv));
969
970 // Abbrev for TYPE_CODE_STRUCT_ANON.
971 Abbv = std::make_shared<BitCodeAbbrev>();
972 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_ANON));
973 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ispacked
974 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
975 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
976 unsigned StructAnonAbbrev = Stream.EmitAbbrev(std::move(Abbv));
977
978 // Abbrev for TYPE_CODE_STRUCT_NAME.
979 Abbv = std::make_shared<BitCodeAbbrev>();
980 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_NAME));
981 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
982 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
983 unsigned StructNameAbbrev = Stream.EmitAbbrev(std::move(Abbv));
984
985 // Abbrev for TYPE_CODE_STRUCT_NAMED.
986 Abbv = std::make_shared<BitCodeAbbrev>();
987 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_NAMED));
988 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ispacked
989 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
990 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
991 unsigned StructNamedAbbrev = Stream.EmitAbbrev(std::move(Abbv));
992
993 // Abbrev for TYPE_CODE_ARRAY.
994 Abbv = std::make_shared<BitCodeAbbrev>();
995 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_ARRAY));
996 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // size
997 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
998 unsigned ArrayAbbrev = Stream.EmitAbbrev(std::move(Abbv));
999
1000 // Emit an entry count so the reader can reserve space.
1001 TypeVals.push_back(TypeList.size());
1002 Stream.EmitRecord(bitc::TYPE_CODE_NUMENTRY, TypeVals);
1003 TypeVals.clear();
1004
1005 // Loop over all of the types, emitting each in turn.
1006 for (Type *T : TypeList) {
1007 int AbbrevToUse = 0;
1008 unsigned Code = 0;
1009
1010 switch (T->getTypeID()) {
1011 case Type::BFloatTyID:
1012 case Type::X86_AMXTyID:
1013 case Type::TokenTyID:
1015 llvm_unreachable("These should never be used!!!");
1016 break;
1017 case Type::VoidTyID:
1019 break;
1020 case Type::HalfTyID:
1022 break;
1023 case Type::FloatTyID:
1025 break;
1026 case Type::DoubleTyID:
1028 break;
1029 case Type::X86_FP80TyID:
1031 break;
1032 case Type::FP128TyID:
1034 break;
1037 break;
1038 case Type::LabelTyID:
1040 break;
1041 case Type::MetadataTyID:
1043 break;
1044 case Type::ByteTyID:
1045 // BYTE: [width]
1046 // Note: we downgrade by converting to the equivalent integer.
1048 TypeVals.push_back(T->getByteBitWidth());
1049 break;
1050 case Type::IntegerTyID:
1051 // INTEGER: [width]
1054 break;
1056 TypedPointerType *PTy = cast<TypedPointerType>(T);
1057 // POINTER: [pointee type, address space]
1059 TypeVals.push_back(getTypeID(PTy->getElementType()));
1060 unsigned AddressSpace = PTy->getAddressSpace();
1061 TypeVals.push_back(AddressSpace);
1062 if (AddressSpace == 0)
1063 AbbrevToUse = PtrAbbrev;
1064 break;
1065 }
1066 case Type::PointerTyID: {
1067 // POINTER: [pointee type, address space]
1068 // Emitting an empty struct type for the pointer's type allows this to be
1069 // order-independent. Non-struct types must be emitted in bitcode before
1070 // they can be referenced.
1071 TypeVals.push_back(false);
1074 "dxilOpaquePtrReservedName", StructNameAbbrev);
1075 break;
1076 }
1077 case Type::FunctionTyID: {
1078 FunctionType *FT = cast<FunctionType>(T);
1079 // FUNCTION: [isvararg, retty, paramty x N]
1081 TypeVals.push_back(FT->isVarArg());
1082 TypeVals.push_back(getTypeID(FT->getReturnType()));
1083 for (Type *PTy : FT->params())
1084 TypeVals.push_back(getTypeID(PTy));
1085 AbbrevToUse = FunctionAbbrev;
1086 break;
1087 }
1088 case Type::StructTyID: {
1089 StructType *ST = cast<StructType>(T);
1090 // STRUCT: [ispacked, eltty x N]
1091 TypeVals.push_back(ST->isPacked());
1092 // Output all of the element types.
1093 for (Type *ElTy : ST->elements())
1094 TypeVals.push_back(getTypeID(ElTy));
1095
1096 if (ST->isLiteral()) {
1098 AbbrevToUse = StructAnonAbbrev;
1099 } else {
1100 if (ST->isOpaque()) {
1102 } else {
1104 AbbrevToUse = StructNamedAbbrev;
1105 }
1106
1107 // Emit the name if it is present.
1108 if (!ST->getName().empty())
1110 StructNameAbbrev);
1111 }
1112 break;
1113 }
1114 case Type::ArrayTyID: {
1116 // ARRAY: [numelts, eltty]
1118 TypeVals.push_back(AT->getNumElements());
1119 TypeVals.push_back(getTypeID(AT->getElementType()));
1120 AbbrevToUse = ArrayAbbrev;
1121 break;
1122 }
1126 // VECTOR [numelts, eltty]
1128 TypeVals.push_back(VT->getElementCount().getKnownMinValue());
1129 TypeVals.push_back(getTypeID(VT->getElementType()));
1130 break;
1131 }
1132 }
1133
1134 // Emit the finished record.
1135 Stream.EmitRecord(Code, TypeVals, AbbrevToUse);
1136 TypeVals.clear();
1137 }
1138
1139 Stream.ExitBlock();
1140}
1141
1142void DXILBitcodeWriter::writeComdats() {
1144 for (const Comdat *C : VE.getComdats()) {
1145 // COMDAT: [selection_kind, name]
1147 size_t Size = C->getName().size();
1149 Vals.push_back(Size);
1150 for (char Chr : C->getName())
1151 Vals.push_back((unsigned char)Chr);
1152 Stream.EmitRecord(bitc::MODULE_CODE_COMDAT, Vals, /*AbbrevToUse=*/0);
1153 Vals.clear();
1154 }
1155}
1156
1157void DXILBitcodeWriter::writeValueSymbolTableForwardDecl() {}
1158
1159/// Emit top-level description of module, including target triple, inline asm,
1160/// descriptors for global variables, and function prototype info.
1161/// Returns the bit offset to backpatch with the location of the real VST.
1162void DXILBitcodeWriter::writeModuleInfo() {
1163 // Emit various pieces of data attached to a module.
1164
1165 // We need to hardcode a triple and datalayout that's compatible with the
1166 // historical DXIL triple and datalayout from DXC.
1167 StringRef Triple = "dxil-ms-dx";
1168 StringRef DL = "e-m:e-p:32:32-i1:32-i8:8-i16:16-i32:32-i64:64-"
1169 "f16:16-f32:32-f64:64-n8:16:32:64";
1170 writeStringRecord(Stream, bitc::MODULE_CODE_TRIPLE, Triple, 0 /*TODO*/);
1172
1173 // The original bitcode writer wrote inline assembly here. Inline assembly
1174 // isn't valid in DXIL, so this is removed.
1175
1176 // Emit information about sections and GC, computing how many there are. Also
1177 // compute the maximum alignment value.
1178 std::map<std::string, unsigned> SectionMap;
1179 std::map<std::string, unsigned> GCMap;
1180 MaybeAlign MaxAlignment;
1181 unsigned MaxGlobalType = 0;
1182 const auto UpdateMaxAlignment = [&MaxAlignment](const MaybeAlign A) {
1183 if (A)
1184 MaxAlignment = !MaxAlignment ? *A : std::max(*MaxAlignment, *A);
1185 };
1186 for (const GlobalVariable &GV : M.globals()) {
1187 UpdateMaxAlignment(GV.getAlign());
1188 // Use getGlobalObjectValueTypeID to look up the enumerated type ID for
1189 // Global Variable types.
1190 MaxGlobalType = std::max(
1191 MaxGlobalType, getGlobalObjectValueTypeID(GV.getValueType(), &GV));
1192 if (GV.hasSection()) {
1193 // Give section names unique ID's.
1194 unsigned &Entry = SectionMap[std::string(GV.getSection())];
1195 if (!Entry) {
1197 GV.getSection(), 0 /*TODO*/);
1198 Entry = SectionMap.size();
1199 }
1200 }
1201 }
1202 for (const Function &F : M) {
1203 UpdateMaxAlignment(F.getAlign());
1204 if (F.hasSection()) {
1205 // Give section names unique ID's.
1206 unsigned &Entry = SectionMap[std::string(F.getSection())];
1207 if (!Entry) {
1209 0 /*TODO*/);
1210 Entry = SectionMap.size();
1211 }
1212 }
1213 if (F.hasGC()) {
1214 // Same for GC names.
1215 unsigned &Entry = GCMap[F.getGC()];
1216 if (!Entry) {
1218 0 /*TODO*/);
1219 Entry = GCMap.size();
1220 }
1221 }
1222 }
1223
1224 // Emit abbrev for globals, now that we know # sections and max alignment.
1225 unsigned SimpleGVarAbbrev = 0;
1226 if (!M.global_empty()) {
1227 // Add an abbrev for common globals with no visibility or thread
1228 // localness.
1229 auto Abbv = std::make_shared<BitCodeAbbrev>();
1230 Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_GLOBALVAR));
1231 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
1232 Log2_32_Ceil(MaxGlobalType + 1)));
1233 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // AddrSpace << 2
1234 //| explicitType << 1
1235 //| constant
1236 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Initializer.
1237 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 5)); // Linkage.
1238 if (!MaxAlignment) // Alignment.
1239 Abbv->Add(BitCodeAbbrevOp(0));
1240 else {
1241 unsigned MaxEncAlignment = getEncodedAlign(MaxAlignment);
1242 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
1243 Log2_32_Ceil(MaxEncAlignment + 1)));
1244 }
1245 if (SectionMap.empty()) // Section.
1246 Abbv->Add(BitCodeAbbrevOp(0));
1247 else
1248 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
1249 Log2_32_Ceil(SectionMap.size() + 1)));
1250 // Don't bother emitting vis + thread local.
1251 SimpleGVarAbbrev = Stream.EmitAbbrev(std::move(Abbv));
1252 }
1253
1254 // Emit the global variable information.
1256 for (const GlobalVariable &GV : M.globals()) {
1257 unsigned AbbrevToUse = 0;
1258
1259 // GLOBALVAR: [type, isconst, initid,
1260 // linkage, alignment, section, visibility, threadlocal,
1261 // unnamed_addr, externally_initialized, dllstorageclass,
1262 // comdat]
1263 Vals.push_back(getGlobalObjectValueTypeID(GV.getValueType(), &GV));
1264 Vals.push_back(
1265 GV.getType()->getAddressSpace() << 2 | 2 |
1266 (GV.isConstant() ? 1 : 0)); // HLSL Change - bitwise | was used with
1267 // unsigned int and bool
1268 Vals.push_back(
1269 GV.isDeclaration() ? 0 : (VE.getValueID(GV.getInitializer()) + 1));
1270 Vals.push_back(getEncodedLinkage(GV));
1271 Vals.push_back(getEncodedAlign(GV.getAlign()));
1272 Vals.push_back(GV.hasSection() ? SectionMap[std::string(GV.getSection())]
1273 : 0);
1274 if (GV.isThreadLocal() ||
1275 GV.getVisibility() != GlobalValue::DefaultVisibility ||
1276 GV.getUnnamedAddr() != GlobalValue::UnnamedAddr::None ||
1277 GV.isExternallyInitialized() ||
1278 GV.getDLLStorageClass() != GlobalValue::DefaultStorageClass ||
1279 GV.hasComdat()) {
1282 Vals.push_back(GV.getUnnamedAddr() != GlobalValue::UnnamedAddr::None);
1283 Vals.push_back(GV.isExternallyInitialized());
1285 Vals.push_back(GV.hasComdat() ? VE.getComdatID(GV.getComdat()) : 0);
1286 } else {
1287 AbbrevToUse = SimpleGVarAbbrev;
1288 }
1289
1290 Stream.EmitRecord(bitc::MODULE_CODE_GLOBALVAR, Vals, AbbrevToUse);
1291 Vals.clear();
1292 }
1293
1294 // Emit the function proto information.
1295 for (const Function &OrigF : M) {
1296 const Function &F = VE.getDXILFunction(OrigF);
1297
1298 // FUNCTION: [type, callingconv, isproto, linkage, paramattrs, alignment,
1299 // section, visibility, gc, unnamed_addr, prologuedata,
1300 // dllstorageclass, comdat, prefixdata, personalityfn]
1301 Vals.push_back(getGlobalObjectValueTypeID(F.getFunctionType(), &F));
1302 Vals.push_back(F.getCallingConv());
1303 Vals.push_back(F.isDeclaration());
1305 Vals.push_back(VE.getAttributeListID(F.getAttributes()));
1306 Vals.push_back(getEncodedAlign(F.getAlign()));
1307 Vals.push_back(F.hasSection() ? SectionMap[std::string(F.getSection())]
1308 : 0);
1310 Vals.push_back(F.hasGC() ? GCMap[F.getGC()] : 0);
1311 Vals.push_back(F.getUnnamedAddr() != GlobalValue::UnnamedAddr::None);
1312 Vals.push_back(
1313 F.hasPrologueData() ? (VE.getValueID(F.getPrologueData()) + 1) : 0);
1315 Vals.push_back(F.hasComdat() ? VE.getComdatID(F.getComdat()) : 0);
1316 Vals.push_back(F.hasPrefixData() ? (VE.getValueID(F.getPrefixData()) + 1)
1317 : 0);
1318 Vals.push_back(
1319 F.hasPersonalityFn() ? (VE.getValueID(F.getPersonalityFn()) + 1) : 0);
1320
1321 unsigned AbbrevToUse = 0;
1322 Stream.EmitRecord(bitc::MODULE_CODE_FUNCTION, Vals, AbbrevToUse);
1323 Vals.clear();
1324 }
1325
1326 // Emit the alias information.
1327 for (const GlobalAlias &A : M.aliases()) {
1328 // ALIAS: [alias type, aliasee val#, linkage, visibility]
1329 Vals.push_back(getTypeID(A.getValueType(), &A));
1330 Vals.push_back(VE.getValueID(A.getAliasee()));
1335 Vals.push_back(A.getUnnamedAddr() != GlobalValue::UnnamedAddr::None);
1336 unsigned AbbrevToUse = 0;
1337 Stream.EmitRecord(bitc::MODULE_CODE_ALIAS_OLD, Vals, AbbrevToUse);
1338 Vals.clear();
1339 }
1340}
1341
1342void DXILBitcodeWriter::writeValueAsMetadata(
1343 const ValueAsMetadata *MD, SmallVectorImpl<uint64_t> &Record) {
1344 // Mimic an MDNode with a value as one operand.
1345 Value *V = MD->getValue();
1346 Type *Ty = V->getType();
1348 Ty = TypedPointerType::get(F->getFunctionType(), F->getAddressSpace());
1349 else if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
1350 Ty = TypedPointerType::get(GV->getValueType(), GV->getAddressSpace());
1351 Record.push_back(getTypeID(Ty, V));
1352 Record.push_back(VE.getValueID(V));
1353 Stream.EmitRecord(bitc::METADATA_VALUE, Record, 0);
1354 Record.clear();
1355}
1356
1357void DXILBitcodeWriter::writeMDTuple(const MDTuple *N,
1358 SmallVectorImpl<uint64_t> &Record,
1359 unsigned Abbrev) {
1360 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
1361 Metadata *MD = N->getOperand(i);
1362 assert(!(MD && isa<LocalAsMetadata>(MD)) &&
1363 "Unexpected function-local metadata");
1364 Record.push_back(VE.getMetadataOrNullID(MD));
1365 }
1366 Stream.EmitRecord(N->isDistinct() ? bitc::METADATA_DISTINCT_NODE
1368 Record, Abbrev);
1369 Record.clear();
1370}
1371
1372void DXILBitcodeWriter::writeDILocation(const DILocation *N,
1373 SmallVectorImpl<uint64_t> &Record,
1374 unsigned &Abbrev) {
1375 if (!Abbrev)
1376 Abbrev = createDILocationAbbrev();
1377 Record.push_back(N->isDistinct());
1378 Record.push_back(N->getLine());
1379 Record.push_back(N->getColumn());
1380 Record.push_back(VE.getMetadataID(N->getScope()));
1381 Record.push_back(VE.getMetadataOrNullID(N->getInlinedAt()));
1382
1383 Stream.EmitRecord(bitc::METADATA_LOCATION, Record, Abbrev);
1384 Record.clear();
1385}
1386
1388 int64_t I = Val.getSExtValue();
1389 uint64_t U = I;
1390 return I < 0 ? ~(U << 1) : U << 1;
1391}
1392
1393void DXILBitcodeWriter::writeDISubrange(const DISubrange *N,
1394 SmallVectorImpl<uint64_t> &Record,
1395 unsigned Abbrev) {
1396 Record.push_back(N->isDistinct());
1397
1398 // Count may be a reference to a DILocalVariable or DIGlobalVariable
1399 // in case of C99 VLA. Non-constant count It is not supported by
1400 // DXIL, so we emit a subrange of -1 (empty).
1401 if (ConstantInt *Count = dyn_cast<ConstantInt *>(N->getCount())) {
1402 Record.push_back(Count->getValue().getSExtValue());
1403 } else {
1404 Record.push_back(-1);
1405 }
1406
1407 // Similarly, non constant lower bound is not allowed here.
1408 DISubrange::BoundType LowerBound = N->getLowerBound();
1409 if (!LowerBound.isNull() && isa<ConstantInt *>(LowerBound)) {
1410 Record.push_back(rotateSign(cast<ConstantInt *>(LowerBound)->getValue()));
1411 } else {
1412 Record.push_back(0);
1413 }
1414
1415 Stream.EmitRecord(bitc::METADATA_SUBRANGE, Record, Abbrev);
1416 Record.clear();
1417}
1418
1419void DXILBitcodeWriter::writeDIEnumerator(const DIEnumerator *N,
1420 SmallVectorImpl<uint64_t> &Record,
1421 unsigned Abbrev) {
1422 Record.push_back(N->isDistinct());
1423 Record.push_back(rotateSign(N->getValue()));
1424 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1425
1426 Stream.EmitRecord(bitc::METADATA_ENUMERATOR, Record, Abbrev);
1427 Record.clear();
1428}
1429
1430void DXILBitcodeWriter::writeDIBasicType(const DIBasicType *N,
1431 SmallVectorImpl<uint64_t> &Record,
1432 unsigned Abbrev) {
1433 Record.push_back(N->isDistinct());
1434 Record.push_back(N->getTag());
1435 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1436 Record.push_back(N->getSizeInBits());
1437 Record.push_back(N->getAlignInBits());
1438 Record.push_back(N->getEncoding());
1439
1440 Stream.EmitRecord(bitc::METADATA_BASIC_TYPE, Record, Abbrev);
1441 Record.clear();
1442}
1443
1444void DXILBitcodeWriter::writeDIDerivedType(const DIDerivedType *N,
1445 SmallVectorImpl<uint64_t> &Record,
1446 unsigned Abbrev) {
1447 Record.push_back(N->isDistinct());
1448 Record.push_back(N->getTag());
1449 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1450 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1451 Record.push_back(N->getLine());
1452 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1453 Record.push_back(VE.getMetadataOrNullID(N->getBaseType()));
1454 Record.push_back(N->getSizeInBits());
1455 Record.push_back(N->getAlignInBits());
1456 Record.push_back(N->getOffsetInBits());
1457 Record.push_back(N->getFlags());
1458 Record.push_back(VE.getMetadataOrNullID(N->getExtraData()));
1459
1460 Stream.EmitRecord(bitc::METADATA_DERIVED_TYPE, Record, Abbrev);
1461 Record.clear();
1462}
1463
1464void DXILBitcodeWriter::writeDICompositeType(const DICompositeType *N,
1465 SmallVectorImpl<uint64_t> &Record,
1466 unsigned Abbrev) {
1467 Record.push_back(N->isDistinct());
1468 Record.push_back(N->getTag());
1469 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1470 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1471 Record.push_back(N->getLine());
1472 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1473 Record.push_back(VE.getMetadataOrNullID(N->getBaseType()));
1474 Record.push_back(N->getSizeInBits());
1475 Record.push_back(N->getAlignInBits());
1476 Record.push_back(N->getOffsetInBits());
1477 Record.push_back(N->getFlags());
1478 Record.push_back(VE.getMetadataOrNullID(N->getElements().get()));
1479 Record.push_back(N->getRuntimeLang());
1480 Record.push_back(VE.getMetadataOrNullID(N->getVTableHolder()));
1481 Record.push_back(VE.getMetadataOrNullID(N->getTemplateParams().get()));
1482 Record.push_back(VE.getMetadataOrNullID(N->getRawIdentifier()));
1483
1484 Stream.EmitRecord(bitc::METADATA_COMPOSITE_TYPE, Record, Abbrev);
1485 Record.clear();
1486}
1487
1488void DXILBitcodeWriter::writeDISubroutineType(const DISubroutineType *N,
1489 SmallVectorImpl<uint64_t> &Record,
1490 unsigned Abbrev) {
1491 Record.push_back(N->isDistinct());
1492 Record.push_back(N->getFlags());
1493 Record.push_back(VE.getMetadataOrNullID(N->getTypeArray().get()));
1494
1495 Stream.EmitRecord(bitc::METADATA_SUBROUTINE_TYPE, Record, Abbrev);
1496 Record.clear();
1497}
1498
1499void DXILBitcodeWriter::writeDIFile(const DIFile *N,
1500 SmallVectorImpl<uint64_t> &Record,
1501 unsigned Abbrev) {
1502 Record.push_back(N->isDistinct());
1503 Record.push_back(VE.getMetadataOrNullID(N->getRawFilename()));
1504 Record.push_back(VE.getMetadataOrNullID(N->getRawDirectory()));
1505
1506 Stream.EmitRecord(bitc::METADATA_FILE, Record, Abbrev);
1507 Record.clear();
1508}
1509
1510void DXILBitcodeWriter::writeDICompileUnit(const DICompileUnit *N,
1511 SmallVectorImpl<uint64_t> &Record,
1512 unsigned Abbrev) {
1513 Record.push_back(N->isDistinct());
1514 Record.push_back(N->getSourceLanguage().getUnversionedName());
1515 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1516 Record.push_back(VE.getMetadataOrNullID(N->getRawProducer()));
1517 Record.push_back(N->isOptimized());
1518 Record.push_back(VE.getMetadataOrNullID(N->getRawFlags()));
1519 Record.push_back(N->getRuntimeVersion());
1520 Record.push_back(VE.getMetadataOrNullID(N->getRawSplitDebugFilename()));
1521 Record.push_back(N->getEmissionKind());
1522 Record.push_back(VE.getMetadataOrNullID(N->getEnumTypes().get()));
1523 Record.push_back(VE.getMetadataOrNullID(N->getRetainedTypes().get()));
1524 Record.push_back(VE.getMetadataOrNullID(DebugInfo.MDExtra.lookup(N)));
1525 Record.push_back(VE.getMetadataOrNullID(N->getGlobalVariables().get()));
1526 Record.push_back(VE.getMetadataOrNullID(N->getImportedEntities().get()));
1527 Record.push_back(N->getDWOId());
1528
1529 Stream.EmitRecord(bitc::METADATA_COMPILE_UNIT, Record, Abbrev);
1530 Record.clear();
1531}
1532
1533void DXILBitcodeWriter::writeDISubprogram(const DISubprogram *N,
1534 SmallVectorImpl<uint64_t> &Record,
1535 unsigned Abbrev) {
1536 Record.push_back(N->isDistinct());
1537 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1538 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1539 Record.push_back(VE.getMetadataOrNullID(N->getRawLinkageName()));
1540 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1541 Record.push_back(N->getLine());
1542 Record.push_back(VE.getMetadataOrNullID(N->getType()));
1543 Record.push_back(N->isLocalToUnit());
1544 Record.push_back(N->isDefinition());
1545 Record.push_back(N->getScopeLine());
1546 Record.push_back(VE.getMetadataOrNullID(N->getContainingType()));
1547 Record.push_back(N->getVirtuality());
1548 Record.push_back(N->getVirtualIndex());
1549 Record.push_back(N->getFlags());
1550 Record.push_back(N->isOptimized());
1551 Record.push_back(VE.getMetadataOrNullID(DebugInfo.MDExtra.lookup(N)));
1552 Record.push_back(VE.getMetadataOrNullID(N->getTemplateParams().get()));
1553 Record.push_back(VE.getMetadataOrNullID(N->getDeclaration()));
1554 Record.push_back(VE.getMetadataOrNullID(N->getRetainedNodes().get()));
1555
1556 Stream.EmitRecord(bitc::METADATA_SUBPROGRAM, Record, Abbrev);
1557 Record.clear();
1558}
1559
1560void DXILBitcodeWriter::writeDILexicalBlock(const DILexicalBlock *N,
1561 SmallVectorImpl<uint64_t> &Record,
1562 unsigned Abbrev) {
1563 Record.push_back(N->isDistinct());
1564 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1565 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1566 Record.push_back(N->getLine());
1567 Record.push_back(N->getColumn());
1568
1569 Stream.EmitRecord(bitc::METADATA_LEXICAL_BLOCK, Record, Abbrev);
1570 Record.clear();
1571}
1572
1573void DXILBitcodeWriter::writeDILexicalBlockFile(
1574 const DILexicalBlockFile *N, SmallVectorImpl<uint64_t> &Record,
1575 unsigned Abbrev) {
1576 Record.push_back(N->isDistinct());
1577 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1578 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1579 Record.push_back(N->getDiscriminator());
1580
1581 Stream.EmitRecord(bitc::METADATA_LEXICAL_BLOCK_FILE, Record, Abbrev);
1582 Record.clear();
1583}
1584
1585void DXILBitcodeWriter::writeDINamespace(const DINamespace *N,
1586 SmallVectorImpl<uint64_t> &Record,
1587 unsigned Abbrev) {
1588 Record.push_back(N->isDistinct());
1589 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1590 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1591 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1592 Record.push_back(/* line number */ 0);
1593
1594 Stream.EmitRecord(bitc::METADATA_NAMESPACE, Record, Abbrev);
1595 Record.clear();
1596}
1597
1598void DXILBitcodeWriter::writeDIModule(const DIModule *N,
1599 SmallVectorImpl<uint64_t> &Record,
1600 unsigned Abbrev) {
1601 Record.push_back(N->isDistinct());
1602 for (auto &I : N->operands())
1603 Record.push_back(VE.getMetadataOrNullID(I));
1604
1605 Stream.EmitRecord(bitc::METADATA_MODULE, Record, Abbrev);
1606 Record.clear();
1607}
1608
1609void DXILBitcodeWriter::writeDITemplateTypeParameter(
1610 const DITemplateTypeParameter *N, SmallVectorImpl<uint64_t> &Record,
1611 unsigned Abbrev) {
1612 Record.push_back(N->isDistinct());
1613 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1614 Record.push_back(VE.getMetadataOrNullID(N->getType()));
1615
1616 Stream.EmitRecord(bitc::METADATA_TEMPLATE_TYPE, Record, Abbrev);
1617 Record.clear();
1618}
1619
1620void DXILBitcodeWriter::writeDITemplateValueParameter(
1621 const DITemplateValueParameter *N, SmallVectorImpl<uint64_t> &Record,
1622 unsigned Abbrev) {
1623 Record.push_back(N->isDistinct());
1624 Record.push_back(N->getTag());
1625 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1626 Record.push_back(VE.getMetadataOrNullID(N->getType()));
1627 Record.push_back(VE.getMetadataOrNullID(N->getValue()));
1628
1629 Stream.EmitRecord(bitc::METADATA_TEMPLATE_VALUE, Record, Abbrev);
1630 Record.clear();
1631}
1632
1633void DXILBitcodeWriter::writeDIGlobalVariable(const DIGlobalVariable *N,
1634 SmallVectorImpl<uint64_t> &Record,
1635 unsigned Abbrev) {
1636 Record.push_back(N->isDistinct());
1637 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1638 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1639 Record.push_back(VE.getMetadataOrNullID(N->getRawLinkageName()));
1640 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1641 Record.push_back(N->getLine());
1642 Record.push_back(VE.getMetadataOrNullID(N->getType()));
1643 Record.push_back(N->isLocalToUnit());
1644 Record.push_back(N->isDefinition());
1645 Record.push_back(VE.getMetadataOrNullID(DebugInfo.MDExtra.lookup(N)));
1646 Record.push_back(VE.getMetadataOrNullID(N->getStaticDataMemberDeclaration()));
1647
1648 Stream.EmitRecord(bitc::METADATA_GLOBAL_VAR, Record, Abbrev);
1649 Record.clear();
1650}
1651
1652void DXILBitcodeWriter::writeDILocalVariable(const DILocalVariable *N,
1653 SmallVectorImpl<uint64_t> &Record,
1654 unsigned Abbrev) {
1655 constexpr unsigned DW_TAG_auto_variable = 0x0100;
1656 constexpr unsigned DW_TAG_arg_variable = 0x0101;
1657 Record.push_back(N->isDistinct());
1658 assert(N->getTag() == dwarf::DW_TAG_variable);
1659 Record.push_back(N->getArg() ? DW_TAG_arg_variable : DW_TAG_auto_variable);
1660 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1661 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1662 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1663 Record.push_back(N->getLine());
1664 Record.push_back(VE.getMetadataOrNullID(N->getType()));
1665 Record.push_back(N->getArg());
1666 Record.push_back(N->getFlags());
1667
1668 Stream.EmitRecord(bitc::METADATA_LOCAL_VAR, Record, Abbrev);
1669 Record.clear();
1670}
1671
1672void DXILBitcodeWriter::writeDIExpression(const DIExpression *N,
1673 SmallVectorImpl<uint64_t> &Record,
1674 unsigned Abbrev) {
1675 Record.reserve(N->getElements().size() + 1);
1676
1677 Record.push_back(N->isDistinct());
1678 Record.append(N->elements_begin(), N->elements_end());
1679
1680 Stream.EmitRecord(bitc::METADATA_EXPRESSION, Record, Abbrev);
1681 Record.clear();
1682}
1683
1684void DXILBitcodeWriter::writeDIObjCProperty(const DIObjCProperty *N,
1685 SmallVectorImpl<uint64_t> &Record,
1686 unsigned Abbrev) {
1687 llvm_unreachable("DXIL does not support objc!!!");
1688}
1689
1690void DXILBitcodeWriter::writeDIImportedEntity(const DIImportedEntity *N,
1691 SmallVectorImpl<uint64_t> &Record,
1692 unsigned Abbrev) {
1693 Record.push_back(N->isDistinct());
1694 Record.push_back(N->getTag());
1695 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1696 Record.push_back(VE.getMetadataOrNullID(N->getEntity()));
1697 Record.push_back(N->getLine());
1698 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1699
1700 Stream.EmitRecord(bitc::METADATA_IMPORTED_ENTITY, Record, Abbrev);
1701 Record.clear();
1702}
1703
1704unsigned DXILBitcodeWriter::createDILocationAbbrev() {
1705 // Abbrev for METADATA_LOCATION.
1706 //
1707 // Assume the column is usually under 128, and always output the inlined-at
1708 // location (it's never more expensive than building an array size 1).
1709 std::shared_ptr<BitCodeAbbrev> Abbv = std::make_shared<BitCodeAbbrev>();
1710 Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_LOCATION));
1711 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
1712 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1713 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1714 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1715 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1716 return Stream.EmitAbbrev(std::move(Abbv));
1717}
1718
1719unsigned DXILBitcodeWriter::createGenericDINodeAbbrev() {
1720 // Abbrev for METADATA_GENERIC_DEBUG.
1721 //
1722 // Assume the column is usually under 128, and always output the inlined-at
1723 // location (it's never more expensive than building an array size 1).
1724 std::shared_ptr<BitCodeAbbrev> Abbv = std::make_shared<BitCodeAbbrev>();
1725 Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_GENERIC_DEBUG));
1726 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
1727 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1728 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
1729 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1730 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1731 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1732 return Stream.EmitAbbrev(std::move(Abbv));
1733}
1734
1735void DXILBitcodeWriter::writeMetadataRecords(ArrayRef<const Metadata *> MDs,
1736 SmallVectorImpl<uint64_t> &Record,
1737 std::vector<unsigned> *MDAbbrevs,
1738 std::vector<uint64_t> *IndexPos) {
1739 if (MDs.empty())
1740 return;
1741
1742 // Initialize MDNode abbreviations.
1743#define HANDLE_MDNODE_LEAF(CLASS) unsigned CLASS##Abbrev = 0;
1744#include "llvm/IR/Metadata.def"
1745
1746 for (const Metadata *MD : MDs) {
1747 if (IndexPos)
1748 IndexPos->push_back(Stream.GetCurrentBitNo());
1749 if (const MDNode *N = dyn_cast<MDNode>(MD)) {
1750 assert(N->isResolved() && "Expected forward references to be resolved");
1751
1752 switch (N->getMetadataID()) {
1753 default:
1754 llvm_unreachable("Invalid MDNode subclass");
1755#define HANDLE_MDNODE_LEAF(CLASS) \
1756 case Metadata::CLASS##Kind: \
1757 if (MDAbbrevs) \
1758 write##CLASS(cast<CLASS>(N), Record, \
1759 (*MDAbbrevs)[MetadataAbbrev::CLASS##AbbrevID]); \
1760 else \
1761 write##CLASS(cast<CLASS>(N), Record, CLASS##Abbrev); \
1762 continue;
1763#include "llvm/IR/Metadata.def"
1764 }
1765 }
1766 writeValueAsMetadata(cast<ValueAsMetadata>(MD), Record);
1767 }
1768}
1769
1770unsigned DXILBitcodeWriter::createMetadataStringsAbbrev() {
1771 auto Abbv = std::make_shared<BitCodeAbbrev>();
1772 Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_STRING_OLD));
1773 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1774 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
1775 return Stream.EmitAbbrev(std::move(Abbv));
1776}
1777
1778void DXILBitcodeWriter::writeMetadataStrings(
1779 ArrayRef<const Metadata *> Strings, SmallVectorImpl<uint64_t> &Record) {
1780 if (Strings.empty())
1781 return;
1782
1783 unsigned MDSAbbrev = createMetadataStringsAbbrev();
1784
1785 for (const Metadata *MD : Strings) {
1786 const MDString *MDS = cast<MDString>(MD);
1787 // Code: [strchar x N]
1788 Record.append(MDS->bytes_begin(), MDS->bytes_end());
1789
1790 // Emit the finished record.
1791 Stream.EmitRecord(bitc::METADATA_STRING_OLD, Record, MDSAbbrev);
1792 Record.clear();
1793 }
1794}
1795
1796void DXILBitcodeWriter::writeModuleMetadata() {
1797 if (!VE.hasMDs() && M.named_metadata_empty())
1798 return;
1799
1800 Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 5);
1801
1802 // Emit all abbrevs upfront, so that the reader can jump in the middle of the
1803 // block and load any metadata.
1804 std::vector<unsigned> MDAbbrevs;
1805
1806 MDAbbrevs.resize(MetadataAbbrev::LastPlusOne);
1807 MDAbbrevs[MetadataAbbrev::DILocationAbbrevID] = createDILocationAbbrev();
1808 MDAbbrevs[MetadataAbbrev::GenericDINodeAbbrevID] =
1809 createGenericDINodeAbbrev();
1810
1811 unsigned NameAbbrev = 0;
1812 if (!M.named_metadata_empty()) {
1813 // Abbrev for METADATA_NAME.
1814 std::shared_ptr<BitCodeAbbrev> Abbv = std::make_shared<BitCodeAbbrev>();
1815 Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_NAME));
1816 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1817 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
1818 NameAbbrev = Stream.EmitAbbrev(std::move(Abbv));
1819 }
1820
1821 SmallVector<uint64_t, 64> Record;
1822 writeMetadataStrings(VE.getMDStrings(), Record);
1823
1824 std::vector<uint64_t> IndexPos;
1825 IndexPos.reserve(VE.getNonMDStrings().size());
1826 writeMetadataRecords(VE.getNonMDStrings(), Record, &MDAbbrevs, &IndexPos);
1827
1828 // Write named metadata.
1829 for (const NamedMDNode &NMD : M.named_metadata()) {
1830 // Write name.
1831 StringRef Str = NMD.getName();
1832 Record.append(Str.bytes_begin(), Str.bytes_end());
1833 Stream.EmitRecord(bitc::METADATA_NAME, Record, NameAbbrev);
1834 Record.clear();
1835
1836 // Write named metadata operands.
1837 for (const MDNode *N : NMD.operands())
1838 Record.push_back(VE.getMetadataID(N));
1839 Stream.EmitRecord(bitc::METADATA_NAMED_NODE, Record, 0);
1840 Record.clear();
1841 }
1842
1843 Stream.ExitBlock();
1844}
1845
1846void DXILBitcodeWriter::writeFunctionMetadata(const Function &F) {
1847 if (!VE.hasMDs())
1848 return;
1849
1850 Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 4);
1851 SmallVector<uint64_t, 64> Record;
1852 writeMetadataStrings(VE.getMDStrings(), Record);
1853 writeMetadataRecords(VE.getNonMDStrings(), Record);
1854 Stream.ExitBlock();
1855}
1856
1857void DXILBitcodeWriter::writeFunctionMetadataAttachment(const Function &F) {
1858 Stream.EnterSubblock(bitc::METADATA_ATTACHMENT_ID, 3);
1859
1860 SmallVector<uint64_t, 64> Record;
1861
1862 // Write metadata attachments
1863 // METADATA_ATTACHMENT - [m x [value, [n x [id, mdnode]]]
1865 F.getAllMetadata(MDs);
1866 if (!MDs.empty()) {
1867 for (const auto &I : MDs) {
1868 if (I.first == LLVMContext::MD_dbg)
1869 continue;
1870 Record.push_back(I.first);
1871 Record.push_back(VE.getMetadataID(I.second));
1872 }
1873 }
1874 if (!Record.empty()) {
1875 Stream.EmitRecord(bitc::METADATA_ATTACHMENT, Record, 0);
1876 Record.clear();
1877 }
1878
1879 for (const BasicBlock &BB : F)
1880 for (const Instruction &OrigI : BB) {
1881 const Instruction &I = VE.getDXILInstruction(OrigI);
1882
1883 MDs.clear();
1884 I.getAllMetadataOtherThanDebugLoc(MDs);
1885
1886 // If no metadata, ignore instruction.
1887 if (MDs.empty())
1888 continue;
1889
1890 Record.push_back(VE.getInstructionID(&I));
1891
1892 for (unsigned i = 0, e = MDs.size(); i != e; ++i) {
1893 Record.push_back(MDs[i].first);
1894 Record.push_back(VE.getMetadataID(MDs[i].second));
1895 }
1896 Stream.EmitRecord(bitc::METADATA_ATTACHMENT, Record, 0);
1897 Record.clear();
1898 }
1899
1900 Stream.ExitBlock();
1901}
1902
1903void DXILBitcodeWriter::writeModuleMetadataKinds() {
1904 SmallVector<uint64_t, 64> Record;
1905
1906 // Write metadata kinds
1907 // METADATA_KIND - [n x [id, name]]
1909 M.getMDKindNames(Names);
1910
1911 if (Names.empty())
1912 return;
1913
1914 Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
1915
1916 for (unsigned MDKindID = 0, e = Names.size(); MDKindID != e; ++MDKindID) {
1917 Record.push_back(MDKindID);
1918 StringRef KName = Names[MDKindID];
1919 Record.append(KName.begin(), KName.end());
1920
1921 Stream.EmitRecord(bitc::METADATA_KIND, Record, 0);
1922 Record.clear();
1923 }
1924
1925 Stream.ExitBlock();
1926}
1927
1928void DXILBitcodeWriter::writeConstants(unsigned FirstVal, unsigned LastVal,
1929 bool isGlobal) {
1930 if (FirstVal == LastVal)
1931 return;
1932
1933 Stream.EnterSubblock(bitc::CONSTANTS_BLOCK_ID, 4);
1934
1935 unsigned AggregateAbbrev = 0;
1936 unsigned String8Abbrev = 0;
1937 unsigned CString7Abbrev = 0;
1938 unsigned CString6Abbrev = 0;
1939 // If this is a constant pool for the module, emit module-specific abbrevs.
1940 if (isGlobal) {
1941 // Abbrev for CST_CODE_AGGREGATE.
1942 auto Abbv = std::make_shared<BitCodeAbbrev>();
1943 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_AGGREGATE));
1944 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1945 Abbv->Add(
1946 BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, Log2_32_Ceil(LastVal + 1)));
1947 AggregateAbbrev = Stream.EmitAbbrev(std::move(Abbv));
1948
1949 // Abbrev for CST_CODE_STRING.
1950 Abbv = std::make_shared<BitCodeAbbrev>();
1951 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_STRING));
1952 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1953 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
1954 String8Abbrev = Stream.EmitAbbrev(std::move(Abbv));
1955 // Abbrev for CST_CODE_CSTRING.
1956 Abbv = std::make_shared<BitCodeAbbrev>();
1957 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING));
1958 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1959 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
1960 CString7Abbrev = Stream.EmitAbbrev(std::move(Abbv));
1961 // Abbrev for CST_CODE_CSTRING.
1962 Abbv = std::make_shared<BitCodeAbbrev>();
1963 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING));
1964 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1965 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
1966 CString6Abbrev = Stream.EmitAbbrev(std::move(Abbv));
1967 }
1968
1969 SmallVector<uint64_t, 64> Record;
1970
1971 const ValueEnumerator::ValueList &Vals = VE.getValues();
1972 Type *LastTy = nullptr;
1973 for (unsigned i = FirstVal; i != LastVal; ++i) {
1974 const Value *V = Vals[i].first;
1975 // If we need to switch types, do so now.
1976 if (V->getType() != LastTy) {
1977 LastTy = V->getType();
1978 Record.push_back(getTypeID(LastTy, V));
1979 Stream.EmitRecord(bitc::CST_CODE_SETTYPE, Record,
1980 CONSTANTS_SETTYPE_ABBREV);
1981 Record.clear();
1982 }
1983
1984 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
1985 Record.push_back(unsigned(IA->hasSideEffects()) |
1986 unsigned(IA->isAlignStack()) << 1 |
1987 unsigned(IA->getDialect() & 1) << 2);
1988
1989 // Add the asm string.
1990 StringRef AsmStr = IA->getAsmString();
1991 Record.push_back(AsmStr.size());
1992 Record.append(AsmStr.begin(), AsmStr.end());
1993
1994 // Add the constraint string.
1995 StringRef ConstraintStr = IA->getConstraintString();
1996 Record.push_back(ConstraintStr.size());
1997 Record.append(ConstraintStr.begin(), ConstraintStr.end());
1998 Stream.EmitRecord(bitc::CST_CODE_INLINEASM, Record);
1999 Record.clear();
2000 continue;
2001 }
2002 const Constant *C = cast<Constant>(V);
2003 unsigned Code = -1U;
2004 unsigned AbbrevToUse = 0;
2005 if (C->isNullValue()) {
2007 } else if (isa<UndefValue>(C)) {
2009 } else if (const ConstantInt *IV = dyn_cast<ConstantInt>(C)) {
2010 if (IV->getBitWidth() <= 64) {
2011 uint64_t V = IV->getSExtValue();
2012 emitSignedInt64(Record, V);
2014 AbbrevToUse = CONSTANTS_INTEGER_ABBREV;
2015 } else { // Wide integers, > 64 bits in size.
2016 // We have an arbitrary precision integer value to write whose
2017 // bit width is > 64. However, in canonical unsigned integer
2018 // format it is likely that the high bits are going to be zero.
2019 // So, we only write the number of active words.
2020 unsigned NWords = IV->getValue().getActiveWords();
2021 const uint64_t *RawWords = IV->getValue().getRawData();
2022 for (unsigned i = 0; i != NWords; ++i) {
2023 emitSignedInt64(Record, RawWords[i]);
2024 }
2026 }
2027 } else if (const ConstantByte *BV = dyn_cast<ConstantByte>(C)) {
2028 // Note: we downgrade by converting to the equivalent integer - this logic
2029 // should match the `ConstantInt` case above.
2030 if (BV->getBitWidth() <= 64) {
2031 uint64_t V = BV->getSExtValue();
2032 emitSignedInt64(Record, V);
2034 AbbrevToUse = CONSTANTS_INTEGER_ABBREV;
2035 } else { // Wide bytes, > 64 bits in size.
2036 unsigned NWords = BV->getValue().getActiveWords();
2037 const uint64_t *RawWords = BV->getValue().getRawData();
2038 for (unsigned i = 0; i != NWords; ++i) {
2039 emitSignedInt64(Record, RawWords[i]);
2040 }
2042 }
2043 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
2045 Type *Ty = CFP->getType()->getScalarType();
2046 if (Ty->isHalfTy() || Ty->isFloatTy() || Ty->isDoubleTy()) {
2047 Record.push_back(CFP->getValueAPF().bitcastToAPInt().getZExtValue());
2048 } else if (Ty->isX86_FP80Ty()) {
2049 // api needed to prevent premature destruction
2050 // bits are not in the same order as a normal i80 APInt, compensate.
2051 APInt api = CFP->getValueAPF().bitcastToAPInt();
2052 const uint64_t *p = api.getRawData();
2053 Record.push_back((p[1] << 48) | (p[0] >> 16));
2054 Record.push_back(p[0] & 0xffffLL);
2055 } else if (Ty->isFP128Ty() || Ty->isPPC_FP128Ty()) {
2056 APInt api = CFP->getValueAPF().bitcastToAPInt();
2057 const uint64_t *p = api.getRawData();
2058 Record.push_back(p[0]);
2059 Record.push_back(p[1]);
2060 } else {
2061 assert(0 && "Unknown FP type!");
2062 }
2063 } else if (isa<ConstantDataSequential>(C) &&
2064 cast<ConstantDataSequential>(C)->isString()) {
2065 const ConstantDataSequential *Str = cast<ConstantDataSequential>(C);
2066 // Emit constant strings specially.
2067 unsigned NumElts = Str->getNumElements();
2068 // If this is a null-terminated string, use the denser CSTRING encoding.
2069 if (Str->isCString()) {
2071 --NumElts; // Don't encode the null, which isn't allowed by char6.
2072 } else {
2074 AbbrevToUse = String8Abbrev;
2075 }
2076 bool isCStr7 = Code == bitc::CST_CODE_CSTRING;
2077 bool isCStrChar6 = Code == bitc::CST_CODE_CSTRING;
2078 for (unsigned i = 0; i != NumElts; ++i) {
2079 unsigned char V = Str->getElementAsInteger(i);
2080 Record.push_back(V);
2081 isCStr7 &= (V & 128) == 0;
2082 if (isCStrChar6)
2083 isCStrChar6 = BitCodeAbbrevOp::isChar6(V);
2084 }
2085
2086 if (isCStrChar6)
2087 AbbrevToUse = CString6Abbrev;
2088 else if (isCStr7)
2089 AbbrevToUse = CString7Abbrev;
2090 } else if (const ConstantDataSequential *CDS =
2093 Type *EltTy = CDS->getElementType();
2094 if (isa<IntegerType>(EltTy) || isa<ByteType>(EltTy)) {
2095 for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i)
2096 Record.push_back(CDS->getElementAsInteger(i));
2097 } else if (EltTy->isFloatTy()) {
2098 for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) {
2099 union {
2100 float F;
2101 uint32_t I;
2102 };
2103 F = CDS->getElementAsFloat(i);
2104 Record.push_back(I);
2105 }
2106 } else {
2107 assert(EltTy->isDoubleTy() && "Unknown ConstantData element type");
2108 for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) {
2109 union {
2110 double F;
2111 uint64_t I;
2112 };
2113 F = CDS->getElementAsDouble(i);
2114 Record.push_back(I);
2115 }
2116 }
2117 } else if (isa<ConstantArray>(C) || isa<ConstantStruct>(C) ||
2120 for (const Value *Op : C->operands())
2121 Record.push_back(VE.getValueID(Op));
2122 AbbrevToUse = AggregateAbbrev;
2123 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
2124 switch (CE->getOpcode()) {
2125 default:
2126 if (Instruction::isCast(CE->getOpcode())) {
2128 Record.push_back(getEncodedCastOpcode(CE->getOpcode()));
2129 Record.push_back(
2130 getTypeID(C->getOperand(0)->getType(), C->getOperand(0)));
2131 Record.push_back(VE.getValueID(C->getOperand(0)));
2132 AbbrevToUse = CONSTANTS_CE_CAST_Abbrev;
2133 } else {
2134 assert(CE->getNumOperands() == 2 && "Unknown constant expr!");
2136 Record.push_back(getEncodedBinaryOpcode(CE->getOpcode()));
2137 Record.push_back(VE.getValueID(C->getOperand(0)));
2138 Record.push_back(VE.getValueID(C->getOperand(1)));
2140 if (Flags != 0)
2141 Record.push_back(Flags);
2142 }
2143 break;
2144 case Instruction::GetElementPtr: {
2146 const auto *GO = cast<GEPOperator>(C);
2147 if (GO->isInBounds())
2149 Record.push_back(getTypeID(GO->getSourceElementType()));
2150 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) {
2151 Record.push_back(
2152 getTypeID(C->getOperand(i)->getType(), C->getOperand(i)));
2153 Record.push_back(VE.getValueID(C->getOperand(i)));
2154 }
2155 break;
2156 }
2157 case Instruction::Select:
2159 Record.push_back(VE.getValueID(C->getOperand(0)));
2160 Record.push_back(VE.getValueID(C->getOperand(1)));
2161 Record.push_back(VE.getValueID(C->getOperand(2)));
2162 break;
2163 case Instruction::ExtractElement:
2165 Record.push_back(getTypeID(C->getOperand(0)->getType()));
2166 Record.push_back(VE.getValueID(C->getOperand(0)));
2167 Record.push_back(getTypeID(C->getOperand(1)->getType()));
2168 Record.push_back(VE.getValueID(C->getOperand(1)));
2169 break;
2170 case Instruction::InsertElement:
2172 Record.push_back(VE.getValueID(C->getOperand(0)));
2173 Record.push_back(VE.getValueID(C->getOperand(1)));
2174 Record.push_back(getTypeID(C->getOperand(2)->getType()));
2175 Record.push_back(VE.getValueID(C->getOperand(2)));
2176 break;
2177 case Instruction::ShuffleVector:
2178 // If the return type and argument types are the same, this is a
2179 // standard shufflevector instruction. If the types are different,
2180 // then the shuffle is widening or truncating the input vectors, and
2181 // the argument type must also be encoded.
2182 if (C->getType() == C->getOperand(0)->getType()) {
2184 } else {
2186 Record.push_back(getTypeID(C->getOperand(0)->getType()));
2187 }
2188 Record.push_back(VE.getValueID(C->getOperand(0)));
2189 Record.push_back(VE.getValueID(C->getOperand(1)));
2190 Record.push_back(VE.getValueID(C->getOperand(2)));
2191 break;
2192 }
2193 } else if (const BlockAddress *BA = dyn_cast<BlockAddress>(C)) {
2195 Record.push_back(getTypeID(BA->getFunction()->getType()));
2196 Record.push_back(VE.getValueID(BA->getFunction()));
2197 Record.push_back(VE.getGlobalBasicBlockID(BA->getBasicBlock()));
2198 } else {
2199#ifndef NDEBUG
2200 C->dump();
2201#endif
2202 llvm_unreachable("Unknown constant!");
2203 }
2204 Stream.EmitRecord(Code, Record, AbbrevToUse);
2205 Record.clear();
2206 }
2207
2208 Stream.ExitBlock();
2209}
2210
2211void DXILBitcodeWriter::writeModuleConstants() {
2212 const ValueEnumerator::ValueList &Vals = VE.getValues();
2213
2214 // Find the first constant to emit, which is the first non-globalvalue value.
2215 // We know globalvalues have been emitted by WriteModuleInfo.
2216 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
2217 if (!isa<GlobalValue>(Vals[i].first)) {
2218 writeConstants(i, Vals.size(), true);
2219 return;
2220 }
2221 }
2222}
2223
2224/// pushValueAndType - The file has to encode both the value and type id for
2225/// many values, because we need to know what type to create for forward
2226/// references. However, most operands are not forward references, so this type
2227/// field is not needed.
2228///
2229/// This function adds V's value ID to Vals. If the value ID is higher than the
2230/// instruction ID, then it is a forward reference, and it also includes the
2231/// type ID. The value ID that is written is encoded relative to the InstID.
2232bool DXILBitcodeWriter::pushValueAndType(const Value *V, unsigned InstID,
2233 SmallVectorImpl<unsigned> &Vals) {
2234 unsigned ValID = VE.getValueID(V);
2235 // Make encoding relative to the InstID.
2236 Vals.push_back(InstID - ValID);
2237 if (ValID >= InstID) {
2238 Vals.push_back(getTypeID(V->getType(), V));
2239 return true;
2240 }
2241 return false;
2242}
2243
2244/// pushValue - Like pushValueAndType, but where the type of the value is
2245/// omitted (perhaps it was already encoded in an earlier operand).
2246void DXILBitcodeWriter::pushValue(const Value *V, unsigned InstID,
2247 SmallVectorImpl<unsigned> &Vals) {
2248 unsigned ValID = VE.getValueID(V);
2249 Vals.push_back(InstID - ValID);
2250}
2251
2252void DXILBitcodeWriter::pushValueSigned(const Value *V, unsigned InstID,
2253 SmallVectorImpl<uint64_t> &Vals) {
2254 unsigned ValID = VE.getValueID(V);
2255 int64_t diff = ((int32_t)InstID - (int32_t)ValID);
2256 emitSignedInt64(Vals, diff);
2257}
2258
2259/// WriteInstruction - Emit an instruction
2260void DXILBitcodeWriter::writeInstruction(const Instruction &I, unsigned InstID,
2261 SmallVectorImpl<unsigned> &Vals) {
2262 unsigned Code = 0;
2263 unsigned AbbrevToUse = 0;
2264 VE.setInstructionID(&I);
2265 switch (I.getOpcode()) {
2266 default:
2267 if (Instruction::isCast(I.getOpcode())) {
2269 if (!pushValueAndType(I.getOperand(0), InstID, Vals))
2270 AbbrevToUse = (unsigned)FUNCTION_INST_CAST_ABBREV;
2271 Vals.push_back(getTypeID(I.getType(), &I));
2272 Vals.push_back(getEncodedCastOpcode(I.getOpcode()));
2273 } else {
2274 assert(isa<BinaryOperator>(I) && "Unknown instruction!");
2276 if (!pushValueAndType(I.getOperand(0), InstID, Vals))
2277 AbbrevToUse = (unsigned)FUNCTION_INST_BINOP_ABBREV;
2278 pushValue(I.getOperand(1), InstID, Vals);
2279 Vals.push_back(getEncodedBinaryOpcode(I.getOpcode()));
2281 if (Flags != 0) {
2282 if (AbbrevToUse == (unsigned)FUNCTION_INST_BINOP_ABBREV)
2283 AbbrevToUse = (unsigned)FUNCTION_INST_BINOP_FLAGS_ABBREV;
2284 Vals.push_back(Flags);
2285 }
2286 }
2287 break;
2288
2289 case Instruction::GetElementPtr: {
2291 AbbrevToUse = (unsigned)FUNCTION_INST_GEP_ABBREV;
2292 auto &GEPInst = cast<GetElementPtrInst>(I);
2293 Vals.push_back(GEPInst.isInBounds());
2294 Vals.push_back(getTypeID(GEPInst.getSourceElementType()));
2295 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
2296 pushValueAndType(I.getOperand(i), InstID, Vals);
2297 break;
2298 }
2299 case Instruction::ExtractValue: {
2301 pushValueAndType(I.getOperand(0), InstID, Vals);
2302 const ExtractValueInst *EVI = cast<ExtractValueInst>(&I);
2303 Vals.append(EVI->idx_begin(), EVI->idx_end());
2304 break;
2305 }
2306 case Instruction::InsertValue: {
2308 pushValueAndType(I.getOperand(0), InstID, Vals);
2309 pushValueAndType(I.getOperand(1), InstID, Vals);
2310 const InsertValueInst *IVI = cast<InsertValueInst>(&I);
2311 Vals.append(IVI->idx_begin(), IVI->idx_end());
2312 break;
2313 }
2314 case Instruction::Select:
2316 pushValueAndType(I.getOperand(1), InstID, Vals);
2317 pushValue(I.getOperand(2), InstID, Vals);
2318 pushValueAndType(I.getOperand(0), InstID, Vals);
2319 break;
2320 case Instruction::ExtractElement:
2322 pushValueAndType(I.getOperand(0), InstID, Vals);
2323 pushValueAndType(I.getOperand(1), InstID, Vals);
2324 break;
2325 case Instruction::InsertElement:
2327 pushValueAndType(I.getOperand(0), InstID, Vals);
2328 pushValue(I.getOperand(1), InstID, Vals);
2329 pushValueAndType(I.getOperand(2), InstID, Vals);
2330 break;
2331 case Instruction::ShuffleVector:
2333 pushValueAndType(I.getOperand(0), InstID, Vals);
2334 pushValue(I.getOperand(1), InstID, Vals);
2335 pushValue(cast<ShuffleVectorInst>(&I)->getShuffleMaskForBitcode(), InstID,
2336 Vals);
2337 break;
2338 case Instruction::ICmp:
2339 case Instruction::FCmp: {
2340 // compare returning Int1Ty or vector of Int1Ty
2342 pushValueAndType(I.getOperand(0), InstID, Vals);
2343 pushValue(I.getOperand(1), InstID, Vals);
2346 if (Flags != 0)
2347 Vals.push_back(Flags);
2348 break;
2349 }
2350
2351 case Instruction::Ret: {
2353 unsigned NumOperands = I.getNumOperands();
2354 if (NumOperands == 0)
2355 AbbrevToUse = (unsigned)FUNCTION_INST_RET_VOID_ABBREV;
2356 else if (NumOperands == 1) {
2357 if (!pushValueAndType(I.getOperand(0), InstID, Vals))
2358 AbbrevToUse = (unsigned)FUNCTION_INST_RET_VAL_ABBREV;
2359 } else {
2360 for (unsigned i = 0, e = NumOperands; i != e; ++i)
2361 pushValueAndType(I.getOperand(i), InstID, Vals);
2362 }
2363 } break;
2364 case Instruction::UncondBr:
2366 Vals.push_back(VE.getValueID(cast<UncondBrInst>(I).getSuccessor()));
2367 break;
2368 case Instruction::CondBr: {
2370 const CondBrInst &II = cast<CondBrInst>(I);
2371 Vals.push_back(VE.getValueID(II.getSuccessor(0)));
2372 Vals.push_back(VE.getValueID(II.getSuccessor(1)));
2373 pushValue(II.getCondition(), InstID, Vals);
2374 } break;
2375 case Instruction::Switch: {
2377 const SwitchInst &SI = cast<SwitchInst>(I);
2378 Vals.push_back(getTypeID(SI.getCondition()->getType()));
2379 pushValue(SI.getCondition(), InstID, Vals);
2380 Vals.push_back(VE.getValueID(SI.getDefaultDest()));
2381 for (auto Case : SI.cases()) {
2382 Vals.push_back(VE.getValueID(Case.getCaseValue()));
2383 Vals.push_back(VE.getValueID(Case.getCaseSuccessor()));
2384 }
2385 } break;
2386 case Instruction::IndirectBr:
2388 Vals.push_back(getTypeID(I.getOperand(0)->getType()));
2389 // Encode the address operand as relative, but not the basic blocks.
2390 pushValue(I.getOperand(0), InstID, Vals);
2391 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i)
2392 Vals.push_back(VE.getValueID(I.getOperand(i)));
2393 break;
2394
2395 case Instruction::Invoke: {
2396 const InvokeInst *II = cast<InvokeInst>(&I);
2397 const Value *Callee = II->getCalledOperand();
2398 FunctionType *FTy = II->getFunctionType();
2400
2401 Vals.push_back(VE.getAttributeListID(II->getAttributes()));
2402 Vals.push_back(II->getCallingConv() | 1 << 13);
2403 Vals.push_back(VE.getValueID(II->getNormalDest()));
2404 Vals.push_back(VE.getValueID(II->getUnwindDest()));
2405 Vals.push_back(getTypeID(FTy));
2406 pushValueAndType(Callee, InstID, Vals);
2407
2408 // Emit value #'s for the fixed parameters.
2409 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
2410 pushValue(I.getOperand(i), InstID, Vals); // fixed param.
2411
2412 // Emit type/value pairs for varargs params.
2413 if (FTy->isVarArg()) {
2414 for (unsigned i = FTy->getNumParams(), e = I.getNumOperands() - 3; i != e;
2415 ++i)
2416 pushValueAndType(I.getOperand(i), InstID, Vals); // vararg
2417 }
2418 break;
2419 }
2420 case Instruction::Resume:
2422 pushValueAndType(I.getOperand(0), InstID, Vals);
2423 break;
2424 case Instruction::Unreachable:
2426 AbbrevToUse = (unsigned)FUNCTION_INST_UNREACHABLE_ABBREV;
2427 break;
2428
2429 case Instruction::PHI: {
2430 const PHINode &PN = cast<PHINode>(I);
2432 // With the newer instruction encoding, forward references could give
2433 // negative valued IDs. This is most common for PHIs, so we use
2434 // signed VBRs.
2436 Vals64.push_back(getTypeID(PN.getType()));
2437 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
2438 pushValueSigned(PN.getIncomingValue(i), InstID, Vals64);
2439 Vals64.push_back(VE.getValueID(PN.getIncomingBlock(i)));
2440 }
2441 // Emit a Vals64 vector and exit.
2442 Stream.EmitRecord(Code, Vals64, AbbrevToUse);
2443 Vals64.clear();
2444 return;
2445 }
2446
2447 case Instruction::LandingPad: {
2448 const LandingPadInst &LP = cast<LandingPadInst>(I);
2450 Vals.push_back(getTypeID(LP.getType()));
2451 Vals.push_back(LP.isCleanup());
2452 Vals.push_back(LP.getNumClauses());
2453 for (unsigned I = 0, E = LP.getNumClauses(); I != E; ++I) {
2454 if (LP.isCatch(I))
2456 else
2458 pushValueAndType(LP.getClause(I), InstID, Vals);
2459 }
2460 break;
2461 }
2462
2463 case Instruction::Alloca: {
2465 const AllocaInst &AI = cast<AllocaInst>(I);
2466 Vals.push_back(getTypeID(AI.getAllocatedType()));
2467 Vals.push_back(getTypeID(I.getOperand(0)->getType()));
2468 Vals.push_back(VE.getValueID(I.getOperand(0))); // size.
2469 unsigned AlignRecord = Log2_32(AI.getAlign().value()) + 1;
2470 assert(AlignRecord < 1 << 5 && "alignment greater than 1 << 64");
2471 AlignRecord |= AI.isUsedWithInAlloca() << 5;
2472 AlignRecord |= 1 << 6;
2473 Vals.push_back(AlignRecord);
2474 break;
2475 }
2476
2477 case Instruction::Load:
2478 if (cast<LoadInst>(I).isAtomic()) {
2480 pushValueAndType(I.getOperand(0), InstID, Vals);
2481 } else {
2483 if (!pushValueAndType(I.getOperand(0), InstID, Vals)) // ptr
2484 AbbrevToUse = (unsigned)FUNCTION_INST_LOAD_ABBREV;
2485 }
2486 Vals.push_back(getTypeID(I.getType()));
2487 Vals.push_back(Log2(cast<LoadInst>(I).getAlign()) + 1);
2488 Vals.push_back(cast<LoadInst>(I).isVolatile());
2489 if (cast<LoadInst>(I).isAtomic()) {
2490 Vals.push_back(getEncodedOrdering(cast<LoadInst>(I).getOrdering()));
2491 Vals.push_back(getEncodedSyncScopeID(cast<LoadInst>(I).getSyncScopeID()));
2492 }
2493 break;
2494 case Instruction::Store:
2495 if (cast<StoreInst>(I).isAtomic())
2497 else
2499 pushValueAndType(I.getOperand(1), InstID, Vals); // ptrty + ptr
2500 pushValueAndType(I.getOperand(0), InstID, Vals); // valty + val
2501 Vals.push_back(Log2(cast<StoreInst>(I).getAlign()) + 1);
2502 Vals.push_back(cast<StoreInst>(I).isVolatile());
2503 if (cast<StoreInst>(I).isAtomic()) {
2504 Vals.push_back(getEncodedOrdering(cast<StoreInst>(I).getOrdering()));
2505 Vals.push_back(
2506 getEncodedSyncScopeID(cast<StoreInst>(I).getSyncScopeID()));
2507 }
2508 break;
2509 case Instruction::AtomicCmpXchg:
2511 pushValueAndType(I.getOperand(0), InstID, Vals); // ptrty + ptr
2512 pushValueAndType(I.getOperand(1), InstID, Vals); // cmp.
2513 pushValue(I.getOperand(2), InstID, Vals); // newval.
2514 Vals.push_back(cast<AtomicCmpXchgInst>(I).isVolatile());
2515 Vals.push_back(
2516 getEncodedOrdering(cast<AtomicCmpXchgInst>(I).getSuccessOrdering()));
2517 Vals.push_back(
2518 getEncodedSyncScopeID(cast<AtomicCmpXchgInst>(I).getSyncScopeID()));
2519 Vals.push_back(
2520 getEncodedOrdering(cast<AtomicCmpXchgInst>(I).getFailureOrdering()));
2521 Vals.push_back(cast<AtomicCmpXchgInst>(I).isWeak());
2522 break;
2523 case Instruction::AtomicRMW:
2525 pushValueAndType(I.getOperand(0), InstID, Vals); // ptrty + ptr
2526 pushValue(I.getOperand(1), InstID, Vals); // val.
2527 Vals.push_back(
2529 Vals.push_back(cast<AtomicRMWInst>(I).isVolatile());
2530 Vals.push_back(getEncodedOrdering(cast<AtomicRMWInst>(I).getOrdering()));
2531 Vals.push_back(
2532 getEncodedSyncScopeID(cast<AtomicRMWInst>(I).getSyncScopeID()));
2533 break;
2534 case Instruction::Fence:
2536 Vals.push_back(getEncodedOrdering(cast<FenceInst>(I).getOrdering()));
2537 Vals.push_back(getEncodedSyncScopeID(cast<FenceInst>(I).getSyncScopeID()));
2538 break;
2539 case Instruction::Call: {
2540 const CallInst &CI = cast<CallInst>(I);
2541 FunctionType *FTy = CI.getFunctionType();
2542
2544
2545 Vals.push_back(VE.getAttributeListID(CI.getAttributes()));
2546 Vals.push_back((CI.getCallingConv() << 1) | unsigned(CI.isTailCall()) |
2547 unsigned(CI.isMustTailCall()) << 14 | 1 << 15);
2548 Vals.push_back(getGlobalObjectValueTypeID(FTy, CI.getCalledFunction()));
2549 pushValueAndType(CI.getCalledOperand(), InstID, Vals); // Callee
2550
2551 // Emit value #'s for the fixed parameters.
2552 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) {
2553 // Check for labels (can happen with asm labels).
2554 if (FTy->getParamType(i)->isLabelTy())
2555 Vals.push_back(VE.getValueID(CI.getArgOperand(i)));
2556 else
2557 pushValue(CI.getArgOperand(i), InstID, Vals); // fixed param.
2558 }
2559
2560 // Emit type/value pairs for varargs params.
2561 if (FTy->isVarArg()) {
2562 for (unsigned i = FTy->getNumParams(), e = CI.arg_size(); i != e; ++i)
2563 pushValueAndType(CI.getArgOperand(i), InstID, Vals); // varargs
2564 }
2565 break;
2566 }
2567 case Instruction::VAArg:
2569 Vals.push_back(getTypeID(I.getOperand(0)->getType())); // valistty
2570 pushValue(I.getOperand(0), InstID, Vals); // valist.
2571 Vals.push_back(getTypeID(I.getType())); // restype.
2572 break;
2573 }
2574
2575 Stream.EmitRecord(Code, Vals, AbbrevToUse);
2576 Vals.clear();
2577}
2578
2579// Emit names for globals/functions etc.
2580void DXILBitcodeWriter::writeFunctionLevelValueSymbolTable(
2581 const ValueSymbolTable &VST) {
2582 if (VST.empty())
2583 return;
2584 Stream.EnterSubblock(bitc::VALUE_SYMTAB_BLOCK_ID, 4);
2585
2587
2588 // HLSL Change
2589 // Read the named values from a sorted list instead of the original list
2590 // to ensure the binary is the same no matter what values ever existed.
2592
2593 for (auto &VI : VST) {
2594 const Value &V = VE.getDXILValue(*VI.second);
2595 SortedTable.push_back(V.getValueName());
2596 }
2597 // The keys are unique, so there shouldn't be stability issues.
2598 llvm::sort(SortedTable, [](const ValueName *A, const ValueName *B) {
2599 return A->first() < B->first();
2600 });
2601
2602 for (const ValueName *SI : SortedTable) {
2603 auto &Name = *SI;
2604
2605 // Figure out the encoding to use for the name.
2606 bool is7Bit = true;
2607 bool isChar6 = true;
2608 for (const char *C = Name.getKeyData(), *E = C + Name.getKeyLength();
2609 C != E; ++C) {
2610 if (isChar6)
2611 isChar6 = BitCodeAbbrevOp::isChar6(*C);
2612 if ((unsigned char)*C & 128) {
2613 is7Bit = false;
2614 break; // don't bother scanning the rest.
2615 }
2616 }
2617
2618 unsigned AbbrevToUse = VST_ENTRY_8_ABBREV;
2619
2620 // VST_ENTRY: [valueid, namechar x N]
2621 // VST_BBENTRY: [bbid, namechar x N]
2622 unsigned Code;
2623 if (isa<BasicBlock>(SI->getValue())) {
2625 if (isChar6)
2626 AbbrevToUse = VST_BBENTRY_6_ABBREV;
2627 } else {
2629 if (isChar6)
2630 AbbrevToUse = VST_ENTRY_6_ABBREV;
2631 else if (is7Bit)
2632 AbbrevToUse = VST_ENTRY_7_ABBREV;
2633 }
2634
2635 NameVals.push_back(VE.getValueID(SI->getValue()));
2636 for (const char *P = Name.getKeyData(),
2637 *E = Name.getKeyData() + Name.getKeyLength();
2638 P != E; ++P)
2639 NameVals.push_back((unsigned char)*P);
2640
2641 // Emit the finished record.
2642 Stream.EmitRecord(Code, NameVals, AbbrevToUse);
2643 NameVals.clear();
2644 }
2645 Stream.ExitBlock();
2646}
2647
2648/// Emit a function body to the module stream.
2649void DXILBitcodeWriter::writeFunction(const Function &F) {
2650 Stream.EnterSubblock(bitc::FUNCTION_BLOCK_ID, 4);
2651 VE.incorporateFunction(F);
2652
2654
2655 // Emit the number of basic blocks, so the reader can create them ahead of
2656 // time.
2657 Vals.push_back(VE.getBasicBlocks().size());
2658 Stream.EmitRecord(bitc::FUNC_CODE_DECLAREBLOCKS, Vals);
2659 Vals.clear();
2660
2661 // If there are function-local constants, emit them now.
2662 unsigned CstStart, CstEnd;
2663 VE.getFunctionConstantRange(CstStart, CstEnd);
2664 writeConstants(CstStart, CstEnd, false);
2665
2666 // If there is function-local metadata, emit it now.
2667 writeFunctionMetadata(F);
2668
2669 // Keep a running idea of what the instruction ID is.
2670 unsigned InstID = CstEnd;
2671
2672 bool NeedsMetadataAttachment = F.hasMetadata();
2673
2674 DILocation *LastDL = nullptr;
2675
2676 // Finally, emit all the instructions, in order.
2677 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
2678 for (BasicBlock::const_iterator It = BB->begin(), E = BB->end(); It != E;
2679 ++It) {
2680 const Instruction &I = VE.getDXILInstruction(*It);
2681
2682 writeInstruction(I, InstID, Vals);
2683
2684 if (!I.getType()->isVoidTy())
2685 ++InstID;
2686
2687 // If the instruction has metadata, write a metadata attachment later.
2688 NeedsMetadataAttachment |= I.hasMetadataOtherThanDebugLoc();
2689
2690 // If the instruction has a debug location, emit it.
2691 DILocation *DL = I.getDebugLoc();
2692 if (!DL)
2693 continue;
2694
2695 if (DL == LastDL) {
2696 // Just repeat the same debug loc as last time.
2697 Stream.EmitRecord(bitc::FUNC_CODE_DEBUG_LOC_AGAIN, Vals);
2698 continue;
2699 }
2700
2701 Vals.push_back(DL->getLine());
2702 Vals.push_back(DL->getColumn());
2703 Vals.push_back(VE.getMetadataOrNullID(DL->getScope()));
2704 Vals.push_back(VE.getMetadataOrNullID(DL->getInlinedAt()));
2705 Stream.EmitRecord(bitc::FUNC_CODE_DEBUG_LOC, Vals);
2706 Vals.clear();
2707
2708 LastDL = DL;
2709 }
2710
2711 // Emit names for all the instructions etc.
2712 if (auto *Symtab = F.getValueSymbolTable())
2713 writeFunctionLevelValueSymbolTable(*Symtab);
2714
2715 if (NeedsMetadataAttachment)
2716 writeFunctionMetadataAttachment(F);
2717
2718 VE.purgeFunction();
2719 Stream.ExitBlock();
2720}
2721
2722// Emit blockinfo, which defines the standard abbreviations etc.
2723void DXILBitcodeWriter::writeBlockInfo() {
2724 // We only want to emit block info records for blocks that have multiple
2725 // instances: CONSTANTS_BLOCK, FUNCTION_BLOCK and VALUE_SYMTAB_BLOCK.
2726 // Other blocks can define their abbrevs inline.
2727 Stream.EnterBlockInfoBlock();
2728
2729 { // 8-bit fixed-width VST_ENTRY/VST_BBENTRY strings.
2730 auto Abbv = std::make_shared<BitCodeAbbrev>();
2731 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3));
2732 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
2733 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2734 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
2735 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
2736 std::move(Abbv)) != VST_ENTRY_8_ABBREV)
2737 assert(false && "Unexpected abbrev ordering!");
2738 }
2739
2740 { // 7-bit fixed width VST_ENTRY strings.
2741 auto Abbv = std::make_shared<BitCodeAbbrev>();
2742 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
2743 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
2744 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2745 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
2746 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
2747 std::move(Abbv)) != VST_ENTRY_7_ABBREV)
2748 assert(false && "Unexpected abbrev ordering!");
2749 }
2750 { // 6-bit char6 VST_ENTRY strings.
2751 auto Abbv = std::make_shared<BitCodeAbbrev>();
2752 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
2753 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
2754 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2755 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
2756 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
2757 std::move(Abbv)) != VST_ENTRY_6_ABBREV)
2758 assert(false && "Unexpected abbrev ordering!");
2759 }
2760 { // 6-bit char6 VST_BBENTRY strings.
2761 auto Abbv = std::make_shared<BitCodeAbbrev>();
2762 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_BBENTRY));
2763 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
2764 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2765 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
2766 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
2767 std::move(Abbv)) != VST_BBENTRY_6_ABBREV)
2768 assert(false && "Unexpected abbrev ordering!");
2769 }
2770
2771 { // SETTYPE abbrev for CONSTANTS_BLOCK.
2772 auto Abbv = std::make_shared<BitCodeAbbrev>();
2773 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_SETTYPE));
2774 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
2775 VE.computeBitsRequiredForTypeIndices()));
2776 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, std::move(Abbv)) !=
2777 CONSTANTS_SETTYPE_ABBREV)
2778 assert(false && "Unexpected abbrev ordering!");
2779 }
2780
2781 { // INTEGER abbrev for CONSTANTS_BLOCK.
2782 auto Abbv = std::make_shared<BitCodeAbbrev>();
2783 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_INTEGER));
2784 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
2785 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, std::move(Abbv)) !=
2786 CONSTANTS_INTEGER_ABBREV)
2787 assert(false && "Unexpected abbrev ordering!");
2788 }
2789
2790 { // CE_CAST abbrev for CONSTANTS_BLOCK.
2791 auto Abbv = std::make_shared<BitCodeAbbrev>();
2792 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CE_CAST));
2793 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // cast opc
2794 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // typeid
2795 VE.computeBitsRequiredForTypeIndices()));
2796 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id
2797
2798 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, std::move(Abbv)) !=
2799 CONSTANTS_CE_CAST_Abbrev)
2800 assert(false && "Unexpected abbrev ordering!");
2801 }
2802 { // NULL abbrev for CONSTANTS_BLOCK.
2803 auto Abbv = std::make_shared<BitCodeAbbrev>();
2804 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_NULL));
2805 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, std::move(Abbv)) !=
2806 CONSTANTS_NULL_Abbrev)
2807 assert(false && "Unexpected abbrev ordering!");
2808 }
2809
2810 // FIXME: This should only use space for first class types!
2811
2812 { // INST_LOAD abbrev for FUNCTION_BLOCK.
2813 auto Abbv = std::make_shared<BitCodeAbbrev>();
2814 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_LOAD));
2815 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Ptr
2816 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // dest ty
2817 VE.computeBitsRequiredForTypeIndices()));
2818 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // Align
2819 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // volatile
2820 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, std::move(Abbv)) !=
2821 (unsigned)FUNCTION_INST_LOAD_ABBREV)
2822 assert(false && "Unexpected abbrev ordering!");
2823 }
2824 { // INST_BINOP abbrev for FUNCTION_BLOCK.
2825 auto Abbv = std::make_shared<BitCodeAbbrev>();
2826 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_BINOP));
2827 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS
2828 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // RHS
2829 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
2830 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, std::move(Abbv)) !=
2831 (unsigned)FUNCTION_INST_BINOP_ABBREV)
2832 assert(false && "Unexpected abbrev ordering!");
2833 }
2834 { // INST_BINOP_FLAGS abbrev for FUNCTION_BLOCK.
2835 auto Abbv = std::make_shared<BitCodeAbbrev>();
2836 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_BINOP));
2837 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS
2838 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // RHS
2839 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
2840 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7)); // flags
2841 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, std::move(Abbv)) !=
2842 (unsigned)FUNCTION_INST_BINOP_FLAGS_ABBREV)
2843 assert(false && "Unexpected abbrev ordering!");
2844 }
2845 { // INST_CAST abbrev for FUNCTION_BLOCK.
2846 auto Abbv = std::make_shared<BitCodeAbbrev>();
2847 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_CAST));
2848 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // OpVal
2849 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // dest ty
2850 VE.computeBitsRequiredForTypeIndices()));
2851 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
2852 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, std::move(Abbv)) !=
2853 (unsigned)FUNCTION_INST_CAST_ABBREV)
2854 assert(false && "Unexpected abbrev ordering!");
2855 }
2856
2857 { // INST_RET abbrev for FUNCTION_BLOCK.
2858 auto Abbv = std::make_shared<BitCodeAbbrev>();
2859 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET));
2860 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, std::move(Abbv)) !=
2861 (unsigned)FUNCTION_INST_RET_VOID_ABBREV)
2862 assert(false && "Unexpected abbrev ordering!");
2863 }
2864 { // INST_RET abbrev for FUNCTION_BLOCK.
2865 auto Abbv = std::make_shared<BitCodeAbbrev>();
2866 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET));
2867 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ValID
2868 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, std::move(Abbv)) !=
2869 (unsigned)FUNCTION_INST_RET_VAL_ABBREV)
2870 assert(false && "Unexpected abbrev ordering!");
2871 }
2872 { // INST_UNREACHABLE abbrev for FUNCTION_BLOCK.
2873 auto Abbv = std::make_shared<BitCodeAbbrev>();
2874 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_UNREACHABLE));
2875 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, std::move(Abbv)) !=
2876 (unsigned)FUNCTION_INST_UNREACHABLE_ABBREV)
2877 assert(false && "Unexpected abbrev ordering!");
2878 }
2879 {
2880 auto Abbv = std::make_shared<BitCodeAbbrev>();
2881 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_GEP));
2882 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
2883 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // dest ty
2884 Log2_32_Ceil(VE.getTypes().size() + 1)));
2885 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2886 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
2887 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, std::move(Abbv)) !=
2888 (unsigned)FUNCTION_INST_GEP_ABBREV)
2889 assert(false && "Unexpected abbrev ordering!");
2890 }
2891
2892 Stream.ExitBlock();
2893}
2894
2895void DXILBitcodeWriter::writeModuleVersion() {
2896 // VERSION: [version#]
2897 Stream.EmitRecord(bitc::MODULE_CODE_VERSION, ArrayRef<unsigned>{1});
2898}
2899
2900/// WriteModule - Emit the specified module to the bitstream.
2902 // The identification block is new since llvm-3.7, but the old bitcode reader
2903 // will skip it.
2904 // writeIdentificationBlock(Stream);
2905
2906 Stream.EnterSubblock(bitc::MODULE_BLOCK_ID, 3);
2907
2908 // It is redundant to fully-specify this here, but nice to make it explicit
2909 // so that it is clear the DXIL module version is different.
2910 DXILBitcodeWriter::writeModuleVersion();
2911
2912 // Emit blockinfo, which defines the standard abbreviations etc.
2913 writeBlockInfo();
2914
2915 // Emit information about attribute groups.
2916 writeAttributeGroupTable();
2917
2918 // Emit information about parameter attributes.
2919 writeAttributeTable();
2920
2921 // Emit information describing all of the types in the module.
2922 writeTypeTable();
2923
2924 writeComdats();
2925
2926 // Emit top-level description of module, including target triple, inline asm,
2927 // descriptors for global variables, and function prototype info.
2928 writeModuleInfo();
2929
2930 // Emit constants.
2931 writeModuleConstants();
2932
2933 // Emit metadata.
2934 writeModuleMetadataKinds();
2935
2936 // Emit metadata.
2937 writeModuleMetadata();
2938
2939 // Emit names for globals/functions etc.
2940 // DXIL uses the same format for module-level value symbol table as for the
2941 // function level table.
2942 writeFunctionLevelValueSymbolTable(M.getValueSymbolTable());
2943
2944 // Emit function bodies.
2945 for (const Function &F : M)
2946 if (!F.isDeclaration())
2947 writeFunction(F);
2948
2949 Stream.ExitBlock();
2950}
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:406
bool hasAttributes() const
Return true if attributes exists in this set.
Definition Attributes.h:484
Functions, function parameters, and return types can have attributes to indicate how they should be t...
Definition Attributes.h:106
AttrKind
This enumeration lists the attributes that can be associated with parameters, function results,...
Definition Attributes.h:125
@ TombstoneKey
Use as Tombstone key for DenseMap of AttrKind.
Definition Attributes.h:132
@ None
No attributes have been set.
Definition Attributes.h:127
@ EmptyKey
Use as Empty key for DenseMap of AttrKind.
Definition Attributes.h:131
@ EndAttrKinds
Sentinel value useful for loops.
Definition Attributes.h:130
InstListType::const_iterator const_iterator
Definition BasicBlock.h:171
BitCodeAbbrevOp - This describes one or more operands in an abbreviation.
Definition BitCodes.h:34
static bool isChar6(char C)
isChar6 - Return true if this character is legal in the Char6 encoding.
Definition BitCodes.h:88
unsigned EmitAbbrev(std::shared_ptr< BitCodeAbbrev > Abbv)
Emits the abbreviation Abbv to the stream.
void EmitRecordWithBlob(unsigned Abbrev, const Container &Vals, StringRef Blob)
EmitRecordWithBlob - Emit the specified record to the stream, using an abbrev that includes a blob at...
void EnterSubblock(unsigned BlockID, unsigned CodeLen)
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation or the function signa...
CallingConv::ID getCallingConv() const
Value * getCalledOperand() const
Value * getArgOperand(unsigned i) const
FunctionType * getFunctionType() const
unsigned arg_size() const
AttributeList getAttributes() const
Return the attributes for this call.
bool isTailCall() const
bool isMustTailCall() const
@ Largest
The linker will choose the largest COMDAT.
Definition Comdat.h:39
@ SameSize
The data referenced by the COMDAT must be the same size.
Definition Comdat.h:41
@ Any
The linker may choose any COMDAT.
Definition Comdat.h:37
@ NoDeduplicate
No deduplication is performed.
Definition Comdat.h:40
@ ExactMatch
The data referenced by the COMDAT must be the same.
Definition Comdat.h:38
iterator find(const_arg_type_t< KeyT > Val)
Definition DenseMap.h: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:763
const unsigned char * bytes_end() const
Definition Metadata.h:764
bool doesNotAccessMemory() const
Whether this function accesses no memory.
Definition ModRef.h:246
bool onlyAccessesArgPointees() const
Whether this function only (at most) accesses argument memory.
Definition ModRef.h:255
bool onlyReadsMemory() const
Whether this function only (at most) reads memory.
Definition ModRef.h:249
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:68
BasicBlock * getIncomingBlock(unsigned i) const
Return incoming basic block number i.
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
unsigned getNumIncomingValues() const
Return the number of incoming edges.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void reserve(size_type N)
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
iterator insert(iterator I, T &&Elt)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
constexpr bool empty() const
Check if the string is empty.
Definition StringRef.h:141
iterator begin() const
Definition StringRef.h:114
constexpr size_t size() const
Get the string size.
Definition StringRef.h:144
iterator end() const
Definition StringRef.h:116
Utility for building string tables with deduplicated suffixes.
Triple - Helper class for working with autoconf configuration names.
Definition Triple.h:48
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
bool isX86_FP80Ty() const
Return true if this is x86 long double.
Definition Type.h:161
bool isFloatTy() const
Return true if this is 'float', a 32-bit IEEE fp type.
Definition Type.h:155
@ X86_AMXTyID
AMX vectors (8192 bits, X86 specific)
Definition Type.h:67
@ FunctionTyID
Functions.
Definition Type.h:73
@ ArrayTyID
Arrays.
Definition Type.h:76
@ TypedPointerTyID
Typed pointer used by some GPU targets.
Definition Type.h:79
@ HalfTyID
16-bit floating point type
Definition Type.h:57
@ TargetExtTyID
Target extension type.
Definition Type.h:80
@ VoidTyID
type with no size
Definition Type.h:64
@ ScalableVectorTyID
Scalable SIMD vector type.
Definition Type.h:78
@ LabelTyID
Labels.
Definition Type.h:65
@ FloatTyID
32-bit floating point type
Definition Type.h:59
@ StructTyID
Structures.
Definition Type.h:75
@ IntegerTyID
Arbitrary bit width integers.
Definition Type.h:71
@ FixedVectorTyID
Fixed width SIMD vector type.
Definition Type.h:77
@ BFloatTyID
16-bit floating point type (7-bit significand)
Definition Type.h:58
@ DoubleTyID
64-bit floating point type
Definition Type.h:60
@ X86_FP80TyID
80-bit floating point type (X87)
Definition Type.h:61
@ PPC_FP128TyID
128-bit floating point type (two 64-bits, PowerPC)
Definition Type.h:63
@ MetadataTyID
Metadata.
Definition Type.h:66
@ TokenTyID
Tokens.
Definition Type.h:68
@ ByteTyID
Arbitrary bit width bytes.
Definition Type.h:72
@ PointerTyID
Pointers.
Definition Type.h:74
@ FP128TyID
128-bit floating point type (112-bit significand)
Definition Type.h:62
bool isPPC_FP128Ty() const
Return true if this is powerpc long double.
Definition Type.h:167
bool isFP128Ty() const
Return true if this is 'fp128'.
Definition Type.h:164
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition Type.h:363
bool isHalfTy() const
Return true if this is 'half', a 16-bit IEEE fp type.
Definition Type.h:144
bool isDoubleTy() const
Return true if this is 'double', a 64-bit IEEE fp type.
Definition Type.h:158
A few GPU targets, such as DXIL and SPIR-V, have typed pointers.
Type * getElementType() const
static LLVM_ABI TypedPointerType * get(Type *ElementType, unsigned AddressSpace)
This constructs a pointer to an object of the specified type in a numbered address space.
unsigned getAddressSpace() const
Return the address space of the Pointer type.
Value * getValue() const
Definition Metadata.h:510
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:257
BitcodeWriter(SmallVectorImpl< char > &Buffer)
Create a BitcodeWriter that writes to Buffer.
void writeModule(const Module &M, const DXILDebugInfoMap &DebugInfo)
Write the specified module to the buffer specified at construction time.
static void emitWideAPInt(SmallVectorImpl< uint64_t > &Vals, const APInt &A)
static unsigned getEncodedThreadLocalMode(const GlobalValue &GV)
static unsigned getEncodedCastOpcode(unsigned Opcode)
Begin dxil::BitcodeWriterBase Implementation.
static void writeStringRecord(BitstreamWriter &Stream, unsigned Code, StringRef Str, unsigned AbbrevToUse)
static uint64_t getAttrKindEncoding(Attribute::AttrKind Kind)
static unsigned getEncodedDLLStorageClass(const GlobalValue &GV)
static unsigned getEncodedOrdering(AtomicOrdering Ordering)
static unsigned getEncodedLinkage(const GlobalValue::LinkageTypes Linkage)
static unsigned getEncodedVisibility(const GlobalValue &GV)
void write()
Emit the current module to the bitstream.
static unsigned getEncodedBinaryOpcode(unsigned Opcode)
static void emitSignedInt64(SmallVectorImpl< uint64_t > &Vals, uint64_t V)
static unsigned getEncodedUnaryOpcode(unsigned Opcode)
DXILBitcodeWriter(const Module &M, SmallVectorImpl< char > &Buffer, StringTableBuilder &StrtabBuilder, BitstreamWriter &Stream, const DXILDebugInfoMap &DebugInfo)
Constructs a ModuleBitcodeWriter object for the given Module, writing to the provided Buffer.
static unsigned getEncodedRMWOperation(AtomicRMWInst::BinOp Op)
static unsigned getEncodedComdatSelectionKind(const Comdat &C)
static uint64_t getOptimizationFlags(const Value *V)
std::pair< unsigned, AttributeSet > IndexAndAttrSet
Attribute groups as encoded in bitcode are almost AttributeSets, but they include the AttributeList i...
std::vector< std::pair< const Value *, unsigned > > ValueList
std::vector< Type * > TypeList
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
This file contains the declaration of the Comdat class, which represents a single COMDAT in LLVM.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char Attrs[]
Key for Kernel::Metadata::mAttrs.
@ Entry
Definition COFF.h:862
Predicate getPredicate(unsigned Condition, unsigned Hint)
Return predicate consisting of specified condition and hint bits.
@ 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.