[llvm][IR] Add per-global code model attribute (#72077)

This adds a per-global code model attribute, which can override the
target's code model to access global variables.

Suggested-by: Arthur Eubanks <aeubanks@google.com>
Link: https://discourse.llvm.org/t/how-to-best-implement-code-model-overriding-for-certain-values/71816
Link: https://discourse.llvm.org/t/rfc-add-per-global-code-model-attribute/74944
This commit is contained in:
hev authored and GitHub committed 2023-12-05 09:42:53 +08:00
1 parent 192439db6e
commit a8874cf50b
13 files changed
+156 -2

No files matched your search

+25
View File
@@ -1144,6 +1144,23 @@ static bool getDecodedDSOLocal(unsigned Val) {
}
}
static std::optional<CodeModel::Model> getDecodedCodeModel(unsigned Val) {
switch (Val) {
case 1:
return CodeModel::Tiny;
case 2:
return CodeModel::Small;
case 3:
return CodeModel::Kernel;
case 4:
return CodeModel::Medium;
case 5:
return CodeModel::Large;
}
return {};
}
static GlobalVariable::ThreadLocalMode getDecodedThreadLocalMode(unsigned Val) {
switch (Val) {
case 0: return GlobalVariable::NotThreadLocal;
@@ -3805,6 +3822,7 @@ Error BitcodeReader::parseGlobalVarRecord(ArrayRef<uint64_t> Record) {
// dllstorageclass, comdat, attributes, preemption specifier,
// partition strtab offset, partition strtab size] (name in VST)
// v2: [strtab_offset, strtab_size, v1]
// v3: [v2, code_model]
StringRef Name;
std::tie(Name, Record) = readNameFromStrtab(Record);
@@ -3913,6 +3931,13 @@ Error BitcodeReader::parseGlobalVarRecord(ArrayRef<uint64_t> Record) {
NewGV->setSanitizerMetadata(Meta);
}
if (Record.size() > 17 && Record[17]) {
if (auto CM = getDecodedCodeModel(Record[17]))
NewGV->setCodeModel(*CM);
else
return error("Invalid global variable code model");
}
return Error::success();
}