Skip to content

[SandboxIR] Implement NoCFIValue #109046

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
Sep 19, 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
39 changes: 39 additions & 0 deletions llvm/include/llvm/SandboxIR/SandboxIR.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ class GlobalObject;
class GlobalIFunc;
class GlobalVariable;
class GlobalAlias;
class NoCFIValue;
class Context;
class Function;
class Instruction;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {}
Expand Down
1 change: 1 addition & 0 deletions llvm/include/llvm/SandboxIR/SandboxIRValues.def
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
18 changes: 18 additions & 0 deletions llvm/lib/SandboxIR/SandboxIR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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;
Expand Down
25 changes: 25 additions & 0 deletions llvm/unittests/SandboxIR/SandboxIRTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Loading