Skip to content

[SandboxIR] Implement getNumBits for Instructions #110748

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 2 commits into from
Oct 1, 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
3 changes: 3 additions & 0 deletions llvm/include/llvm/SandboxIR/Instruction.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ class Instruction : public User {

const char *getOpcodeName() const { return getOpcodeName(Opc); }

const DataLayout &getDataLayout() const {
return cast<llvm::Instruction>(Val)->getModule()->getDataLayout();
}
// Note that these functions below are calling into llvm::Instruction.
// A sandbox IR instruction could introduce a new opcode that could change the
// behavior of one of these functions. It is better that these functions are
Expand Down
6 changes: 6 additions & 0 deletions llvm/include/llvm/SandboxIR/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ class Utils {
return DL.getTypeSizeInBits(Ty->LLVMTy);
}

/// \Returns the number of bits required to represent the operands or
/// return value of \p I.
static unsigned getNumBits(Instruction *I) {
return I->getDataLayout().getTypeSizeInBits(getExpectedType(I)->LLVMTy);
}

/// Equivalent to MemoryLocation::getOrNone(I).
static std::optional<llvm::MemoryLocation>
memoryLocationGetOrNone(const Instruction *I) {
Expand Down
20 changes: 0 additions & 20 deletions llvm/unittests/SandboxIR/SandboxIRTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1512,26 +1512,6 @@ define void @bar(float %v, ptr %ptr) {
EXPECT_EQ(sandboxir::Utils::getExpectedValue(RetV), nullptr);
}

TEST_F(SandboxIRTest, GetNumBits) {
parseIR(C, R"IR(
define void @foo(float %arg0, double %arg1, i8 %arg2, i64 %arg3) {
bb0:
ret void
}
)IR");
llvm::Function &Foo = *M->getFunction("foo");
sandboxir::Context Ctx(C);
sandboxir::Function *F = Ctx.createFunction(&Foo);
const DataLayout &DL = M->getDataLayout();
// getNumBits for scalars
EXPECT_EQ(sandboxir::Utils::getNumBits(F->getArg(0), DL),
DL.getTypeSizeInBits(Type::getFloatTy(C)));
EXPECT_EQ(sandboxir::Utils::getNumBits(F->getArg(1), DL),
DL.getTypeSizeInBits(Type::getDoubleTy(C)));
EXPECT_EQ(sandboxir::Utils::getNumBits(F->getArg(2), DL), 8u);
EXPECT_EQ(sandboxir::Utils::getNumBits(F->getArg(3), DL), 64u);
}

TEST_F(SandboxIRTest, RAUW_RUWIf) {
parseIR(C, R"IR(
define void @foo(ptr %ptr) {
Expand Down
38 changes: 38 additions & 0 deletions llvm/unittests/SandboxIR/UtilsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,41 @@ define void @foo(ptr %ptr) {
EXPECT_FALSE(sandboxir::Utils::atLowerAddress(L1, L0, SE, DL));
EXPECT_FALSE(sandboxir::Utils::atLowerAddress(L3, V3L3, SE, DL));
}

TEST_F(UtilsTest, GetNumBits) {
parseIR(C, R"IR(
define void @foo(float %arg0, double %arg1, i8 %arg2, i64 %arg3, ptr %arg4) {
bb0:
%ld0 = load float, ptr %arg4
%ld1 = load double, ptr %arg4
%ld2 = load i8, ptr %arg4
%ld3 = load i64, ptr %arg4
ret void
}
)IR");
llvm::Function &Foo = *M->getFunction("foo");
sandboxir::Context Ctx(C);
sandboxir::Function *F = Ctx.createFunction(&Foo);
const DataLayout &DL = M->getDataLayout();
// getNumBits for scalars via the Value overload
EXPECT_EQ(sandboxir::Utils::getNumBits(F->getArg(0), DL),
DL.getTypeSizeInBits(Type::getFloatTy(C)));
EXPECT_EQ(sandboxir::Utils::getNumBits(F->getArg(1), DL),
DL.getTypeSizeInBits(Type::getDoubleTy(C)));
EXPECT_EQ(sandboxir::Utils::getNumBits(F->getArg(2), DL), 8u);
EXPECT_EQ(sandboxir::Utils::getNumBits(F->getArg(3), DL), 64u);

auto &BB = *F->begin();
auto It = BB.begin();
auto *L0 = cast<sandboxir::LoadInst>(&*It++);
auto *L1 = cast<sandboxir::LoadInst>(&*It++);
auto *L2 = cast<sandboxir::LoadInst>(&*It++);
auto *L3 = cast<sandboxir::LoadInst>(&*It++);
// getNumBits for scalars via the Instruction overload
EXPECT_EQ(sandboxir::Utils::getNumBits(L0),
DL.getTypeSizeInBits(Type::getFloatTy(C)));
EXPECT_EQ(sandboxir::Utils::getNumBits(L1),
DL.getTypeSizeInBits(Type::getDoubleTy(C)));
EXPECT_EQ(sandboxir::Utils::getNumBits(L2), 8u);
EXPECT_EQ(sandboxir::Utils::getNumBits(L3), 64u);
}
Loading