[llvm] [LLVM] Extend setModuleFlag interface. (PR #86031)

Daniel Kiss via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 20 15:57:40 PDT 2024


https://github.com/DanielKristofKiss created https://github.com/llvm/llvm-project/pull/86031

None

>From 80ed0162d7db292373aef9158e83cefc6d7da446 Mon Sep 17 00:00:00 2001
From: Daniel Kiss <daniel.kiss at arm.com>
Date: Wed, 20 Mar 2024 23:47:01 +0100
Subject: [PATCH] Extend setModuleFlag interface.

---
 llvm/include/llvm/IR/Module.h    |  2 ++
 llvm/lib/IR/Module.cpp           |  9 +++++++++
 llvm/unittests/IR/ModuleTest.cpp | 16 ++++++++++++++++
 3 files changed, 27 insertions(+)

diff --git a/llvm/include/llvm/IR/Module.h b/llvm/include/llvm/IR/Module.h
index bb2e667ef6f410..907aeaf7f67efa 100644
--- a/llvm/include/llvm/IR/Module.h
+++ b/llvm/include/llvm/IR/Module.h
@@ -542,6 +542,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
diff --git a/llvm/lib/IR/Module.cpp b/llvm/lib/IR/Module.cpp
index a8696ed9e3ce5d..25231f54313b33 100644
--- a/llvm/lib/IR/Module.cpp
+++ b/llvm/lib/IR/Module.cpp
@@ -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);
diff --git a/llvm/unittests/IR/ModuleTest.cpp b/llvm/unittests/IR/ModuleTest.cpp
index da684b85a4dfb5..c18301d5e6d758 100644
--- a/llvm/unittests/IR/ModuleTest.cpp
+++ b/llvm/unittests/IR/ModuleTest.cpp
@@ -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"
@@ -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}
 



More information about the llvm-commits mailing list