[llvm] 6d859c1 - [SandboxIR] Implement GlobalValue (#108317)
via llvm-commits
llvm-commits at lists.llvm.org
Thu Sep 12 16:16:52 PDT 2024
Author: vporpo
Date: 2024-09-12T16:16:48-07:00
New Revision: 6d859c1712b539fb36e799bd7b1801f4acacc1ac
URL: https://github.com/llvm/llvm-project/commit/6d859c1712b539fb36e799bd7b1801f4acacc1ac
DIFF: https://github.com/llvm/llvm-project/commit/6d859c1712b539fb36e799bd7b1801f4acacc1ac.diff
LOG: [SandboxIR] Implement GlobalValue (#108317)
This patch implements sandboxir::GlobalValue mirroring
llvm::GlobalValue. Please note that the implementation is incomplete as
it's missing several member functions.
Added:
Modified:
llvm/include/llvm/SandboxIR/SandboxIR.h
llvm/include/llvm/SandboxIR/SandboxIRValues.def
llvm/lib/SandboxIR/SandboxIR.cpp
llvm/unittests/SandboxIR/SandboxIRTest.cpp
llvm/unittests/SandboxIR/TrackerTest.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/SandboxIR/SandboxIR.h b/llvm/include/llvm/SandboxIR/SandboxIR.h
index 95fe239555fb41..5b57d5cebc3346 100644
--- a/llvm/include/llvm/SandboxIR/SandboxIR.h
+++ b/llvm/include/llvm/SandboxIR/SandboxIR.h
@@ -125,6 +125,7 @@ class ConstantPointerNull;
class PoisonValue;
class BlockAddress;
class ConstantTokenNone;
+class GlobalValue;
class Context;
class Function;
class Instruction;
@@ -326,6 +327,7 @@ class Value {
friend class UndefValue; // For `Val`.
friend class PoisonValue; // For `Val`.
friend class BlockAddress; // For `Val`.
+ friend class GlobalValue; // For `Val`.
/// All values point to the context.
Context &Ctx;
@@ -1115,6 +1117,80 @@ class PoisonValue final : public UndefValue {
#endif
};
+class GlobalValue : public Constant {
+protected:
+ GlobalValue(ClassID ID, llvm::GlobalValue *C, Context &Ctx)
+ : Constant(ID, C, Ctx) {}
+ friend class Context; // For constructor.
+ Use getOperandUseInternal(unsigned OpIdx, bool Verify) const override {
+ return getOperandUseDefault(OpIdx, Verify);
+ }
+
+public:
+ unsigned getUseOperandNo(const Use &Use) const override {
+ return getUseOperandNoDefault(Use);
+ }
+ /// For isa/dyn_cast.
+ static bool classof(const sandboxir::Value *From) {
+ switch (From->getSubclassID()) {
+ case ClassID::Function:
+ case ClassID::GlobalVariable:
+ case ClassID::GlobalAlias:
+ case ClassID::GlobalIFunc:
+ return true;
+ default:
+ return false;
+ }
+ }
+
+ unsigned getAddressSpace() const {
+ return cast<llvm::GlobalValue>(Val)->getAddressSpace();
+ }
+ bool hasGlobalUnnamedAddr() const {
+ return cast<llvm::GlobalValue>(Val)->hasGlobalUnnamedAddr();
+ }
+
+ /// Returns true if this value's address is not significant in this module.
+ /// This attribute is intended to be used only by the code generator and LTO
+ /// to allow the linker to decide whether the global needs to be in the symbol
+ /// table. It should probably not be used in optimizations, as the value may
+ /// have uses outside the module; use hasGlobalUnnamedAddr() instead.
+ bool hasAtLeastLocalUnnamedAddr() const {
+ return cast<llvm::GlobalValue>(Val)->hasAtLeastLocalUnnamedAddr();
+ }
+
+ using UnnamedAddr = llvm::GlobalValue::UnnamedAddr;
+
+ UnnamedAddr getUnnamedAddr() const {
+ return cast<llvm::GlobalValue>(Val)->getUnnamedAddr();
+ }
+ void setUnnamedAddr(UnnamedAddr V);
+
+ static UnnamedAddr getMinUnnamedAddr(UnnamedAddr A, UnnamedAddr B) {
+ return llvm::GlobalValue::getMinUnnamedAddr(A, B);
+ }
+
+ bool hasComdat() const { return cast<llvm::GlobalValue>(Val)->hasComdat(); }
+
+ // TODO: We need a SandboxIR Comdat if we want to implement getComdat().
+ using VisibilityTypes = llvm::GlobalValue::VisibilityTypes;
+ VisibilityTypes getVisibility() const {
+ return cast<llvm::GlobalValue>(Val)->getVisibility();
+ }
+ bool hasDefaultVisibility() const {
+ return cast<llvm::GlobalValue>(Val)->hasDefaultVisibility();
+ }
+ bool hasHiddenVisibility() const {
+ return cast<llvm::GlobalValue>(Val)->hasHiddenVisibility();
+ }
+ bool hasProtectedVisibility() const {
+ return cast<llvm::GlobalValue>(Val)->hasProtectedVisibility();
+ }
+ void setVisibility(VisibilityTypes V);
+
+ // TODO: Add missing functions.
+};
+
class BlockAddress final : public Constant {
BlockAddress(llvm::BlockAddress *C, Context &Ctx)
: Constant(ClassID::BlockAddress, C, Ctx) {}
@@ -3845,8 +3921,9 @@ class Context {
friend class PointerType; // For LLVMCtx.
friend class CmpInst; // For LLVMCtx. TODO: cleanup when sandboxir::VectorType
// is complete
- friend class IntegerType; // For LLVMCtx.
- friend class StructType; // For LLVMCtx.
+ friend class IntegerType; // For LLVMCtx.
+ friend class StructType; // For LLVMCtx.
+ friend class TargetExtType; // For LLVMCtx.
Tracker IRTracker;
/// Maps LLVM Value to the corresponding sandboxir::Value. Owns all
diff --git a/llvm/include/llvm/SandboxIR/SandboxIRValues.def b/llvm/include/llvm/SandboxIR/SandboxIRValues.def
index bd2f533e880ac6..7b72f9b7173e6d 100644
--- a/llvm/include/llvm/SandboxIR/SandboxIRValues.def
+++ b/llvm/include/llvm/SandboxIR/SandboxIRValues.def
@@ -34,6 +34,9 @@ DEF_CONST(ConstantAggregateZero, ConstantAggregateZero)
DEF_CONST(ConstantPointerNull, ConstantPointerNull)
DEF_CONST(UndefValue, UndefValue)
DEF_CONST(PoisonValue, PoisonValue)
+DEF_CONST(GlobalVariable, GlobalVariable)
+DEF_CONST(GlobalIFunc, GlobalIFunc)
+DEF_CONST(GlobalAlias, GlobalAlias)
DEF_CONST(BlockAddress, BlockAddress)
DEF_CONST(ConstantTokenNone, ConstantTokenNone)
diff --git a/llvm/lib/SandboxIR/SandboxIR.cpp b/llvm/lib/SandboxIR/SandboxIR.cpp
index 05d05f7ed10fb9..8a7c3981e6680d 100644
--- a/llvm/lib/SandboxIR/SandboxIR.cpp
+++ b/llvm/lib/SandboxIR/SandboxIR.cpp
@@ -2495,6 +2495,20 @@ PoisonValue *PoisonValue::getElementValue(unsigned Idx) const {
cast<llvm::PoisonValue>(Val)->getElementValue(Idx)));
}
+void GlobalValue::setUnnamedAddr(UnnamedAddr V) {
+ Ctx.getTracker()
+ .emplaceIfTracking<GenericSetter<&GlobalValue::getUnnamedAddr,
+ &GlobalValue::setUnnamedAddr>>(this);
+ cast<llvm::GlobalValue>(Val)->setUnnamedAddr(V);
+}
+
+void GlobalValue::setVisibility(VisibilityTypes V) {
+ Ctx.getTracker()
+ .emplaceIfTracking<GenericSetter<&GlobalValue::getVisibility,
+ &GlobalValue::setVisibility>>(this);
+ cast<llvm::GlobalValue>(Val)->setVisibility(V);
+}
+
BlockAddress *BlockAddress::get(Function *F, BasicBlock *BB) {
auto *LLVMC = llvm::BlockAddress::get(cast<llvm::Function>(F->Val),
cast<llvm::BasicBlock>(BB->Val));
diff --git a/llvm/unittests/SandboxIR/SandboxIRTest.cpp b/llvm/unittests/SandboxIR/SandboxIRTest.cpp
index 6280963d588fac..ad5508f041d6cd 100644
--- a/llvm/unittests/SandboxIR/SandboxIRTest.cpp
+++ b/llvm/unittests/SandboxIR/SandboxIRTest.cpp
@@ -729,6 +729,72 @@ define void @foo() {
EXPECT_EQ(UndefStruct->getNumElements(), 2u);
}
+TEST_F(SandboxIRTest, GlobalValue) {
+ parseIR(C, R"IR(
+declare external void @bar()
+define void @foo() {
+ call void @bar()
+ ret void
+}
+)IR");
+ Function &LLVMF = *M->getFunction("foo");
+ auto *LLVMBB = &*LLVMF.begin();
+ auto LLVMIt = LLVMBB->begin();
+ auto *LLVMCall = cast<llvm::CallInst>(&*LLVMIt++);
+ auto *LLVMGV = cast<llvm::GlobalValue>(LLVMCall->getCalledOperand());
+ sandboxir::Context Ctx(C);
+
+ auto &F = *Ctx.createFunction(&LLVMF);
+ auto *BB = &*F.begin();
+ auto It = BB->begin();
+ auto *Call = cast<sandboxir::CallInst>(&*It++);
+ [[maybe_unused]] auto *Ret = cast<sandboxir::ReturnInst>(&*It++);
+
+ // Check classof(), creation, getFunction(), getBasicBlock().
+ auto *GV = cast<sandboxir::GlobalValue>(Call->getCalledOperand());
+ // Check getAddressSpace().
+ EXPECT_EQ(GV->getAddressSpace(), LLVMGV->getAddressSpace());
+ // Check hasGlobalUnnamedAddr().
+ EXPECT_EQ(GV->hasGlobalUnnamedAddr(), LLVMGV->hasGlobalUnnamedAddr());
+ // Check hasAtLeastLocalUnnamedAddr().
+ EXPECT_EQ(GV->hasAtLeastLocalUnnamedAddr(),
+ LLVMGV->hasAtLeastLocalUnnamedAddr());
+ // Check getUnnamedAddr().
+ EXPECT_EQ(GV->getUnnamedAddr(), LLVMGV->getUnnamedAddr());
+ // Check setUnnamedAddr().
+ auto OrigUnnamedAddr = GV->getUnnamedAddr();
+ auto NewUnnamedAddr = sandboxir::GlobalValue::UnnamedAddr::Global;
+ EXPECT_NE(NewUnnamedAddr, OrigUnnamedAddr);
+ GV->setUnnamedAddr(NewUnnamedAddr);
+ EXPECT_EQ(GV->getUnnamedAddr(), NewUnnamedAddr);
+ GV->setUnnamedAddr(OrigUnnamedAddr);
+ EXPECT_EQ(GV->getUnnamedAddr(), OrigUnnamedAddr);
+ // Check getMinUnnamedAddr().
+ EXPECT_EQ(
+ sandboxir::GlobalValue::getMinUnnamedAddr(OrigUnnamedAddr,
+ NewUnnamedAddr),
+ llvm::GlobalValue::getMinUnnamedAddr(OrigUnnamedAddr, NewUnnamedAddr));
+ // Check hasComdat().
+ EXPECT_EQ(GV->hasComdat(), LLVMGV->hasComdat());
+ // Check getVisibility().
+ EXPECT_EQ(GV->getVisibility(), LLVMGV->getVisibility());
+ // Check hasDefaultVisibility().
+ EXPECT_EQ(GV->hasDefaultVisibility(), LLVMGV->hasDefaultVisibility());
+ // Check hasHiddenVisibility().
+ EXPECT_EQ(GV->hasHiddenVisibility(), LLVMGV->hasHiddenVisibility());
+ // Check hasProtectedVisibility().
+ EXPECT_EQ(GV->hasProtectedVisibility(), LLVMGV->hasProtectedVisibility());
+ // Check setVisibility().
+ auto OrigVisibility = GV->getVisibility();
+ auto NewVisibility =
+ sandboxir::GlobalValue::VisibilityTypes::ProtectedVisibility;
+ EXPECT_NE(NewVisibility, OrigVisibility);
+ GV->setVisibility(NewVisibility);
+ EXPECT_EQ(GV->getVisibility(), NewVisibility);
+ GV->setVisibility(OrigVisibility);
+ EXPECT_EQ(GV->getVisibility(), OrigVisibility);
+}
+
TEST_F(SandboxIRTest, BlockAddress) {
parseIR(C, R"IR(
define void @foo(ptr %ptr) {
diff --git a/llvm/unittests/SandboxIR/TrackerTest.cpp b/llvm/unittests/SandboxIR/TrackerTest.cpp
index a1a4117b5e97b1..6454c54336e6aa 100644
--- a/llvm/unittests/SandboxIR/TrackerTest.cpp
+++ b/llvm/unittests/SandboxIR/TrackerTest.cpp
@@ -1521,6 +1521,43 @@ define void @foo(i64 %i0, i64 %i1, float %f0, float %f1) {
checkCmpInst(Ctx, ICmp);
}
+TEST_F(TrackerTest, GlobalValueSetters) {
+ parseIR(C, R"IR(
+define void @foo() {
+ call void @foo()
+ ret void
+}
+)IR");
+ Function &LLVMF = *M->getFunction("foo");
+ sandboxir::Context Ctx(C);
+
+ auto &F = *Ctx.createFunction(&LLVMF);
+ auto *BB = &*F.begin();
+ auto *Call = cast<sandboxir::CallInst>(&*BB->begin());
+
+ auto *GV = cast<sandboxir::GlobalValue>(Call->getCalledOperand());
+ // Check setUnnamedAddr().
+ auto OrigUnnamedAddr = GV->getUnnamedAddr();
+ auto NewUnnamedAddr = sandboxir::GlobalValue::UnnamedAddr::Global;
+ EXPECT_NE(NewUnnamedAddr, OrigUnnamedAddr);
+ Ctx.save();
+ GV->setUnnamedAddr(NewUnnamedAddr);
+ EXPECT_EQ(GV->getUnnamedAddr(), NewUnnamedAddr);
+ Ctx.revert();
+ EXPECT_EQ(GV->getUnnamedAddr(), OrigUnnamedAddr);
+
+ // Check setVisibility().
+ auto OrigVisibility = GV->getVisibility();
+ auto NewVisibility =
+ sandboxir::GlobalValue::VisibilityTypes::ProtectedVisibility;
+ EXPECT_NE(NewVisibility, OrigVisibility);
+ Ctx.save();
+ GV->setVisibility(NewVisibility);
+ EXPECT_EQ(GV->getVisibility(), NewVisibility);
+ Ctx.revert();
+ EXPECT_EQ(GV->getVisibility(), OrigVisibility);
+}
+
TEST_F(TrackerTest, SetVolatile) {
parseIR(C, R"IR(
define void @foo(ptr %arg0, i8 %val) {
More information about the llvm-commits
mailing list