Skip to content

[SandboxIR] OpaqueValue #127699

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
Feb 19, 2025
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
23 changes: 23 additions & 0 deletions llvm/include/llvm/SandboxIR/Value.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#ifndef LLVM_SANDBOXIR_VALUE_H
#define LLVM_SANDBOXIR_VALUE_H

#include "llvm/IR/Metadata.h"
#include "llvm/IR/Value.h"
#include "llvm/SandboxIR/Use.h"

Expand Down Expand Up @@ -282,6 +283,28 @@ class Value {
#endif
};

class OpaqueValue : public Value {
protected:
OpaqueValue(llvm::Value *V, Context &Ctx)
: Value(ClassID::OpaqueValue, V, Ctx) {}
friend class Context; // For constructor.

public:
static bool classof(const Value *From) {
return From->getSubclassID() == ClassID::OpaqueValue;
}
#ifndef NDEBUG
void verify() const override {
assert((isa<llvm::MetadataAsValue>(Val) || isa<llvm::InlineAsm>(Val)) &&
"Expected Metadata or InlineAssembly!");
}
void dumpOS(raw_ostream &OS) const override {
dumpCommonPrefix(OS);
dumpCommonSuffix(OS);
}
#endif // NDEBUG
};

} // namespace llvm::sandboxir

#endif // LLVM_SANDBOXIR_VALUE_H
1 change: 1 addition & 0 deletions llvm/include/llvm/SandboxIR/Values.def
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

DEF_CONST(Function, Function)
DEF_VALUE(Argument, Argument)
DEF_VALUE(OpaqueValue, OpaqueValue)

DEF_USER(User, User)
DEF_VALUE(Block, BasicBlock)
Expand Down
6 changes: 0 additions & 6 deletions llvm/lib/SandboxIR/BasicBlock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,6 @@ void BasicBlock::buildBasicBlockFromLLVMIR(llvm::BasicBlock *LLVMBB) {
// Skip instruction's label operands
if (isa<llvm::BasicBlock>(Op))
continue;
// Skip metadata
if (isa<llvm::MetadataAsValue>(Op))
continue;
// Skip asm
if (isa<llvm::InlineAsm>(Op))
continue;
Ctx.getOrCreateValue(Op);
}
}
Expand Down
10 changes: 10 additions & 0 deletions llvm/lib/SandboxIR/Context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//===----------------------------------------------------------------------===//

#include "llvm/SandboxIR/Context.h"
#include "llvm/IR/InlineAsm.h"
#include "llvm/SandboxIR/Function.h"
#include "llvm/SandboxIR/Instruction.h"
#include "llvm/SandboxIR/Module.h"
Expand Down Expand Up @@ -169,6 +170,15 @@ Value *Context::getOrCreateValueInternal(llvm::Value *LLVMV, llvm::User *U) {
return SBBB;
return nullptr;
}
// TODO: Move these checks after more common Values, like after Instruction.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the TODO rather than adding the code at the right place?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to make the diff more easy to read, doing one thing at a time .I will follow up with an NFC.

if (auto *MD = dyn_cast<llvm::MetadataAsValue>(LLVMV)) {
It->second = std::unique_ptr<OpaqueValue>(new OpaqueValue(MD, *this));
return It->second.get();
}
if (auto *Asm = dyn_cast<llvm::InlineAsm>(LLVMV)) {
It->second = std::unique_ptr<OpaqueValue>(new OpaqueValue(Asm, *this));
return It->second.get();
}
assert(isa<llvm::Instruction>(LLVMV) && "Expected Instruction");

switch (cast<llvm::Instruction>(LLVMV)->getOpcode()) {
Expand Down
23 changes: 23 additions & 0 deletions llvm/unittests/SandboxIR/SandboxIRTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6166,3 +6166,26 @@ define void @bar() {
// This should not crash, even though there is already a value for LLVMBar.
Ctx.createFunction(&LLVMBar);
}

TEST_F(SandboxIRTest, OpaqueValue) {
parseIR(C, R"IR(
declare void @bar(metadata)
define void @foo() {
call void @bar(metadata !1)
call void asm "asm", ""()
ret void
}
!1 = !{}
)IR");
Function &LLVMFoo = *M->getFunction("foo");
sandboxir::Context Ctx(C);
auto *F = Ctx.createFunction(&LLVMFoo);
auto *BB = &*F->begin();
auto It = BB->begin();
auto *Call = cast<sandboxir::CallInst>(&*It++);
auto *Op0 = Call->getOperand(0);
EXPECT_TRUE(isa<sandboxir::OpaqueValue>(Op0));
auto *Asm = cast<sandboxir::CallInst>(&*It++);
auto *AsmOp0 = Asm->getOperand(0);
EXPECT_TRUE(isa<sandboxir::OpaqueValue>(AsmOp0));
}