Skip to content

[LLVM] Extend setModuleFlag interface. #86031

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
Jun 20, 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
2 changes: 2 additions & 0 deletions llvm/include/llvm/IR/Module.h
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,8 @@ class LLVM_EXTERNAL_VISIBILITY Module {
void addModuleFlag(MDNode *Node);
/// Like addModuleFlag but replaces the old module flag if it already exists.
void setModuleFlag(ModFlagBehavior Behavior, StringRef Key, Metadata *Val);
void setModuleFlag(ModFlagBehavior Behavior, StringRef Key, Constant *Val);
void setModuleFlag(ModFlagBehavior Behavior, StringRef Key, uint32_t Val);

/// @}
/// @name Materialization
Expand Down
9 changes: 9 additions & 0 deletions llvm/lib/IR/Module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,15 @@ void Module::setModuleFlag(ModFlagBehavior Behavior, StringRef Key,
}
addModuleFlag(Behavior, Key, Val);
}
void Module::setModuleFlag(ModFlagBehavior Behavior, StringRef Key,
Constant *Val) {
setModuleFlag(Behavior, Key, ConstantAsMetadata::get(Val));
}
void Module::setModuleFlag(ModFlagBehavior Behavior, StringRef Key,
uint32_t Val) {
Type *Int32Ty = Type::getInt32Ty(Context);
setModuleFlag(Behavior, Key, ConstantInt::get(Int32Ty, Val));
}

void Module::setDataLayout(StringRef Desc) {
DL.reset(Desc);
Expand Down
16 changes: 16 additions & 0 deletions llvm/unittests/IR/ModuleTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "llvm/IR/Module.h"
#include "llvm/AsmParser/Parser.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/GlobalVariable.h"
#include "llvm/IR/ModuleSummaryIndex.h"
#include "llvm/Pass.h"
Expand Down Expand Up @@ -86,6 +87,21 @@ TEST(ModuleTest, setModuleFlag) {
EXPECT_EQ(Val2, M.getModuleFlag(Key));
}

TEST(ModuleTest, setModuleFlagInt) {
LLVMContext Context;
Module M("M", Context);
StringRef Key = "Key";
uint32_t Val1 = 1;
uint32_t Val2 = 2;
EXPECT_EQ(nullptr, M.getModuleFlag(Key));
M.setModuleFlag(Module::ModFlagBehavior::Error, Key, Val1);
auto A1 = mdconst::extract_or_null<ConstantInt>(M.getModuleFlag(Key));
EXPECT_EQ(Val1, A1->getZExtValue());
M.setModuleFlag(Module::ModFlagBehavior::Error, Key, Val2);
auto A2 = mdconst::extract_or_null<ConstantInt>(M.getModuleFlag(Key));
EXPECT_EQ(Val2, A2->getZExtValue());
}

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

Expand Down
Loading