[llvm] [SandboxIR] Implement getNumBits for Instructions (PR #110748)
via llvm-commits
llvm-commits at lists.llvm.org
Tue Oct 1 15:42:31 PDT 2024
https://github.com/Sterling-Augustine updated https://github.com/llvm/llvm-project/pull/110748
>From c7a4448aec897b98369f9566d911ecaae53de960 Mon Sep 17 00:00:00 2001
From: Sterling Augustine <saugustine at google.com>
Date: Tue, 1 Oct 2024 14:45:40 -0700
Subject: [PATCH 1/2] [SandboxIR] Implement getNumBits for Instructions
---
llvm/include/llvm/SandboxIR/Instruction.h | 3 ++
llvm/include/llvm/SandboxIR/Utils.h | 6 ++++
llvm/unittests/SandboxIR/SandboxIRTest.cpp | 20 -------------
llvm/unittests/SandboxIR/UtilsTest.cpp | 34 ++++++++++++++++++++++
4 files changed, 43 insertions(+), 20 deletions(-)
diff --git a/llvm/include/llvm/SandboxIR/Instruction.h b/llvm/include/llvm/SandboxIR/Instruction.h
index f5f5bb5c4443c2..1ab65831fbc8b7 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 3bd520f3174c26..885ed9c44775d8 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..44b23d866cec5b 100644
--- a/llvm/unittests/SandboxIR/UtilsTest.cpp
+++ b/llvm/unittests/SandboxIR/UtilsTest.cpp
@@ -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))),
+ 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);
+}
>From 82bb9cabd6c04ee77629dd2eae30ddc599b1faa6 Mon Sep 17 00:00:00 2001
From: Sterling Augustine <saugustine at google.com>
Date: Tue, 1 Oct 2024 15:41:58 -0700
Subject: [PATCH 2/2] Now with a proper test
---
llvm/unittests/SandboxIR/UtilsTest.cpp | 30 +++++++++++++++-----------
1 file changed, 17 insertions(+), 13 deletions(-)
diff --git a/llvm/unittests/SandboxIR/UtilsTest.cpp b/llvm/unittests/SandboxIR/UtilsTest.cpp
index 44b23d866cec5b..2d76a20071d2ed 100644
--- a/llvm/unittests/SandboxIR/UtilsTest.cpp
+++ b/llvm/unittests/SandboxIR/UtilsTest.cpp
@@ -138,8 +138,12 @@ define void @foo(ptr %ptr) {
TEST_F(UtilsTest, GetNumBits) {
parseIR(C, R"IR(
-define void @foo(float %arg0, double %arg1, i8 %arg2, i64 %arg3) {
+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");
@@ -155,17 +159,17 @@ define void @foo(float %arg0, double %arg1, i8 %arg2, i64 %arg3) {
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(cast<sandboxir::Instruction>(F->getArg(0))),
- 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);
+ 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