LLVM 20.0.0git
Utility.cpp
Go to the documentation of this file.
1//===- Utility.cpp ------ Collection of generic offloading utilities ------===//
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
13#include "llvm/IR/Constants.h"
14#include "llvm/IR/GlobalValue.h"
16#include "llvm/IR/Value.h"
20
21using namespace llvm;
22using namespace llvm::offloading;
23
25 LLVMContext &C = M.getContext();
27 StructType::getTypeByName(C, "struct.__tgt_offload_entry");
28 if (!EntryTy)
30 "struct.__tgt_offload_entry", Type::getInt64Ty(C), Type::getInt16Ty(C),
31 Type::getInt16Ty(C), Type::getInt32Ty(C), PointerType::getUnqual(C),
32 PointerType::getUnqual(C), Type::getInt64Ty(C), Type::getInt64Ty(C),
33 PointerType::getUnqual(C));
34 return EntryTy;
35}
36
37std::pair<Constant *, GlobalVariable *>
40 uint64_t Size, uint32_t Flags,
41 uint64_t Data, Constant *AuxAddr) {
42 llvm::Triple Triple(M.getTargetTriple());
43 Type *PtrTy = PointerType::getUnqual(M.getContext());
44 Type *Int64Ty = Type::getInt64Ty(M.getContext());
45 Type *Int32Ty = Type::getInt32Ty(M.getContext());
46 Type *Int16Ty = Type::getInt16Ty(M.getContext());
47
48 Constant *AddrName = ConstantDataArray::getString(M.getContext(), Name);
49
50 StringRef Prefix =
51 Triple.isNVPTX() ? "$offloading$entry_name" : ".offloading.entry_name";
52
53 // Create the constant string used to look up the symbol in the device.
54 auto *Str =
55 new GlobalVariable(M, AddrName->getType(), /*isConstant=*/true,
56 GlobalValue::InternalLinkage, AddrName, Prefix);
57 StringRef SectionName = ".llvm.rodata.offloading";
58 Str->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
59 Str->setSection(SectionName);
60 Str->setAlignment(Align(1));
61
62 // Make a metadata node for these constants so it can be queried from IR.
63 NamedMDNode *MD = M.getOrInsertNamedMetadata("llvm.offloading.symbols");
64 Metadata *MDVals[] = {ConstantAsMetadata::get(Str)};
65 MD->addOperand(llvm::MDNode::get(M.getContext(), MDVals));
66
67 // Construct the offloading entry.
68 Constant *EntryData[] = {
69 ConstantExpr::getNullValue(Int64Ty),
70 ConstantInt::get(Int16Ty, 1),
71 ConstantInt::get(Int16Ty, Kind),
72 ConstantInt::get(Int32Ty, Flags),
75 ConstantInt::get(Int64Ty, Size),
76 ConstantInt::get(Int64Ty, Data),
78 : ConstantExpr::getNullValue(PtrTy)};
79 Constant *EntryInitializer = ConstantStruct::get(getEntryTy(M), EntryData);
80 return {EntryInitializer, Str};
81}
82
85 uint64_t Size, uint32_t Flags,
87 Constant *AuxAddr) {
88 llvm::Triple Triple(M.getTargetTriple());
89
90 auto [EntryInitializer, NameGV] = getOffloadingEntryInitializer(
91 M, Kind, Addr, Name, Size, Flags, Data, AuxAddr);
92
93 StringRef Prefix =
94 Triple.isNVPTX() ? "$offloading$entry$" : ".offloading.entry.";
95 auto *Entry = new GlobalVariable(
96 M, getEntryTy(M),
97 /*isConstant=*/true, GlobalValue::WeakAnyLinkage, EntryInitializer,
98 Prefix + Name, nullptr, GlobalValue::NotThreadLocal,
99 M.getDataLayout().getDefaultGlobalsAddressSpace());
100
101 // The entry has to be created in the section the linker expects it to be.
103 Entry->setSection((SectionName + "$OE").str());
104 else
105 Entry->setSection(SectionName);
106 Entry->setAlignment(Align(1));
107}
108
109std::pair<GlobalVariable *, GlobalVariable *>
111 llvm::Triple Triple(M.getTargetTriple());
112
113 auto *ZeroInitilaizer =
114 ConstantAggregateZero::get(ArrayType::get(getEntryTy(M), 0u));
115 auto *EntryInit = Triple.isOSBinFormatCOFF() ? ZeroInitilaizer : nullptr;
116 auto *EntryType = ArrayType::get(getEntryTy(M), 0);
119
120 auto *EntriesB =
121 new GlobalVariable(M, EntryType, /*isConstant=*/true, Linkage, EntryInit,
122 "__start_" + SectionName);
123 EntriesB->setVisibility(GlobalValue::HiddenVisibility);
124 auto *EntriesE =
125 new GlobalVariable(M, EntryType, /*isConstant=*/true, Linkage, EntryInit,
126 "__stop_" + SectionName);
127 EntriesE->setVisibility(GlobalValue::HiddenVisibility);
128
129 if (Triple.isOSBinFormatELF()) {
130 // We assume that external begin/end symbols that we have created above will
131 // be defined by the linker. This is done whenever a section name with a
132 // valid C-identifier is present. We define a dummy variable here to force
133 // the linker to always provide these symbols.
134 auto *DummyEntry = new GlobalVariable(
135 M, ZeroInitilaizer->getType(), true, GlobalVariable::InternalLinkage,
136 ZeroInitilaizer, "__dummy." + SectionName);
137 DummyEntry->setSection(SectionName);
138 appendToCompilerUsed(M, DummyEntry);
139 } else {
140 // The COFF linker will merge sections containing a '$' together into a
141 // single section. The order of entries in this section will be sorted
142 // alphabetically by the characters following the '$' in the name. Set the
143 // sections here to ensure that the beginning and end symbols are sorted.
144 EntriesB->setSection((SectionName + "$OA").str());
145 EntriesE->setSection((SectionName + "$OZ").str());
146 }
147
148 return std::make_pair(EntriesB, EntriesE);
149}
150
152 uint32_t ImageFlags,
153 StringRef EnvTargetID) {
154 using namespace llvm::ELF;
155 StringRef EnvArch = EnvTargetID.split(":").first;
156
157 // Trivial check if the base processors match.
158 if (EnvArch != ImageArch)
159 return false;
160
161 // Check if the image is requesting xnack on or off.
162 switch (ImageFlags & EF_AMDGPU_FEATURE_XNACK_V4) {
163 case EF_AMDGPU_FEATURE_XNACK_OFF_V4:
164 // The image is 'xnack-' so the environment must be 'xnack-'.
165 if (!EnvTargetID.contains("xnack-"))
166 return false;
167 break;
168 case EF_AMDGPU_FEATURE_XNACK_ON_V4:
169 // The image is 'xnack+' so the environment must be 'xnack+'.
170 if (!EnvTargetID.contains("xnack+"))
171 return false;
172 break;
173 case EF_AMDGPU_FEATURE_XNACK_UNSUPPORTED_V4:
174 case EF_AMDGPU_FEATURE_XNACK_ANY_V4:
175 default:
176 break;
177 }
178
179 // Check if the image is requesting sramecc on or off.
180 switch (ImageFlags & EF_AMDGPU_FEATURE_SRAMECC_V4) {
181 case EF_AMDGPU_FEATURE_SRAMECC_OFF_V4:
182 // The image is 'sramecc-' so the environment must be 'sramecc-'.
183 if (!EnvTargetID.contains("sramecc-"))
184 return false;
185 break;
186 case EF_AMDGPU_FEATURE_SRAMECC_ON_V4:
187 // The image is 'sramecc+' so the environment must be 'sramecc+'.
188 if (!EnvTargetID.contains("sramecc+"))
189 return false;
190 break;
191 case EF_AMDGPU_FEATURE_SRAMECC_UNSUPPORTED_V4:
192 case EF_AMDGPU_FEATURE_SRAMECC_ANY_V4:
193 break;
194 }
195
196 return true;
197}
198
199namespace {
200/// Reads the AMDGPU specific per-kernel-metadata from an image.
201class KernelInfoReader {
202public:
204 : KernelInfoMap(KIM) {}
205
206 /// Process ELF note to read AMDGPU metadata from respective information
207 /// fields.
208 Error processNote(const llvm::object::ELF64LE::Note &Note, size_t Align) {
209 if (Note.getName() != "AMDGPU")
210 return Error::success(); // We are not interested in other things
211
212 assert(Note.getType() == ELF::NT_AMDGPU_METADATA &&
213 "Parse AMDGPU MetaData");
214 auto Desc = Note.getDesc(Align);
215 StringRef MsgPackString =
216 StringRef(reinterpret_cast<const char *>(Desc.data()), Desc.size());
217 msgpack::Document MsgPackDoc;
218 if (!MsgPackDoc.readFromBlob(MsgPackString, /*Multi=*/false))
219 return Error::success();
220
222 if (!Verifier.verify(MsgPackDoc.getRoot()))
223 return Error::success();
224
225 auto RootMap = MsgPackDoc.getRoot().getMap(true);
226
227 if (auto Err = iterateAMDKernels(RootMap))
228 return Err;
229
230 return Error::success();
231 }
232
233private:
234 /// Extracts the relevant information via simple string look-up in the msgpack
235 /// document elements.
236 Error
237 extractKernelData(msgpack::MapDocNode::MapTy::value_type V,
238 std::string &KernelName,
240 if (!V.first.isString())
241 return Error::success();
242
243 const auto IsKey = [](const msgpack::DocNode &DK, StringRef SK) {
244 return DK.getString() == SK;
245 };
246
247 const auto GetSequenceOfThreeInts = [](msgpack::DocNode &DN,
248 uint32_t *Vals) {
249 assert(DN.isArray() && "MsgPack DocNode is an array node");
250 auto DNA = DN.getArray();
251 assert(DNA.size() == 3 && "ArrayNode has at most three elements");
252
253 int I = 0;
254 for (auto DNABegin = DNA.begin(), DNAEnd = DNA.end(); DNABegin != DNAEnd;
255 ++DNABegin) {
256 Vals[I++] = DNABegin->getUInt();
257 }
258 };
259
260 if (IsKey(V.first, ".name")) {
261 KernelName = V.second.toString();
262 } else if (IsKey(V.first, ".sgpr_count")) {
263 KernelData.SGPRCount = V.second.getUInt();
264 } else if (IsKey(V.first, ".sgpr_spill_count")) {
265 KernelData.SGPRSpillCount = V.second.getUInt();
266 } else if (IsKey(V.first, ".vgpr_count")) {
267 KernelData.VGPRCount = V.second.getUInt();
268 } else if (IsKey(V.first, ".vgpr_spill_count")) {
269 KernelData.VGPRSpillCount = V.second.getUInt();
270 } else if (IsKey(V.first, ".agpr_count")) {
271 KernelData.AGPRCount = V.second.getUInt();
272 } else if (IsKey(V.first, ".private_segment_fixed_size")) {
273 KernelData.PrivateSegmentSize = V.second.getUInt();
274 } else if (IsKey(V.first, ".group_segment_fixed_size")) {
275 KernelData.GroupSegmentList = V.second.getUInt();
276 } else if (IsKey(V.first, ".reqd_workgroup_size")) {
277 GetSequenceOfThreeInts(V.second, KernelData.RequestedWorkgroupSize);
278 } else if (IsKey(V.first, ".workgroup_size_hint")) {
279 GetSequenceOfThreeInts(V.second, KernelData.WorkgroupSizeHint);
280 } else if (IsKey(V.first, ".wavefront_size")) {
281 KernelData.WavefrontSize = V.second.getUInt();
282 } else if (IsKey(V.first, ".max_flat_workgroup_size")) {
283 KernelData.MaxFlatWorkgroupSize = V.second.getUInt();
284 }
285
286 return Error::success();
287 }
288
289 /// Get the "amdhsa.kernels" element from the msgpack Document
291 auto Res = MDN.find("amdhsa.kernels");
292 if (Res == MDN.end())
294 "Could not find amdhsa.kernels key");
295
296 auto Pair = *Res;
297 assert(Pair.second.isArray() &&
298 "AMDGPU kernel entries are arrays of entries");
299
300 return Pair.second.getArray();
301 }
302
303 /// Iterate all entries for one "amdhsa.kernels" entry. Each entry is a
304 /// MapDocNode that either maps a string to a single value (most of them) or
305 /// to another array of things. Currently, we only handle the case that maps
306 /// to scalar value.
307 Error generateKernelInfo(msgpack::ArrayDocNode::ArrayTy::iterator It) {
309 std::string KernelName;
310 auto Entry = (*It).getMap();
311 for (auto MI = Entry.begin(), E = Entry.end(); MI != E; ++MI)
312 if (auto Err = extractKernelData(*MI, KernelName, KernelData))
313 return Err;
314
315 KernelInfoMap.insert({KernelName, KernelData});
316 return Error::success();
317 }
318
319 /// Go over the list of AMD kernels in the "amdhsa.kernels" entry
320 Error iterateAMDKernels(msgpack::MapDocNode &MDN) {
321 auto KernelsOrErr = getAMDKernelsArray(MDN);
322 if (auto Err = KernelsOrErr.takeError())
323 return Err;
324
325 auto KernelsArr = *KernelsOrErr;
326 for (auto It = KernelsArr.begin(), E = KernelsArr.end(); It != E; ++It) {
327 if (!It->isMap())
328 continue; // we expect <key,value> pairs
329
330 // Obtain the value for the different entries. Each array entry is a
331 // MapDocNode
332 if (auto Err = generateKernelInfo(It))
333 return Err;
334 }
335 return Error::success();
336 }
337
338 // Kernel names are the keys
340};
341} // namespace
342
344 MemoryBufferRef MemBuffer,
346 uint16_t &ELFABIVersion) {
347 Error Err = Error::success(); // Used later as out-parameter
348
349 auto ELFOrError = object::ELF64LEFile::create(MemBuffer.getBuffer());
350 if (auto Err = ELFOrError.takeError())
351 return Err;
352
353 const object::ELF64LEFile ELFObj = ELFOrError.get();
355 if (!Sections)
356 return Sections.takeError();
357 KernelInfoReader Reader(KernelInfoMap);
358
359 // Read the code object version from ELF image header
360 auto Header = ELFObj.getHeader();
361 ELFABIVersion = (uint8_t)(Header.e_ident[ELF::EI_ABIVERSION]);
362 for (const auto &S : *Sections) {
363 if (S.sh_type != ELF::SHT_NOTE)
364 continue;
365
366 for (const auto N : ELFObj.notes(S, Err)) {
367 if (Err)
368 return Err;
369 // Fills the KernelInfoTabel entries in the reader
370 if ((Err = Reader.processNote(N, S.sh_addralign)))
371 return Err;
372 }
373 }
374 return Error::success();
375}
This is a verifier for AMDGPU HSA metadata, which can verify both well-typed metadata and untyped met...
This file contains the declarations for the subclasses of Constant, which represent the different fla...
uint64_t Addr
std::string Name
uint64_t Size
IRTranslator LLVM IR MI
#define I(x, y, z)
Definition: MD5.cpp:58
This file declares a class that exposes a simple in-memory representation of a document of MsgPack ob...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
verify safepoint Safepoint IR Verifier
static ConstantAggregateZero * get(Type *Ty)
Definition: Constants.cpp:1672
static ConstantAsMetadata * get(Constant *C)
Definition: Metadata.h:532
static Constant * getString(LLVMContext &Context, StringRef Initializer, bool AddNull=true)
This method constructs a CDS and initializes it with a text string.
Definition: Constants.cpp:2991
static Constant * getPointerBitCastOrAddrSpaceCast(Constant *C, Type *Ty)
Create a BitCast or AddrSpaceCast for a pointer type depending on the address space.
Definition: Constants.cpp:2268
static Constant * get(StructType *T, ArrayRef< Constant * > V)
Definition: Constants.cpp:1378
This is an important base class in LLVM.
Definition: Constant.h:42
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
static ErrorSuccess success()
Create a success value.
Definition: Error.h:337
Tagged union holding either a T or a Error.
Definition: Error.h:481
Error takeError()
Take ownership of the stored error.
Definition: Error.h:608
@ HiddenVisibility
The GV is hidden.
Definition: GlobalValue.h:68
@ InternalLinkage
Rename collisions when linking (static functions).
Definition: GlobalValue.h:59
@ WeakODRLinkage
Same, but only replaced by something equivalent.
Definition: GlobalValue.h:57
@ ExternalLinkage
Externally visible function.
Definition: GlobalValue.h:52
@ WeakAnyLinkage
Keep one copy of named function when linking (weak)
Definition: GlobalValue.h:56
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition: Metadata.h:1549
StringRef getBuffer() const
Root of the metadata hierarchy.
Definition: Metadata.h:62
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
A tuple of MDNodes.
Definition: Metadata.h:1737
void addOperand(MDNode *M)
Definition: Metadata.cpp:1431
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition: StringMap.h:128
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
Definition: StringRef.h:700
bool contains(StringRef Other) const
Return true if the given string is a substring of *this, and false otherwise.
Definition: StringRef.h:424
Class to represent struct types.
Definition: DerivedTypes.h:218
static StructType * getTypeByName(LLVMContext &C, StringRef Name)
Return the type with the specified name, or null if there is none by that name.
Definition: Type.cpp:731
static StructType * create(LLVMContext &Context, StringRef Name)
This creates an identified struct.
Definition: Type.cpp:612
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
bool isOSBinFormatCOFF() const
Tests whether the OS uses the COFF binary format.
Definition: Triple.h:758
bool isNVPTX() const
Tests whether the target is NVPTX (32- or 64-bit).
Definition: Triple.h:881
bool isOSBinFormatELF() const
Tests whether the OS uses the ELF binary format.
Definition: Triple.h:753
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
static IntegerType * getInt16Ty(LLVMContext &C)
static IntegerType * getInt32Ty(LLVMContext &C)
static IntegerType * getInt64Ty(LLVMContext &C)
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
A node in a MsgPack Document.
MapDocNode & getMap(bool Convert=false)
Get a MapDocNode for a map node.
ArrayDocNode & getArray(bool Convert=false)
Get an ArrayDocNode for an array node.
StringRef getString() const
Simple in-memory representation of a document of msgpack objects with ability to find and create arra...
DocNode & getRoot()
Get ref to the document's root element.
bool readFromBlob(StringRef Blob, bool Multi, function_ref< int(DocNode *DestNode, DocNode SrcNode, DocNode MapKey)> Merger=[](DocNode *DestNode, DocNode SrcNode, DocNode MapKey) { return -1;})
Read a document from a binary msgpack blob, merging into anything already in the Document.
A DocNode that is a map.
MapTy::iterator find(DocNode Key)
const Elf_Ehdr & getHeader() const
Definition: ELF.h:279
static Expected< ELFFile > create(StringRef Object)
Definition: ELF.h:888
iterator_range< Elf_Note_Iterator > notes(const Elf_Phdr &Phdr, Error &Err) const
Get an iterator range over notes of a program header.
Definition: ELF.h:462
Expected< Elf_Shdr_Range > sections() const
Definition: ELF.h:925
@ Entry
Definition: COFF.h:844
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
Definition: ELF.h:28
@ EI_ABIVERSION
Definition: ELF.h:57
@ SHT_NOTE
Definition: ELF.h:1104
@ NT_AMDGPU_METADATA
Definition: ELF.h:1923
OffloadKind
The producer of the associated offloading image.
Definition: OffloadBinary.h:33
Error getAMDGPUMetaDataFromImage(MemoryBufferRef MemBuffer, StringMap< AMDGPUKernelMetaData > &KernelInfoMap, uint16_t &ELFABIVersion)
Reads AMDGPU specific metadata from the ELF file and propagates the KernelInfoMap.
Definition: Utility.cpp:343
bool isImageCompatibleWithEnv(StringRef ImageArch, uint32_t ImageFlags, StringRef EnvTargetID)
Check if an image is compatible with current system's environment.
Definition: Utility.cpp:151
void emitOffloadingEntry(Module &M, object::OffloadKind Kind, Constant *Addr, StringRef Name, uint64_t Size, uint32_t Flags, uint64_t Data, StringRef SectionName, Constant *AuxAddr=nullptr)
Create an offloading section struct used to register this global at runtime.
Definition: Utility.cpp:83
std::pair< Constant *, GlobalVariable * > getOffloadingEntryInitializer(Module &M, object::OffloadKind Kind, Constant *Addr, StringRef Name, uint64_t Size, uint32_t Flags, uint64_t Data, Constant *AuxAddr)
Create a constant struct initializer used to register this global at runtime.
Definition: Utility.cpp:38
StructType * getEntryTy(Module &M)
Returns the type of the offloading entry we use to store kernels and globals that will be registered ...
Definition: Utility.cpp:24
std::pair< GlobalVariable *, GlobalVariable * > getOffloadEntryArray(Module &M, StringRef SectionName)
Creates a pair of globals used to iterate the array of offloading entries by accessing the section va...
Definition: Utility.cpp:110
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition: Error.cpp:98
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition: Error.h:1291
void appendToCompilerUsed(Module &M, ArrayRef< GlobalValue * > Values)
Adds global values to the llvm.compiler.used list.
#define N
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
Description of the encoding of one expression Op.
This is the record of an object that just be registered with the offloading runtime.
Definition: Utility.h:26
Struct for holding metadata related to AMDGPU kernels, for more information about the metadata and it...
Definition: Utility.h:117
uint32_t SGPRSpillCount
Number of stores from a scalar register to a register allocator created spill location.
Definition: Utility.h:132
uint32_t SGPRCount
Number of scalar registers required by a wavefront.
Definition: Utility.h:127
uint32_t VGPRSpillCount
Number of stores from a vector register to a register allocator created spill location.
Definition: Utility.h:135
uint32_t VGPRCount
Number of vector registers required by each work-item.
Definition: Utility.h:129
uint32_t PrivateSegmentSize
The amount of fixed private address space memory required for a work-item in bytes.
Definition: Utility.h:125
uint32_t GroupSegmentList
The amount of group segment memory required by a work-group in bytes.
Definition: Utility.h:122
uint32_t MaxFlatWorkgroupSize
Maximum flat work-group size supported by the kernel in work-items.
Definition: Utility.h:146
uint32_t WorkgroupSizeHint[3]
Corresponds to the OpenCL work_group_size_hint attribute.
Definition: Utility.h:142
uint32_t AGPRCount
Number of accumulator registers required by each work-item.
Definition: Utility.h:137
uint32_t RequestedWorkgroupSize[3]
Corresponds to the OpenCL reqd_work_group_size attribute.
Definition: Utility.h:139