Skip to content
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
9 changes: 7 additions & 2 deletions llvm/lib/IR/Module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
30 changes: 30 additions & 0 deletions llvm/unittests/IR/ModuleTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,36 @@ TEST(ModuleTest, setModuleFlagInt) {
EXPECT_EQ(Val2, A2->getZExtValue());
}

TEST(ModuleTest, setModuleFlagTwoMod) {
LLVMContext Context;
Module MA("MA", Context);
Module MB("MB", Context);
StringRef Key = "Key";
uint32_t Val1 = 1;
uint32_t Val2 = 2;

// Set a flag to MA
EXPECT_EQ(nullptr, MA.getModuleFlag(Key));
MA.setModuleFlag(Module::ModFlagBehavior::Error, Key, Val1);
auto A1 = mdconst::extract_or_null<ConstantInt>(MA.getModuleFlag(Key));
EXPECT_EQ(Val1, A1->getZExtValue());

// Set a flag to MB
EXPECT_EQ(nullptr, MB.getModuleFlag(Key));
MB.setModuleFlag(Module::ModFlagBehavior::Error, Key, Val1);
auto B1 = mdconst::extract_or_null<ConstantInt>(MB.getModuleFlag(Key));
EXPECT_EQ(Val1, B1->getZExtValue());

// Change the flag of MA
MA.setModuleFlag(Module::ModFlagBehavior::Error, Key, Val2);
auto A2 = mdconst::extract_or_null<ConstantInt>(MA.getModuleFlag(Key));
EXPECT_EQ(Val2, A2->getZExtValue());

// MB should keep the original flag value
auto B2 = mdconst::extract_or_null<ConstantInt>(MB.getModuleFlag(Key));
EXPECT_EQ(Val1, B2->getZExtValue());
}

const char *IRString = R"IR(
!llvm.module.flags = !{!0}

Expand Down