Skip to content

Commit 6f436b7

Browse files
[LLVM] Extend setModuleFlag interface.
1 parent d0dc29c commit 6f436b7

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

llvm/include/llvm/IR/Module.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,8 @@ class LLVM_EXTERNAL_VISIBILITY Module {
548548
void addModuleFlag(MDNode *Node);
549549
/// Like addModuleFlag but replaces the old module flag if it already exists.
550550
void setModuleFlag(ModFlagBehavior Behavior, StringRef Key, Metadata *Val);
551+
void setModuleFlag(ModFlagBehavior Behavior, StringRef Key, Constant *Val);
552+
void setModuleFlag(ModFlagBehavior Behavior, StringRef Key, uint32_t Val);
551553

552554
/// @}
553555
/// @name Materialization

llvm/lib/IR/Module.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,15 @@ void Module::setModuleFlag(ModFlagBehavior Behavior, StringRef Key,
397397
}
398398
addModuleFlag(Behavior, Key, Val);
399399
}
400+
void Module::setModuleFlag(ModFlagBehavior Behavior, StringRef Key,
401+
Constant *Val) {
402+
setModuleFlag(Behavior, Key, ConstantAsMetadata::get(Val));
403+
}
404+
void Module::setModuleFlag(ModFlagBehavior Behavior, StringRef Key,
405+
uint32_t Val) {
406+
Type *Int32Ty = Type::getInt32Ty(Context);
407+
setModuleFlag(Behavior, Key, ConstantInt::get(Int32Ty, Val));
408+
}
400409

401410
void Module::setDataLayout(StringRef Desc) {
402411
DL.reset(Desc);

llvm/unittests/IR/ModuleTest.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include "llvm/IR/Module.h"
1010
#include "llvm/AsmParser/Parser.h"
11+
#include "llvm/IR/Constants.h"
1112
#include "llvm/IR/GlobalVariable.h"
1213
#include "llvm/IR/ModuleSummaryIndex.h"
1314
#include "llvm/Pass.h"
@@ -86,6 +87,21 @@ TEST(ModuleTest, setModuleFlag) {
8687
EXPECT_EQ(Val2, M.getModuleFlag(Key));
8788
}
8889

90+
TEST(ModuleTest, setModuleFlagInt) {
91+
LLVMContext Context;
92+
Module M("M", Context);
93+
StringRef Key = "Key";
94+
uint32_t Val1 = 1;
95+
uint32_t Val2 = 2;
96+
EXPECT_EQ(nullptr, M.getModuleFlag(Key));
97+
M.setModuleFlag(Module::ModFlagBehavior::Error, Key, Val1);
98+
auto A1 = mdconst::extract_or_null<ConstantInt>(M.getModuleFlag(Key));
99+
EXPECT_EQ(Val1, A1->getZExtValue());
100+
M.setModuleFlag(Module::ModFlagBehavior::Error, Key, Val2);
101+
auto A2 = mdconst::extract_or_null<ConstantInt>(M.getModuleFlag(Key));
102+
EXPECT_EQ(Val2, A2->getZExtValue());
103+
}
104+
89105
const char *IRString = R"IR(
90106
!llvm.module.flags = !{!0}
91107

0 commit comments

Comments
 (0)