aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/IR/Module.cpp
diff options
context:
space:
mode:
authorAndrew Savonichev <andrew.savonichev@gmail.com>2025-10-24 09:43:10 +0900
committerGitHub <noreply@github.com>2025-10-24 09:43:10 +0900
commit6ed814a823258fa357c25bd71e76eb688fbbff79 (patch)
tree6c6f3237331c1e1087aa0eebd9d999147d7ae433 /llvm/lib/IR/Module.cpp
parent169626f11ab19a0b33fba2ca84cf4d9ec2b76fb1 (diff)
downloadllvm-6ed814a823258fa357c25bd71e76eb688fbbff79.zip
llvm-6ed814a823258fa357c25bd71e76eb688fbbff79.tar.gz
llvm-6ed814a823258fa357c25bd71e76eb688fbbff79.tar.bz2
[IR] Fix Module::setModuleFlag for uniqued metadata (#164580)
`Module::setModuleFlag` is supposed to change a single module. However, when an `MDNode` has the same value in more than one module in the same `LLVMContext`, such `MDNode` is shared (uniqued) across all of them. Therefore `MDNode::replaceOperandWith` changes all modules that share the same `MDNode`. This used to cause problems for #86212, where a module is marked as "upgraded" via a module flag. When this flag is shared across multiple modules, all of them are marked, yet some may not have been processed at all. After the patch we now construct a new `MDNode` and replace the old one.
Diffstat (limited to 'llvm/lib/IR/Module.cpp')
-rw-r--r--llvm/lib/IR/Module.cpp9
1 files changed, 7 insertions, 2 deletions
diff --git a/llvm/lib/IR/Module.cpp b/llvm/lib/IR/Module.cpp
index 30b5e48..e19336e 100644
--- a/llvm/lib/IR/Module.cpp
+++ b/llvm/lib/IR/Module.cpp
@@ -403,9 +403,14 @@ void Module::setModuleFlag(ModFlagBehavior Behavior, StringRef Key,
Metadata *Val) {
NamedMDNode *ModFlags = getOrInsertModuleFlagsMetadata();
// Replace the flag if it already exists.
- for (MDNode *Flag : ModFlags->operands()) {
+ for (unsigned i = 0; i < ModFlags->getNumOperands(); ++i) {
+ MDNode *Flag = ModFlags->getOperand(i);
if (cast<MDString>(Flag->getOperand(1))->getString() == Key) {
- Flag->replaceOperandWith(2, Val);
+ Type *Int32Ty = Type::getInt32Ty(Context);
+ Metadata *Ops[3] = {
+ ConstantAsMetadata::get(ConstantInt::get(Int32Ty, Behavior)),
+ MDString::get(Context, Key), Val};
+ ModFlags->setOperand(i, MDNode::get(Context, Ops));
return;
}
}