[llvm] 2f43272 - [SandboxIR] Implement getNumBits for Instructions (#110748)
via llvm-commits
llvm-commits at lists.llvm.org
Tue Oct 1 15:48:46 PDT 2024
Author: Sterling-Augustine
Date: 2024-10-01T15:48:41-07:00
New Revision: 2f4327294dccc27fc9d5febe71196f6f854d66ff
URL: https://github.com/llvm/llvm-project/commit/2f4327294dccc27fc9d5febe71196f6f854d66ff
DIFF: https://github.com/llvm/llvm-project/commit/2f4327294dccc27fc9d5febe71196f6f854d66ff.diff
LOG: [SandboxIR] Implement getNumBits for Instructions (#110748)
This also moves the other getNumbits test into UtilsTest.cc where it
belongs.
Added:
Modified:
llvm/include/llvm/SandboxIR/Instruction.h
llvm/include/llvm/SandboxIR/Utils.h
llvm/unittests/SandboxIR/SandboxIRTest.cpp
llvm/unittests/SandboxIR/UtilsTest.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/SandboxIR/Instruction.h b/llvm/include/llvm/SandboxIR/Instruction.h
index a34573a0bc1b01..fb3bcbdc99dbf3 100644
--- a/llvm/include/llvm/SandboxIR/Instruction.h
+++ b/llvm/include/llvm/SandboxIR/Instruction.h
@@ -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
diff --git a/llvm/include/llvm/SandboxIR/Utils.h b/llvm/include/llvm/SandboxIR/Utils.h
index 17fc837f555b8e..c25edd7dc74e0f 100644
--- a/llvm/include/llvm/SandboxIR/Utils.h
+++ b/llvm/include/llvm/SandboxIR/Utils.h
@@ -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) {
diff --git a/llvm/unittests/SandboxIR/SandboxIRTest.cpp b/llvm/unittests/SandboxIR/SandboxIRTest.cpp
index 7206ee34d36e3a..01940e7dccbf87 100644
--- a/llvm/unittests/SandboxIR/SandboxIRTest.cpp
+++ b/llvm/unittests/SandboxIR/SandboxIRTest.cpp
@@ -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) {
diff --git a/llvm/unittests/SandboxIR/UtilsTest.cpp b/llvm/unittests/SandboxIR/UtilsTest.cpp
index 7ce8f40f8985bf..2d76a20071d2ed 100644
--- a/llvm/unittests/SandboxIR/UtilsTest.cpp
+++ b/llvm/unittests/SandboxIR/UtilsTest.cpp
@@ -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);
+}
More information about the llvm-commits
mailing list