[llvm] [SandboxIR] Implement NoCFIValue (PR #109046)
via llvm-commits
llvm-commits at lists.llvm.org
Tue Sep 17 13:49:45 PDT 2024
https://github.com/vporpo created https://github.com/llvm/llvm-project/pull/109046
This patch implements sandboxir::NoCFIValue mirroring llvm::NoCFIValue.
>From bb1c091ebf0daaac55788d1b8cfa507e87ab4262 Mon Sep 17 00:00:00 2001
From: Vasileios Porpodas <vporpodas at google.com>
Date: Fri, 23 Aug 2024 14:32:11 -0700
Subject: [PATCH] [SandboxIR] Implement NoCFIValue
This patch implements sandboxir::NoCFIValue mirroring llvm::NoCFIValue.
---
llvm/include/llvm/SandboxIR/SandboxIR.h | 39 +++++++++++++++++++
.../llvm/SandboxIR/SandboxIRValues.def | 1 +
llvm/lib/SandboxIR/SandboxIR.cpp | 18 +++++++++
llvm/unittests/SandboxIR/SandboxIRTest.cpp | 25 ++++++++++++
4 files changed, 83 insertions(+)
diff --git a/llvm/include/llvm/SandboxIR/SandboxIR.h b/llvm/include/llvm/SandboxIR/SandboxIR.h
index b276c06033596d..461a0a672adca6 100644
--- a/llvm/include/llvm/SandboxIR/SandboxIR.h
+++ b/llvm/include/llvm/SandboxIR/SandboxIR.h
@@ -131,6 +131,7 @@ class GlobalObject;
class GlobalIFunc;
class GlobalVariable;
class GlobalAlias;
+class NoCFIValue;
class Context;
class Function;
class Instruction;
@@ -338,6 +339,7 @@ class Value {
friend class GlobalIFunc; // For `Val`.
friend class GlobalVariable; // For `Val`.
friend class GlobalAlias; // For `Val`.
+ friend class NoCFIValue; // For `Val`.
/// All values point to the context.
Context &Ctx;
@@ -1562,6 +1564,43 @@ class GlobalAlias final
}
};
+class NoCFIValue final : public Constant {
+ NoCFIValue(llvm::NoCFIValue *C, Context &Ctx)
+ : Constant(ClassID::NoCFIValue, C, Ctx) {}
+ friend class Context; // For constructor.
+
+ Use getOperandUseInternal(unsigned OpIdx, bool Verify) const final {
+ return getOperandUseDefault(OpIdx, Verify);
+ }
+
+public:
+ /// Return a NoCFIValue for the specified function.
+ static NoCFIValue *get(GlobalValue *GV);
+
+ GlobalValue *getGlobalValue() const;
+
+ /// NoCFIValue is always a pointer.
+ PointerType *getType() const;
+ /// For isa/dyn_cast.
+ static bool classof(const sandboxir::Value *From) {
+ return From->getSubclassID() == ClassID::NoCFIValue;
+ }
+
+ unsigned getUseOperandNo(const Use &Use) const final {
+ return getUseOperandNoDefault(Use);
+ }
+
+#ifndef NDEBUG
+ void verify() const override {
+ assert(isa<llvm::NoCFIValue>(Val) && "Expected a NoCFIValue!");
+ }
+ void dumpOS(raw_ostream &OS) const override {
+ dumpCommonPrefix(OS);
+ dumpCommonSuffix(OS);
+ }
+#endif
+};
+
class BlockAddress final : public Constant {
BlockAddress(llvm::BlockAddress *C, Context &Ctx)
: Constant(ClassID::BlockAddress, C, Ctx) {}
diff --git a/llvm/include/llvm/SandboxIR/SandboxIRValues.def b/llvm/include/llvm/SandboxIR/SandboxIRValues.def
index c218ffee3ce383..054de8db6d1281 100644
--- a/llvm/include/llvm/SandboxIR/SandboxIRValues.def
+++ b/llvm/include/llvm/SandboxIR/SandboxIRValues.def
@@ -38,6 +38,7 @@ DEF_CONST(GlobalVariable, GlobalVariable)
DEF_CONST(GlobalIFunc, GlobalIFunc)
DEF_CONST(GlobalAlias, GlobalAlias)
DEF_CONST(BlockAddress, BlockAddress)
+DEF_CONST(NoCFIValue, NoCFIValue)
DEF_CONST(DSOLocalEquivalent, DSOLocalEquivalent)
DEF_CONST(ConstantTokenNone, ConstantTokenNone)
diff --git a/llvm/lib/SandboxIR/SandboxIR.cpp b/llvm/lib/SandboxIR/SandboxIR.cpp
index d047a53b4752ee..b2eac75962fdab 100644
--- a/llvm/lib/SandboxIR/SandboxIR.cpp
+++ b/llvm/lib/SandboxIR/SandboxIR.cpp
@@ -2621,6 +2621,20 @@ void GlobalValue::setVisibility(VisibilityTypes V) {
cast<llvm::GlobalValue>(Val)->setVisibility(V);
}
+NoCFIValue *NoCFIValue::get(GlobalValue *GV) {
+ auto *LLVMC = llvm::NoCFIValue::get(cast<llvm::GlobalValue>(GV->Val));
+ return cast<NoCFIValue>(GV->getContext().getOrCreateConstant(LLVMC));
+}
+
+GlobalValue *NoCFIValue::getGlobalValue() const {
+ auto *LLVMC = cast<llvm::NoCFIValue>(Val)->getGlobalValue();
+ return cast<GlobalValue>(Ctx.getOrCreateConstant(LLVMC));
+}
+
+PointerType *NoCFIValue::getType() const {
+ return cast<PointerType>(Ctx.getType(cast<llvm::NoCFIValue>(Val)->getType()));
+}
+
BlockAddress *BlockAddress::get(Function *F, BasicBlock *BB) {
auto *LLVMC = llvm::BlockAddress::get(cast<llvm::Function>(F->Val),
cast<llvm::BasicBlock>(BB->Val));
@@ -2827,6 +2841,10 @@ Value *Context::getOrCreateValueInternal(llvm::Value *LLVMV, llvm::User *U) {
It->second = std::unique_ptr<GlobalAlias>(
new GlobalAlias(cast<llvm::GlobalAlias>(C), *this));
break;
+ case llvm::Value::NoCFIValueVal:
+ It->second = std::unique_ptr<NoCFIValue>(
+ new NoCFIValue(cast<llvm::NoCFIValue>(C), *this));
+ break;
default:
It->second = std::unique_ptr<Constant>(new Constant(C, *this));
break;
diff --git a/llvm/unittests/SandboxIR/SandboxIRTest.cpp b/llvm/unittests/SandboxIR/SandboxIRTest.cpp
index b998091f6d7a97..1768d442373350 100644
--- a/llvm/unittests/SandboxIR/SandboxIRTest.cpp
+++ b/llvm/unittests/SandboxIR/SandboxIRTest.cpp
@@ -1117,6 +1117,31 @@ define void @foo() {
Ctx.getValue(LLVMAlias0->getAliaseeObject()));
}
+TEST_F(SandboxIRTest, NoCFIValue) {
+ parseIR(C, R"IR(
+define void @foo() {
+ call void no_cfi @foo()
+ ret void
+}
+)IR");
+ Function &LLVMF = *M->getFunction("foo");
+ sandboxir::Context Ctx(C);
+
+ auto &F = *Ctx.createFunction(&LLVMF);
+ auto *BB = &*F.begin();
+ auto It = BB->begin();
+ auto *Call = cast<sandboxir::CallInst>(&*It++);
+ // Check classof(), creation.
+ auto *NoCFI = cast<sandboxir::NoCFIValue>(Call->getCalledOperand());
+ // Check get().
+ auto *NewNoCFI = sandboxir::NoCFIValue::get(&F);
+ EXPECT_EQ(NewNoCFI, NoCFI);
+ // Check getGlobalValue().
+ EXPECT_EQ(NoCFI->getGlobalValue(), &F);
+ // Check getType().
+ EXPECT_EQ(NoCFI->getType(), F.getType());
+}
+
TEST_F(SandboxIRTest, BlockAddress) {
parseIR(C, R"IR(
define void @foo(ptr %ptr) {
More information about the llvm-commits
mailing list