Skip to content

[IR] Don't verify module flags on every access #102153

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions llvm/include/llvm/IR/Module.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,6 @@ class LLVM_EXTERNAL_VISIBILITY Module {
/// converted result in MFB.
static bool isValidModFlagBehavior(Metadata *MD, ModFlagBehavior &MFB);

/// Check if the given module flag metadata represents a valid module flag,
/// and store the flag behavior, the key string and the value metadata.
static bool isValidModuleFlag(const MDNode &ModFlag, ModFlagBehavior &MFB,
MDString *&Key, Metadata *&Val);

struct ModuleFlagEntry {
ModFlagBehavior Behavior;
MDString *Key;
Expand Down Expand Up @@ -502,7 +497,7 @@ class LLVM_EXTERNAL_VISIBILITY Module {

/// Return the first NamedMDNode in the module with the specified name. This
/// method returns null if a NamedMDNode with the specified name is not found.
NamedMDNode *getNamedMetadata(const Twine &Name) const;
NamedMDNode *getNamedMetadata(StringRef Name) const;

/// Return the named MDNode in the module with the specified name. This method
/// returns a new NamedMDNode if a NamedMDNode with the specified name is not
Expand Down
20 changes: 19 additions & 1 deletion llvm/lib/IR/AutoUpgrade.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4886,7 +4886,25 @@ bool llvm::UpgradeDebugInfo(Module &M) {
if (DisableAutoUpgradeDebugInfo)
return false;

unsigned Version = getDebugMetadataVersionFromModule(M);
// We need to get metadata before the module is verified (i.e., getModuleFlag
// makes assumptions that we haven't verified yet). Carefully extract the flag
// from the metadata.
unsigned Version = 0;
if (NamedMDNode *ModFlags = M.getModuleFlagsMetadata()) {
auto OpIt = find_if(ModFlags->operands(), [](const MDNode *Flag) {
if (Flag->getNumOperands() < 3)
return false;
if (MDString *K = dyn_cast_or_null<MDString>(Flag->getOperand(1)))
return K->getString() == "Debug Info Version";
return false;
});
if (OpIt != ModFlags->op_end()) {
const MDOperand &ValOp = (*OpIt)->getOperand(2);
if (auto *CI = mdconst::dyn_extract_or_null<ConstantInt>(ValOp))
Version = CI->getZExtValue();
}
}

if (Version == DEBUG_METADATA_VERSION) {
bool BrokenDebugInfo = false;
if (verifyModule(M, &llvm::errs(), &BrokenDebugInfo))
Expand Down
50 changes: 15 additions & 35 deletions llvm/lib/IR/Module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,10 +259,8 @@ GlobalIFunc *Module::getNamedIFunc(StringRef Name) const {
/// getNamedMetadata - Return the first NamedMDNode in the module with the
/// specified name. This method returns null if a NamedMDNode with the
/// specified name is not found.
NamedMDNode *Module::getNamedMetadata(const Twine &Name) const {
SmallString<256> NameData;
StringRef NameRef = Name.toStringRef(NameData);
return NamedMDSymTab.lookup(NameRef);
NamedMDNode *Module::getNamedMetadata(StringRef Name) const {
return NamedMDSymTab.lookup(Name);
}

/// getOrInsertNamedMetadata - Return the first named MDNode in the module
Expand Down Expand Up @@ -296,46 +294,31 @@ bool Module::isValidModFlagBehavior(Metadata *MD, ModFlagBehavior &MFB) {
return false;
}

bool Module::isValidModuleFlag(const MDNode &ModFlag, ModFlagBehavior &MFB,
MDString *&Key, Metadata *&Val) {
if (ModFlag.getNumOperands() < 3)
return false;
if (!isValidModFlagBehavior(ModFlag.getOperand(0), MFB))
return false;
MDString *K = dyn_cast_or_null<MDString>(ModFlag.getOperand(1));
if (!K)
return false;
Key = K;
Val = ModFlag.getOperand(2);
return true;
}

/// getModuleFlagsMetadata - Returns the module flags in the provided vector.
void Module::
getModuleFlagsMetadata(SmallVectorImpl<ModuleFlagEntry> &Flags) const {
const NamedMDNode *ModFlags = getModuleFlagsMetadata();
if (!ModFlags) return;

for (const MDNode *Flag : ModFlags->operands()) {
ModFlagBehavior MFB;
MDString *Key = nullptr;
Metadata *Val = nullptr;
if (isValidModuleFlag(*Flag, MFB, Key, Val)) {
// Check the operands of the MDNode before accessing the operands.
// The verifier will actually catch these failures.
Flags.push_back(ModuleFlagEntry(MFB, Key, Val));
}
// The verifier will catch errors, so no need to check them here.
auto *MFBConstant = mdconst::extract<ConstantInt>(Flag->getOperand(0));
auto MFB = static_cast<ModFlagBehavior>(MFBConstant->getLimitedValue());
MDString *Key = cast<MDString>(Flag->getOperand(1));
Metadata *Val = Flag->getOperand(2);
Flags.push_back(ModuleFlagEntry(MFB, Key, Val));
}
}

/// Return the corresponding value if Key appears in module flags, otherwise
/// return null.
Metadata *Module::getModuleFlag(StringRef Key) const {
SmallVector<Module::ModuleFlagEntry, 8> ModuleFlags;
getModuleFlagsMetadata(ModuleFlags);
for (const ModuleFlagEntry &MFE : ModuleFlags) {
if (Key == MFE.Key->getString())
return MFE.Val;
const NamedMDNode *ModFlags = getModuleFlagsMetadata();
if (!ModFlags)
return nullptr;
for (const MDNode *Flag : ModFlags->operands()) {
if (Key == cast<MDString>(Flag->getOperand(1))->getString())
return Flag->getOperand(2);
}
return nullptr;
}
Expand Down Expand Up @@ -388,10 +371,7 @@ void Module::setModuleFlag(ModFlagBehavior Behavior, StringRef Key,
NamedMDNode *ModFlags = getOrInsertModuleFlagsMetadata();
// Replace the flag if it already exists.
for (MDNode *Flag : ModFlags->operands()) {
ModFlagBehavior MFB;
MDString *K = nullptr;
Metadata *V = nullptr;
if (isValidModuleFlag(*Flag, MFB, K, V) && K->getString() == Key) {
if (cast<MDString>(Flag->getOperand(1))->getString() == Key) {
Flag->replaceOperandWith(2, Val);
return;
}
Expand Down
Loading