Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
34 changes: 34 additions & 0 deletions llvm/unittests/SandboxIR/UtilsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,37 @@ 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) {
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 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);

// getNumBits for scalars via the Instruction overload
EXPECT_EQ(
sandboxir::Utils::getNumBits(cast<sandboxir::Instruction>(F->getArg(0))),
Copy link
Contributor

Choose a reason for hiding this comment

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

This should trigger an assertion because F->getArg(0) is not a sandboxir::Instruction.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I had been running the SandboxVectorizerTests, not the SandboxIR tests.

Fixed.

DL.getTypeSizeInBits(Type::getFloatTy(C)));
EXPECT_EQ(
sandboxir::Utils::getNumBits(cast<sandboxir::Instruction>(F->getArg(1))),
DL.getTypeSizeInBits(Type::getDoubleTy(C)));
EXPECT_EQ(
sandboxir::Utils::getNumBits(cast<sandboxir::Instruction>(F->getArg(2))),
8u);
EXPECT_EQ(
sandboxir::Utils::getNumBits(cast<sandboxir::Instruction>(F->getArg(3))),
64u);
}
Loading