Skip to content

Commit 5375a55

Browse files
author
git apple-llvm automerger
committed
Merge commit '2f4327294dcc' from llvm.org/main into next
2 parents ff349f5 + 2f43272 commit 5375a55

File tree

4 files changed

+47
-20
lines changed

4 files changed

+47
-20
lines changed

llvm/include/llvm/SandboxIR/Instruction.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ class Instruction : public User {
9797

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

100+
const DataLayout &getDataLayout() const {
101+
return cast<llvm::Instruction>(Val)->getModule()->getDataLayout();
102+
}
100103
// Note that these functions below are calling into llvm::Instruction.
101104
// A sandbox IR instruction could introduce a new opcode that could change the
102105
// behavior of one of these functions. It is better that these functions are

llvm/include/llvm/SandboxIR/Utils.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ class Utils {
5656
return DL.getTypeSizeInBits(Ty->LLVMTy);
5757
}
5858

59+
/// \Returns the number of bits required to represent the operands or
60+
/// return value of \p I.
61+
static unsigned getNumBits(Instruction *I) {
62+
return I->getDataLayout().getTypeSizeInBits(getExpectedType(I)->LLVMTy);
63+
}
64+
5965
/// Equivalent to MemoryLocation::getOrNone(I).
6066
static std::optional<llvm::MemoryLocation>
6167
memoryLocationGetOrNone(const Instruction *I) {

llvm/unittests/SandboxIR/SandboxIRTest.cpp

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1512,26 +1512,6 @@ define void @bar(float %v, ptr %ptr) {
15121512
EXPECT_EQ(sandboxir::Utils::getExpectedValue(RetV), nullptr);
15131513
}
15141514

1515-
TEST_F(SandboxIRTest, GetNumBits) {
1516-
parseIR(C, R"IR(
1517-
define void @foo(float %arg0, double %arg1, i8 %arg2, i64 %arg3) {
1518-
bb0:
1519-
ret void
1520-
}
1521-
)IR");
1522-
llvm::Function &Foo = *M->getFunction("foo");
1523-
sandboxir::Context Ctx(C);
1524-
sandboxir::Function *F = Ctx.createFunction(&Foo);
1525-
const DataLayout &DL = M->getDataLayout();
1526-
// getNumBits for scalars
1527-
EXPECT_EQ(sandboxir::Utils::getNumBits(F->getArg(0), DL),
1528-
DL.getTypeSizeInBits(Type::getFloatTy(C)));
1529-
EXPECT_EQ(sandboxir::Utils::getNumBits(F->getArg(1), DL),
1530-
DL.getTypeSizeInBits(Type::getDoubleTy(C)));
1531-
EXPECT_EQ(sandboxir::Utils::getNumBits(F->getArg(2), DL), 8u);
1532-
EXPECT_EQ(sandboxir::Utils::getNumBits(F->getArg(3), DL), 64u);
1533-
}
1534-
15351515
TEST_F(SandboxIRTest, RAUW_RUWIf) {
15361516
parseIR(C, R"IR(
15371517
define void @foo(ptr %ptr) {

llvm/unittests/SandboxIR/UtilsTest.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,41 @@ define void @foo(ptr %ptr) {
135135
EXPECT_FALSE(sandboxir::Utils::atLowerAddress(L1, L0, SE, DL));
136136
EXPECT_FALSE(sandboxir::Utils::atLowerAddress(L3, V3L3, SE, DL));
137137
}
138+
139+
TEST_F(UtilsTest, GetNumBits) {
140+
parseIR(C, R"IR(
141+
define void @foo(float %arg0, double %arg1, i8 %arg2, i64 %arg3, ptr %arg4) {
142+
bb0:
143+
%ld0 = load float, ptr %arg4
144+
%ld1 = load double, ptr %arg4
145+
%ld2 = load i8, ptr %arg4
146+
%ld3 = load i64, ptr %arg4
147+
ret void
148+
}
149+
)IR");
150+
llvm::Function &Foo = *M->getFunction("foo");
151+
sandboxir::Context Ctx(C);
152+
sandboxir::Function *F = Ctx.createFunction(&Foo);
153+
const DataLayout &DL = M->getDataLayout();
154+
// getNumBits for scalars via the Value overload
155+
EXPECT_EQ(sandboxir::Utils::getNumBits(F->getArg(0), DL),
156+
DL.getTypeSizeInBits(Type::getFloatTy(C)));
157+
EXPECT_EQ(sandboxir::Utils::getNumBits(F->getArg(1), DL),
158+
DL.getTypeSizeInBits(Type::getDoubleTy(C)));
159+
EXPECT_EQ(sandboxir::Utils::getNumBits(F->getArg(2), DL), 8u);
160+
EXPECT_EQ(sandboxir::Utils::getNumBits(F->getArg(3), DL), 64u);
161+
162+
auto &BB = *F->begin();
163+
auto It = BB.begin();
164+
auto *L0 = cast<sandboxir::LoadInst>(&*It++);
165+
auto *L1 = cast<sandboxir::LoadInst>(&*It++);
166+
auto *L2 = cast<sandboxir::LoadInst>(&*It++);
167+
auto *L3 = cast<sandboxir::LoadInst>(&*It++);
168+
// getNumBits for scalars via the Instruction overload
169+
EXPECT_EQ(sandboxir::Utils::getNumBits(L0),
170+
DL.getTypeSizeInBits(Type::getFloatTy(C)));
171+
EXPECT_EQ(sandboxir::Utils::getNumBits(L1),
172+
DL.getTypeSizeInBits(Type::getDoubleTy(C)));
173+
EXPECT_EQ(sandboxir::Utils::getNumBits(L2), 8u);
174+
EXPECT_EQ(sandboxir::Utils::getNumBits(L3), 64u);
175+
}

0 commit comments

Comments
 (0)